ggml-cuda.cu 489 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347
  1. #include "ggml-cuda.h"
  2. #include "ggml.h"
  3. #include "ggml-backend-impl.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <atomic>
  7. #include <cinttypes>
  8. #include <cstddef>
  9. #include <cstdint>
  10. #include <float.h>
  11. #include <limits>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <string>
  15. #include <vector>
  16. #include <map>
  17. #include <array>
  18. // stringize macro for converting __CUDA_ARCH_LIST__ (list of integers) to string
  19. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  20. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  21. #if defined(GGML_USE_HIPBLAS)
  22. #include <hip/hip_runtime.h>
  23. #include <hipblas/hipblas.h>
  24. #include <hip/hip_fp16.h>
  25. #ifdef __HIP_PLATFORM_AMD__
  26. // for rocblas_initialize()
  27. #include "rocblas/rocblas.h"
  28. #endif // __HIP_PLATFORM_AMD__
  29. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  30. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  31. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  32. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  33. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  34. #define CUBLAS_OP_N HIPBLAS_OP_N
  35. #define CUBLAS_OP_T HIPBLAS_OP_T
  36. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  37. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  38. #define CUDA_R_16F HIPBLAS_R_16F
  39. #define CUDA_R_32F HIPBLAS_R_32F
  40. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  41. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  42. #define cublasCreate hipblasCreate
  43. #define cublasGemmEx hipblasGemmEx
  44. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  45. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  46. #define cublasHandle_t hipblasHandle_t
  47. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  48. #define cublasSetStream hipblasSetStream
  49. #define cublasSgemm hipblasSgemm
  50. #define cublasStatus_t hipblasStatus_t
  51. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  52. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  53. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  54. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  55. #define cudaDeviceProp hipDeviceProp_t
  56. #define cudaDeviceSynchronize hipDeviceSynchronize
  57. #define cudaError_t hipError_t
  58. #define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
  59. #define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
  60. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  61. #define cudaEventDisableTiming hipEventDisableTiming
  62. #define cudaEventRecord hipEventRecord
  63. #define cudaEvent_t hipEvent_t
  64. #define cudaEventDestroy hipEventDestroy
  65. #define cudaFree hipFree
  66. #define cudaFreeHost hipHostFree
  67. #define cudaGetDevice hipGetDevice
  68. #define cudaGetDeviceCount hipGetDeviceCount
  69. #define cudaGetDeviceProperties hipGetDeviceProperties
  70. #define cudaGetErrorString hipGetErrorString
  71. #define cudaGetLastError hipGetLastError
  72. #ifdef GGML_HIP_UMA
  73. #define cudaMalloc hipMallocManaged
  74. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  75. #else
  76. #define cudaMalloc hipMalloc
  77. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  78. #endif
  79. #define cudaMemcpy hipMemcpy
  80. #define cudaMemcpyAsync hipMemcpyAsync
  81. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  82. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  83. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  84. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  85. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  86. #define cudaMemcpyKind hipMemcpyKind
  87. #define cudaMemset hipMemset
  88. #define cudaMemsetAsync hipMemsetAsync
  89. #define cudaMemGetInfo hipMemGetInfo
  90. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  91. #define cudaSetDevice hipSetDevice
  92. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  93. #define cudaStreamFireAndForget hipStreamFireAndForget
  94. #define cudaStreamNonBlocking hipStreamNonBlocking
  95. #define cudaStreamSynchronize hipStreamSynchronize
  96. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  97. #define cudaStream_t hipStream_t
  98. #define cudaSuccess hipSuccess
  99. #define __trap abort
  100. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  101. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  102. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  103. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  104. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  105. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  106. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  107. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  108. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  109. #else
  110. #include <cuda_runtime.h>
  111. #include <cuda.h>
  112. #include <cublas_v2.h>
  113. #include <cuda_fp16.h>
  114. #if CUDART_VERSION < 11020
  115. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  116. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  117. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  118. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  119. #define cublasComputeType_t cudaDataType_t
  120. #endif // CUDART_VERSION < 11020
  121. #endif // defined(GGML_USE_HIPBLAS)
  122. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  123. #define CC_PASCAL 600
  124. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  125. #define CC_VOLTA 700
  126. #define CC_OFFSET_AMD 1000000
  127. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  128. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  129. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  130. #define GGML_CUDA_MAX_NODES 8192
  131. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  132. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  133. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  134. // - 7B quantum model: +100-200 MB
  135. // - 13B quantum model: +200-400 MB
  136. //
  137. //#define GGML_CUDA_FORCE_MMQ
  138. // TODO: improve this to be correct for more hardware
  139. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  140. #if !defined(GGML_CUDA_FORCE_MMQ)
  141. #define CUDA_USE_TENSOR_CORES
  142. #endif
  143. #define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
  144. #define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
  145. #if defined(GGML_USE_HIPBLAS)
  146. #define __CUDA_ARCH__ 1300
  147. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  148. defined(__gfx1150__) || defined(__gfx1151__)
  149. #define RDNA3
  150. #endif
  151. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  152. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  153. #define RDNA2
  154. #endif
  155. #ifndef __has_builtin
  156. #define __has_builtin(x) 0
  157. #endif
  158. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  159. typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4)));
  160. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  161. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  162. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  163. #if __has_builtin(__builtin_elementwise_sub_sat)
  164. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  165. return reinterpret_cast<const int &>(c);
  166. #else
  167. int8x4_t c;
  168. int16_t tmp;
  169. #pragma unroll
  170. for (int i = 0; i < 4; i++) {
  171. tmp = va[i] - vb[i];
  172. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  173. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  174. c[i] = tmp;
  175. }
  176. return reinterpret_cast<int &>(c);
  177. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  178. }
  179. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  180. return __vsubss4(a, b);
  181. }
  182. static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) {
  183. const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a);
  184. const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b);
  185. unsigned int c;
  186. uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c);
  187. #pragma unroll
  188. for (int i = 0; i < 4; ++i) {
  189. vc[i] = va[i] == vb[i] ? 0xff : 0x00;
  190. }
  191. return c;
  192. }
  193. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  194. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  195. c = __builtin_amdgcn_sdot4(a, b, c, false);
  196. #elif defined(RDNA3)
  197. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  198. #elif defined(__gfx1010__) || defined(__gfx900__)
  199. int tmp1;
  200. int tmp2;
  201. asm("\n \
  202. 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 \
  203. 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 \
  204. v_add3_u32 %0, %1, %2, %0 \n \
  205. 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 \
  206. 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 \
  207. v_add3_u32 %0, %1, %2, %0 \n \
  208. "
  209. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  210. : "v"(a), "v"(b)
  211. );
  212. #else
  213. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  214. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  215. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  216. #endif
  217. return c;
  218. }
  219. #endif // defined(GGML_USE_HIPBLAS)
  220. #if defined(_MSC_VER)
  221. #pragma warning(disable: 4244 4267) // possible loss of data
  222. #endif
  223. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  224. [[noreturn]]
  225. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  226. int id = -1; // in case cudaGetDevice fails
  227. cudaGetDevice(&id);
  228. fprintf(stderr, "CUDA error: %s\n", msg);
  229. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  230. fprintf(stderr, " %s\n", stmt);
  231. // abort with GGML_ASSERT to get a stack trace
  232. GGML_ASSERT(!"CUDA error");
  233. }
  234. #define CUDA_CHECK_GEN(err, success, error_fn) \
  235. do { \
  236. auto err_ = (err); \
  237. if (err_ != (success)) { \
  238. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  239. } \
  240. } while (0)
  241. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  242. #if CUDART_VERSION >= 12000
  243. static const char * cublas_get_error_str(const cublasStatus_t err) {
  244. return cublasGetStatusString(err);
  245. }
  246. #else
  247. static const char * cublas_get_error_str(const cublasStatus_t err) {
  248. switch (err) {
  249. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  250. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  251. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  252. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  253. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  254. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  255. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  256. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  257. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  258. default: return "unknown error";
  259. }
  260. }
  261. #endif // CUDART_VERSION >= 12000
  262. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  263. #if !defined(GGML_USE_HIPBLAS)
  264. static const char * cu_get_error_str(CUresult err) {
  265. const char * err_str;
  266. cuGetErrorString(err, &err_str);
  267. return err_str;
  268. }
  269. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  270. #endif
  271. #if CUDART_VERSION >= 11100
  272. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  273. #else
  274. #define GGML_CUDA_ASSUME(x)
  275. #endif // CUDART_VERSION >= 11100
  276. #ifdef GGML_CUDA_F16
  277. typedef half dfloat; // dequantize float
  278. typedef half2 dfloat2;
  279. #else
  280. typedef float dfloat; // dequantize float
  281. typedef float2 dfloat2;
  282. #endif //GGML_CUDA_F16
  283. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  284. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  285. int x32 = 0;
  286. x32 |= x16[0] << 0;
  287. x32 |= x16[1] << 16;
  288. return x32;
  289. }
  290. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  291. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  292. int x32 = 0;
  293. x32 |= x16[0] << 0;
  294. x32 |= x16[1] << 16;
  295. return x32;
  296. }
  297. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  298. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  299. }
  300. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  301. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  302. }
  303. template<typename T>
  304. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  305. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  306. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  307. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  308. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  309. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  310. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  311. typedef void (*ggml_cuda_op_mul_mat_t)(
  312. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  313. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  314. const int64_t src1_padded_row_size, cudaStream_t stream);
  315. typedef void (*ggml_cuda_op_flatten_t)(
  316. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  317. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  318. // QK = number of values after dequantization
  319. // QR = QK / number of values before dequantization
  320. // QI = number of 32 bit integers before dequantization
  321. #define QK4_0 32
  322. #define QR4_0 2
  323. #define QI4_0 (QK4_0 / (4 * QR4_0))
  324. typedef struct {
  325. half d; // delta
  326. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  327. } block_q4_0;
  328. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  329. #define QK4_1 32
  330. #define QR4_1 2
  331. #define QI4_1 (QK4_1 / (4 * QR4_1))
  332. typedef struct {
  333. half2 dm; // dm.x = delta, dm.y = min
  334. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  335. } block_q4_1;
  336. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  337. #define QK5_0 32
  338. #define QR5_0 2
  339. #define QI5_0 (QK5_0 / (4 * QR5_0))
  340. typedef struct {
  341. half d; // delta
  342. uint8_t qh[4]; // 5-th bit of quants
  343. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  344. } block_q5_0;
  345. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  346. #define QK5_1 32
  347. #define QR5_1 2
  348. #define QI5_1 (QK5_1 / (4 * QR5_1))
  349. typedef struct {
  350. half2 dm; // dm.x = delta, dm.y = min
  351. uint8_t qh[4]; // 5-th bit of quants
  352. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  353. } block_q5_1;
  354. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  355. #define QK8_0 32
  356. #define QR8_0 1
  357. #define QI8_0 (QK8_0 / (4 * QR8_0))
  358. typedef struct {
  359. half d; // delta
  360. int8_t qs[QK8_0]; // quants
  361. } block_q8_0;
  362. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  363. #define QK8_1 32
  364. #define QR8_1 1
  365. #define QI8_1 (QK8_1 / (4 * QR8_1))
  366. typedef struct {
  367. half2 ds; // ds.x = delta, ds.y = sum
  368. int8_t qs[QK8_0]; // quants
  369. } block_q8_1;
  370. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  371. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  372. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  373. typedef void (*load_tiles_cuda_t)(
  374. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  375. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  376. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  377. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  378. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  379. //================================= k-quants
  380. #ifdef GGML_QKK_64
  381. #define QK_K 64
  382. #define K_SCALE_SIZE 4
  383. #else
  384. #define QK_K 256
  385. #define K_SCALE_SIZE 12
  386. #endif
  387. #define QR2_K 4
  388. #define QI2_K (QK_K / (4*QR2_K))
  389. typedef struct {
  390. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  391. uint8_t qs[QK_K/4]; // quants
  392. half2 dm; // super-block scale for quantized scales/mins
  393. } block_q2_K;
  394. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  395. #define QR3_K 4
  396. #define QI3_K (QK_K / (4*QR3_K))
  397. typedef struct {
  398. uint8_t hmask[QK_K/8]; // quants - high bit
  399. uint8_t qs[QK_K/4]; // quants - low 2 bits
  400. #ifdef GGML_QKK_64
  401. uint8_t scales[2]; // scales, quantized with 8 bits
  402. #else
  403. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  404. #endif
  405. half d; // super-block scale
  406. } block_q3_K;
  407. //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");
  408. #define QR4_K 2
  409. #define QI4_K (QK_K / (4*QR4_K))
  410. #ifdef GGML_QKK_64
  411. typedef struct {
  412. half dm[2]; // super-block scales/mins
  413. uint8_t scales[2]; // 4-bit block scales/mins
  414. uint8_t qs[QK_K/2]; // 4--bit quants
  415. } block_q4_K;
  416. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  417. #else
  418. typedef struct {
  419. half2 dm; // super-block scale for quantized scales/mins
  420. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  421. uint8_t qs[QK_K/2]; // 4--bit quants
  422. } block_q4_K;
  423. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  424. #endif
  425. #define QR5_K 2
  426. #define QI5_K (QK_K / (4*QR5_K))
  427. #ifdef GGML_QKK_64
  428. typedef struct {
  429. half d; // super-block scale
  430. int8_t scales[QK_K/16]; // block scales
  431. uint8_t qh[QK_K/8]; // quants, high bit
  432. uint8_t qs[QK_K/2]; // quants, low 4 bits
  433. } block_q5_K;
  434. 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");
  435. #else
  436. typedef struct {
  437. half2 dm; // super-block scale for quantized scales/mins
  438. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  439. uint8_t qh[QK_K/8]; // quants, high bit
  440. uint8_t qs[QK_K/2]; // quants, low 4 bits
  441. } block_q5_K;
  442. 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");
  443. #endif
  444. #define QR6_K 2
  445. #define QI6_K (QK_K / (4*QR6_K))
  446. typedef struct {
  447. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  448. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  449. int8_t scales[QK_K/16]; // scales
  450. half d; // delta
  451. } block_q6_K;
  452. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  453. #define QR2_XXS 8
  454. #define QI2_XXS (QK_K / (4*QR2_XXS))
  455. typedef struct {
  456. half d;
  457. uint16_t qs[QK_K/8];
  458. } block_iq2_xxs;
  459. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  460. #define QR2_XS 8
  461. #define QI2_XS (QK_K / (4*QR2_XS))
  462. typedef struct {
  463. half d;
  464. uint16_t qs[QK_K/8];
  465. uint8_t scales[QK_K/32];
  466. } block_iq2_xs;
  467. 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");
  468. // 2.5625 bpw quants
  469. #define QR2_S 8
  470. #define QI2_S (QK_K / (4*QR2_S))
  471. typedef struct {
  472. half d;
  473. uint8_t qs[QK_K/4];
  474. uint8_t qh[QK_K/32];
  475. uint8_t scales[QK_K/32];
  476. } block_iq2_s;
  477. static_assert(sizeof(block_iq2_s) == sizeof(ggml_fp16_t) + QK_K/4 + QK_K/16, "wrong iq2_s block size/padding");
  478. #define QR3_XXS 8
  479. #define QI3_XXS (QK_K / (4*QR3_XXS))
  480. typedef struct {
  481. half d;
  482. uint8_t qs[3*(QK_K/8)];
  483. } block_iq3_xxs;
  484. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_fp16_t) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  485. #define QR3_XS 8
  486. #define QI3_XS (QK_K / (4*QR3_XS))
  487. #if QK_K == 64
  488. #define IQ3S_N_SCALE 2
  489. #else
  490. #define IQ3S_N_SCALE QK_K/64
  491. #endif
  492. typedef struct {
  493. half d;
  494. uint8_t qs[QK_K/4];
  495. uint8_t qh[QK_K/32];
  496. uint8_t signs[QK_K/8];
  497. uint8_t scales[IQ3S_N_SCALE];
  498. } block_iq3_s;
  499. static_assert(sizeof(block_iq3_s) == sizeof(ggml_fp16_t) + 13*(QK_K/32) + IQ3S_N_SCALE, "wrong iq3_s block size/padding");
  500. #define QR1_S 8
  501. #define QI1_S (QK_K / (4*QR1_S))
  502. typedef struct {
  503. half d;
  504. uint8_t qs[QK_K/8];
  505. uint8_t scales[QK_K/16];
  506. } block_iq1_s;
  507. static_assert(sizeof(block_iq1_s) == sizeof(ggml_fp16_t) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  508. #define QK4_NL 32
  509. #define QR4_NL 2
  510. #define QI4_NL (QK4_NL / (4*QR4_NL))
  511. typedef struct {
  512. half d;
  513. uint8_t qs[QK4_NL/2];
  514. } block_iq4_nl;
  515. static_assert(sizeof(block_iq4_nl) == sizeof(ggml_fp16_t) + QK4_NL/2, "wrong iq4_nl block size/padding");
  516. #if QK_K == 64
  517. #define block_iq4_xs block_iq4_nl
  518. #define QR4_XS QR4_NL
  519. #define QI4_XS QI4_NL
  520. #else
  521. // QR4_XS = 8 is very slightly faster than QR4_XS = 4
  522. #define QR4_XS 8
  523. #define QI4_XS (QK_K / (4*QR4_XS))
  524. typedef struct {
  525. half d;
  526. uint16_t scales_h;
  527. uint8_t scales_l[QK_K/64];
  528. uint8_t qs[QK_K/2];
  529. } block_iq4_xs;
  530. static_assert(sizeof(block_iq4_xs) == sizeof(ggml_fp16_t) + sizeof(uint16_t) + QK_K/64 + QK_K/2, "wrong iq4_xs block size/padding");
  531. #endif
  532. #define WARP_SIZE 32
  533. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  534. #define CUDA_GELU_BLOCK_SIZE 256
  535. #define CUDA_SILU_BLOCK_SIZE 256
  536. #define CUDA_TANH_BLOCK_SIZE 256
  537. #define CUDA_RELU_BLOCK_SIZE 256
  538. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  539. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  540. #define CUDA_SQR_BLOCK_SIZE 256
  541. #define CUDA_CPY_BLOCK_SIZE 32
  542. #define CUDA_SCALE_BLOCK_SIZE 256
  543. #define CUDA_CLAMP_BLOCK_SIZE 256
  544. #define CUDA_ROPE_BLOCK_SIZE 256
  545. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  546. #define CUDA_ALIBI_BLOCK_SIZE 32
  547. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  548. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  549. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  550. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  551. #define CUDA_UPSCALE_BLOCK_SIZE 256
  552. #define CUDA_CONCAT_BLOCK_SIZE 256
  553. #define CUDA_PAD_BLOCK_SIZE 256
  554. #define CUDA_ACC_BLOCK_SIZE 256
  555. #define CUDA_IM2COL_BLOCK_SIZE 256
  556. #define CUDA_POOL2D_BLOCK_SIZE 256
  557. #define CUDA_Q8_0_NE_ALIGN 2048
  558. // dmmv = dequantize_mul_mat_vec
  559. #ifndef GGML_CUDA_DMMV_X
  560. #define GGML_CUDA_DMMV_X 32
  561. #endif
  562. #ifndef GGML_CUDA_MMV_Y
  563. #define GGML_CUDA_MMV_Y 1
  564. #endif
  565. #ifndef K_QUANTS_PER_ITERATION
  566. #define K_QUANTS_PER_ITERATION 2
  567. #else
  568. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  569. #endif
  570. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  571. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  572. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  573. #define MUL_MAT_SRC1_COL_STRIDE 128
  574. #define MAX_STREAMS 8
  575. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  576. struct ggml_tensor_extra_gpu {
  577. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  578. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  579. };
  580. // this is faster on Windows
  581. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  582. static void ggml_cuda_set_device(const int device) {
  583. int current_device;
  584. CUDA_CHECK(cudaGetDevice(&current_device));
  585. if (device == current_device) {
  586. return;
  587. }
  588. CUDA_CHECK(cudaSetDevice(device));
  589. }
  590. static int g_device_count = -1;
  591. static int g_main_device = 0;
  592. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  593. struct cuda_device_capabilities {
  594. int cc; // compute capability
  595. size_t smpb; // max. shared memory per block
  596. bool vmm; // virtual memory support
  597. size_t vmm_granularity; // granularity of virtual memory
  598. };
  599. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  600. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  601. [[noreturn]]
  602. static __device__ void no_device_code(
  603. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  604. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  605. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  606. file_name, line, function_name, arch);
  607. (void) arch_list;
  608. #else
  609. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  610. file_name, line, function_name, arch, arch_list);
  611. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  612. __trap();
  613. (void) no_device_code; // suppress unused function warning
  614. }
  615. #ifdef __CUDA_ARCH__
  616. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  617. #else
  618. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  619. #endif // __CUDA_ARCH__
  620. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  621. #pragma unroll
  622. for (int mask = 16; mask > 0; mask >>= 1) {
  623. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  624. }
  625. return x;
  626. }
  627. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  628. #pragma unroll
  629. for (int mask = 16; mask > 0; mask >>= 1) {
  630. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  631. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  632. }
  633. return a;
  634. }
  635. #ifdef GGML_CUDA_F16
  636. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  637. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  638. #pragma unroll
  639. for (int mask = 16; mask > 0; mask >>= 1) {
  640. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  641. }
  642. return a;
  643. #else
  644. (void) a;
  645. NO_DEVICE_CODE;
  646. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  647. }
  648. #endif // GGML_CUDA_F16
  649. static __device__ __forceinline__ float warp_reduce_max(float x) {
  650. #pragma unroll
  651. for (int mask = 16; mask > 0; mask >>= 1) {
  652. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  653. }
  654. return x;
  655. }
  656. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  657. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  658. //#pragma unroll
  659. // for (int mask = 16; mask > 0; mask >>= 1) {
  660. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  661. // }
  662. // return x;
  663. //#else
  664. // (void) x;
  665. // NO_DEVICE_CODE;
  666. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  667. //}
  668. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  669. return b;
  670. GGML_UNUSED(a);
  671. }
  672. static __device__ __forceinline__ float op_add(const float a, const float b) {
  673. return a + b;
  674. }
  675. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  676. return a * b;
  677. }
  678. static __device__ __forceinline__ float op_div(const float a, const float b) {
  679. return a / b;
  680. }
  681. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  682. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  683. int ne0, int ne1, int ne2, int ne3,
  684. int ne10, int ne11, int ne12, int ne13,
  685. /*int s0, */ int s1, int s2, int s3,
  686. /*int s10,*/ int s11, int s12, int s13) {
  687. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  688. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  689. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  690. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  691. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  692. return;
  693. }
  694. const int i11 = i1 % ne11;
  695. const int i12 = i2 % ne12;
  696. const int i13 = i3 % ne13;
  697. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  698. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  699. const size_t i_dst = i_src0;
  700. const src0_t * src0_row = src0 + i_src0;
  701. const src1_t * src1_row = src1 + i_src1;
  702. dst_t * dst_row = dst + i_dst;
  703. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  704. const int i10 = i0 % ne10;
  705. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  706. }
  707. }
  708. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  709. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  710. int ne0, int ne1, int ne2, int ne3,
  711. int ne10, int ne11, int ne12, int ne13,
  712. /*int s0, */ int s1, int s2, int s3,
  713. /*int s10,*/ int s11, int s12, int s13) {
  714. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  715. const int i3 = i/(ne2*ne1*ne0);
  716. const int i2 = (i/(ne1*ne0)) % ne2;
  717. const int i1 = (i/ne0) % ne1;
  718. const int i0 = i % ne0;
  719. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  720. return;
  721. }
  722. const int i11 = i1 % ne11;
  723. const int i12 = i2 % ne12;
  724. const int i13 = i3 % ne13;
  725. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  726. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  727. const size_t i_dst = i_src0;
  728. const src0_t * src0_row = src0 + i_src0;
  729. const src1_t * src1_row = src1 + i_src1;
  730. dst_t * dst_row = dst + i_dst;
  731. const int i10 = i0 % ne10;
  732. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  733. }
  734. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  735. const int ne10, const int ne11, const int ne12,
  736. const int nb1, const int nb2, int offset) {
  737. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  738. if (i >= ne) {
  739. return;
  740. }
  741. int src1_idx = i - offset;
  742. int oz = src1_idx / nb2;
  743. int oy = (src1_idx - (oz * nb2)) / nb1;
  744. int ox = src1_idx % nb1;
  745. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  746. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  747. } else {
  748. dst[i] = x[i];
  749. }
  750. }
  751. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  752. const float GELU_COEF_A = 0.044715f;
  753. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  754. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  755. if (i >= k) {
  756. return;
  757. }
  758. float xi = x[i];
  759. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  760. }
  761. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  762. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  763. if (i >= k) {
  764. return;
  765. }
  766. dst[i] = x[i] / (1.0f + expf(-x[i]));
  767. }
  768. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  769. const float GELU_QUICK_COEF = -1.702f;
  770. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  771. if (i >= k) {
  772. return;
  773. }
  774. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  775. }
  776. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  777. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  778. if (i >= k) {
  779. return;
  780. }
  781. dst[i] = tanhf(x[i]);
  782. }
  783. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  784. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  785. if (i >= k) {
  786. return;
  787. }
  788. dst[i] = fmaxf(x[i], 0);
  789. }
  790. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  791. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  792. if (i >= k) {
  793. return;
  794. }
  795. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  796. }
  797. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  798. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  799. if (i >= k) {
  800. return;
  801. }
  802. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  803. }
  804. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  805. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  806. if (i >= k) {
  807. return;
  808. }
  809. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  810. }
  811. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  812. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  813. if (i >= k) {
  814. return;
  815. }
  816. dst[i] = x[i] * x[i];
  817. }
  818. template <int block_size>
  819. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  820. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  821. const int tid = threadIdx.x;
  822. float2 mean_var = make_float2(0.f, 0.f);
  823. for (int col = tid; col < ncols; col += block_size) {
  824. const float xi = x[row*ncols + col];
  825. mean_var.x += xi;
  826. mean_var.y += xi * xi;
  827. }
  828. // sum up partial sums
  829. mean_var = warp_reduce_sum(mean_var);
  830. if (block_size > WARP_SIZE) {
  831. __shared__ float2 s_sum[32];
  832. int warp_id = threadIdx.x / WARP_SIZE;
  833. int lane_id = threadIdx.x % WARP_SIZE;
  834. if (lane_id == 0) {
  835. s_sum[warp_id] = mean_var;
  836. }
  837. __syncthreads();
  838. mean_var = s_sum[lane_id];
  839. mean_var = warp_reduce_sum(mean_var);
  840. }
  841. const float mean = mean_var.x / ncols;
  842. const float var = mean_var.y / ncols - mean * mean;
  843. const float inv_std = rsqrtf(var + eps);
  844. for (int col = tid; col < ncols; col += block_size) {
  845. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  846. }
  847. }
  848. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  849. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  850. if (nidx >= ne0) {
  851. return;
  852. }
  853. // operation
  854. int offset_dst =
  855. nidx +
  856. blockIdx.y * ne0 +
  857. blockIdx.z * ne0 * gridDim.y;
  858. if (blockIdx.z < ne02) { // src0
  859. int offset_src =
  860. nidx +
  861. blockIdx.y * ne0 +
  862. blockIdx.z * ne0 * gridDim.y;
  863. dst[offset_dst] = x[offset_src];
  864. } else {
  865. int offset_src =
  866. nidx +
  867. blockIdx.y * ne0 +
  868. (blockIdx.z - ne02) * ne0 * gridDim.y;
  869. dst[offset_dst] = y[offset_src];
  870. }
  871. }
  872. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  873. int ne0 = ne00 * scale_factor;
  874. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  875. if (nidx >= ne0) {
  876. return;
  877. }
  878. // operation
  879. int i00 = nidx / scale_factor;
  880. int i01 = blockIdx.y / scale_factor;
  881. int offset_src =
  882. i00 +
  883. i01 * ne00 +
  884. blockIdx.z * nb02;
  885. int offset_dst =
  886. nidx +
  887. blockIdx.y * ne0 +
  888. blockIdx.z * ne0 * gridDim.y;
  889. dst[offset_dst] = x[offset_src];
  890. }
  891. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  892. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  893. if (nidx >= ne0) {
  894. return;
  895. }
  896. // operation
  897. int offset_dst =
  898. nidx +
  899. blockIdx.y * ne0 +
  900. blockIdx.z * ne0 * gridDim.y;
  901. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  902. int offset_src =
  903. nidx +
  904. blockIdx.y * ne00 +
  905. blockIdx.z * ne00 * ne01;
  906. dst[offset_dst] = x[offset_src];
  907. } else {
  908. dst[offset_dst] = 0.0f;
  909. }
  910. }
  911. template <int block_size>
  912. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  913. int start = blockIdx.x * group_size;
  914. int end = start + group_size;
  915. start += threadIdx.x;
  916. if (end >= ne_elements) {
  917. end = ne_elements;
  918. }
  919. float tmp = 0.0f; // partial sum for thread in warp
  920. for (int j = start; j < end; j += block_size) {
  921. tmp += x[j];
  922. }
  923. tmp = warp_reduce_sum(tmp);
  924. if (block_size > WARP_SIZE) {
  925. __shared__ float s_sum[32];
  926. int warp_id = threadIdx.x / WARP_SIZE;
  927. int lane_id = threadIdx.x % WARP_SIZE;
  928. if (lane_id == 0) {
  929. s_sum[warp_id] = tmp;
  930. }
  931. __syncthreads();
  932. tmp = s_sum[lane_id];
  933. tmp = warp_reduce_sum(tmp);
  934. }
  935. float mean = tmp / group_size;
  936. tmp = 0.0f;
  937. for (int j = start; j < end; j += block_size) {
  938. float xi = x[j] - mean;
  939. dst[j] = xi;
  940. tmp += xi * xi;
  941. }
  942. tmp = warp_reduce_sum(tmp);
  943. if (block_size > WARP_SIZE) {
  944. __shared__ float s_sum[32];
  945. int warp_id = threadIdx.x / WARP_SIZE;
  946. int lane_id = threadIdx.x % WARP_SIZE;
  947. if (lane_id == 0) {
  948. s_sum[warp_id] = tmp;
  949. }
  950. __syncthreads();
  951. tmp = s_sum[lane_id];
  952. tmp = warp_reduce_sum(tmp);
  953. }
  954. float variance = tmp / group_size;
  955. float scale = rsqrtf(variance + eps);
  956. for (int j = start; j < end; j += block_size) {
  957. dst[j] *= scale;
  958. }
  959. }
  960. template <int block_size>
  961. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  962. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  963. const int tid = threadIdx.x;
  964. float tmp = 0.0f; // partial sum for thread in warp
  965. for (int col = tid; col < ncols; col += block_size) {
  966. const float xi = x[row*ncols + col];
  967. tmp += xi * xi;
  968. }
  969. // sum up partial sums
  970. tmp = warp_reduce_sum(tmp);
  971. if (block_size > WARP_SIZE) {
  972. __shared__ float s_sum[32];
  973. int warp_id = threadIdx.x / WARP_SIZE;
  974. int lane_id = threadIdx.x % WARP_SIZE;
  975. if (lane_id == 0) {
  976. s_sum[warp_id] = tmp;
  977. }
  978. __syncthreads();
  979. tmp = s_sum[lane_id];
  980. tmp = warp_reduce_sum(tmp);
  981. }
  982. const float mean = tmp / ncols;
  983. const float scale = rsqrtf(mean + eps);
  984. for (int col = tid; col < ncols; col += block_size) {
  985. dst[row*ncols + col] = scale * x[row*ncols + col];
  986. }
  987. }
  988. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  989. const block_q4_0 * x = (const block_q4_0 *) vx;
  990. const dfloat d = x[ib].d;
  991. const int vui = x[ib].qs[iqs];
  992. v.x = vui & 0xF;
  993. v.y = vui >> 4;
  994. #ifdef GGML_CUDA_F16
  995. v = __hsub2(v, {8.0f, 8.0f});
  996. v = __hmul2(v, {d, d});
  997. #else
  998. v.x = (v.x - 8.0f) * d;
  999. v.y = (v.y - 8.0f) * d;
  1000. #endif // GGML_CUDA_F16
  1001. }
  1002. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1003. const block_q4_1 * x = (const block_q4_1 *) vx;
  1004. const dfloat d = __low2half(x[ib].dm);
  1005. const dfloat m = __high2half(x[ib].dm);
  1006. const int vui = x[ib].qs[iqs];
  1007. v.x = vui & 0xF;
  1008. v.y = vui >> 4;
  1009. #ifdef GGML_CUDA_F16
  1010. v = __hmul2(v, {d, d});
  1011. v = __hadd2(v, {m, m});
  1012. #else
  1013. v.x = (v.x * d) + m;
  1014. v.y = (v.y * d) + m;
  1015. #endif // GGML_CUDA_F16
  1016. }
  1017. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1018. const block_q5_0 * x = (const block_q5_0 *) vx;
  1019. const dfloat d = x[ib].d;
  1020. uint32_t qh;
  1021. memcpy(&qh, x[ib].qh, sizeof(qh));
  1022. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1023. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1024. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1025. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1026. #ifdef GGML_CUDA_F16
  1027. v = __hsub2(v, {16.0f, 16.0f});
  1028. v = __hmul2(v, {d, d});
  1029. #else
  1030. v.x = (v.x - 16.0f) * d;
  1031. v.y = (v.y - 16.0f) * d;
  1032. #endif // GGML_CUDA_F16
  1033. }
  1034. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1035. const block_q5_1 * x = (const block_q5_1 *) vx;
  1036. const dfloat d = __low2half(x[ib].dm);
  1037. const dfloat m = __high2half(x[ib].dm);
  1038. uint32_t qh;
  1039. memcpy(&qh, x[ib].qh, sizeof(qh));
  1040. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1041. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1042. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1043. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1044. #ifdef GGML_CUDA_F16
  1045. v = __hmul2(v, {d, d});
  1046. v = __hadd2(v, {m, m});
  1047. #else
  1048. v.x = (v.x * d) + m;
  1049. v.y = (v.y * d) + m;
  1050. #endif // GGML_CUDA_F16
  1051. }
  1052. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1053. const block_q8_0 * x = (const block_q8_0 *) vx;
  1054. const dfloat d = x[ib].d;
  1055. v.x = x[ib].qs[iqs + 0];
  1056. v.y = x[ib].qs[iqs + 1];
  1057. #ifdef GGML_CUDA_F16
  1058. v = __hmul2(v, {d, d});
  1059. #else
  1060. v.x *= d;
  1061. v.y *= d;
  1062. #endif // GGML_CUDA_F16
  1063. }
  1064. template<typename dst_t>
  1065. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1066. const int i = blockIdx.x;
  1067. // assume 32 threads
  1068. const int tid = threadIdx.x;
  1069. const int il = tid/8;
  1070. const int ir = tid%8;
  1071. const int ib = 8*i + ir;
  1072. if (ib >= nb32) {
  1073. return;
  1074. }
  1075. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1076. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1077. const float d = __half2float(x->d);
  1078. const float dm = -8*d;
  1079. const uint8_t * q = x->qs + 4*il;
  1080. for (int l = 0; l < 4; ++l) {
  1081. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1082. y[l+16] = d * (q[l] >> 4) + dm;
  1083. }
  1084. }
  1085. template<typename dst_t>
  1086. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1087. const int i = blockIdx.x;
  1088. // assume 32 threads
  1089. const int tid = threadIdx.x;
  1090. const int il = tid/8;
  1091. const int ir = tid%8;
  1092. const int ib = 8*i + ir;
  1093. if (ib >= nb32) {
  1094. return;
  1095. }
  1096. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1097. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1098. const float2 d = __half22float2(x->dm);
  1099. const uint8_t * q = x->qs + 4*il;
  1100. for (int l = 0; l < 4; ++l) {
  1101. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1102. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1103. }
  1104. }
  1105. //================================== k-quants
  1106. template<typename dst_t>
  1107. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1108. const int i = blockIdx.x;
  1109. const block_q2_K * x = (const block_q2_K *) vx;
  1110. const int tid = threadIdx.x;
  1111. #if QK_K == 256
  1112. const int n = tid/32;
  1113. const int l = tid - 32*n;
  1114. const int is = 8*n + l/16;
  1115. const uint8_t q = x[i].qs[32*n + l];
  1116. dst_t * y = yy + i*QK_K + 128*n;
  1117. float dall = __low2half(x[i].dm);
  1118. float dmin = __high2half(x[i].dm);
  1119. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1120. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1121. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1122. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1123. #else
  1124. const int is = tid/16; // 0 or 1
  1125. const int il = tid%16; // 0...15
  1126. const uint8_t q = x[i].qs[il] >> (2*is);
  1127. dst_t * y = yy + i*QK_K + 16*is + il;
  1128. float dall = __low2half(x[i].dm);
  1129. float dmin = __high2half(x[i].dm);
  1130. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1131. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1132. #endif
  1133. }
  1134. template<typename dst_t>
  1135. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1136. const int i = blockIdx.x;
  1137. const block_q3_K * x = (const block_q3_K *) vx;
  1138. #if QK_K == 256
  1139. const int r = threadIdx.x/4;
  1140. const int tid = r/2;
  1141. const int is0 = r%2;
  1142. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1143. const int n = tid / 4;
  1144. const int j = tid - 4*n;
  1145. uint8_t m = 1 << (4*n + j);
  1146. int is = 8*n + 2*j + is0;
  1147. int shift = 2*j;
  1148. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1149. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1150. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1151. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1152. float d_all = x[i].d;
  1153. float dl = d_all * (us - 32);
  1154. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1155. const uint8_t * q = x[i].qs + 32*n;
  1156. const uint8_t * hm = x[i].hmask;
  1157. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1158. #else
  1159. const int tid = threadIdx.x;
  1160. const int is = tid/16; // 0 or 1
  1161. const int il = tid%16; // 0...15
  1162. const int im = il/8; // 0...1
  1163. const int in = il%8; // 0...7
  1164. dst_t * y = yy + i*QK_K + 16*is + il;
  1165. const uint8_t q = x[i].qs[il] >> (2*is);
  1166. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1167. const float d = (float)x[i].d;
  1168. if (is == 0) {
  1169. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1170. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1171. } else {
  1172. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1173. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1174. }
  1175. #endif
  1176. }
  1177. #if QK_K == 256
  1178. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1179. if (j < 4) {
  1180. d = q[j] & 63; m = q[j + 4] & 63;
  1181. } else {
  1182. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1183. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1184. }
  1185. }
  1186. #endif
  1187. template<typename dst_t>
  1188. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1189. const block_q4_K * x = (const block_q4_K *) vx;
  1190. const int i = blockIdx.x;
  1191. #if QK_K == 256
  1192. // assume 32 threads
  1193. const int tid = threadIdx.x;
  1194. const int il = tid/8;
  1195. const int ir = tid%8;
  1196. const int is = 2*il;
  1197. const int n = 4;
  1198. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1199. const float dall = __low2half(x[i].dm);
  1200. const float dmin = __high2half(x[i].dm);
  1201. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1202. uint8_t sc, m;
  1203. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1204. const float d1 = dall * sc; const float m1 = dmin * m;
  1205. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1206. const float d2 = dall * sc; const float m2 = dmin * m;
  1207. for (int l = 0; l < n; ++l) {
  1208. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1209. y[l +32] = d2 * (q[l] >> 4) - m2;
  1210. }
  1211. #else
  1212. const int tid = threadIdx.x;
  1213. const uint8_t * q = x[i].qs;
  1214. dst_t * y = yy + i*QK_K;
  1215. const float d = (float)x[i].dm[0];
  1216. const float m = (float)x[i].dm[1];
  1217. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1218. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1219. #endif
  1220. }
  1221. template<typename dst_t>
  1222. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1223. const block_q5_K * x = (const block_q5_K *) vx;
  1224. const int i = blockIdx.x;
  1225. #if QK_K == 256
  1226. // assume 64 threads - this is very slightly better than the one below
  1227. const int tid = threadIdx.x;
  1228. const int il = tid/16; // il is in 0...3
  1229. const int ir = tid%16; // ir is in 0...15
  1230. const int is = 2*il; // is is in 0...6
  1231. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1232. const float dall = __low2half(x[i].dm);
  1233. const float dmin = __high2half(x[i].dm);
  1234. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1235. const uint8_t * qh = x[i].qh + 2*ir;
  1236. uint8_t sc, m;
  1237. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1238. const float d1 = dall * sc; const float m1 = dmin * m;
  1239. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1240. const float d2 = dall * sc; const float m2 = dmin * m;
  1241. uint8_t hm = 1 << (2*il);
  1242. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1243. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1244. hm <<= 1;
  1245. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1246. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1247. #else
  1248. const int tid = threadIdx.x;
  1249. const uint8_t q = x[i].qs[tid];
  1250. const int im = tid/8; // 0...3
  1251. const int in = tid%8; // 0...7
  1252. const int is = tid/16; // 0 or 1
  1253. const uint8_t h = x[i].qh[in] >> im;
  1254. const float d = x[i].d;
  1255. dst_t * y = yy + i*QK_K + tid;
  1256. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1257. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1258. #endif
  1259. }
  1260. template<typename dst_t>
  1261. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1262. const block_q6_K * x = (const block_q6_K *) vx;
  1263. const int i = blockIdx.x;
  1264. #if QK_K == 256
  1265. // assume 64 threads - this is very slightly better than the one below
  1266. const int tid = threadIdx.x;
  1267. const int ip = tid/32; // ip is 0 or 1
  1268. const int il = tid - 32*ip; // 0...32
  1269. const int is = 8*ip + il/16;
  1270. dst_t * y = yy + i*QK_K + 128*ip + il;
  1271. const float d = x[i].d;
  1272. const uint8_t * ql = x[i].ql + 64*ip + il;
  1273. const uint8_t qh = x[i].qh[32*ip + il];
  1274. const int8_t * sc = x[i].scales + is;
  1275. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1276. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1277. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1278. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1279. #else
  1280. // assume 32 threads
  1281. const int tid = threadIdx.x;
  1282. const int ip = tid/16; // 0 or 1
  1283. const int il = tid - 16*ip; // 0...15
  1284. dst_t * y = yy + i*QK_K + 16*ip + il;
  1285. const float d = x[i].d;
  1286. const uint8_t ql = x[i].ql[16*ip + il];
  1287. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1288. const int8_t * sc = x[i].scales;
  1289. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1290. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1291. #endif
  1292. }
  1293. static const __device__ uint64_t iq2xxs_grid[256] = {
  1294. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1295. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1296. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1297. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1298. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1299. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1300. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1301. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1302. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1303. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1304. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1305. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1306. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1307. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1308. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1309. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1310. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1311. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1312. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1313. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1314. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1315. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1316. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1317. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1318. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1319. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1320. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1321. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1322. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1323. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1324. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1325. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1326. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1327. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1328. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1329. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1330. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1331. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1332. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1333. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1334. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1335. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1336. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1337. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1338. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1339. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1340. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1341. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1342. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1343. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1344. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1345. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1346. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1347. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1348. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1349. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1350. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1351. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1352. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1353. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1354. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1355. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1356. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1357. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1358. };
  1359. static const __device__ uint64_t iq2xs_grid[512] = {
  1360. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1361. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1362. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1363. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1364. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1365. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  1366. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  1367. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  1368. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  1369. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  1370. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  1371. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  1372. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  1373. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  1374. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  1375. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  1376. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  1377. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  1378. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  1379. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  1380. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  1381. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  1382. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  1383. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  1384. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  1385. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  1386. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  1387. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  1388. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  1389. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  1390. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  1391. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  1392. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  1393. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  1394. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  1395. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  1396. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  1397. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  1398. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  1399. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  1400. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  1401. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  1402. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  1403. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  1404. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  1405. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  1406. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  1407. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  1408. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  1409. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  1410. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  1411. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  1412. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  1413. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  1414. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  1415. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  1416. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  1417. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  1418. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  1419. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  1420. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  1421. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  1422. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  1423. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  1424. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  1425. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  1426. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  1427. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  1428. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  1429. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  1430. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  1431. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  1432. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  1433. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  1434. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  1435. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  1436. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  1437. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  1438. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  1439. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  1440. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  1441. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  1442. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  1443. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  1444. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  1445. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  1446. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  1447. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  1448. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  1449. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  1450. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  1451. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  1452. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  1453. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  1454. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  1455. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  1456. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  1457. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  1458. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  1459. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  1460. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  1461. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  1462. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  1463. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  1464. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  1465. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  1466. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  1467. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  1468. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  1469. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  1470. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  1471. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  1472. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  1473. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  1474. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  1475. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  1476. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  1477. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  1478. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  1479. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  1480. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  1481. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  1482. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  1483. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  1484. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  1485. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  1486. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  1487. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  1488. };
  1489. static const __device__ uint64_t iq2s_grid[1024] = {
  1490. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1491. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1492. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1493. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1494. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1495. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
  1496. 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
  1497. 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
  1498. 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
  1499. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
  1500. 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
  1501. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
  1502. 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
  1503. 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
  1504. 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
  1505. 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
  1506. 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
  1507. 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
  1508. 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
  1509. 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
  1510. 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  1511. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
  1512. 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
  1513. 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
  1514. 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
  1515. 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
  1516. 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
  1517. 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
  1518. 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
  1519. 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
  1520. 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
  1521. 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
  1522. 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
  1523. 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
  1524. 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
  1525. 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
  1526. 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
  1527. 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
  1528. 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
  1529. 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
  1530. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
  1531. 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
  1532. 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
  1533. 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
  1534. 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
  1535. 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
  1536. 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
  1537. 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
  1538. 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
  1539. 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
  1540. 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
  1541. 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
  1542. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
  1543. 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
  1544. 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
  1545. 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
  1546. 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
  1547. 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
  1548. 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
  1549. 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
  1550. 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
  1551. 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
  1552. 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
  1553. 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
  1554. 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
  1555. 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
  1556. 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
  1557. 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
  1558. 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
  1559. 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
  1560. 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  1561. 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
  1562. 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
  1563. 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
  1564. 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
  1565. 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
  1566. 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
  1567. 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
  1568. 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
  1569. 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
  1570. 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
  1571. 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
  1572. 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
  1573. 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
  1574. 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
  1575. 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
  1576. 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
  1577. 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
  1578. 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
  1579. 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
  1580. 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
  1581. 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
  1582. 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
  1583. 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
  1584. 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
  1585. 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
  1586. 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
  1587. 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
  1588. 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
  1589. 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
  1590. 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
  1591. 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
  1592. 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
  1593. 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
  1594. 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
  1595. 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
  1596. 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
  1597. 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
  1598. 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
  1599. 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
  1600. 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
  1601. 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
  1602. 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
  1603. 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
  1604. 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
  1605. 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
  1606. 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
  1607. 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  1608. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
  1609. 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
  1610. 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
  1611. 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
  1612. 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
  1613. 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
  1614. 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
  1615. 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
  1616. 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  1617. 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
  1618. 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
  1619. 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
  1620. 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
  1621. 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
  1622. 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
  1623. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
  1624. 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
  1625. 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
  1626. 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
  1627. 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
  1628. 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
  1629. 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
  1630. 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
  1631. 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
  1632. 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
  1633. 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
  1634. 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
  1635. 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
  1636. 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
  1637. 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
  1638. 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
  1639. 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
  1640. 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
  1641. 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
  1642. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
  1643. 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
  1644. 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
  1645. 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
  1646. 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
  1647. 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
  1648. 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
  1649. 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
  1650. 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
  1651. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
  1652. 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
  1653. 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
  1654. 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
  1655. 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
  1656. 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
  1657. 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
  1658. 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
  1659. 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
  1660. 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
  1661. 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
  1662. 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
  1663. 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
  1664. 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
  1665. 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
  1666. 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
  1667. 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
  1668. 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
  1669. 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
  1670. 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
  1671. 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
  1672. 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
  1673. 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
  1674. 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
  1675. 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
  1676. 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
  1677. 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
  1678. 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
  1679. 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
  1680. 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
  1681. 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
  1682. 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
  1683. 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
  1684. 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
  1685. 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
  1686. 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
  1687. 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
  1688. 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
  1689. 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
  1690. 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
  1691. 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
  1692. 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
  1693. 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
  1694. 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
  1695. 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  1696. 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
  1697. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
  1698. 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
  1699. 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
  1700. 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
  1701. 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
  1702. 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
  1703. 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
  1704. 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
  1705. 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
  1706. 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
  1707. 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
  1708. 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
  1709. 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
  1710. 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
  1711. 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
  1712. 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
  1713. 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
  1714. 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
  1715. 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
  1716. 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
  1717. 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
  1718. 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
  1719. 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
  1720. 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
  1721. 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
  1722. 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
  1723. 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
  1724. 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
  1725. 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
  1726. 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
  1727. 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
  1728. 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
  1729. 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
  1730. 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
  1731. 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
  1732. 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
  1733. 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
  1734. 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
  1735. 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
  1736. 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
  1737. 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
  1738. 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
  1739. 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
  1740. 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
  1741. 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
  1742. 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
  1743. 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
  1744. 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
  1745. 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
  1746. };
  1747. static const __device__ uint32_t iq3xxs_grid[256] = {
  1748. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  1749. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  1750. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  1751. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  1752. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  1753. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  1754. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  1755. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  1756. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  1757. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  1758. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  1759. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  1760. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  1761. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  1762. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  1763. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  1764. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  1765. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  1766. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  1767. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  1768. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  1769. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  1770. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  1771. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  1772. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  1773. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  1774. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  1775. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  1776. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  1777. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  1778. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  1779. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  1780. };
  1781. static const __device__ uint32_t iq3xs_grid[512] = {
  1782. 0x04040404, 0x0404040c, 0x04040414, 0x0404042c, 0x0404043e, 0x04040c04, 0x04040c0c, 0x04040c14,
  1783. 0x04040c24, 0x04040c34, 0x04041404, 0x0404140c, 0x0404142c, 0x04041c1c, 0x04042404, 0x04042414,
  1784. 0x0404242c, 0x0404243e, 0x04042c0c, 0x04042c1c, 0x04043404, 0x04043414, 0x04043e0c, 0x04043e24,
  1785. 0x04043e3e, 0x040c0404, 0x040c040c, 0x040c0414, 0x040c0424, 0x040c0c04, 0x040c0c0c, 0x040c0c2c,
  1786. 0x040c1404, 0x040c141c, 0x040c143e, 0x040c1c0c, 0x040c1c2c, 0x040c2424, 0x040c340c, 0x040c342c,
  1787. 0x040c3e14, 0x04140404, 0x0414040c, 0x0414042c, 0x0414043e, 0x04140c04, 0x04140c1c, 0x04140c34,
  1788. 0x0414140c, 0x0414142c, 0x04141c04, 0x04141c24, 0x04142414, 0x0414242c, 0x0414243e, 0x04142c0c,
  1789. 0x04142c1c, 0x04143e04, 0x04143e1c, 0x041c041c, 0x041c0c0c, 0x041c0c2c, 0x041c1404, 0x041c1414,
  1790. 0x041c1c0c, 0x041c1c1c, 0x041c1c34, 0x041c2424, 0x041c2c04, 0x041c2c14, 0x041c343e, 0x041c3e0c,
  1791. 0x041c3e2c, 0x04240404, 0x04240c1c, 0x04240c3e, 0x0424140c, 0x04241424, 0x04241c14, 0x04242404,
  1792. 0x0424241c, 0x04242c0c, 0x04243e04, 0x042c0414, 0x042c0424, 0x042c1404, 0x042c1414, 0x042c1434,
  1793. 0x042c1c1c, 0x042c240c, 0x042c242c, 0x042c243e, 0x042c3434, 0x042c3e1c, 0x04340434, 0x04340c0c,
  1794. 0x04340c1c, 0x04341c0c, 0x04342c14, 0x04343e0c, 0x043e0404, 0x043e0414, 0x043e0424, 0x043e1404,
  1795. 0x043e1414, 0x043e1434, 0x043e1c1c, 0x043e2c04, 0x043e2c24, 0x0c040404, 0x0c04040c, 0x0c040414,
  1796. 0x0c040424, 0x0c040c04, 0x0c040c0c, 0x0c040c1c, 0x0c040c2c, 0x0c040c3e, 0x0c041404, 0x0c041414,
  1797. 0x0c041c0c, 0x0c041c24, 0x0c041c34, 0x0c042c24, 0x0c042c34, 0x0c04340c, 0x0c043e14, 0x0c0c0404,
  1798. 0x0c0c040c, 0x0c0c041c, 0x0c0c0434, 0x0c0c0c04, 0x0c0c0c24, 0x0c0c140c, 0x0c0c1c04, 0x0c0c1c1c,
  1799. 0x0c0c240c, 0x0c0c2c04, 0x0c0c2c14, 0x0c0c3e04, 0x0c0c3e34, 0x0c140404, 0x0c140c14, 0x0c140c2c,
  1800. 0x0c140c3e, 0x0c141404, 0x0c141424, 0x0c141c14, 0x0c142404, 0x0c14241c, 0x0c142c2c, 0x0c143404,
  1801. 0x0c143e14, 0x0c1c040c, 0x0c1c0424, 0x0c1c043e, 0x0c1c0c04, 0x0c1c0c1c, 0x0c1c140c, 0x0c1c143e,
  1802. 0x0c1c1c04, 0x0c1c1c24, 0x0c1c240c, 0x0c1c3414, 0x0c1c3e04, 0x0c24041c, 0x0c24042c, 0x0c240c14,
  1803. 0x0c240c24, 0x0c241c0c, 0x0c241c1c, 0x0c242414, 0x0c242434, 0x0c242c04, 0x0c242c24, 0x0c2c040c,
  1804. 0x0c2c0c04, 0x0c2c0c1c, 0x0c2c140c, 0x0c2c1c04, 0x0c2c1c14, 0x0c2c2c0c, 0x0c341404, 0x0c341424,
  1805. 0x0c34143e, 0x0c342424, 0x0c342434, 0x0c3e040c, 0x0c3e041c, 0x0c3e0c04, 0x0c3e0c14, 0x0c3e140c,
  1806. 0x0c3e1c2c, 0x0c3e240c, 0x0c3e3414, 0x0c3e3e04, 0x14040404, 0x1404040c, 0x1404041c, 0x1404042c,
  1807. 0x1404043e, 0x14040c04, 0x14040c14, 0x14040c24, 0x14040c34, 0x1404140c, 0x1404141c, 0x1404143e,
  1808. 0x14041c04, 0x14041c14, 0x1404240c, 0x1404241c, 0x1404242c, 0x14042c04, 0x14042c14, 0x1404343e,
  1809. 0x14043e04, 0x14043e1c, 0x14043e2c, 0x140c0404, 0x140c0414, 0x140c0c04, 0x140c0c1c, 0x140c0c3e,
  1810. 0x140c1414, 0x140c142c, 0x140c1c0c, 0x140c1c24, 0x140c2414, 0x140c2c0c, 0x1414040c, 0x14140424,
  1811. 0x1414043e, 0x1414140c, 0x1414141c, 0x14141c04, 0x14141c3e, 0x1414240c, 0x14142c1c, 0x14142c3e,
  1812. 0x14143e0c, 0x14143e24, 0x141c0404, 0x141c0414, 0x141c042c, 0x141c0c0c, 0x141c1414, 0x141c1424,
  1813. 0x141c1c0c, 0x141c1c1c, 0x141c2414, 0x141c2c04, 0x141c3434, 0x1424040c, 0x1424043e, 0x14241404,
  1814. 0x1424141c, 0x14241c14, 0x14241c2c, 0x1424240c, 0x14243e14, 0x14243e2c, 0x142c0424, 0x142c0c0c,
  1815. 0x142c1414, 0x142c1c3e, 0x142c2404, 0x142c2c1c, 0x142c3e04, 0x14340404, 0x14340414, 0x1434043e,
  1816. 0x1434140c, 0x14342c2c, 0x1434340c, 0x143e042c, 0x143e0c0c, 0x143e1434, 0x143e1c04, 0x143e241c,
  1817. 0x143e2c04, 0x1c040414, 0x1c040c0c, 0x1c040c1c, 0x1c040c2c, 0x1c040c3e, 0x1c041414, 0x1c041c0c,
  1818. 0x1c041c1c, 0x1c041c2c, 0x1c042414, 0x1c042424, 0x1c04243e, 0x1c042c0c, 0x1c04341c, 0x1c043e0c,
  1819. 0x1c0c040c, 0x1c0c041c, 0x1c0c042c, 0x1c0c0c24, 0x1c0c140c, 0x1c0c141c, 0x1c0c2404, 0x1c0c3404,
  1820. 0x1c0c3e14, 0x1c0c3e34, 0x1c140404, 0x1c140c14, 0x1c141404, 0x1c141c14, 0x1c141c24, 0x1c142c04,
  1821. 0x1c1c040c, 0x1c1c0c04, 0x1c1c0c24, 0x1c1c140c, 0x1c1c141c, 0x1c1c143e, 0x1c1c1c04, 0x1c1c240c,
  1822. 0x1c1c241c, 0x1c1c243e, 0x1c1c2c2c, 0x1c1c3e1c, 0x1c24041c, 0x1c240c0c, 0x1c240c34, 0x1c241414,
  1823. 0x1c241c0c, 0x1c242c14, 0x1c243404, 0x1c243424, 0x1c2c040c, 0x1c2c0c04, 0x1c2c0c14, 0x1c2c142c,
  1824. 0x1c2c1c14, 0x1c2c2424, 0x1c2c2c34, 0x1c2c3e1c, 0x1c340c34, 0x1c34240c, 0x1c3e040c, 0x1c3e041c,
  1825. 0x1c3e1404, 0x1c3e1414, 0x1c3e1c2c, 0x24040404, 0x24040424, 0x24040c14, 0x24041404, 0x24041424,
  1826. 0x2404143e, 0x24041c14, 0x2404240c, 0x24042c04, 0x24043e04, 0x240c0414, 0x240c043e, 0x240c0c0c,
  1827. 0x240c0c1c, 0x240c1414, 0x240c1c04, 0x240c1c2c, 0x240c241c, 0x240c2c0c, 0x240c2c2c, 0x2414040c,
  1828. 0x2414041c, 0x24140c04, 0x24140c2c, 0x2414140c, 0x24141c1c, 0x24142404, 0x24142c3e, 0x24143414,
  1829. 0x24143e04, 0x241c0424, 0x241c0c0c, 0x241c0c1c, 0x241c1404, 0x241c1414, 0x241c1c0c, 0x241c1c2c,
  1830. 0x24240404, 0x24240414, 0x24241424, 0x24241c3e, 0x24242404, 0x24243e0c, 0x242c042c, 0x242c043e,
  1831. 0x242c140c, 0x242c3414, 0x24340c1c, 0x24341c24, 0x24343404, 0x243e0c04, 0x243e0c2c, 0x243e1c04,
  1832. 0x243e241c, 0x243e2c0c, 0x2c040414, 0x2c040c04, 0x2c040c24, 0x2c041414, 0x2c042404, 0x2c042424,
  1833. 0x2c04243e, 0x2c042c14, 0x2c043434, 0x2c043e24, 0x2c0c040c, 0x2c0c041c, 0x2c0c042c, 0x2c0c0c14,
  1834. 0x2c0c140c, 0x2c0c1c14, 0x2c0c3e14, 0x2c140404, 0x2c140c0c, 0x2c14141c, 0x2c141c04, 0x2c141c34,
  1835. 0x2c142c1c, 0x2c1c0414, 0x2c1c043e, 0x2c1c0c04, 0x2c1c143e, 0x2c1c2424, 0x2c1c2c0c, 0x2c1c342c,
  1836. 0x2c1c3e1c, 0x2c24040c, 0x2c240424, 0x2c241404, 0x2c241c14, 0x2c242434, 0x2c2c0c14, 0x2c2c1434,
  1837. 0x2c2c2c0c, 0x2c2c2c1c, 0x2c342414, 0x2c3e0414, 0x2c3e0424, 0x2c3e1414, 0x34040c0c, 0x34040c1c,
  1838. 0x34040c2c, 0x34041c0c, 0x34041c1c, 0x34043404, 0x340c0404, 0x340c1404, 0x340c143e, 0x340c3424,
  1839. 0x34140c14, 0x34141c24, 0x34142414, 0x34142c2c, 0x34143414, 0x34143e04, 0x341c0404, 0x341c0c24,
  1840. 0x341c140c, 0x341c2404, 0x3424142c, 0x3424241c, 0x34243414, 0x342c0404, 0x342c041c, 0x342c1c24,
  1841. 0x342c3404, 0x3434042c, 0x34342404, 0x343e0c0c, 0x343e0c1c, 0x3e040404, 0x3e040424, 0x3e04043e,
  1842. 0x3e041404, 0x3e041414, 0x3e041c34, 0x3e042404, 0x3e042c24, 0x3e043414, 0x3e0c0414, 0x3e0c0c0c,
  1843. 0x3e0c1424, 0x3e0c241c, 0x3e0c242c, 0x3e14040c, 0x3e140424, 0x3e140c04, 0x3e140c34, 0x3e14140c,
  1844. 0x3e141c04, 0x3e142c0c, 0x3e1c0414, 0x3e1c1c14, 0x3e1c1c2c, 0x3e1c2c1c, 0x3e24040c, 0x3e24042c,
  1845. 0x3e240c1c, 0x3e241404, 0x3e242c04, 0x3e2c1414, 0x3e2c2414, 0x3e340414, 0x3e341c0c, 0x3e3e0404,
  1846. };
  1847. static const __device__ uint64_t iq1s_grid[512] = {
  1848. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  1849. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  1850. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  1851. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  1852. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  1853. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  1854. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  1855. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  1856. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  1857. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  1858. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  1859. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  1860. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  1861. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  1862. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  1863. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  1864. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  1865. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  1866. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  1867. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  1868. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  1869. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  1870. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  1871. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  1872. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  1873. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  1874. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  1875. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  1876. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  1877. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  1878. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  1879. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  1880. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  1881. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  1882. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  1883. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  1884. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  1885. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  1886. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  1887. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  1888. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  1889. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  1890. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  1891. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  1892. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  1893. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  1894. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  1895. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  1896. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  1897. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  1898. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  1899. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  1900. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  1901. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  1902. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1903. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  1904. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  1905. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  1906. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1907. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  1908. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  1909. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  1910. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  1911. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1912. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  1913. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  1914. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  1915. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  1916. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  1917. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  1918. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  1919. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  1920. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  1921. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  1922. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  1923. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  1924. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  1925. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  1926. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  1927. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  1928. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  1929. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  1930. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  1931. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  1932. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  1933. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  1934. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  1935. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  1936. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  1937. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  1938. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  1939. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  1940. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  1941. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  1942. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  1943. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  1944. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  1945. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  1946. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  1947. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  1948. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  1949. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  1950. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  1951. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  1952. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  1953. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  1954. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  1955. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  1956. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  1957. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  1958. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  1959. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  1960. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  1961. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  1962. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  1963. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  1964. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  1965. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  1966. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  1967. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  1968. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  1969. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  1970. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  1971. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  1972. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  1973. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  1974. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  1975. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  1976. };
  1977. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1978. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1979. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1980. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1981. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1982. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1983. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1984. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1985. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1986. };
  1987. //#if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1988. static const __device__ uint64_t ksigns64[128] = {
  1989. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  1990. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  1991. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  1992. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  1993. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  1994. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  1995. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  1996. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  1997. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  1998. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  1999. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  2000. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  2001. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  2002. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  2003. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  2004. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  2005. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  2006. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  2007. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  2008. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  2009. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  2010. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  2011. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  2012. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  2013. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  2014. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  2015. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  2016. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  2017. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  2018. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  2019. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  2020. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  2021. };
  2022. //#endif
  2023. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2024. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  2025. switch (type) {
  2026. case GGML_TYPE_Q4_0:
  2027. case GGML_TYPE_Q4_1:
  2028. case GGML_TYPE_Q5_0:
  2029. case GGML_TYPE_Q5_1:
  2030. case GGML_TYPE_Q8_0:
  2031. case GGML_TYPE_Q2_K:
  2032. case GGML_TYPE_Q3_K:
  2033. case GGML_TYPE_Q4_K:
  2034. case GGML_TYPE_Q5_K:
  2035. case GGML_TYPE_Q6_K:
  2036. return true;
  2037. default:
  2038. return false;
  2039. }
  2040. }
  2041. template<typename dst_t>
  2042. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2043. const int i = blockIdx.x;
  2044. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  2045. const int tid = threadIdx.x;
  2046. #if QK_K == 256
  2047. const int il = tid/8; // 0...3
  2048. const int ib = tid%8; // 0...7
  2049. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2050. const uint16_t * q2 = x[i].qs + 4*ib;
  2051. const uint8_t * aux8 = (const uint8_t *)q2;
  2052. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  2053. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  2054. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  2055. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  2056. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2057. #else
  2058. assert(false);
  2059. #endif
  2060. }
  2061. template<typename dst_t>
  2062. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2063. const int i = blockIdx.x;
  2064. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  2065. const int tid = threadIdx.x;
  2066. #if QK_K == 256
  2067. const int il = tid/8; // 0...3
  2068. const int ib = tid%8; // 0...7
  2069. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2070. const uint16_t * q2 = x[i].qs + 4*ib;
  2071. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  2072. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  2073. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  2074. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2075. #else
  2076. assert(false);
  2077. #endif
  2078. }
  2079. template<typename dst_t>
  2080. static __global__ void dequantize_block_iq2_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2081. const int i = blockIdx.x;
  2082. const block_iq2_s * x = (const block_iq2_s *) vx;
  2083. const int tid = threadIdx.x;
  2084. #if QK_K == 256
  2085. const int il = tid/8; // 0...3
  2086. const int ib = tid%8; // 0...7
  2087. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2088. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  2089. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  2090. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  2091. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2092. #else
  2093. assert(false);
  2094. #endif
  2095. }
  2096. template<typename dst_t>
  2097. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2098. const int i = blockIdx.x;
  2099. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  2100. const int tid = threadIdx.x;
  2101. #if QK_K == 256
  2102. const int il = tid/8; // 0...3
  2103. const int ib = tid%8; // 0...7
  2104. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2105. const uint8_t * q3 = x[i].qs + 8*ib;
  2106. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  2107. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  2108. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  2109. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  2110. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  2111. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  2112. for (int j = 0; j < 4; ++j) {
  2113. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2114. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2115. }
  2116. #else
  2117. assert(false);
  2118. #endif
  2119. }
  2120. template<typename dst_t>
  2121. static __global__ void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2122. const int i = blockIdx.x;
  2123. const block_iq3_s * x = (const block_iq3_s *) vx;
  2124. const int tid = threadIdx.x;
  2125. #if QK_K == 256
  2126. const int il = tid/8; // 0...3
  2127. const int ib = tid%8; // 0...7
  2128. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2129. const uint8_t * qs = x[i].qs + 8*ib;
  2130. const uint8_t * grid1 = (const uint8_t *)(iq3xs_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  2131. const uint8_t * grid2 = (const uint8_t *)(iq3xs_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  2132. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf)) * 0.5f;
  2133. const uint8_t signs = x[i].signs[4*ib + il];
  2134. for (int j = 0; j < 4; ++j) {
  2135. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2136. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2137. }
  2138. #else
  2139. assert(false);
  2140. #endif
  2141. }
  2142. template<typename dst_t>
  2143. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2144. const int i = blockIdx.x;
  2145. const block_iq1_s * x = (const block_iq1_s *) vx;
  2146. const int tid = threadIdx.x;
  2147. #if QK_K == 256
  2148. const int il = tid/8; // 0...3
  2149. const int ib = tid%8; // 0...7
  2150. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2151. const int i8 = 4*ib+il;
  2152. uint8_t h = x[i].scales[i8/2] >> 4*(i8%2);
  2153. const int8_t * grid = (const int8_t *)(iq1s_grid + (x[i].qs[i8] | ((h & 8) << 5)));
  2154. const float d = (float)x[i].d * (2*(h & 7) + 1);
  2155. for (int j = 0; j < 8; ++j) y[j] = d * grid[j];
  2156. #else
  2157. assert(false);
  2158. #endif
  2159. }
  2160. static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2161. template<typename dst_t>
  2162. static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2163. const int i = blockIdx.x;
  2164. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  2165. const int tid = threadIdx.x;
  2166. const int il = tid/8; // 0...3
  2167. const int ib = tid%8; // 0...7
  2168. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  2169. const uint8_t * q4 = x[ib].qs + 4*il;
  2170. const float d = (float)x[ib].d;
  2171. for (int j = 0; j < 4; ++j) {
  2172. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  2173. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  2174. }
  2175. }
  2176. #if QK_K != 64
  2177. template<typename dst_t>
  2178. static __global__ void dequantize_block_iq4_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2179. const int i = blockIdx.x;
  2180. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  2181. const int tid = threadIdx.x;
  2182. const int il = tid/8; // 0...3
  2183. const int ib = tid%8; // 0...7
  2184. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  2185. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  2186. const float d = (float)x[i].d * ((((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4)) - 32);
  2187. for (int j = 0; j < 4; ++j) {
  2188. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  2189. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  2190. }
  2191. }
  2192. #endif
  2193. 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) {
  2194. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2195. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2196. if (row > nrows) return;
  2197. const int num_blocks_per_row = ncols / QK_K;
  2198. const int ib0 = row*num_blocks_per_row;
  2199. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  2200. float tmp = 0; // partial sum for thread in warp
  2201. #if QK_K == 256
  2202. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  2203. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2204. const int step = 16/K_QUANTS_PER_ITERATION;
  2205. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2206. const int in = tid - step*im; // 0...15 or 0...7
  2207. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  2208. const int q_offset = 32*im + l0;
  2209. const int s_offset = 8*im;
  2210. const int y_offset = 128*im + l0;
  2211. uint32_t aux[4];
  2212. const uint8_t * d = (const uint8_t *)aux;
  2213. const uint8_t * m = (const uint8_t *)(aux + 2);
  2214. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2215. const float * y = yy + i * QK_K + y_offset;
  2216. const uint8_t * q = x[i].qs + q_offset;
  2217. const float dall = __low2half(x[i].dm);
  2218. const float dmin = __high2half(x[i].dm);
  2219. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  2220. aux[0] = a[0] & 0x0f0f0f0f;
  2221. aux[1] = a[1] & 0x0f0f0f0f;
  2222. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  2223. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  2224. float sum1 = 0, sum2 = 0;
  2225. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2226. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  2227. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  2228. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  2229. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  2230. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  2231. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  2232. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  2233. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  2234. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  2235. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  2236. }
  2237. tmp += dall * sum1 - dmin * sum2;
  2238. }
  2239. #else
  2240. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  2241. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  2242. const int offset = tid * K_QUANTS_PER_ITERATION;
  2243. uint32_t uaux[2];
  2244. const uint8_t * d = (const uint8_t *)uaux;
  2245. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2246. const float * y = yy + i * QK_K + offset;
  2247. const uint8_t * q = x[i].qs + offset;
  2248. const uint32_t * s = (const uint32_t *)x[i].scales;
  2249. uaux[0] = s[0] & 0x0f0f0f0f;
  2250. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  2251. const float2 dall = __half22float2(x[i].dm);
  2252. float sum1 = 0, sum2 = 0;
  2253. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2254. const uint8_t ql = q[l];
  2255. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  2256. + y[l+16] * d[1] * ((ql >> 2) & 3)
  2257. + y[l+32] * d[2] * ((ql >> 4) & 3)
  2258. + y[l+48] * d[3] * ((ql >> 6) & 3);
  2259. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  2260. }
  2261. tmp += dall.x * sum1 - dall.y * sum2;
  2262. }
  2263. #endif
  2264. // sum up partial sums and write back result
  2265. tmp = warp_reduce_sum(tmp);
  2266. if (threadIdx.x == 0) {
  2267. dst[row] = tmp;
  2268. }
  2269. }
  2270. 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) {
  2271. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2272. if (row > nrows) return;
  2273. const int num_blocks_per_row = ncols / QK_K;
  2274. const int ib0 = row*num_blocks_per_row;
  2275. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  2276. float tmp = 0; // partial sum for thread in warp
  2277. #if QK_K == 256
  2278. const uint16_t kmask1 = 0x0303;
  2279. const uint16_t kmask2 = 0x0f0f;
  2280. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2281. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2282. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  2283. const int step = 16/K_QUANTS_PER_ITERATION;
  2284. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2285. const int in = tid - step*im; // 0....15 or 0...7
  2286. const uint8_t m = 1 << (4*im);
  2287. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  2288. const int q_offset = 32*im + l0;
  2289. const int y_offset = 128*im + l0;
  2290. uint16_t utmp[4];
  2291. const int8_t * s = (const int8_t *)utmp;
  2292. const uint16_t s_shift = 4*im;
  2293. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2294. const float * y = yy + i * QK_K + y_offset;
  2295. const uint8_t * q = x[i].qs + q_offset;
  2296. const uint8_t * h = x[i].hmask + l0;
  2297. const uint16_t * a = (const uint16_t *)x[i].scales;
  2298. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  2299. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  2300. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  2301. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  2302. const float d = x[i].d;
  2303. float sum = 0;
  2304. for (int l = 0; l < n; ++l) {
  2305. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  2306. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  2307. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  2308. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  2309. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  2310. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  2311. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  2312. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  2313. }
  2314. tmp += d * sum;
  2315. }
  2316. #else
  2317. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  2318. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  2319. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  2320. const int in = offset/8; // 0 or 1
  2321. const int im = offset%8; // 0...7
  2322. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2323. const float * y = yy + i * QK_K + offset;
  2324. const uint8_t * q = x[i].qs + offset;
  2325. const uint8_t * s = x[i].scales;
  2326. const float dall = (float)x[i].d;
  2327. float sum = 0;
  2328. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2329. const uint8_t hl = x[i].hmask[im+l] >> in;
  2330. const uint8_t ql = q[l];
  2331. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  2332. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  2333. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  2334. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  2335. }
  2336. tmp += sum;
  2337. }
  2338. #endif
  2339. // sum up partial sums and write back result
  2340. tmp = warp_reduce_sum(tmp);
  2341. if (threadIdx.x == 0) {
  2342. dst[row] = tmp;
  2343. }
  2344. }
  2345. 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) {
  2346. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2347. if (row > nrows) return;
  2348. const int num_blocks_per_row = ncols / QK_K;
  2349. const int ib0 = row*num_blocks_per_row;
  2350. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  2351. #if QK_K == 256
  2352. const uint16_t kmask1 = 0x3f3f;
  2353. const uint16_t kmask2 = 0x0f0f;
  2354. const uint16_t kmask3 = 0xc0c0;
  2355. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2356. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2357. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  2358. const int il = tid/step; // 0...3
  2359. const int ir = tid - step*il; // 0...7 or 0...3
  2360. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  2361. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2362. const int in = il%2;
  2363. const int l0 = n*(2*ir + in);
  2364. const int q_offset = 32*im + l0;
  2365. const int y_offset = 64*im + l0;
  2366. uint16_t aux[4];
  2367. const uint8_t * sc = (const uint8_t *)aux;
  2368. #if K_QUANTS_PER_ITERATION == 2
  2369. uint32_t q32[4];
  2370. const uint8_t * q4 = (const uint8_t *)q32;
  2371. #else
  2372. uint16_t q16[4];
  2373. const uint8_t * q4 = (const uint8_t *)q16;
  2374. #endif
  2375. float tmp = 0; // partial sum for thread in warp
  2376. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2377. const float * y1 = yy + i*QK_K + y_offset;
  2378. const float * y2 = y1 + 128;
  2379. const float dall = __low2half(x[i].dm);
  2380. const float dmin = __high2half(x[i].dm);
  2381. const uint16_t * a = (const uint16_t *)x[i].scales;
  2382. aux[0] = a[im+0] & kmask1;
  2383. aux[1] = a[im+2] & kmask1;
  2384. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2385. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2386. #if K_QUANTS_PER_ITERATION == 2
  2387. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  2388. const uint32_t * q2 = q1 + 16;
  2389. q32[0] = q1[0] & 0x0f0f0f0f;
  2390. q32[1] = q1[0] & 0xf0f0f0f0;
  2391. q32[2] = q2[0] & 0x0f0f0f0f;
  2392. q32[3] = q2[0] & 0xf0f0f0f0;
  2393. float4 s = {0.f, 0.f, 0.f, 0.f};
  2394. float smin = 0;
  2395. for (int l = 0; l < 4; ++l) {
  2396. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  2397. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  2398. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  2399. }
  2400. 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;
  2401. #else
  2402. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  2403. const uint16_t * q2 = q1 + 32;
  2404. q16[0] = q1[0] & 0x0f0f;
  2405. q16[1] = q1[0] & 0xf0f0;
  2406. q16[2] = q2[0] & 0x0f0f;
  2407. q16[3] = q2[0] & 0xf0f0;
  2408. float4 s = {0.f, 0.f, 0.f, 0.f};
  2409. float smin = 0;
  2410. for (int l = 0; l < 2; ++l) {
  2411. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  2412. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  2413. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  2414. }
  2415. 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;
  2416. #endif
  2417. }
  2418. #else
  2419. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2420. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2421. const int step = tid * K_QUANTS_PER_ITERATION;
  2422. uint16_t aux16[2];
  2423. const uint8_t * s = (const uint8_t *)aux16;
  2424. float tmp = 0;
  2425. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2426. const uint8_t * q = x[i].qs + step;
  2427. const float * y = yy + i*QK_K + step;
  2428. const uint16_t * a = (const uint16_t *)x[i].scales;
  2429. aux16[0] = a[0] & 0x0f0f;
  2430. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2431. const float d = (float)x[i].dm[0];
  2432. const float m = (float)x[i].dm[1];
  2433. float sum = 0.f;
  2434. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2435. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  2436. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  2437. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  2438. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  2439. }
  2440. tmp += sum;
  2441. }
  2442. #endif
  2443. // sum up partial sums and write back result
  2444. tmp = warp_reduce_sum(tmp);
  2445. if (tid == 0) {
  2446. dst[row] = tmp;
  2447. }
  2448. }
  2449. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  2450. const int row = blockIdx.x;
  2451. const int num_blocks_per_row = ncols / QK_K;
  2452. const int ib0 = row*num_blocks_per_row;
  2453. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  2454. float tmp = 0; // partial sum for thread in warp
  2455. #if QK_K == 256
  2456. const uint16_t kmask1 = 0x3f3f;
  2457. const uint16_t kmask2 = 0x0f0f;
  2458. const uint16_t kmask3 = 0xc0c0;
  2459. const int tid = threadIdx.x/2; // 0...15
  2460. const int ix = threadIdx.x%2;
  2461. const int il = tid/4; // 0...3
  2462. const int ir = tid - 4*il;// 0...3
  2463. const int n = 2;
  2464. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2465. const int in = il%2;
  2466. const int l0 = n*(2*ir + in);
  2467. const int q_offset = 32*im + l0;
  2468. const int y_offset = 64*im + l0;
  2469. const uint8_t hm1 = 1 << (2*im);
  2470. const uint8_t hm2 = hm1 << 4;
  2471. uint16_t aux[4];
  2472. const uint8_t * sc = (const uint8_t *)aux;
  2473. uint16_t q16[8];
  2474. const uint8_t * q4 = (const uint8_t *)q16;
  2475. for (int i = ix; i < num_blocks_per_row; i += 2) {
  2476. const uint8_t * ql1 = x[i].qs + q_offset;
  2477. const uint8_t * qh = x[i].qh + l0;
  2478. const float * y1 = yy + i*QK_K + y_offset;
  2479. const float * y2 = y1 + 128;
  2480. const float dall = __low2half(x[i].dm);
  2481. const float dmin = __high2half(x[i].dm);
  2482. const uint16_t * a = (const uint16_t *)x[i].scales;
  2483. aux[0] = a[im+0] & kmask1;
  2484. aux[1] = a[im+2] & kmask1;
  2485. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2486. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2487. float4 sum = {0.f, 0.f, 0.f, 0.f};
  2488. float smin = 0;
  2489. const uint16_t * q1 = (const uint16_t *)ql1;
  2490. const uint16_t * q2 = q1 + 32;
  2491. q16[0] = q1[0] & 0x0f0f;
  2492. q16[1] = q1[8] & 0x0f0f;
  2493. q16[2] = (q1[0] >> 4) & 0x0f0f;
  2494. q16[3] = (q1[8] >> 4) & 0x0f0f;
  2495. q16[4] = q2[0] & 0x0f0f;
  2496. q16[5] = q2[8] & 0x0f0f;
  2497. q16[6] = (q2[0] >> 4) & 0x0f0f;
  2498. q16[7] = (q2[8] >> 4) & 0x0f0f;
  2499. for (int l = 0; l < n; ++l) {
  2500. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  2501. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  2502. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  2503. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  2504. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  2505. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  2506. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  2507. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  2508. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  2509. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  2510. }
  2511. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  2512. }
  2513. #else
  2514. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2515. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2516. const int step = tid * K_QUANTS_PER_ITERATION;
  2517. const int im = step/8;
  2518. const int in = step%8;
  2519. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2520. const uint8_t * q = x[i].qs + step;
  2521. const int8_t * s = x[i].scales;
  2522. const float * y = yy + i*QK_K + step;
  2523. const float d = x[i].d;
  2524. float sum = 0.f;
  2525. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2526. const uint8_t h = x[i].qh[in+j] >> im;
  2527. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  2528. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  2529. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  2530. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  2531. }
  2532. tmp += sum;
  2533. }
  2534. #endif
  2535. // sum up partial sums and write back result
  2536. tmp = warp_reduce_sum(tmp);
  2537. if (threadIdx.x == 0) {
  2538. dst[row] = tmp;
  2539. }
  2540. }
  2541. 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) {
  2542. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2543. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2544. if (row > nrows) return;
  2545. const int num_blocks_per_row = ncols / QK_K;
  2546. const int ib0 = row*num_blocks_per_row;
  2547. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  2548. #if QK_K == 256
  2549. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2550. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  2551. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  2552. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2553. const int in = tid - step*im; // 0...15 or 0...7
  2554. #if K_QUANTS_PER_ITERATION == 1
  2555. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  2556. const int is = 0;
  2557. #else
  2558. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  2559. const int is = in / 4;
  2560. #endif
  2561. const int ql_offset = 64*im + l0;
  2562. const int qh_offset = 32*im + l0;
  2563. const int s_offset = 8*im + is;
  2564. const int y_offset = 128*im + l0;
  2565. float tmp = 0; // partial sum for thread in warp
  2566. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2567. const float * y = yy + i * QK_K + y_offset;
  2568. const uint8_t * ql = x[i].ql + ql_offset;
  2569. const uint8_t * qh = x[i].qh + qh_offset;
  2570. const int8_t * s = x[i].scales + s_offset;
  2571. const float d = x[i].d;
  2572. #if K_QUANTS_PER_ITERATION == 1
  2573. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  2574. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  2575. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  2576. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  2577. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  2578. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  2579. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  2580. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  2581. tmp += sum;
  2582. #else
  2583. float sum = 0;
  2584. for (int l = 0; l < 4; ++l) {
  2585. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  2586. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  2587. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  2588. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  2589. }
  2590. tmp += sum;
  2591. #endif
  2592. }
  2593. #else
  2594. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  2595. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  2596. const int step = tid * K_QUANTS_PER_ITERATION;
  2597. float tmp = 0; // partial sum for thread in warp
  2598. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2599. const float * y = yy + i * QK_K + step;
  2600. const uint8_t * ql = x[i].ql + step;
  2601. const uint8_t * qh = x[i].qh + step;
  2602. const int8_t * s = x[i].scales;
  2603. const float d = x[i+0].d;
  2604. float sum = 0;
  2605. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2606. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  2607. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  2608. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  2609. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  2610. }
  2611. tmp += sum;
  2612. }
  2613. #endif
  2614. // sum up partial sums and write back result
  2615. tmp = warp_reduce_sum(tmp);
  2616. if (tid == 0) {
  2617. dst[row] = tmp;
  2618. }
  2619. }
  2620. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  2621. const half * x = (const half *) vx;
  2622. // automatic half -> float type cast if dfloat == float
  2623. v.x = x[ib + iqs + 0];
  2624. v.y = x[ib + iqs + 1];
  2625. }
  2626. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  2627. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  2628. if (ix >= kx_padded) {
  2629. return;
  2630. }
  2631. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  2632. const int i_padded = iy*kx_padded + ix;
  2633. block_q8_1 * y = (block_q8_1 *) vy;
  2634. const int ib = i_padded / QK8_1; // block index
  2635. const int iqs = i_padded % QK8_1; // quant index
  2636. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  2637. float amax = fabsf(xi);
  2638. float sum = xi;
  2639. amax = warp_reduce_max(amax);
  2640. sum = warp_reduce_sum(sum);
  2641. const float d = amax / 127;
  2642. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  2643. y[ib].qs[iqs] = q;
  2644. if (iqs > 0) {
  2645. return;
  2646. }
  2647. reinterpret_cast<half&>(y[ib].ds.x) = d;
  2648. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  2649. }
  2650. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2651. static __global__ void k_get_rows(
  2652. const void * src0, const int32_t * src1, dst_t * dst,
  2653. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2654. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2655. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2656. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2657. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2658. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  2659. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2660. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2661. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2662. if (i00 >= ne00) {
  2663. return;
  2664. }
  2665. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2666. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2667. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  2668. const int ib = i00/qk; // block index
  2669. const int iqs = (i00%qk)/qr; // quant index
  2670. const int iybs = i00 - i00%qk; // dst block start index
  2671. const int y_offset = qr == 1 ? 1 : qk/2;
  2672. // dequantize
  2673. dfloat2 v;
  2674. dequantize_kernel(src0_row, ib, iqs, v);
  2675. dst_row[iybs + iqs + 0] = v.x;
  2676. dst_row[iybs + iqs + y_offset] = v.y;
  2677. }
  2678. template<typename src0_t, typename dst_t>
  2679. static __global__ void k_get_rows_float(
  2680. const src0_t * src0, const int32_t * src1, dst_t * dst,
  2681. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2682. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2683. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2684. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2685. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2686. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2687. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2688. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2689. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2690. if (i00 >= ne00) {
  2691. return;
  2692. }
  2693. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2694. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2695. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2696. dst_row[i00] = src0_row[i00];
  2697. }
  2698. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2699. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2700. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2701. if (i >= k) {
  2702. return;
  2703. }
  2704. const int ib = i/qk; // block index
  2705. const int iqs = (i%qk)/qr; // quant index
  2706. const int iybs = i - i%qk; // y block start index
  2707. const int y_offset = qr == 1 ? 1 : qk/2;
  2708. // dequantize
  2709. dfloat2 v;
  2710. dequantize_kernel(vx, ib, iqs, v);
  2711. y[iybs + iqs + 0] = v.x;
  2712. y[iybs + iqs + y_offset] = v.y;
  2713. }
  2714. template <typename src_t, typename dst_t>
  2715. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2716. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2717. if (i >= k) {
  2718. return;
  2719. }
  2720. const src_t * x = (src_t *) vx;
  2721. y[i] = x[i];
  2722. }
  2723. template <bool need_check>
  2724. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2725. #if __CUDA_ARCH__ >= CC_PASCAL
  2726. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2727. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2728. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2729. half2 * y2 = (half2 *) (y + i0);
  2730. __shared__ int vals[nint];
  2731. #pragma unroll
  2732. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2733. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2734. break;
  2735. }
  2736. const int ix = ix0 + threadIdx.x;
  2737. vals[ix] = x0[ix];
  2738. }
  2739. #pragma unroll
  2740. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2741. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2742. return;
  2743. }
  2744. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2745. const half d = *b0;
  2746. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2747. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2748. }
  2749. #else
  2750. (void) vx; (void) y; (void) k;
  2751. NO_DEVICE_CODE;
  2752. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2753. }
  2754. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2755. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2756. #define VDR_Q4_0_Q8_1_MMVQ 2
  2757. #define VDR_Q4_0_Q8_1_MMQ 4
  2758. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2759. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2760. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2761. int sumi = 0;
  2762. #pragma unroll
  2763. for (int i = 0; i < vdr; ++i) {
  2764. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2765. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2766. // SIMD dot product of quantized values
  2767. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2768. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2769. }
  2770. const float2 ds8f = __half22float2(ds8);
  2771. // second part effectively subtracts 8 from each quant value
  2772. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2773. #else
  2774. NO_DEVICE_CODE;
  2775. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2776. }
  2777. #define VDR_Q4_1_Q8_1_MMVQ 2
  2778. #define VDR_Q4_1_Q8_1_MMQ 4
  2779. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2780. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2781. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2782. int sumi = 0;
  2783. #pragma unroll
  2784. for (int i = 0; i < vdr; ++i) {
  2785. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2786. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2787. // SIMD dot product of quantized values
  2788. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2789. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2790. }
  2791. #ifdef GGML_CUDA_F16
  2792. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2793. const float d4d8 = tmp.x;
  2794. const float m4s8 = tmp.y;
  2795. #else
  2796. const float2 dm4f = __half22float2(dm4);
  2797. const float2 ds8f = __half22float2(ds8);
  2798. const float d4d8 = dm4f.x * ds8f.x;
  2799. const float m4s8 = dm4f.y * ds8f.y;
  2800. #endif // GGML_CUDA_F16
  2801. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2802. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2803. #else
  2804. NO_DEVICE_CODE;
  2805. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2806. }
  2807. #define VDR_Q5_0_Q8_1_MMVQ 2
  2808. #define VDR_Q5_0_Q8_1_MMQ 4
  2809. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2810. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2811. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2812. int sumi = 0;
  2813. #pragma unroll
  2814. for (int i = 0; i < vdr; ++i) {
  2815. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2816. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2817. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2818. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2819. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2820. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2821. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2822. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2823. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2824. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2825. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2826. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2827. }
  2828. const float2 ds8f = __half22float2(ds8);
  2829. // second part effectively subtracts 16 from each quant value
  2830. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2831. #else
  2832. NO_DEVICE_CODE;
  2833. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2834. }
  2835. #define VDR_Q5_1_Q8_1_MMVQ 2
  2836. #define VDR_Q5_1_Q8_1_MMQ 4
  2837. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2838. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2839. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2840. int sumi = 0;
  2841. #pragma unroll
  2842. for (int i = 0; i < vdr; ++i) {
  2843. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2844. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2845. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2846. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2847. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2848. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2849. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2850. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2851. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2852. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2853. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2854. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2855. }
  2856. #ifdef GGML_CUDA_F16
  2857. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2858. const float d5d8 = tmp.x;
  2859. const float m5s8 = tmp.y;
  2860. #else
  2861. const float2 dm5f = __half22float2(dm5);
  2862. const float2 ds8f = __half22float2(ds8);
  2863. const float d5d8 = dm5f.x * ds8f.x;
  2864. const float m5s8 = dm5f.y * ds8f.y;
  2865. #endif // GGML_CUDA_F16
  2866. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2867. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2868. #else
  2869. NO_DEVICE_CODE;
  2870. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2871. }
  2872. #define VDR_Q8_0_Q8_1_MMVQ 2
  2873. #define VDR_Q8_0_Q8_1_MMQ 8
  2874. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2875. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2876. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2877. int sumi = 0;
  2878. #pragma unroll
  2879. for (int i = 0; i < vdr; ++i) {
  2880. // SIMD dot product of quantized values
  2881. sumi = __dp4a(v[i], u[i], sumi);
  2882. }
  2883. return d8_0*d8_1 * sumi;
  2884. #else
  2885. NO_DEVICE_CODE;
  2886. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2887. }
  2888. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2889. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2890. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2891. int sumi = 0;
  2892. #pragma unroll
  2893. for (int i = 0; i < vdr; ++i) {
  2894. // SIMD dot product of quantized values
  2895. sumi = __dp4a(v[i], u[i], sumi);
  2896. }
  2897. #ifdef GGML_CUDA_F16
  2898. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2899. const float d8d8 = tmp.x;
  2900. const float m8s8 = tmp.y;
  2901. #else
  2902. const float2 dm8f = __half22float2(dm8);
  2903. const float2 ds8f = __half22float2(ds8);
  2904. const float d8d8 = dm8f.x * ds8f.x;
  2905. const float m8s8 = dm8f.y * ds8f.y;
  2906. #endif // GGML_CUDA_F16
  2907. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2908. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2909. #else
  2910. NO_DEVICE_CODE;
  2911. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2912. }
  2913. #define VDR_Q2_K_Q8_1_MMVQ 1
  2914. #define VDR_Q2_K_Q8_1_MMQ 2
  2915. // contiguous v/x values
  2916. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2917. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2918. const half2 & dm2, const float * __restrict__ d8) {
  2919. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2920. float sumf_d = 0.0f;
  2921. float sumf_m = 0.0f;
  2922. #pragma unroll
  2923. for (int i = 0; i < QR2_K; ++i) {
  2924. const int sc = scales[2*i];
  2925. const int vi = (v >> (2*i)) & 0x03030303;
  2926. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2927. // fill int with 4x m
  2928. int m = sc >> 4;
  2929. m |= m << 8;
  2930. m |= m << 16;
  2931. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2932. }
  2933. const float2 dm2f = __half22float2(dm2);
  2934. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2935. #else
  2936. NO_DEVICE_CODE;
  2937. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2938. }
  2939. // contiguous u/y values
  2940. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2941. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2942. const half2 & dm2, const float & d8) {
  2943. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2944. int sumi_d = 0;
  2945. int sumi_m = 0;
  2946. #pragma unroll
  2947. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2948. int sumi_d_sc = 0;
  2949. const int sc = scales[i0 / (QI8_1/2)];
  2950. // fill int with 4x m
  2951. int m = sc >> 4;
  2952. m |= m << 8;
  2953. m |= m << 16;
  2954. #pragma unroll
  2955. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2956. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2957. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2958. }
  2959. sumi_d += sumi_d_sc * (sc & 0xF);
  2960. }
  2961. const float2 dm2f = __half22float2(dm2);
  2962. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2963. #else
  2964. NO_DEVICE_CODE;
  2965. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2966. }
  2967. #define VDR_Q3_K_Q8_1_MMVQ 1
  2968. #define VDR_Q3_K_Q8_1_MMQ 2
  2969. // contiguous v/x values
  2970. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2971. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2972. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2973. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2974. float sumf = 0.0f;
  2975. #pragma unroll
  2976. for (int i = 0; i < QR3_K; ++i) {
  2977. const int isc = scale_offset + 2*i;
  2978. const int isc_low = isc % (QK_K/32);
  2979. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2980. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2981. const int isc_high = isc % (QK_K/64);
  2982. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2983. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2984. const int sc = (sc_low | sc_high) - 32;
  2985. const int vil = (vl >> (2*i)) & 0x03030303;
  2986. const int vih = ((vh >> i) << 2) & 0x04040404;
  2987. const int vi = __vsubss4(vil, vih);
  2988. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2989. }
  2990. return d3 * sumf;
  2991. #else
  2992. NO_DEVICE_CODE;
  2993. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2994. }
  2995. // contiguous u/y values
  2996. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2997. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2998. const float & d3, const float & d8) {
  2999. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3000. int sumi = 0;
  3001. #pragma unroll
  3002. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  3003. int sumi_sc = 0;
  3004. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  3005. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  3006. }
  3007. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  3008. }
  3009. return d3*d8 * sumi;
  3010. #else
  3011. NO_DEVICE_CODE;
  3012. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3013. }
  3014. #define VDR_Q4_K_Q8_1_MMVQ 2
  3015. #define VDR_Q4_K_Q8_1_MMQ 8
  3016. // contiguous v/x values
  3017. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  3018. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3019. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  3020. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3021. float sumf_d = 0.0f;
  3022. float sumf_m = 0.0f;
  3023. #pragma unroll
  3024. for (int i = 0; i < QR4_K; ++i) {
  3025. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  3026. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  3027. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  3028. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  3029. sumf_d += d8[i] * (dot1 * sc[i]);
  3030. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  3031. }
  3032. const float2 dm4f = __half22float2(dm4);
  3033. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3034. #else
  3035. NO_DEVICE_CODE;
  3036. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3037. }
  3038. // contiguous u/y values
  3039. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  3040. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3041. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  3042. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3043. float sumf_d = 0.0f;
  3044. float sumf_m = 0.0f;
  3045. #pragma unroll
  3046. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  3047. int sumi_d = 0;
  3048. #pragma unroll
  3049. for (int j = 0; j < QI8_1; ++j) {
  3050. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  3051. }
  3052. const float2 ds8f = __half22float2(ds8[i]);
  3053. sumf_d += ds8f.x * (sc[i] * sumi_d);
  3054. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  3055. }
  3056. const float2 dm4f = __half22float2(dm4);
  3057. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3058. #else
  3059. NO_DEVICE_CODE;
  3060. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3061. }
  3062. #define VDR_Q5_K_Q8_1_MMVQ 2
  3063. #define VDR_Q5_K_Q8_1_MMQ 8
  3064. // contiguous v/x values
  3065. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  3066. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3067. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  3068. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3069. float sumf_d = 0.0f;
  3070. float sumf_m = 0.0f;
  3071. #pragma unroll
  3072. for (int i = 0; i < QR5_K; ++i) {
  3073. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  3074. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  3075. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  3076. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  3077. const int v0i = vl0i | vh0i;
  3078. const int v1i = vl1i | vh1i;
  3079. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  3080. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  3081. sumf_d += d8[i] * (dot1 * sc[i]);
  3082. sumf_m += d8[i] * (dot2 * m[i]);
  3083. }
  3084. const float2 dm5f = __half22float2(dm5);
  3085. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  3086. #else
  3087. NO_DEVICE_CODE;
  3088. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3089. }
  3090. // contiguous u/y values
  3091. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  3092. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3093. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  3094. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3095. float sumf_d = 0.0f;
  3096. float sumf_m = 0.0f;
  3097. #pragma unroll
  3098. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  3099. int sumi_d = 0;
  3100. #pragma unroll
  3101. for (int j = 0; j < QI8_1; ++j) {
  3102. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  3103. }
  3104. const float2 ds8f = __half22float2(ds8[i]);
  3105. sumf_d += ds8f.x * (sc[i] * sumi_d);
  3106. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  3107. }
  3108. const float2 dm4f = __half22float2(dm4);
  3109. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3110. #else
  3111. NO_DEVICE_CODE;
  3112. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3113. }
  3114. #define VDR_Q6_K_Q8_1_MMVQ 1
  3115. #define VDR_Q6_K_Q8_1_MMQ 8
  3116. // contiguous v/x values
  3117. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  3118. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  3119. const float & d, const float * __restrict__ d8) {
  3120. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3121. float sumf = 0.0f;
  3122. #pragma unroll
  3123. for (int i = 0; i < QR6_K; ++i) {
  3124. const int sc = scales[4*i];
  3125. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  3126. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  3127. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  3128. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  3129. }
  3130. return d*sumf;
  3131. #else
  3132. NO_DEVICE_CODE;
  3133. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3134. }
  3135. // contiguous u/y values
  3136. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  3137. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  3138. const float & d6, const float * __restrict__ d8) {
  3139. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3140. float sumf_d = 0.0f;
  3141. #pragma unroll
  3142. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  3143. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  3144. #pragma unroll
  3145. for (int i = i0; i < i0 + 2; ++i) {
  3146. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  3147. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  3148. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  3149. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  3150. }
  3151. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  3152. }
  3153. return d6 * sumf_d;
  3154. #else
  3155. NO_DEVICE_CODE;
  3156. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3157. }
  3158. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  3159. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3160. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  3161. int v[VDR_Q4_0_Q8_1_MMVQ];
  3162. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  3163. #pragma unroll
  3164. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  3165. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  3166. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3167. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  3168. }
  3169. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  3170. }
  3171. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3172. (void)x_qh; (void)x_sc;
  3173. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3174. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  3175. *x_ql = tile_x_qs;
  3176. *x_dm = (half2 *) tile_x_d;
  3177. }
  3178. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  3179. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3180. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3181. (void)x_qh; (void)x_sc;
  3182. GGML_CUDA_ASSUME(i_offset >= 0);
  3183. GGML_CUDA_ASSUME(i_offset < nwarps);
  3184. GGML_CUDA_ASSUME(k >= 0);
  3185. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3186. const int kbx = k / QI4_0;
  3187. const int kqsx = k % QI4_0;
  3188. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  3189. float * x_dmf = (float *) x_dm;
  3190. #pragma unroll
  3191. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3192. int i = i0 + i_offset;
  3193. if (need_check) {
  3194. i = min(i, i_max);
  3195. }
  3196. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3197. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3198. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  3199. }
  3200. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  3201. const int kbxd = k % blocks_per_tile_x_row;
  3202. #pragma unroll
  3203. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  3204. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  3205. if (need_check) {
  3206. i = min(i, i_max);
  3207. }
  3208. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3209. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  3210. }
  3211. }
  3212. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  3213. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3214. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3215. (void)x_qh; (void)x_sc;
  3216. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3217. const float * x_dmf = (const float *) x_dm;
  3218. int u[2*VDR_Q4_0_Q8_1_MMQ];
  3219. #pragma unroll
  3220. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  3221. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3222. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  3223. }
  3224. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  3225. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  3226. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  3227. }
  3228. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  3229. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3230. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  3231. int v[VDR_Q4_1_Q8_1_MMVQ];
  3232. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  3233. #pragma unroll
  3234. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  3235. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  3236. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3237. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  3238. }
  3239. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  3240. }
  3241. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3242. (void)x_qh; (void)x_sc;
  3243. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  3244. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  3245. *x_ql = tile_x_qs;
  3246. *x_dm = tile_x_dm;
  3247. }
  3248. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  3249. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3250. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3251. (void)x_qh; (void)x_sc;
  3252. GGML_CUDA_ASSUME(i_offset >= 0);
  3253. GGML_CUDA_ASSUME(i_offset < nwarps);
  3254. GGML_CUDA_ASSUME(k >= 0);
  3255. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3256. const int kbx = k / QI4_1;
  3257. const int kqsx = k % QI4_1;
  3258. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  3259. #pragma unroll
  3260. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3261. int i = i0 + i_offset;
  3262. if (need_check) {
  3263. i = min(i, i_max);
  3264. }
  3265. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3266. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3267. }
  3268. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  3269. const int kbxd = k % blocks_per_tile_x_row;
  3270. #pragma unroll
  3271. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  3272. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  3273. if (need_check) {
  3274. i = min(i, i_max);
  3275. }
  3276. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3277. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  3278. }
  3279. }
  3280. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  3281. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3282. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3283. (void)x_qh; (void)x_sc;
  3284. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3285. int u[2*VDR_Q4_1_Q8_1_MMQ];
  3286. #pragma unroll
  3287. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  3288. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3289. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  3290. }
  3291. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  3292. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  3293. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  3294. }
  3295. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  3296. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3297. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  3298. int vl[VDR_Q5_0_Q8_1_MMVQ];
  3299. int vh[VDR_Q5_0_Q8_1_MMVQ];
  3300. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  3301. #pragma unroll
  3302. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  3303. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  3304. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  3305. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3306. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  3307. }
  3308. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  3309. }
  3310. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3311. (void)x_qh; (void)x_sc;
  3312. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3313. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  3314. *x_ql = tile_x_ql;
  3315. *x_dm = (half2 *) tile_x_d;
  3316. }
  3317. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  3318. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3319. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3320. (void)x_qh; (void)x_sc;
  3321. GGML_CUDA_ASSUME(i_offset >= 0);
  3322. GGML_CUDA_ASSUME(i_offset < nwarps);
  3323. GGML_CUDA_ASSUME(k >= 0);
  3324. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3325. const int kbx = k / QI5_0;
  3326. const int kqsx = k % QI5_0;
  3327. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  3328. #pragma unroll
  3329. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3330. int i = i0 + i_offset;
  3331. if (need_check) {
  3332. i = min(i, i_max);
  3333. }
  3334. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3335. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  3336. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  3337. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3338. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3339. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3340. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3341. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3342. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  3343. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3344. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3345. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3346. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3347. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3348. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3349. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  3350. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3351. }
  3352. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  3353. const int kbxd = k % blocks_per_tile_x_row;
  3354. float * x_dmf = (float *) x_dm;
  3355. #pragma unroll
  3356. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  3357. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  3358. if (need_check) {
  3359. i = min(i, i_max);
  3360. }
  3361. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3362. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  3363. }
  3364. }
  3365. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  3366. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3367. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3368. (void)x_qh; (void)x_sc;
  3369. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3370. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  3371. const float * x_dmf = (const float *) x_dm;
  3372. const float * y_df = (const float *) y_ds;
  3373. int u[2*VDR_Q5_0_Q8_1_MMQ];
  3374. #pragma unroll
  3375. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  3376. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3377. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  3378. }
  3379. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  3380. (&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)]);
  3381. }
  3382. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  3383. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3384. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  3385. int vl[VDR_Q5_1_Q8_1_MMVQ];
  3386. int vh[VDR_Q5_1_Q8_1_MMVQ];
  3387. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  3388. #pragma unroll
  3389. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  3390. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  3391. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  3392. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3393. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  3394. }
  3395. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  3396. }
  3397. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3398. (void)x_qh; (void)x_sc;
  3399. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3400. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  3401. *x_ql = tile_x_ql;
  3402. *x_dm = tile_x_dm;
  3403. }
  3404. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  3405. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3406. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3407. (void)x_qh; (void)x_sc;
  3408. GGML_CUDA_ASSUME(i_offset >= 0);
  3409. GGML_CUDA_ASSUME(i_offset < nwarps);
  3410. GGML_CUDA_ASSUME(k >= 0);
  3411. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3412. const int kbx = k / QI5_1;
  3413. const int kqsx = k % QI5_1;
  3414. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  3415. #pragma unroll
  3416. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3417. int i = i0 + i_offset;
  3418. if (need_check) {
  3419. i = min(i, i_max);
  3420. }
  3421. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3422. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3423. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  3424. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3425. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3426. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3427. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3428. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3429. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3430. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3431. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3432. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3433. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3434. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3435. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3436. }
  3437. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  3438. const int kbxd = k % blocks_per_tile_x_row;
  3439. #pragma unroll
  3440. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  3441. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  3442. if (need_check) {
  3443. i = min(i, i_max);
  3444. }
  3445. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3446. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  3447. }
  3448. }
  3449. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  3450. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3451. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3452. (void)x_qh; (void)x_sc;
  3453. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3454. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  3455. int u[2*VDR_Q5_1_Q8_1_MMQ];
  3456. #pragma unroll
  3457. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  3458. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3459. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  3460. }
  3461. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  3462. (&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)]);
  3463. }
  3464. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  3465. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3466. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  3467. int v[VDR_Q8_0_Q8_1_MMVQ];
  3468. int u[VDR_Q8_0_Q8_1_MMVQ];
  3469. #pragma unroll
  3470. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  3471. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  3472. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3473. }
  3474. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  3475. }
  3476. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3477. (void)x_qh; (void)x_sc;
  3478. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3479. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  3480. *x_ql = tile_x_qs;
  3481. *x_dm = (half2 *) tile_x_d;
  3482. }
  3483. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  3484. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3485. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3486. (void)x_qh; (void)x_sc;
  3487. GGML_CUDA_ASSUME(i_offset >= 0);
  3488. GGML_CUDA_ASSUME(i_offset < nwarps);
  3489. GGML_CUDA_ASSUME(k >= 0);
  3490. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3491. const int kbx = k / QI8_0;
  3492. const int kqsx = k % QI8_0;
  3493. float * x_dmf = (float *) x_dm;
  3494. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  3495. #pragma unroll
  3496. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3497. int i = i0 + i_offset;
  3498. if (need_check) {
  3499. i = min(i, i_max);
  3500. }
  3501. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3502. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  3503. }
  3504. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  3505. const int kbxd = k % blocks_per_tile_x_row;
  3506. #pragma unroll
  3507. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  3508. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  3509. if (need_check) {
  3510. i = min(i, i_max);
  3511. }
  3512. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3513. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  3514. }
  3515. }
  3516. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  3517. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3518. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3519. (void)x_qh; (void)x_sc;
  3520. const float * x_dmf = (const float *) x_dm;
  3521. const float * y_df = (const float *) y_ds;
  3522. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  3523. (&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],
  3524. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  3525. }
  3526. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  3527. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3528. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  3529. const int bq8_offset = QR2_K * (iqs / QI8_1);
  3530. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3531. const uint8_t * scales = bq2_K->scales + scale_offset;
  3532. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  3533. int u[QR2_K];
  3534. float d8[QR2_K];
  3535. #pragma unroll
  3536. for (int i = 0; i < QR2_K; ++ i) {
  3537. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3538. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3539. }
  3540. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  3541. }
  3542. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3543. (void)x_qh;
  3544. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3545. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  3546. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3547. *x_ql = tile_x_ql;
  3548. *x_dm = tile_x_dm;
  3549. *x_sc = tile_x_sc;
  3550. }
  3551. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  3552. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3553. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3554. (void)x_qh;
  3555. GGML_CUDA_ASSUME(i_offset >= 0);
  3556. GGML_CUDA_ASSUME(i_offset < nwarps);
  3557. GGML_CUDA_ASSUME(k >= 0);
  3558. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3559. const int kbx = k / QI2_K;
  3560. const int kqsx = k % QI2_K;
  3561. const block_q2_K * bx0 = (const block_q2_K *) vx;
  3562. #pragma unroll
  3563. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3564. int i = i0 + i_offset;
  3565. if (need_check) {
  3566. i = min(i, i_max);
  3567. }
  3568. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  3569. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3570. }
  3571. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  3572. const int kbxd = k % blocks_per_tile_x_row;
  3573. #pragma unroll
  3574. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  3575. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  3576. if (need_check) {
  3577. i = min(i, i_max);
  3578. }
  3579. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3580. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  3581. }
  3582. #pragma unroll
  3583. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3584. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3585. if (need_check) {
  3586. i = min(i, i_max);
  3587. }
  3588. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  3589. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  3590. }
  3591. }
  3592. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  3593. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3594. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3595. (void)x_qh;
  3596. const int kbx = k / QI2_K;
  3597. const int ky = (k % QI2_K) * QR2_K;
  3598. const float * y_df = (const float *) y_ds;
  3599. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  3600. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  3601. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  3602. #pragma unroll
  3603. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  3604. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3605. }
  3606. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  3607. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  3608. 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]);
  3609. }
  3610. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  3611. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3612. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  3613. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  3614. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3615. const float d = bq3_K->d;
  3616. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  3617. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3618. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  3619. int u[QR3_K];
  3620. float d8[QR3_K];
  3621. #pragma unroll
  3622. for (int i = 0; i < QR3_K; ++i) {
  3623. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3624. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3625. }
  3626. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  3627. }
  3628. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3629. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3630. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  3631. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  3632. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3633. *x_ql = tile_x_ql;
  3634. *x_dm = tile_x_dm;
  3635. *x_qh = tile_x_qh;
  3636. *x_sc = tile_x_sc;
  3637. }
  3638. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  3639. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3640. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3641. GGML_CUDA_ASSUME(i_offset >= 0);
  3642. GGML_CUDA_ASSUME(i_offset < nwarps);
  3643. GGML_CUDA_ASSUME(k >= 0);
  3644. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3645. const int kbx = k / QI3_K;
  3646. const int kqsx = k % QI3_K;
  3647. const block_q3_K * bx0 = (const block_q3_K *) vx;
  3648. #pragma unroll
  3649. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3650. int i = i0 + i_offset;
  3651. if (need_check) {
  3652. i = min(i, i_max);
  3653. }
  3654. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  3655. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3656. }
  3657. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  3658. const int kbxd = k % blocks_per_tile_x_row;
  3659. float * x_dmf = (float *) x_dm;
  3660. #pragma unroll
  3661. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  3662. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  3663. if (need_check) {
  3664. i = min(i, i_max);
  3665. }
  3666. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3667. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  3668. }
  3669. #pragma unroll
  3670. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  3671. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  3672. if (need_check) {
  3673. i = min(i, i_max);
  3674. }
  3675. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  3676. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3677. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  3678. }
  3679. #pragma unroll
  3680. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3681. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3682. if (need_check) {
  3683. i = min(i, i_max);
  3684. }
  3685. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  3686. const int ksc = k % (QI3_K/4);
  3687. const int ksc_low = ksc % (QI3_K/8);
  3688. const int shift_low = 4 * (ksc / (QI3_K/8));
  3689. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3690. const int ksc_high = QI3_K/8;
  3691. const int shift_high = 2 * ksc;
  3692. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3693. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3694. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3695. }
  3696. }
  3697. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3698. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3699. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3700. const int kbx = k / QI3_K;
  3701. const int ky = (k % QI3_K) * QR3_K;
  3702. const float * x_dmf = (const float *) x_dm;
  3703. const float * y_df = (const float *) y_ds;
  3704. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3705. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3706. #pragma unroll
  3707. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3708. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3709. const int shift = 2 * ((ky % 32) / 8);
  3710. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3711. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3712. const int vlh = (vh << 2) & 0x04040404;
  3713. v[l] = __vsubss4(vll, vlh);
  3714. }
  3715. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3716. 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]);
  3717. }
  3718. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3719. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3720. #ifndef GGML_QKK_64
  3721. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3722. int v[2];
  3723. int u[2*QR4_K];
  3724. float d8[QR4_K];
  3725. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3726. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3727. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3728. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3729. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3730. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3731. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3732. v[0] = q4[0];
  3733. v[1] = q4[4];
  3734. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3735. uint16_t aux[2];
  3736. const int j = bq8_offset/2;
  3737. if (j < 2) {
  3738. aux[0] = scales[j+0] & 0x3f3f;
  3739. aux[1] = scales[j+2] & 0x3f3f;
  3740. } else {
  3741. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3742. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3743. }
  3744. const uint8_t * sc = (const uint8_t *)aux;
  3745. const uint8_t * m = sc + 2;
  3746. for (int i = 0; i < QR4_K; ++i) {
  3747. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3748. d8[i] = __low2half(bq8i->ds);
  3749. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3750. u[2*i+0] = q8[0];
  3751. u[2*i+1] = q8[4];
  3752. }
  3753. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3754. #else
  3755. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3756. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3757. float sumf_d = 0.0f;
  3758. float sumf_m = 0.0f;
  3759. uint16_t aux16[2];
  3760. const uint8_t * s = (const uint8_t *)aux16;
  3761. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3762. aux16[0] = a[0] & 0x0f0f;
  3763. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3764. const float dall = bq4_K->dm[0];
  3765. const float dmin = bq4_K->dm[1];
  3766. const float d8_1 = __low2float(bq8_1[0].ds);
  3767. const float d8_2 = __low2float(bq8_1[1].ds);
  3768. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3769. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3770. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3771. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3772. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3773. const int v1 = q4[0];
  3774. const int v2 = q4[4];
  3775. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3776. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3777. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3778. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3779. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3780. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3781. return dall * sumf_d - dmin * sumf_m;
  3782. #else
  3783. NO_DEVICE_CODE;
  3784. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3785. #endif
  3786. }
  3787. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3788. (void)x_qh;
  3789. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3790. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3791. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3792. *x_ql = tile_x_ql;
  3793. *x_dm = tile_x_dm;
  3794. *x_sc = tile_x_sc;
  3795. }
  3796. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3797. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3798. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3799. (void)x_qh;
  3800. GGML_CUDA_ASSUME(i_offset >= 0);
  3801. GGML_CUDA_ASSUME(i_offset < nwarps);
  3802. GGML_CUDA_ASSUME(k >= 0);
  3803. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3804. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3805. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3806. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3807. #pragma unroll
  3808. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3809. int i = i0 + i_offset;
  3810. if (need_check) {
  3811. i = min(i, i_max);
  3812. }
  3813. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3814. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3815. }
  3816. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3817. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3818. #pragma unroll
  3819. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3820. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3821. if (need_check) {
  3822. i = min(i, i_max);
  3823. }
  3824. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3825. #if QK_K == 256
  3826. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3827. #else
  3828. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3829. #endif
  3830. }
  3831. #pragma unroll
  3832. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3833. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3834. if (need_check) {
  3835. i = min(i, i_max);
  3836. }
  3837. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3838. const int * scales = (const int *) bxi->scales;
  3839. const int ksc = k % (WARP_SIZE/8);
  3840. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3841. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3842. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3843. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3844. }
  3845. }
  3846. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3847. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3848. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3849. (void)x_qh;
  3850. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3851. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3852. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3853. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3854. }
  3855. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3856. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3857. #ifndef GGML_QKK_64
  3858. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3859. int vl[2];
  3860. int vh[2];
  3861. int u[2*QR5_K];
  3862. float d8[QR5_K];
  3863. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3864. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3865. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3866. vl[0] = ql[0];
  3867. vl[1] = ql[4];
  3868. vh[0] = qh[0] >> bq8_offset;
  3869. vh[1] = qh[4] >> bq8_offset;
  3870. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3871. uint16_t aux[2];
  3872. const int j = bq8_offset/2;
  3873. if (j < 2) {
  3874. aux[0] = scales[j+0] & 0x3f3f;
  3875. aux[1] = scales[j+2] & 0x3f3f;
  3876. } else {
  3877. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3878. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3879. }
  3880. const uint8_t * sc = (const uint8_t *)aux;
  3881. const uint8_t * m = sc + 2;
  3882. #pragma unroll
  3883. for (int i = 0; i < QR5_K; ++i) {
  3884. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3885. d8[i] = __low2float(bq8i->ds);
  3886. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3887. u[2*i+0] = q8[0];
  3888. u[2*i+1] = q8[4];
  3889. }
  3890. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3891. #else
  3892. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3893. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3894. const int8_t * s = bq5_K->scales;
  3895. const float d = bq5_K->d;
  3896. const float d8_1 = __low2half(bq8_1[0].ds);
  3897. const float d8_2 = __low2half(bq8_1[1].ds);
  3898. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3899. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3900. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3901. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3902. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3903. const int vl1 = ql[0];
  3904. const int vl2 = ql[4];
  3905. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3906. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3907. const int in = step%8; // 0, 4, 0, 4
  3908. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3909. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3910. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3911. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3912. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3913. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3914. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3915. return d * sumf_d;
  3916. #else
  3917. NO_DEVICE_CODE;
  3918. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3919. #endif
  3920. }
  3921. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3922. (void)x_qh;
  3923. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3924. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3925. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3926. *x_ql = tile_x_ql;
  3927. *x_dm = tile_x_dm;
  3928. *x_sc = tile_x_sc;
  3929. }
  3930. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3931. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3932. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3933. (void)x_qh;
  3934. GGML_CUDA_ASSUME(i_offset >= 0);
  3935. GGML_CUDA_ASSUME(i_offset < nwarps);
  3936. GGML_CUDA_ASSUME(k >= 0);
  3937. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3938. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3939. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3940. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3941. #pragma unroll
  3942. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3943. int i = i0 + i_offset;
  3944. if (need_check) {
  3945. i = min(i, i_max);
  3946. }
  3947. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3948. const int ky = QR5_K*kqsx;
  3949. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3950. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3951. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3952. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3953. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3954. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3955. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3956. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3957. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3958. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3959. }
  3960. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3961. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3962. #pragma unroll
  3963. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3964. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3965. if (need_check) {
  3966. i = min(i, i_max);
  3967. }
  3968. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3969. #if QK_K == 256
  3970. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3971. #endif
  3972. }
  3973. #pragma unroll
  3974. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3975. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3976. if (need_check) {
  3977. i = min(i, i_max);
  3978. }
  3979. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3980. const int * scales = (const int *) bxi->scales;
  3981. const int ksc = k % (WARP_SIZE/8);
  3982. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3983. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3984. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3985. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3986. }
  3987. }
  3988. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3989. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3990. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3991. (void)x_qh;
  3992. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3993. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3994. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3995. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3996. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3997. }
  3998. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3999. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4000. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  4001. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  4002. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  4003. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  4004. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  4005. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  4006. const int8_t * scales = bq6_K->scales + scale_offset;
  4007. int u[QR6_K];
  4008. float d8[QR6_K];
  4009. #pragma unroll
  4010. for (int i = 0; i < QR6_K; ++i) {
  4011. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  4012. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  4013. }
  4014. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  4015. }
  4016. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  4017. (void)x_qh;
  4018. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  4019. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  4020. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  4021. *x_ql = tile_x_ql;
  4022. *x_dm = tile_x_dm;
  4023. *x_sc = tile_x_sc;
  4024. }
  4025. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  4026. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  4027. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  4028. (void)x_qh;
  4029. GGML_CUDA_ASSUME(i_offset >= 0);
  4030. GGML_CUDA_ASSUME(i_offset < nwarps);
  4031. GGML_CUDA_ASSUME(k >= 0);
  4032. GGML_CUDA_ASSUME(k < WARP_SIZE);
  4033. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  4034. const int kqsx = k % QI6_K; // == k if QK_K == 256
  4035. const block_q6_K * bx0 = (const block_q6_K *) vx;
  4036. #pragma unroll
  4037. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  4038. int i = i0 + i_offset;
  4039. if (need_check) {
  4040. i = min(i, i_max);
  4041. }
  4042. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  4043. const int ky = QR6_K*kqsx;
  4044. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  4045. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  4046. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  4047. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  4048. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  4049. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  4050. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  4051. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  4052. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  4053. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  4054. }
  4055. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  4056. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  4057. float * x_dmf = (float *) x_dm;
  4058. #pragma unroll
  4059. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  4060. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  4061. if (need_check) {
  4062. i = min(i, i_max);
  4063. }
  4064. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  4065. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  4066. }
  4067. #pragma unroll
  4068. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  4069. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  4070. if (need_check) {
  4071. i = min(i, i_max);
  4072. }
  4073. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  4074. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  4075. }
  4076. }
  4077. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  4078. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  4079. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  4080. (void)x_qh;
  4081. const float * x_dmf = (const float *) x_dm;
  4082. const float * y_df = (const float *) y_ds;
  4083. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  4084. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  4085. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  4086. 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]);
  4087. }
  4088. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  4089. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4090. #if QK_K == 256
  4091. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  4092. #if QR2_XXS == 8
  4093. const int ib32 = iqs;
  4094. const uint16_t * q2 = bq2->qs + 4*ib32;
  4095. const uint8_t * aux8 = (const uint8_t *)q2;
  4096. const int8_t * q8 = bq8_1[ib32].qs;
  4097. uint32_t aux32 = q2[2] | (q2[3] << 16);
  4098. int sumi = 0;
  4099. for (int l = 0; l < 4; ++l) {
  4100. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  4101. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  4102. for (int j = 0; j < 8; ++j) {
  4103. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  4104. }
  4105. q8 += 8;
  4106. aux32 >>= 7;
  4107. }
  4108. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  4109. return d * sumi;
  4110. #else
  4111. // iqs is 0...15
  4112. const int ib32 = iqs/2;
  4113. const int il = iqs%2;
  4114. const uint16_t * q2 = bq2->qs + 4*ib32;
  4115. const uint8_t * aux8 = (const uint8_t *)q2;
  4116. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  4117. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  4118. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  4119. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  4120. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  4121. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  4122. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  4123. int sumi1 = 0, sumi2 = 0;
  4124. for (int j = 0; j < 8; ++j) {
  4125. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  4126. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  4127. }
  4128. return d * (sumi1 + sumi2);
  4129. #endif
  4130. #else
  4131. assert(false);
  4132. return 0.f;
  4133. #endif
  4134. }
  4135. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  4136. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4137. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4138. #if QK_K == 256
  4139. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  4140. const int ib32 = iqs;
  4141. const uint16_t * q2 = bq2->qs + 4*ib32;
  4142. const int8_t * q8 = bq8_1[ib32].qs;
  4143. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  4144. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  4145. int sumi1 = 0;
  4146. for (int l = 0; l < 2; ++l) {
  4147. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  4148. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  4149. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  4150. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  4151. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  4152. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  4153. q8 += 8;
  4154. }
  4155. int sumi2 = 0;
  4156. for (int l = 2; l < 4; ++l) {
  4157. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  4158. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  4159. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  4160. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  4161. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  4162. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  4163. q8 += 8;
  4164. }
  4165. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  4166. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  4167. #else
  4168. (void) ksigns64;
  4169. assert(false);
  4170. return 0.f;
  4171. #endif
  4172. #else
  4173. (void) ksigns64;
  4174. assert(false);
  4175. return 0.f;
  4176. #endif
  4177. }
  4178. // TODO
  4179. static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(
  4180. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4181. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4182. #if QK_K == 256
  4183. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  4184. const int ib32 = iqs;
  4185. const int8_t * q8 = bq8_1[ib32].qs;
  4186. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  4187. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  4188. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  4189. int sumi1 = 0;
  4190. for (int l = 0; l < 2; ++l) {
  4191. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  4192. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4193. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4194. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  4195. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  4196. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  4197. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  4198. q8 += 8;
  4199. }
  4200. int sumi2 = 0;
  4201. for (int l = 2; l < 4; ++l) {
  4202. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  4203. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4204. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4205. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  4206. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  4207. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  4208. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  4209. q8 += 8;
  4210. }
  4211. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  4212. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  4213. #else
  4214. (void) ksigns64;
  4215. assert(false);
  4216. return 0.f;
  4217. #endif
  4218. #else
  4219. (void) ksigns64;
  4220. assert(false);
  4221. return 0.f;
  4222. #endif
  4223. }
  4224. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  4225. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4226. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4227. #if QK_K == 256
  4228. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  4229. const int ib32 = iqs;
  4230. const uint8_t * q3 = bq2->qs + 8*ib32;
  4231. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  4232. const int8_t * q8 = bq8_1[ib32].qs;
  4233. uint32_t aux32 = gas[0] | (gas[1] << 16);
  4234. int sumi = 0;
  4235. for (int l = 0; l < 4; ++l) {
  4236. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  4237. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  4238. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  4239. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  4240. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  4241. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  4242. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  4243. q8 += 8;
  4244. aux32 >>= 7;
  4245. }
  4246. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  4247. return d * sumi;
  4248. #else
  4249. assert(false);
  4250. return 0.f;
  4251. #endif
  4252. #else
  4253. assert(false);
  4254. return 0.f;
  4255. #endif
  4256. }
  4257. // TODO: don't use lookup table for signs
  4258. static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(
  4259. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4260. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4261. #if QK_K == 256
  4262. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  4263. const int ib32 = iqs;
  4264. const uint8_t * qs = bq2->qs + 8*ib32;
  4265. const int8_t * q8 = bq8_1[ib32].qs;
  4266. int sumi = 0;
  4267. for (int l = 0; l < 4; ++l) {
  4268. const uint32_t * grid1 = iq3xs_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  4269. const uint32_t * grid2 = iq3xs_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  4270. uint32_t signs0 = __vcmpeq4(((bq2->signs[4*ib32+l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4271. uint32_t signs1 = __vcmpeq4(((bq2->signs[4*ib32+l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4272. const int grid_l = __vsub4(grid1[0] ^ signs0, signs0);
  4273. const int grid_h = __vsub4(grid2[0] ^ signs1, signs1);
  4274. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  4275. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  4276. q8 += 8;
  4277. }
  4278. const float d = (float)bq2->d * (0.5f + ((bq2->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * __low2float(bq8_1[ib32].ds) * 0.5f;
  4279. return d * sumi;
  4280. #else
  4281. assert(false);
  4282. return 0.f;
  4283. #endif
  4284. #else
  4285. assert(false);
  4286. return 0.f;
  4287. #endif
  4288. }
  4289. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  4290. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4291. #if QK_K == 256
  4292. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  4293. const int ib32 = iqs;
  4294. int sumi1 = 0, sumi2 = 0, sumi3 = 0, sumi4 = 0;
  4295. const uint8_t h1 = bq1->scales[2*ib32+0];
  4296. const uint8_t h2 = bq1->scales[2*ib32+1];
  4297. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4298. const int * q8 = (const int *)bq8_1[ib32].qs;
  4299. const int * grid1 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  4300. const int * grid2 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  4301. const int * grid3 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  4302. const int * grid4 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  4303. for (int j = 0; j < 2; ++j) {
  4304. sumi1 = __dp4a(q8[j+0], grid1[j], sumi1);
  4305. sumi2 = __dp4a(q8[j+2], grid2[j], sumi2);
  4306. sumi3 = __dp4a(q8[j+4], grid3[j], sumi3);
  4307. sumi4 = __dp4a(q8[j+6], grid4[j], sumi4);
  4308. }
  4309. #else
  4310. const int8_t * q8 = bq8_1[ib32].qs;
  4311. const int8_t * grid1 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  4312. const int8_t * grid2 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  4313. const int8_t * grid3 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  4314. const int8_t * grid4 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  4315. for (int j = 0; j < 8; ++j) {
  4316. sumi1 += q8[j+ 0] * grid1[j];
  4317. sumi2 += q8[j+ 8] * grid2[j];
  4318. sumi3 += q8[j+16] * grid3[j];
  4319. sumi4 += q8[j+24] * grid4[j];
  4320. }
  4321. #endif
  4322. const float d = (float)bq1->d * __low2float(bq8_1[ib32].ds);
  4323. return d * (sumi1 * (2*(h1 & 7) + 1) + sumi2 * (2*((h1 >> 4) & 7) + 1) +
  4324. sumi3 * (2*(h2 & 7) + 1) + sumi4 * (2*((h2 >> 4) & 7) + 1));
  4325. #else
  4326. assert(false);
  4327. return 0.f;
  4328. #endif
  4329. }
  4330. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4331. static __device__ __forceinline__ void get_int_from_table_16(const uint32_t & q4, const uint8_t * values,
  4332. int & val1, int & val2) {
  4333. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  4334. aux32 = q4 & 0x0f0f0f0f;
  4335. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  4336. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  4337. val1 = v1 | (v2 << 16);
  4338. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  4339. v1 = values[q8[0]] | (values[q8[1]] << 8);
  4340. v2 = values[q8[2]] | (values[q8[3]] << 8);
  4341. val2 = v1 | (v2 << 16);
  4342. }
  4343. #endif
  4344. static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
  4345. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4346. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  4347. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4348. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  4349. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  4350. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  4351. int v1, v2;
  4352. int sumi1 = 0, sumi2 = 0;
  4353. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  4354. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  4355. get_int_from_table_16(aux, values, v1, v2);
  4356. sumi1 = __dp4a(v1, q8[l+0], sumi1);
  4357. sumi2 = __dp4a(v2, q8[l+4], sumi2);
  4358. }
  4359. #else
  4360. const uint8_t * q4 = bq->qs + 4*iqs;
  4361. const int8_t * q8 = bq8_1->qs + 4*iqs;
  4362. int sumi1 = 0, sumi2 = 0;
  4363. for (int l = 0; l < 4*VDR_Q4_0_Q8_1_MMVQ; ++l) {
  4364. sumi1 += q8[l+ 0] * kvalues_iq4nl[q4[l] & 0xf];
  4365. sumi2 += q8[l+16] * kvalues_iq4nl[q4[l] >> 4];
  4366. }
  4367. #endif
  4368. const float d = (float)bq->d * __low2float(bq8_1->ds);
  4369. return d * (sumi1 + sumi2);
  4370. }
  4371. static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(
  4372. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4373. #if QK_K == 256
  4374. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4375. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  4376. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  4377. //// iqs is 0...7
  4378. //const int ib64 = iqs/2;
  4379. //const int il = iqs%2;
  4380. //const int32_t * q8_1 = (const int *)bq8_1[2*ib64+0].qs + 2*il;
  4381. //const int32_t * q8_2 = (const int *)bq8_1[2*ib64+1].qs + 2*il;
  4382. //const uint32_t * q4_1 = (const uint32_t *)bq4->qs + 8*ib64 + 2*il;
  4383. //const uint32_t * q4_2 = q4_1 + 4;
  4384. //const int8_t ls1 = (bq4->scales_l[ib64] & 0xf) | (((bq4->scales_h >> (4*ib64+0)) & 3) << 4);
  4385. //const int8_t ls2 = (bq4->scales_l[ib64] >> 4) | (((bq4->scales_h >> (4*ib64+2)) & 3) << 4);
  4386. //const float d1 = (float)bq4->d * (ls1 - 32) * __low2float(bq8_1[2*ib64+0].ds);
  4387. //const float d2 = (float)bq4->d * (ls2 - 32) * __low2float(bq8_1[2*ib64+1].ds);
  4388. //int v1, v2;
  4389. //int sumi1 = 0, sumi2 = 0;
  4390. //for (int j = 0; j < 2; ++j) {
  4391. // get_int_from_table_16(q4_1[j], values, v1, v2);
  4392. // sumi1 = __dp4a(v2, q8_1[j+4], __dp4a(v1, q8_1[j+0], sumi1));
  4393. // get_int_from_table_16(q4_2[j], values, v1, v2);
  4394. // sumi2 = __dp4a(v2, q8_2[j+4], __dp4a(v1, q8_2[j+0], sumi2));
  4395. //}
  4396. //return d1 * sumi1 + d2 * sumi2;
  4397. // iqs is 0...7
  4398. const int ib32 = iqs;
  4399. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  4400. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  4401. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  4402. const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  4403. int v1, v2;
  4404. int sumi1 = 0, sumi2 = 0;
  4405. for (int j = 0; j < 4; ++j) {
  4406. get_int_from_table_16(q4[j], values, v1, v2);
  4407. sumi1 = __dp4a(v1, q8[j+0], sumi1);
  4408. sumi2 = __dp4a(v2, q8[j+4], sumi2);
  4409. }
  4410. return d * (sumi1 + sumi2);
  4411. //// iqs is 0...15
  4412. //const int ib32 = iqs/2;
  4413. //const int il = iqs%2;
  4414. //const int32_t * q8 = (const int *)bq8_1[ib32].qs + 2*il;
  4415. //const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32 + 2*il;
  4416. //const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  4417. //const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  4418. //int v1, v2;
  4419. //int sumi1 = 0, sumi2 = 0;
  4420. //for (int j = 0; j < 2; ++j) {
  4421. // get_int_from_table_16(q4[j], values, v1, v2);
  4422. // sumi1 = __dp4a(v1, q8[j+0], sumi1);
  4423. // sumi2 = __dp4a(v2, q8[j+4], sumi2);
  4424. //}
  4425. //return d * (sumi1 + sumi2);
  4426. #else
  4427. assert(false);
  4428. return 0.f;
  4429. #endif
  4430. #else
  4431. return vec_dot_iq4_xs_q8_1(vbq, bq8_1, iqs);
  4432. #endif
  4433. }
  4434. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  4435. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  4436. static __device__ __forceinline__ void mul_mat_q(
  4437. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4438. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4439. const block_q_t * x = (const block_q_t *) vx;
  4440. const block_q8_1 * y = (const block_q8_1 *) vy;
  4441. const int blocks_per_row_x = ncols_x / qk;
  4442. const int blocks_per_col_y = nrows_y / QK8_1;
  4443. const int blocks_per_warp = WARP_SIZE / qi;
  4444. const int & ncols_dst = ncols_y;
  4445. const int row_dst_0 = blockIdx.x*mmq_y;
  4446. const int & row_x_0 = row_dst_0;
  4447. const int col_dst_0 = blockIdx.y*mmq_x;
  4448. const int & col_y_0 = col_dst_0;
  4449. int * tile_x_ql = nullptr;
  4450. half2 * tile_x_dm = nullptr;
  4451. int * tile_x_qh = nullptr;
  4452. int * tile_x_sc = nullptr;
  4453. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  4454. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  4455. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  4456. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  4457. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  4458. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  4459. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  4460. #pragma unroll
  4461. for (int ir = 0; ir < qr; ++ir) {
  4462. const int kqs = ir*WARP_SIZE + threadIdx.x;
  4463. const int kbxd = kqs / QI8_1;
  4464. #pragma unroll
  4465. for (int i = 0; i < mmq_x; i += nwarps) {
  4466. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  4467. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  4468. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  4469. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  4470. }
  4471. #pragma unroll
  4472. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  4473. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  4474. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  4475. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  4476. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  4477. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  4478. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  4479. if (need_sum) {
  4480. *dsi_dst = *dsi_src;
  4481. } else {
  4482. float * dfi_dst = (float *) dsi_dst;
  4483. *dfi_dst = __low2half(*dsi_src);
  4484. }
  4485. }
  4486. __syncthreads();
  4487. // #pragma unroll // unrolling this loop causes too much register pressure
  4488. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  4489. #pragma unroll
  4490. for (int j = 0; j < mmq_x; j += nwarps) {
  4491. #pragma unroll
  4492. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  4493. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  4494. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  4495. threadIdx.x + i, threadIdx.y + j, k);
  4496. }
  4497. }
  4498. }
  4499. __syncthreads();
  4500. }
  4501. }
  4502. #pragma unroll
  4503. for (int j = 0; j < mmq_x; j += nwarps) {
  4504. const int col_dst = col_dst_0 + j + threadIdx.y;
  4505. if (col_dst >= ncols_dst) {
  4506. return;
  4507. }
  4508. #pragma unroll
  4509. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  4510. const int row_dst = row_dst_0 + threadIdx.x + i;
  4511. if (row_dst >= nrows_dst) {
  4512. continue;
  4513. }
  4514. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  4515. }
  4516. }
  4517. }
  4518. #define MMQ_X_Q4_0_RDNA2 64
  4519. #define MMQ_Y_Q4_0_RDNA2 128
  4520. #define NWARPS_Q4_0_RDNA2 8
  4521. #define MMQ_X_Q4_0_RDNA1 64
  4522. #define MMQ_Y_Q4_0_RDNA1 64
  4523. #define NWARPS_Q4_0_RDNA1 8
  4524. #if defined(CUDA_USE_TENSOR_CORES)
  4525. #define MMQ_X_Q4_0_AMPERE 4
  4526. #define MMQ_Y_Q4_0_AMPERE 32
  4527. #define NWARPS_Q4_0_AMPERE 4
  4528. #else
  4529. #define MMQ_X_Q4_0_AMPERE 64
  4530. #define MMQ_Y_Q4_0_AMPERE 128
  4531. #define NWARPS_Q4_0_AMPERE 4
  4532. #endif
  4533. #define MMQ_X_Q4_0_PASCAL 64
  4534. #define MMQ_Y_Q4_0_PASCAL 64
  4535. #define NWARPS_Q4_0_PASCAL 8
  4536. template <bool need_check> static __global__ void
  4537. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4538. #if defined(RDNA3) || defined(RDNA2)
  4539. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  4540. #endif // defined(RDNA3) || defined(RDNA2)
  4541. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4542. mul_mat_q4_0(
  4543. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4544. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4545. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4546. #if defined(RDNA3) || defined(RDNA2)
  4547. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  4548. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  4549. const int nwarps = NWARPS_Q4_0_RDNA2;
  4550. #else
  4551. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  4552. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  4553. const int nwarps = NWARPS_Q4_0_RDNA1;
  4554. #endif // defined(RDNA3) || defined(RDNA2)
  4555. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4556. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4557. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4558. #elif __CUDA_ARCH__ >= CC_VOLTA
  4559. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  4560. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  4561. const int nwarps = NWARPS_Q4_0_AMPERE;
  4562. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4563. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4564. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4565. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4566. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  4567. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  4568. const int nwarps = NWARPS_Q4_0_PASCAL;
  4569. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4570. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4571. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4572. #else
  4573. (void) vec_dot_q4_0_q8_1_mul_mat;
  4574. NO_DEVICE_CODE;
  4575. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4576. }
  4577. #define MMQ_X_Q4_1_RDNA2 64
  4578. #define MMQ_Y_Q4_1_RDNA2 128
  4579. #define NWARPS_Q4_1_RDNA2 8
  4580. #define MMQ_X_Q4_1_RDNA1 64
  4581. #define MMQ_Y_Q4_1_RDNA1 64
  4582. #define NWARPS_Q4_1_RDNA1 8
  4583. #if defined(CUDA_USE_TENSOR_CORES)
  4584. #define MMQ_X_Q4_1_AMPERE 4
  4585. #define MMQ_Y_Q4_1_AMPERE 32
  4586. #define NWARPS_Q4_1_AMPERE 4
  4587. #else
  4588. #define MMQ_X_Q4_1_AMPERE 64
  4589. #define MMQ_Y_Q4_1_AMPERE 128
  4590. #define NWARPS_Q4_1_AMPERE 4
  4591. #endif
  4592. #define MMQ_X_Q4_1_PASCAL 64
  4593. #define MMQ_Y_Q4_1_PASCAL 64
  4594. #define NWARPS_Q4_1_PASCAL 8
  4595. template <bool need_check> static __global__ void
  4596. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4597. #if defined(RDNA3) || defined(RDNA2)
  4598. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  4599. #endif // defined(RDNA3) || defined(RDNA2)
  4600. #elif __CUDA_ARCH__ < CC_VOLTA
  4601. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  4602. #endif // __CUDA_ARCH__ < CC_VOLTA
  4603. mul_mat_q4_1(
  4604. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4605. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4606. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4607. #if defined(RDNA3) || defined(RDNA2)
  4608. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  4609. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  4610. const int nwarps = NWARPS_Q4_1_RDNA2;
  4611. #else
  4612. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  4613. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  4614. const int nwarps = NWARPS_Q4_1_RDNA1;
  4615. #endif // defined(RDNA3) || defined(RDNA2)
  4616. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4617. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4618. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4619. #elif __CUDA_ARCH__ >= CC_VOLTA
  4620. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  4621. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  4622. const int nwarps = NWARPS_Q4_1_AMPERE;
  4623. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4624. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4625. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4626. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4627. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  4628. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  4629. const int nwarps = NWARPS_Q4_1_PASCAL;
  4630. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4631. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4632. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4633. #else
  4634. (void) vec_dot_q4_1_q8_1_mul_mat;
  4635. NO_DEVICE_CODE;
  4636. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4637. }
  4638. #define MMQ_X_Q5_0_RDNA2 64
  4639. #define MMQ_Y_Q5_0_RDNA2 128
  4640. #define NWARPS_Q5_0_RDNA2 8
  4641. #define MMQ_X_Q5_0_RDNA1 64
  4642. #define MMQ_Y_Q5_0_RDNA1 64
  4643. #define NWARPS_Q5_0_RDNA1 8
  4644. #if defined(CUDA_USE_TENSOR_CORES)
  4645. #define MMQ_X_Q5_0_AMPERE 4
  4646. #define MMQ_Y_Q5_0_AMPERE 32
  4647. #define NWARPS_Q5_0_AMPERE 4
  4648. #else
  4649. #define MMQ_X_Q5_0_AMPERE 128
  4650. #define MMQ_Y_Q5_0_AMPERE 64
  4651. #define NWARPS_Q5_0_AMPERE 4
  4652. #endif
  4653. #define MMQ_X_Q5_0_PASCAL 64
  4654. #define MMQ_Y_Q5_0_PASCAL 64
  4655. #define NWARPS_Q5_0_PASCAL 8
  4656. template <bool need_check> static __global__ void
  4657. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4658. #if defined(RDNA3) || defined(RDNA2)
  4659. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  4660. #endif // defined(RDNA3) || defined(RDNA2)
  4661. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4662. mul_mat_q5_0(
  4663. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4664. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4665. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4666. #if defined(RDNA3) || defined(RDNA2)
  4667. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  4668. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  4669. const int nwarps = NWARPS_Q5_0_RDNA2;
  4670. #else
  4671. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  4672. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  4673. const int nwarps = NWARPS_Q5_0_RDNA1;
  4674. #endif // defined(RDNA3) || defined(RDNA2)
  4675. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4676. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4677. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4678. #elif __CUDA_ARCH__ >= CC_VOLTA
  4679. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  4680. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  4681. const int nwarps = NWARPS_Q5_0_AMPERE;
  4682. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4683. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4684. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4685. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4686. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  4687. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  4688. const int nwarps = NWARPS_Q5_0_PASCAL;
  4689. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4690. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4691. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4692. #else
  4693. (void) vec_dot_q5_0_q8_1_mul_mat;
  4694. NO_DEVICE_CODE;
  4695. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4696. }
  4697. #define MMQ_X_Q5_1_RDNA2 64
  4698. #define MMQ_Y_Q5_1_RDNA2 128
  4699. #define NWARPS_Q5_1_RDNA2 8
  4700. #define MMQ_X_Q5_1_RDNA1 64
  4701. #define MMQ_Y_Q5_1_RDNA1 64
  4702. #define NWARPS_Q5_1_RDNA1 8
  4703. #if defined(CUDA_USE_TENSOR_CORES)
  4704. #define MMQ_X_Q5_1_AMPERE 4
  4705. #define MMQ_Y_Q5_1_AMPERE 32
  4706. #define NWARPS_Q5_1_AMPERE 4
  4707. #else
  4708. #define MMQ_X_Q5_1_AMPERE 128
  4709. #define MMQ_Y_Q5_1_AMPERE 64
  4710. #define NWARPS_Q5_1_AMPERE 4
  4711. #endif
  4712. #define MMQ_X_Q5_1_PASCAL 64
  4713. #define MMQ_Y_Q5_1_PASCAL 64
  4714. #define NWARPS_Q5_1_PASCAL 8
  4715. template <bool need_check> static __global__ void
  4716. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4717. #if defined(RDNA3) || defined(RDNA2)
  4718. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4719. #endif // defined(RDNA3) || defined(RDNA2)
  4720. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4721. mul_mat_q5_1(
  4722. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4723. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4724. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4725. #if defined(RDNA3) || defined(RDNA2)
  4726. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4727. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4728. const int nwarps = NWARPS_Q5_1_RDNA2;
  4729. #else
  4730. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4731. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4732. const int nwarps = NWARPS_Q5_1_RDNA1;
  4733. #endif // defined(RDNA3) || defined(RDNA2)
  4734. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4735. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4736. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4737. #elif __CUDA_ARCH__ >= CC_VOLTA
  4738. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4739. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4740. const int nwarps = NWARPS_Q5_1_AMPERE;
  4741. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4742. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4743. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4744. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4745. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4746. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4747. const int nwarps = NWARPS_Q5_1_PASCAL;
  4748. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4749. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4750. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4751. #else
  4752. (void) vec_dot_q5_1_q8_1_mul_mat;
  4753. NO_DEVICE_CODE;
  4754. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4755. }
  4756. #define MMQ_X_Q8_0_RDNA2 64
  4757. #define MMQ_Y_Q8_0_RDNA2 128
  4758. #define NWARPS_Q8_0_RDNA2 8
  4759. #define MMQ_X_Q8_0_RDNA1 64
  4760. #define MMQ_Y_Q8_0_RDNA1 64
  4761. #define NWARPS_Q8_0_RDNA1 8
  4762. #if defined(CUDA_USE_TENSOR_CORES)
  4763. #define MMQ_X_Q8_0_AMPERE 4
  4764. #define MMQ_Y_Q8_0_AMPERE 32
  4765. #define NWARPS_Q8_0_AMPERE 4
  4766. #else
  4767. #define MMQ_X_Q8_0_AMPERE 128
  4768. #define MMQ_Y_Q8_0_AMPERE 64
  4769. #define NWARPS_Q8_0_AMPERE 4
  4770. #endif
  4771. #define MMQ_X_Q8_0_PASCAL 64
  4772. #define MMQ_Y_Q8_0_PASCAL 64
  4773. #define NWARPS_Q8_0_PASCAL 8
  4774. template <bool need_check> static __global__ void
  4775. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4776. #if defined(RDNA3) || defined(RDNA2)
  4777. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4778. #endif // defined(RDNA3) || defined(RDNA2)
  4779. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4780. mul_mat_q8_0(
  4781. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4782. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4783. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4784. #if defined(RDNA3) || defined(RDNA2)
  4785. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4786. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4787. const int nwarps = NWARPS_Q8_0_RDNA2;
  4788. #else
  4789. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4790. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4791. const int nwarps = NWARPS_Q8_0_RDNA1;
  4792. #endif // defined(RDNA3) || defined(RDNA2)
  4793. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4794. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4795. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4796. #elif __CUDA_ARCH__ >= CC_VOLTA
  4797. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4798. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4799. const int nwarps = NWARPS_Q8_0_AMPERE;
  4800. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4801. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4802. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4803. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4804. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4805. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4806. const int nwarps = NWARPS_Q8_0_PASCAL;
  4807. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4808. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4809. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4810. #else
  4811. (void) vec_dot_q8_0_q8_1_mul_mat;
  4812. NO_DEVICE_CODE;
  4813. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4814. }
  4815. #define MMQ_X_Q2_K_RDNA2 64
  4816. #define MMQ_Y_Q2_K_RDNA2 128
  4817. #define NWARPS_Q2_K_RDNA2 8
  4818. #define MMQ_X_Q2_K_RDNA1 128
  4819. #define MMQ_Y_Q2_K_RDNA1 32
  4820. #define NWARPS_Q2_K_RDNA1 8
  4821. #if defined(CUDA_USE_TENSOR_CORES)
  4822. #define MMQ_X_Q2_K_AMPERE 4
  4823. #define MMQ_Y_Q2_K_AMPERE 32
  4824. #define NWARPS_Q2_K_AMPERE 4
  4825. #else
  4826. #define MMQ_X_Q2_K_AMPERE 64
  4827. #define MMQ_Y_Q2_K_AMPERE 128
  4828. #define NWARPS_Q2_K_AMPERE 4
  4829. #endif
  4830. #define MMQ_X_Q2_K_PASCAL 64
  4831. #define MMQ_Y_Q2_K_PASCAL 64
  4832. #define NWARPS_Q2_K_PASCAL 8
  4833. template <bool need_check> static __global__ void
  4834. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4835. #if defined(RDNA3) || defined(RDNA2)
  4836. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4837. #endif // defined(RDNA3) || defined(RDNA2)
  4838. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4839. mul_mat_q2_K(
  4840. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4841. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4842. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4843. #if defined(RDNA3) || defined(RDNA2)
  4844. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4845. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4846. const int nwarps = NWARPS_Q2_K_RDNA2;
  4847. #else
  4848. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4849. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4850. const int nwarps = NWARPS_Q2_K_RDNA1;
  4851. #endif // defined(RDNA3) || defined(RDNA2)
  4852. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4853. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4854. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4855. #elif __CUDA_ARCH__ >= CC_VOLTA
  4856. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4857. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4858. const int nwarps = NWARPS_Q2_K_AMPERE;
  4859. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4860. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4861. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4862. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4863. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4864. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4865. const int nwarps = NWARPS_Q2_K_PASCAL;
  4866. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4867. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4868. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4869. #else
  4870. (void) vec_dot_q2_K_q8_1_mul_mat;
  4871. NO_DEVICE_CODE;
  4872. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4873. }
  4874. #define MMQ_X_Q3_K_RDNA2 128
  4875. #define MMQ_Y_Q3_K_RDNA2 64
  4876. #define NWARPS_Q3_K_RDNA2 8
  4877. #define MMQ_X_Q3_K_RDNA1 32
  4878. #define MMQ_Y_Q3_K_RDNA1 128
  4879. #define NWARPS_Q3_K_RDNA1 8
  4880. #if defined(CUDA_USE_TENSOR_CORES)
  4881. #define MMQ_X_Q3_K_AMPERE 4
  4882. #define MMQ_Y_Q3_K_AMPERE 32
  4883. #define NWARPS_Q3_K_AMPERE 4
  4884. #else
  4885. #define MMQ_X_Q3_K_AMPERE 128
  4886. #define MMQ_Y_Q3_K_AMPERE 128
  4887. #define NWARPS_Q3_K_AMPERE 4
  4888. #endif
  4889. #define MMQ_X_Q3_K_PASCAL 64
  4890. #define MMQ_Y_Q3_K_PASCAL 64
  4891. #define NWARPS_Q3_K_PASCAL 8
  4892. template <bool need_check> static __global__ void
  4893. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4894. #if defined(RDNA3) || defined(RDNA2)
  4895. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4896. #endif // defined(RDNA3) || defined(RDNA2)
  4897. #elif __CUDA_ARCH__ < CC_VOLTA
  4898. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4899. #endif // __CUDA_ARCH__ < CC_VOLTA
  4900. mul_mat_q3_K(
  4901. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4902. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4903. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4904. #if defined(RDNA3) || defined(RDNA2)
  4905. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4906. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4907. const int nwarps = NWARPS_Q3_K_RDNA2;
  4908. #else
  4909. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4910. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4911. const int nwarps = NWARPS_Q3_K_RDNA1;
  4912. #endif // defined(RDNA3) || defined(RDNA2)
  4913. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4914. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4915. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4916. #elif __CUDA_ARCH__ >= CC_VOLTA
  4917. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4918. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4919. const int nwarps = NWARPS_Q3_K_AMPERE;
  4920. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4921. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4922. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4923. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4924. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4925. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4926. const int nwarps = NWARPS_Q3_K_PASCAL;
  4927. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4928. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4929. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4930. #else
  4931. (void) vec_dot_q3_K_q8_1_mul_mat;
  4932. NO_DEVICE_CODE;
  4933. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4934. }
  4935. #define MMQ_X_Q4_K_RDNA2 64
  4936. #define MMQ_Y_Q4_K_RDNA2 128
  4937. #define NWARPS_Q4_K_RDNA2 8
  4938. #define MMQ_X_Q4_K_RDNA1 32
  4939. #define MMQ_Y_Q4_K_RDNA1 64
  4940. #define NWARPS_Q4_K_RDNA1 8
  4941. #if defined(CUDA_USE_TENSOR_CORES)
  4942. #define MMQ_X_Q4_K_AMPERE 4
  4943. #define MMQ_Y_Q4_K_AMPERE 32
  4944. #define NWARPS_Q4_K_AMPERE 4
  4945. #else
  4946. #define MMQ_X_Q4_K_AMPERE 64
  4947. #define MMQ_Y_Q4_K_AMPERE 128
  4948. #define NWARPS_Q4_K_AMPERE 4
  4949. #endif
  4950. #define MMQ_X_Q4_K_PASCAL 64
  4951. #define MMQ_Y_Q4_K_PASCAL 64
  4952. #define NWARPS_Q4_K_PASCAL 8
  4953. template <bool need_check> static __global__ void
  4954. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4955. #if defined(RDNA3) || defined(RDNA2)
  4956. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4957. #endif // defined(RDNA3) || defined(RDNA2)
  4958. #elif __CUDA_ARCH__ < CC_VOLTA
  4959. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4960. #endif // __CUDA_ARCH__ < CC_VOLTA
  4961. mul_mat_q4_K(
  4962. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4963. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4964. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4965. #if defined(RDNA3) || defined(RDNA2)
  4966. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4967. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4968. const int nwarps = NWARPS_Q4_K_RDNA2;
  4969. #else
  4970. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4971. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4972. const int nwarps = NWARPS_Q4_K_RDNA1;
  4973. #endif // defined(RDNA3) || defined(RDNA2)
  4974. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4975. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4976. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4977. #elif __CUDA_ARCH__ >= CC_VOLTA
  4978. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4979. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4980. const int nwarps = NWARPS_Q4_K_AMPERE;
  4981. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4982. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4983. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4984. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4985. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4986. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4987. const int nwarps = NWARPS_Q4_K_PASCAL;
  4988. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4989. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4990. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4991. #else
  4992. (void) vec_dot_q4_K_q8_1_mul_mat;
  4993. NO_DEVICE_CODE;
  4994. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4995. }
  4996. #define MMQ_X_Q5_K_RDNA2 64
  4997. #define MMQ_Y_Q5_K_RDNA2 128
  4998. #define NWARPS_Q5_K_RDNA2 8
  4999. #define MMQ_X_Q5_K_RDNA1 32
  5000. #define MMQ_Y_Q5_K_RDNA1 64
  5001. #define NWARPS_Q5_K_RDNA1 8
  5002. #if defined(CUDA_USE_TENSOR_CORES)
  5003. #define MMQ_X_Q5_K_AMPERE 4
  5004. #define MMQ_Y_Q5_K_AMPERE 32
  5005. #define NWARPS_Q5_K_AMPERE 4
  5006. #else
  5007. #define MMQ_X_Q5_K_AMPERE 64
  5008. #define MMQ_Y_Q5_K_AMPERE 128
  5009. #define NWARPS_Q5_K_AMPERE 4
  5010. #endif
  5011. #define MMQ_X_Q5_K_PASCAL 64
  5012. #define MMQ_Y_Q5_K_PASCAL 64
  5013. #define NWARPS_Q5_K_PASCAL 8
  5014. template <bool need_check> static __global__ void
  5015. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5016. #if defined(RDNA3) || defined(RDNA2)
  5017. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  5018. #endif // defined(RDNA3) || defined(RDNA2)
  5019. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5020. mul_mat_q5_K(
  5021. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5022. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  5023. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5024. #if defined(RDNA3) || defined(RDNA2)
  5025. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  5026. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  5027. const int nwarps = NWARPS_Q5_K_RDNA2;
  5028. #else
  5029. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  5030. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  5031. const int nwarps = NWARPS_Q5_K_RDNA1;
  5032. #endif // defined(RDNA3) || defined(RDNA2)
  5033. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  5034. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  5035. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5036. #elif __CUDA_ARCH__ >= CC_VOLTA
  5037. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  5038. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  5039. const int nwarps = NWARPS_Q5_K_AMPERE;
  5040. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  5041. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  5042. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5043. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  5044. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  5045. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  5046. const int nwarps = NWARPS_Q5_K_PASCAL;
  5047. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  5048. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  5049. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5050. #else
  5051. (void) vec_dot_q5_K_q8_1_mul_mat;
  5052. NO_DEVICE_CODE;
  5053. #endif // __CUDA_ARCH__ >= CC_VOLTA
  5054. }
  5055. #define MMQ_X_Q6_K_RDNA2 64
  5056. #define MMQ_Y_Q6_K_RDNA2 128
  5057. #define NWARPS_Q6_K_RDNA2 8
  5058. #define MMQ_X_Q6_K_RDNA1 32
  5059. #define MMQ_Y_Q6_K_RDNA1 64
  5060. #define NWARPS_Q6_K_RDNA1 8
  5061. #if defined(CUDA_USE_TENSOR_CORES)
  5062. #define MMQ_X_Q6_K_AMPERE 4
  5063. #define MMQ_Y_Q6_K_AMPERE 32
  5064. #define NWARPS_Q6_K_AMPERE 4
  5065. #else
  5066. #define MMQ_X_Q6_K_AMPERE 64
  5067. #define MMQ_Y_Q6_K_AMPERE 64
  5068. #define NWARPS_Q6_K_AMPERE 4
  5069. #endif
  5070. #define MMQ_X_Q6_K_PASCAL 64
  5071. #define MMQ_Y_Q6_K_PASCAL 64
  5072. #define NWARPS_Q6_K_PASCAL 8
  5073. template <bool need_check> static __global__ void
  5074. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5075. #if defined(RDNA3) || defined(RDNA2)
  5076. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  5077. #endif // defined(RDNA3) || defined(RDNA2)
  5078. #elif __CUDA_ARCH__ < CC_VOLTA
  5079. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  5080. #endif // __CUDA_ARCH__ < CC_VOLTA
  5081. mul_mat_q6_K(
  5082. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5083. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  5084. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5085. #if defined(RDNA3) || defined(RDNA2)
  5086. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  5087. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  5088. const int nwarps = NWARPS_Q6_K_RDNA2;
  5089. #else
  5090. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  5091. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  5092. const int nwarps = NWARPS_Q6_K_RDNA1;
  5093. #endif // defined(RDNA3) || defined(RDNA2)
  5094. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5095. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5096. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5097. #elif __CUDA_ARCH__ >= CC_VOLTA
  5098. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  5099. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  5100. const int nwarps = NWARPS_Q6_K_AMPERE;
  5101. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5102. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5103. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5104. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  5105. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  5106. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  5107. const int nwarps = NWARPS_Q6_K_PASCAL;
  5108. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5109. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5110. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5111. #else
  5112. (void) vec_dot_q6_K_q8_1_mul_mat;
  5113. NO_DEVICE_CODE;
  5114. #endif // __CUDA_ARCH__ >= CC_VOLTA
  5115. }
  5116. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  5117. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  5118. // tell the compiler to use as many registers as it wants, see nwarps definition below
  5119. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  5120. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  5121. static __global__ void mul_mat_vec_q(
  5122. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5123. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  5124. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  5125. constexpr int nwarps = 1;
  5126. constexpr int rows_per_cuda_block = 1;
  5127. #else
  5128. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  5129. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  5130. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  5131. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  5132. const int row0 = rows_per_cuda_block*blockIdx.x;
  5133. const int blocks_per_row_x = ncols_x / qk;
  5134. const int blocks_per_col_y = nrows_y / QK8_1;
  5135. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  5136. // partial sum for each thread
  5137. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  5138. const block_q_t * x = (const block_q_t *) vx;
  5139. const block_q8_1 * y = (const block_q8_1 *) vy;
  5140. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  5141. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  5142. // x block quant index when casting the quants to int
  5143. const int kqs = vdr * (tid % (qi/vdr));
  5144. #pragma unroll
  5145. for (int j = 0; j < ncols_y; ++j) {
  5146. #pragma unroll
  5147. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5148. tmp[j][i] += vec_dot_q_cuda(
  5149. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  5150. }
  5151. }
  5152. }
  5153. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  5154. if (threadIdx.y > 0) {
  5155. #pragma unroll
  5156. for (int j = 0; j < ncols_y; ++j) {
  5157. #pragma unroll
  5158. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5159. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  5160. }
  5161. }
  5162. }
  5163. __syncthreads();
  5164. if (threadIdx.y > 0) {
  5165. return;
  5166. }
  5167. // sum up partial sums and write back result
  5168. #pragma unroll
  5169. for (int j = 0; j < ncols_y; ++j) {
  5170. #pragma unroll
  5171. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5172. #pragma unroll
  5173. for (int l = 0; l < nwarps-1; ++l) {
  5174. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  5175. }
  5176. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  5177. }
  5178. if (threadIdx.x < rows_per_cuda_block) {
  5179. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  5180. }
  5181. }
  5182. }
  5183. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  5184. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  5185. // qk = quantized weights per x block
  5186. // qr = number of quantized weights per data value in x block
  5187. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  5188. if (row >= nrows) {
  5189. return;
  5190. }
  5191. const int tid = threadIdx.x;
  5192. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  5193. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  5194. const int y_offset = qr == 1 ? 1 : qk/2;
  5195. // partial sum for each thread
  5196. #ifdef GGML_CUDA_F16
  5197. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  5198. #else
  5199. float tmp = 0.0f;
  5200. #endif // GGML_CUDA_F16
  5201. for (int i = 0; i < ncols; i += iter_stride) {
  5202. const int col = i + vals_per_iter*tid;
  5203. const int ib = (row*ncols + col)/qk; // x block index
  5204. const int iqs = (col%qk)/qr; // x quant index
  5205. const int iybs = col - col%qk; // y block start index
  5206. // processing >2 values per i iter is faster for fast GPUs
  5207. #pragma unroll
  5208. for (int j = 0; j < vals_per_iter; j += 2) {
  5209. // process 2 vals per j iter
  5210. // dequantize
  5211. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  5212. dfloat2 v;
  5213. dequantize_kernel(vx, ib, iqs + j/qr, v);
  5214. // matrix multiplication
  5215. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  5216. #ifdef GGML_CUDA_F16
  5217. tmp += __hmul2(v, {
  5218. y[iybs + iqs + j/qr + 0],
  5219. y[iybs + iqs + j/qr + y_offset]
  5220. });
  5221. #else
  5222. tmp += v.x * y[iybs + iqs + j/qr + 0];
  5223. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  5224. #endif // GGML_CUDA_F16
  5225. }
  5226. }
  5227. // sum up partial sums and write back result
  5228. tmp = warp_reduce_sum(tmp);
  5229. if (tid == 0) {
  5230. #ifdef GGML_CUDA_F16
  5231. dst[row] = tmp.x + tmp.y;
  5232. #else
  5233. dst[row] = tmp;
  5234. #endif // GGML_CUDA_F16
  5235. }
  5236. }
  5237. static __global__ void mul_mat_p021_f16_f32(
  5238. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  5239. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  5240. const half * x = (const half *) vx;
  5241. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  5242. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  5243. const int channel_x = channel / (nchannels_y / nchannels_x);
  5244. const int nrows_y = ncols_x;
  5245. const int nrows_dst = nrows_x;
  5246. const int row_dst = row_x;
  5247. float tmp = 0.0f;
  5248. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  5249. const int col_x = col_x0 + threadIdx.x;
  5250. if (col_x >= ncols_x) {
  5251. break;
  5252. }
  5253. // x is transposed and permuted
  5254. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  5255. const float xi = __half2float(x[ix]);
  5256. const int row_y = col_x;
  5257. // y is not transposed but permuted
  5258. const int iy = channel*nrows_y + row_y;
  5259. tmp += xi * y[iy];
  5260. }
  5261. // dst is not transposed and not permuted
  5262. const int idst = channel*nrows_dst + row_dst;
  5263. // sum up partial sums and write back result
  5264. tmp = warp_reduce_sum(tmp);
  5265. if (threadIdx.x == 0) {
  5266. dst[idst] = tmp;
  5267. }
  5268. }
  5269. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  5270. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  5271. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  5272. const half * x = (const half *) vx;
  5273. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  5274. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  5275. const int channel_x = channel / channel_x_divisor;
  5276. const int nrows_y = ncols_x;
  5277. const int nrows_dst = nrows_x;
  5278. const int row_dst = row_x;
  5279. const int idst = channel*nrows_dst + row_dst;
  5280. float tmp = 0.0f;
  5281. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  5282. const int col_x = col_x0 + threadIdx.x;
  5283. if (col_x >= ncols_x) {
  5284. break;
  5285. }
  5286. const int row_y = col_x;
  5287. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  5288. const int iy = channel*nrows_y + row_y;
  5289. const float xi = __half2float(x[ix]);
  5290. tmp += xi * y[iy];
  5291. }
  5292. // sum up partial sums and write back result
  5293. tmp = warp_reduce_sum(tmp);
  5294. if (threadIdx.x == 0) {
  5295. dst[idst] = tmp;
  5296. }
  5297. }
  5298. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  5299. const float * xi = (const float *) cxi;
  5300. float * dsti = (float *) cdsti;
  5301. *dsti = *xi;
  5302. }
  5303. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  5304. const float * xi = (const float *) cxi;
  5305. half * dsti = (half *) cdsti;
  5306. *dsti = __float2half(*xi);
  5307. }
  5308. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  5309. const half * xi = (const half *) cxi;
  5310. half * dsti = (half *) cdsti;
  5311. *dsti = *xi;
  5312. }
  5313. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  5314. const half * xi = (const half *) cxi;
  5315. float * dsti = (float *) cdsti;
  5316. *dsti = *xi;
  5317. }
  5318. template <cpy_kernel_t cpy_1>
  5319. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  5320. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5321. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  5322. const int nb12, const int nb13) {
  5323. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5324. if (i >= ne) {
  5325. return;
  5326. }
  5327. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  5328. // then combine those indices with the corresponding byte offsets to get the total offsets
  5329. const int i03 = i/(ne00 * ne01 * ne02);
  5330. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  5331. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  5332. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  5333. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  5334. const int i13 = i/(ne10 * ne11 * ne12);
  5335. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  5336. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  5337. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  5338. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  5339. cpy_1(cx + x_offset, cdst + dst_offset);
  5340. }
  5341. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  5342. const float * xi = (const float *) cxi;
  5343. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  5344. float amax = 0.0f; // absolute max
  5345. for (int j = 0; j < QK8_0; j++) {
  5346. const float v = xi[j];
  5347. amax = fmaxf(amax, fabsf(v));
  5348. }
  5349. const float d = amax / ((1 << 7) - 1);
  5350. const float id = d ? 1.0f/d : 0.0f;
  5351. dsti->d = d;
  5352. for (int j = 0; j < QK8_0; ++j) {
  5353. const float x0 = xi[j]*id;
  5354. dsti->qs[j] = roundf(x0);
  5355. }
  5356. }
  5357. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  5358. const float * xi = (const float *) cxi;
  5359. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  5360. float amax = 0.0f;
  5361. float vmax = 0.0f;
  5362. for (int j = 0; j < QK4_0; ++j) {
  5363. const float v = xi[j];
  5364. if (amax < fabsf(v)) {
  5365. amax = fabsf(v);
  5366. vmax = v;
  5367. }
  5368. }
  5369. const float d = vmax / -8;
  5370. const float id = d ? 1.0f/d : 0.0f;
  5371. dsti->d = d;
  5372. for (int j = 0; j < QK4_0/2; ++j) {
  5373. const float x0 = xi[0 + j]*id;
  5374. const float x1 = xi[QK4_0/2 + j]*id;
  5375. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  5376. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  5377. dsti->qs[j] = xi0;
  5378. dsti->qs[j] |= xi1 << 4;
  5379. }
  5380. }
  5381. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  5382. const float * xi = (const float *) cxi;
  5383. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  5384. float vmin = FLT_MAX;
  5385. float vmax = -FLT_MAX;
  5386. for (int j = 0; j < QK4_1; ++j) {
  5387. const float v = xi[j];
  5388. if (v < vmin) vmin = v;
  5389. if (v > vmax) vmax = v;
  5390. }
  5391. const float d = (vmax - vmin) / ((1 << 4) - 1);
  5392. const float id = d ? 1.0f/d : 0.0f;
  5393. dsti->dm.x = d;
  5394. dsti->dm.y = vmin;
  5395. for (int j = 0; j < QK4_1/2; ++j) {
  5396. const float x0 = (xi[0 + j] - vmin)*id;
  5397. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  5398. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  5399. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  5400. dsti->qs[j] = xi0;
  5401. dsti->qs[j] |= xi1 << 4;
  5402. }
  5403. }
  5404. template <cpy_kernel_t cpy_blck, int qk>
  5405. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  5406. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5407. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  5408. const int nb12, const int nb13) {
  5409. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  5410. if (i >= ne) {
  5411. return;
  5412. }
  5413. const int i03 = i/(ne00 * ne01 * ne02);
  5414. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  5415. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  5416. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  5417. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  5418. const int i13 = i/(ne10 * ne11 * ne12);
  5419. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  5420. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  5421. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  5422. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  5423. cpy_blck(cx + x_offset, cdst + dst_offset);
  5424. }
  5425. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  5426. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  5427. return 1.0f - min(1.0f, max(0.0f, y));
  5428. }
  5429. struct rope_corr_dims {
  5430. float v[4];
  5431. };
  5432. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  5433. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  5434. static __device__ void rope_yarn(
  5435. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  5436. float * cos_theta, float * sin_theta
  5437. ) {
  5438. // Get n-d rotational scaling corrected for extrapolation
  5439. float theta_interp = freq_scale * theta_extrap;
  5440. float theta = theta_interp;
  5441. if (ext_factor != 0.0f) {
  5442. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  5443. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  5444. // Get n-d magnitude scaling corrected for interpolation
  5445. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  5446. }
  5447. *cos_theta = cosf(theta) * mscale;
  5448. *sin_theta = sinf(theta) * mscale;
  5449. }
  5450. // rope == RoPE == rotary positional embedding
  5451. template<typename T, bool has_pos>
  5452. static __global__ void rope(
  5453. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  5454. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  5455. ) {
  5456. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  5457. if (col >= ncols) {
  5458. return;
  5459. }
  5460. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5461. const int i = row*ncols + col;
  5462. const int i2 = row/p_delta_rows;
  5463. const int p = has_pos ? pos[i2] : 0;
  5464. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  5465. float cos_theta, sin_theta;
  5466. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  5467. const float x0 = x[i + 0];
  5468. const float x1 = x[i + 1];
  5469. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5470. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  5471. }
  5472. template<typename T, bool has_pos>
  5473. static __global__ void rope_neox(
  5474. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  5475. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  5476. ) {
  5477. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  5478. if (col >= ncols) {
  5479. return;
  5480. }
  5481. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5482. const int ib = col / n_dims;
  5483. const int ic = col % n_dims;
  5484. if (ib > 0) {
  5485. const int i = row*ncols + ib*n_dims + ic;
  5486. dst[i + 0] = x[i + 0];
  5487. dst[i + 1] = x[i + 1];
  5488. return;
  5489. }
  5490. const int i = row*ncols + ib*n_dims + ic/2;
  5491. const int i2 = row/p_delta_rows;
  5492. float cur_rot = inv_ndims * ic - ib;
  5493. const int p = has_pos ? pos[i2] : 0;
  5494. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  5495. float cos_theta, sin_theta;
  5496. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  5497. const float x0 = x[i + 0];
  5498. const float x1 = x[i + n_dims/2];
  5499. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5500. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  5501. }
  5502. static __global__ void rope_glm_f32(
  5503. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  5504. int n_ctx
  5505. ) {
  5506. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  5507. const int half_n_dims = ncols/4;
  5508. if (col >= half_n_dims) {
  5509. return;
  5510. }
  5511. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  5512. const int i = row*ncols + col;
  5513. const int i2 = row/p_delta_rows;
  5514. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  5515. // FIXME: this is likely wrong
  5516. const int p = pos != nullptr ? pos[i2] : 0;
  5517. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  5518. const float sin_theta = sinf(theta);
  5519. const float cos_theta = cosf(theta);
  5520. const float x0 = x[i + 0];
  5521. const float x1 = x[i + half_n_dims];
  5522. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5523. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  5524. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  5525. const float sin_block_theta = sinf(block_theta);
  5526. const float cos_block_theta = cosf(block_theta);
  5527. const float x2 = x[i + half_n_dims * 2];
  5528. const float x3 = x[i + half_n_dims * 3];
  5529. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  5530. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  5531. }
  5532. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  5533. const int n_heads_log2_floor, const float m0, const float m1) {
  5534. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  5535. if (col >= ncols) {
  5536. return;
  5537. }
  5538. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  5539. const int i = row*ncols + col;
  5540. const int k = row/k_rows;
  5541. float m_k;
  5542. if (k < n_heads_log2_floor) {
  5543. m_k = powf(m0, k + 1);
  5544. } else {
  5545. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  5546. }
  5547. dst[i] = col * m_k + x[i];
  5548. }
  5549. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  5550. const int row = blockIdx.x;
  5551. const int col = threadIdx.x;
  5552. float sum = 0.0f;
  5553. for (int i = col; i < ncols; i += blockDim.x) {
  5554. sum += x[row * ncols + i];
  5555. }
  5556. sum = warp_reduce_sum(sum);
  5557. if (col == 0) {
  5558. dst[row] = sum;
  5559. }
  5560. }
  5561. template<typename T>
  5562. static inline __device__ void swap(T & a, T & b) {
  5563. T tmp = a;
  5564. a = b;
  5565. b = tmp;
  5566. }
  5567. template<ggml_sort_order order>
  5568. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  5569. // bitonic sort
  5570. int col = threadIdx.x;
  5571. int row = blockIdx.y;
  5572. if (col >= ncols) return;
  5573. const float * x_row = x + row * ncols;
  5574. int * dst_row = dst + row * ncols;
  5575. // initialize indices
  5576. if (col < ncols) {
  5577. dst_row[col] = col;
  5578. }
  5579. __syncthreads();
  5580. for (int k = 2; k <= ncols; k *= 2) {
  5581. for (int j = k / 2; j > 0; j /= 2) {
  5582. int ixj = col ^ j;
  5583. if (ixj > col) {
  5584. if ((col & k) == 0) {
  5585. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  5586. swap(dst_row[col], dst_row[ixj]);
  5587. }
  5588. } else {
  5589. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  5590. swap(dst_row[col], dst_row[ixj]);
  5591. }
  5592. }
  5593. }
  5594. __syncthreads();
  5595. }
  5596. }
  5597. }
  5598. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  5599. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  5600. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5601. if (col >= ncols) {
  5602. return;
  5603. }
  5604. const int i = row*ncols + col;
  5605. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  5606. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  5607. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  5608. }
  5609. template <bool vals_smem, int ncols_template, int block_size_template>
  5610. 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) {
  5611. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  5612. const int tid = threadIdx.x;
  5613. const int rowx = blockIdx.x;
  5614. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  5615. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  5616. const int warp_id = threadIdx.x / WARP_SIZE;
  5617. const int lane_id = threadIdx.x % WARP_SIZE;
  5618. float slope = 0.0f;
  5619. // ALiBi
  5620. if (max_bias > 0.0f) {
  5621. const int h = rowx/nrows_y; // head index
  5622. const float base = h < n_head_log2 ? m0 : m1;
  5623. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  5624. slope = powf(base, exp);
  5625. }
  5626. extern __shared__ float data_soft_max_f32[];
  5627. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  5628. // shared memory buffer to cache values between iterations:
  5629. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  5630. float max_val = -INFINITY;
  5631. #pragma unroll
  5632. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5633. const int col = col0 + tid;
  5634. if (ncols_template == 0 && col >= ncols) {
  5635. break;
  5636. }
  5637. const int ix = rowx*ncols + col;
  5638. const int iy = rowy*ncols + col;
  5639. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  5640. vals[col] = val;
  5641. max_val = max(max_val, val);
  5642. }
  5643. // find the max value in the block
  5644. max_val = warp_reduce_max(max_val);
  5645. if (block_size > WARP_SIZE) {
  5646. if (warp_id == 0) {
  5647. buf_iw[lane_id] = -INFINITY;
  5648. }
  5649. __syncthreads();
  5650. if (lane_id == 0) {
  5651. buf_iw[warp_id] = max_val;
  5652. }
  5653. __syncthreads();
  5654. max_val = buf_iw[lane_id];
  5655. max_val = warp_reduce_max(max_val);
  5656. }
  5657. float tmp = 0.0f; // partial sum
  5658. #pragma unroll
  5659. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5660. const int col = col0 + tid;
  5661. if (ncols_template == 0 && col >= ncols) {
  5662. break;
  5663. }
  5664. const float val = expf(vals[col] - max_val);
  5665. tmp += val;
  5666. vals[col] = val;
  5667. }
  5668. // find the sum of exps in the block
  5669. tmp = warp_reduce_sum(tmp);
  5670. if (block_size > WARP_SIZE) {
  5671. if (warp_id == 0) {
  5672. buf_iw[lane_id] = 0.0f;
  5673. }
  5674. __syncthreads();
  5675. if (lane_id == 0) {
  5676. buf_iw[warp_id] = tmp;
  5677. }
  5678. __syncthreads();
  5679. tmp = buf_iw[lane_id];
  5680. tmp = warp_reduce_sum(tmp);
  5681. }
  5682. const float inv_sum = 1.0f / tmp;
  5683. #pragma unroll
  5684. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5685. const int col = col0 + tid;
  5686. if (ncols_template == 0 && col >= ncols) {
  5687. return;
  5688. }
  5689. const int idst = rowx*ncols + col;
  5690. dst[idst] = vals[col] * inv_sum;
  5691. }
  5692. }
  5693. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5694. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5695. if (i >= k) {
  5696. return;
  5697. }
  5698. dst[i] = scale * x[i];
  5699. }
  5700. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5701. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5702. if (i >= k) {
  5703. return;
  5704. }
  5705. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5706. }
  5707. template <typename T>
  5708. static __global__ void im2col_kernel(
  5709. const float * x, T * dst, int batch_offset,
  5710. int offset_delta, int IC, int IW, int IH, int OH, int OW, int KW, int KH, int pelements, int CHW,
  5711. int s0, int s1, int p0, int p1, int d0, int d1) {
  5712. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  5713. if (i >= pelements) {
  5714. return;
  5715. }
  5716. const int ksize = OW * (KH > 1 ? KW : 1);
  5717. const int kx = i / ksize;
  5718. const int kd = kx * ksize;
  5719. const int ky = (i - kd) / OW;
  5720. const int ix = i % OW;
  5721. const int oh = blockIdx.y;
  5722. const int batch = blockIdx.z / IC;
  5723. const int ic = blockIdx.z % IC;
  5724. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5725. const int64_t iih = oh * s1 + ky * d1 - p1;
  5726. const int64_t offset_dst =
  5727. ((batch * OH + oh) * OW + ix) * CHW +
  5728. (ic * (KW * KH) + ky * KW + kx);
  5729. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5730. dst[offset_dst] = 0.0f;
  5731. } else {
  5732. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5733. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5734. }
  5735. }
  5736. template <typename Ti, typename To>
  5737. static __global__ void pool2d_nchw_kernel(
  5738. const int ih, const int iw, const int oh, const int ow,
  5739. const int kh, const int kw, const int sh, const int sw,
  5740. const int ph, const int pw, const int parallel_elements,
  5741. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5742. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5743. if (idx >= parallel_elements) {
  5744. return;
  5745. }
  5746. const int I_HW = ih * iw;
  5747. const int O_HW = oh * ow;
  5748. const int nc = idx / O_HW;
  5749. const int cur_oh = idx % O_HW / ow;
  5750. const int cur_ow = idx % O_HW % ow;
  5751. const Ti* i_ptr = src + nc * I_HW;
  5752. To* o_ptr = dst + nc * O_HW;
  5753. const int start_h = cur_oh * sh - ph;
  5754. const int bh = max(0, start_h);
  5755. const int eh = min(ih, start_h + kh);
  5756. const int start_w = cur_ow * sw - pw;
  5757. const int bw = max(0, start_w);
  5758. const int ew = min(iw, start_w + kw);
  5759. const To scale = 1. / (kh * kw);
  5760. To res = 0;
  5761. switch (op) {
  5762. case GGML_OP_POOL_AVG: res = 0; break;
  5763. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5764. }
  5765. for (int i = bh; i < eh; i += 1) {
  5766. for (int j = bw; j < ew; j += 1) {
  5767. #if __CUDA_ARCH__ >= 350
  5768. Ti cur = __ldg(i_ptr + i * iw + j);
  5769. #else
  5770. Ti cur = i_ptr[i * iw + j];
  5771. #endif
  5772. switch (op) {
  5773. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5774. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5775. }
  5776. }
  5777. }
  5778. o_ptr[cur_oh * ow + cur_ow] = res;
  5779. }
  5780. template<int qk, int qr, dequantize_kernel_t dq>
  5781. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5782. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5783. GGML_TENSOR_BINARY_OP_LOCALS
  5784. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5785. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5786. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5787. // strides in elements
  5788. //const size_t s0 = nb0 / ggml_element_size(dst);
  5789. const size_t s1 = nb1 / ggml_element_size(dst);
  5790. const size_t s2 = nb2 / ggml_element_size(dst);
  5791. const size_t s3 = nb3 / ggml_element_size(dst);
  5792. const size_t s10 = nb10 / ggml_element_size(src1);
  5793. const size_t s11 = nb11 / ggml_element_size(src1);
  5794. const size_t s12 = nb12 / ggml_element_size(src1);
  5795. //const size_t s13 = nb13 / ggml_element_size(src1);
  5796. GGML_ASSERT(ne00 % 2 == 0);
  5797. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5798. src0_dd, src1_dd, dst_dd,
  5799. ne00, /*ne01, ne02, ne03,*/
  5800. /*ne10, ne11,*/ ne12, /*ne13,*/
  5801. /* s0,*/ s1, s2, s3,
  5802. /* nb00,*/ nb01, nb02, nb03,
  5803. s10, s11, s12/*, s13*/);
  5804. (void) dst;
  5805. }
  5806. template<typename src0_t>
  5807. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5808. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5809. GGML_TENSOR_BINARY_OP_LOCALS
  5810. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5811. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5812. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5813. // strides in elements
  5814. //const size_t s0 = nb0 / ggml_element_size(dst);
  5815. const size_t s1 = nb1 / ggml_element_size(dst);
  5816. const size_t s2 = nb2 / ggml_element_size(dst);
  5817. const size_t s3 = nb3 / ggml_element_size(dst);
  5818. const size_t s10 = nb10 / ggml_element_size(src1);
  5819. const size_t s11 = nb11 / ggml_element_size(src1);
  5820. const size_t s12 = nb12 / ggml_element_size(src1);
  5821. //const size_t s13 = nb13 / ggml_element_size(src1);
  5822. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5823. src0_dd, src1_dd, dst_dd,
  5824. ne00, /*ne01, ne02, ne03,*/
  5825. /*ne10, ne11,*/ ne12, /*ne13,*/
  5826. /* s0,*/ s1, s2, s3,
  5827. /* nb00,*/ nb01, nb02, nb03,
  5828. s10, s11, s12/*, s13*/);
  5829. (void) dst;
  5830. }
  5831. template<float (*bin_op)(const float, const float)>
  5832. struct bin_bcast_cuda {
  5833. template<typename src0_t, typename src1_t, typename dst_t>
  5834. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5835. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5836. cudaStream_t stream) {
  5837. GGML_TENSOR_BINARY_OP_LOCALS
  5838. int nr0 = ne10/ne0;
  5839. int nr1 = ne11/ne1;
  5840. int nr2 = ne12/ne2;
  5841. int nr3 = ne13/ne3;
  5842. int nr[4] = { nr0, nr1, nr2, nr3 };
  5843. // collapse dimensions until first broadcast dimension
  5844. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5845. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5846. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5847. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5848. auto collapse = [](int64_t cne[]) {
  5849. cne[0] *= cne[1];
  5850. cne[1] = cne[2];
  5851. cne[2] = cne[3];
  5852. cne[3] = 1;
  5853. };
  5854. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5855. cnb[1] *= cne[1];
  5856. cnb[2] *= cne[2];
  5857. cnb[3] *= cne[3];
  5858. };
  5859. for (int i = 0; i < 4; i++) {
  5860. if (nr[i] != 1) {
  5861. break;
  5862. }
  5863. if (i > 0) {
  5864. collapse_nb(cnb0, cne0);
  5865. collapse_nb(cnb1, cne1);
  5866. collapse(cne0);
  5867. collapse(cne1);
  5868. }
  5869. }
  5870. {
  5871. int64_t ne0 = cne0[0];
  5872. int64_t ne1 = cne0[1];
  5873. int64_t ne2 = cne0[2];
  5874. int64_t ne3 = cne0[3];
  5875. int64_t ne10 = cne1[0];
  5876. int64_t ne11 = cne1[1];
  5877. int64_t ne12 = cne1[2];
  5878. int64_t ne13 = cne1[3];
  5879. size_t nb0 = cnb0[0];
  5880. size_t nb1 = cnb0[1];
  5881. size_t nb2 = cnb0[2];
  5882. size_t nb3 = cnb0[3];
  5883. size_t nb10 = cnb1[0];
  5884. size_t nb11 = cnb1[1];
  5885. size_t nb12 = cnb1[2];
  5886. size_t nb13 = cnb1[3];
  5887. size_t s0 = nb0 / sizeof(dst_t);
  5888. size_t s1 = nb1 / sizeof(dst_t);
  5889. size_t s2 = nb2 / sizeof(dst_t);
  5890. size_t s3 = nb3 / sizeof(dst_t);
  5891. size_t s10 = nb10 / sizeof(src1_t);
  5892. size_t s11 = nb11 / sizeof(src1_t);
  5893. size_t s12 = nb12 / sizeof(src1_t);
  5894. size_t s13 = nb13 / sizeof(src1_t);
  5895. GGML_ASSERT(s0 == 1);
  5896. GGML_ASSERT(s10 == 1);
  5897. const int block_size = 128;
  5898. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5899. dim3 block_dims;
  5900. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5901. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5902. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5903. dim3 block_nums(
  5904. (hne0 + block_dims.x - 1) / block_dims.x,
  5905. (ne1 + block_dims.y - 1) / block_dims.y,
  5906. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5907. );
  5908. if (block_nums.z > 65535) {
  5909. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5910. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5911. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5912. src0_dd, src1_dd, dst_dd,
  5913. ne0, ne1, ne2, ne3,
  5914. ne10, ne11, ne12, ne13,
  5915. /* s0, */ s1, s2, s3,
  5916. /* s10, */ s11, s12, s13);
  5917. } else {
  5918. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5919. src0_dd, src1_dd, dst_dd,
  5920. ne0, ne1, ne2, ne3,
  5921. ne10, ne11, ne12, ne13,
  5922. /* s0, */ s1, s2, s3,
  5923. /* s10, */ s11, s12, s13);
  5924. }
  5925. }
  5926. }
  5927. };
  5928. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5929. const int ne10, const int ne11, const int ne12,
  5930. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5931. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5932. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5933. }
  5934. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5935. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5936. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5937. }
  5938. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5939. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5940. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5941. }
  5942. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5943. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5944. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5945. }
  5946. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5947. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5948. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5949. }
  5950. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5951. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5952. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5953. }
  5954. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5955. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5956. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5957. }
  5958. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5959. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5960. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5961. }
  5962. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5963. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5964. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5965. }
  5966. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5967. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5968. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5969. }
  5970. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5971. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5972. if (ncols < 1024) {
  5973. const dim3 block_dims(WARP_SIZE, 1, 1);
  5974. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5975. } else {
  5976. const dim3 block_dims(1024, 1, 1);
  5977. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5978. }
  5979. }
  5980. 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) {
  5981. static const float eps = 1e-6f;
  5982. if (group_size < 1024) {
  5983. const dim3 block_dims(WARP_SIZE, 1, 1);
  5984. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5985. } else {
  5986. const dim3 block_dims(1024, 1, 1);
  5987. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5988. }
  5989. }
  5990. 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) {
  5991. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5992. dim3 gridDim(num_blocks, ne1, ne2);
  5993. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5994. }
  5995. 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) {
  5996. int ne0 = (ne00 * scale_factor);
  5997. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5998. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  5999. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  6000. }
  6001. static void pad_f32_cuda(const float * x, float * dst,
  6002. const int ne00, const int ne01, const int ne02,
  6003. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  6004. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  6005. dim3 gridDim(num_blocks, ne1, ne2);
  6006. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  6007. }
  6008. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  6009. GGML_ASSERT(ncols % WARP_SIZE == 0);
  6010. if (ncols < 1024) {
  6011. const dim3 block_dims(WARP_SIZE, 1, 1);
  6012. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  6013. } else {
  6014. const dim3 block_dims(1024, 1, 1);
  6015. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  6016. }
  6017. }
  6018. 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) {
  6019. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  6020. const dim3 num_blocks(block_num_x, ky, 1);
  6021. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  6022. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  6023. }
  6024. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  6025. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  6026. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  6027. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  6028. }
  6029. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  6030. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  6031. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  6032. const bool need_check = false;
  6033. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  6034. } else {
  6035. const bool need_check = true;
  6036. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  6037. }
  6038. }
  6039. template<typename dst_t>
  6040. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6041. const int nb = k / QK_K;
  6042. #if QK_K == 256
  6043. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  6044. #else
  6045. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  6046. #endif
  6047. }
  6048. template<typename dst_t>
  6049. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6050. const int nb = k / QK_K;
  6051. #if QK_K == 256
  6052. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  6053. #else
  6054. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  6055. #endif
  6056. }
  6057. template<typename dst_t>
  6058. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6059. const int nb32 = k / 32;
  6060. const int nb = (k + 255) / 256;
  6061. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  6062. }
  6063. template<typename dst_t>
  6064. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6065. const int nb32 = k / 32;
  6066. const int nb = (k + 255) / 256;
  6067. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  6068. }
  6069. template<typename dst_t>
  6070. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6071. const int nb = k / QK_K;
  6072. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  6073. }
  6074. template<typename dst_t>
  6075. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6076. const int nb = k / QK_K;
  6077. #if QK_K == 256
  6078. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  6079. #else
  6080. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  6081. #endif
  6082. }
  6083. template<typename dst_t>
  6084. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6085. const int nb = k / QK_K;
  6086. #if QK_K == 256
  6087. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  6088. #else
  6089. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  6090. #endif
  6091. }
  6092. template<typename dst_t>
  6093. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6094. const int nb = k / QK_K;
  6095. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  6096. }
  6097. template<typename dst_t>
  6098. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6099. const int nb = k / QK_K;
  6100. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  6101. }
  6102. template<typename dst_t>
  6103. static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6104. const int nb = k / QK_K;
  6105. dequantize_block_iq2_s<<<nb, 32, 0, stream>>>(vx, y);
  6106. }
  6107. template<typename dst_t>
  6108. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6109. const int nb = k / QK_K;
  6110. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  6111. }
  6112. template<typename dst_t>
  6113. static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6114. const int nb = k / QK_K;
  6115. dequantize_block_iq3_s<<<nb, 32, 0, stream>>>(vx, y);
  6116. }
  6117. template<typename dst_t>
  6118. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6119. const int nb = k / QK_K;
  6120. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  6121. }
  6122. template<typename dst_t>
  6123. static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6124. const int nb = (k + QK_K - 1) / QK_K;
  6125. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  6126. }
  6127. template<typename dst_t>
  6128. static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6129. const int nb = (k + QK_K - 1) / QK_K;
  6130. #if QK_K == 64
  6131. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  6132. #else
  6133. dequantize_block_iq4_xs<<<nb, 32, 0, stream>>>(vx, y);
  6134. #endif
  6135. }
  6136. template <typename src_t, typename dst_t>
  6137. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  6138. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  6139. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  6140. }
  6141. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  6142. int id;
  6143. switch (type) {
  6144. case GGML_TYPE_Q4_0:
  6145. return dequantize_row_q4_0_cuda;
  6146. case GGML_TYPE_Q4_1:
  6147. return dequantize_row_q4_1_cuda;
  6148. case GGML_TYPE_Q5_0:
  6149. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  6150. case GGML_TYPE_Q5_1:
  6151. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  6152. case GGML_TYPE_Q8_0:
  6153. CUDA_CHECK(cudaGetDevice(&id));
  6154. if (g_device_caps[id].cc >= CC_PASCAL) {
  6155. return dequantize_block_q8_0_f16_cuda;
  6156. }
  6157. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  6158. case GGML_TYPE_Q2_K:
  6159. return dequantize_row_q2_K_cuda;
  6160. case GGML_TYPE_Q3_K:
  6161. return dequantize_row_q3_K_cuda;
  6162. case GGML_TYPE_Q4_K:
  6163. return dequantize_row_q4_K_cuda;
  6164. case GGML_TYPE_Q5_K:
  6165. return dequantize_row_q5_K_cuda;
  6166. case GGML_TYPE_Q6_K:
  6167. return dequantize_row_q6_K_cuda;
  6168. case GGML_TYPE_IQ2_XXS:
  6169. return dequantize_row_iq2_xxs_cuda;
  6170. case GGML_TYPE_IQ2_XS:
  6171. return dequantize_row_iq2_xs_cuda;
  6172. case GGML_TYPE_IQ2_S:
  6173. return dequantize_row_iq2_s_cuda;
  6174. case GGML_TYPE_IQ3_XXS:
  6175. return dequantize_row_iq3_xxs_cuda;
  6176. case GGML_TYPE_IQ1_S:
  6177. return dequantize_row_iq1_s_cuda;
  6178. case GGML_TYPE_IQ4_NL:
  6179. return dequantize_row_iq4_nl_cuda;
  6180. case GGML_TYPE_IQ4_XS:
  6181. return dequantize_row_iq4_xs_cuda;
  6182. case GGML_TYPE_IQ3_S:
  6183. return dequantize_row_iq3_s_cuda;
  6184. case GGML_TYPE_F32:
  6185. return convert_unary_cuda<float>;
  6186. default:
  6187. return nullptr;
  6188. }
  6189. }
  6190. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  6191. switch (type) {
  6192. case GGML_TYPE_Q4_0:
  6193. return dequantize_row_q4_0_cuda;
  6194. case GGML_TYPE_Q4_1:
  6195. return dequantize_row_q4_1_cuda;
  6196. case GGML_TYPE_Q5_0:
  6197. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  6198. case GGML_TYPE_Q5_1:
  6199. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  6200. case GGML_TYPE_Q8_0:
  6201. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  6202. case GGML_TYPE_Q2_K:
  6203. return dequantize_row_q2_K_cuda;
  6204. case GGML_TYPE_Q3_K:
  6205. return dequantize_row_q3_K_cuda;
  6206. case GGML_TYPE_Q4_K:
  6207. return dequantize_row_q4_K_cuda;
  6208. case GGML_TYPE_Q5_K:
  6209. return dequantize_row_q5_K_cuda;
  6210. case GGML_TYPE_Q6_K:
  6211. return dequantize_row_q6_K_cuda;
  6212. case GGML_TYPE_IQ2_XXS:
  6213. return dequantize_row_iq2_xxs_cuda;
  6214. case GGML_TYPE_IQ2_XS:
  6215. return dequantize_row_iq2_xs_cuda;
  6216. case GGML_TYPE_IQ2_S:
  6217. return dequantize_row_iq2_s_cuda;
  6218. case GGML_TYPE_IQ3_XXS:
  6219. return dequantize_row_iq3_xxs_cuda;
  6220. case GGML_TYPE_IQ1_S:
  6221. return dequantize_row_iq1_s_cuda;
  6222. case GGML_TYPE_IQ4_NL:
  6223. return dequantize_row_iq4_nl_cuda;
  6224. case GGML_TYPE_IQ4_XS:
  6225. return dequantize_row_iq4_xs_cuda;
  6226. case GGML_TYPE_IQ3_S:
  6227. return dequantize_row_iq3_s_cuda;
  6228. case GGML_TYPE_F16:
  6229. return convert_unary_cuda<half>;
  6230. default:
  6231. return nullptr;
  6232. }
  6233. }
  6234. 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) {
  6235. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6236. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6237. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  6238. const dim3 block_nums(block_num_y, 1, 1);
  6239. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6240. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  6241. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6242. }
  6243. 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) {
  6244. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6245. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6246. const dim3 block_nums(block_num_y, 1, 1);
  6247. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6248. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  6249. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6250. }
  6251. 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) {
  6252. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6253. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6254. const dim3 block_nums(block_num_y, 1, 1);
  6255. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6256. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  6257. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6258. }
  6259. 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) {
  6260. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6261. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6262. const dim3 block_nums(block_num_y, 1, 1);
  6263. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6264. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  6265. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6266. }
  6267. 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) {
  6268. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6269. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6270. const dim3 block_nums(block_num_y, 1, 1);
  6271. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6272. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  6273. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6274. }
  6275. 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) {
  6276. GGML_ASSERT(ncols % QK_K == 0);
  6277. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  6278. const int block_num_y = (nrows + ny - 1) / ny;
  6279. const dim3 block_nums(block_num_y, 1, 1);
  6280. const dim3 block_dims(32, ny, 1);
  6281. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6282. }
  6283. 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) {
  6284. GGML_ASSERT(ncols % QK_K == 0);
  6285. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6286. const int block_num_y = (nrows + ny - 1) / ny;
  6287. const dim3 block_nums(block_num_y, 1, 1);
  6288. const dim3 block_dims(32, ny, 1);
  6289. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6290. }
  6291. 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) {
  6292. GGML_ASSERT(ncols % QK_K == 0);
  6293. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6294. const int block_num_y = (nrows + ny - 1) / ny;
  6295. const dim3 block_nums(block_num_y, 1, 1);
  6296. const dim3 block_dims(32, ny, 1);
  6297. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6298. }
  6299. 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) {
  6300. GGML_ASSERT(ncols % QK_K == 0);
  6301. const dim3 block_dims(32, 1, 1);
  6302. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  6303. }
  6304. 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) {
  6305. GGML_ASSERT(ncols % QK_K == 0);
  6306. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6307. const int block_num_y = (nrows + ny - 1) / ny;
  6308. const dim3 block_nums(block_num_y, 1, 1);
  6309. const dim3 block_dims(32, ny, 1);
  6310. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6311. }
  6312. 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) {
  6313. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6314. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6315. const dim3 block_nums(block_num_y, 1, 1);
  6316. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6317. dequantize_mul_mat_vec<1, 1, convert_f16>
  6318. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6319. }
  6320. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  6321. static void mul_mat_vec_q_cuda(
  6322. const void * vx, const void * vy, float * dst,
  6323. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  6324. GGML_ASSERT(ncols_x % qk == 0);
  6325. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  6326. int id;
  6327. CUDA_CHECK(cudaGetDevice(&id));
  6328. int64_t nwarps = 1;
  6329. int64_t rows_per_cuda_block = 1;
  6330. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  6331. switch(ncols_y) {
  6332. case 1:
  6333. nwarps = 4;
  6334. rows_per_cuda_block = 1;
  6335. break;
  6336. case 2:
  6337. case 3:
  6338. case 4:
  6339. nwarps = 4;
  6340. rows_per_cuda_block = 2;
  6341. break;
  6342. case 5:
  6343. case 6:
  6344. case 7:
  6345. case 8:
  6346. nwarps = 2;
  6347. rows_per_cuda_block = 2;
  6348. break;
  6349. default:
  6350. GGML_ASSERT(false);
  6351. break;
  6352. }
  6353. }
  6354. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  6355. const dim3 block_nums(nblocks, 1, 1);
  6356. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6357. switch (ncols_y) {
  6358. case 1:
  6359. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  6360. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6361. break;
  6362. case 2:
  6363. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  6364. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6365. break;
  6366. case 3:
  6367. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  6368. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6369. break;
  6370. case 4:
  6371. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  6372. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6373. break;
  6374. case 5:
  6375. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  6376. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6377. break;
  6378. case 6:
  6379. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  6380. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6381. break;
  6382. case 7:
  6383. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  6384. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6385. break;
  6386. case 8:
  6387. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  6388. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6389. break;
  6390. default:
  6391. GGML_ASSERT(false);
  6392. break;
  6393. }
  6394. }
  6395. static void ggml_mul_mat_q4_0_q8_1_cuda(
  6396. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6397. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6398. int id;
  6399. CUDA_CHECK(cudaGetDevice(&id));
  6400. const int compute_capability = g_device_caps[id].cc;
  6401. int mmq_x, mmq_y, nwarps;
  6402. if (compute_capability >= CC_RDNA2) {
  6403. mmq_x = MMQ_X_Q4_0_RDNA2;
  6404. mmq_y = MMQ_Y_Q4_0_RDNA2;
  6405. nwarps = NWARPS_Q4_0_RDNA2;
  6406. } else if (compute_capability >= CC_OFFSET_AMD) {
  6407. mmq_x = MMQ_X_Q4_0_RDNA1;
  6408. mmq_y = MMQ_Y_Q4_0_RDNA1;
  6409. nwarps = NWARPS_Q4_0_RDNA1;
  6410. } else if (compute_capability >= CC_VOLTA) {
  6411. mmq_x = MMQ_X_Q4_0_AMPERE;
  6412. mmq_y = MMQ_Y_Q4_0_AMPERE;
  6413. nwarps = NWARPS_Q4_0_AMPERE;
  6414. } else if (compute_capability >= MIN_CC_DP4A) {
  6415. mmq_x = MMQ_X_Q4_0_PASCAL;
  6416. mmq_y = MMQ_Y_Q4_0_PASCAL;
  6417. nwarps = NWARPS_Q4_0_PASCAL;
  6418. } else {
  6419. GGML_ASSERT(false);
  6420. }
  6421. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6422. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6423. const dim3 block_nums(block_num_x, block_num_y, 1);
  6424. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6425. if (nrows_x % mmq_y == 0) {
  6426. const bool need_check = false;
  6427. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6428. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6429. } else {
  6430. const bool need_check = true;
  6431. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6432. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6433. }
  6434. }
  6435. static void ggml_mul_mat_q4_1_q8_1_cuda(
  6436. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6437. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6438. int id;
  6439. CUDA_CHECK(cudaGetDevice(&id));
  6440. const int compute_capability = g_device_caps[id].cc;
  6441. int mmq_x, mmq_y, nwarps;
  6442. if (compute_capability >= CC_RDNA2) {
  6443. mmq_x = MMQ_X_Q4_1_RDNA2;
  6444. mmq_y = MMQ_Y_Q4_1_RDNA2;
  6445. nwarps = NWARPS_Q4_1_RDNA2;
  6446. } else if (compute_capability >= CC_OFFSET_AMD) {
  6447. mmq_x = MMQ_X_Q4_1_RDNA1;
  6448. mmq_y = MMQ_Y_Q4_1_RDNA1;
  6449. nwarps = NWARPS_Q4_1_RDNA1;
  6450. } else if (compute_capability >= CC_VOLTA) {
  6451. mmq_x = MMQ_X_Q4_1_AMPERE;
  6452. mmq_y = MMQ_Y_Q4_1_AMPERE;
  6453. nwarps = NWARPS_Q4_1_AMPERE;
  6454. } else if (compute_capability >= MIN_CC_DP4A) {
  6455. mmq_x = MMQ_X_Q4_1_PASCAL;
  6456. mmq_y = MMQ_Y_Q4_1_PASCAL;
  6457. nwarps = NWARPS_Q4_1_PASCAL;
  6458. } else {
  6459. GGML_ASSERT(false);
  6460. }
  6461. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6462. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6463. const dim3 block_nums(block_num_x, block_num_y, 1);
  6464. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6465. if (nrows_x % mmq_y == 0) {
  6466. const bool need_check = false;
  6467. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6468. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6469. } else {
  6470. const bool need_check = true;
  6471. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6472. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6473. }
  6474. }
  6475. static void ggml_mul_mat_q5_0_q8_1_cuda(
  6476. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6477. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6478. int id;
  6479. CUDA_CHECK(cudaGetDevice(&id));
  6480. const int compute_capability = g_device_caps[id].cc;
  6481. int mmq_x, mmq_y, nwarps;
  6482. if (compute_capability >= CC_RDNA2) {
  6483. mmq_x = MMQ_X_Q5_0_RDNA2;
  6484. mmq_y = MMQ_Y_Q5_0_RDNA2;
  6485. nwarps = NWARPS_Q5_0_RDNA2;
  6486. } else if (compute_capability >= CC_OFFSET_AMD) {
  6487. mmq_x = MMQ_X_Q5_0_RDNA1;
  6488. mmq_y = MMQ_Y_Q5_0_RDNA1;
  6489. nwarps = NWARPS_Q5_0_RDNA1;
  6490. } else if (compute_capability >= CC_VOLTA) {
  6491. mmq_x = MMQ_X_Q5_0_AMPERE;
  6492. mmq_y = MMQ_Y_Q5_0_AMPERE;
  6493. nwarps = NWARPS_Q5_0_AMPERE;
  6494. } else if (compute_capability >= MIN_CC_DP4A) {
  6495. mmq_x = MMQ_X_Q5_0_PASCAL;
  6496. mmq_y = MMQ_Y_Q5_0_PASCAL;
  6497. nwarps = NWARPS_Q5_0_PASCAL;
  6498. } else {
  6499. GGML_ASSERT(false);
  6500. }
  6501. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6502. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6503. const dim3 block_nums(block_num_x, block_num_y, 1);
  6504. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6505. if (nrows_x % mmq_y == 0) {
  6506. const bool need_check = false;
  6507. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6508. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6509. } else {
  6510. const bool need_check = true;
  6511. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6512. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6513. }
  6514. }
  6515. static void ggml_mul_mat_q5_1_q8_1_cuda(
  6516. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6517. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6518. int id;
  6519. CUDA_CHECK(cudaGetDevice(&id));
  6520. const int compute_capability = g_device_caps[id].cc;
  6521. int mmq_x, mmq_y, nwarps;
  6522. if (compute_capability >= CC_RDNA2) {
  6523. mmq_x = MMQ_X_Q5_1_RDNA2;
  6524. mmq_y = MMQ_Y_Q5_1_RDNA2;
  6525. nwarps = NWARPS_Q5_1_RDNA2;
  6526. } else if (compute_capability >= CC_OFFSET_AMD) {
  6527. mmq_x = MMQ_X_Q5_1_RDNA1;
  6528. mmq_y = MMQ_Y_Q5_1_RDNA1;
  6529. nwarps = NWARPS_Q5_1_RDNA1;
  6530. } else if (compute_capability >= CC_VOLTA) {
  6531. mmq_x = MMQ_X_Q5_1_AMPERE;
  6532. mmq_y = MMQ_Y_Q5_1_AMPERE;
  6533. nwarps = NWARPS_Q5_1_AMPERE;
  6534. } else if (compute_capability >= MIN_CC_DP4A) {
  6535. mmq_x = MMQ_X_Q5_1_PASCAL;
  6536. mmq_y = MMQ_Y_Q5_1_PASCAL;
  6537. nwarps = NWARPS_Q5_1_PASCAL;
  6538. } else {
  6539. GGML_ASSERT(false);
  6540. }
  6541. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6542. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6543. const dim3 block_nums(block_num_x, block_num_y, 1);
  6544. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6545. if (nrows_x % mmq_y == 0) {
  6546. const bool need_check = false;
  6547. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6548. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6549. } else {
  6550. const bool need_check = true;
  6551. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6552. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6553. }
  6554. }
  6555. static void ggml_mul_mat_q8_0_q8_1_cuda(
  6556. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6557. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6558. int id;
  6559. CUDA_CHECK(cudaGetDevice(&id));
  6560. const int compute_capability = g_device_caps[id].cc;
  6561. int mmq_x, mmq_y, nwarps;
  6562. if (compute_capability >= CC_RDNA2) {
  6563. mmq_x = MMQ_X_Q8_0_RDNA2;
  6564. mmq_y = MMQ_Y_Q8_0_RDNA2;
  6565. nwarps = NWARPS_Q8_0_RDNA2;
  6566. } else if (compute_capability >= CC_OFFSET_AMD) {
  6567. mmq_x = MMQ_X_Q8_0_RDNA1;
  6568. mmq_y = MMQ_Y_Q8_0_RDNA1;
  6569. nwarps = NWARPS_Q8_0_RDNA1;
  6570. } else if (compute_capability >= CC_VOLTA) {
  6571. mmq_x = MMQ_X_Q8_0_AMPERE;
  6572. mmq_y = MMQ_Y_Q8_0_AMPERE;
  6573. nwarps = NWARPS_Q8_0_AMPERE;
  6574. } else if (compute_capability >= MIN_CC_DP4A) {
  6575. mmq_x = MMQ_X_Q8_0_PASCAL;
  6576. mmq_y = MMQ_Y_Q8_0_PASCAL;
  6577. nwarps = NWARPS_Q8_0_PASCAL;
  6578. } else {
  6579. GGML_ASSERT(false);
  6580. }
  6581. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6582. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6583. const dim3 block_nums(block_num_x, block_num_y, 1);
  6584. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6585. if (nrows_x % mmq_y == 0) {
  6586. const bool need_check = false;
  6587. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6588. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6589. } else {
  6590. const bool need_check = true;
  6591. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6592. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6593. }
  6594. }
  6595. static void ggml_mul_mat_q2_K_q8_1_cuda(
  6596. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6597. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6598. int id;
  6599. CUDA_CHECK(cudaGetDevice(&id));
  6600. const int compute_capability = g_device_caps[id].cc;
  6601. int mmq_x, mmq_y, nwarps;
  6602. if (compute_capability >= CC_RDNA2) {
  6603. mmq_x = MMQ_X_Q2_K_RDNA2;
  6604. mmq_y = MMQ_Y_Q2_K_RDNA2;
  6605. nwarps = NWARPS_Q2_K_RDNA2;
  6606. } else if (compute_capability >= CC_OFFSET_AMD) {
  6607. mmq_x = MMQ_X_Q2_K_RDNA1;
  6608. mmq_y = MMQ_Y_Q2_K_RDNA1;
  6609. nwarps = NWARPS_Q2_K_RDNA1;
  6610. } else if (compute_capability >= CC_VOLTA) {
  6611. mmq_x = MMQ_X_Q2_K_AMPERE;
  6612. mmq_y = MMQ_Y_Q2_K_AMPERE;
  6613. nwarps = NWARPS_Q2_K_AMPERE;
  6614. } else if (compute_capability >= MIN_CC_DP4A) {
  6615. mmq_x = MMQ_X_Q2_K_PASCAL;
  6616. mmq_y = MMQ_Y_Q2_K_PASCAL;
  6617. nwarps = NWARPS_Q2_K_PASCAL;
  6618. } else {
  6619. GGML_ASSERT(false);
  6620. }
  6621. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6622. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6623. const dim3 block_nums(block_num_x, block_num_y, 1);
  6624. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6625. if (nrows_x % mmq_y == 0) {
  6626. const bool need_check = false;
  6627. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6628. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6629. } else {
  6630. const bool need_check = true;
  6631. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6632. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6633. }
  6634. }
  6635. static void ggml_mul_mat_q3_K_q8_1_cuda(
  6636. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6637. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6638. #if QK_K == 256
  6639. int id;
  6640. CUDA_CHECK(cudaGetDevice(&id));
  6641. const int compute_capability = g_device_caps[id].cc;
  6642. int mmq_x, mmq_y, nwarps;
  6643. if (compute_capability >= CC_RDNA2) {
  6644. mmq_x = MMQ_X_Q3_K_RDNA2;
  6645. mmq_y = MMQ_Y_Q3_K_RDNA2;
  6646. nwarps = NWARPS_Q3_K_RDNA2;
  6647. } else if (compute_capability >= CC_OFFSET_AMD) {
  6648. mmq_x = MMQ_X_Q3_K_RDNA1;
  6649. mmq_y = MMQ_Y_Q3_K_RDNA1;
  6650. nwarps = NWARPS_Q3_K_RDNA1;
  6651. } else if (compute_capability >= CC_VOLTA) {
  6652. mmq_x = MMQ_X_Q3_K_AMPERE;
  6653. mmq_y = MMQ_Y_Q3_K_AMPERE;
  6654. nwarps = NWARPS_Q3_K_AMPERE;
  6655. } else if (compute_capability >= MIN_CC_DP4A) {
  6656. mmq_x = MMQ_X_Q3_K_PASCAL;
  6657. mmq_y = MMQ_Y_Q3_K_PASCAL;
  6658. nwarps = NWARPS_Q3_K_PASCAL;
  6659. } else {
  6660. GGML_ASSERT(false);
  6661. }
  6662. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6663. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6664. const dim3 block_nums(block_num_x, block_num_y, 1);
  6665. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6666. if (nrows_x % mmq_y == 0) {
  6667. const bool need_check = false;
  6668. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6669. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6670. } else {
  6671. const bool need_check = true;
  6672. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6673. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6674. }
  6675. #endif
  6676. }
  6677. static void ggml_mul_mat_q4_K_q8_1_cuda(
  6678. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6679. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6680. int id;
  6681. CUDA_CHECK(cudaGetDevice(&id));
  6682. const int compute_capability = g_device_caps[id].cc;
  6683. int mmq_x, mmq_y, nwarps;
  6684. if (compute_capability >= CC_RDNA2) {
  6685. mmq_x = MMQ_X_Q4_K_RDNA2;
  6686. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6687. nwarps = NWARPS_Q4_K_RDNA2;
  6688. } else if (compute_capability >= CC_OFFSET_AMD) {
  6689. mmq_x = MMQ_X_Q4_K_RDNA1;
  6690. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6691. nwarps = NWARPS_Q4_K_RDNA1;
  6692. } else if (compute_capability >= CC_VOLTA) {
  6693. mmq_x = MMQ_X_Q4_K_AMPERE;
  6694. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6695. nwarps = NWARPS_Q4_K_AMPERE;
  6696. } else if (compute_capability >= MIN_CC_DP4A) {
  6697. mmq_x = MMQ_X_Q4_K_PASCAL;
  6698. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6699. nwarps = NWARPS_Q4_K_PASCAL;
  6700. } else {
  6701. GGML_ASSERT(false);
  6702. }
  6703. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6704. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6705. const dim3 block_nums(block_num_x, block_num_y, 1);
  6706. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6707. if (nrows_x % mmq_y == 0) {
  6708. const bool need_check = false;
  6709. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6710. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6711. } else {
  6712. const bool need_check = true;
  6713. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6714. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6715. }
  6716. }
  6717. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6718. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6719. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6720. int id;
  6721. CUDA_CHECK(cudaGetDevice(&id));
  6722. const int compute_capability = g_device_caps[id].cc;
  6723. int mmq_x, mmq_y, nwarps;
  6724. if (compute_capability >= CC_RDNA2) {
  6725. mmq_x = MMQ_X_Q5_K_RDNA2;
  6726. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6727. nwarps = NWARPS_Q5_K_RDNA2;
  6728. } else if (compute_capability >= CC_OFFSET_AMD) {
  6729. mmq_x = MMQ_X_Q5_K_RDNA1;
  6730. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6731. nwarps = NWARPS_Q5_K_RDNA1;
  6732. } else if (compute_capability >= CC_VOLTA) {
  6733. mmq_x = MMQ_X_Q5_K_AMPERE;
  6734. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6735. nwarps = NWARPS_Q5_K_AMPERE;
  6736. } else if (compute_capability >= MIN_CC_DP4A) {
  6737. mmq_x = MMQ_X_Q5_K_PASCAL;
  6738. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6739. nwarps = NWARPS_Q5_K_PASCAL;
  6740. } else {
  6741. GGML_ASSERT(false);
  6742. }
  6743. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6744. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6745. const dim3 block_nums(block_num_x, block_num_y, 1);
  6746. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6747. if (nrows_x % mmq_y == 0) {
  6748. const bool need_check = false;
  6749. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6750. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6751. } else {
  6752. const bool need_check = true;
  6753. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6754. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6755. }
  6756. }
  6757. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6758. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6759. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6760. int id;
  6761. CUDA_CHECK(cudaGetDevice(&id));
  6762. const int compute_capability = g_device_caps[id].cc;
  6763. int mmq_x, mmq_y, nwarps;
  6764. if (compute_capability >= CC_RDNA2) {
  6765. mmq_x = MMQ_X_Q6_K_RDNA2;
  6766. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6767. nwarps = NWARPS_Q6_K_RDNA2;
  6768. } else if (compute_capability >= CC_OFFSET_AMD) {
  6769. mmq_x = MMQ_X_Q6_K_RDNA1;
  6770. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6771. nwarps = NWARPS_Q6_K_RDNA1;
  6772. } else if (compute_capability >= CC_VOLTA) {
  6773. mmq_x = MMQ_X_Q6_K_AMPERE;
  6774. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6775. nwarps = NWARPS_Q6_K_AMPERE;
  6776. } else if (compute_capability >= MIN_CC_DP4A) {
  6777. mmq_x = MMQ_X_Q6_K_PASCAL;
  6778. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6779. nwarps = NWARPS_Q6_K_PASCAL;
  6780. } else {
  6781. GGML_ASSERT(false);
  6782. }
  6783. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6784. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6785. const dim3 block_nums(block_num_x, block_num_y, 1);
  6786. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6787. if (nrows_x % mmq_y == 0) {
  6788. const bool need_check = false;
  6789. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6790. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6791. } else {
  6792. const bool need_check = true;
  6793. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6794. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6795. }
  6796. }
  6797. static void ggml_mul_mat_p021_f16_f32_cuda(
  6798. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6799. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6800. const dim3 block_nums(1, nrows_x, nchannels_y);
  6801. const dim3 block_dims(WARP_SIZE, 1, 1);
  6802. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6803. }
  6804. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6805. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6806. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6807. const dim3 block_nums(1, nrows_x, nchannels_y);
  6808. const dim3 block_dims(WARP_SIZE, 1, 1);
  6809. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6810. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6811. }
  6812. static void ggml_cpy_f16_f32_cuda(
  6813. const char * cx, char * cdst, const int ne,
  6814. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6815. 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) {
  6816. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6817. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6818. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6819. }
  6820. static void ggml_cpy_f32_f32_cuda(
  6821. const char * cx, char * cdst, const int ne,
  6822. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6823. 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) {
  6824. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6825. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6826. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6827. }
  6828. static void ggml_cpy_f32_f16_cuda(
  6829. const char * cx, char * cdst, const int ne,
  6830. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6831. 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) {
  6832. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6833. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6834. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6835. }
  6836. static void ggml_cpy_f32_q8_0_cuda(
  6837. const char * cx, char * cdst, const int ne,
  6838. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6839. 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) {
  6840. GGML_ASSERT(ne % QK8_0 == 0);
  6841. const int num_blocks = ne / QK8_0;
  6842. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6843. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6844. }
  6845. static void ggml_cpy_f32_q4_0_cuda(
  6846. const char * cx, char * cdst, const int ne,
  6847. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6848. 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) {
  6849. GGML_ASSERT(ne % QK4_0 == 0);
  6850. const int num_blocks = ne / QK4_0;
  6851. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6852. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6853. }
  6854. static void ggml_cpy_f32_q4_1_cuda(
  6855. const char * cx, char * cdst, const int ne,
  6856. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6857. 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) {
  6858. GGML_ASSERT(ne % QK4_1 == 0);
  6859. const int num_blocks = ne / QK4_1;
  6860. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6861. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6862. }
  6863. static void ggml_cpy_f16_f16_cuda(
  6864. const char * cx, char * cdst, const int ne,
  6865. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6866. 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) {
  6867. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6868. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6869. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6870. }
  6871. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  6872. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  6873. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  6874. }
  6875. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  6876. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  6877. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  6878. }
  6879. template<typename T>
  6880. static void rope_cuda(
  6881. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6882. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6883. ) {
  6884. GGML_ASSERT(ncols % 2 == 0);
  6885. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6886. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6887. const dim3 block_nums(nrows, num_blocks_x, 1);
  6888. if (pos == nullptr) {
  6889. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6890. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6891. );
  6892. } else {
  6893. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6894. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6895. );
  6896. }
  6897. }
  6898. template<typename T>
  6899. static void rope_neox_cuda(
  6900. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6901. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6902. ) {
  6903. GGML_ASSERT(ncols % 2 == 0);
  6904. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6905. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6906. const dim3 block_nums(nrows, num_blocks_x, 1);
  6907. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6908. const float inv_ndims = -1.0f / n_dims;
  6909. if (pos == nullptr) {
  6910. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6911. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6912. theta_scale, inv_ndims
  6913. );
  6914. } else {
  6915. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6916. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6917. theta_scale, inv_ndims
  6918. );
  6919. }
  6920. }
  6921. static void rope_glm_f32_cuda(
  6922. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6923. float freq_base, int n_ctx, cudaStream_t stream
  6924. ) {
  6925. GGML_ASSERT(ncols % 4 == 0);
  6926. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6927. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6928. const dim3 block_nums(num_blocks_x, nrows, 1);
  6929. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6930. }
  6931. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6932. const int k_rows, const int n_heads_log2_floor, const float m0,
  6933. const float m1, cudaStream_t stream) {
  6934. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6935. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6936. const dim3 block_nums(num_blocks_x, nrows, 1);
  6937. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6938. }
  6939. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6940. const dim3 block_dims(WARP_SIZE, 1, 1);
  6941. const dim3 block_nums(nrows, 1, 1);
  6942. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6943. }
  6944. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6945. // bitonic sort requires ncols to be power of 2
  6946. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6947. const dim3 block_dims(ncols, 1, 1);
  6948. const dim3 block_nums(1, nrows, 1);
  6949. if (order == GGML_SORT_ORDER_ASC) {
  6950. k_argsort_f32_i32<GGML_SORT_ORDER_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6951. } else if (order == GGML_SORT_ORDER_DESC) {
  6952. k_argsort_f32_i32<GGML_SORT_ORDER_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6953. } else {
  6954. GGML_ASSERT(false);
  6955. }
  6956. }
  6957. 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) {
  6958. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6959. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6960. const dim3 block_nums(nrows_x, block_num_x, 1);
  6961. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6962. }
  6963. 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) {
  6964. int nth = WARP_SIZE;
  6965. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6966. const dim3 block_dims(nth, 1, 1);
  6967. const dim3 block_nums(nrows_x, 1, 1);
  6968. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6969. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6970. const uint32_t n_head_kv = nrows_x/nrows_y;
  6971. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6972. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6973. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6974. if (shmem < g_device_caps[g_main_device].smpb) {
  6975. switch (ncols_x) {
  6976. case 32:
  6977. 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);
  6978. break;
  6979. case 64:
  6980. 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);
  6981. break;
  6982. case 128:
  6983. 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);
  6984. break;
  6985. case 256:
  6986. 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);
  6987. break;
  6988. case 512:
  6989. 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);
  6990. break;
  6991. case 1024:
  6992. 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);
  6993. break;
  6994. case 2048:
  6995. 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);
  6996. break;
  6997. case 4096:
  6998. 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);
  6999. break;
  7000. default:
  7001. 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);
  7002. break;
  7003. }
  7004. } else {
  7005. const size_t shmem_low = WARP_SIZE*sizeof(float);
  7006. 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);
  7007. }
  7008. }
  7009. template <typename T>
  7010. static void im2col_cuda(const float* x, T* dst,
  7011. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  7012. int batch, int batch_offset, int offset_delta,
  7013. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  7014. const int parallel_elements = OW * KW * KH;
  7015. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  7016. dim3 block_nums(num_blocks, OH, batch * IC);
  7017. 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);
  7018. }
  7019. // buffer pool for cuda
  7020. #define MAX_CUDA_BUFFERS 256
  7021. struct scoped_spin_lock {
  7022. std::atomic_flag& lock;
  7023. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  7024. while (lock.test_and_set(std::memory_order_acquire)) {
  7025. ; // spin
  7026. }
  7027. }
  7028. ~scoped_spin_lock() {
  7029. lock.clear(std::memory_order_release);
  7030. }
  7031. scoped_spin_lock(const scoped_spin_lock&) = delete;
  7032. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  7033. };
  7034. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  7035. // #define DEBUG_CUDA_MALLOC
  7036. struct ggml_cuda_buffer {
  7037. void * ptr = nullptr;
  7038. size_t size = 0;
  7039. };
  7040. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  7041. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  7042. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  7043. scoped_spin_lock lock(g_cuda_pool_lock);
  7044. #ifdef DEBUG_CUDA_MALLOC
  7045. int nnz = 0;
  7046. size_t max_size = 0;
  7047. #endif
  7048. size_t best_diff = 1ull << 36;
  7049. int ibest = -1;
  7050. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  7051. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  7052. if (b.ptr != nullptr) {
  7053. #ifdef DEBUG_CUDA_MALLOC
  7054. ++nnz;
  7055. if (b.size > max_size) max_size = b.size;
  7056. #endif
  7057. if (b.size >= size) {
  7058. size_t diff = b.size - size;
  7059. if (diff < best_diff) {
  7060. best_diff = diff;
  7061. ibest = i;
  7062. if (!best_diff) {
  7063. void * ptr = b.ptr;
  7064. *actual_size = b.size;
  7065. b.ptr = nullptr;
  7066. b.size = 0;
  7067. return ptr;
  7068. }
  7069. }
  7070. }
  7071. }
  7072. }
  7073. if (ibest >= 0) {
  7074. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  7075. void * ptr = b.ptr;
  7076. *actual_size = b.size;
  7077. b.ptr = nullptr;
  7078. b.size = 0;
  7079. return ptr;
  7080. }
  7081. void * ptr;
  7082. size_t look_ahead_size = (size_t) (1.05 * size);
  7083. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  7084. ggml_cuda_set_device(device);
  7085. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  7086. *actual_size = look_ahead_size;
  7087. g_cuda_pool_size[device] += look_ahead_size;
  7088. #ifdef DEBUG_CUDA_MALLOC
  7089. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, device, nnz,
  7090. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[device]/1024/1024), (uint32_t)(size/1024/1024));
  7091. #endif
  7092. return ptr;
  7093. }
  7094. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  7095. scoped_spin_lock lock(g_cuda_pool_lock);
  7096. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  7097. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  7098. if (b.ptr == nullptr) {
  7099. b.ptr = ptr;
  7100. b.size = size;
  7101. return;
  7102. }
  7103. }
  7104. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  7105. ggml_cuda_set_device(device);
  7106. CUDA_CHECK(cudaFree(ptr));
  7107. g_cuda_pool_size[device] -= size;
  7108. }
  7109. #if !defined(GGML_USE_HIPBLAS)
  7110. // pool with virtual memory
  7111. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  7112. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  7113. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  7114. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  7115. scoped_spin_lock lock(g_cuda_pool_lock);
  7116. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  7117. const size_t alignment = 128;
  7118. size = alignment * ((size + alignment - 1) / alignment);
  7119. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  7120. if (size > avail) {
  7121. // round up to the next multiple of the granularity
  7122. size_t reserve_size = size - avail;
  7123. const size_t granularity = g_device_caps[device].vmm_granularity;
  7124. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  7125. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  7126. // allocate more physical memory
  7127. CUmemAllocationProp prop = {};
  7128. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  7129. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  7130. prop.location.id = device;
  7131. CUmemGenericAllocationHandle handle;
  7132. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  7133. // reserve virtual address space (if not already reserved)
  7134. if (g_cuda_pool_addr[device] == 0) {
  7135. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  7136. }
  7137. // map at the end of the pool
  7138. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  7139. // the memory allocation handle is no longer needed after mapping
  7140. CU_CHECK(cuMemRelease(handle));
  7141. // set access
  7142. CUmemAccessDesc access = {};
  7143. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  7144. access.location.id = device;
  7145. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  7146. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  7147. // add to the pool
  7148. g_cuda_pool_size[device] += reserve_size;
  7149. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  7150. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  7151. // (unsigned long long) (reserve_size/1024/1024));
  7152. }
  7153. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  7154. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  7155. *actual_size = size;
  7156. g_cuda_pool_used[device] += size;
  7157. #ifdef DEBUG_CUDA_MALLOC
  7158. printf("cuda pool[%d]: allocated %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  7159. #endif
  7160. return ptr;
  7161. }
  7162. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  7163. scoped_spin_lock lock(g_cuda_pool_lock);
  7164. #ifdef DEBUG_CUDA_MALLOC
  7165. printf("cuda pool[%d]: freed %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  7166. #endif
  7167. g_cuda_pool_used[device] -= size;
  7168. // all deallocations must be in reverse order of the allocations
  7169. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  7170. }
  7171. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  7172. if (g_device_caps[device].vmm) {
  7173. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  7174. } else {
  7175. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  7176. }
  7177. }
  7178. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  7179. if (g_device_caps[device].vmm) {
  7180. ggml_cuda_pool_free_vmm(device, ptr, size);
  7181. } else {
  7182. ggml_cuda_pool_free_leg(device, ptr, size);
  7183. }
  7184. }
  7185. #else
  7186. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  7187. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  7188. #endif // !defined(GGML_USE_HIPBLAS)
  7189. template<typename T>
  7190. struct cuda_pool_alloc {
  7191. int device = -1;
  7192. T * ptr = nullptr;
  7193. size_t actual_size = 0;
  7194. // size is in number of elements
  7195. T * alloc(size_t size) {
  7196. GGML_ASSERT(ptr == nullptr);
  7197. CUDA_CHECK(cudaGetDevice(&device));
  7198. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  7199. return ptr;
  7200. }
  7201. cuda_pool_alloc(size_t size) {
  7202. alloc(size);
  7203. }
  7204. ~cuda_pool_alloc() {
  7205. if (ptr != nullptr) {
  7206. ggml_cuda_pool_free(device, ptr, actual_size);
  7207. }
  7208. }
  7209. T * get() {
  7210. return ptr;
  7211. }
  7212. cuda_pool_alloc() = default;
  7213. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  7214. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  7215. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  7216. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  7217. };
  7218. static bool g_cublas_loaded = false;
  7219. GGML_CALL bool ggml_cublas_loaded(void) {
  7220. return g_cublas_loaded;
  7221. }
  7222. GGML_CALL void ggml_init_cublas() {
  7223. static bool initialized = false;
  7224. if (!initialized) {
  7225. #ifdef __HIP_PLATFORM_AMD__
  7226. // Workaround for a rocBLAS bug when using multiple graphics cards:
  7227. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  7228. rocblas_initialize();
  7229. CUDA_CHECK(cudaDeviceSynchronize());
  7230. #endif
  7231. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  7232. initialized = true;
  7233. g_cublas_loaded = false;
  7234. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  7235. return;
  7236. }
  7237. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  7238. int64_t total_vram = 0;
  7239. #if defined(GGML_CUDA_FORCE_MMQ)
  7240. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  7241. #else
  7242. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  7243. #endif
  7244. #if defined(CUDA_USE_TENSOR_CORES)
  7245. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  7246. #else
  7247. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  7248. #endif
  7249. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  7250. for (int id = 0; id < g_device_count; ++id) {
  7251. int device_vmm = 0;
  7252. #if !defined(GGML_USE_HIPBLAS)
  7253. CUdevice device;
  7254. CU_CHECK(cuDeviceGet(&device, id));
  7255. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  7256. if (device_vmm) {
  7257. CUmemAllocationProp alloc_prop = {};
  7258. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  7259. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  7260. alloc_prop.location.id = id;
  7261. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  7262. }
  7263. #endif // !defined(GGML_USE_HIPBLAS)
  7264. g_device_caps[id].vmm = !!device_vmm;
  7265. cudaDeviceProp prop;
  7266. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  7267. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  7268. g_default_tensor_split[id] = total_vram;
  7269. total_vram += prop.totalGlobalMem;
  7270. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7271. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  7272. #else
  7273. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  7274. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7275. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  7276. }
  7277. for (int id = 0; id < g_device_count; ++id) {
  7278. g_default_tensor_split[id] /= total_vram;
  7279. }
  7280. for (int id = 0; id < g_device_count; ++id) {
  7281. ggml_cuda_set_device(id);
  7282. // create cuda streams
  7283. for (int is = 0; is < MAX_STREAMS; ++is) {
  7284. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  7285. }
  7286. // create cublas handle
  7287. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  7288. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  7289. }
  7290. // configure logging to stdout
  7291. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  7292. initialized = true;
  7293. g_cublas_loaded = true;
  7294. }
  7295. }
  7296. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  7297. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  7298. return nullptr;
  7299. }
  7300. void * ptr = nullptr;
  7301. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  7302. if (err != cudaSuccess) {
  7303. // clear the error
  7304. cudaGetLastError();
  7305. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  7306. size/1024.0/1024.0, cudaGetErrorString(err));
  7307. return nullptr;
  7308. }
  7309. return ptr;
  7310. }
  7311. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  7312. CUDA_CHECK(cudaFreeHost(ptr));
  7313. }
  7314. static cudaError_t ggml_cuda_cpy_tensor_2d(
  7315. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  7316. cudaMemcpyKind kind;
  7317. char * src_ptr;
  7318. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  7319. kind = cudaMemcpyHostToDevice;
  7320. src_ptr = (char *) src->data;
  7321. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  7322. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  7323. kind = cudaMemcpyDeviceToDevice;
  7324. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  7325. int id;
  7326. CUDA_CHECK(cudaGetDevice(&id));
  7327. src_ptr = (char *) extra->data_device[id];
  7328. } else {
  7329. GGML_ASSERT(false);
  7330. }
  7331. char * dst_ptr = (char *) dst;
  7332. const int64_t ne0 = src->ne[0];
  7333. const int64_t nb0 = src->nb[0];
  7334. const int64_t nb1 = src->nb[1];
  7335. const int64_t nb2 = src->nb[2];
  7336. const int64_t nb3 = src->nb[3];
  7337. const enum ggml_type type = src->type;
  7338. const int64_t ts = ggml_type_size(type);
  7339. const int64_t bs = ggml_blck_size(type);
  7340. int64_t i1_diff = i1_high - i1_low;
  7341. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  7342. if (nb0 == ts && nb1 == ts*ne0/bs) {
  7343. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  7344. } else if (nb0 == ts) {
  7345. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  7346. } else {
  7347. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  7348. const void * rx = (const void *) ((const char *) x + i1*nb1);
  7349. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  7350. // pretend the row is a matrix with cols=1
  7351. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  7352. if (r != cudaSuccess) return r;
  7353. }
  7354. return cudaSuccess;
  7355. }
  7356. }
  7357. static void ggml_cuda_op_get_rows(
  7358. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7359. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  7360. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7361. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7362. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  7363. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  7364. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  7365. const int32_t * src1_i32 = (const int32_t *) src1_d;
  7366. switch (src0->type) {
  7367. case GGML_TYPE_F16:
  7368. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  7369. break;
  7370. case GGML_TYPE_F32:
  7371. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7372. break;
  7373. case GGML_TYPE_Q4_0:
  7374. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7375. break;
  7376. case GGML_TYPE_Q4_1:
  7377. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7378. break;
  7379. case GGML_TYPE_Q5_0:
  7380. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7381. break;
  7382. case GGML_TYPE_Q5_1:
  7383. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7384. break;
  7385. case GGML_TYPE_Q8_0:
  7386. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7387. break;
  7388. default:
  7389. // TODO: k-quants
  7390. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  7391. GGML_ASSERT(false);
  7392. break;
  7393. }
  7394. }
  7395. template<class op>
  7396. static void ggml_cuda_op_bin_bcast(
  7397. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7398. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7399. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7400. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7401. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7402. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7403. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  7404. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  7405. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  7406. } else {
  7407. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  7408. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  7409. GGML_ASSERT(false);
  7410. }
  7411. }
  7412. static void ggml_cuda_op_repeat(
  7413. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7414. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  7415. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  7416. (void) src1;
  7417. (void) src1_d;
  7418. }
  7419. static void ggml_cuda_op_add(
  7420. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7421. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7422. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7423. }
  7424. static void ggml_cuda_op_acc(
  7425. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7426. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7427. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7428. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7429. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7430. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  7431. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  7432. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  7433. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  7434. int offset = dst->op_params[3] / 4; // offset in bytes
  7435. 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);
  7436. (void) dst;
  7437. }
  7438. static void ggml_cuda_op_mul(
  7439. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7440. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7441. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7442. }
  7443. static void ggml_cuda_op_div(
  7444. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7445. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7446. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7447. }
  7448. static void ggml_cuda_op_gelu(
  7449. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7450. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7451. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7452. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7453. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7454. (void) src1;
  7455. (void) dst;
  7456. (void) src1_dd;
  7457. }
  7458. static void ggml_cuda_op_silu(
  7459. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7460. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7461. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7462. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7463. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7464. (void) src1;
  7465. (void) dst;
  7466. (void) src1_dd;
  7467. }
  7468. static void ggml_cuda_op_gelu_quick(
  7469. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7470. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7471. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7472. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7473. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7474. (void) src1;
  7475. (void) dst;
  7476. (void) src1_dd;
  7477. }
  7478. static void ggml_cuda_op_tanh(
  7479. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7480. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7481. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7482. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7483. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7484. (void) src1;
  7485. (void) dst;
  7486. (void) src1_dd;
  7487. }
  7488. static void ggml_cuda_op_relu(
  7489. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7490. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7491. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7492. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7493. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7494. (void) src1;
  7495. (void) dst;
  7496. (void) src1_dd;
  7497. }
  7498. static void ggml_cuda_op_hardsigmoid(
  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. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7504. (void) src1;
  7505. (void) dst;
  7506. (void) src1_dd;
  7507. }
  7508. static void ggml_cuda_op_hardswish(
  7509. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7510. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7511. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7512. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7513. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7514. (void) src1;
  7515. (void) dst;
  7516. (void) src1_dd;
  7517. }
  7518. static void ggml_cuda_op_leaky_relu(
  7519. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7520. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7521. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7522. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7523. float negative_slope;
  7524. memcpy(&negative_slope, dst->op_params, sizeof(float));
  7525. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  7526. (void) src1;
  7527. (void) dst;
  7528. (void) src1_dd;
  7529. }
  7530. static void ggml_cuda_op_sqr(
  7531. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7532. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7533. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7534. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7535. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7536. (void) src1;
  7537. (void) dst;
  7538. (void) src1_dd;
  7539. }
  7540. static void ggml_cuda_op_norm(
  7541. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7542. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7543. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7544. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7545. const int64_t ne00 = src0->ne[0];
  7546. const int64_t nrows = ggml_nrows(src0);
  7547. float eps;
  7548. memcpy(&eps, dst->op_params, sizeof(float));
  7549. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7550. (void) src1;
  7551. (void) dst;
  7552. (void) src1_dd;
  7553. }
  7554. static void ggml_cuda_op_group_norm(
  7555. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7556. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7557. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7558. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7559. int num_groups = dst->op_params[0];
  7560. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  7561. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  7562. (void) src1;
  7563. (void) dst;
  7564. (void) src1_dd;
  7565. }
  7566. static void ggml_cuda_op_concat(
  7567. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7568. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7569. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7570. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7571. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7572. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  7573. 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);
  7574. }
  7575. (void) src1;
  7576. (void) dst;
  7577. }
  7578. static void ggml_cuda_op_upscale(
  7579. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7580. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7581. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7582. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7583. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7584. const int scale_factor = dst->op_params[0];
  7585. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  7586. (void) src1;
  7587. (void) dst;
  7588. (void) src1_dd;
  7589. }
  7590. static void ggml_cuda_op_pad(
  7591. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7592. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7593. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7594. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7595. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7596. pad_f32_cuda(src0_dd, dst_dd,
  7597. src0->ne[0], src0->ne[1], src0->ne[2],
  7598. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  7599. (void) src1;
  7600. (void) dst;
  7601. (void) src1_dd;
  7602. }
  7603. static void ggml_cuda_op_rms_norm(
  7604. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7605. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7606. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7607. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7608. const int64_t ne00 = src0->ne[0];
  7609. const int64_t nrows = ggml_nrows(src0);
  7610. float eps;
  7611. memcpy(&eps, dst->op_params, sizeof(float));
  7612. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7613. (void) src1;
  7614. (void) dst;
  7615. (void) src1_dd;
  7616. }
  7617. static void ggml_cuda_op_mul_mat_q(
  7618. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7619. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7620. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7621. const int64_t ne00 = src0->ne[0];
  7622. const int64_t ne10 = src1->ne[0];
  7623. GGML_ASSERT(ne10 % QK8_1 == 0);
  7624. const int64_t ne0 = dst->ne[0];
  7625. const int64_t row_diff = row_high - row_low;
  7626. int id;
  7627. CUDA_CHECK(cudaGetDevice(&id));
  7628. // the main device has a larger memory buffer to hold the results from all GPUs
  7629. // nrows_dst == nrows of the matrix that the kernel writes into
  7630. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7631. switch (src0->type) {
  7632. case GGML_TYPE_Q4_0:
  7633. 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);
  7634. break;
  7635. case GGML_TYPE_Q4_1:
  7636. 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);
  7637. break;
  7638. case GGML_TYPE_Q5_0:
  7639. 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);
  7640. break;
  7641. case GGML_TYPE_Q5_1:
  7642. 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);
  7643. break;
  7644. case GGML_TYPE_Q8_0:
  7645. 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);
  7646. break;
  7647. case GGML_TYPE_Q2_K:
  7648. 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);
  7649. break;
  7650. case GGML_TYPE_Q3_K:
  7651. 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);
  7652. break;
  7653. case GGML_TYPE_Q4_K:
  7654. 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);
  7655. break;
  7656. case GGML_TYPE_Q5_K:
  7657. 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);
  7658. break;
  7659. case GGML_TYPE_Q6_K:
  7660. 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);
  7661. break;
  7662. default:
  7663. GGML_ASSERT(false);
  7664. break;
  7665. }
  7666. (void) src1;
  7667. (void) dst;
  7668. (void) src1_ddf_i;
  7669. }
  7670. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  7671. int64_t min_compute_capability = INT_MAX;
  7672. int64_t max_compute_capability = INT_MIN;
  7673. for (int id = 0; id < g_device_count; ++id) {
  7674. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  7675. if (min_compute_capability > g_device_caps[id].cc) {
  7676. min_compute_capability = g_device_caps[id].cc;
  7677. }
  7678. if (max_compute_capability < g_device_caps[id].cc) {
  7679. max_compute_capability = g_device_caps[id].cc;
  7680. }
  7681. }
  7682. }
  7683. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7684. switch(type) {
  7685. case GGML_TYPE_Q4_0:
  7686. case GGML_TYPE_Q4_1:
  7687. case GGML_TYPE_Q5_0:
  7688. case GGML_TYPE_Q5_1:
  7689. case GGML_TYPE_Q8_0:
  7690. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7691. case GGML_TYPE_F16:
  7692. case GGML_TYPE_F32:
  7693. return 1;
  7694. case GGML_TYPE_Q2_K:
  7695. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  7696. case GGML_TYPE_Q3_K:
  7697. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  7698. case GGML_TYPE_Q4_K:
  7699. case GGML_TYPE_Q5_K:
  7700. case GGML_TYPE_Q6_K:
  7701. case GGML_TYPE_IQ2_XXS:
  7702. case GGML_TYPE_IQ2_XS:
  7703. case GGML_TYPE_IQ2_S:
  7704. case GGML_TYPE_IQ3_XXS:
  7705. case GGML_TYPE_IQ1_S:
  7706. case GGML_TYPE_IQ4_NL:
  7707. case GGML_TYPE_IQ4_XS:
  7708. case GGML_TYPE_IQ3_S:
  7709. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7710. default:
  7711. GGML_ASSERT(false);
  7712. }
  7713. #else
  7714. switch(type) {
  7715. case GGML_TYPE_Q4_0:
  7716. case GGML_TYPE_Q4_1:
  7717. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7718. case GGML_TYPE_Q5_0:
  7719. case GGML_TYPE_Q5_1:
  7720. case GGML_TYPE_Q8_0:
  7721. return 64;
  7722. case GGML_TYPE_F16:
  7723. case GGML_TYPE_F32:
  7724. return 1;
  7725. case GGML_TYPE_Q2_K:
  7726. case GGML_TYPE_Q3_K:
  7727. case GGML_TYPE_Q4_K:
  7728. case GGML_TYPE_Q5_K:
  7729. case GGML_TYPE_IQ2_XXS:
  7730. case GGML_TYPE_IQ2_XS:
  7731. case GGML_TYPE_IQ2_S:
  7732. case GGML_TYPE_IQ3_XXS:
  7733. case GGML_TYPE_IQ1_S:
  7734. case GGML_TYPE_IQ4_NL:
  7735. case GGML_TYPE_IQ4_XS:
  7736. case GGML_TYPE_IQ3_S:
  7737. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7738. case GGML_TYPE_Q6_K:
  7739. return 64;
  7740. default:
  7741. GGML_ASSERT(false);
  7742. }
  7743. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7744. }
  7745. 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) {
  7746. const int64_t nrows = ggml_nrows(tensor);
  7747. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  7748. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  7749. *row_low -= *row_low % rounding;
  7750. if (id == g_device_count - 1) {
  7751. *row_high = nrows;
  7752. } else {
  7753. *row_high = nrows*tensor_split[id + 1];
  7754. *row_high -= *row_high % rounding;
  7755. }
  7756. }
  7757. static void ggml_cuda_op_mul_mat_vec_q(
  7758. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7759. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7760. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7761. const int64_t ne00 = src0->ne[0];
  7762. const int64_t row_diff = row_high - row_low;
  7763. const int64_t ne10 = src1->ne[0];
  7764. GGML_ASSERT(ne10 % QK8_1 == 0);
  7765. const int64_t ne0 = dst->ne[0];
  7766. int id;
  7767. CUDA_CHECK(cudaGetDevice(&id));
  7768. // the main device has a larger memory buffer to hold the results from all GPUs
  7769. // nrows_dst == nrows of the matrix that the kernel writes into
  7770. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7771. switch (src0->type) {
  7772. case GGML_TYPE_Q4_0:
  7773. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7774. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7775. break;
  7776. case GGML_TYPE_Q4_1:
  7777. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7778. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7779. break;
  7780. case GGML_TYPE_Q5_0:
  7781. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7782. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7783. break;
  7784. case GGML_TYPE_Q5_1:
  7785. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7786. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7787. break;
  7788. case GGML_TYPE_Q8_0:
  7789. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7790. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7791. break;
  7792. case GGML_TYPE_Q2_K:
  7793. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7794. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7795. break;
  7796. case GGML_TYPE_Q3_K:
  7797. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7798. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7799. break;
  7800. case GGML_TYPE_Q4_K:
  7801. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7802. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7803. break;
  7804. case GGML_TYPE_Q5_K:
  7805. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7806. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7807. break;
  7808. case GGML_TYPE_Q6_K:
  7809. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7810. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7811. break;
  7812. case GGML_TYPE_IQ2_XXS:
  7813. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7814. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7815. break;
  7816. case GGML_TYPE_IQ2_XS:
  7817. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7818. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7819. break;
  7820. case GGML_TYPE_IQ2_S:
  7821. mul_mat_vec_q_cuda<QK_K, QI2_S, block_iq2_s, 1, vec_dot_iq2_s_q8_1>
  7822. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7823. break;
  7824. case GGML_TYPE_IQ3_XXS:
  7825. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7826. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7827. break;
  7828. case GGML_TYPE_IQ1_S:
  7829. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7830. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7831. break;
  7832. case GGML_TYPE_IQ4_NL:
  7833. mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
  7834. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7835. break;
  7836. case GGML_TYPE_IQ4_XS:
  7837. mul_mat_vec_q_cuda<QK_K, QI4_XS, block_iq4_xs, 1, vec_dot_iq4_xs_q8_1>
  7838. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7839. break;
  7840. case GGML_TYPE_IQ3_S:
  7841. mul_mat_vec_q_cuda<QK_K, QI3_XS, block_iq3_s, 1, vec_dot_iq3_s_q8_1>
  7842. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7843. break;
  7844. default:
  7845. GGML_ASSERT(false);
  7846. break;
  7847. }
  7848. (void) src1;
  7849. (void) dst;
  7850. (void) src1_ddf_i;
  7851. (void) src1_ncols;
  7852. (void) src1_padded_row_size;
  7853. }
  7854. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7855. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7856. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7857. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7858. const int64_t ne00 = src0->ne[0];
  7859. const int64_t row_diff = row_high - row_low;
  7860. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7861. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7862. #ifdef GGML_CUDA_F16
  7863. cuda_pool_alloc<half> src1_dfloat_a;
  7864. half * src1_dfloat = nullptr; // dfloat == half
  7865. bool src1_convert_f16 =
  7866. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7867. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7868. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7869. if (src1_convert_f16) {
  7870. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7871. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7872. GGML_ASSERT(to_fp16_cuda != nullptr);
  7873. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7874. }
  7875. #else
  7876. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7877. #endif // GGML_CUDA_F16
  7878. switch (src0->type) {
  7879. case GGML_TYPE_Q4_0:
  7880. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7881. break;
  7882. case GGML_TYPE_Q4_1:
  7883. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7884. break;
  7885. case GGML_TYPE_Q5_0:
  7886. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7887. break;
  7888. case GGML_TYPE_Q5_1:
  7889. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7890. break;
  7891. case GGML_TYPE_Q8_0:
  7892. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7893. break;
  7894. case GGML_TYPE_Q2_K:
  7895. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7896. break;
  7897. case GGML_TYPE_Q3_K:
  7898. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7899. break;
  7900. case GGML_TYPE_Q4_K:
  7901. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7902. break;
  7903. case GGML_TYPE_Q5_K:
  7904. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7905. break;
  7906. case GGML_TYPE_Q6_K:
  7907. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7908. break;
  7909. case GGML_TYPE_F16:
  7910. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7911. break;
  7912. default:
  7913. GGML_ASSERT(false);
  7914. break;
  7915. }
  7916. (void) src1;
  7917. (void) dst;
  7918. (void) src1_ddq_i;
  7919. (void) src1_ncols;
  7920. (void) src1_padded_row_size;
  7921. }
  7922. static void ggml_cuda_op_mul_mat_cublas(
  7923. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7924. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7925. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7926. GGML_ASSERT(src0_dd_i != nullptr);
  7927. GGML_ASSERT(src1_ddf_i != nullptr);
  7928. GGML_ASSERT(dst_dd_i != nullptr);
  7929. const int64_t ne00 = src0->ne[0];
  7930. const int64_t ne10 = src1->ne[0];
  7931. const int64_t ne0 = dst->ne[0];
  7932. const int64_t row_diff = row_high - row_low;
  7933. int id;
  7934. CUDA_CHECK(cudaGetDevice(&id));
  7935. // the main device has a larger memory buffer to hold the results from all GPUs
  7936. // ldc == nrows of the matrix that cuBLAS writes into
  7937. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7938. const int compute_capability = g_device_caps[id].cc;
  7939. 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) {
  7940. //printf("this branch\n");
  7941. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7942. cuda_pool_alloc<half> src0_as_f16;
  7943. if (src0->type != GGML_TYPE_F16) {
  7944. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7945. GGML_ASSERT(to_fp16_cuda != nullptr);
  7946. size_t ne = row_diff*ne00;
  7947. src0_as_f16.alloc(ne);
  7948. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7949. }
  7950. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7951. cuda_pool_alloc<half> src1_as_f16;
  7952. if (src1->type != GGML_TYPE_F16) {
  7953. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7954. GGML_ASSERT(to_fp16_cuda != nullptr);
  7955. size_t ne = src1_ncols*ne10;
  7956. src1_as_f16.alloc(ne);
  7957. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7958. }
  7959. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7960. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7961. const half alpha_f16 = 1.0f;
  7962. const half beta_f16 = 0.0f;
  7963. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7964. CUBLAS_CHECK(
  7965. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7966. row_diff, src1_ncols, ne10,
  7967. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7968. src1_ptr, CUDA_R_16F, ne10,
  7969. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7970. CUBLAS_COMPUTE_16F,
  7971. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7972. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7973. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7974. } else {
  7975. cuda_pool_alloc<float> src0_ddq_as_f32;
  7976. cuda_pool_alloc<float> src1_ddq_as_f32;
  7977. if (src0->type != GGML_TYPE_F32) {
  7978. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7979. GGML_ASSERT(to_fp32_cuda != nullptr);
  7980. src0_ddq_as_f32.alloc(row_diff*ne00);
  7981. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7982. }
  7983. if (src1->type != GGML_TYPE_F32) {
  7984. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7985. GGML_ASSERT(to_fp32_cuda != nullptr);
  7986. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7987. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7988. }
  7989. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7990. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7991. const float alpha = 1.0f;
  7992. const float beta = 0.0f;
  7993. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7994. CUBLAS_CHECK(
  7995. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7996. row_diff, src1_ncols, ne10,
  7997. &alpha, src0_ddf_i, ne00,
  7998. src1_ddf1_i, ne10,
  7999. &beta, dst_dd_i, ldc));
  8000. }
  8001. (void) dst;
  8002. (void) src1_ddq_i;
  8003. (void) src1_padded_row_size;
  8004. }
  8005. static void ggml_cuda_op_rope(
  8006. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8007. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8008. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  8009. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  8010. GGML_ASSERT(src0->type == dst->type);
  8011. const int64_t ne00 = src0->ne[0];
  8012. const int64_t ne01 = src0->ne[1];
  8013. const int64_t ne2 = dst->ne[2];
  8014. const int64_t nrows = ggml_nrows(src0);
  8015. //const int n_past = ((int32_t *) dst->op_params)[0];
  8016. const int n_dims = ((int32_t *) dst->op_params)[1];
  8017. const int mode = ((int32_t *) dst->op_params)[2];
  8018. const int n_ctx = ((int32_t *) dst->op_params)[3];
  8019. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  8020. // RoPE alteration for extended context
  8021. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  8022. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  8023. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  8024. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  8025. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  8026. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  8027. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  8028. const int32_t * pos = nullptr;
  8029. if ((mode & 1) == 0) {
  8030. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  8031. GGML_ASSERT(src1->ne[0] == ne2);
  8032. pos = (const int32_t *) src1_dd;
  8033. }
  8034. const bool is_neox = mode & 2;
  8035. const bool is_glm = mode & 4;
  8036. rope_corr_dims corr_dims;
  8037. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  8038. // compute
  8039. if (is_glm) {
  8040. GGML_ASSERT(false);
  8041. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  8042. } else if (is_neox) {
  8043. if (src0->type == GGML_TYPE_F32) {
  8044. rope_neox_cuda(
  8045. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  8046. attn_factor, corr_dims, main_stream
  8047. );
  8048. } else if (src0->type == GGML_TYPE_F16) {
  8049. rope_neox_cuda(
  8050. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  8051. attn_factor, corr_dims, main_stream
  8052. );
  8053. } else {
  8054. GGML_ASSERT(false);
  8055. }
  8056. } else {
  8057. if (src0->type == GGML_TYPE_F32) {
  8058. rope_cuda(
  8059. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  8060. attn_factor, corr_dims, main_stream
  8061. );
  8062. } else if (src0->type == GGML_TYPE_F16) {
  8063. rope_cuda(
  8064. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  8065. attn_factor, corr_dims, main_stream
  8066. );
  8067. } else {
  8068. GGML_ASSERT(false);
  8069. }
  8070. }
  8071. (void) src1;
  8072. (void) dst;
  8073. (void) src1_dd;
  8074. }
  8075. static void ggml_cuda_op_alibi(
  8076. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8077. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8078. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8079. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8080. const int64_t ne00 = src0->ne[0];
  8081. const int64_t ne01 = src0->ne[1];
  8082. const int64_t ne02 = src0->ne[2];
  8083. const int64_t nrows = ggml_nrows(src0);
  8084. //const int n_past = ((int32_t *) dst->op_params)[0];
  8085. const int n_head = ((int32_t *) dst->op_params)[1];
  8086. float max_bias;
  8087. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  8088. //GGML_ASSERT(ne01 + n_past == ne00);
  8089. GGML_ASSERT(n_head == ne02);
  8090. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  8091. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  8092. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  8093. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  8094. (void) src1;
  8095. (void) src1_dd;
  8096. }
  8097. static void ggml_cuda_op_pool2d(
  8098. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8099. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8100. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8101. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8102. const int32_t * opts = (const int32_t *)dst->op_params;
  8103. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  8104. const int k0 = opts[1];
  8105. const int k1 = opts[2];
  8106. const int s0 = opts[3];
  8107. const int s1 = opts[4];
  8108. const int p0 = opts[5];
  8109. const int p1 = opts[6];
  8110. const int64_t IH = src0->ne[1];
  8111. const int64_t IW = src0->ne[0];
  8112. const int64_t N = dst->ne[3];
  8113. const int64_t OC = dst->ne[2];
  8114. const int64_t OH = dst->ne[1];
  8115. const int64_t OW = dst->ne[0];
  8116. const int parallel_elements = N * OC * OH * OW;
  8117. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  8118. dim3 block_nums(num_blocks);
  8119. 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);
  8120. (void) src1;
  8121. (void) src1_dd;
  8122. }
  8123. static void ggml_cuda_op_im2col(
  8124. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8125. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8126. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8127. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8128. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  8129. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8130. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  8131. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  8132. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  8133. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  8134. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  8135. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  8136. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  8137. const int64_t IH = is_2D ? src1->ne[1] : 1;
  8138. const int64_t IW = src1->ne[0];
  8139. const int64_t KH = is_2D ? src0->ne[1] : 1;
  8140. const int64_t KW = src0->ne[0];
  8141. const int64_t OH = is_2D ? dst->ne[2] : 1;
  8142. const int64_t OW = dst->ne[1];
  8143. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  8144. const int64_t batch = src1->ne[3];
  8145. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  8146. if(dst->type == GGML_TYPE_F16) {
  8147. 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);
  8148. } else {
  8149. 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);
  8150. }
  8151. (void) src0;
  8152. (void) src0_dd;
  8153. }
  8154. static void ggml_cuda_op_sum_rows(
  8155. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8156. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8157. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8158. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8159. const int64_t ncols = src0->ne[0];
  8160. const int64_t nrows = ggml_nrows(src0);
  8161. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  8162. (void) src1;
  8163. (void) dst;
  8164. (void) src1_dd;
  8165. }
  8166. static void ggml_cuda_op_argsort(
  8167. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8168. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8169. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8170. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  8171. const int64_t ncols = src0->ne[0];
  8172. const int64_t nrows = ggml_nrows(src0);
  8173. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  8174. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  8175. (void) src1;
  8176. (void) dst;
  8177. (void) src1_dd;
  8178. }
  8179. static void ggml_cuda_op_diag_mask_inf(
  8180. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8181. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8182. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8183. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8184. const int64_t ne00 = src0->ne[0];
  8185. const int64_t ne01 = src0->ne[1];
  8186. const int nrows0 = ggml_nrows(src0);
  8187. const int n_past = ((int32_t *) dst->op_params)[0];
  8188. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  8189. (void) src1;
  8190. (void) dst;
  8191. (void) src1_dd;
  8192. }
  8193. static void ggml_cuda_op_soft_max(
  8194. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8195. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8196. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8197. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8198. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  8199. const int64_t ne00 = src0->ne[0];
  8200. const int64_t nrows_x = ggml_nrows(src0);
  8201. const int64_t nrows_y = src0->ne[1];
  8202. float scale = 1.0f;
  8203. float max_bias = 0.0f;
  8204. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  8205. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  8206. // positions tensor
  8207. float * src2_dd = nullptr;
  8208. cuda_pool_alloc<float> src2_f;
  8209. ggml_tensor * src2 = dst->src[2];
  8210. const bool use_src2 = src2 != nullptr;
  8211. if (use_src2) {
  8212. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  8213. if (src2_on_device) {
  8214. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  8215. src2_dd = (float *) src2_extra->data_device[g_main_device];
  8216. } else {
  8217. src2_dd = src2_f.alloc(ggml_nelements(src2));
  8218. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  8219. }
  8220. }
  8221. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  8222. }
  8223. static void ggml_cuda_op_scale(
  8224. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8225. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8226. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8227. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8228. float scale;
  8229. memcpy(&scale, dst->op_params, sizeof(float));
  8230. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  8231. CUDA_CHECK(cudaGetLastError());
  8232. (void) src1;
  8233. (void) dst;
  8234. (void) src1_dd;
  8235. }
  8236. static void ggml_cuda_op_clamp(
  8237. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8238. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8239. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8240. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8241. float min;
  8242. float max;
  8243. memcpy(&min, dst->op_params, sizeof(float));
  8244. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  8245. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  8246. CUDA_CHECK(cudaGetLastError());
  8247. (void) src1;
  8248. (void) dst;
  8249. (void) src1_dd;
  8250. }
  8251. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  8252. const int64_t nrows0 = ggml_nrows(src0);
  8253. const bool use_src1 = src1 != nullptr;
  8254. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  8255. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8256. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8257. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8258. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  8259. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8260. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8261. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  8262. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  8263. // dd = data device
  8264. float * src0_ddf = nullptr;
  8265. float * src1_ddf = nullptr;
  8266. float * dst_ddf = nullptr;
  8267. cuda_pool_alloc<float> src0_f;
  8268. cuda_pool_alloc<float> src1_f;
  8269. cuda_pool_alloc<float> dst_f;
  8270. ggml_cuda_set_device(g_main_device);
  8271. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8272. if (src0_on_device) {
  8273. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  8274. } else {
  8275. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  8276. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  8277. }
  8278. if (use_src1) {
  8279. if (src1_on_device) {
  8280. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8281. } else {
  8282. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  8283. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  8284. }
  8285. }
  8286. if (dst_on_device) {
  8287. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8288. } else {
  8289. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  8290. }
  8291. // do the computation
  8292. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  8293. CUDA_CHECK(cudaGetLastError());
  8294. // copy dst to host if necessary
  8295. if (!dst_on_device) {
  8296. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  8297. }
  8298. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8299. CUDA_CHECK(cudaDeviceSynchronize());
  8300. }
  8301. }
  8302. static void ggml_cuda_set_peer_access(const int n_tokens) {
  8303. static bool peer_access_enabled = false;
  8304. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  8305. if (peer_access_enabled == enable_peer_access) {
  8306. return;
  8307. }
  8308. #ifdef NDEBUG
  8309. for (int id = 0; id < g_device_count; ++id) {
  8310. ggml_cuda_set_device(id);
  8311. CUDA_CHECK(cudaDeviceSynchronize());
  8312. }
  8313. for (int id = 0; id < g_device_count; ++id) {
  8314. ggml_cuda_set_device(id);
  8315. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  8316. if (id == id_other) {
  8317. continue;
  8318. }
  8319. if (id != g_main_device && id_other != g_main_device) {
  8320. continue;
  8321. }
  8322. int can_access_peer;
  8323. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  8324. if (can_access_peer) {
  8325. if (enable_peer_access) {
  8326. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  8327. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  8328. CUDA_CHECK(err);
  8329. }
  8330. } else {
  8331. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  8332. if (err != cudaErrorPeerAccessNotEnabled) {
  8333. CUDA_CHECK(err);
  8334. }
  8335. }
  8336. }
  8337. }
  8338. }
  8339. #endif // NDEBUG
  8340. peer_access_enabled = enable_peer_access;
  8341. }
  8342. // FIXME: move this somewhere else
  8343. struct ggml_backend_cuda_split_buffer_type_context {
  8344. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  8345. };
  8346. static void ggml_cuda_op_mul_mat(
  8347. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  8348. const bool convert_src1_to_q8_1) {
  8349. const int64_t ne00 = src0->ne[0];
  8350. const int64_t ne01 = src0->ne[1];
  8351. const int64_t ne02 = src0->ne[2];
  8352. const int64_t ne03 = src0->ne[3];
  8353. const int64_t ne10 = src1->ne[0];
  8354. const int64_t ne11 = src1->ne[1];
  8355. const int64_t ne12 = src1->ne[2];
  8356. const int64_t ne13 = src1->ne[3];
  8357. const int64_t nrows1 = ggml_nrows(src1);
  8358. GGML_ASSERT(ne03 == ne13);
  8359. const int64_t ne0 = dst->ne[0];
  8360. const int64_t ne1 = dst->ne[1];
  8361. const int nb2 = dst->nb[2];
  8362. const int nb3 = dst->nb[3];
  8363. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8364. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8365. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  8366. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  8367. const int64_t i02_divisor = ne12 / ne02;
  8368. const size_t src0_ts = ggml_type_size(src0->type);
  8369. const size_t src0_bs = ggml_blck_size(src0->type);
  8370. const size_t q8_1_ts = sizeof(block_q8_1);
  8371. const size_t q8_1_bs = QK8_1;
  8372. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8373. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8374. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8375. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8376. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  8377. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  8378. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  8379. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8380. GGML_ASSERT(!(split && ne02 > 1));
  8381. GGML_ASSERT(!(split && ne03 > 1));
  8382. GGML_ASSERT(!(split && ne02 < ne12));
  8383. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  8384. if (split) {
  8385. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  8386. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  8387. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8388. tensor_split = buft_ctx->tensor_split;
  8389. }
  8390. struct dev_data {
  8391. cuda_pool_alloc<char> src0_dd_alloc;
  8392. cuda_pool_alloc<float> src1_ddf_alloc;
  8393. cuda_pool_alloc<char> src1_ddq_alloc;
  8394. cuda_pool_alloc<float> dst_dd_alloc;
  8395. char * src0_dd = nullptr;
  8396. float * src1_ddf = nullptr; // float
  8397. char * src1_ddq = nullptr; // q8_1
  8398. float * dst_dd = nullptr;
  8399. int64_t row_low;
  8400. int64_t row_high;
  8401. };
  8402. dev_data dev[GGML_CUDA_MAX_DEVICES];
  8403. int used_devices = 0;
  8404. for (int id = 0; id < g_device_count; ++id) {
  8405. // by default, use all rows
  8406. dev[id].row_low = 0;
  8407. dev[id].row_high = ne01;
  8408. // for multi GPU, get the row boundaries from tensor split
  8409. // and round to mul_mat_q tile sizes
  8410. if (split) {
  8411. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  8412. if (id != 0) {
  8413. dev[id].row_low = ne01*tensor_split[id];
  8414. if (dev[id].row_low < ne01) {
  8415. dev[id].row_low -= dev[id].row_low % rounding;
  8416. }
  8417. }
  8418. if (id != g_device_count - 1) {
  8419. dev[id].row_high = ne01*tensor_split[id + 1];
  8420. if (dev[id].row_high < ne01) {
  8421. dev[id].row_high -= dev[id].row_high % rounding;
  8422. }
  8423. }
  8424. }
  8425. }
  8426. for (int id = 0; id < g_device_count; ++id) {
  8427. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  8428. continue;
  8429. }
  8430. used_devices++;
  8431. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8432. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8433. ggml_cuda_set_device(id);
  8434. cudaStream_t stream = g_cudaStreams[id][0];
  8435. if (src0_on_device && src0_is_contiguous) {
  8436. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  8437. } else {
  8438. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  8439. }
  8440. if (src1_on_device && src1_is_contiguous) {
  8441. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  8442. } else {
  8443. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  8444. }
  8445. if (convert_src1_to_q8_1) {
  8446. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  8447. if (src1_on_device && src1_is_contiguous) {
  8448. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  8449. CUDA_CHECK(cudaGetLastError());
  8450. }
  8451. }
  8452. if (dst_on_device) {
  8453. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  8454. } else {
  8455. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  8456. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  8457. }
  8458. }
  8459. // if multiple devices are used they need to wait for the main device
  8460. // here an event is recorded that signals that the main device has finished calculating the input data
  8461. if (split && used_devices > 1) {
  8462. ggml_cuda_set_device(g_main_device);
  8463. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  8464. }
  8465. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  8466. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  8467. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  8468. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  8469. for (int id = 0; id < g_device_count; ++id) {
  8470. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  8471. continue;
  8472. }
  8473. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8474. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8475. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  8476. ggml_cuda_set_device(id);
  8477. cudaStream_t stream = g_cudaStreams[id][is];
  8478. // wait for main GPU data if necessary
  8479. if (split && (id != g_main_device || is != 0)) {
  8480. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  8481. }
  8482. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  8483. const int64_t i03 = i0 / ne12;
  8484. const int64_t i02 = i0 % ne12;
  8485. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  8486. // for split tensors the data begins at i0 == i0_offset_low
  8487. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  8488. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  8489. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  8490. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  8491. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  8492. // in that case an offset on dst_ddf_i is needed
  8493. if (dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device) {
  8494. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  8495. }
  8496. // copy src0, src1 to device if necessary
  8497. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  8498. if (id != g_main_device) {
  8499. if (convert_src1_to_q8_1) {
  8500. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  8501. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  8502. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  8503. } else {
  8504. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  8505. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  8506. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  8507. src1_ncols*ne10*sizeof(float), stream));
  8508. }
  8509. }
  8510. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  8511. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  8512. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  8513. } else {
  8514. GGML_ASSERT(false);
  8515. }
  8516. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  8517. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  8518. CUDA_CHECK(cudaGetLastError());
  8519. }
  8520. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  8521. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  8522. }
  8523. // do the computation
  8524. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  8525. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  8526. CUDA_CHECK(cudaGetLastError());
  8527. // copy dst to host or other device if necessary
  8528. if (!dst_on_device) {
  8529. void * dst_off_device;
  8530. cudaMemcpyKind kind;
  8531. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8532. dst_off_device = dst->data;
  8533. kind = cudaMemcpyDeviceToHost;
  8534. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  8535. dst_off_device = dst_extra->data_device[g_main_device];
  8536. kind = cudaMemcpyDeviceToDevice;
  8537. } else {
  8538. GGML_ASSERT(false);
  8539. }
  8540. if (split) {
  8541. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  8542. // dst is NOT transposed.
  8543. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  8544. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  8545. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  8546. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  8547. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  8548. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  8549. #if !defined(GGML_USE_HIPBLAS)
  8550. if (kind == cudaMemcpyDeviceToDevice) {
  8551. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  8552. cudaMemcpy3DPeerParms p = {};
  8553. p.dstDevice = g_main_device;
  8554. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  8555. p.srcDevice = id;
  8556. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  8557. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  8558. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  8559. } else
  8560. #endif
  8561. {
  8562. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  8563. dst_dd_i, row_diff*sizeof(float),
  8564. row_diff*sizeof(float), src1_ncols,
  8565. kind, stream));
  8566. }
  8567. } else {
  8568. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  8569. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  8570. dhf_dst_i += src1_col_0*ne0;
  8571. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  8572. }
  8573. }
  8574. // add event for the main device to wait on until other device is done
  8575. if (split && (id != g_main_device || is != 0)) {
  8576. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  8577. }
  8578. }
  8579. }
  8580. }
  8581. // main device waits for all other devices to be finished
  8582. if (split && g_device_count > 1) {
  8583. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  8584. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  8585. ggml_cuda_set_device(g_main_device);
  8586. for (int id = 0; id < g_device_count; ++id) {
  8587. if (dev[id].row_low == dev[id].row_high) {
  8588. continue;
  8589. }
  8590. for (int64_t is = 0; is < is_max; ++is) {
  8591. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  8592. }
  8593. }
  8594. }
  8595. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8596. ggml_cuda_set_device(g_main_device);
  8597. CUDA_CHECK(cudaDeviceSynchronize());
  8598. }
  8599. }
  8600. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8601. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  8602. }
  8603. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8604. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  8605. }
  8606. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8607. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  8608. }
  8609. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8610. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  8611. }
  8612. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8613. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  8614. }
  8615. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8616. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  8617. }
  8618. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8619. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  8620. }
  8621. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8622. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  8623. }
  8624. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8625. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  8626. }
  8627. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8628. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  8629. }
  8630. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8631. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  8632. }
  8633. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8634. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  8635. }
  8636. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8637. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  8638. }
  8639. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8640. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  8641. }
  8642. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8643. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  8644. }
  8645. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8646. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  8647. }
  8648. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8649. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  8650. }
  8651. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8652. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  8653. }
  8654. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8655. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  8656. }
  8657. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8658. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  8659. }
  8660. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8661. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  8662. }
  8663. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  8664. if (!g_cublas_loaded) return false;
  8665. const int64_t ne10 = src1->ne[0];
  8666. const int64_t ne0 = dst->ne[0];
  8667. const int64_t ne1 = dst->ne[1];
  8668. // TODO: find the optimal values for these
  8669. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  8670. src1->type == GGML_TYPE_F32 &&
  8671. dst->type == GGML_TYPE_F32 &&
  8672. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  8673. }
  8674. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8675. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  8676. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8677. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  8678. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  8679. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8680. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8681. const int64_t ne00 = src0->ne[0];
  8682. const int64_t ne01 = src0->ne[1];
  8683. const int64_t ne02 = src0->ne[2];
  8684. const int64_t ne12 = src1->ne[2];
  8685. ggml_cuda_set_device(g_main_device);
  8686. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8687. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8688. void * src0_ddq = src0_extra->data_device[g_main_device];
  8689. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8690. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8691. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8692. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8693. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8694. }
  8695. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8696. GGML_ASSERT(!ggml_is_transposed(src0));
  8697. GGML_ASSERT(!ggml_is_transposed(src1));
  8698. GGML_ASSERT(!ggml_is_permuted(src0));
  8699. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8700. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8701. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8702. const int64_t ne00 = src0->ne[0];
  8703. const int64_t ne01 = src0->ne[1];
  8704. const int64_t ne02 = src0->ne[2];
  8705. const int64_t nb01 = src0->nb[1];
  8706. const int64_t nb02 = src0->nb[2];
  8707. const int64_t ne12 = src1->ne[2];
  8708. ggml_cuda_set_device(g_main_device);
  8709. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8710. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8711. void * src0_ddq = src0_extra->data_device[g_main_device];
  8712. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8713. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8714. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8715. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8716. const int64_t row_stride_x = nb01 / sizeof(half);
  8717. const int64_t channel_stride_x = nb02 / sizeof(half);
  8718. 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);
  8719. }
  8720. static __global__ void k_compute_batched_ptrs(
  8721. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8722. const void ** ptrs_src, void ** ptrs_dst,
  8723. int64_t ne12, int64_t ne13,
  8724. int64_t ne23,
  8725. size_t nb02, size_t nb03,
  8726. size_t nb12, size_t nb13,
  8727. size_t nbd2, size_t nbd3,
  8728. int64_t r2, int64_t r3) {
  8729. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8730. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8731. if (i13 >= ne13 || i12 >= ne12) {
  8732. return;
  8733. }
  8734. int64_t i03 = i13 / r3;
  8735. int64_t i02 = i12 / r2;
  8736. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8737. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8738. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8739. }
  8740. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8741. GGML_ASSERT(!ggml_is_transposed(src0));
  8742. GGML_ASSERT(!ggml_is_transposed(src1));
  8743. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8744. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8745. GGML_TENSOR_BINARY_OP_LOCALS
  8746. const int64_t ne_dst = ggml_nelements(dst);
  8747. ggml_cuda_set_device(g_main_device);
  8748. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8749. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8750. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8751. void * src0_ddq = src0_extra->data_device[g_main_device];
  8752. half * src0_f16 = (half *) src0_ddq;
  8753. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8754. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8755. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8756. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8757. // convert src1 to fp16
  8758. cuda_pool_alloc<half> src1_f16_alloc;
  8759. if (src1->type != GGML_TYPE_F16) {
  8760. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8761. const int64_t ne_src1 = ggml_nelements(src1);
  8762. src1_f16_alloc.alloc(ne_src1);
  8763. GGML_ASSERT(to_fp16_cuda != nullptr);
  8764. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8765. }
  8766. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8767. cuda_pool_alloc<half> dst_f16;
  8768. char * dst_t;
  8769. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8770. cudaDataType_t cu_data_type = CUDA_R_16F;
  8771. // dst strides
  8772. size_t nbd2 = dst->nb[2];
  8773. size_t nbd3 = dst->nb[3];
  8774. const half alpha_f16 = 1.0f;
  8775. const half beta_f16 = 0.0f;
  8776. const float alpha_f32 = 1.0f;
  8777. const float beta_f32 = 0.0f;
  8778. const void * alpha = &alpha_f16;
  8779. const void * beta = &beta_f16;
  8780. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8781. dst_t = (char *) dst_f16.alloc(ne_dst);
  8782. nbd2 /= sizeof(float) / sizeof(half);
  8783. nbd3 /= sizeof(float) / sizeof(half);
  8784. } else {
  8785. dst_t = (char *) dst_ddf;
  8786. cu_compute_type = CUBLAS_COMPUTE_32F;
  8787. cu_data_type = CUDA_R_32F;
  8788. alpha = &alpha_f32;
  8789. beta = &beta_f32;
  8790. }
  8791. GGML_ASSERT(ne12 % ne02 == 0);
  8792. GGML_ASSERT(ne13 % ne03 == 0);
  8793. // broadcast factors
  8794. const int64_t r2 = ne12/ne02;
  8795. const int64_t r3 = ne13/ne03;
  8796. #if 0
  8797. // use cublasGemmEx
  8798. {
  8799. for (int i13 = 0; i13 < ne13; ++i13) {
  8800. for (int i12 = 0; i12 < ne12; ++i12) {
  8801. int i03 = i13 / r3;
  8802. int i02 = i12 / r2;
  8803. CUBLAS_CHECK(
  8804. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8805. ne01, ne11, ne10,
  8806. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8807. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8808. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8809. cu_compute_type,
  8810. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8811. }
  8812. }
  8813. }
  8814. #else
  8815. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8816. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8817. // use cublasGemmStridedBatchedEx
  8818. CUBLAS_CHECK(
  8819. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8820. ne01, ne11, ne10,
  8821. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8822. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8823. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8824. ne12*ne13,
  8825. cu_compute_type,
  8826. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8827. } else {
  8828. // use cublasGemmBatchedEx
  8829. const int ne23 = ne12*ne13;
  8830. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8831. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8832. dim3 block_dims(ne13, ne12);
  8833. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8834. src0_f16, src1_f16, dst_t,
  8835. ptrs_src.get(), ptrs_dst.get(),
  8836. ne12, ne13,
  8837. ne23,
  8838. nb02, nb03,
  8839. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8840. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8841. nbd2, nbd3,
  8842. r2, r3);
  8843. CUDA_CHECK(cudaGetLastError());
  8844. CUBLAS_CHECK(
  8845. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8846. ne01, ne11, ne10,
  8847. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8848. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8849. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8850. ne23,
  8851. cu_compute_type,
  8852. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8853. }
  8854. #endif
  8855. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8856. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8857. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8858. }
  8859. }
  8860. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8861. const bool all_on_device =
  8862. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  8863. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  8864. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  8865. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8866. int64_t min_compute_capability = INT_MAX;
  8867. bool any_pascal_with_slow_fp16 = false;
  8868. if (split) {
  8869. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8870. auto & tensor_split = buft_ctx->tensor_split;
  8871. for (int id = 0; id < g_device_count; ++id) {
  8872. // skip devices that are not going to do any work:
  8873. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8874. continue;
  8875. }
  8876. if (min_compute_capability > g_device_caps[id].cc) {
  8877. min_compute_capability = g_device_caps[id].cc;
  8878. }
  8879. if (g_device_caps[id].cc == 610) {
  8880. any_pascal_with_slow_fp16 = true;
  8881. }
  8882. }
  8883. } else {
  8884. min_compute_capability = g_device_caps[g_main_device].cc;
  8885. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8886. }
  8887. // check data types and tensor shapes for custom matrix multiplication kernels:
  8888. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8889. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8890. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8891. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8892. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8893. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8894. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8895. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8896. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8897. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8898. #ifdef CUDA_USE_TENSOR_CORES
  8899. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8900. #endif // CUDA_USE_TENSOR_CORES
  8901. #else
  8902. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8903. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8904. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8905. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8906. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8907. #ifdef CUDA_USE_TENSOR_CORES
  8908. // when tensor cores are available, use them for large batch size
  8909. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8910. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8911. #endif // CUDA_USE_TENSOR_CORES
  8912. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8913. // if mmvq is available it's a better choice than dmmv:
  8914. #ifndef GGML_CUDA_FORCE_DMMV
  8915. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8916. #endif // GGML_CUDA_FORCE_DMMV
  8917. // debug helpers
  8918. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8919. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8920. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8921. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8922. //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);
  8923. //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);
  8924. 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) {
  8925. // KQ single-batch
  8926. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8927. } 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) {
  8928. // KQV single-batch
  8929. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8930. } 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) {
  8931. // KQ + KQV multi-batch
  8932. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8933. } else if (use_dequantize_mul_mat_vec) {
  8934. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8935. } else if (use_mul_mat_vec_q) {
  8936. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8937. } else if (use_mul_mat_q) {
  8938. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8939. } else {
  8940. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8941. }
  8942. }
  8943. #if 0
  8944. template<typename ... Srcs>
  8945. static __global__ void k_compute_batched_ptrs_id(
  8946. const void ** ptrs_src, void ** ptrs_dst,
  8947. int ne12, int ne13,
  8948. int ne23,
  8949. int nb02, int nb03,
  8950. int nb12, int nb13,
  8951. int nb2, int nb3,
  8952. int r2, int r3,
  8953. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8954. const half * src1_f16, half * dst_f16,
  8955. const int32_t * ids, const int id,
  8956. Srcs... src0s) {
  8957. int i = ids[id];
  8958. half * src0_f16;
  8959. const void * srcs_ar[] = { (const half *) src0s... };
  8960. if (src0_type == GGML_TYPE_F16) {
  8961. src0_f16 = (half *) srcs_ar[i];
  8962. } else {
  8963. src0_f16 = src0_as_f16;
  8964. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8965. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8966. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8967. }
  8968. }
  8969. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8970. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8971. if (i13 >= ne13 || i12 >= ne12) {
  8972. return;
  8973. }
  8974. int i03 = i13 / r3;
  8975. int i02 = i12 / r2;
  8976. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8977. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8978. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8979. }
  8980. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8981. const struct ggml_tensor * ids = dst->src[0];
  8982. const struct ggml_tensor * src1 = dst->src[1];
  8983. const struct ggml_tensor * src00 = dst->src[2];
  8984. const int id = dst->op_params[0];
  8985. GGML_ASSERT(!ggml_is_transposed(src00));
  8986. GGML_ASSERT(!ggml_is_transposed(src1));
  8987. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8988. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8989. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8990. const int64_t ne01 = src00->ne[1];
  8991. const int64_t ne02 = src00->ne[2];
  8992. const int64_t ne03 = src00->ne[3];
  8993. //const int64_t nb01 = src00->nb[1];
  8994. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8995. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8996. const int64_t ne10 = src1->ne[0];
  8997. const int64_t ne11 = src1->ne[1];
  8998. const int64_t ne12 = src1->ne[2];
  8999. const int64_t ne13 = src1->ne[3];
  9000. //const int64_t nb11 = src1->nb[1];
  9001. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  9002. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  9003. const int64_t ne1 = ggml_nelements(src1);
  9004. const int64_t ne = ggml_nelements(dst);
  9005. ggml_cuda_set_device(g_main_device);
  9006. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  9007. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  9008. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  9009. //void * src0_ddq = src0_extra->data_device[g_main_device];
  9010. //half * src0_as_f16 = (half *) src0_ddq;
  9011. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  9012. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  9013. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  9014. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  9015. // convert src1 to fp16
  9016. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  9017. GGML_ASSERT(to_fp16_cuda != nullptr);
  9018. size_t src1_as = 0;
  9019. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  9020. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  9021. size_t dst_as = 0;
  9022. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  9023. GGML_ASSERT(ne12 % ne02 == 0);
  9024. GGML_ASSERT(ne13 % ne03 == 0);
  9025. // broadcast factors
  9026. const int64_t r2 = ne12/ne02;
  9027. const int64_t r3 = ne13/ne03;
  9028. const half alpha_f16 = 1.0f;
  9029. const half beta_f16 = 0.0f;
  9030. // use cublasGemmBatchedEx
  9031. const int ne23 = ne12*ne13;
  9032. const void ** ptrs_src = nullptr;
  9033. void ** ptrs_dst = nullptr;
  9034. size_t ptrs_src_s = 0;
  9035. size_t ptrs_dst_s = 0;
  9036. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  9037. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  9038. int64_t src0_ne = ggml_nelements(src00);
  9039. half * src0_as_f16 = nullptr;
  9040. size_t src0_as = 0;
  9041. if (src00->type != GGML_TYPE_F16) {
  9042. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  9043. }
  9044. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  9045. dim3 block_dims(ne13, ne12);
  9046. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  9047. ptrs_src, ptrs_dst,
  9048. ne12, ne13,
  9049. ne23,
  9050. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  9051. nb12, nb13,
  9052. dst->nb[2], dst->nb[3],
  9053. r2, r3,
  9054. src00->type, src0_as_f16, src0_ne,
  9055. src1_as_f16, dst_f16,
  9056. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  9057. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  9058. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  9059. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  9060. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  9061. );
  9062. CUDA_CHECK(cudaGetLastError());
  9063. CUBLAS_CHECK(
  9064. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  9065. ne01, ne11, ne10,
  9066. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  9067. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  9068. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  9069. ne23,
  9070. CUBLAS_COMPUTE_16F,
  9071. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  9072. if (src0_as != 0) {
  9073. ggml_cuda_pool_free(src0_as_f16, src0_as);
  9074. }
  9075. if (ptrs_src_s != 0) {
  9076. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  9077. }
  9078. if (ptrs_dst_s != 0) {
  9079. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  9080. }
  9081. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  9082. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  9083. ggml_cuda_pool_free(src1_as_f16, src1_as);
  9084. ggml_cuda_pool_free(dst_f16, dst_as);
  9085. }
  9086. #endif
  9087. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9088. #if 0
  9089. ggml_cuda_mul_mat_id_cublas(dst);
  9090. // TODO: mmq/mmv support
  9091. #endif
  9092. const size_t nb11 = src1->nb[1];
  9093. const size_t nb1 = dst->nb[1];
  9094. const struct ggml_tensor * ids = src0;
  9095. const int32_t id = ((int32_t *) dst->op_params)[0];
  9096. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  9097. std::vector<char> ids_host(ggml_nbytes(ids));
  9098. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  9099. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  9100. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  9101. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  9102. CUDA_CHECK(cudaStreamSynchronize(stream));
  9103. } else {
  9104. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  9105. }
  9106. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  9107. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  9108. ggml_tensor_extra_gpu src1_row_extra;
  9109. ggml_tensor_extra_gpu dst_row_extra;
  9110. ggml_tensor src1_row = *src1;
  9111. ggml_tensor dst_row = *dst;
  9112. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  9113. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  9114. src1_row.extra = &src1_row_extra;
  9115. dst_row.extra = &dst_row_extra;
  9116. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  9117. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  9118. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  9119. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  9120. if (src1->ne[1] == 1) {
  9121. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  9122. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  9123. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  9124. //int32_t row_id;
  9125. //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]));
  9126. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  9127. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  9128. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  9129. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  9130. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  9131. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  9132. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  9133. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  9134. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  9135. }
  9136. } else {
  9137. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  9138. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  9139. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  9140. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  9141. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_TYPE_CPU ?
  9142. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  9143. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_TYPE_CPU ?
  9144. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  9145. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  9146. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  9147. int64_t num_src1_rows = 0;
  9148. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  9149. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  9150. if (row_id_i != row_id) {
  9151. continue;
  9152. }
  9153. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  9154. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  9155. nb11, src1_kind, stream));
  9156. num_src1_rows++;
  9157. }
  9158. if (num_src1_rows == 0) {
  9159. continue;
  9160. }
  9161. src1_row.ne[1] = num_src1_rows;
  9162. dst_row.ne[1] = num_src1_rows;
  9163. src1_row.nb[1] = nb11;
  9164. src1_row.nb[2] = num_src1_rows*nb11;
  9165. src1_row.nb[3] = num_src1_rows*nb11;
  9166. dst_row.nb[1] = nb1;
  9167. dst_row.nb[2] = num_src1_rows*nb1;
  9168. dst_row.nb[3] = num_src1_rows*nb1;
  9169. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  9170. num_src1_rows = 0;
  9171. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  9172. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  9173. if (row_id_i != row_id) {
  9174. continue;
  9175. }
  9176. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  9177. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  9178. nb1, dst_kind, stream));
  9179. num_src1_rows++;
  9180. }
  9181. }
  9182. }
  9183. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  9184. CUDA_CHECK(cudaStreamSynchronize(stream));
  9185. }
  9186. }
  9187. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9188. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  9189. }
  9190. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9191. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  9192. }
  9193. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9194. const int64_t ne = ggml_nelements(src0);
  9195. GGML_ASSERT(ne == ggml_nelements(src1));
  9196. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  9197. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  9198. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  9199. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  9200. const int64_t ne00 = src0->ne[0];
  9201. const int64_t ne01 = src0->ne[1];
  9202. const int64_t ne02 = src0->ne[2];
  9203. //GGML_ASSERT(src0->ne[3] == 1);
  9204. const int64_t nb00 = src0->nb[0];
  9205. const int64_t nb01 = src0->nb[1];
  9206. const int64_t nb02 = src0->nb[2];
  9207. const int64_t nb03 = src0->nb[3];
  9208. const int64_t ne10 = src1->ne[0];
  9209. const int64_t ne11 = src1->ne[1];
  9210. const int64_t ne12 = src1->ne[2];
  9211. //GGML_ASSERT(src1->ne[3] == 1);
  9212. const int64_t nb10 = src1->nb[0];
  9213. const int64_t nb11 = src1->nb[1];
  9214. const int64_t nb12 = src1->nb[2];
  9215. const int64_t nb13 = src1->nb[3];
  9216. ggml_cuda_set_device(g_main_device);
  9217. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  9218. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  9219. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  9220. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  9221. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  9222. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  9223. 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);
  9224. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  9225. 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);
  9226. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  9227. 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);
  9228. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  9229. 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);
  9230. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  9231. 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);
  9232. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  9233. 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);
  9234. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  9235. 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);
  9236. } else {
  9237. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  9238. ggml_type_name(src0->type), ggml_type_name(src1->type));
  9239. GGML_ASSERT(false);
  9240. }
  9241. (void) dst;
  9242. }
  9243. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9244. // TODO: why do we pass dst as src1 here?
  9245. ggml_cuda_cpy(src0, dst, nullptr);
  9246. (void) src1;
  9247. }
  9248. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9249. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  9250. }
  9251. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9252. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  9253. }
  9254. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9255. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  9256. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  9257. }
  9258. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9259. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  9260. }
  9261. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9262. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  9263. }
  9264. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9265. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  9266. }
  9267. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9268. GGML_ASSERT(ggml_is_contiguous(src0));
  9269. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  9270. }
  9271. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9272. GGML_ASSERT(ggml_is_contiguous(src0));
  9273. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  9274. }
  9275. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9276. (void) src0;
  9277. (void) src1;
  9278. (void) dst;
  9279. }
  9280. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  9281. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  9282. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  9283. }
  9284. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  9285. if (main_device >= g_device_count) {
  9286. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  9287. main_device, g_device_count, g_main_device);
  9288. return;
  9289. }
  9290. if (g_main_device != main_device && g_device_count > 1) {
  9291. g_main_device = main_device;
  9292. //cudaDeviceProp prop;
  9293. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  9294. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  9295. }
  9296. }
  9297. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  9298. if (!g_cublas_loaded) return false;
  9299. ggml_cuda_func_t func;
  9300. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  9301. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  9302. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  9303. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  9304. return false;
  9305. }
  9306. if (tensor->op == GGML_OP_MUL_MAT) {
  9307. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  9308. #ifndef NDEBUG
  9309. 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]);
  9310. #endif
  9311. return false;
  9312. }
  9313. }
  9314. switch (tensor->op) {
  9315. case GGML_OP_REPEAT:
  9316. func = ggml_cuda_repeat;
  9317. break;
  9318. case GGML_OP_GET_ROWS:
  9319. func = ggml_cuda_get_rows;
  9320. break;
  9321. case GGML_OP_DUP:
  9322. func = ggml_cuda_dup;
  9323. break;
  9324. case GGML_OP_ADD:
  9325. func = ggml_cuda_add;
  9326. break;
  9327. case GGML_OP_ACC:
  9328. func = ggml_cuda_acc;
  9329. break;
  9330. case GGML_OP_MUL:
  9331. func = ggml_cuda_mul;
  9332. break;
  9333. case GGML_OP_DIV:
  9334. func = ggml_cuda_div;
  9335. break;
  9336. case GGML_OP_UNARY:
  9337. switch (ggml_get_unary_op(tensor)) {
  9338. case GGML_UNARY_OP_GELU:
  9339. func = ggml_cuda_gelu;
  9340. break;
  9341. case GGML_UNARY_OP_SILU:
  9342. func = ggml_cuda_silu;
  9343. break;
  9344. case GGML_UNARY_OP_GELU_QUICK:
  9345. func = ggml_cuda_gelu_quick;
  9346. break;
  9347. case GGML_UNARY_OP_TANH:
  9348. func = ggml_cuda_tanh;
  9349. break;
  9350. case GGML_UNARY_OP_RELU:
  9351. func = ggml_cuda_relu;
  9352. break;
  9353. case GGML_UNARY_OP_HARDSIGMOID:
  9354. func = ggml_cuda_hardsigmoid;
  9355. break;
  9356. case GGML_UNARY_OP_HARDSWISH:
  9357. func = ggml_cuda_hardswish;
  9358. break;
  9359. default:
  9360. return false;
  9361. }
  9362. break;
  9363. case GGML_OP_NORM:
  9364. func = ggml_cuda_norm;
  9365. break;
  9366. case GGML_OP_GROUP_NORM:
  9367. func = ggml_cuda_group_norm;
  9368. break;
  9369. case GGML_OP_CONCAT:
  9370. func = ggml_cuda_concat;
  9371. break;
  9372. case GGML_OP_UPSCALE:
  9373. func = ggml_cuda_upscale;
  9374. break;
  9375. case GGML_OP_PAD:
  9376. func = ggml_cuda_pad;
  9377. break;
  9378. case GGML_OP_LEAKY_RELU:
  9379. func = ggml_cuda_leaky_relu;
  9380. break;
  9381. case GGML_OP_RMS_NORM:
  9382. func = ggml_cuda_rms_norm;
  9383. break;
  9384. case GGML_OP_MUL_MAT:
  9385. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  9386. return false;
  9387. }
  9388. func = ggml_cuda_mul_mat;
  9389. break;
  9390. case GGML_OP_MUL_MAT_ID:
  9391. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  9392. return false;
  9393. }
  9394. func = ggml_cuda_mul_mat_id;
  9395. break;
  9396. case GGML_OP_SCALE:
  9397. func = ggml_cuda_scale;
  9398. break;
  9399. case GGML_OP_SQR:
  9400. func = ggml_cuda_sqr;
  9401. break;
  9402. case GGML_OP_CLAMP:
  9403. func = ggml_cuda_clamp;
  9404. break;
  9405. case GGML_OP_CPY:
  9406. func = ggml_cuda_cpy;
  9407. break;
  9408. case GGML_OP_CONT:
  9409. func = ggml_cuda_dup;
  9410. break;
  9411. case GGML_OP_NONE:
  9412. case GGML_OP_RESHAPE:
  9413. case GGML_OP_VIEW:
  9414. case GGML_OP_PERMUTE:
  9415. case GGML_OP_TRANSPOSE:
  9416. func = ggml_cuda_nop;
  9417. break;
  9418. case GGML_OP_DIAG_MASK_INF:
  9419. func = ggml_cuda_diag_mask_inf;
  9420. break;
  9421. case GGML_OP_SOFT_MAX:
  9422. func = ggml_cuda_soft_max;
  9423. break;
  9424. case GGML_OP_ROPE:
  9425. func = ggml_cuda_rope;
  9426. break;
  9427. case GGML_OP_ALIBI:
  9428. func = ggml_cuda_alibi;
  9429. break;
  9430. case GGML_OP_IM2COL:
  9431. func = ggml_cuda_im2col;
  9432. break;
  9433. case GGML_OP_POOL_2D:
  9434. func = ggml_cuda_pool2d;
  9435. break;
  9436. case GGML_OP_SUM_ROWS:
  9437. func = ggml_cuda_sum_rows;
  9438. break;
  9439. case GGML_OP_ARGSORT:
  9440. func = ggml_cuda_argsort;
  9441. break;
  9442. default:
  9443. return false;
  9444. }
  9445. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  9446. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  9447. }
  9448. if (params->ith != 0) {
  9449. return true;
  9450. }
  9451. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  9452. return true;
  9453. }
  9454. func(tensor->src[0], tensor->src[1], tensor);
  9455. return true;
  9456. }
  9457. GGML_CALL int ggml_cuda_get_device_count() {
  9458. int device_count;
  9459. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  9460. return 0;
  9461. }
  9462. return device_count;
  9463. }
  9464. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  9465. cudaDeviceProp prop;
  9466. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  9467. snprintf(description, description_size, "%s", prop.name);
  9468. }
  9469. ////////////////////////////////////////////////////////////////////////////////
  9470. // backend interface
  9471. #define UNUSED GGML_UNUSED
  9472. struct ggml_backend_cuda_context {
  9473. int device;
  9474. std::string name;
  9475. };
  9476. // cuda buffer
  9477. struct ggml_backend_cuda_buffer_context {
  9478. int device;
  9479. void * dev_ptr = nullptr;
  9480. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  9481. size_t temp_tensor_extra_index = 0;
  9482. std::string name;
  9483. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  9484. device(device), dev_ptr(dev_ptr),
  9485. name(GGML_CUDA_NAME + std::to_string(device)) {
  9486. }
  9487. ~ggml_backend_cuda_buffer_context() {
  9488. delete[] temp_tensor_extras;
  9489. }
  9490. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  9491. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  9492. if (temp_tensor_extras == nullptr) {
  9493. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  9494. }
  9495. size_t alloc_index = temp_tensor_extra_index;
  9496. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  9497. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  9498. memset(extra, 0, sizeof(*extra));
  9499. return extra;
  9500. }
  9501. };
  9502. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  9503. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9504. return ctx->name.c_str();
  9505. }
  9506. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  9507. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  9508. }
  9509. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9510. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9511. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  9512. delete ctx;
  9513. }
  9514. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  9515. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9516. return ctx->dev_ptr;
  9517. }
  9518. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9519. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9520. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  9521. assert(tensor->view_src->buffer->buft == buffer->buft);
  9522. tensor->backend = tensor->view_src->backend;
  9523. tensor->extra = tensor->view_src->extra;
  9524. return;
  9525. }
  9526. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  9527. extra->data_device[ctx->device] = tensor->data;
  9528. tensor->backend = GGML_BACKEND_TYPE_GPU;
  9529. tensor->extra = extra;
  9530. if (ggml_is_quantized(tensor->type)) {
  9531. // initialize padding to 0 to avoid possible NaN values
  9532. size_t original_size = ggml_nbytes(tensor);
  9533. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  9534. if (padded_size > original_size && tensor->view_src == nullptr) {
  9535. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  9536. }
  9537. }
  9538. }
  9539. 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) {
  9540. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9541. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9542. ggml_cuda_set_device(ctx->device);
  9543. CUDA_CHECK(cudaDeviceSynchronize());
  9544. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  9545. CUDA_CHECK(cudaDeviceSynchronize());
  9546. }
  9547. 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) {
  9548. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9549. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9550. ggml_cuda_set_device(ctx->device);
  9551. CUDA_CHECK(cudaDeviceSynchronize());
  9552. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  9553. CUDA_CHECK(cudaDeviceSynchronize());
  9554. }
  9555. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  9556. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  9557. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  9558. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9559. ggml_cuda_set_device(src_ctx->device);
  9560. CUDA_CHECK(cudaDeviceSynchronize());
  9561. ggml_cuda_set_device(dst_ctx->device);
  9562. CUDA_CHECK(cudaDeviceSynchronize());
  9563. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  9564. CUDA_CHECK(cudaDeviceSynchronize());
  9565. return true;
  9566. }
  9567. return false;
  9568. }
  9569. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9570. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9571. ggml_cuda_set_device(ctx->device);
  9572. CUDA_CHECK(cudaDeviceSynchronize());
  9573. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  9574. CUDA_CHECK(cudaDeviceSynchronize());
  9575. }
  9576. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  9577. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  9578. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  9579. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  9580. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  9581. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  9582. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  9583. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  9584. /* .clear = */ ggml_backend_cuda_buffer_clear,
  9585. /* .reset = */ NULL,
  9586. };
  9587. // cuda buffer type
  9588. struct ggml_backend_cuda_buffer_type_context {
  9589. int device;
  9590. std::string name;
  9591. };
  9592. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9593. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9594. return ctx->name.c_str();
  9595. }
  9596. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9597. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9598. ggml_cuda_set_device(buft_ctx->device);
  9599. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  9600. void * dev_ptr;
  9601. cudaError_t err = cudaMalloc(&dev_ptr, size);
  9602. if (err != cudaSuccess) {
  9603. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  9604. return nullptr;
  9605. }
  9606. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  9607. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  9608. }
  9609. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9610. return 128;
  9611. UNUSED(buft);
  9612. }
  9613. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9614. size_t size = ggml_nbytes(tensor);
  9615. int64_t ne0 = tensor->ne[0];
  9616. if (ggml_is_quantized(tensor->type)) {
  9617. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9618. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9619. }
  9620. }
  9621. return size;
  9622. UNUSED(buft);
  9623. }
  9624. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9625. if (!ggml_backend_is_cuda(backend)) {
  9626. return false;
  9627. }
  9628. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9629. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9630. return buft_ctx->device == cuda_ctx->device;
  9631. }
  9632. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  9633. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  9634. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  9635. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  9636. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9637. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  9638. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  9639. /* .is_host = */ NULL,
  9640. };
  9641. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  9642. // FIXME: this is not thread safe
  9643. if (device >= ggml_backend_cuda_get_device_count()) {
  9644. return nullptr;
  9645. }
  9646. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  9647. static bool ggml_backend_cuda_buffer_type_initialized = false;
  9648. if (!ggml_backend_cuda_buffer_type_initialized) {
  9649. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  9650. ggml_backend_cuda_buffer_types[i] = {
  9651. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  9652. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  9653. };
  9654. }
  9655. ggml_backend_cuda_buffer_type_initialized = true;
  9656. }
  9657. return &ggml_backend_cuda_buffer_types[device];
  9658. }
  9659. // cuda split buffer
  9660. struct ggml_backend_cuda_split_buffer_context {
  9661. ~ggml_backend_cuda_split_buffer_context() {
  9662. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  9663. for (int id = 0; id < g_device_count; ++id) {
  9664. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9665. if (extra->events[id][is] != nullptr) {
  9666. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  9667. }
  9668. }
  9669. if (extra->data_device[id] != nullptr) {
  9670. CUDA_CHECK(cudaFree(extra->data_device[id]));
  9671. }
  9672. }
  9673. delete extra;
  9674. }
  9675. }
  9676. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  9677. };
  9678. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  9679. return GGML_CUDA_NAME "_Split";
  9680. UNUSED(buffer);
  9681. }
  9682. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  9683. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  9684. UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  9685. }
  9686. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9687. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9688. delete ctx;
  9689. }
  9690. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  9691. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  9692. return (void *)0x1000;
  9693. UNUSED(buffer);
  9694. }
  9695. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9696. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  9697. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9698. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9699. const int64_t ne0 = tensor->ne[0];
  9700. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  9701. ctx->tensor_extras.push_back(extra);
  9702. for (int id = 0; id < g_device_count; ++id) {
  9703. int64_t row_low, row_high;
  9704. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9705. int64_t nrows_split = row_high - row_low;
  9706. if (nrows_split == 0) {
  9707. continue;
  9708. }
  9709. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9710. const size_t original_size = size;
  9711. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9712. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9713. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9714. }
  9715. // FIXME: do not crash if cudaMalloc fails
  9716. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  9717. ggml_cuda_set_device(id);
  9718. char * buf;
  9719. CUDA_CHECK(cudaMalloc(&buf, size));
  9720. // set padding to 0 to avoid possible NaN values
  9721. if (size > original_size) {
  9722. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  9723. }
  9724. extra->data_device[id] = buf;
  9725. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9726. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  9727. }
  9728. }
  9729. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  9730. tensor->extra = extra;
  9731. }
  9732. 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) {
  9733. // split tensors must always be set in their entirety at once
  9734. GGML_ASSERT(offset == 0);
  9735. GGML_ASSERT(size == ggml_nbytes(tensor));
  9736. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9737. const int64_t ne0 = tensor->ne[0];
  9738. const size_t nb1 = tensor->nb[1];
  9739. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9740. for (int id = 0; id < g_device_count; ++id) {
  9741. int64_t row_low, row_high;
  9742. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9743. int64_t nrows_split = row_high - row_low;
  9744. if (nrows_split == 0) {
  9745. continue;
  9746. }
  9747. const size_t offset_split = row_low*nb1;
  9748. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9749. const size_t original_size = size;
  9750. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9751. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9752. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9753. }
  9754. const char * buf_host = (const char *)data + offset_split;
  9755. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  9756. }
  9757. }
  9758. 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) {
  9759. // split tensors must always be set in their entirety at once
  9760. GGML_ASSERT(offset == 0);
  9761. GGML_ASSERT(size == ggml_nbytes(tensor));
  9762. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9763. const int64_t ne0 = tensor->ne[0];
  9764. const size_t nb1 = tensor->nb[1];
  9765. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9766. for (int id = 0; id < g_device_count; ++id) {
  9767. int64_t row_low, row_high;
  9768. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9769. int64_t nrows_split = row_high - row_low;
  9770. if (nrows_split == 0) {
  9771. continue;
  9772. }
  9773. const size_t offset_split = row_low*nb1;
  9774. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9775. const size_t original_size = size;
  9776. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9777. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9778. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9779. }
  9780. char * buf_host = (char *)data + offset_split;
  9781. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  9782. }
  9783. }
  9784. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9785. UNUSED(buffer);
  9786. UNUSED(value);
  9787. }
  9788. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  9789. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  9790. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  9791. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  9792. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  9793. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  9794. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  9795. /* .cpy_tensor = */ NULL,
  9796. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  9797. /* .reset = */ NULL,
  9798. };
  9799. // cuda split buffer type
  9800. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9801. return GGML_CUDA_NAME "_Split";
  9802. UNUSED(buft);
  9803. }
  9804. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9805. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  9806. // instead, we allocate them for each tensor separately in init_tensor
  9807. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  9808. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  9809. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  9810. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  9811. }
  9812. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9813. return 128;
  9814. UNUSED(buft);
  9815. }
  9816. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9817. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9818. size_t total_size = 0;
  9819. const int64_t ne0 = tensor->ne[0];
  9820. for (int id = 0; id < g_device_count; ++id) {
  9821. int64_t row_low, row_high;
  9822. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9823. int64_t nrows_split = row_high - row_low;
  9824. if (nrows_split == 0) {
  9825. continue;
  9826. }
  9827. total_size += ggml_nbytes_split(tensor, nrows_split);
  9828. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9829. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9830. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9831. }
  9832. }
  9833. return total_size;
  9834. }
  9835. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9836. return ggml_backend_is_cuda(backend);
  9837. UNUSED(buft);
  9838. }
  9839. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9840. return false;
  9841. UNUSED(buft);
  9842. }
  9843. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9844. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9845. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9846. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9847. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9848. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9849. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9850. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9851. };
  9852. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9853. // FIXME: this is not thread safe
  9854. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9855. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9856. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9857. if (all_zero) {
  9858. tensor_split_arr = g_default_tensor_split;
  9859. } else {
  9860. float split_sum = 0.0f;
  9861. for (int i = 0; i < g_device_count; ++i) {
  9862. tensor_split_arr[i] = split_sum;
  9863. split_sum += tensor_split[i];
  9864. }
  9865. for (int i = 0; i < g_device_count; ++i) {
  9866. tensor_split_arr[i] /= split_sum;
  9867. }
  9868. }
  9869. auto it = buft_map.find(tensor_split_arr);
  9870. if (it != buft_map.end()) {
  9871. return &it->second;
  9872. }
  9873. struct ggml_backend_buffer_type buft {
  9874. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9875. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9876. };
  9877. auto result = buft_map.emplace(tensor_split_arr, buft);
  9878. return &result.first->second;
  9879. }
  9880. // host buffer type
  9881. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9882. return GGML_CUDA_NAME "_Host";
  9883. UNUSED(buft);
  9884. }
  9885. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9886. return GGML_CUDA_NAME "_Host";
  9887. UNUSED(buffer);
  9888. }
  9889. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9890. ggml_cuda_host_free(buffer->context);
  9891. }
  9892. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9893. void * ptr = ggml_cuda_host_malloc(size);
  9894. if (ptr == nullptr) {
  9895. // fallback to cpu buffer
  9896. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9897. }
  9898. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9899. buffer->buft = buft;
  9900. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9901. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9902. return buffer;
  9903. }
  9904. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9905. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9906. /* .iface = */ {
  9907. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9908. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9909. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9910. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9911. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9912. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9913. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9914. },
  9915. /* .context = */ nullptr,
  9916. };
  9917. return &ggml_backend_cuda_buffer_type_host;
  9918. }
  9919. // backend
  9920. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9921. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9922. return cuda_ctx->name.c_str();
  9923. }
  9924. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9925. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9926. delete cuda_ctx;
  9927. delete backend;
  9928. }
  9929. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9930. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9931. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9932. }
  9933. 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) {
  9934. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9935. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9936. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9937. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9938. }
  9939. 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) {
  9940. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9941. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9942. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9943. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9944. }
  9945. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9946. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9947. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9948. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9949. return true;
  9950. }
  9951. return false;
  9952. }
  9953. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9954. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9955. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9956. UNUSED(backend);
  9957. }
  9958. GGML_CALL static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9959. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9960. ggml_cuda_set_main_device(cuda_ctx->device);
  9961. ggml_compute_params params = {};
  9962. params.type = GGML_TASK_TYPE_COMPUTE;
  9963. params.ith = 0;
  9964. for (int i = 0; i < cgraph->n_nodes; i++) {
  9965. ggml_tensor * node = cgraph->nodes[i];
  9966. 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) {
  9967. continue;
  9968. }
  9969. #ifndef NDEBUG
  9970. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9971. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9972. assert(node->extra != nullptr);
  9973. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9974. if (node->src[j] != nullptr) {
  9975. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9976. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9977. assert(node->src[j]->extra != nullptr);
  9978. }
  9979. }
  9980. #endif
  9981. bool ok = ggml_cuda_compute_forward(&params, node);
  9982. if (!ok) {
  9983. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9984. }
  9985. GGML_ASSERT(ok);
  9986. }
  9987. return true;
  9988. }
  9989. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9990. switch (op->op) {
  9991. case GGML_OP_UNARY:
  9992. switch (ggml_get_unary_op(op)) {
  9993. case GGML_UNARY_OP_GELU:
  9994. case GGML_UNARY_OP_SILU:
  9995. case GGML_UNARY_OP_RELU:
  9996. case GGML_UNARY_OP_HARDSIGMOID:
  9997. case GGML_UNARY_OP_HARDSWISH:
  9998. case GGML_UNARY_OP_GELU_QUICK:
  9999. case GGML_UNARY_OP_TANH:
  10000. return true;
  10001. default:
  10002. return false;
  10003. }
  10004. break;
  10005. case GGML_OP_MUL_MAT:
  10006. case GGML_OP_MUL_MAT_ID:
  10007. {
  10008. struct ggml_tensor * a;
  10009. struct ggml_tensor * b;
  10010. if (op->op == GGML_OP_MUL_MAT) {
  10011. a = op->src[0];
  10012. b = op->src[1];
  10013. } else {
  10014. a = op->src[2];
  10015. b = op->src[1];
  10016. }
  10017. if (a->ne[3] != b->ne[3]) {
  10018. return false;
  10019. }
  10020. ggml_type a_type = a->type;
  10021. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  10022. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ3_S ||
  10023. a_type == GGML_TYPE_IQ2_S || a_type == GGML_TYPE_IQ4_XS) {
  10024. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  10025. return false;
  10026. }
  10027. }
  10028. return true;
  10029. } break;
  10030. case GGML_OP_GET_ROWS:
  10031. {
  10032. switch (op->src[0]->type) {
  10033. case GGML_TYPE_F16:
  10034. case GGML_TYPE_F32:
  10035. case GGML_TYPE_Q4_0:
  10036. case GGML_TYPE_Q4_1:
  10037. case GGML_TYPE_Q5_0:
  10038. case GGML_TYPE_Q5_1:
  10039. case GGML_TYPE_Q8_0:
  10040. return true;
  10041. default:
  10042. return false;
  10043. }
  10044. } break;
  10045. case GGML_OP_CPY:
  10046. {
  10047. ggml_type src0_type = op->src[0]->type;
  10048. ggml_type src1_type = op->src[1]->type;
  10049. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  10050. return true;
  10051. }
  10052. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  10053. return true;
  10054. }
  10055. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  10056. return true;
  10057. }
  10058. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  10059. return true;
  10060. }
  10061. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  10062. return true;
  10063. }
  10064. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  10065. return true;
  10066. }
  10067. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  10068. return true;
  10069. }
  10070. return false;
  10071. } break;
  10072. case GGML_OP_DUP:
  10073. case GGML_OP_REPEAT:
  10074. case GGML_OP_CONCAT:
  10075. {
  10076. ggml_type src0_type = op->src[0]->type;
  10077. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  10078. } break;
  10079. case GGML_OP_NONE:
  10080. case GGML_OP_RESHAPE:
  10081. case GGML_OP_VIEW:
  10082. case GGML_OP_PERMUTE:
  10083. case GGML_OP_TRANSPOSE:
  10084. case GGML_OP_NORM:
  10085. case GGML_OP_ADD:
  10086. case GGML_OP_MUL:
  10087. case GGML_OP_DIV:
  10088. case GGML_OP_RMS_NORM:
  10089. case GGML_OP_SCALE:
  10090. case GGML_OP_SQR:
  10091. case GGML_OP_CLAMP:
  10092. case GGML_OP_CONT:
  10093. case GGML_OP_DIAG_MASK_INF:
  10094. case GGML_OP_SOFT_MAX:
  10095. case GGML_OP_ROPE:
  10096. case GGML_OP_ALIBI:
  10097. case GGML_OP_IM2COL:
  10098. case GGML_OP_POOL_2D:
  10099. case GGML_OP_SUM_ROWS:
  10100. case GGML_OP_ARGSORT:
  10101. case GGML_OP_ACC:
  10102. case GGML_OP_GROUP_NORM:
  10103. case GGML_OP_UPSCALE:
  10104. case GGML_OP_PAD:
  10105. case GGML_OP_LEAKY_RELU:
  10106. return true;
  10107. default:
  10108. return false;
  10109. }
  10110. UNUSED(backend);
  10111. }
  10112. static ggml_backend_i ggml_backend_cuda_interface = {
  10113. /* .get_name = */ ggml_backend_cuda_name,
  10114. /* .free = */ ggml_backend_cuda_free,
  10115. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  10116. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  10117. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  10118. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  10119. /* .synchronize = */ ggml_backend_cuda_synchronize,
  10120. /* .graph_plan_create = */ NULL,
  10121. /* .graph_plan_free = */ NULL,
  10122. /* .graph_plan_compute = */ NULL,
  10123. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  10124. /* .supports_op = */ ggml_backend_cuda_supports_op,
  10125. };
  10126. static ggml_guid_t ggml_backend_cuda_guid() {
  10127. static ggml_guid guid = { 0x2c, 0xdd, 0xe8, 0x1c, 0x65, 0xb3, 0x65, 0x73, 0x6a, 0x12, 0x88, 0x61, 0x1c, 0xc9, 0xdc, 0x25 };
  10128. return &guid;
  10129. }
  10130. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  10131. ggml_init_cublas(); // TODO: remove from ggml.c
  10132. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  10133. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  10134. return nullptr;
  10135. }
  10136. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  10137. ggml_cuda_set_main_device(device);
  10138. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  10139. /* .device = */ device,
  10140. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  10141. };
  10142. ggml_backend_t cuda_backend = new ggml_backend {
  10143. /* .guid = */ ggml_backend_cuda_guid(),
  10144. /* .interface = */ ggml_backend_cuda_interface,
  10145. /* .context = */ ctx
  10146. };
  10147. return cuda_backend;
  10148. }
  10149. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  10150. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cuda_guid());
  10151. }
  10152. GGML_CALL int ggml_backend_cuda_get_device_count() {
  10153. return ggml_cuda_get_device_count();
  10154. }
  10155. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  10156. ggml_cuda_get_device_description(device, description, description_size);
  10157. }
  10158. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  10159. ggml_cuda_set_device(device);
  10160. CUDA_CHECK(cudaMemGetInfo(free, total));
  10161. }
  10162. // backend registry
  10163. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  10164. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  10165. return cuda_backend;
  10166. UNUSED(params);
  10167. }
  10168. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  10169. GGML_CALL int ggml_backend_cuda_reg_devices() {
  10170. int device_count = ggml_cuda_get_device_count();
  10171. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  10172. for (int i = 0; i < device_count; i++) {
  10173. char name[128];
  10174. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  10175. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  10176. }
  10177. return device_count;
  10178. }