ggml-cuda.cu 485 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236
  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. typedef struct {
  488. half d;
  489. uint8_t qs[QK_K/4];
  490. uint8_t qh[QK_K/32];
  491. uint8_t signs[QK_K/8];
  492. uint8_t scales[QK_K/64];
  493. } block_iq3_s;
  494. static_assert(sizeof(block_iq3_s) == sizeof(ggml_fp16_t) + 27*(QK_K/64), "wrong iq3_s block size/padding");
  495. #define QR1_S 8
  496. #define QI1_S (QK_K / (4*QR1_S))
  497. typedef struct {
  498. half d;
  499. uint8_t qs[QK_K/8];
  500. uint8_t scales[QK_K/16];
  501. } block_iq1_s;
  502. static_assert(sizeof(block_iq1_s) == sizeof(ggml_fp16_t) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  503. #define QK4_NL 32
  504. #define QR4_NL 2
  505. #define QI4_NL (QK4_NL / (4*QR4_NL))
  506. typedef struct {
  507. half d;
  508. uint8_t qs[QK4_NL/2];
  509. } block_iq4_nl;
  510. static_assert(sizeof(block_iq4_nl) == sizeof(ggml_fp16_t) + QK4_NL/2, "wrong iq4_nl block size/padding");
  511. #define WARP_SIZE 32
  512. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  513. #define CUDA_GELU_BLOCK_SIZE 256
  514. #define CUDA_SILU_BLOCK_SIZE 256
  515. #define CUDA_TANH_BLOCK_SIZE 256
  516. #define CUDA_RELU_BLOCK_SIZE 256
  517. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  518. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  519. #define CUDA_SQR_BLOCK_SIZE 256
  520. #define CUDA_CPY_BLOCK_SIZE 32
  521. #define CUDA_SCALE_BLOCK_SIZE 256
  522. #define CUDA_CLAMP_BLOCK_SIZE 256
  523. #define CUDA_ROPE_BLOCK_SIZE 256
  524. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  525. #define CUDA_ALIBI_BLOCK_SIZE 32
  526. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  527. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  528. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  529. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  530. #define CUDA_UPSCALE_BLOCK_SIZE 256
  531. #define CUDA_CONCAT_BLOCK_SIZE 256
  532. #define CUDA_PAD_BLOCK_SIZE 256
  533. #define CUDA_ACC_BLOCK_SIZE 256
  534. #define CUDA_IM2COL_BLOCK_SIZE 256
  535. #define CUDA_POOL2D_BLOCK_SIZE 256
  536. #define CUDA_Q8_0_NE_ALIGN 2048
  537. // dmmv = dequantize_mul_mat_vec
  538. #ifndef GGML_CUDA_DMMV_X
  539. #define GGML_CUDA_DMMV_X 32
  540. #endif
  541. #ifndef GGML_CUDA_MMV_Y
  542. #define GGML_CUDA_MMV_Y 1
  543. #endif
  544. #ifndef K_QUANTS_PER_ITERATION
  545. #define K_QUANTS_PER_ITERATION 2
  546. #else
  547. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  548. #endif
  549. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  550. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  551. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  552. #define MUL_MAT_SRC1_COL_STRIDE 128
  553. #define MAX_STREAMS 8
  554. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  555. struct ggml_tensor_extra_gpu {
  556. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  557. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  558. };
  559. // this is faster on Windows
  560. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  561. static void ggml_cuda_set_device(const int device) {
  562. int current_device;
  563. CUDA_CHECK(cudaGetDevice(&current_device));
  564. if (device == current_device) {
  565. return;
  566. }
  567. CUDA_CHECK(cudaSetDevice(device));
  568. }
  569. static int g_device_count = -1;
  570. static int g_main_device = 0;
  571. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  572. struct cuda_device_capabilities {
  573. int cc; // compute capability
  574. size_t smpb; // max. shared memory per block
  575. bool vmm; // virtual memory support
  576. size_t vmm_granularity; // granularity of virtual memory
  577. };
  578. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  579. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  580. [[noreturn]]
  581. static __device__ void no_device_code(
  582. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  583. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  584. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  585. file_name, line, function_name, arch);
  586. (void) arch_list;
  587. #else
  588. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  589. file_name, line, function_name, arch, arch_list);
  590. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  591. __trap();
  592. (void) no_device_code; // suppress unused function warning
  593. }
  594. #ifdef __CUDA_ARCH__
  595. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  596. #else
  597. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  598. #endif // __CUDA_ARCH__
  599. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  600. #pragma unroll
  601. for (int mask = 16; mask > 0; mask >>= 1) {
  602. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  603. }
  604. return x;
  605. }
  606. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  607. #pragma unroll
  608. for (int mask = 16; mask > 0; mask >>= 1) {
  609. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  610. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  611. }
  612. return a;
  613. }
  614. //static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  615. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  616. //#pragma unroll
  617. // for (int mask = 16; mask > 0; mask >>= 1) {
  618. // a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  619. // }
  620. // return a;
  621. //#else
  622. // (void) a;
  623. // NO_DEVICE_CODE;
  624. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  625. //}
  626. static __device__ __forceinline__ float warp_reduce_max(float x) {
  627. #pragma unroll
  628. for (int mask = 16; mask > 0; mask >>= 1) {
  629. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  630. }
  631. return x;
  632. }
  633. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  634. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  635. //#pragma unroll
  636. // for (int mask = 16; mask > 0; mask >>= 1) {
  637. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  638. // }
  639. // return x;
  640. //#else
  641. // (void) x;
  642. // NO_DEVICE_CODE;
  643. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  644. //}
  645. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  646. return b;
  647. GGML_UNUSED(a);
  648. }
  649. static __device__ __forceinline__ float op_add(const float a, const float b) {
  650. return a + b;
  651. }
  652. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  653. return a * b;
  654. }
  655. static __device__ __forceinline__ float op_div(const float a, const float b) {
  656. return a / b;
  657. }
  658. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  659. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  660. int ne0, int ne1, int ne2, int ne3,
  661. int ne10, int ne11, int ne12, int ne13,
  662. /*int s0, */ int s1, int s2, int s3,
  663. /*int s10,*/ int s11, int s12, int s13) {
  664. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  665. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  666. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  667. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  668. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  669. return;
  670. }
  671. const int i11 = i1 % ne11;
  672. const int i12 = i2 % ne12;
  673. const int i13 = i3 % ne13;
  674. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  675. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  676. const size_t i_dst = i_src0;
  677. const src0_t * src0_row = src0 + i_src0;
  678. const src1_t * src1_row = src1 + i_src1;
  679. dst_t * dst_row = dst + i_dst;
  680. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  681. const int i10 = i0 % ne10;
  682. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  683. }
  684. }
  685. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  686. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  687. int ne0, int ne1, int ne2, int ne3,
  688. int ne10, int ne11, int ne12, int ne13,
  689. /*int s0, */ int s1, int s2, int s3,
  690. /*int s10,*/ int s11, int s12, int s13) {
  691. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  692. const int i3 = i/(ne2*ne1*ne0);
  693. const int i2 = (i/(ne1*ne0)) % ne2;
  694. const int i1 = (i/ne0) % ne1;
  695. const int i0 = i % ne0;
  696. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  697. return;
  698. }
  699. const int i11 = i1 % ne11;
  700. const int i12 = i2 % ne12;
  701. const int i13 = i3 % ne13;
  702. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  703. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  704. const size_t i_dst = i_src0;
  705. const src0_t * src0_row = src0 + i_src0;
  706. const src1_t * src1_row = src1 + i_src1;
  707. dst_t * dst_row = dst + i_dst;
  708. const int i10 = i0 % ne10;
  709. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  710. }
  711. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  712. const int ne10, const int ne11, const int ne12,
  713. const int nb1, const int nb2, int offset) {
  714. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  715. if (i >= ne) {
  716. return;
  717. }
  718. int src1_idx = i - offset;
  719. int oz = src1_idx / nb2;
  720. int oy = (src1_idx - (oz * nb2)) / nb1;
  721. int ox = src1_idx % nb1;
  722. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  723. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  724. } else {
  725. dst[i] = x[i];
  726. }
  727. }
  728. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  729. const float GELU_COEF_A = 0.044715f;
  730. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  731. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  732. if (i >= k) {
  733. return;
  734. }
  735. float xi = x[i];
  736. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  737. }
  738. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  739. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  740. if (i >= k) {
  741. return;
  742. }
  743. dst[i] = x[i] / (1.0f + expf(-x[i]));
  744. }
  745. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  746. const float GELU_QUICK_COEF = -1.702f;
  747. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  748. if (i >= k) {
  749. return;
  750. }
  751. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  752. }
  753. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  754. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  755. if (i >= k) {
  756. return;
  757. }
  758. dst[i] = tanhf(x[i]);
  759. }
  760. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  761. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  762. if (i >= k) {
  763. return;
  764. }
  765. dst[i] = fmaxf(x[i], 0);
  766. }
  767. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  768. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  769. if (i >= k) {
  770. return;
  771. }
  772. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  773. }
  774. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  775. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  776. if (i >= k) {
  777. return;
  778. }
  779. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  780. }
  781. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  782. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  783. if (i >= k) {
  784. return;
  785. }
  786. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  787. }
  788. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  789. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  790. if (i >= k) {
  791. return;
  792. }
  793. dst[i] = x[i] * x[i];
  794. }
  795. template <int block_size>
  796. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  797. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  798. const int tid = threadIdx.x;
  799. float2 mean_var = make_float2(0.f, 0.f);
  800. for (int col = tid; col < ncols; col += block_size) {
  801. const float xi = x[row*ncols + col];
  802. mean_var.x += xi;
  803. mean_var.y += xi * xi;
  804. }
  805. // sum up partial sums
  806. mean_var = warp_reduce_sum(mean_var);
  807. if (block_size > WARP_SIZE) {
  808. __shared__ float2 s_sum[32];
  809. int warp_id = threadIdx.x / WARP_SIZE;
  810. int lane_id = threadIdx.x % WARP_SIZE;
  811. if (lane_id == 0) {
  812. s_sum[warp_id] = mean_var;
  813. }
  814. __syncthreads();
  815. mean_var = s_sum[lane_id];
  816. mean_var = warp_reduce_sum(mean_var);
  817. }
  818. const float mean = mean_var.x / ncols;
  819. const float var = mean_var.y / ncols - mean * mean;
  820. const float inv_std = rsqrtf(var + eps);
  821. for (int col = tid; col < ncols; col += block_size) {
  822. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  823. }
  824. }
  825. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  826. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  827. if (nidx >= ne0) {
  828. return;
  829. }
  830. // operation
  831. int offset_dst =
  832. nidx +
  833. blockIdx.y * ne0 +
  834. blockIdx.z * ne0 * gridDim.y;
  835. if (blockIdx.z < ne02) { // src0
  836. int offset_src =
  837. nidx +
  838. blockIdx.y * ne0 +
  839. blockIdx.z * ne0 * gridDim.y;
  840. dst[offset_dst] = x[offset_src];
  841. } else {
  842. int offset_src =
  843. nidx +
  844. blockIdx.y * ne0 +
  845. (blockIdx.z - ne02) * ne0 * gridDim.y;
  846. dst[offset_dst] = y[offset_src];
  847. }
  848. }
  849. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  850. int ne0 = ne00 * scale_factor;
  851. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  852. if (nidx >= ne0) {
  853. return;
  854. }
  855. // operation
  856. int i00 = nidx / scale_factor;
  857. int i01 = blockIdx.y / scale_factor;
  858. int offset_src =
  859. i00 +
  860. i01 * ne00 +
  861. blockIdx.z * nb02;
  862. int offset_dst =
  863. nidx +
  864. blockIdx.y * ne0 +
  865. blockIdx.z * ne0 * gridDim.y;
  866. dst[offset_dst] = x[offset_src];
  867. }
  868. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  869. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  870. if (nidx >= ne0) {
  871. return;
  872. }
  873. // operation
  874. int offset_dst =
  875. nidx +
  876. blockIdx.y * ne0 +
  877. blockIdx.z * ne0 * gridDim.y;
  878. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  879. int offset_src =
  880. nidx +
  881. blockIdx.y * ne00 +
  882. blockIdx.z * ne00 * ne01;
  883. dst[offset_dst] = x[offset_src];
  884. } else {
  885. dst[offset_dst] = 0.0f;
  886. }
  887. }
  888. template <int block_size>
  889. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  890. int start = blockIdx.x * group_size;
  891. int end = start + group_size;
  892. start += threadIdx.x;
  893. if (end >= ne_elements) {
  894. end = ne_elements;
  895. }
  896. float tmp = 0.0f; // partial sum for thread in warp
  897. for (int j = start; j < end; j += block_size) {
  898. tmp += x[j];
  899. }
  900. tmp = warp_reduce_sum(tmp);
  901. if (block_size > WARP_SIZE) {
  902. __shared__ float s_sum[32];
  903. int warp_id = threadIdx.x / WARP_SIZE;
  904. int lane_id = threadIdx.x % WARP_SIZE;
  905. if (lane_id == 0) {
  906. s_sum[warp_id] = tmp;
  907. }
  908. __syncthreads();
  909. tmp = s_sum[lane_id];
  910. tmp = warp_reduce_sum(tmp);
  911. }
  912. float mean = tmp / group_size;
  913. tmp = 0.0f;
  914. for (int j = start; j < end; j += block_size) {
  915. float xi = x[j] - mean;
  916. dst[j] = xi;
  917. tmp += xi * xi;
  918. }
  919. tmp = warp_reduce_sum(tmp);
  920. if (block_size > WARP_SIZE) {
  921. __shared__ float s_sum[32];
  922. int warp_id = threadIdx.x / WARP_SIZE;
  923. int lane_id = threadIdx.x % WARP_SIZE;
  924. if (lane_id == 0) {
  925. s_sum[warp_id] = tmp;
  926. }
  927. __syncthreads();
  928. tmp = s_sum[lane_id];
  929. tmp = warp_reduce_sum(tmp);
  930. }
  931. float variance = tmp / group_size;
  932. float scale = rsqrtf(variance + eps);
  933. for (int j = start; j < end; j += block_size) {
  934. dst[j] *= scale;
  935. }
  936. }
  937. template <int block_size>
  938. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  939. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  940. const int tid = threadIdx.x;
  941. float tmp = 0.0f; // partial sum for thread in warp
  942. for (int col = tid; col < ncols; col += block_size) {
  943. const float xi = x[row*ncols + col];
  944. tmp += xi * xi;
  945. }
  946. // sum up partial sums
  947. tmp = warp_reduce_sum(tmp);
  948. if (block_size > WARP_SIZE) {
  949. __shared__ float s_sum[32];
  950. int warp_id = threadIdx.x / WARP_SIZE;
  951. int lane_id = threadIdx.x % WARP_SIZE;
  952. if (lane_id == 0) {
  953. s_sum[warp_id] = tmp;
  954. }
  955. __syncthreads();
  956. tmp = s_sum[lane_id];
  957. tmp = warp_reduce_sum(tmp);
  958. }
  959. const float mean = tmp / ncols;
  960. const float scale = rsqrtf(mean + eps);
  961. for (int col = tid; col < ncols; col += block_size) {
  962. dst[row*ncols + col] = scale * x[row*ncols + col];
  963. }
  964. }
  965. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  966. const block_q4_0 * x = (const block_q4_0 *) vx;
  967. const dfloat d = x[ib].d;
  968. const int vui = x[ib].qs[iqs];
  969. v.x = vui & 0xF;
  970. v.y = vui >> 4;
  971. #ifdef GGML_CUDA_F16
  972. v = __hsub2(v, {8.0f, 8.0f});
  973. v = __hmul2(v, {d, d});
  974. #else
  975. v.x = (v.x - 8.0f) * d;
  976. v.y = (v.y - 8.0f) * d;
  977. #endif // GGML_CUDA_F16
  978. }
  979. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  980. const block_q4_1 * x = (const block_q4_1 *) vx;
  981. const dfloat d = __low2half(x[ib].dm);
  982. const dfloat m = __high2half(x[ib].dm);
  983. const int vui = x[ib].qs[iqs];
  984. v.x = vui & 0xF;
  985. v.y = vui >> 4;
  986. #ifdef GGML_CUDA_F16
  987. v = __hmul2(v, {d, d});
  988. v = __hadd2(v, {m, m});
  989. #else
  990. v.x = (v.x * d) + m;
  991. v.y = (v.y * d) + m;
  992. #endif // GGML_CUDA_F16
  993. }
  994. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  995. const block_q5_0 * x = (const block_q5_0 *) vx;
  996. const dfloat d = x[ib].d;
  997. uint32_t qh;
  998. memcpy(&qh, x[ib].qh, sizeof(qh));
  999. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1000. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1001. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1002. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1003. #ifdef GGML_CUDA_F16
  1004. v = __hsub2(v, {16.0f, 16.0f});
  1005. v = __hmul2(v, {d, d});
  1006. #else
  1007. v.x = (v.x - 16.0f) * d;
  1008. v.y = (v.y - 16.0f) * d;
  1009. #endif // GGML_CUDA_F16
  1010. }
  1011. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1012. const block_q5_1 * x = (const block_q5_1 *) vx;
  1013. const dfloat d = __low2half(x[ib].dm);
  1014. const dfloat m = __high2half(x[ib].dm);
  1015. uint32_t qh;
  1016. memcpy(&qh, x[ib].qh, sizeof(qh));
  1017. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1018. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1019. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1020. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1021. #ifdef GGML_CUDA_F16
  1022. v = __hmul2(v, {d, d});
  1023. v = __hadd2(v, {m, m});
  1024. #else
  1025. v.x = (v.x * d) + m;
  1026. v.y = (v.y * d) + m;
  1027. #endif // GGML_CUDA_F16
  1028. }
  1029. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1030. const block_q8_0 * x = (const block_q8_0 *) vx;
  1031. const dfloat d = x[ib].d;
  1032. v.x = x[ib].qs[iqs + 0];
  1033. v.y = x[ib].qs[iqs + 1];
  1034. #ifdef GGML_CUDA_F16
  1035. v = __hmul2(v, {d, d});
  1036. #else
  1037. v.x *= d;
  1038. v.y *= d;
  1039. #endif // GGML_CUDA_F16
  1040. }
  1041. template<typename dst_t>
  1042. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1043. const int i = blockIdx.x;
  1044. // assume 32 threads
  1045. const int tid = threadIdx.x;
  1046. const int il = tid/8;
  1047. const int ir = tid%8;
  1048. const int ib = 8*i + ir;
  1049. if (ib >= nb32) {
  1050. return;
  1051. }
  1052. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1053. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1054. const float d = __half2float(x->d);
  1055. const float dm = -8*d;
  1056. const uint8_t * q = x->qs + 4*il;
  1057. for (int l = 0; l < 4; ++l) {
  1058. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1059. y[l+16] = d * (q[l] >> 4) + dm;
  1060. }
  1061. }
  1062. template<typename dst_t>
  1063. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1064. const int i = blockIdx.x;
  1065. // assume 32 threads
  1066. const int tid = threadIdx.x;
  1067. const int il = tid/8;
  1068. const int ir = tid%8;
  1069. const int ib = 8*i + ir;
  1070. if (ib >= nb32) {
  1071. return;
  1072. }
  1073. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1074. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1075. const float2 d = __half22float2(x->dm);
  1076. const uint8_t * q = x->qs + 4*il;
  1077. for (int l = 0; l < 4; ++l) {
  1078. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1079. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1080. }
  1081. }
  1082. //================================== k-quants
  1083. template<typename dst_t>
  1084. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1085. const int i = blockIdx.x;
  1086. const block_q2_K * x = (const block_q2_K *) vx;
  1087. const int tid = threadIdx.x;
  1088. #if QK_K == 256
  1089. const int n = tid/32;
  1090. const int l = tid - 32*n;
  1091. const int is = 8*n + l/16;
  1092. const uint8_t q = x[i].qs[32*n + l];
  1093. dst_t * y = yy + i*QK_K + 128*n;
  1094. float dall = __low2half(x[i].dm);
  1095. float dmin = __high2half(x[i].dm);
  1096. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1097. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1098. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1099. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1100. #else
  1101. const int is = tid/16; // 0 or 1
  1102. const int il = tid%16; // 0...15
  1103. const uint8_t q = x[i].qs[il] >> (2*is);
  1104. dst_t * y = yy + i*QK_K + 16*is + il;
  1105. float dall = __low2half(x[i].dm);
  1106. float dmin = __high2half(x[i].dm);
  1107. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1108. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1109. #endif
  1110. }
  1111. template<typename dst_t>
  1112. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1113. const int i = blockIdx.x;
  1114. const block_q3_K * x = (const block_q3_K *) vx;
  1115. #if QK_K == 256
  1116. const int r = threadIdx.x/4;
  1117. const int tid = r/2;
  1118. const int is0 = r%2;
  1119. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1120. const int n = tid / 4;
  1121. const int j = tid - 4*n;
  1122. uint8_t m = 1 << (4*n + j);
  1123. int is = 8*n + 2*j + is0;
  1124. int shift = 2*j;
  1125. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1126. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1127. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1128. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1129. float d_all = x[i].d;
  1130. float dl = d_all * (us - 32);
  1131. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1132. const uint8_t * q = x[i].qs + 32*n;
  1133. const uint8_t * hm = x[i].hmask;
  1134. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1135. #else
  1136. const int tid = threadIdx.x;
  1137. const int is = tid/16; // 0 or 1
  1138. const int il = tid%16; // 0...15
  1139. const int im = il/8; // 0...1
  1140. const int in = il%8; // 0...7
  1141. dst_t * y = yy + i*QK_K + 16*is + il;
  1142. const uint8_t q = x[i].qs[il] >> (2*is);
  1143. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1144. const float d = (float)x[i].d;
  1145. if (is == 0) {
  1146. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1147. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1148. } else {
  1149. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1150. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1151. }
  1152. #endif
  1153. }
  1154. #if QK_K == 256
  1155. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1156. if (j < 4) {
  1157. d = q[j] & 63; m = q[j + 4] & 63;
  1158. } else {
  1159. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1160. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1161. }
  1162. }
  1163. #endif
  1164. template<typename dst_t>
  1165. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1166. const block_q4_K * x = (const block_q4_K *) vx;
  1167. const int i = blockIdx.x;
  1168. #if QK_K == 256
  1169. // assume 32 threads
  1170. const int tid = threadIdx.x;
  1171. const int il = tid/8;
  1172. const int ir = tid%8;
  1173. const int is = 2*il;
  1174. const int n = 4;
  1175. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1176. const float dall = __low2half(x[i].dm);
  1177. const float dmin = __high2half(x[i].dm);
  1178. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1179. uint8_t sc, m;
  1180. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1181. const float d1 = dall * sc; const float m1 = dmin * m;
  1182. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1183. const float d2 = dall * sc; const float m2 = dmin * m;
  1184. for (int l = 0; l < n; ++l) {
  1185. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1186. y[l +32] = d2 * (q[l] >> 4) - m2;
  1187. }
  1188. #else
  1189. const int tid = threadIdx.x;
  1190. const uint8_t * q = x[i].qs;
  1191. dst_t * y = yy + i*QK_K;
  1192. const float d = (float)x[i].dm[0];
  1193. const float m = (float)x[i].dm[1];
  1194. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1195. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1196. #endif
  1197. }
  1198. template<typename dst_t>
  1199. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1200. const block_q5_K * x = (const block_q5_K *) vx;
  1201. const int i = blockIdx.x;
  1202. #if QK_K == 256
  1203. // assume 64 threads - this is very slightly better than the one below
  1204. const int tid = threadIdx.x;
  1205. const int il = tid/16; // il is in 0...3
  1206. const int ir = tid%16; // ir is in 0...15
  1207. const int is = 2*il; // is is in 0...6
  1208. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1209. const float dall = __low2half(x[i].dm);
  1210. const float dmin = __high2half(x[i].dm);
  1211. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1212. const uint8_t * qh = x[i].qh + 2*ir;
  1213. uint8_t sc, m;
  1214. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1215. const float d1 = dall * sc; const float m1 = dmin * m;
  1216. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1217. const float d2 = dall * sc; const float m2 = dmin * m;
  1218. uint8_t hm = 1 << (2*il);
  1219. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1220. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1221. hm <<= 1;
  1222. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1223. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1224. #else
  1225. const int tid = threadIdx.x;
  1226. const uint8_t q = x[i].qs[tid];
  1227. const int im = tid/8; // 0...3
  1228. const int in = tid%8; // 0...7
  1229. const int is = tid/16; // 0 or 1
  1230. const uint8_t h = x[i].qh[in] >> im;
  1231. const float d = x[i].d;
  1232. dst_t * y = yy + i*QK_K + tid;
  1233. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1234. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1235. #endif
  1236. }
  1237. template<typename dst_t>
  1238. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1239. const block_q6_K * x = (const block_q6_K *) vx;
  1240. const int i = blockIdx.x;
  1241. #if QK_K == 256
  1242. // assume 64 threads - this is very slightly better than the one below
  1243. const int tid = threadIdx.x;
  1244. const int ip = tid/32; // ip is 0 or 1
  1245. const int il = tid - 32*ip; // 0...32
  1246. const int is = 8*ip + il/16;
  1247. dst_t * y = yy + i*QK_K + 128*ip + il;
  1248. const float d = x[i].d;
  1249. const uint8_t * ql = x[i].ql + 64*ip + il;
  1250. const uint8_t qh = x[i].qh[32*ip + il];
  1251. const int8_t * sc = x[i].scales + is;
  1252. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1253. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1254. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1255. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1256. #else
  1257. // assume 32 threads
  1258. const int tid = threadIdx.x;
  1259. const int ip = tid/16; // 0 or 1
  1260. const int il = tid - 16*ip; // 0...15
  1261. dst_t * y = yy + i*QK_K + 16*ip + il;
  1262. const float d = x[i].d;
  1263. const uint8_t ql = x[i].ql[16*ip + il];
  1264. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1265. const int8_t * sc = x[i].scales;
  1266. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1267. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1268. #endif
  1269. }
  1270. static const __device__ uint64_t iq2xxs_grid[256] = {
  1271. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1272. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1273. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1274. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1275. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1276. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1277. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1278. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1279. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1280. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1281. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1282. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1283. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1284. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1285. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1286. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1287. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1288. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1289. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1290. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1291. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1292. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1293. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1294. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1295. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1296. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1297. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1298. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1299. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1300. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1301. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1302. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1303. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1304. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1305. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1306. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1307. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1308. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1309. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1310. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1311. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1312. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1313. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1314. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1315. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1316. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1317. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1318. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1319. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1320. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1321. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1322. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1323. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1324. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1325. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1326. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1327. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1328. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1329. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1330. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1331. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1332. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1333. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1334. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1335. };
  1336. static const __device__ uint64_t iq2xs_grid[512] = {
  1337. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1338. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1339. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1340. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1341. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1342. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  1343. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  1344. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  1345. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  1346. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  1347. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  1348. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  1349. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  1350. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  1351. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  1352. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  1353. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  1354. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  1355. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  1356. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  1357. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  1358. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  1359. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  1360. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  1361. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  1362. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  1363. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  1364. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  1365. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  1366. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  1367. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  1368. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  1369. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  1370. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  1371. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  1372. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  1373. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  1374. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  1375. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  1376. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  1377. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  1378. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  1379. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  1380. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  1381. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  1382. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  1383. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  1384. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  1385. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  1386. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  1387. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  1388. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  1389. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  1390. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  1391. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  1392. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  1393. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  1394. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  1395. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  1396. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  1397. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  1398. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  1399. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  1400. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  1401. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  1402. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  1403. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  1404. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  1405. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  1406. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  1407. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  1408. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  1409. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  1410. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  1411. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  1412. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  1413. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  1414. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  1415. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  1416. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  1417. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  1418. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  1419. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  1420. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  1421. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  1422. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  1423. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  1424. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  1425. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  1426. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  1427. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  1428. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  1429. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  1430. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  1431. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  1432. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  1433. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  1434. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  1435. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  1436. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  1437. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  1438. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  1439. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  1440. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  1441. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  1442. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  1443. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  1444. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  1445. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  1446. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  1447. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  1448. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  1449. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  1450. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  1451. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  1452. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  1453. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  1454. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  1455. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  1456. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  1457. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  1458. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  1459. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  1460. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  1461. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  1462. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  1463. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  1464. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  1465. };
  1466. static const __device__ uint64_t iq2s_grid[1024] = {
  1467. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1468. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1469. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1470. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1471. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1472. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
  1473. 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
  1474. 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
  1475. 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
  1476. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
  1477. 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
  1478. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
  1479. 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
  1480. 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
  1481. 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
  1482. 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
  1483. 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
  1484. 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
  1485. 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
  1486. 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
  1487. 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  1488. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
  1489. 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
  1490. 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
  1491. 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
  1492. 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
  1493. 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
  1494. 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
  1495. 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
  1496. 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
  1497. 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
  1498. 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
  1499. 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
  1500. 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
  1501. 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
  1502. 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
  1503. 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
  1504. 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
  1505. 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
  1506. 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
  1507. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
  1508. 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
  1509. 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
  1510. 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
  1511. 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
  1512. 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
  1513. 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
  1514. 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
  1515. 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
  1516. 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
  1517. 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
  1518. 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
  1519. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
  1520. 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
  1521. 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
  1522. 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
  1523. 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
  1524. 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
  1525. 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
  1526. 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
  1527. 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
  1528. 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
  1529. 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
  1530. 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
  1531. 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
  1532. 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
  1533. 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
  1534. 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
  1535. 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
  1536. 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
  1537. 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  1538. 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
  1539. 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
  1540. 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
  1541. 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
  1542. 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
  1543. 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
  1544. 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
  1545. 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
  1546. 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
  1547. 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
  1548. 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
  1549. 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
  1550. 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
  1551. 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
  1552. 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
  1553. 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
  1554. 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
  1555. 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
  1556. 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
  1557. 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
  1558. 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
  1559. 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
  1560. 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
  1561. 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
  1562. 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
  1563. 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
  1564. 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
  1565. 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
  1566. 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
  1567. 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
  1568. 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
  1569. 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
  1570. 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
  1571. 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
  1572. 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
  1573. 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
  1574. 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
  1575. 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
  1576. 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
  1577. 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
  1578. 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
  1579. 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
  1580. 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
  1581. 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
  1582. 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
  1583. 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
  1584. 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  1585. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
  1586. 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
  1587. 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
  1588. 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
  1589. 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
  1590. 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
  1591. 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
  1592. 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
  1593. 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  1594. 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
  1595. 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
  1596. 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
  1597. 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
  1598. 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
  1599. 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
  1600. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
  1601. 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
  1602. 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
  1603. 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
  1604. 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
  1605. 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
  1606. 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
  1607. 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
  1608. 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
  1609. 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
  1610. 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
  1611. 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
  1612. 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
  1613. 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
  1614. 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
  1615. 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
  1616. 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
  1617. 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
  1618. 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
  1619. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
  1620. 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
  1621. 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
  1622. 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
  1623. 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
  1624. 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
  1625. 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
  1626. 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
  1627. 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
  1628. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
  1629. 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
  1630. 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
  1631. 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
  1632. 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
  1633. 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
  1634. 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
  1635. 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
  1636. 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
  1637. 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
  1638. 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
  1639. 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
  1640. 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
  1641. 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
  1642. 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
  1643. 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
  1644. 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
  1645. 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
  1646. 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
  1647. 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
  1648. 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
  1649. 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
  1650. 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
  1651. 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
  1652. 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
  1653. 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
  1654. 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
  1655. 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
  1656. 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
  1657. 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
  1658. 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
  1659. 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
  1660. 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
  1661. 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
  1662. 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
  1663. 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
  1664. 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
  1665. 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
  1666. 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
  1667. 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
  1668. 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
  1669. 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
  1670. 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
  1671. 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
  1672. 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  1673. 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
  1674. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
  1675. 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
  1676. 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
  1677. 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
  1678. 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
  1679. 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
  1680. 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
  1681. 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
  1682. 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
  1683. 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
  1684. 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
  1685. 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
  1686. 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
  1687. 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
  1688. 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
  1689. 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
  1690. 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
  1691. 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
  1692. 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
  1693. 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
  1694. 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
  1695. 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
  1696. 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
  1697. 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
  1698. 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
  1699. 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
  1700. 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
  1701. 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
  1702. 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
  1703. 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
  1704. 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
  1705. 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
  1706. 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
  1707. 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
  1708. 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
  1709. 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
  1710. 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
  1711. 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
  1712. 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
  1713. 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
  1714. 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
  1715. 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
  1716. 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
  1717. 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
  1718. 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
  1719. 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
  1720. 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
  1721. 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
  1722. 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
  1723. };
  1724. static const __device__ uint32_t iq3xxs_grid[256] = {
  1725. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  1726. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  1727. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  1728. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  1729. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  1730. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  1731. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  1732. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  1733. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  1734. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  1735. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  1736. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  1737. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  1738. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  1739. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  1740. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  1741. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  1742. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  1743. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  1744. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  1745. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  1746. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  1747. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  1748. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  1749. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  1750. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  1751. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  1752. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  1753. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  1754. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  1755. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  1756. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  1757. };
  1758. static const __device__ uint32_t iq3xs_grid[512] = {
  1759. 0x04040404, 0x0404040c, 0x04040414, 0x0404042c, 0x0404043e, 0x04040c04, 0x04040c0c, 0x04040c14,
  1760. 0x04040c24, 0x04040c34, 0x04041404, 0x0404140c, 0x0404142c, 0x04041c1c, 0x04042404, 0x04042414,
  1761. 0x0404242c, 0x0404243e, 0x04042c0c, 0x04042c1c, 0x04043404, 0x04043414, 0x04043e0c, 0x04043e24,
  1762. 0x04043e3e, 0x040c0404, 0x040c040c, 0x040c0414, 0x040c0424, 0x040c0c04, 0x040c0c0c, 0x040c0c2c,
  1763. 0x040c1404, 0x040c141c, 0x040c143e, 0x040c1c0c, 0x040c1c2c, 0x040c2424, 0x040c340c, 0x040c342c,
  1764. 0x040c3e14, 0x04140404, 0x0414040c, 0x0414042c, 0x0414043e, 0x04140c04, 0x04140c1c, 0x04140c34,
  1765. 0x0414140c, 0x0414142c, 0x04141c04, 0x04141c24, 0x04142414, 0x0414242c, 0x0414243e, 0x04142c0c,
  1766. 0x04142c1c, 0x04143e04, 0x04143e1c, 0x041c041c, 0x041c0c0c, 0x041c0c2c, 0x041c1404, 0x041c1414,
  1767. 0x041c1c0c, 0x041c1c1c, 0x041c1c34, 0x041c2424, 0x041c2c04, 0x041c2c14, 0x041c343e, 0x041c3e0c,
  1768. 0x041c3e2c, 0x04240404, 0x04240c1c, 0x04240c3e, 0x0424140c, 0x04241424, 0x04241c14, 0x04242404,
  1769. 0x0424241c, 0x04242c0c, 0x04243e04, 0x042c0414, 0x042c0424, 0x042c1404, 0x042c1414, 0x042c1434,
  1770. 0x042c1c1c, 0x042c240c, 0x042c242c, 0x042c243e, 0x042c3434, 0x042c3e1c, 0x04340434, 0x04340c0c,
  1771. 0x04340c1c, 0x04341c0c, 0x04342c14, 0x04343e0c, 0x043e0404, 0x043e0414, 0x043e0424, 0x043e1404,
  1772. 0x043e1414, 0x043e1434, 0x043e1c1c, 0x043e2c04, 0x043e2c24, 0x0c040404, 0x0c04040c, 0x0c040414,
  1773. 0x0c040424, 0x0c040c04, 0x0c040c0c, 0x0c040c1c, 0x0c040c2c, 0x0c040c3e, 0x0c041404, 0x0c041414,
  1774. 0x0c041c0c, 0x0c041c24, 0x0c041c34, 0x0c042c24, 0x0c042c34, 0x0c04340c, 0x0c043e14, 0x0c0c0404,
  1775. 0x0c0c040c, 0x0c0c041c, 0x0c0c0434, 0x0c0c0c04, 0x0c0c0c24, 0x0c0c140c, 0x0c0c1c04, 0x0c0c1c1c,
  1776. 0x0c0c240c, 0x0c0c2c04, 0x0c0c2c14, 0x0c0c3e04, 0x0c0c3e34, 0x0c140404, 0x0c140c14, 0x0c140c2c,
  1777. 0x0c140c3e, 0x0c141404, 0x0c141424, 0x0c141c14, 0x0c142404, 0x0c14241c, 0x0c142c2c, 0x0c143404,
  1778. 0x0c143e14, 0x0c1c040c, 0x0c1c0424, 0x0c1c043e, 0x0c1c0c04, 0x0c1c0c1c, 0x0c1c140c, 0x0c1c143e,
  1779. 0x0c1c1c04, 0x0c1c1c24, 0x0c1c240c, 0x0c1c3414, 0x0c1c3e04, 0x0c24041c, 0x0c24042c, 0x0c240c14,
  1780. 0x0c240c24, 0x0c241c0c, 0x0c241c1c, 0x0c242414, 0x0c242434, 0x0c242c04, 0x0c242c24, 0x0c2c040c,
  1781. 0x0c2c0c04, 0x0c2c0c1c, 0x0c2c140c, 0x0c2c1c04, 0x0c2c1c14, 0x0c2c2c0c, 0x0c341404, 0x0c341424,
  1782. 0x0c34143e, 0x0c342424, 0x0c342434, 0x0c3e040c, 0x0c3e041c, 0x0c3e0c04, 0x0c3e0c14, 0x0c3e140c,
  1783. 0x0c3e1c2c, 0x0c3e240c, 0x0c3e3414, 0x0c3e3e04, 0x14040404, 0x1404040c, 0x1404041c, 0x1404042c,
  1784. 0x1404043e, 0x14040c04, 0x14040c14, 0x14040c24, 0x14040c34, 0x1404140c, 0x1404141c, 0x1404143e,
  1785. 0x14041c04, 0x14041c14, 0x1404240c, 0x1404241c, 0x1404242c, 0x14042c04, 0x14042c14, 0x1404343e,
  1786. 0x14043e04, 0x14043e1c, 0x14043e2c, 0x140c0404, 0x140c0414, 0x140c0c04, 0x140c0c1c, 0x140c0c3e,
  1787. 0x140c1414, 0x140c142c, 0x140c1c0c, 0x140c1c24, 0x140c2414, 0x140c2c0c, 0x1414040c, 0x14140424,
  1788. 0x1414043e, 0x1414140c, 0x1414141c, 0x14141c04, 0x14141c3e, 0x1414240c, 0x14142c1c, 0x14142c3e,
  1789. 0x14143e0c, 0x14143e24, 0x141c0404, 0x141c0414, 0x141c042c, 0x141c0c0c, 0x141c1414, 0x141c1424,
  1790. 0x141c1c0c, 0x141c1c1c, 0x141c2414, 0x141c2c04, 0x141c3434, 0x1424040c, 0x1424043e, 0x14241404,
  1791. 0x1424141c, 0x14241c14, 0x14241c2c, 0x1424240c, 0x14243e14, 0x14243e2c, 0x142c0424, 0x142c0c0c,
  1792. 0x142c1414, 0x142c1c3e, 0x142c2404, 0x142c2c1c, 0x142c3e04, 0x14340404, 0x14340414, 0x1434043e,
  1793. 0x1434140c, 0x14342c2c, 0x1434340c, 0x143e042c, 0x143e0c0c, 0x143e1434, 0x143e1c04, 0x143e241c,
  1794. 0x143e2c04, 0x1c040414, 0x1c040c0c, 0x1c040c1c, 0x1c040c2c, 0x1c040c3e, 0x1c041414, 0x1c041c0c,
  1795. 0x1c041c1c, 0x1c041c2c, 0x1c042414, 0x1c042424, 0x1c04243e, 0x1c042c0c, 0x1c04341c, 0x1c043e0c,
  1796. 0x1c0c040c, 0x1c0c041c, 0x1c0c042c, 0x1c0c0c24, 0x1c0c140c, 0x1c0c141c, 0x1c0c2404, 0x1c0c3404,
  1797. 0x1c0c3e14, 0x1c0c3e34, 0x1c140404, 0x1c140c14, 0x1c141404, 0x1c141c14, 0x1c141c24, 0x1c142c04,
  1798. 0x1c1c040c, 0x1c1c0c04, 0x1c1c0c24, 0x1c1c140c, 0x1c1c141c, 0x1c1c143e, 0x1c1c1c04, 0x1c1c240c,
  1799. 0x1c1c241c, 0x1c1c243e, 0x1c1c2c2c, 0x1c1c3e1c, 0x1c24041c, 0x1c240c0c, 0x1c240c34, 0x1c241414,
  1800. 0x1c241c0c, 0x1c242c14, 0x1c243404, 0x1c243424, 0x1c2c040c, 0x1c2c0c04, 0x1c2c0c14, 0x1c2c142c,
  1801. 0x1c2c1c14, 0x1c2c2424, 0x1c2c2c34, 0x1c2c3e1c, 0x1c340c34, 0x1c34240c, 0x1c3e040c, 0x1c3e041c,
  1802. 0x1c3e1404, 0x1c3e1414, 0x1c3e1c2c, 0x24040404, 0x24040424, 0x24040c14, 0x24041404, 0x24041424,
  1803. 0x2404143e, 0x24041c14, 0x2404240c, 0x24042c04, 0x24043e04, 0x240c0414, 0x240c043e, 0x240c0c0c,
  1804. 0x240c0c1c, 0x240c1414, 0x240c1c04, 0x240c1c2c, 0x240c241c, 0x240c2c0c, 0x240c2c2c, 0x2414040c,
  1805. 0x2414041c, 0x24140c04, 0x24140c2c, 0x2414140c, 0x24141c1c, 0x24142404, 0x24142c3e, 0x24143414,
  1806. 0x24143e04, 0x241c0424, 0x241c0c0c, 0x241c0c1c, 0x241c1404, 0x241c1414, 0x241c1c0c, 0x241c1c2c,
  1807. 0x24240404, 0x24240414, 0x24241424, 0x24241c3e, 0x24242404, 0x24243e0c, 0x242c042c, 0x242c043e,
  1808. 0x242c140c, 0x242c3414, 0x24340c1c, 0x24341c24, 0x24343404, 0x243e0c04, 0x243e0c2c, 0x243e1c04,
  1809. 0x243e241c, 0x243e2c0c, 0x2c040414, 0x2c040c04, 0x2c040c24, 0x2c041414, 0x2c042404, 0x2c042424,
  1810. 0x2c04243e, 0x2c042c14, 0x2c043434, 0x2c043e24, 0x2c0c040c, 0x2c0c041c, 0x2c0c042c, 0x2c0c0c14,
  1811. 0x2c0c140c, 0x2c0c1c14, 0x2c0c3e14, 0x2c140404, 0x2c140c0c, 0x2c14141c, 0x2c141c04, 0x2c141c34,
  1812. 0x2c142c1c, 0x2c1c0414, 0x2c1c043e, 0x2c1c0c04, 0x2c1c143e, 0x2c1c2424, 0x2c1c2c0c, 0x2c1c342c,
  1813. 0x2c1c3e1c, 0x2c24040c, 0x2c240424, 0x2c241404, 0x2c241c14, 0x2c242434, 0x2c2c0c14, 0x2c2c1434,
  1814. 0x2c2c2c0c, 0x2c2c2c1c, 0x2c342414, 0x2c3e0414, 0x2c3e0424, 0x2c3e1414, 0x34040c0c, 0x34040c1c,
  1815. 0x34040c2c, 0x34041c0c, 0x34041c1c, 0x34043404, 0x340c0404, 0x340c1404, 0x340c143e, 0x340c3424,
  1816. 0x34140c14, 0x34141c24, 0x34142414, 0x34142c2c, 0x34143414, 0x34143e04, 0x341c0404, 0x341c0c24,
  1817. 0x341c140c, 0x341c2404, 0x3424142c, 0x3424241c, 0x34243414, 0x342c0404, 0x342c041c, 0x342c1c24,
  1818. 0x342c3404, 0x3434042c, 0x34342404, 0x343e0c0c, 0x343e0c1c, 0x3e040404, 0x3e040424, 0x3e04043e,
  1819. 0x3e041404, 0x3e041414, 0x3e041c34, 0x3e042404, 0x3e042c24, 0x3e043414, 0x3e0c0414, 0x3e0c0c0c,
  1820. 0x3e0c1424, 0x3e0c241c, 0x3e0c242c, 0x3e14040c, 0x3e140424, 0x3e140c04, 0x3e140c34, 0x3e14140c,
  1821. 0x3e141c04, 0x3e142c0c, 0x3e1c0414, 0x3e1c1c14, 0x3e1c1c2c, 0x3e1c2c1c, 0x3e24040c, 0x3e24042c,
  1822. 0x3e240c1c, 0x3e241404, 0x3e242c04, 0x3e2c1414, 0x3e2c2414, 0x3e340414, 0x3e341c0c, 0x3e3e0404,
  1823. };
  1824. static const __device__ uint64_t iq1s_grid[512] = {
  1825. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  1826. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  1827. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  1828. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  1829. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  1830. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  1831. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  1832. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  1833. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  1834. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  1835. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  1836. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  1837. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  1838. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  1839. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  1840. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  1841. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  1842. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  1843. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  1844. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  1845. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  1846. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  1847. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  1848. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  1849. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  1850. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  1851. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  1852. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  1853. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  1854. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  1855. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  1856. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  1857. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  1858. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  1859. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  1860. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  1861. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  1862. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  1863. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  1864. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  1865. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  1866. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  1867. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  1868. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  1869. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  1870. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  1871. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  1872. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  1873. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  1874. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  1875. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  1876. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  1877. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  1878. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  1879. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1880. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  1881. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  1882. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  1883. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1884. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  1885. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  1886. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  1887. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  1888. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1889. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  1890. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  1891. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  1892. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  1893. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  1894. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  1895. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  1896. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  1897. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  1898. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  1899. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  1900. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  1901. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  1902. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  1903. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  1904. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  1905. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  1906. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  1907. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  1908. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  1909. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  1910. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  1911. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  1912. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  1913. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  1914. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  1915. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  1916. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  1917. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  1918. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  1919. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  1920. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  1921. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  1922. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  1923. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  1924. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  1925. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  1926. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  1927. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  1928. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  1929. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  1930. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  1931. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  1932. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  1933. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  1934. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  1935. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  1936. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  1937. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  1938. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  1939. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  1940. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  1941. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  1942. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  1943. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  1944. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  1945. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  1946. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  1947. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  1948. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  1949. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  1950. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  1951. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  1952. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  1953. };
  1954. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1955. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1956. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1957. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1958. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1959. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1960. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1961. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1962. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1963. };
  1964. //#if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1965. static const __device__ uint64_t ksigns64[128] = {
  1966. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  1967. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  1968. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  1969. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  1970. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  1971. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  1972. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  1973. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  1974. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  1975. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  1976. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  1977. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  1978. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  1979. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  1980. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  1981. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  1982. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  1983. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  1984. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  1985. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  1986. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  1987. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  1988. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  1989. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  1990. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  1991. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  1992. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  1993. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  1994. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  1995. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  1996. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  1997. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  1998. };
  1999. //#endif
  2000. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2001. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  2002. switch (type) {
  2003. case GGML_TYPE_Q4_0:
  2004. case GGML_TYPE_Q4_1:
  2005. case GGML_TYPE_Q5_0:
  2006. case GGML_TYPE_Q5_1:
  2007. case GGML_TYPE_Q8_0:
  2008. case GGML_TYPE_Q2_K:
  2009. case GGML_TYPE_Q3_K:
  2010. case GGML_TYPE_Q4_K:
  2011. case GGML_TYPE_Q5_K:
  2012. case GGML_TYPE_Q6_K:
  2013. return true;
  2014. default:
  2015. return false;
  2016. }
  2017. }
  2018. template<typename dst_t>
  2019. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2020. const int i = blockIdx.x;
  2021. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  2022. const int tid = threadIdx.x;
  2023. #if QK_K == 256
  2024. const int il = tid/8; // 0...3
  2025. const int ib = tid%8; // 0...7
  2026. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2027. const uint16_t * q2 = x[i].qs + 4*ib;
  2028. const uint8_t * aux8 = (const uint8_t *)q2;
  2029. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  2030. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  2031. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  2032. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  2033. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2034. #else
  2035. assert(false);
  2036. #endif
  2037. }
  2038. template<typename dst_t>
  2039. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2040. const int i = blockIdx.x;
  2041. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  2042. const int tid = threadIdx.x;
  2043. #if QK_K == 256
  2044. const int il = tid/8; // 0...3
  2045. const int ib = tid%8; // 0...7
  2046. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2047. const uint16_t * q2 = x[i].qs + 4*ib;
  2048. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  2049. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  2050. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  2051. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2052. #else
  2053. assert(false);
  2054. #endif
  2055. }
  2056. template<typename dst_t>
  2057. static __global__ void dequantize_block_iq2_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2058. const int i = blockIdx.x;
  2059. const block_iq2_s * x = (const block_iq2_s *) vx;
  2060. const int tid = threadIdx.x;
  2061. #if QK_K == 256
  2062. const int il = tid/8; // 0...3
  2063. const int ib = tid%8; // 0...7
  2064. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2065. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  2066. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  2067. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  2068. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2069. #else
  2070. assert(false);
  2071. #endif
  2072. }
  2073. template<typename dst_t>
  2074. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2075. const int i = blockIdx.x;
  2076. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  2077. const int tid = threadIdx.x;
  2078. #if QK_K == 256
  2079. const int il = tid/8; // 0...3
  2080. const int ib = tid%8; // 0...7
  2081. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2082. const uint8_t * q3 = x[i].qs + 8*ib;
  2083. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  2084. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  2085. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  2086. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  2087. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  2088. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  2089. for (int j = 0; j < 4; ++j) {
  2090. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2091. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2092. }
  2093. #else
  2094. assert(false);
  2095. #endif
  2096. }
  2097. template<typename dst_t>
  2098. static __global__ void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2099. const int i = blockIdx.x;
  2100. const block_iq3_s * x = (const block_iq3_s *) vx;
  2101. const int tid = threadIdx.x;
  2102. #if QK_K == 256
  2103. const int il = tid/8; // 0...3
  2104. const int ib = tid%8; // 0...7
  2105. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2106. const uint8_t * qs = x[i].qs + 8*ib;
  2107. const uint8_t * grid1 = (const uint8_t *)(iq3xs_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  2108. const uint8_t * grid2 = (const uint8_t *)(iq3xs_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  2109. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf)) * 0.5f;
  2110. const uint8_t signs = x[i].signs[4*ib + il];
  2111. for (int j = 0; j < 4; ++j) {
  2112. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2113. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2114. }
  2115. #else
  2116. assert(false);
  2117. #endif
  2118. }
  2119. template<typename dst_t>
  2120. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2121. const int i = blockIdx.x;
  2122. const block_iq1_s * x = (const block_iq1_s *) vx;
  2123. const int tid = threadIdx.x;
  2124. #if QK_K == 256
  2125. const int il = tid/8; // 0...3
  2126. const int ib = tid%8; // 0...7
  2127. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2128. const int i8 = 4*ib+il;
  2129. uint8_t h = x[i].scales[i8/2] >> 4*(i8%2);
  2130. const int8_t * grid = (const int8_t *)(iq1s_grid + (x[i].qs[i8] | ((h & 8) << 5)));
  2131. const float d = (float)x[i].d * (2*(h & 7) + 1);
  2132. for (int j = 0; j < 8; ++j) y[j] = d * grid[j];
  2133. #else
  2134. assert(false);
  2135. #endif
  2136. }
  2137. static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2138. template<typename dst_t>
  2139. static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2140. const int i = blockIdx.x;
  2141. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  2142. const int tid = threadIdx.x;
  2143. const int il = tid/8; // 0...3
  2144. const int ib = tid%8; // 0...7
  2145. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  2146. const uint8_t * q4 = x[ib].qs + 4*il;
  2147. const float d = (float)x[ib].d;
  2148. for (int j = 0; j < 4; ++j) {
  2149. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  2150. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  2151. }
  2152. }
  2153. 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) {
  2154. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2155. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2156. if (row > nrows) return;
  2157. const int num_blocks_per_row = ncols / QK_K;
  2158. const int ib0 = row*num_blocks_per_row;
  2159. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  2160. float tmp = 0; // partial sum for thread in warp
  2161. #if QK_K == 256
  2162. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  2163. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2164. const int step = 16/K_QUANTS_PER_ITERATION;
  2165. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2166. const int in = tid - step*im; // 0...15 or 0...7
  2167. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  2168. const int q_offset = 32*im + l0;
  2169. const int s_offset = 8*im;
  2170. const int y_offset = 128*im + l0;
  2171. uint32_t aux[4];
  2172. const uint8_t * d = (const uint8_t *)aux;
  2173. const uint8_t * m = (const uint8_t *)(aux + 2);
  2174. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2175. const float * y = yy + i * QK_K + y_offset;
  2176. const uint8_t * q = x[i].qs + q_offset;
  2177. const float dall = __low2half(x[i].dm);
  2178. const float dmin = __high2half(x[i].dm);
  2179. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  2180. aux[0] = a[0] & 0x0f0f0f0f;
  2181. aux[1] = a[1] & 0x0f0f0f0f;
  2182. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  2183. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  2184. float sum1 = 0, sum2 = 0;
  2185. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2186. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  2187. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  2188. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  2189. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  2190. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  2191. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  2192. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  2193. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  2194. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  2195. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  2196. }
  2197. tmp += dall * sum1 - dmin * sum2;
  2198. }
  2199. #else
  2200. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  2201. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  2202. const int offset = tid * K_QUANTS_PER_ITERATION;
  2203. uint32_t uaux[2];
  2204. const uint8_t * d = (const uint8_t *)uaux;
  2205. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2206. const float * y = yy + i * QK_K + offset;
  2207. const uint8_t * q = x[i].qs + offset;
  2208. const uint32_t * s = (const uint32_t *)x[i].scales;
  2209. uaux[0] = s[0] & 0x0f0f0f0f;
  2210. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  2211. const float2 dall = __half22float2(x[i].dm);
  2212. float sum1 = 0, sum2 = 0;
  2213. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2214. const uint8_t ql = q[l];
  2215. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  2216. + y[l+16] * d[1] * ((ql >> 2) & 3)
  2217. + y[l+32] * d[2] * ((ql >> 4) & 3)
  2218. + y[l+48] * d[3] * ((ql >> 6) & 3);
  2219. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  2220. }
  2221. tmp += dall.x * sum1 - dall.y * sum2;
  2222. }
  2223. #endif
  2224. // sum up partial sums and write back result
  2225. #pragma unroll
  2226. for (int mask = 16; mask > 0; mask >>= 1) {
  2227. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2228. }
  2229. if (threadIdx.x == 0) {
  2230. dst[row] = tmp;
  2231. }
  2232. }
  2233. 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) {
  2234. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2235. if (row > nrows) return;
  2236. const int num_blocks_per_row = ncols / QK_K;
  2237. const int ib0 = row*num_blocks_per_row;
  2238. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  2239. float tmp = 0; // partial sum for thread in warp
  2240. #if QK_K == 256
  2241. const uint16_t kmask1 = 0x0303;
  2242. const uint16_t kmask2 = 0x0f0f;
  2243. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2244. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2245. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  2246. const int step = 16/K_QUANTS_PER_ITERATION;
  2247. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2248. const int in = tid - step*im; // 0....15 or 0...7
  2249. const uint8_t m = 1 << (4*im);
  2250. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  2251. const int q_offset = 32*im + l0;
  2252. const int y_offset = 128*im + l0;
  2253. uint16_t utmp[4];
  2254. const int8_t * s = (const int8_t *)utmp;
  2255. const uint16_t s_shift = 4*im;
  2256. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2257. const float * y = yy + i * QK_K + y_offset;
  2258. const uint8_t * q = x[i].qs + q_offset;
  2259. const uint8_t * h = x[i].hmask + l0;
  2260. const uint16_t * a = (const uint16_t *)x[i].scales;
  2261. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  2262. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  2263. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  2264. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  2265. const float d = x[i].d;
  2266. float sum = 0;
  2267. for (int l = 0; l < n; ++l) {
  2268. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  2269. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  2270. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  2271. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  2272. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  2273. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  2274. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  2275. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  2276. }
  2277. tmp += d * sum;
  2278. }
  2279. #else
  2280. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  2281. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  2282. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  2283. const int in = offset/8; // 0 or 1
  2284. const int im = offset%8; // 0...7
  2285. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2286. const float * y = yy + i * QK_K + offset;
  2287. const uint8_t * q = x[i].qs + offset;
  2288. const uint8_t * s = x[i].scales;
  2289. const float dall = (float)x[i].d;
  2290. float sum = 0;
  2291. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2292. const uint8_t hl = x[i].hmask[im+l] >> in;
  2293. const uint8_t ql = q[l];
  2294. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  2295. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  2296. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  2297. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  2298. }
  2299. tmp += sum;
  2300. }
  2301. #endif
  2302. // sum up partial sums and write back result
  2303. #pragma unroll
  2304. for (int mask = 16; mask > 0; mask >>= 1) {
  2305. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2306. }
  2307. if (threadIdx.x == 0) {
  2308. dst[row] = tmp;
  2309. }
  2310. }
  2311. 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) {
  2312. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2313. if (row > nrows) return;
  2314. const int num_blocks_per_row = ncols / QK_K;
  2315. const int ib0 = row*num_blocks_per_row;
  2316. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  2317. #if QK_K == 256
  2318. const uint16_t kmask1 = 0x3f3f;
  2319. const uint16_t kmask2 = 0x0f0f;
  2320. const uint16_t kmask3 = 0xc0c0;
  2321. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2322. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2323. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  2324. const int il = tid/step; // 0...3
  2325. const int ir = tid - step*il; // 0...7 or 0...3
  2326. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  2327. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2328. const int in = il%2;
  2329. const int l0 = n*(2*ir + in);
  2330. const int q_offset = 32*im + l0;
  2331. const int y_offset = 64*im + l0;
  2332. uint16_t aux[4];
  2333. const uint8_t * sc = (const uint8_t *)aux;
  2334. #if K_QUANTS_PER_ITERATION == 2
  2335. uint32_t q32[4];
  2336. const uint8_t * q4 = (const uint8_t *)q32;
  2337. #else
  2338. uint16_t q16[4];
  2339. const uint8_t * q4 = (const uint8_t *)q16;
  2340. #endif
  2341. float tmp = 0; // partial sum for thread in warp
  2342. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2343. const float * y1 = yy + i*QK_K + y_offset;
  2344. const float * y2 = y1 + 128;
  2345. const float dall = __low2half(x[i].dm);
  2346. const float dmin = __high2half(x[i].dm);
  2347. const uint16_t * a = (const uint16_t *)x[i].scales;
  2348. aux[0] = a[im+0] & kmask1;
  2349. aux[1] = a[im+2] & kmask1;
  2350. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2351. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2352. #if K_QUANTS_PER_ITERATION == 2
  2353. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  2354. const uint32_t * q2 = q1 + 16;
  2355. q32[0] = q1[0] & 0x0f0f0f0f;
  2356. q32[1] = q1[0] & 0xf0f0f0f0;
  2357. q32[2] = q2[0] & 0x0f0f0f0f;
  2358. q32[3] = q2[0] & 0xf0f0f0f0;
  2359. float4 s = {0.f, 0.f, 0.f, 0.f};
  2360. float smin = 0;
  2361. for (int l = 0; l < 4; ++l) {
  2362. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  2363. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  2364. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  2365. }
  2366. 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;
  2367. #else
  2368. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  2369. const uint16_t * q2 = q1 + 32;
  2370. q16[0] = q1[0] & 0x0f0f;
  2371. q16[1] = q1[0] & 0xf0f0;
  2372. q16[2] = q2[0] & 0x0f0f;
  2373. q16[3] = q2[0] & 0xf0f0;
  2374. float4 s = {0.f, 0.f, 0.f, 0.f};
  2375. float smin = 0;
  2376. for (int l = 0; l < 2; ++l) {
  2377. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  2378. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  2379. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  2380. }
  2381. 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;
  2382. #endif
  2383. }
  2384. #else
  2385. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2386. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2387. const int step = tid * K_QUANTS_PER_ITERATION;
  2388. uint16_t aux16[2];
  2389. const uint8_t * s = (const uint8_t *)aux16;
  2390. float tmp = 0;
  2391. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2392. const uint8_t * q = x[i].qs + step;
  2393. const float * y = yy + i*QK_K + step;
  2394. const uint16_t * a = (const uint16_t *)x[i].scales;
  2395. aux16[0] = a[0] & 0x0f0f;
  2396. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2397. const float d = (float)x[i].dm[0];
  2398. const float m = (float)x[i].dm[1];
  2399. float sum = 0.f;
  2400. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2401. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  2402. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  2403. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  2404. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  2405. }
  2406. tmp += sum;
  2407. }
  2408. #endif
  2409. // sum up partial sums and write back result
  2410. #pragma unroll
  2411. for (int mask = 16; mask > 0; mask >>= 1) {
  2412. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2413. }
  2414. if (tid == 0) {
  2415. dst[row] = tmp;
  2416. }
  2417. }
  2418. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  2419. const int row = blockIdx.x;
  2420. const int num_blocks_per_row = ncols / QK_K;
  2421. const int ib0 = row*num_blocks_per_row;
  2422. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  2423. float tmp = 0; // partial sum for thread in warp
  2424. #if QK_K == 256
  2425. const uint16_t kmask1 = 0x3f3f;
  2426. const uint16_t kmask2 = 0x0f0f;
  2427. const uint16_t kmask3 = 0xc0c0;
  2428. const int tid = threadIdx.x/2; // 0...15
  2429. const int ix = threadIdx.x%2;
  2430. const int il = tid/4; // 0...3
  2431. const int ir = tid - 4*il;// 0...3
  2432. const int n = 2;
  2433. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2434. const int in = il%2;
  2435. const int l0 = n*(2*ir + in);
  2436. const int q_offset = 32*im + l0;
  2437. const int y_offset = 64*im + l0;
  2438. const uint8_t hm1 = 1 << (2*im);
  2439. const uint8_t hm2 = hm1 << 4;
  2440. uint16_t aux[4];
  2441. const uint8_t * sc = (const uint8_t *)aux;
  2442. uint16_t q16[8];
  2443. const uint8_t * q4 = (const uint8_t *)q16;
  2444. for (int i = ix; i < num_blocks_per_row; i += 2) {
  2445. const uint8_t * ql1 = x[i].qs + q_offset;
  2446. const uint8_t * qh = x[i].qh + l0;
  2447. const float * y1 = yy + i*QK_K + y_offset;
  2448. const float * y2 = y1 + 128;
  2449. const float dall = __low2half(x[i].dm);
  2450. const float dmin = __high2half(x[i].dm);
  2451. const uint16_t * a = (const uint16_t *)x[i].scales;
  2452. aux[0] = a[im+0] & kmask1;
  2453. aux[1] = a[im+2] & kmask1;
  2454. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2455. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2456. float4 sum = {0.f, 0.f, 0.f, 0.f};
  2457. float smin = 0;
  2458. const uint16_t * q1 = (const uint16_t *)ql1;
  2459. const uint16_t * q2 = q1 + 32;
  2460. q16[0] = q1[0] & 0x0f0f;
  2461. q16[1] = q1[8] & 0x0f0f;
  2462. q16[2] = (q1[0] >> 4) & 0x0f0f;
  2463. q16[3] = (q1[8] >> 4) & 0x0f0f;
  2464. q16[4] = q2[0] & 0x0f0f;
  2465. q16[5] = q2[8] & 0x0f0f;
  2466. q16[6] = (q2[0] >> 4) & 0x0f0f;
  2467. q16[7] = (q2[8] >> 4) & 0x0f0f;
  2468. for (int l = 0; l < n; ++l) {
  2469. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  2470. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  2471. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  2472. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  2473. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  2474. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  2475. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  2476. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  2477. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  2478. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  2479. }
  2480. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  2481. }
  2482. #else
  2483. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2484. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2485. const int step = tid * K_QUANTS_PER_ITERATION;
  2486. const int im = step/8;
  2487. const int in = step%8;
  2488. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2489. const uint8_t * q = x[i].qs + step;
  2490. const int8_t * s = x[i].scales;
  2491. const float * y = yy + i*QK_K + step;
  2492. const float d = x[i].d;
  2493. float sum = 0.f;
  2494. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2495. const uint8_t h = x[i].qh[in+j] >> im;
  2496. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  2497. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  2498. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  2499. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  2500. }
  2501. tmp += sum;
  2502. }
  2503. #endif
  2504. // sum up partial sums and write back result
  2505. #pragma unroll
  2506. for (int mask = 16; mask > 0; mask >>= 1) {
  2507. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2508. }
  2509. if (threadIdx.x == 0) {
  2510. dst[row] = tmp;
  2511. }
  2512. }
  2513. 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) {
  2514. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2515. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2516. if (row > nrows) return;
  2517. const int num_blocks_per_row = ncols / QK_K;
  2518. const int ib0 = row*num_blocks_per_row;
  2519. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  2520. #if QK_K == 256
  2521. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2522. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  2523. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  2524. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2525. const int in = tid - step*im; // 0...15 or 0...7
  2526. #if K_QUANTS_PER_ITERATION == 1
  2527. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  2528. const int is = 0;
  2529. #else
  2530. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  2531. const int is = in / 4;
  2532. #endif
  2533. const int ql_offset = 64*im + l0;
  2534. const int qh_offset = 32*im + l0;
  2535. const int s_offset = 8*im + is;
  2536. const int y_offset = 128*im + l0;
  2537. float tmp = 0; // partial sum for thread in warp
  2538. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2539. const float * y = yy + i * QK_K + y_offset;
  2540. const uint8_t * ql = x[i].ql + ql_offset;
  2541. const uint8_t * qh = x[i].qh + qh_offset;
  2542. const int8_t * s = x[i].scales + s_offset;
  2543. const float d = x[i].d;
  2544. #if K_QUANTS_PER_ITERATION == 1
  2545. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  2546. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  2547. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  2548. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  2549. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  2550. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  2551. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  2552. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  2553. tmp += sum;
  2554. #else
  2555. float sum = 0;
  2556. for (int l = 0; l < 4; ++l) {
  2557. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  2558. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  2559. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  2560. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  2561. }
  2562. tmp += sum;
  2563. #endif
  2564. }
  2565. #else
  2566. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  2567. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  2568. const int step = tid * K_QUANTS_PER_ITERATION;
  2569. float tmp = 0; // partial sum for thread in warp
  2570. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2571. const float * y = yy + i * QK_K + step;
  2572. const uint8_t * ql = x[i].ql + step;
  2573. const uint8_t * qh = x[i].qh + step;
  2574. const int8_t * s = x[i].scales;
  2575. const float d = x[i+0].d;
  2576. float sum = 0;
  2577. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2578. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  2579. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  2580. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  2581. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  2582. }
  2583. tmp += sum;
  2584. }
  2585. #endif
  2586. // sum up partial sums and write back result
  2587. #pragma unroll
  2588. for (int mask = 16; mask > 0; mask >>= 1) {
  2589. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2590. }
  2591. if (tid == 0) {
  2592. dst[row] = tmp;
  2593. }
  2594. }
  2595. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  2596. const half * x = (const half *) vx;
  2597. // automatic half -> float type cast if dfloat == float
  2598. v.x = x[ib + iqs + 0];
  2599. v.y = x[ib + iqs + 1];
  2600. }
  2601. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  2602. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  2603. if (ix >= kx_padded) {
  2604. return;
  2605. }
  2606. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  2607. const int i_padded = iy*kx_padded + ix;
  2608. block_q8_1 * y = (block_q8_1 *) vy;
  2609. const int ib = i_padded / QK8_1; // block index
  2610. const int iqs = i_padded % QK8_1; // quant index
  2611. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  2612. float amax = fabsf(xi);
  2613. float sum = xi;
  2614. #pragma unroll
  2615. for (int mask = 16; mask > 0; mask >>= 1) {
  2616. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  2617. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  2618. }
  2619. const float d = amax / 127;
  2620. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  2621. y[ib].qs[iqs] = q;
  2622. if (iqs > 0) {
  2623. return;
  2624. }
  2625. reinterpret_cast<half&>(y[ib].ds.x) = d;
  2626. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  2627. }
  2628. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2629. static __global__ void k_get_rows(
  2630. const void * src0, const int32_t * src1, dst_t * dst,
  2631. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2632. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2633. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2634. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2635. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2636. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  2637. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2638. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2639. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2640. if (i00 >= ne00) {
  2641. return;
  2642. }
  2643. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2644. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2645. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  2646. const int ib = i00/qk; // block index
  2647. const int iqs = (i00%qk)/qr; // quant index
  2648. const int iybs = i00 - i00%qk; // dst block start index
  2649. const int y_offset = qr == 1 ? 1 : qk/2;
  2650. // dequantize
  2651. dfloat2 v;
  2652. dequantize_kernel(src0_row, ib, iqs, v);
  2653. dst_row[iybs + iqs + 0] = v.x;
  2654. dst_row[iybs + iqs + y_offset] = v.y;
  2655. }
  2656. template<typename src0_t, typename dst_t>
  2657. static __global__ void k_get_rows_float(
  2658. const src0_t * src0, const int32_t * src1, dst_t * dst,
  2659. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2660. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2661. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2662. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2663. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2664. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2665. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2666. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2667. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2668. if (i00 >= ne00) {
  2669. return;
  2670. }
  2671. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2672. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2673. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2674. dst_row[i00] = src0_row[i00];
  2675. }
  2676. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2677. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2678. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2679. if (i >= k) {
  2680. return;
  2681. }
  2682. const int ib = i/qk; // block index
  2683. const int iqs = (i%qk)/qr; // quant index
  2684. const int iybs = i - i%qk; // y block start index
  2685. const int y_offset = qr == 1 ? 1 : qk/2;
  2686. // dequantize
  2687. dfloat2 v;
  2688. dequantize_kernel(vx, ib, iqs, v);
  2689. y[iybs + iqs + 0] = v.x;
  2690. y[iybs + iqs + y_offset] = v.y;
  2691. }
  2692. template <typename src_t, typename dst_t>
  2693. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2694. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2695. if (i >= k) {
  2696. return;
  2697. }
  2698. const src_t * x = (src_t *) vx;
  2699. y[i] = x[i];
  2700. }
  2701. template <bool need_check>
  2702. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2703. #if __CUDA_ARCH__ >= CC_PASCAL
  2704. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2705. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2706. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2707. half2 * y2 = (half2 *) (y + i0);
  2708. __shared__ int vals[nint];
  2709. #pragma unroll
  2710. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2711. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2712. break;
  2713. }
  2714. const int ix = ix0 + threadIdx.x;
  2715. vals[ix] = x0[ix];
  2716. }
  2717. #pragma unroll
  2718. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2719. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2720. return;
  2721. }
  2722. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2723. const half d = *b0;
  2724. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2725. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2726. }
  2727. #else
  2728. (void) vx; (void) y; (void) k;
  2729. NO_DEVICE_CODE;
  2730. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2731. }
  2732. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2733. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2734. #define VDR_Q4_0_Q8_1_MMVQ 2
  2735. #define VDR_Q4_0_Q8_1_MMQ 4
  2736. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2737. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2738. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2739. int sumi = 0;
  2740. #pragma unroll
  2741. for (int i = 0; i < vdr; ++i) {
  2742. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2743. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2744. // SIMD dot product of quantized values
  2745. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2746. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2747. }
  2748. const float2 ds8f = __half22float2(ds8);
  2749. // second part effectively subtracts 8 from each quant value
  2750. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2751. #else
  2752. NO_DEVICE_CODE;
  2753. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2754. }
  2755. #define VDR_Q4_1_Q8_1_MMVQ 2
  2756. #define VDR_Q4_1_Q8_1_MMQ 4
  2757. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2758. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2759. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2760. int sumi = 0;
  2761. #pragma unroll
  2762. for (int i = 0; i < vdr; ++i) {
  2763. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2764. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2765. // SIMD dot product of quantized values
  2766. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2767. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2768. }
  2769. #ifdef GGML_CUDA_F16
  2770. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2771. const float d4d8 = tmp.x;
  2772. const float m4s8 = tmp.y;
  2773. #else
  2774. const float2 dm4f = __half22float2(dm4);
  2775. const float2 ds8f = __half22float2(ds8);
  2776. const float d4d8 = dm4f.x * ds8f.x;
  2777. const float m4s8 = dm4f.y * ds8f.y;
  2778. #endif // GGML_CUDA_F16
  2779. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2780. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2781. #else
  2782. NO_DEVICE_CODE;
  2783. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2784. }
  2785. #define VDR_Q5_0_Q8_1_MMVQ 2
  2786. #define VDR_Q5_0_Q8_1_MMQ 4
  2787. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2788. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2789. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2790. int sumi = 0;
  2791. #pragma unroll
  2792. for (int i = 0; i < vdr; ++i) {
  2793. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2794. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2795. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2796. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2797. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2798. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2799. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2800. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2801. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2802. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2803. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2804. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2805. }
  2806. const float2 ds8f = __half22float2(ds8);
  2807. // second part effectively subtracts 16 from each quant value
  2808. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2809. #else
  2810. NO_DEVICE_CODE;
  2811. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2812. }
  2813. #define VDR_Q5_1_Q8_1_MMVQ 2
  2814. #define VDR_Q5_1_Q8_1_MMQ 4
  2815. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2816. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2817. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2818. int sumi = 0;
  2819. #pragma unroll
  2820. for (int i = 0; i < vdr; ++i) {
  2821. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2822. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2823. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2824. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2825. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2826. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2827. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2828. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2829. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2830. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2831. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2832. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2833. }
  2834. #ifdef GGML_CUDA_F16
  2835. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2836. const float d5d8 = tmp.x;
  2837. const float m5s8 = tmp.y;
  2838. #else
  2839. const float2 dm5f = __half22float2(dm5);
  2840. const float2 ds8f = __half22float2(ds8);
  2841. const float d5d8 = dm5f.x * ds8f.x;
  2842. const float m5s8 = dm5f.y * ds8f.y;
  2843. #endif // GGML_CUDA_F16
  2844. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2845. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2846. #else
  2847. NO_DEVICE_CODE;
  2848. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2849. }
  2850. #define VDR_Q8_0_Q8_1_MMVQ 2
  2851. #define VDR_Q8_0_Q8_1_MMQ 8
  2852. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2853. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2854. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2855. int sumi = 0;
  2856. #pragma unroll
  2857. for (int i = 0; i < vdr; ++i) {
  2858. // SIMD dot product of quantized values
  2859. sumi = __dp4a(v[i], u[i], sumi);
  2860. }
  2861. return d8_0*d8_1 * sumi;
  2862. #else
  2863. NO_DEVICE_CODE;
  2864. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2865. }
  2866. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2867. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2868. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2869. int sumi = 0;
  2870. #pragma unroll
  2871. for (int i = 0; i < vdr; ++i) {
  2872. // SIMD dot product of quantized values
  2873. sumi = __dp4a(v[i], u[i], sumi);
  2874. }
  2875. #ifdef GGML_CUDA_F16
  2876. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2877. const float d8d8 = tmp.x;
  2878. const float m8s8 = tmp.y;
  2879. #else
  2880. const float2 dm8f = __half22float2(dm8);
  2881. const float2 ds8f = __half22float2(ds8);
  2882. const float d8d8 = dm8f.x * ds8f.x;
  2883. const float m8s8 = dm8f.y * ds8f.y;
  2884. #endif // GGML_CUDA_F16
  2885. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2886. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2887. #else
  2888. NO_DEVICE_CODE;
  2889. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2890. }
  2891. #define VDR_Q2_K_Q8_1_MMVQ 1
  2892. #define VDR_Q2_K_Q8_1_MMQ 2
  2893. // contiguous v/x values
  2894. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2895. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2896. const half2 & dm2, const float * __restrict__ d8) {
  2897. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2898. float sumf_d = 0.0f;
  2899. float sumf_m = 0.0f;
  2900. #pragma unroll
  2901. for (int i = 0; i < QR2_K; ++i) {
  2902. const int sc = scales[2*i];
  2903. const int vi = (v >> (2*i)) & 0x03030303;
  2904. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2905. // fill int with 4x m
  2906. int m = sc >> 4;
  2907. m |= m << 8;
  2908. m |= m << 16;
  2909. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2910. }
  2911. const float2 dm2f = __half22float2(dm2);
  2912. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2913. #else
  2914. NO_DEVICE_CODE;
  2915. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2916. }
  2917. // contiguous u/y values
  2918. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2919. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2920. const half2 & dm2, const float & d8) {
  2921. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2922. int sumi_d = 0;
  2923. int sumi_m = 0;
  2924. #pragma unroll
  2925. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2926. int sumi_d_sc = 0;
  2927. const int sc = scales[i0 / (QI8_1/2)];
  2928. // fill int with 4x m
  2929. int m = sc >> 4;
  2930. m |= m << 8;
  2931. m |= m << 16;
  2932. #pragma unroll
  2933. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2934. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2935. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2936. }
  2937. sumi_d += sumi_d_sc * (sc & 0xF);
  2938. }
  2939. const float2 dm2f = __half22float2(dm2);
  2940. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2941. #else
  2942. NO_DEVICE_CODE;
  2943. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2944. }
  2945. #define VDR_Q3_K_Q8_1_MMVQ 1
  2946. #define VDR_Q3_K_Q8_1_MMQ 2
  2947. // contiguous v/x values
  2948. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2949. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2950. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2951. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2952. float sumf = 0.0f;
  2953. #pragma unroll
  2954. for (int i = 0; i < QR3_K; ++i) {
  2955. const int isc = scale_offset + 2*i;
  2956. const int isc_low = isc % (QK_K/32);
  2957. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2958. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2959. const int isc_high = isc % (QK_K/64);
  2960. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2961. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2962. const int sc = (sc_low | sc_high) - 32;
  2963. const int vil = (vl >> (2*i)) & 0x03030303;
  2964. const int vih = ((vh >> i) << 2) & 0x04040404;
  2965. const int vi = __vsubss4(vil, vih);
  2966. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2967. }
  2968. return d3 * sumf;
  2969. #else
  2970. NO_DEVICE_CODE;
  2971. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2972. }
  2973. // contiguous u/y values
  2974. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2975. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2976. const float & d3, const float & d8) {
  2977. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2978. int sumi = 0;
  2979. #pragma unroll
  2980. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2981. int sumi_sc = 0;
  2982. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2983. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2984. }
  2985. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2986. }
  2987. return d3*d8 * sumi;
  2988. #else
  2989. NO_DEVICE_CODE;
  2990. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2991. }
  2992. #define VDR_Q4_K_Q8_1_MMVQ 2
  2993. #define VDR_Q4_K_Q8_1_MMQ 8
  2994. // contiguous v/x values
  2995. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2996. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2997. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2998. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2999. float sumf_d = 0.0f;
  3000. float sumf_m = 0.0f;
  3001. #pragma unroll
  3002. for (int i = 0; i < QR4_K; ++i) {
  3003. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  3004. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  3005. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  3006. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  3007. sumf_d += d8[i] * (dot1 * sc[i]);
  3008. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  3009. }
  3010. const float2 dm4f = __half22float2(dm4);
  3011. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3012. #else
  3013. NO_DEVICE_CODE;
  3014. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3015. }
  3016. // contiguous u/y values
  3017. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  3018. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3019. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  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*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  3025. int sumi_d = 0;
  3026. #pragma unroll
  3027. for (int j = 0; j < QI8_1; ++j) {
  3028. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  3029. }
  3030. const float2 ds8f = __half22float2(ds8[i]);
  3031. sumf_d += ds8f.x * (sc[i] * sumi_d);
  3032. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  3033. }
  3034. const float2 dm4f = __half22float2(dm4);
  3035. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3036. #else
  3037. NO_DEVICE_CODE;
  3038. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3039. }
  3040. #define VDR_Q5_K_Q8_1_MMVQ 2
  3041. #define VDR_Q5_K_Q8_1_MMQ 8
  3042. // contiguous v/x values
  3043. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  3044. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3045. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  3046. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3047. float sumf_d = 0.0f;
  3048. float sumf_m = 0.0f;
  3049. #pragma unroll
  3050. for (int i = 0; i < QR5_K; ++i) {
  3051. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  3052. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  3053. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  3054. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  3055. const int v0i = vl0i | vh0i;
  3056. const int v1i = vl1i | vh1i;
  3057. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  3058. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  3059. sumf_d += d8[i] * (dot1 * sc[i]);
  3060. sumf_m += d8[i] * (dot2 * m[i]);
  3061. }
  3062. const float2 dm5f = __half22float2(dm5);
  3063. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  3064. #else
  3065. NO_DEVICE_CODE;
  3066. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3067. }
  3068. // contiguous u/y values
  3069. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  3070. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3071. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  3072. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3073. float sumf_d = 0.0f;
  3074. float sumf_m = 0.0f;
  3075. #pragma unroll
  3076. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  3077. int sumi_d = 0;
  3078. #pragma unroll
  3079. for (int j = 0; j < QI8_1; ++j) {
  3080. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  3081. }
  3082. const float2 ds8f = __half22float2(ds8[i]);
  3083. sumf_d += ds8f.x * (sc[i] * sumi_d);
  3084. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  3085. }
  3086. const float2 dm4f = __half22float2(dm4);
  3087. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3088. #else
  3089. NO_DEVICE_CODE;
  3090. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3091. }
  3092. #define VDR_Q6_K_Q8_1_MMVQ 1
  3093. #define VDR_Q6_K_Q8_1_MMQ 8
  3094. // contiguous v/x values
  3095. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  3096. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  3097. const float & d, const float * __restrict__ d8) {
  3098. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3099. float sumf = 0.0f;
  3100. #pragma unroll
  3101. for (int i = 0; i < QR6_K; ++i) {
  3102. const int sc = scales[4*i];
  3103. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  3104. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  3105. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  3106. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  3107. }
  3108. return d*sumf;
  3109. #else
  3110. NO_DEVICE_CODE;
  3111. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3112. }
  3113. // contiguous u/y values
  3114. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  3115. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  3116. const float & d6, const float * __restrict__ d8) {
  3117. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3118. float sumf_d = 0.0f;
  3119. #pragma unroll
  3120. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  3121. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  3122. #pragma unroll
  3123. for (int i = i0; i < i0 + 2; ++i) {
  3124. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  3125. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  3126. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  3127. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  3128. }
  3129. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  3130. }
  3131. return d6 * sumf_d;
  3132. #else
  3133. NO_DEVICE_CODE;
  3134. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3135. }
  3136. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  3137. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3138. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  3139. int v[VDR_Q4_0_Q8_1_MMVQ];
  3140. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  3141. #pragma unroll
  3142. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  3143. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  3144. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3145. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  3146. }
  3147. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  3148. }
  3149. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3150. (void)x_qh; (void)x_sc;
  3151. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3152. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  3153. *x_ql = tile_x_qs;
  3154. *x_dm = (half2 *) tile_x_d;
  3155. }
  3156. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  3157. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3158. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3159. (void)x_qh; (void)x_sc;
  3160. GGML_CUDA_ASSUME(i_offset >= 0);
  3161. GGML_CUDA_ASSUME(i_offset < nwarps);
  3162. GGML_CUDA_ASSUME(k >= 0);
  3163. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3164. const int kbx = k / QI4_0;
  3165. const int kqsx = k % QI4_0;
  3166. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  3167. float * x_dmf = (float *) x_dm;
  3168. #pragma unroll
  3169. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3170. int i = i0 + i_offset;
  3171. if (need_check) {
  3172. i = min(i, i_max);
  3173. }
  3174. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3175. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3176. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  3177. }
  3178. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  3179. const int kbxd = k % blocks_per_tile_x_row;
  3180. #pragma unroll
  3181. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  3182. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  3183. if (need_check) {
  3184. i = min(i, i_max);
  3185. }
  3186. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3187. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  3188. }
  3189. }
  3190. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  3191. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3192. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3193. (void)x_qh; (void)x_sc;
  3194. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3195. const float * x_dmf = (const float *) x_dm;
  3196. int u[2*VDR_Q4_0_Q8_1_MMQ];
  3197. #pragma unroll
  3198. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  3199. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3200. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  3201. }
  3202. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  3203. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  3204. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  3205. }
  3206. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  3207. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3208. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  3209. int v[VDR_Q4_1_Q8_1_MMVQ];
  3210. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  3211. #pragma unroll
  3212. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  3213. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  3214. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3215. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  3216. }
  3217. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  3218. }
  3219. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3220. (void)x_qh; (void)x_sc;
  3221. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  3222. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  3223. *x_ql = tile_x_qs;
  3224. *x_dm = tile_x_dm;
  3225. }
  3226. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  3227. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3228. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3229. (void)x_qh; (void)x_sc;
  3230. GGML_CUDA_ASSUME(i_offset >= 0);
  3231. GGML_CUDA_ASSUME(i_offset < nwarps);
  3232. GGML_CUDA_ASSUME(k >= 0);
  3233. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3234. const int kbx = k / QI4_1;
  3235. const int kqsx = k % QI4_1;
  3236. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  3237. #pragma unroll
  3238. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3239. int i = i0 + i_offset;
  3240. if (need_check) {
  3241. i = min(i, i_max);
  3242. }
  3243. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3244. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3245. }
  3246. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  3247. const int kbxd = k % blocks_per_tile_x_row;
  3248. #pragma unroll
  3249. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  3250. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  3251. if (need_check) {
  3252. i = min(i, i_max);
  3253. }
  3254. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3255. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  3256. }
  3257. }
  3258. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  3259. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3260. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3261. (void)x_qh; (void)x_sc;
  3262. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3263. int u[2*VDR_Q4_1_Q8_1_MMQ];
  3264. #pragma unroll
  3265. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  3266. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3267. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  3268. }
  3269. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  3270. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  3271. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  3272. }
  3273. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  3274. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3275. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  3276. int vl[VDR_Q5_0_Q8_1_MMVQ];
  3277. int vh[VDR_Q5_0_Q8_1_MMVQ];
  3278. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  3279. #pragma unroll
  3280. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  3281. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  3282. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  3283. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3284. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  3285. }
  3286. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  3287. }
  3288. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3289. (void)x_qh; (void)x_sc;
  3290. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3291. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  3292. *x_ql = tile_x_ql;
  3293. *x_dm = (half2 *) tile_x_d;
  3294. }
  3295. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  3296. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3297. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3298. (void)x_qh; (void)x_sc;
  3299. GGML_CUDA_ASSUME(i_offset >= 0);
  3300. GGML_CUDA_ASSUME(i_offset < nwarps);
  3301. GGML_CUDA_ASSUME(k >= 0);
  3302. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3303. const int kbx = k / QI5_0;
  3304. const int kqsx = k % QI5_0;
  3305. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  3306. #pragma unroll
  3307. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3308. int i = i0 + i_offset;
  3309. if (need_check) {
  3310. i = min(i, i_max);
  3311. }
  3312. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3313. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  3314. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  3315. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3316. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3317. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3318. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3319. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3320. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  3321. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3322. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3323. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3324. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3325. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3326. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3327. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  3328. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3329. }
  3330. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  3331. const int kbxd = k % blocks_per_tile_x_row;
  3332. float * x_dmf = (float *) x_dm;
  3333. #pragma unroll
  3334. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  3335. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  3336. if (need_check) {
  3337. i = min(i, i_max);
  3338. }
  3339. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3340. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  3341. }
  3342. }
  3343. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  3344. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3345. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3346. (void)x_qh; (void)x_sc;
  3347. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3348. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  3349. const float * x_dmf = (const float *) x_dm;
  3350. const float * y_df = (const float *) y_ds;
  3351. int u[2*VDR_Q5_0_Q8_1_MMQ];
  3352. #pragma unroll
  3353. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  3354. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3355. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  3356. }
  3357. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  3358. (&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)]);
  3359. }
  3360. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  3361. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3362. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  3363. int vl[VDR_Q5_1_Q8_1_MMVQ];
  3364. int vh[VDR_Q5_1_Q8_1_MMVQ];
  3365. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  3366. #pragma unroll
  3367. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  3368. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  3369. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  3370. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3371. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  3372. }
  3373. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  3374. }
  3375. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3376. (void)x_qh; (void)x_sc;
  3377. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3378. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  3379. *x_ql = tile_x_ql;
  3380. *x_dm = tile_x_dm;
  3381. }
  3382. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  3383. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3384. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3385. (void)x_qh; (void)x_sc;
  3386. GGML_CUDA_ASSUME(i_offset >= 0);
  3387. GGML_CUDA_ASSUME(i_offset < nwarps);
  3388. GGML_CUDA_ASSUME(k >= 0);
  3389. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3390. const int kbx = k / QI5_1;
  3391. const int kqsx = k % QI5_1;
  3392. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  3393. #pragma unroll
  3394. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3395. int i = i0 + i_offset;
  3396. if (need_check) {
  3397. i = min(i, i_max);
  3398. }
  3399. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3400. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3401. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  3402. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3403. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3404. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3405. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3406. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3407. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3408. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3409. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3410. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3411. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3412. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3413. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3414. }
  3415. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  3416. const int kbxd = k % blocks_per_tile_x_row;
  3417. #pragma unroll
  3418. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  3419. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  3420. if (need_check) {
  3421. i = min(i, i_max);
  3422. }
  3423. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3424. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  3425. }
  3426. }
  3427. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  3428. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3429. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3430. (void)x_qh; (void)x_sc;
  3431. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3432. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  3433. int u[2*VDR_Q5_1_Q8_1_MMQ];
  3434. #pragma unroll
  3435. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  3436. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3437. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  3438. }
  3439. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  3440. (&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)]);
  3441. }
  3442. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  3443. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3444. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  3445. int v[VDR_Q8_0_Q8_1_MMVQ];
  3446. int u[VDR_Q8_0_Q8_1_MMVQ];
  3447. #pragma unroll
  3448. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  3449. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  3450. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3451. }
  3452. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  3453. }
  3454. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3455. (void)x_qh; (void)x_sc;
  3456. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3457. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  3458. *x_ql = tile_x_qs;
  3459. *x_dm = (half2 *) tile_x_d;
  3460. }
  3461. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  3462. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3463. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3464. (void)x_qh; (void)x_sc;
  3465. GGML_CUDA_ASSUME(i_offset >= 0);
  3466. GGML_CUDA_ASSUME(i_offset < nwarps);
  3467. GGML_CUDA_ASSUME(k >= 0);
  3468. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3469. const int kbx = k / QI8_0;
  3470. const int kqsx = k % QI8_0;
  3471. float * x_dmf = (float *) x_dm;
  3472. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  3473. #pragma unroll
  3474. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3475. int i = i0 + i_offset;
  3476. if (need_check) {
  3477. i = min(i, i_max);
  3478. }
  3479. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3480. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  3481. }
  3482. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  3483. const int kbxd = k % blocks_per_tile_x_row;
  3484. #pragma unroll
  3485. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  3486. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  3487. if (need_check) {
  3488. i = min(i, i_max);
  3489. }
  3490. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3491. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  3492. }
  3493. }
  3494. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  3495. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3496. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3497. (void)x_qh; (void)x_sc;
  3498. const float * x_dmf = (const float *) x_dm;
  3499. const float * y_df = (const float *) y_ds;
  3500. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  3501. (&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],
  3502. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  3503. }
  3504. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  3505. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3506. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  3507. const int bq8_offset = QR2_K * (iqs / QI8_1);
  3508. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3509. const uint8_t * scales = bq2_K->scales + scale_offset;
  3510. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  3511. int u[QR2_K];
  3512. float d8[QR2_K];
  3513. #pragma unroll
  3514. for (int i = 0; i < QR2_K; ++ i) {
  3515. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3516. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3517. }
  3518. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  3519. }
  3520. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3521. (void)x_qh;
  3522. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3523. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  3524. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3525. *x_ql = tile_x_ql;
  3526. *x_dm = tile_x_dm;
  3527. *x_sc = tile_x_sc;
  3528. }
  3529. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  3530. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3531. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3532. (void)x_qh;
  3533. GGML_CUDA_ASSUME(i_offset >= 0);
  3534. GGML_CUDA_ASSUME(i_offset < nwarps);
  3535. GGML_CUDA_ASSUME(k >= 0);
  3536. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3537. const int kbx = k / QI2_K;
  3538. const int kqsx = k % QI2_K;
  3539. const block_q2_K * bx0 = (const block_q2_K *) vx;
  3540. #pragma unroll
  3541. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3542. int i = i0 + i_offset;
  3543. if (need_check) {
  3544. i = min(i, i_max);
  3545. }
  3546. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  3547. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3548. }
  3549. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  3550. const int kbxd = k % blocks_per_tile_x_row;
  3551. #pragma unroll
  3552. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  3553. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  3554. if (need_check) {
  3555. i = min(i, i_max);
  3556. }
  3557. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3558. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  3559. }
  3560. #pragma unroll
  3561. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3562. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3563. if (need_check) {
  3564. i = min(i, i_max);
  3565. }
  3566. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  3567. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  3568. }
  3569. }
  3570. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  3571. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3572. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3573. (void)x_qh;
  3574. const int kbx = k / QI2_K;
  3575. const int ky = (k % QI2_K) * QR2_K;
  3576. const float * y_df = (const float *) y_ds;
  3577. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  3578. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  3579. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  3580. #pragma unroll
  3581. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  3582. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3583. }
  3584. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  3585. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  3586. 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]);
  3587. }
  3588. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  3589. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3590. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  3591. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  3592. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3593. const float d = bq3_K->d;
  3594. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  3595. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3596. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  3597. int u[QR3_K];
  3598. float d8[QR3_K];
  3599. #pragma unroll
  3600. for (int i = 0; i < QR3_K; ++i) {
  3601. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3602. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3603. }
  3604. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  3605. }
  3606. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3607. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3608. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  3609. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  3610. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3611. *x_ql = tile_x_ql;
  3612. *x_dm = tile_x_dm;
  3613. *x_qh = tile_x_qh;
  3614. *x_sc = tile_x_sc;
  3615. }
  3616. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  3617. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3618. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3619. GGML_CUDA_ASSUME(i_offset >= 0);
  3620. GGML_CUDA_ASSUME(i_offset < nwarps);
  3621. GGML_CUDA_ASSUME(k >= 0);
  3622. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3623. const int kbx = k / QI3_K;
  3624. const int kqsx = k % QI3_K;
  3625. const block_q3_K * bx0 = (const block_q3_K *) vx;
  3626. #pragma unroll
  3627. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3628. int i = i0 + i_offset;
  3629. if (need_check) {
  3630. i = min(i, i_max);
  3631. }
  3632. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  3633. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3634. }
  3635. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  3636. const int kbxd = k % blocks_per_tile_x_row;
  3637. float * x_dmf = (float *) x_dm;
  3638. #pragma unroll
  3639. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  3640. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  3641. if (need_check) {
  3642. i = min(i, i_max);
  3643. }
  3644. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3645. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  3646. }
  3647. #pragma unroll
  3648. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  3649. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  3650. if (need_check) {
  3651. i = min(i, i_max);
  3652. }
  3653. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  3654. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3655. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  3656. }
  3657. #pragma unroll
  3658. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3659. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3660. if (need_check) {
  3661. i = min(i, i_max);
  3662. }
  3663. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  3664. const int ksc = k % (QI3_K/4);
  3665. const int ksc_low = ksc % (QI3_K/8);
  3666. const int shift_low = 4 * (ksc / (QI3_K/8));
  3667. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3668. const int ksc_high = QI3_K/8;
  3669. const int shift_high = 2 * ksc;
  3670. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3671. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3672. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3673. }
  3674. }
  3675. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3676. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3677. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3678. const int kbx = k / QI3_K;
  3679. const int ky = (k % QI3_K) * QR3_K;
  3680. const float * x_dmf = (const float *) x_dm;
  3681. const float * y_df = (const float *) y_ds;
  3682. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3683. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3684. #pragma unroll
  3685. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3686. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3687. const int shift = 2 * ((ky % 32) / 8);
  3688. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3689. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3690. const int vlh = (vh << 2) & 0x04040404;
  3691. v[l] = __vsubss4(vll, vlh);
  3692. }
  3693. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3694. 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]);
  3695. }
  3696. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3697. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3698. #ifndef GGML_QKK_64
  3699. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3700. int v[2];
  3701. int u[2*QR4_K];
  3702. float d8[QR4_K];
  3703. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3704. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3705. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3706. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3707. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3708. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3709. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3710. v[0] = q4[0];
  3711. v[1] = q4[4];
  3712. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3713. uint16_t aux[2];
  3714. const int j = bq8_offset/2;
  3715. if (j < 2) {
  3716. aux[0] = scales[j+0] & 0x3f3f;
  3717. aux[1] = scales[j+2] & 0x3f3f;
  3718. } else {
  3719. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3720. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3721. }
  3722. const uint8_t * sc = (const uint8_t *)aux;
  3723. const uint8_t * m = sc + 2;
  3724. for (int i = 0; i < QR4_K; ++i) {
  3725. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3726. d8[i] = __low2half(bq8i->ds);
  3727. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3728. u[2*i+0] = q8[0];
  3729. u[2*i+1] = q8[4];
  3730. }
  3731. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3732. #else
  3733. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3734. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3735. float sumf_d = 0.0f;
  3736. float sumf_m = 0.0f;
  3737. uint16_t aux16[2];
  3738. const uint8_t * s = (const uint8_t *)aux16;
  3739. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3740. aux16[0] = a[0] & 0x0f0f;
  3741. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3742. const float dall = bq4_K->dm[0];
  3743. const float dmin = bq4_K->dm[1];
  3744. const float d8_1 = __low2float(bq8_1[0].ds);
  3745. const float d8_2 = __low2float(bq8_1[1].ds);
  3746. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3747. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3748. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3749. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3750. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3751. const int v1 = q4[0];
  3752. const int v2 = q4[4];
  3753. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3754. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3755. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3756. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3757. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3758. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3759. return dall * sumf_d - dmin * sumf_m;
  3760. #else
  3761. NO_DEVICE_CODE;
  3762. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3763. #endif
  3764. }
  3765. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3766. (void)x_qh;
  3767. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3768. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3769. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3770. *x_ql = tile_x_ql;
  3771. *x_dm = tile_x_dm;
  3772. *x_sc = tile_x_sc;
  3773. }
  3774. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3775. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3776. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3777. (void)x_qh;
  3778. GGML_CUDA_ASSUME(i_offset >= 0);
  3779. GGML_CUDA_ASSUME(i_offset < nwarps);
  3780. GGML_CUDA_ASSUME(k >= 0);
  3781. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3782. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3783. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3784. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3785. #pragma unroll
  3786. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3787. int i = i0 + i_offset;
  3788. if (need_check) {
  3789. i = min(i, i_max);
  3790. }
  3791. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3792. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3793. }
  3794. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3795. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3796. #pragma unroll
  3797. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3798. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3799. if (need_check) {
  3800. i = min(i, i_max);
  3801. }
  3802. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3803. #if QK_K == 256
  3804. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3805. #else
  3806. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3807. #endif
  3808. }
  3809. #pragma unroll
  3810. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3811. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3812. if (need_check) {
  3813. i = min(i, i_max);
  3814. }
  3815. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3816. const int * scales = (const int *) bxi->scales;
  3817. const int ksc = k % (WARP_SIZE/8);
  3818. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3819. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3820. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3821. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3822. }
  3823. }
  3824. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3825. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3826. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3827. (void)x_qh;
  3828. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3829. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3830. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3831. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3832. }
  3833. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3834. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3835. #ifndef GGML_QKK_64
  3836. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3837. int vl[2];
  3838. int vh[2];
  3839. int u[2*QR5_K];
  3840. float d8[QR5_K];
  3841. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3842. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3843. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3844. vl[0] = ql[0];
  3845. vl[1] = ql[4];
  3846. vh[0] = qh[0] >> bq8_offset;
  3847. vh[1] = qh[4] >> bq8_offset;
  3848. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3849. uint16_t aux[2];
  3850. const int j = bq8_offset/2;
  3851. if (j < 2) {
  3852. aux[0] = scales[j+0] & 0x3f3f;
  3853. aux[1] = scales[j+2] & 0x3f3f;
  3854. } else {
  3855. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3856. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3857. }
  3858. const uint8_t * sc = (const uint8_t *)aux;
  3859. const uint8_t * m = sc + 2;
  3860. #pragma unroll
  3861. for (int i = 0; i < QR5_K; ++i) {
  3862. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3863. d8[i] = __low2float(bq8i->ds);
  3864. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3865. u[2*i+0] = q8[0];
  3866. u[2*i+1] = q8[4];
  3867. }
  3868. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3869. #else
  3870. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3871. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3872. const int8_t * s = bq5_K->scales;
  3873. const float d = bq5_K->d;
  3874. const float d8_1 = __low2half(bq8_1[0].ds);
  3875. const float d8_2 = __low2half(bq8_1[1].ds);
  3876. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3877. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3878. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3879. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3880. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3881. const int vl1 = ql[0];
  3882. const int vl2 = ql[4];
  3883. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3884. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3885. const int in = step%8; // 0, 4, 0, 4
  3886. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3887. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3888. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3889. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3890. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3891. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3892. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3893. return d * sumf_d;
  3894. #else
  3895. NO_DEVICE_CODE;
  3896. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3897. #endif
  3898. }
  3899. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3900. (void)x_qh;
  3901. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3902. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3903. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3904. *x_ql = tile_x_ql;
  3905. *x_dm = tile_x_dm;
  3906. *x_sc = tile_x_sc;
  3907. }
  3908. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3909. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3910. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3911. (void)x_qh;
  3912. GGML_CUDA_ASSUME(i_offset >= 0);
  3913. GGML_CUDA_ASSUME(i_offset < nwarps);
  3914. GGML_CUDA_ASSUME(k >= 0);
  3915. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3916. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3917. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3918. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3919. #pragma unroll
  3920. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3921. int i = i0 + i_offset;
  3922. if (need_check) {
  3923. i = min(i, i_max);
  3924. }
  3925. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3926. const int ky = QR5_K*kqsx;
  3927. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3928. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3929. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3930. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3931. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3932. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3933. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3934. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3935. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3936. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3937. }
  3938. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3939. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3940. #pragma unroll
  3941. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3942. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3943. if (need_check) {
  3944. i = min(i, i_max);
  3945. }
  3946. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3947. #if QK_K == 256
  3948. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3949. #endif
  3950. }
  3951. #pragma unroll
  3952. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3953. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3954. if (need_check) {
  3955. i = min(i, i_max);
  3956. }
  3957. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3958. const int * scales = (const int *) bxi->scales;
  3959. const int ksc = k % (WARP_SIZE/8);
  3960. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3961. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3962. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3963. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3964. }
  3965. }
  3966. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3967. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3968. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3969. (void)x_qh;
  3970. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3971. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3972. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3973. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3974. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3975. }
  3976. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3977. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3978. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3979. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3980. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3981. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3982. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3983. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3984. const int8_t * scales = bq6_K->scales + scale_offset;
  3985. int u[QR6_K];
  3986. float d8[QR6_K];
  3987. #pragma unroll
  3988. for (int i = 0; i < QR6_K; ++i) {
  3989. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3990. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3991. }
  3992. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3993. }
  3994. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3995. (void)x_qh;
  3996. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3997. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3998. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3999. *x_ql = tile_x_ql;
  4000. *x_dm = tile_x_dm;
  4001. *x_sc = tile_x_sc;
  4002. }
  4003. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  4004. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  4005. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  4006. (void)x_qh;
  4007. GGML_CUDA_ASSUME(i_offset >= 0);
  4008. GGML_CUDA_ASSUME(i_offset < nwarps);
  4009. GGML_CUDA_ASSUME(k >= 0);
  4010. GGML_CUDA_ASSUME(k < WARP_SIZE);
  4011. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  4012. const int kqsx = k % QI6_K; // == k if QK_K == 256
  4013. const block_q6_K * bx0 = (const block_q6_K *) vx;
  4014. #pragma unroll
  4015. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  4016. int i = i0 + i_offset;
  4017. if (need_check) {
  4018. i = min(i, i_max);
  4019. }
  4020. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  4021. const int ky = QR6_K*kqsx;
  4022. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  4023. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  4024. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  4025. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  4026. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  4027. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  4028. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  4029. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  4030. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  4031. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  4032. }
  4033. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  4034. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  4035. float * x_dmf = (float *) x_dm;
  4036. #pragma unroll
  4037. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  4038. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  4039. if (need_check) {
  4040. i = min(i, i_max);
  4041. }
  4042. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  4043. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  4044. }
  4045. #pragma unroll
  4046. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  4047. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  4048. if (need_check) {
  4049. i = min(i, i_max);
  4050. }
  4051. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  4052. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  4053. }
  4054. }
  4055. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  4056. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  4057. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  4058. (void)x_qh;
  4059. const float * x_dmf = (const float *) x_dm;
  4060. const float * y_df = (const float *) y_ds;
  4061. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  4062. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  4063. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  4064. 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]);
  4065. }
  4066. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  4067. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4068. #if QK_K == 256
  4069. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  4070. #if QR2_XXS == 8
  4071. const int ib32 = iqs;
  4072. const uint16_t * q2 = bq2->qs + 4*ib32;
  4073. const uint8_t * aux8 = (const uint8_t *)q2;
  4074. const int8_t * q8 = bq8_1[ib32].qs;
  4075. uint32_t aux32 = q2[2] | (q2[3] << 16);
  4076. int sumi = 0;
  4077. for (int l = 0; l < 4; ++l) {
  4078. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  4079. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  4080. for (int j = 0; j < 8; ++j) {
  4081. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  4082. }
  4083. q8 += 8;
  4084. aux32 >>= 7;
  4085. }
  4086. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  4087. return d * sumi;
  4088. #else
  4089. // iqs is 0...15
  4090. const int ib32 = iqs/2;
  4091. const int il = iqs%2;
  4092. const uint16_t * q2 = bq2->qs + 4*ib32;
  4093. const uint8_t * aux8 = (const uint8_t *)q2;
  4094. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  4095. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  4096. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  4097. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  4098. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  4099. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  4100. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  4101. int sumi1 = 0, sumi2 = 0;
  4102. for (int j = 0; j < 8; ++j) {
  4103. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  4104. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  4105. }
  4106. return d * (sumi1 + sumi2);
  4107. #endif
  4108. #else
  4109. assert(false);
  4110. return 0.f;
  4111. #endif
  4112. }
  4113. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  4114. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4115. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4116. #if QK_K == 256
  4117. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  4118. const int ib32 = iqs;
  4119. const uint16_t * q2 = bq2->qs + 4*ib32;
  4120. const int8_t * q8 = bq8_1[ib32].qs;
  4121. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  4122. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  4123. int sumi1 = 0;
  4124. for (int l = 0; l < 2; ++l) {
  4125. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  4126. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  4127. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  4128. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  4129. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  4130. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  4131. q8 += 8;
  4132. }
  4133. int sumi2 = 0;
  4134. for (int l = 2; l < 4; ++l) {
  4135. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  4136. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  4137. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  4138. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  4139. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  4140. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  4141. q8 += 8;
  4142. }
  4143. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  4144. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  4145. #else
  4146. (void) ksigns64;
  4147. assert(false);
  4148. return 0.f;
  4149. #endif
  4150. #else
  4151. (void) ksigns64;
  4152. assert(false);
  4153. return 0.f;
  4154. #endif
  4155. }
  4156. // TODO
  4157. static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(
  4158. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4159. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4160. #if QK_K == 256
  4161. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  4162. const int ib32 = iqs;
  4163. const int8_t * q8 = bq8_1[ib32].qs;
  4164. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  4165. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  4166. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  4167. int sumi1 = 0;
  4168. for (int l = 0; l < 2; ++l) {
  4169. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  4170. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4171. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4172. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  4173. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  4174. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  4175. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  4176. q8 += 8;
  4177. }
  4178. int sumi2 = 0;
  4179. for (int l = 2; l < 4; ++l) {
  4180. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  4181. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4182. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4183. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  4184. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  4185. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  4186. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  4187. q8 += 8;
  4188. }
  4189. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  4190. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  4191. #else
  4192. (void) ksigns64;
  4193. assert(false);
  4194. return 0.f;
  4195. #endif
  4196. #else
  4197. (void) ksigns64;
  4198. assert(false);
  4199. return 0.f;
  4200. #endif
  4201. }
  4202. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  4203. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4204. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4205. #if QK_K == 256
  4206. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  4207. const int ib32 = iqs;
  4208. const uint8_t * q3 = bq2->qs + 8*ib32;
  4209. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  4210. const int8_t * q8 = bq8_1[ib32].qs;
  4211. uint32_t aux32 = gas[0] | (gas[1] << 16);
  4212. int sumi = 0;
  4213. for (int l = 0; l < 4; ++l) {
  4214. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  4215. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  4216. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  4217. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  4218. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  4219. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  4220. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  4221. q8 += 8;
  4222. aux32 >>= 7;
  4223. }
  4224. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  4225. return d * sumi;
  4226. #else
  4227. assert(false);
  4228. return 0.f;
  4229. #endif
  4230. #else
  4231. assert(false);
  4232. return 0.f;
  4233. #endif
  4234. }
  4235. // TODO: don't use lookup table for signs
  4236. static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(
  4237. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4238. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4239. #if QK_K == 256
  4240. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  4241. const int ib32 = iqs;
  4242. const uint8_t * qs = bq2->qs + 8*ib32;
  4243. const int8_t * q8 = bq8_1[ib32].qs;
  4244. int sumi = 0;
  4245. for (int l = 0; l < 4; ++l) {
  4246. const uint32_t * grid1 = iq3xs_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  4247. const uint32_t * grid2 = iq3xs_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  4248. uint32_t signs0 = __vcmpeq4(((bq2->signs[4*ib32+l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4249. uint32_t signs1 = __vcmpeq4(((bq2->signs[4*ib32+l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4250. const int grid_l = __vsub4(grid1[0] ^ signs0, signs0);
  4251. const int grid_h = __vsub4(grid2[0] ^ signs1, signs1);
  4252. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  4253. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  4254. q8 += 8;
  4255. }
  4256. const float d = (float)bq2->d * (0.5f + ((bq2->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * __low2float(bq8_1[ib32].ds) * 0.5f;
  4257. return d * sumi;
  4258. #else
  4259. assert(false);
  4260. return 0.f;
  4261. #endif
  4262. #else
  4263. assert(false);
  4264. return 0.f;
  4265. #endif
  4266. }
  4267. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  4268. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4269. #if QK_K == 256
  4270. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  4271. const int ib32 = iqs;
  4272. int sumi1 = 0, sumi2 = 0, sumi3 = 0, sumi4 = 0;
  4273. const uint8_t h1 = bq1->scales[2*ib32+0];
  4274. const uint8_t h2 = bq1->scales[2*ib32+1];
  4275. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4276. const int * q8 = (const int *)bq8_1[ib32].qs;
  4277. const int * grid1 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  4278. const int * grid2 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  4279. const int * grid3 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  4280. const int * grid4 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  4281. for (int j = 0; j < 2; ++j) {
  4282. sumi1 = __dp4a(q8[j+0], grid1[j], sumi1);
  4283. sumi2 = __dp4a(q8[j+2], grid2[j], sumi2);
  4284. sumi3 = __dp4a(q8[j+4], grid3[j], sumi3);
  4285. sumi4 = __dp4a(q8[j+6], grid4[j], sumi4);
  4286. }
  4287. #else
  4288. const int8_t * q8 = bq8_1[ib32].qs;
  4289. const int8_t * grid1 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  4290. const int8_t * grid2 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  4291. const int8_t * grid3 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  4292. const int8_t * grid4 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  4293. for (int j = 0; j < 8; ++j) {
  4294. sumi1 += q8[j+ 0] * grid1[j];
  4295. sumi2 += q8[j+ 8] * grid2[j];
  4296. sumi3 += q8[j+16] * grid3[j];
  4297. sumi4 += q8[j+24] * grid4[j];
  4298. }
  4299. #endif
  4300. const float d = (float)bq1->d * __low2float(bq8_1[ib32].ds);
  4301. return d * (sumi1 * (2*(h1 & 7) + 1) + sumi2 * (2*((h1 >> 4) & 7) + 1) +
  4302. sumi3 * (2*(h2 & 7) + 1) + sumi4 * (2*((h2 >> 4) & 7) + 1));
  4303. #else
  4304. assert(false);
  4305. return 0.f;
  4306. #endif
  4307. }
  4308. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4309. static __device__ __forceinline__ void get_int_from_table_16(const uint32_t & q4, const uint8_t * values,
  4310. int & val1, int & val2) {
  4311. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  4312. aux32 = q4 & 0x0f0f0f0f;
  4313. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  4314. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  4315. val1 = v1 | (v2 << 16);
  4316. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  4317. v1 = values[q8[0]] | (values[q8[1]] << 8);
  4318. v2 = values[q8[2]] | (values[q8[3]] << 8);
  4319. val2 = v1 | (v2 << 16);
  4320. }
  4321. #endif
  4322. static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
  4323. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4324. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  4325. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4326. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  4327. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  4328. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  4329. int v1, v2;
  4330. int sumi1 = 0, sumi2 = 0;
  4331. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  4332. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  4333. get_int_from_table_16(aux, values, v1, v2);
  4334. sumi1 = __dp4a(v1, q8[l+0], sumi1);
  4335. sumi2 = __dp4a(v2, q8[l+4], sumi2);
  4336. }
  4337. #else
  4338. const uint8_t * q4 = bq->qs + 4*iqs;
  4339. const int8_t * q8 = bq8_1->qs + 4*iqs;
  4340. int sumi1 = 0, sumi2 = 0;
  4341. for (int l = 0; l < 4*VDR_Q4_0_Q8_1_MMVQ; ++l) {
  4342. sumi1 += q8[l+ 0] * kvalues_iq4nl[q4[l] & 0xf];
  4343. sumi2 += q8[l+16] * kvalues_iq4nl[q4[l] >> 4];
  4344. }
  4345. #endif
  4346. const float d = (float)bq->d * __low2float(bq8_1->ds);
  4347. return d * (sumi1 + sumi2);
  4348. }
  4349. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  4350. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  4351. static __device__ __forceinline__ void mul_mat_q(
  4352. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4353. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4354. const block_q_t * x = (const block_q_t *) vx;
  4355. const block_q8_1 * y = (const block_q8_1 *) vy;
  4356. const int blocks_per_row_x = ncols_x / qk;
  4357. const int blocks_per_col_y = nrows_y / QK8_1;
  4358. const int blocks_per_warp = WARP_SIZE / qi;
  4359. const int & ncols_dst = ncols_y;
  4360. const int row_dst_0 = blockIdx.x*mmq_y;
  4361. const int & row_x_0 = row_dst_0;
  4362. const int col_dst_0 = blockIdx.y*mmq_x;
  4363. const int & col_y_0 = col_dst_0;
  4364. int * tile_x_ql = nullptr;
  4365. half2 * tile_x_dm = nullptr;
  4366. int * tile_x_qh = nullptr;
  4367. int * tile_x_sc = nullptr;
  4368. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  4369. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  4370. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  4371. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  4372. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  4373. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  4374. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  4375. #pragma unroll
  4376. for (int ir = 0; ir < qr; ++ir) {
  4377. const int kqs = ir*WARP_SIZE + threadIdx.x;
  4378. const int kbxd = kqs / QI8_1;
  4379. #pragma unroll
  4380. for (int i = 0; i < mmq_x; i += nwarps) {
  4381. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  4382. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  4383. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  4384. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  4385. }
  4386. #pragma unroll
  4387. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  4388. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  4389. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  4390. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  4391. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  4392. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  4393. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  4394. if (need_sum) {
  4395. *dsi_dst = *dsi_src;
  4396. } else {
  4397. float * dfi_dst = (float *) dsi_dst;
  4398. *dfi_dst = __low2half(*dsi_src);
  4399. }
  4400. }
  4401. __syncthreads();
  4402. // #pragma unroll // unrolling this loop causes too much register pressure
  4403. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  4404. #pragma unroll
  4405. for (int j = 0; j < mmq_x; j += nwarps) {
  4406. #pragma unroll
  4407. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  4408. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  4409. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  4410. threadIdx.x + i, threadIdx.y + j, k);
  4411. }
  4412. }
  4413. }
  4414. __syncthreads();
  4415. }
  4416. }
  4417. #pragma unroll
  4418. for (int j = 0; j < mmq_x; j += nwarps) {
  4419. const int col_dst = col_dst_0 + j + threadIdx.y;
  4420. if (col_dst >= ncols_dst) {
  4421. return;
  4422. }
  4423. #pragma unroll
  4424. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  4425. const int row_dst = row_dst_0 + threadIdx.x + i;
  4426. if (row_dst >= nrows_dst) {
  4427. continue;
  4428. }
  4429. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  4430. }
  4431. }
  4432. }
  4433. #define MMQ_X_Q4_0_RDNA2 64
  4434. #define MMQ_Y_Q4_0_RDNA2 128
  4435. #define NWARPS_Q4_0_RDNA2 8
  4436. #define MMQ_X_Q4_0_RDNA1 64
  4437. #define MMQ_Y_Q4_0_RDNA1 64
  4438. #define NWARPS_Q4_0_RDNA1 8
  4439. #if defined(CUDA_USE_TENSOR_CORES)
  4440. #define MMQ_X_Q4_0_AMPERE 4
  4441. #define MMQ_Y_Q4_0_AMPERE 32
  4442. #define NWARPS_Q4_0_AMPERE 4
  4443. #else
  4444. #define MMQ_X_Q4_0_AMPERE 64
  4445. #define MMQ_Y_Q4_0_AMPERE 128
  4446. #define NWARPS_Q4_0_AMPERE 4
  4447. #endif
  4448. #define MMQ_X_Q4_0_PASCAL 64
  4449. #define MMQ_Y_Q4_0_PASCAL 64
  4450. #define NWARPS_Q4_0_PASCAL 8
  4451. template <bool need_check> static __global__ void
  4452. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4453. #if defined(RDNA3) || defined(RDNA2)
  4454. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  4455. #endif // defined(RDNA3) || defined(RDNA2)
  4456. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4457. mul_mat_q4_0(
  4458. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4459. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4460. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4461. #if defined(RDNA3) || defined(RDNA2)
  4462. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  4463. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  4464. const int nwarps = NWARPS_Q4_0_RDNA2;
  4465. #else
  4466. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  4467. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  4468. const int nwarps = NWARPS_Q4_0_RDNA1;
  4469. #endif // defined(RDNA3) || defined(RDNA2)
  4470. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4471. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4472. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4473. #elif __CUDA_ARCH__ >= CC_VOLTA
  4474. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  4475. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  4476. const int nwarps = NWARPS_Q4_0_AMPERE;
  4477. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4478. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4479. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4480. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4481. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  4482. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  4483. const int nwarps = NWARPS_Q4_0_PASCAL;
  4484. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4485. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4486. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4487. #else
  4488. (void) vec_dot_q4_0_q8_1_mul_mat;
  4489. NO_DEVICE_CODE;
  4490. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4491. }
  4492. #define MMQ_X_Q4_1_RDNA2 64
  4493. #define MMQ_Y_Q4_1_RDNA2 128
  4494. #define NWARPS_Q4_1_RDNA2 8
  4495. #define MMQ_X_Q4_1_RDNA1 64
  4496. #define MMQ_Y_Q4_1_RDNA1 64
  4497. #define NWARPS_Q4_1_RDNA1 8
  4498. #if defined(CUDA_USE_TENSOR_CORES)
  4499. #define MMQ_X_Q4_1_AMPERE 4
  4500. #define MMQ_Y_Q4_1_AMPERE 32
  4501. #define NWARPS_Q4_1_AMPERE 4
  4502. #else
  4503. #define MMQ_X_Q4_1_AMPERE 64
  4504. #define MMQ_Y_Q4_1_AMPERE 128
  4505. #define NWARPS_Q4_1_AMPERE 4
  4506. #endif
  4507. #define MMQ_X_Q4_1_PASCAL 64
  4508. #define MMQ_Y_Q4_1_PASCAL 64
  4509. #define NWARPS_Q4_1_PASCAL 8
  4510. template <bool need_check> static __global__ void
  4511. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4512. #if defined(RDNA3) || defined(RDNA2)
  4513. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  4514. #endif // defined(RDNA3) || defined(RDNA2)
  4515. #elif __CUDA_ARCH__ < CC_VOLTA
  4516. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  4517. #endif // __CUDA_ARCH__ < CC_VOLTA
  4518. mul_mat_q4_1(
  4519. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4520. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4521. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4522. #if defined(RDNA3) || defined(RDNA2)
  4523. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  4524. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  4525. const int nwarps = NWARPS_Q4_1_RDNA2;
  4526. #else
  4527. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  4528. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  4529. const int nwarps = NWARPS_Q4_1_RDNA1;
  4530. #endif // defined(RDNA3) || defined(RDNA2)
  4531. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4532. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4533. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4534. #elif __CUDA_ARCH__ >= CC_VOLTA
  4535. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  4536. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  4537. const int nwarps = NWARPS_Q4_1_AMPERE;
  4538. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4539. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4540. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4541. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4542. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  4543. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  4544. const int nwarps = NWARPS_Q4_1_PASCAL;
  4545. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4546. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4547. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4548. #else
  4549. (void) vec_dot_q4_1_q8_1_mul_mat;
  4550. NO_DEVICE_CODE;
  4551. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4552. }
  4553. #define MMQ_X_Q5_0_RDNA2 64
  4554. #define MMQ_Y_Q5_0_RDNA2 128
  4555. #define NWARPS_Q5_0_RDNA2 8
  4556. #define MMQ_X_Q5_0_RDNA1 64
  4557. #define MMQ_Y_Q5_0_RDNA1 64
  4558. #define NWARPS_Q5_0_RDNA1 8
  4559. #if defined(CUDA_USE_TENSOR_CORES)
  4560. #define MMQ_X_Q5_0_AMPERE 4
  4561. #define MMQ_Y_Q5_0_AMPERE 32
  4562. #define NWARPS_Q5_0_AMPERE 4
  4563. #else
  4564. #define MMQ_X_Q5_0_AMPERE 128
  4565. #define MMQ_Y_Q5_0_AMPERE 64
  4566. #define NWARPS_Q5_0_AMPERE 4
  4567. #endif
  4568. #define MMQ_X_Q5_0_PASCAL 64
  4569. #define MMQ_Y_Q5_0_PASCAL 64
  4570. #define NWARPS_Q5_0_PASCAL 8
  4571. template <bool need_check> static __global__ void
  4572. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4573. #if defined(RDNA3) || defined(RDNA2)
  4574. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  4575. #endif // defined(RDNA3) || defined(RDNA2)
  4576. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4577. mul_mat_q5_0(
  4578. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4579. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4580. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4581. #if defined(RDNA3) || defined(RDNA2)
  4582. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  4583. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  4584. const int nwarps = NWARPS_Q5_0_RDNA2;
  4585. #else
  4586. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  4587. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  4588. const int nwarps = NWARPS_Q5_0_RDNA1;
  4589. #endif // defined(RDNA3) || defined(RDNA2)
  4590. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4591. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4592. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4593. #elif __CUDA_ARCH__ >= CC_VOLTA
  4594. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  4595. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  4596. const int nwarps = NWARPS_Q5_0_AMPERE;
  4597. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4598. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4599. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4600. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4601. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  4602. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  4603. const int nwarps = NWARPS_Q5_0_PASCAL;
  4604. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4605. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4606. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4607. #else
  4608. (void) vec_dot_q5_0_q8_1_mul_mat;
  4609. NO_DEVICE_CODE;
  4610. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4611. }
  4612. #define MMQ_X_Q5_1_RDNA2 64
  4613. #define MMQ_Y_Q5_1_RDNA2 128
  4614. #define NWARPS_Q5_1_RDNA2 8
  4615. #define MMQ_X_Q5_1_RDNA1 64
  4616. #define MMQ_Y_Q5_1_RDNA1 64
  4617. #define NWARPS_Q5_1_RDNA1 8
  4618. #if defined(CUDA_USE_TENSOR_CORES)
  4619. #define MMQ_X_Q5_1_AMPERE 4
  4620. #define MMQ_Y_Q5_1_AMPERE 32
  4621. #define NWARPS_Q5_1_AMPERE 4
  4622. #else
  4623. #define MMQ_X_Q5_1_AMPERE 128
  4624. #define MMQ_Y_Q5_1_AMPERE 64
  4625. #define NWARPS_Q5_1_AMPERE 4
  4626. #endif
  4627. #define MMQ_X_Q5_1_PASCAL 64
  4628. #define MMQ_Y_Q5_1_PASCAL 64
  4629. #define NWARPS_Q5_1_PASCAL 8
  4630. template <bool need_check> static __global__ void
  4631. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4632. #if defined(RDNA3) || defined(RDNA2)
  4633. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4634. #endif // defined(RDNA3) || defined(RDNA2)
  4635. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4636. mul_mat_q5_1(
  4637. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4638. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4639. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4640. #if defined(RDNA3) || defined(RDNA2)
  4641. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4642. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4643. const int nwarps = NWARPS_Q5_1_RDNA2;
  4644. #else
  4645. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4646. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4647. const int nwarps = NWARPS_Q5_1_RDNA1;
  4648. #endif // defined(RDNA3) || defined(RDNA2)
  4649. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4650. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4651. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4652. #elif __CUDA_ARCH__ >= CC_VOLTA
  4653. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4654. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4655. const int nwarps = NWARPS_Q5_1_AMPERE;
  4656. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4657. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4658. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4659. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4660. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4661. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4662. const int nwarps = NWARPS_Q5_1_PASCAL;
  4663. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4664. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4665. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4666. #else
  4667. (void) vec_dot_q5_1_q8_1_mul_mat;
  4668. NO_DEVICE_CODE;
  4669. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4670. }
  4671. #define MMQ_X_Q8_0_RDNA2 64
  4672. #define MMQ_Y_Q8_0_RDNA2 128
  4673. #define NWARPS_Q8_0_RDNA2 8
  4674. #define MMQ_X_Q8_0_RDNA1 64
  4675. #define MMQ_Y_Q8_0_RDNA1 64
  4676. #define NWARPS_Q8_0_RDNA1 8
  4677. #if defined(CUDA_USE_TENSOR_CORES)
  4678. #define MMQ_X_Q8_0_AMPERE 4
  4679. #define MMQ_Y_Q8_0_AMPERE 32
  4680. #define NWARPS_Q8_0_AMPERE 4
  4681. #else
  4682. #define MMQ_X_Q8_0_AMPERE 128
  4683. #define MMQ_Y_Q8_0_AMPERE 64
  4684. #define NWARPS_Q8_0_AMPERE 4
  4685. #endif
  4686. #define MMQ_X_Q8_0_PASCAL 64
  4687. #define MMQ_Y_Q8_0_PASCAL 64
  4688. #define NWARPS_Q8_0_PASCAL 8
  4689. template <bool need_check> static __global__ void
  4690. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4691. #if defined(RDNA3) || defined(RDNA2)
  4692. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4693. #endif // defined(RDNA3) || defined(RDNA2)
  4694. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4695. mul_mat_q8_0(
  4696. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4697. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4698. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4699. #if defined(RDNA3) || defined(RDNA2)
  4700. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4701. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4702. const int nwarps = NWARPS_Q8_0_RDNA2;
  4703. #else
  4704. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4705. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4706. const int nwarps = NWARPS_Q8_0_RDNA1;
  4707. #endif // defined(RDNA3) || defined(RDNA2)
  4708. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4709. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4710. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4711. #elif __CUDA_ARCH__ >= CC_VOLTA
  4712. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4713. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4714. const int nwarps = NWARPS_Q8_0_AMPERE;
  4715. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4716. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4717. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4718. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4719. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4720. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4721. const int nwarps = NWARPS_Q8_0_PASCAL;
  4722. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4723. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4724. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4725. #else
  4726. (void) vec_dot_q8_0_q8_1_mul_mat;
  4727. NO_DEVICE_CODE;
  4728. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4729. }
  4730. #define MMQ_X_Q2_K_RDNA2 64
  4731. #define MMQ_Y_Q2_K_RDNA2 128
  4732. #define NWARPS_Q2_K_RDNA2 8
  4733. #define MMQ_X_Q2_K_RDNA1 128
  4734. #define MMQ_Y_Q2_K_RDNA1 32
  4735. #define NWARPS_Q2_K_RDNA1 8
  4736. #if defined(CUDA_USE_TENSOR_CORES)
  4737. #define MMQ_X_Q2_K_AMPERE 4
  4738. #define MMQ_Y_Q2_K_AMPERE 32
  4739. #define NWARPS_Q2_K_AMPERE 4
  4740. #else
  4741. #define MMQ_X_Q2_K_AMPERE 64
  4742. #define MMQ_Y_Q2_K_AMPERE 128
  4743. #define NWARPS_Q2_K_AMPERE 4
  4744. #endif
  4745. #define MMQ_X_Q2_K_PASCAL 64
  4746. #define MMQ_Y_Q2_K_PASCAL 64
  4747. #define NWARPS_Q2_K_PASCAL 8
  4748. template <bool need_check> static __global__ void
  4749. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4750. #if defined(RDNA3) || defined(RDNA2)
  4751. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4752. #endif // defined(RDNA3) || defined(RDNA2)
  4753. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4754. mul_mat_q2_K(
  4755. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4756. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4757. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4758. #if defined(RDNA3) || defined(RDNA2)
  4759. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4760. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4761. const int nwarps = NWARPS_Q2_K_RDNA2;
  4762. #else
  4763. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4764. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4765. const int nwarps = NWARPS_Q2_K_RDNA1;
  4766. #endif // defined(RDNA3) || defined(RDNA2)
  4767. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4768. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4769. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4770. #elif __CUDA_ARCH__ >= CC_VOLTA
  4771. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4772. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4773. const int nwarps = NWARPS_Q2_K_AMPERE;
  4774. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4775. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4776. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4777. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4778. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4779. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4780. const int nwarps = NWARPS_Q2_K_PASCAL;
  4781. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4782. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4783. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4784. #else
  4785. (void) vec_dot_q2_K_q8_1_mul_mat;
  4786. NO_DEVICE_CODE;
  4787. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4788. }
  4789. #define MMQ_X_Q3_K_RDNA2 128
  4790. #define MMQ_Y_Q3_K_RDNA2 64
  4791. #define NWARPS_Q3_K_RDNA2 8
  4792. #define MMQ_X_Q3_K_RDNA1 32
  4793. #define MMQ_Y_Q3_K_RDNA1 128
  4794. #define NWARPS_Q3_K_RDNA1 8
  4795. #if defined(CUDA_USE_TENSOR_CORES)
  4796. #define MMQ_X_Q3_K_AMPERE 4
  4797. #define MMQ_Y_Q3_K_AMPERE 32
  4798. #define NWARPS_Q3_K_AMPERE 4
  4799. #else
  4800. #define MMQ_X_Q3_K_AMPERE 128
  4801. #define MMQ_Y_Q3_K_AMPERE 128
  4802. #define NWARPS_Q3_K_AMPERE 4
  4803. #endif
  4804. #define MMQ_X_Q3_K_PASCAL 64
  4805. #define MMQ_Y_Q3_K_PASCAL 64
  4806. #define NWARPS_Q3_K_PASCAL 8
  4807. template <bool need_check> static __global__ void
  4808. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4809. #if defined(RDNA3) || defined(RDNA2)
  4810. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4811. #endif // defined(RDNA3) || defined(RDNA2)
  4812. #elif __CUDA_ARCH__ < CC_VOLTA
  4813. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4814. #endif // __CUDA_ARCH__ < CC_VOLTA
  4815. mul_mat_q3_K(
  4816. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4817. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4818. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4819. #if defined(RDNA3) || defined(RDNA2)
  4820. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4821. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4822. const int nwarps = NWARPS_Q3_K_RDNA2;
  4823. #else
  4824. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4825. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4826. const int nwarps = NWARPS_Q3_K_RDNA1;
  4827. #endif // defined(RDNA3) || defined(RDNA2)
  4828. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4829. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4830. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4831. #elif __CUDA_ARCH__ >= CC_VOLTA
  4832. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4833. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4834. const int nwarps = NWARPS_Q3_K_AMPERE;
  4835. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4836. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4837. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4838. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4839. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4840. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4841. const int nwarps = NWARPS_Q3_K_PASCAL;
  4842. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4843. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4844. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4845. #else
  4846. (void) vec_dot_q3_K_q8_1_mul_mat;
  4847. NO_DEVICE_CODE;
  4848. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4849. }
  4850. #define MMQ_X_Q4_K_RDNA2 64
  4851. #define MMQ_Y_Q4_K_RDNA2 128
  4852. #define NWARPS_Q4_K_RDNA2 8
  4853. #define MMQ_X_Q4_K_RDNA1 32
  4854. #define MMQ_Y_Q4_K_RDNA1 64
  4855. #define NWARPS_Q4_K_RDNA1 8
  4856. #if defined(CUDA_USE_TENSOR_CORES)
  4857. #define MMQ_X_Q4_K_AMPERE 4
  4858. #define MMQ_Y_Q4_K_AMPERE 32
  4859. #define NWARPS_Q4_K_AMPERE 4
  4860. #else
  4861. #define MMQ_X_Q4_K_AMPERE 64
  4862. #define MMQ_Y_Q4_K_AMPERE 128
  4863. #define NWARPS_Q4_K_AMPERE 4
  4864. #endif
  4865. #define MMQ_X_Q4_K_PASCAL 64
  4866. #define MMQ_Y_Q4_K_PASCAL 64
  4867. #define NWARPS_Q4_K_PASCAL 8
  4868. template <bool need_check> static __global__ void
  4869. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4870. #if defined(RDNA3) || defined(RDNA2)
  4871. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4872. #endif // defined(RDNA3) || defined(RDNA2)
  4873. #elif __CUDA_ARCH__ < CC_VOLTA
  4874. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4875. #endif // __CUDA_ARCH__ < CC_VOLTA
  4876. mul_mat_q4_K(
  4877. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4878. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4879. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4880. #if defined(RDNA3) || defined(RDNA2)
  4881. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4882. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4883. const int nwarps = NWARPS_Q4_K_RDNA2;
  4884. #else
  4885. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4886. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4887. const int nwarps = NWARPS_Q4_K_RDNA1;
  4888. #endif // defined(RDNA3) || defined(RDNA2)
  4889. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4890. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4891. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4892. #elif __CUDA_ARCH__ >= CC_VOLTA
  4893. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4894. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4895. const int nwarps = NWARPS_Q4_K_AMPERE;
  4896. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4897. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4898. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4899. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4900. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4901. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4902. const int nwarps = NWARPS_Q4_K_PASCAL;
  4903. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4904. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4905. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4906. #else
  4907. (void) vec_dot_q4_K_q8_1_mul_mat;
  4908. NO_DEVICE_CODE;
  4909. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4910. }
  4911. #define MMQ_X_Q5_K_RDNA2 64
  4912. #define MMQ_Y_Q5_K_RDNA2 128
  4913. #define NWARPS_Q5_K_RDNA2 8
  4914. #define MMQ_X_Q5_K_RDNA1 32
  4915. #define MMQ_Y_Q5_K_RDNA1 64
  4916. #define NWARPS_Q5_K_RDNA1 8
  4917. #if defined(CUDA_USE_TENSOR_CORES)
  4918. #define MMQ_X_Q5_K_AMPERE 4
  4919. #define MMQ_Y_Q5_K_AMPERE 32
  4920. #define NWARPS_Q5_K_AMPERE 4
  4921. #else
  4922. #define MMQ_X_Q5_K_AMPERE 64
  4923. #define MMQ_Y_Q5_K_AMPERE 128
  4924. #define NWARPS_Q5_K_AMPERE 4
  4925. #endif
  4926. #define MMQ_X_Q5_K_PASCAL 64
  4927. #define MMQ_Y_Q5_K_PASCAL 64
  4928. #define NWARPS_Q5_K_PASCAL 8
  4929. template <bool need_check> static __global__ void
  4930. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4931. #if defined(RDNA3) || defined(RDNA2)
  4932. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  4933. #endif // defined(RDNA3) || defined(RDNA2)
  4934. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4935. mul_mat_q5_K(
  4936. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4937. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4938. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4939. #if defined(RDNA3) || defined(RDNA2)
  4940. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  4941. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  4942. const int nwarps = NWARPS_Q5_K_RDNA2;
  4943. #else
  4944. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  4945. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  4946. const int nwarps = NWARPS_Q5_K_RDNA1;
  4947. #endif // defined(RDNA3) || defined(RDNA2)
  4948. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4949. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4950. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4951. #elif __CUDA_ARCH__ >= CC_VOLTA
  4952. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  4953. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  4954. const int nwarps = NWARPS_Q5_K_AMPERE;
  4955. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4956. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4957. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4958. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4959. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  4960. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  4961. const int nwarps = NWARPS_Q5_K_PASCAL;
  4962. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4963. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4964. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4965. #else
  4966. (void) vec_dot_q5_K_q8_1_mul_mat;
  4967. NO_DEVICE_CODE;
  4968. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4969. }
  4970. #define MMQ_X_Q6_K_RDNA2 64
  4971. #define MMQ_Y_Q6_K_RDNA2 128
  4972. #define NWARPS_Q6_K_RDNA2 8
  4973. #define MMQ_X_Q6_K_RDNA1 32
  4974. #define MMQ_Y_Q6_K_RDNA1 64
  4975. #define NWARPS_Q6_K_RDNA1 8
  4976. #if defined(CUDA_USE_TENSOR_CORES)
  4977. #define MMQ_X_Q6_K_AMPERE 4
  4978. #define MMQ_Y_Q6_K_AMPERE 32
  4979. #define NWARPS_Q6_K_AMPERE 4
  4980. #else
  4981. #define MMQ_X_Q6_K_AMPERE 64
  4982. #define MMQ_Y_Q6_K_AMPERE 64
  4983. #define NWARPS_Q6_K_AMPERE 4
  4984. #endif
  4985. #define MMQ_X_Q6_K_PASCAL 64
  4986. #define MMQ_Y_Q6_K_PASCAL 64
  4987. #define NWARPS_Q6_K_PASCAL 8
  4988. template <bool need_check> static __global__ void
  4989. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4990. #if defined(RDNA3) || defined(RDNA2)
  4991. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4992. #endif // defined(RDNA3) || defined(RDNA2)
  4993. #elif __CUDA_ARCH__ < CC_VOLTA
  4994. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4995. #endif // __CUDA_ARCH__ < CC_VOLTA
  4996. mul_mat_q6_K(
  4997. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4998. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4999. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5000. #if defined(RDNA3) || defined(RDNA2)
  5001. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  5002. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  5003. const int nwarps = NWARPS_Q6_K_RDNA2;
  5004. #else
  5005. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  5006. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  5007. const int nwarps = NWARPS_Q6_K_RDNA1;
  5008. #endif // defined(RDNA3) || defined(RDNA2)
  5009. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5010. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5011. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5012. #elif __CUDA_ARCH__ >= CC_VOLTA
  5013. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  5014. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  5015. const int nwarps = NWARPS_Q6_K_AMPERE;
  5016. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5017. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5018. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5019. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  5020. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  5021. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  5022. const int nwarps = NWARPS_Q6_K_PASCAL;
  5023. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5024. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5025. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5026. #else
  5027. (void) vec_dot_q6_K_q8_1_mul_mat;
  5028. NO_DEVICE_CODE;
  5029. #endif // __CUDA_ARCH__ >= CC_VOLTA
  5030. }
  5031. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  5032. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  5033. // tell the compiler to use as many registers as it wants, see nwarps definition below
  5034. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  5035. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  5036. static __global__ void mul_mat_vec_q(
  5037. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5038. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  5039. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  5040. constexpr int nwarps = 1;
  5041. constexpr int rows_per_cuda_block = 1;
  5042. #else
  5043. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  5044. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  5045. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  5046. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  5047. const int row0 = rows_per_cuda_block*blockIdx.x;
  5048. const int blocks_per_row_x = ncols_x / qk;
  5049. const int blocks_per_col_y = nrows_y / QK8_1;
  5050. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  5051. // partial sum for each thread
  5052. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  5053. const block_q_t * x = (const block_q_t *) vx;
  5054. const block_q8_1 * y = (const block_q8_1 *) vy;
  5055. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  5056. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  5057. // x block quant index when casting the quants to int
  5058. const int kqs = vdr * (tid % (qi/vdr));
  5059. #pragma unroll
  5060. for (int j = 0; j < ncols_y; ++j) {
  5061. #pragma unroll
  5062. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5063. tmp[j][i] += vec_dot_q_cuda(
  5064. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  5065. }
  5066. }
  5067. }
  5068. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  5069. if (threadIdx.y > 0) {
  5070. #pragma unroll
  5071. for (int j = 0; j < ncols_y; ++j) {
  5072. #pragma unroll
  5073. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5074. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  5075. }
  5076. }
  5077. }
  5078. __syncthreads();
  5079. if (threadIdx.y > 0) {
  5080. return;
  5081. }
  5082. // sum up partial sums and write back result
  5083. #pragma unroll
  5084. for (int j = 0; j < ncols_y; ++j) {
  5085. #pragma unroll
  5086. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5087. #pragma unroll
  5088. for (int l = 0; l < nwarps-1; ++l) {
  5089. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  5090. }
  5091. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  5092. }
  5093. if (threadIdx.x < rows_per_cuda_block) {
  5094. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  5095. }
  5096. }
  5097. }
  5098. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  5099. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  5100. // qk = quantized weights per x block
  5101. // qr = number of quantized weights per data value in x block
  5102. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  5103. if (row >= nrows) {
  5104. return;
  5105. }
  5106. const int tid = threadIdx.x;
  5107. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  5108. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  5109. const int y_offset = qr == 1 ? 1 : qk/2;
  5110. // partial sum for each thread
  5111. #ifdef GGML_CUDA_F16
  5112. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  5113. #else
  5114. float tmp = 0.0f;
  5115. #endif // GGML_CUDA_F16
  5116. for (int i = 0; i < ncols; i += iter_stride) {
  5117. const int col = i + vals_per_iter*tid;
  5118. const int ib = (row*ncols + col)/qk; // x block index
  5119. const int iqs = (col%qk)/qr; // x quant index
  5120. const int iybs = col - col%qk; // y block start index
  5121. // processing >2 values per i iter is faster for fast GPUs
  5122. #pragma unroll
  5123. for (int j = 0; j < vals_per_iter; j += 2) {
  5124. // process 2 vals per j iter
  5125. // dequantize
  5126. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  5127. dfloat2 v;
  5128. dequantize_kernel(vx, ib, iqs + j/qr, v);
  5129. // matrix multiplication
  5130. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  5131. #ifdef GGML_CUDA_F16
  5132. tmp += __hmul2(v, {
  5133. y[iybs + iqs + j/qr + 0],
  5134. y[iybs + iqs + j/qr + y_offset]
  5135. });
  5136. #else
  5137. tmp += v.x * y[iybs + iqs + j/qr + 0];
  5138. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  5139. #endif // GGML_CUDA_F16
  5140. }
  5141. }
  5142. // sum up partial sums and write back result
  5143. #pragma unroll
  5144. for (int mask = 16; mask > 0; mask >>= 1) {
  5145. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  5146. }
  5147. if (tid == 0) {
  5148. #ifdef GGML_CUDA_F16
  5149. dst[row] = tmp.x + tmp.y;
  5150. #else
  5151. dst[row] = tmp;
  5152. #endif // GGML_CUDA_F16
  5153. }
  5154. }
  5155. static __global__ void mul_mat_p021_f16_f32(
  5156. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  5157. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  5158. const half * x = (const half *) vx;
  5159. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  5160. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  5161. const int channel_x = channel / (nchannels_y / nchannels_x);
  5162. const int nrows_y = ncols_x;
  5163. const int nrows_dst = nrows_x;
  5164. const int row_dst = row_x;
  5165. float tmp = 0.0f;
  5166. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  5167. const int col_x = col_x0 + threadIdx.x;
  5168. if (col_x >= ncols_x) {
  5169. break;
  5170. }
  5171. // x is transposed and permuted
  5172. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  5173. const float xi = __half2float(x[ix]);
  5174. const int row_y = col_x;
  5175. // y is not transposed but permuted
  5176. const int iy = channel*nrows_y + row_y;
  5177. tmp += xi * y[iy];
  5178. }
  5179. // dst is not transposed and not permuted
  5180. const int idst = channel*nrows_dst + row_dst;
  5181. // sum up partial sums and write back result
  5182. #pragma unroll
  5183. for (int mask = 16; mask > 0; mask >>= 1) {
  5184. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  5185. }
  5186. if (threadIdx.x == 0) {
  5187. dst[idst] = tmp;
  5188. }
  5189. }
  5190. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  5191. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  5192. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  5193. const half * x = (const half *) vx;
  5194. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  5195. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  5196. const int channel_x = channel / channel_x_divisor;
  5197. const int nrows_y = ncols_x;
  5198. const int nrows_dst = nrows_x;
  5199. const int row_dst = row_x;
  5200. const int idst = channel*nrows_dst + row_dst;
  5201. float tmp = 0.0f;
  5202. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  5203. const int col_x = col_x0 + threadIdx.x;
  5204. if (col_x >= ncols_x) {
  5205. break;
  5206. }
  5207. const int row_y = col_x;
  5208. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  5209. const int iy = channel*nrows_y + row_y;
  5210. const float xi = __half2float(x[ix]);
  5211. tmp += xi * y[iy];
  5212. }
  5213. // sum up partial sums and write back result
  5214. #pragma unroll
  5215. for (int mask = 16; mask > 0; mask >>= 1) {
  5216. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  5217. }
  5218. if (threadIdx.x == 0) {
  5219. dst[idst] = tmp;
  5220. }
  5221. }
  5222. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  5223. const float * xi = (const float *) cxi;
  5224. float * dsti = (float *) cdsti;
  5225. *dsti = *xi;
  5226. }
  5227. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  5228. const float * xi = (const float *) cxi;
  5229. half * dsti = (half *) cdsti;
  5230. *dsti = __float2half(*xi);
  5231. }
  5232. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  5233. const half * xi = (const half *) cxi;
  5234. half * dsti = (half *) cdsti;
  5235. *dsti = *xi;
  5236. }
  5237. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  5238. const half * xi = (const half *) cxi;
  5239. float * dsti = (float *) cdsti;
  5240. *dsti = *xi;
  5241. }
  5242. template <cpy_kernel_t cpy_1>
  5243. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  5244. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5245. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  5246. const int nb12, const int nb13) {
  5247. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5248. if (i >= ne) {
  5249. return;
  5250. }
  5251. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  5252. // then combine those indices with the corresponding byte offsets to get the total offsets
  5253. const int i03 = i/(ne00 * ne01 * ne02);
  5254. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  5255. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  5256. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  5257. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  5258. const int i13 = i/(ne10 * ne11 * ne12);
  5259. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  5260. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  5261. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  5262. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  5263. cpy_1(cx + x_offset, cdst + dst_offset);
  5264. }
  5265. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  5266. const float * xi = (const float *) cxi;
  5267. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  5268. float amax = 0.0f; // absolute max
  5269. for (int j = 0; j < QK8_0; j++) {
  5270. const float v = xi[j];
  5271. amax = fmaxf(amax, fabsf(v));
  5272. }
  5273. const float d = amax / ((1 << 7) - 1);
  5274. const float id = d ? 1.0f/d : 0.0f;
  5275. dsti->d = d;
  5276. for (int j = 0; j < QK8_0; ++j) {
  5277. const float x0 = xi[j]*id;
  5278. dsti->qs[j] = roundf(x0);
  5279. }
  5280. }
  5281. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  5282. const float * xi = (const float *) cxi;
  5283. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  5284. float amax = 0.0f;
  5285. float vmax = 0.0f;
  5286. for (int j = 0; j < QK4_0; ++j) {
  5287. const float v = xi[j];
  5288. if (amax < fabsf(v)) {
  5289. amax = fabsf(v);
  5290. vmax = v;
  5291. }
  5292. }
  5293. const float d = vmax / -8;
  5294. const float id = d ? 1.0f/d : 0.0f;
  5295. dsti->d = d;
  5296. for (int j = 0; j < QK4_0/2; ++j) {
  5297. const float x0 = xi[0 + j]*id;
  5298. const float x1 = xi[QK4_0/2 + j]*id;
  5299. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  5300. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  5301. dsti->qs[j] = xi0;
  5302. dsti->qs[j] |= xi1 << 4;
  5303. }
  5304. }
  5305. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  5306. const float * xi = (const float *) cxi;
  5307. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  5308. float vmin = FLT_MAX;
  5309. float vmax = -FLT_MAX;
  5310. for (int j = 0; j < QK4_1; ++j) {
  5311. const float v = xi[j];
  5312. if (v < vmin) vmin = v;
  5313. if (v > vmax) vmax = v;
  5314. }
  5315. const float d = (vmax - vmin) / ((1 << 4) - 1);
  5316. const float id = d ? 1.0f/d : 0.0f;
  5317. dsti->dm.x = d;
  5318. dsti->dm.y = vmin;
  5319. for (int j = 0; j < QK4_1/2; ++j) {
  5320. const float x0 = (xi[0 + j] - vmin)*id;
  5321. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  5322. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  5323. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  5324. dsti->qs[j] = xi0;
  5325. dsti->qs[j] |= xi1 << 4;
  5326. }
  5327. }
  5328. template <cpy_kernel_t cpy_blck, int qk>
  5329. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  5330. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5331. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  5332. const int nb12, const int nb13) {
  5333. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  5334. if (i >= ne) {
  5335. return;
  5336. }
  5337. const int i03 = i/(ne00 * ne01 * ne02);
  5338. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  5339. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  5340. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  5341. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  5342. const int i13 = i/(ne10 * ne11 * ne12);
  5343. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  5344. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  5345. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  5346. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  5347. cpy_blck(cx + x_offset, cdst + dst_offset);
  5348. }
  5349. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  5350. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  5351. return 1.0f - min(1.0f, max(0.0f, y));
  5352. }
  5353. struct rope_corr_dims {
  5354. float v[4];
  5355. };
  5356. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  5357. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  5358. static __device__ void rope_yarn(
  5359. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  5360. float * cos_theta, float * sin_theta
  5361. ) {
  5362. // Get n-d rotational scaling corrected for extrapolation
  5363. float theta_interp = freq_scale * theta_extrap;
  5364. float theta = theta_interp;
  5365. if (ext_factor != 0.0f) {
  5366. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  5367. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  5368. // Get n-d magnitude scaling corrected for interpolation
  5369. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  5370. }
  5371. *cos_theta = cosf(theta) * mscale;
  5372. *sin_theta = sinf(theta) * mscale;
  5373. }
  5374. // rope == RoPE == rotary positional embedding
  5375. template<typename T, bool has_pos>
  5376. static __global__ void rope(
  5377. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  5378. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  5379. ) {
  5380. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  5381. if (col >= ncols) {
  5382. return;
  5383. }
  5384. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5385. const int i = row*ncols + col;
  5386. const int i2 = row/p_delta_rows;
  5387. const int p = has_pos ? pos[i2] : 0;
  5388. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  5389. float cos_theta, sin_theta;
  5390. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  5391. const float x0 = x[i + 0];
  5392. const float x1 = x[i + 1];
  5393. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5394. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  5395. }
  5396. template<typename T, bool has_pos>
  5397. static __global__ void rope_neox(
  5398. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  5399. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  5400. ) {
  5401. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  5402. if (col >= ncols) {
  5403. return;
  5404. }
  5405. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5406. const int ib = col / n_dims;
  5407. const int ic = col % n_dims;
  5408. if (ib > 0) {
  5409. const int i = row*ncols + ib*n_dims + ic;
  5410. dst[i + 0] = x[i + 0];
  5411. dst[i + 1] = x[i + 1];
  5412. return;
  5413. }
  5414. const int i = row*ncols + ib*n_dims + ic/2;
  5415. const int i2 = row/p_delta_rows;
  5416. float cur_rot = inv_ndims * ic - ib;
  5417. const int p = has_pos ? pos[i2] : 0;
  5418. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  5419. float cos_theta, sin_theta;
  5420. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  5421. const float x0 = x[i + 0];
  5422. const float x1 = x[i + n_dims/2];
  5423. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5424. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  5425. }
  5426. static __global__ void rope_glm_f32(
  5427. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  5428. int n_ctx
  5429. ) {
  5430. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  5431. const int half_n_dims = ncols/4;
  5432. if (col >= half_n_dims) {
  5433. return;
  5434. }
  5435. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  5436. const int i = row*ncols + col;
  5437. const int i2 = row/p_delta_rows;
  5438. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  5439. // FIXME: this is likely wrong
  5440. const int p = pos != nullptr ? pos[i2] : 0;
  5441. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  5442. const float sin_theta = sinf(theta);
  5443. const float cos_theta = cosf(theta);
  5444. const float x0 = x[i + 0];
  5445. const float x1 = x[i + half_n_dims];
  5446. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5447. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  5448. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  5449. const float sin_block_theta = sinf(block_theta);
  5450. const float cos_block_theta = cosf(block_theta);
  5451. const float x2 = x[i + half_n_dims * 2];
  5452. const float x3 = x[i + half_n_dims * 3];
  5453. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  5454. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  5455. }
  5456. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  5457. const int n_heads_log2_floor, const float m0, const float m1) {
  5458. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  5459. if (col >= ncols) {
  5460. return;
  5461. }
  5462. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  5463. const int i = row*ncols + col;
  5464. const int k = row/k_rows;
  5465. float m_k;
  5466. if (k < n_heads_log2_floor) {
  5467. m_k = powf(m0, k + 1);
  5468. } else {
  5469. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  5470. }
  5471. dst[i] = col * m_k + x[i];
  5472. }
  5473. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  5474. const int row = blockIdx.x;
  5475. const int col = threadIdx.x;
  5476. float sum = 0.0f;
  5477. for (int i = col; i < ncols; i += blockDim.x) {
  5478. sum += x[row * ncols + i];
  5479. }
  5480. sum = warp_reduce_sum(sum);
  5481. if (col == 0) {
  5482. dst[row] = sum;
  5483. }
  5484. }
  5485. template<typename T>
  5486. static inline __device__ void swap(T & a, T & b) {
  5487. T tmp = a;
  5488. a = b;
  5489. b = tmp;
  5490. }
  5491. template<ggml_sort_order order>
  5492. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  5493. // bitonic sort
  5494. int col = threadIdx.x;
  5495. int row = blockIdx.y;
  5496. if (col >= ncols) return;
  5497. const float * x_row = x + row * ncols;
  5498. int * dst_row = dst + row * ncols;
  5499. // initialize indices
  5500. if (col < ncols) {
  5501. dst_row[col] = col;
  5502. }
  5503. __syncthreads();
  5504. for (int k = 2; k <= ncols; k *= 2) {
  5505. for (int j = k / 2; j > 0; j /= 2) {
  5506. int ixj = col ^ j;
  5507. if (ixj > col) {
  5508. if ((col & k) == 0) {
  5509. 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]]) {
  5510. swap(dst_row[col], dst_row[ixj]);
  5511. }
  5512. } else {
  5513. 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]]) {
  5514. swap(dst_row[col], dst_row[ixj]);
  5515. }
  5516. }
  5517. }
  5518. __syncthreads();
  5519. }
  5520. }
  5521. }
  5522. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  5523. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  5524. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5525. if (col >= ncols) {
  5526. return;
  5527. }
  5528. const int i = row*ncols + col;
  5529. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  5530. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  5531. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  5532. }
  5533. template <bool vals_smem, int ncols_template, int block_size_template>
  5534. 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) {
  5535. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  5536. const int tid = threadIdx.x;
  5537. const int rowx = blockIdx.x;
  5538. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  5539. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  5540. const int warp_id = threadIdx.x / WARP_SIZE;
  5541. const int lane_id = threadIdx.x % WARP_SIZE;
  5542. float slope = 0.0f;
  5543. // ALiBi
  5544. if (max_bias > 0.0f) {
  5545. const int h = rowx/nrows_y; // head index
  5546. const float base = h < n_head_log2 ? m0 : m1;
  5547. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  5548. slope = powf(base, exp);
  5549. }
  5550. extern __shared__ float data_soft_max_f32[];
  5551. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  5552. // shared memory buffer to cache values between iterations:
  5553. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  5554. float max_val = -INFINITY;
  5555. #pragma unroll
  5556. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5557. const int col = col0 + tid;
  5558. if (ncols_template == 0 && col >= ncols) {
  5559. break;
  5560. }
  5561. const int ix = rowx*ncols + col;
  5562. const int iy = rowy*ncols + col;
  5563. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  5564. vals[col] = val;
  5565. max_val = max(max_val, val);
  5566. }
  5567. // find the max value in the block
  5568. max_val = warp_reduce_max(max_val);
  5569. if (block_size > WARP_SIZE) {
  5570. if (warp_id == 0) {
  5571. buf_iw[lane_id] = -INFINITY;
  5572. }
  5573. __syncthreads();
  5574. if (lane_id == 0) {
  5575. buf_iw[warp_id] = max_val;
  5576. }
  5577. __syncthreads();
  5578. max_val = buf_iw[lane_id];
  5579. max_val = warp_reduce_max(max_val);
  5580. }
  5581. float tmp = 0.0f; // partial sum
  5582. #pragma unroll
  5583. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5584. const int col = col0 + tid;
  5585. if (ncols_template == 0 && col >= ncols) {
  5586. break;
  5587. }
  5588. const float val = expf(vals[col] - max_val);
  5589. tmp += val;
  5590. vals[col] = val;
  5591. }
  5592. // find the sum of exps in the block
  5593. tmp = warp_reduce_sum(tmp);
  5594. if (block_size > WARP_SIZE) {
  5595. if (warp_id == 0) {
  5596. buf_iw[lane_id] = 0.0f;
  5597. }
  5598. __syncthreads();
  5599. if (lane_id == 0) {
  5600. buf_iw[warp_id] = tmp;
  5601. }
  5602. __syncthreads();
  5603. tmp = buf_iw[lane_id];
  5604. tmp = warp_reduce_sum(tmp);
  5605. }
  5606. const float inv_sum = 1.0f / tmp;
  5607. #pragma unroll
  5608. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5609. const int col = col0 + tid;
  5610. if (ncols_template == 0 && col >= ncols) {
  5611. return;
  5612. }
  5613. const int idst = rowx*ncols + col;
  5614. dst[idst] = vals[col] * inv_sum;
  5615. }
  5616. }
  5617. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5618. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5619. if (i >= k) {
  5620. return;
  5621. }
  5622. dst[i] = scale * x[i];
  5623. }
  5624. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5625. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5626. if (i >= k) {
  5627. return;
  5628. }
  5629. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5630. }
  5631. template <typename T>
  5632. static __global__ void im2col_kernel(
  5633. const float * x, T * dst, int batch_offset,
  5634. int offset_delta, int IC, int IW, int IH, int OH, int OW, int KW, int KH, int pelements, int CHW,
  5635. int s0, int s1, int p0, int p1, int d0, int d1) {
  5636. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  5637. if (i >= pelements) {
  5638. return;
  5639. }
  5640. const int ksize = OW * (KH > 1 ? KW : 1);
  5641. const int kx = i / ksize;
  5642. const int kd = kx * ksize;
  5643. const int ky = (i - kd) / OW;
  5644. const int ix = i % OW;
  5645. const int oh = blockIdx.y;
  5646. const int batch = blockIdx.z / IC;
  5647. const int ic = blockIdx.z % IC;
  5648. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5649. const int64_t iih = oh * s1 + ky * d1 - p1;
  5650. const int64_t offset_dst =
  5651. ((batch * OH + oh) * OW + ix) * CHW +
  5652. (ic * (KW * KH) + ky * KW + kx);
  5653. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5654. dst[offset_dst] = 0.0f;
  5655. } else {
  5656. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5657. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5658. }
  5659. }
  5660. template <typename Ti, typename To>
  5661. static __global__ void pool2d_nchw_kernel(
  5662. const int ih, const int iw, const int oh, const int ow,
  5663. const int kh, const int kw, const int sh, const int sw,
  5664. const int ph, const int pw, const int parallel_elements,
  5665. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5666. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5667. if (idx >= parallel_elements) {
  5668. return;
  5669. }
  5670. const int I_HW = ih * iw;
  5671. const int O_HW = oh * ow;
  5672. const int nc = idx / O_HW;
  5673. const int cur_oh = idx % O_HW / ow;
  5674. const int cur_ow = idx % O_HW % ow;
  5675. const Ti* i_ptr = src + nc * I_HW;
  5676. To* o_ptr = dst + nc * O_HW;
  5677. const int start_h = cur_oh * sh - ph;
  5678. const int bh = max(0, start_h);
  5679. const int eh = min(ih, start_h + kh);
  5680. const int start_w = cur_ow * sw - pw;
  5681. const int bw = max(0, start_w);
  5682. const int ew = min(iw, start_w + kw);
  5683. const To scale = 1. / (kh * kw);
  5684. To res = 0;
  5685. switch (op) {
  5686. case GGML_OP_POOL_AVG: res = 0; break;
  5687. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5688. }
  5689. for (int i = bh; i < eh; i += 1) {
  5690. for (int j = bw; j < ew; j += 1) {
  5691. #if __CUDA_ARCH__ >= 350
  5692. Ti cur = __ldg(i_ptr + i * iw + j);
  5693. #else
  5694. Ti cur = i_ptr[i * iw + j];
  5695. #endif
  5696. switch (op) {
  5697. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5698. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5699. }
  5700. }
  5701. }
  5702. o_ptr[cur_oh * ow + cur_ow] = res;
  5703. }
  5704. template<int qk, int qr, dequantize_kernel_t dq>
  5705. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5706. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5707. GGML_TENSOR_BINARY_OP_LOCALS
  5708. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5709. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5710. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5711. // strides in elements
  5712. //const size_t s0 = nb0 / ggml_element_size(dst);
  5713. const size_t s1 = nb1 / ggml_element_size(dst);
  5714. const size_t s2 = nb2 / ggml_element_size(dst);
  5715. const size_t s3 = nb3 / ggml_element_size(dst);
  5716. const size_t s10 = nb10 / ggml_element_size(src1);
  5717. const size_t s11 = nb11 / ggml_element_size(src1);
  5718. const size_t s12 = nb12 / ggml_element_size(src1);
  5719. //const size_t s13 = nb13 / ggml_element_size(src1);
  5720. GGML_ASSERT(ne00 % 2 == 0);
  5721. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5722. src0_dd, src1_dd, dst_dd,
  5723. ne00, /*ne01, ne02, ne03,*/
  5724. /*ne10, ne11,*/ ne12, /*ne13,*/
  5725. /* s0,*/ s1, s2, s3,
  5726. /* nb00,*/ nb01, nb02, nb03,
  5727. s10, s11, s12/*, s13*/);
  5728. (void) dst;
  5729. }
  5730. template<typename src0_t>
  5731. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5732. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5733. GGML_TENSOR_BINARY_OP_LOCALS
  5734. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5735. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5736. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5737. // strides in elements
  5738. //const size_t s0 = nb0 / ggml_element_size(dst);
  5739. const size_t s1 = nb1 / ggml_element_size(dst);
  5740. const size_t s2 = nb2 / ggml_element_size(dst);
  5741. const size_t s3 = nb3 / ggml_element_size(dst);
  5742. const size_t s10 = nb10 / ggml_element_size(src1);
  5743. const size_t s11 = nb11 / ggml_element_size(src1);
  5744. const size_t s12 = nb12 / ggml_element_size(src1);
  5745. //const size_t s13 = nb13 / ggml_element_size(src1);
  5746. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5747. src0_dd, src1_dd, dst_dd,
  5748. ne00, /*ne01, ne02, ne03,*/
  5749. /*ne10, ne11,*/ ne12, /*ne13,*/
  5750. /* s0,*/ s1, s2, s3,
  5751. /* nb00,*/ nb01, nb02, nb03,
  5752. s10, s11, s12/*, s13*/);
  5753. (void) dst;
  5754. }
  5755. template<float (*bin_op)(const float, const float)>
  5756. struct bin_bcast_cuda {
  5757. template<typename src0_t, typename src1_t, typename dst_t>
  5758. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5759. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5760. cudaStream_t stream) {
  5761. GGML_TENSOR_BINARY_OP_LOCALS
  5762. int nr0 = ne10/ne0;
  5763. int nr1 = ne11/ne1;
  5764. int nr2 = ne12/ne2;
  5765. int nr3 = ne13/ne3;
  5766. int nr[4] = { nr0, nr1, nr2, nr3 };
  5767. // collapse dimensions until first broadcast dimension
  5768. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5769. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5770. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5771. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5772. auto collapse = [](int64_t cne[]) {
  5773. cne[0] *= cne[1];
  5774. cne[1] = cne[2];
  5775. cne[2] = cne[3];
  5776. cne[3] = 1;
  5777. };
  5778. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5779. cnb[1] *= cne[1];
  5780. cnb[2] *= cne[2];
  5781. cnb[3] *= cne[3];
  5782. };
  5783. for (int i = 0; i < 4; i++) {
  5784. if (nr[i] != 1) {
  5785. break;
  5786. }
  5787. if (i > 0) {
  5788. collapse_nb(cnb0, cne0);
  5789. collapse_nb(cnb1, cne1);
  5790. collapse(cne0);
  5791. collapse(cne1);
  5792. }
  5793. }
  5794. {
  5795. int64_t ne0 = cne0[0];
  5796. int64_t ne1 = cne0[1];
  5797. int64_t ne2 = cne0[2];
  5798. int64_t ne3 = cne0[3];
  5799. int64_t ne10 = cne1[0];
  5800. int64_t ne11 = cne1[1];
  5801. int64_t ne12 = cne1[2];
  5802. int64_t ne13 = cne1[3];
  5803. size_t nb0 = cnb0[0];
  5804. size_t nb1 = cnb0[1];
  5805. size_t nb2 = cnb0[2];
  5806. size_t nb3 = cnb0[3];
  5807. size_t nb10 = cnb1[0];
  5808. size_t nb11 = cnb1[1];
  5809. size_t nb12 = cnb1[2];
  5810. size_t nb13 = cnb1[3];
  5811. size_t s0 = nb0 / sizeof(dst_t);
  5812. size_t s1 = nb1 / sizeof(dst_t);
  5813. size_t s2 = nb2 / sizeof(dst_t);
  5814. size_t s3 = nb3 / sizeof(dst_t);
  5815. size_t s10 = nb10 / sizeof(src1_t);
  5816. size_t s11 = nb11 / sizeof(src1_t);
  5817. size_t s12 = nb12 / sizeof(src1_t);
  5818. size_t s13 = nb13 / sizeof(src1_t);
  5819. GGML_ASSERT(s0 == 1);
  5820. GGML_ASSERT(s10 == 1);
  5821. const int block_size = 128;
  5822. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5823. dim3 block_dims;
  5824. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5825. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5826. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5827. dim3 block_nums(
  5828. (hne0 + block_dims.x - 1) / block_dims.x,
  5829. (ne1 + block_dims.y - 1) / block_dims.y,
  5830. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5831. );
  5832. if (block_nums.z > 65535) {
  5833. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5834. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5835. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5836. src0_dd, src1_dd, dst_dd,
  5837. ne0, ne1, ne2, ne3,
  5838. ne10, ne11, ne12, ne13,
  5839. /* s0, */ s1, s2, s3,
  5840. /* s10, */ s11, s12, s13);
  5841. } else {
  5842. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5843. src0_dd, src1_dd, dst_dd,
  5844. ne0, ne1, ne2, ne3,
  5845. ne10, ne11, ne12, ne13,
  5846. /* s0, */ s1, s2, s3,
  5847. /* s10, */ s11, s12, s13);
  5848. }
  5849. }
  5850. }
  5851. };
  5852. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5853. const int ne10, const int ne11, const int ne12,
  5854. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5855. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5856. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5857. }
  5858. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5859. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5860. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5861. }
  5862. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5863. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5864. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5865. }
  5866. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5867. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5868. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5869. }
  5870. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5871. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5872. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5873. }
  5874. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5875. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5876. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5877. }
  5878. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5879. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5880. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5881. }
  5882. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5883. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5884. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5885. }
  5886. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5887. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5888. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5889. }
  5890. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5891. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5892. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5893. }
  5894. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5895. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5896. if (ncols < 1024) {
  5897. const dim3 block_dims(WARP_SIZE, 1, 1);
  5898. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5899. } else {
  5900. const dim3 block_dims(1024, 1, 1);
  5901. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5902. }
  5903. }
  5904. 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) {
  5905. static const float eps = 1e-6f;
  5906. if (group_size < 1024) {
  5907. const dim3 block_dims(WARP_SIZE, 1, 1);
  5908. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5909. } else {
  5910. const dim3 block_dims(1024, 1, 1);
  5911. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5912. }
  5913. }
  5914. 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) {
  5915. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5916. dim3 gridDim(num_blocks, ne1, ne2);
  5917. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5918. }
  5919. 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) {
  5920. int ne0 = (ne00 * scale_factor);
  5921. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5922. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  5923. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  5924. }
  5925. static void pad_f32_cuda(const float * x, float * dst,
  5926. const int ne00, const int ne01, const int ne02,
  5927. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  5928. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  5929. dim3 gridDim(num_blocks, ne1, ne2);
  5930. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  5931. }
  5932. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5933. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5934. if (ncols < 1024) {
  5935. const dim3 block_dims(WARP_SIZE, 1, 1);
  5936. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5937. } else {
  5938. const dim3 block_dims(1024, 1, 1);
  5939. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5940. }
  5941. }
  5942. 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) {
  5943. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  5944. const dim3 num_blocks(block_num_x, ky, 1);
  5945. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  5946. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  5947. }
  5948. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5949. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5950. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  5951. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5952. }
  5953. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  5954. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  5955. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  5956. const bool need_check = false;
  5957. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5958. } else {
  5959. const bool need_check = true;
  5960. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5961. }
  5962. }
  5963. template<typename dst_t>
  5964. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5965. const int nb = k / QK_K;
  5966. #if QK_K == 256
  5967. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  5968. #else
  5969. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  5970. #endif
  5971. }
  5972. template<typename dst_t>
  5973. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5974. const int nb = k / QK_K;
  5975. #if QK_K == 256
  5976. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  5977. #else
  5978. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  5979. #endif
  5980. }
  5981. template<typename dst_t>
  5982. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5983. const int nb32 = k / 32;
  5984. const int nb = (k + 255) / 256;
  5985. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5986. }
  5987. template<typename dst_t>
  5988. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5989. const int nb32 = k / 32;
  5990. const int nb = (k + 255) / 256;
  5991. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5992. }
  5993. template<typename dst_t>
  5994. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5995. const int nb = k / QK_K;
  5996. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  5997. }
  5998. template<typename dst_t>
  5999. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6000. const int nb = k / QK_K;
  6001. #if QK_K == 256
  6002. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  6003. #else
  6004. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  6005. #endif
  6006. }
  6007. template<typename dst_t>
  6008. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6009. const int nb = k / QK_K;
  6010. #if QK_K == 256
  6011. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  6012. #else
  6013. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  6014. #endif
  6015. }
  6016. template<typename dst_t>
  6017. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6018. const int nb = k / QK_K;
  6019. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  6020. }
  6021. template<typename dst_t>
  6022. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6023. const int nb = k / QK_K;
  6024. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  6025. }
  6026. template<typename dst_t>
  6027. static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6028. const int nb = k / QK_K;
  6029. dequantize_block_iq2_s<<<nb, 32, 0, stream>>>(vx, y);
  6030. }
  6031. template<typename dst_t>
  6032. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6033. const int nb = k / QK_K;
  6034. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  6035. }
  6036. template<typename dst_t>
  6037. static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6038. const int nb = k / QK_K;
  6039. dequantize_block_iq3_s<<<nb, 32, 0, stream>>>(vx, y);
  6040. }
  6041. template<typename dst_t>
  6042. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6043. const int nb = k / QK_K;
  6044. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  6045. }
  6046. template<typename dst_t>
  6047. static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6048. const int nb = (k + QK_K - 1) / QK_K;
  6049. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  6050. }
  6051. template <typename src_t, typename dst_t>
  6052. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  6053. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  6054. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  6055. }
  6056. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  6057. int id;
  6058. switch (type) {
  6059. case GGML_TYPE_Q4_0:
  6060. return dequantize_row_q4_0_cuda;
  6061. case GGML_TYPE_Q4_1:
  6062. return dequantize_row_q4_1_cuda;
  6063. case GGML_TYPE_Q5_0:
  6064. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  6065. case GGML_TYPE_Q5_1:
  6066. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  6067. case GGML_TYPE_Q8_0:
  6068. CUDA_CHECK(cudaGetDevice(&id));
  6069. if (g_device_caps[id].cc >= CC_PASCAL) {
  6070. return dequantize_block_q8_0_f16_cuda;
  6071. }
  6072. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  6073. case GGML_TYPE_Q2_K:
  6074. return dequantize_row_q2_K_cuda;
  6075. case GGML_TYPE_Q3_K:
  6076. return dequantize_row_q3_K_cuda;
  6077. case GGML_TYPE_Q4_K:
  6078. return dequantize_row_q4_K_cuda;
  6079. case GGML_TYPE_Q5_K:
  6080. return dequantize_row_q5_K_cuda;
  6081. case GGML_TYPE_Q6_K:
  6082. return dequantize_row_q6_K_cuda;
  6083. case GGML_TYPE_IQ2_XXS:
  6084. return dequantize_row_iq2_xxs_cuda;
  6085. case GGML_TYPE_IQ2_XS:
  6086. return dequantize_row_iq2_xs_cuda;
  6087. case GGML_TYPE_IQ2_S:
  6088. return dequantize_row_iq2_s_cuda;
  6089. case GGML_TYPE_IQ3_XXS:
  6090. return dequantize_row_iq3_xxs_cuda;
  6091. case GGML_TYPE_IQ1_S:
  6092. return dequantize_row_iq1_s_cuda;
  6093. case GGML_TYPE_IQ4_NL:
  6094. return dequantize_row_iq4_nl_cuda;
  6095. case GGML_TYPE_IQ3_S:
  6096. return dequantize_row_iq3_s_cuda;
  6097. case GGML_TYPE_F32:
  6098. return convert_unary_cuda<float>;
  6099. default:
  6100. return nullptr;
  6101. }
  6102. }
  6103. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  6104. switch (type) {
  6105. case GGML_TYPE_Q4_0:
  6106. return dequantize_row_q4_0_cuda;
  6107. case GGML_TYPE_Q4_1:
  6108. return dequantize_row_q4_1_cuda;
  6109. case GGML_TYPE_Q5_0:
  6110. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  6111. case GGML_TYPE_Q5_1:
  6112. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  6113. case GGML_TYPE_Q8_0:
  6114. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  6115. case GGML_TYPE_Q2_K:
  6116. return dequantize_row_q2_K_cuda;
  6117. case GGML_TYPE_Q3_K:
  6118. return dequantize_row_q3_K_cuda;
  6119. case GGML_TYPE_Q4_K:
  6120. return dequantize_row_q4_K_cuda;
  6121. case GGML_TYPE_Q5_K:
  6122. return dequantize_row_q5_K_cuda;
  6123. case GGML_TYPE_Q6_K:
  6124. return dequantize_row_q6_K_cuda;
  6125. case GGML_TYPE_IQ2_XXS:
  6126. return dequantize_row_iq2_xxs_cuda;
  6127. case GGML_TYPE_IQ2_XS:
  6128. return dequantize_row_iq2_xs_cuda;
  6129. case GGML_TYPE_IQ2_S:
  6130. return dequantize_row_iq2_s_cuda;
  6131. case GGML_TYPE_IQ3_XXS:
  6132. return dequantize_row_iq3_xxs_cuda;
  6133. case GGML_TYPE_IQ1_S:
  6134. return dequantize_row_iq1_s_cuda;
  6135. case GGML_TYPE_IQ4_NL:
  6136. return dequantize_row_iq4_nl_cuda;
  6137. case GGML_TYPE_IQ3_S:
  6138. return dequantize_row_iq3_s_cuda;
  6139. case GGML_TYPE_F16:
  6140. return convert_unary_cuda<half>;
  6141. default:
  6142. return nullptr;
  6143. }
  6144. }
  6145. 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) {
  6146. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6147. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6148. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  6149. const dim3 block_nums(block_num_y, 1, 1);
  6150. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6151. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  6152. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6153. }
  6154. 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) {
  6155. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6156. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6157. const dim3 block_nums(block_num_y, 1, 1);
  6158. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6159. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  6160. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6161. }
  6162. 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) {
  6163. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6164. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6165. const dim3 block_nums(block_num_y, 1, 1);
  6166. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6167. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  6168. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6169. }
  6170. 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) {
  6171. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6172. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6173. const dim3 block_nums(block_num_y, 1, 1);
  6174. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6175. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  6176. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6177. }
  6178. 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) {
  6179. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6180. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6181. const dim3 block_nums(block_num_y, 1, 1);
  6182. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6183. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  6184. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6185. }
  6186. 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) {
  6187. GGML_ASSERT(ncols % QK_K == 0);
  6188. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  6189. const int block_num_y = (nrows + ny - 1) / ny;
  6190. const dim3 block_nums(block_num_y, 1, 1);
  6191. const dim3 block_dims(32, ny, 1);
  6192. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6193. }
  6194. 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) {
  6195. GGML_ASSERT(ncols % QK_K == 0);
  6196. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6197. const int block_num_y = (nrows + ny - 1) / ny;
  6198. const dim3 block_nums(block_num_y, 1, 1);
  6199. const dim3 block_dims(32, ny, 1);
  6200. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6201. }
  6202. 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) {
  6203. GGML_ASSERT(ncols % QK_K == 0);
  6204. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6205. const int block_num_y = (nrows + ny - 1) / ny;
  6206. const dim3 block_nums(block_num_y, 1, 1);
  6207. const dim3 block_dims(32, ny, 1);
  6208. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6209. }
  6210. 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) {
  6211. GGML_ASSERT(ncols % QK_K == 0);
  6212. const dim3 block_dims(32, 1, 1);
  6213. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  6214. }
  6215. 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) {
  6216. GGML_ASSERT(ncols % QK_K == 0);
  6217. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6218. const int block_num_y = (nrows + ny - 1) / ny;
  6219. const dim3 block_nums(block_num_y, 1, 1);
  6220. const dim3 block_dims(32, ny, 1);
  6221. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6222. }
  6223. 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) {
  6224. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6225. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6226. const dim3 block_nums(block_num_y, 1, 1);
  6227. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6228. dequantize_mul_mat_vec<1, 1, convert_f16>
  6229. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6230. }
  6231. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  6232. static void mul_mat_vec_q_cuda(
  6233. const void * vx, const void * vy, float * dst,
  6234. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  6235. GGML_ASSERT(ncols_x % qk == 0);
  6236. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  6237. int id;
  6238. CUDA_CHECK(cudaGetDevice(&id));
  6239. int64_t nwarps = 1;
  6240. int64_t rows_per_cuda_block = 1;
  6241. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  6242. switch(ncols_y) {
  6243. case 1:
  6244. nwarps = 4;
  6245. rows_per_cuda_block = 1;
  6246. break;
  6247. case 2:
  6248. case 3:
  6249. case 4:
  6250. nwarps = 4;
  6251. rows_per_cuda_block = 2;
  6252. break;
  6253. case 5:
  6254. case 6:
  6255. case 7:
  6256. case 8:
  6257. nwarps = 2;
  6258. rows_per_cuda_block = 2;
  6259. break;
  6260. default:
  6261. GGML_ASSERT(false);
  6262. break;
  6263. }
  6264. }
  6265. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  6266. const dim3 block_nums(nblocks, 1, 1);
  6267. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6268. switch (ncols_y) {
  6269. case 1:
  6270. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  6271. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6272. break;
  6273. case 2:
  6274. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  6275. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6276. break;
  6277. case 3:
  6278. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  6279. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6280. break;
  6281. case 4:
  6282. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  6283. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6284. break;
  6285. case 5:
  6286. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  6287. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6288. break;
  6289. case 6:
  6290. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  6291. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6292. break;
  6293. case 7:
  6294. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  6295. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6296. break;
  6297. case 8:
  6298. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  6299. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6300. break;
  6301. default:
  6302. GGML_ASSERT(false);
  6303. break;
  6304. }
  6305. }
  6306. static void ggml_mul_mat_q4_0_q8_1_cuda(
  6307. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6308. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6309. int id;
  6310. CUDA_CHECK(cudaGetDevice(&id));
  6311. const int compute_capability = g_device_caps[id].cc;
  6312. int mmq_x, mmq_y, nwarps;
  6313. if (compute_capability >= CC_RDNA2) {
  6314. mmq_x = MMQ_X_Q4_0_RDNA2;
  6315. mmq_y = MMQ_Y_Q4_0_RDNA2;
  6316. nwarps = NWARPS_Q4_0_RDNA2;
  6317. } else if (compute_capability >= CC_OFFSET_AMD) {
  6318. mmq_x = MMQ_X_Q4_0_RDNA1;
  6319. mmq_y = MMQ_Y_Q4_0_RDNA1;
  6320. nwarps = NWARPS_Q4_0_RDNA1;
  6321. } else if (compute_capability >= CC_VOLTA) {
  6322. mmq_x = MMQ_X_Q4_0_AMPERE;
  6323. mmq_y = MMQ_Y_Q4_0_AMPERE;
  6324. nwarps = NWARPS_Q4_0_AMPERE;
  6325. } else if (compute_capability >= MIN_CC_DP4A) {
  6326. mmq_x = MMQ_X_Q4_0_PASCAL;
  6327. mmq_y = MMQ_Y_Q4_0_PASCAL;
  6328. nwarps = NWARPS_Q4_0_PASCAL;
  6329. } else {
  6330. GGML_ASSERT(false);
  6331. }
  6332. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6333. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6334. const dim3 block_nums(block_num_x, block_num_y, 1);
  6335. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6336. if (nrows_x % mmq_y == 0) {
  6337. const bool need_check = false;
  6338. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6339. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6340. } else {
  6341. const bool need_check = true;
  6342. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6343. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6344. }
  6345. }
  6346. static void ggml_mul_mat_q4_1_q8_1_cuda(
  6347. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6348. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6349. int id;
  6350. CUDA_CHECK(cudaGetDevice(&id));
  6351. const int compute_capability = g_device_caps[id].cc;
  6352. int mmq_x, mmq_y, nwarps;
  6353. if (compute_capability >= CC_RDNA2) {
  6354. mmq_x = MMQ_X_Q4_1_RDNA2;
  6355. mmq_y = MMQ_Y_Q4_1_RDNA2;
  6356. nwarps = NWARPS_Q4_1_RDNA2;
  6357. } else if (compute_capability >= CC_OFFSET_AMD) {
  6358. mmq_x = MMQ_X_Q4_1_RDNA1;
  6359. mmq_y = MMQ_Y_Q4_1_RDNA1;
  6360. nwarps = NWARPS_Q4_1_RDNA1;
  6361. } else if (compute_capability >= CC_VOLTA) {
  6362. mmq_x = MMQ_X_Q4_1_AMPERE;
  6363. mmq_y = MMQ_Y_Q4_1_AMPERE;
  6364. nwarps = NWARPS_Q4_1_AMPERE;
  6365. } else if (compute_capability >= MIN_CC_DP4A) {
  6366. mmq_x = MMQ_X_Q4_1_PASCAL;
  6367. mmq_y = MMQ_Y_Q4_1_PASCAL;
  6368. nwarps = NWARPS_Q4_1_PASCAL;
  6369. } else {
  6370. GGML_ASSERT(false);
  6371. }
  6372. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6373. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6374. const dim3 block_nums(block_num_x, block_num_y, 1);
  6375. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6376. if (nrows_x % mmq_y == 0) {
  6377. const bool need_check = false;
  6378. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6379. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6380. } else {
  6381. const bool need_check = true;
  6382. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6383. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6384. }
  6385. }
  6386. static void ggml_mul_mat_q5_0_q8_1_cuda(
  6387. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6388. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6389. int id;
  6390. CUDA_CHECK(cudaGetDevice(&id));
  6391. const int compute_capability = g_device_caps[id].cc;
  6392. int mmq_x, mmq_y, nwarps;
  6393. if (compute_capability >= CC_RDNA2) {
  6394. mmq_x = MMQ_X_Q5_0_RDNA2;
  6395. mmq_y = MMQ_Y_Q5_0_RDNA2;
  6396. nwarps = NWARPS_Q5_0_RDNA2;
  6397. } else if (compute_capability >= CC_OFFSET_AMD) {
  6398. mmq_x = MMQ_X_Q5_0_RDNA1;
  6399. mmq_y = MMQ_Y_Q5_0_RDNA1;
  6400. nwarps = NWARPS_Q5_0_RDNA1;
  6401. } else if (compute_capability >= CC_VOLTA) {
  6402. mmq_x = MMQ_X_Q5_0_AMPERE;
  6403. mmq_y = MMQ_Y_Q5_0_AMPERE;
  6404. nwarps = NWARPS_Q5_0_AMPERE;
  6405. } else if (compute_capability >= MIN_CC_DP4A) {
  6406. mmq_x = MMQ_X_Q5_0_PASCAL;
  6407. mmq_y = MMQ_Y_Q5_0_PASCAL;
  6408. nwarps = NWARPS_Q5_0_PASCAL;
  6409. } else {
  6410. GGML_ASSERT(false);
  6411. }
  6412. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6413. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6414. const dim3 block_nums(block_num_x, block_num_y, 1);
  6415. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6416. if (nrows_x % mmq_y == 0) {
  6417. const bool need_check = false;
  6418. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6419. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6420. } else {
  6421. const bool need_check = true;
  6422. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6423. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6424. }
  6425. }
  6426. static void ggml_mul_mat_q5_1_q8_1_cuda(
  6427. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6428. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6429. int id;
  6430. CUDA_CHECK(cudaGetDevice(&id));
  6431. const int compute_capability = g_device_caps[id].cc;
  6432. int mmq_x, mmq_y, nwarps;
  6433. if (compute_capability >= CC_RDNA2) {
  6434. mmq_x = MMQ_X_Q5_1_RDNA2;
  6435. mmq_y = MMQ_Y_Q5_1_RDNA2;
  6436. nwarps = NWARPS_Q5_1_RDNA2;
  6437. } else if (compute_capability >= CC_OFFSET_AMD) {
  6438. mmq_x = MMQ_X_Q5_1_RDNA1;
  6439. mmq_y = MMQ_Y_Q5_1_RDNA1;
  6440. nwarps = NWARPS_Q5_1_RDNA1;
  6441. } else if (compute_capability >= CC_VOLTA) {
  6442. mmq_x = MMQ_X_Q5_1_AMPERE;
  6443. mmq_y = MMQ_Y_Q5_1_AMPERE;
  6444. nwarps = NWARPS_Q5_1_AMPERE;
  6445. } else if (compute_capability >= MIN_CC_DP4A) {
  6446. mmq_x = MMQ_X_Q5_1_PASCAL;
  6447. mmq_y = MMQ_Y_Q5_1_PASCAL;
  6448. nwarps = NWARPS_Q5_1_PASCAL;
  6449. } else {
  6450. GGML_ASSERT(false);
  6451. }
  6452. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6453. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6454. const dim3 block_nums(block_num_x, block_num_y, 1);
  6455. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6456. if (nrows_x % mmq_y == 0) {
  6457. const bool need_check = false;
  6458. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6459. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6460. } else {
  6461. const bool need_check = true;
  6462. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6463. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6464. }
  6465. }
  6466. static void ggml_mul_mat_q8_0_q8_1_cuda(
  6467. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6468. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6469. int id;
  6470. CUDA_CHECK(cudaGetDevice(&id));
  6471. const int compute_capability = g_device_caps[id].cc;
  6472. int mmq_x, mmq_y, nwarps;
  6473. if (compute_capability >= CC_RDNA2) {
  6474. mmq_x = MMQ_X_Q8_0_RDNA2;
  6475. mmq_y = MMQ_Y_Q8_0_RDNA2;
  6476. nwarps = NWARPS_Q8_0_RDNA2;
  6477. } else if (compute_capability >= CC_OFFSET_AMD) {
  6478. mmq_x = MMQ_X_Q8_0_RDNA1;
  6479. mmq_y = MMQ_Y_Q8_0_RDNA1;
  6480. nwarps = NWARPS_Q8_0_RDNA1;
  6481. } else if (compute_capability >= CC_VOLTA) {
  6482. mmq_x = MMQ_X_Q8_0_AMPERE;
  6483. mmq_y = MMQ_Y_Q8_0_AMPERE;
  6484. nwarps = NWARPS_Q8_0_AMPERE;
  6485. } else if (compute_capability >= MIN_CC_DP4A) {
  6486. mmq_x = MMQ_X_Q8_0_PASCAL;
  6487. mmq_y = MMQ_Y_Q8_0_PASCAL;
  6488. nwarps = NWARPS_Q8_0_PASCAL;
  6489. } else {
  6490. GGML_ASSERT(false);
  6491. }
  6492. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6493. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6494. const dim3 block_nums(block_num_x, block_num_y, 1);
  6495. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6496. if (nrows_x % mmq_y == 0) {
  6497. const bool need_check = false;
  6498. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6499. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6500. } else {
  6501. const bool need_check = true;
  6502. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6503. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6504. }
  6505. }
  6506. static void ggml_mul_mat_q2_K_q8_1_cuda(
  6507. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6508. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6509. int id;
  6510. CUDA_CHECK(cudaGetDevice(&id));
  6511. const int compute_capability = g_device_caps[id].cc;
  6512. int mmq_x, mmq_y, nwarps;
  6513. if (compute_capability >= CC_RDNA2) {
  6514. mmq_x = MMQ_X_Q2_K_RDNA2;
  6515. mmq_y = MMQ_Y_Q2_K_RDNA2;
  6516. nwarps = NWARPS_Q2_K_RDNA2;
  6517. } else if (compute_capability >= CC_OFFSET_AMD) {
  6518. mmq_x = MMQ_X_Q2_K_RDNA1;
  6519. mmq_y = MMQ_Y_Q2_K_RDNA1;
  6520. nwarps = NWARPS_Q2_K_RDNA1;
  6521. } else if (compute_capability >= CC_VOLTA) {
  6522. mmq_x = MMQ_X_Q2_K_AMPERE;
  6523. mmq_y = MMQ_Y_Q2_K_AMPERE;
  6524. nwarps = NWARPS_Q2_K_AMPERE;
  6525. } else if (compute_capability >= MIN_CC_DP4A) {
  6526. mmq_x = MMQ_X_Q2_K_PASCAL;
  6527. mmq_y = MMQ_Y_Q2_K_PASCAL;
  6528. nwarps = NWARPS_Q2_K_PASCAL;
  6529. } else {
  6530. GGML_ASSERT(false);
  6531. }
  6532. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6533. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6534. const dim3 block_nums(block_num_x, block_num_y, 1);
  6535. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6536. if (nrows_x % mmq_y == 0) {
  6537. const bool need_check = false;
  6538. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6539. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6540. } else {
  6541. const bool need_check = true;
  6542. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6543. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6544. }
  6545. }
  6546. static void ggml_mul_mat_q3_K_q8_1_cuda(
  6547. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6548. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6549. #if QK_K == 256
  6550. int id;
  6551. CUDA_CHECK(cudaGetDevice(&id));
  6552. const int compute_capability = g_device_caps[id].cc;
  6553. int mmq_x, mmq_y, nwarps;
  6554. if (compute_capability >= CC_RDNA2) {
  6555. mmq_x = MMQ_X_Q3_K_RDNA2;
  6556. mmq_y = MMQ_Y_Q3_K_RDNA2;
  6557. nwarps = NWARPS_Q3_K_RDNA2;
  6558. } else if (compute_capability >= CC_OFFSET_AMD) {
  6559. mmq_x = MMQ_X_Q3_K_RDNA1;
  6560. mmq_y = MMQ_Y_Q3_K_RDNA1;
  6561. nwarps = NWARPS_Q3_K_RDNA1;
  6562. } else if (compute_capability >= CC_VOLTA) {
  6563. mmq_x = MMQ_X_Q3_K_AMPERE;
  6564. mmq_y = MMQ_Y_Q3_K_AMPERE;
  6565. nwarps = NWARPS_Q3_K_AMPERE;
  6566. } else if (compute_capability >= MIN_CC_DP4A) {
  6567. mmq_x = MMQ_X_Q3_K_PASCAL;
  6568. mmq_y = MMQ_Y_Q3_K_PASCAL;
  6569. nwarps = NWARPS_Q3_K_PASCAL;
  6570. } else {
  6571. GGML_ASSERT(false);
  6572. }
  6573. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6574. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6575. const dim3 block_nums(block_num_x, block_num_y, 1);
  6576. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6577. if (nrows_x % mmq_y == 0) {
  6578. const bool need_check = false;
  6579. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6580. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6581. } else {
  6582. const bool need_check = true;
  6583. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6584. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6585. }
  6586. #endif
  6587. }
  6588. static void ggml_mul_mat_q4_K_q8_1_cuda(
  6589. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6590. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6591. int id;
  6592. CUDA_CHECK(cudaGetDevice(&id));
  6593. const int compute_capability = g_device_caps[id].cc;
  6594. int mmq_x, mmq_y, nwarps;
  6595. if (compute_capability >= CC_RDNA2) {
  6596. mmq_x = MMQ_X_Q4_K_RDNA2;
  6597. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6598. nwarps = NWARPS_Q4_K_RDNA2;
  6599. } else if (compute_capability >= CC_OFFSET_AMD) {
  6600. mmq_x = MMQ_X_Q4_K_RDNA1;
  6601. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6602. nwarps = NWARPS_Q4_K_RDNA1;
  6603. } else if (compute_capability >= CC_VOLTA) {
  6604. mmq_x = MMQ_X_Q4_K_AMPERE;
  6605. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6606. nwarps = NWARPS_Q4_K_AMPERE;
  6607. } else if (compute_capability >= MIN_CC_DP4A) {
  6608. mmq_x = MMQ_X_Q4_K_PASCAL;
  6609. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6610. nwarps = NWARPS_Q4_K_PASCAL;
  6611. } else {
  6612. GGML_ASSERT(false);
  6613. }
  6614. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6615. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6616. const dim3 block_nums(block_num_x, block_num_y, 1);
  6617. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6618. if (nrows_x % mmq_y == 0) {
  6619. const bool need_check = false;
  6620. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6621. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6622. } else {
  6623. const bool need_check = true;
  6624. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6625. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6626. }
  6627. }
  6628. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6629. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6630. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6631. int id;
  6632. CUDA_CHECK(cudaGetDevice(&id));
  6633. const int compute_capability = g_device_caps[id].cc;
  6634. int mmq_x, mmq_y, nwarps;
  6635. if (compute_capability >= CC_RDNA2) {
  6636. mmq_x = MMQ_X_Q5_K_RDNA2;
  6637. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6638. nwarps = NWARPS_Q5_K_RDNA2;
  6639. } else if (compute_capability >= CC_OFFSET_AMD) {
  6640. mmq_x = MMQ_X_Q5_K_RDNA1;
  6641. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6642. nwarps = NWARPS_Q5_K_RDNA1;
  6643. } else if (compute_capability >= CC_VOLTA) {
  6644. mmq_x = MMQ_X_Q5_K_AMPERE;
  6645. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6646. nwarps = NWARPS_Q5_K_AMPERE;
  6647. } else if (compute_capability >= MIN_CC_DP4A) {
  6648. mmq_x = MMQ_X_Q5_K_PASCAL;
  6649. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6650. nwarps = NWARPS_Q5_K_PASCAL;
  6651. } else {
  6652. GGML_ASSERT(false);
  6653. }
  6654. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6655. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6656. const dim3 block_nums(block_num_x, block_num_y, 1);
  6657. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6658. if (nrows_x % mmq_y == 0) {
  6659. const bool need_check = false;
  6660. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6661. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6662. } else {
  6663. const bool need_check = true;
  6664. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6665. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6666. }
  6667. }
  6668. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6669. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6670. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6671. int id;
  6672. CUDA_CHECK(cudaGetDevice(&id));
  6673. const int compute_capability = g_device_caps[id].cc;
  6674. int mmq_x, mmq_y, nwarps;
  6675. if (compute_capability >= CC_RDNA2) {
  6676. mmq_x = MMQ_X_Q6_K_RDNA2;
  6677. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6678. nwarps = NWARPS_Q6_K_RDNA2;
  6679. } else if (compute_capability >= CC_OFFSET_AMD) {
  6680. mmq_x = MMQ_X_Q6_K_RDNA1;
  6681. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6682. nwarps = NWARPS_Q6_K_RDNA1;
  6683. } else if (compute_capability >= CC_VOLTA) {
  6684. mmq_x = MMQ_X_Q6_K_AMPERE;
  6685. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6686. nwarps = NWARPS_Q6_K_AMPERE;
  6687. } else if (compute_capability >= MIN_CC_DP4A) {
  6688. mmq_x = MMQ_X_Q6_K_PASCAL;
  6689. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6690. nwarps = NWARPS_Q6_K_PASCAL;
  6691. } else {
  6692. GGML_ASSERT(false);
  6693. }
  6694. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6695. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6696. const dim3 block_nums(block_num_x, block_num_y, 1);
  6697. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6698. if (nrows_x % mmq_y == 0) {
  6699. const bool need_check = false;
  6700. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6701. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6702. } else {
  6703. const bool need_check = true;
  6704. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6705. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6706. }
  6707. }
  6708. static void ggml_mul_mat_p021_f16_f32_cuda(
  6709. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6710. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6711. const dim3 block_nums(1, nrows_x, nchannels_y);
  6712. const dim3 block_dims(WARP_SIZE, 1, 1);
  6713. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6714. }
  6715. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6716. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6717. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6718. const dim3 block_nums(1, nrows_x, nchannels_y);
  6719. const dim3 block_dims(WARP_SIZE, 1, 1);
  6720. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6721. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6722. }
  6723. static void ggml_cpy_f16_f32_cuda(
  6724. const char * cx, char * cdst, const int ne,
  6725. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6726. 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) {
  6727. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6728. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6729. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6730. }
  6731. static void ggml_cpy_f32_f32_cuda(
  6732. const char * cx, char * cdst, const int ne,
  6733. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6734. 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) {
  6735. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6736. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6737. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6738. }
  6739. static void ggml_cpy_f32_f16_cuda(
  6740. const char * cx, char * cdst, const int ne,
  6741. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6742. 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) {
  6743. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6744. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6745. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6746. }
  6747. static void ggml_cpy_f32_q8_0_cuda(
  6748. const char * cx, char * cdst, const int ne,
  6749. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6750. 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) {
  6751. GGML_ASSERT(ne % QK8_0 == 0);
  6752. const int num_blocks = ne / QK8_0;
  6753. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6754. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6755. }
  6756. static void ggml_cpy_f32_q4_0_cuda(
  6757. const char * cx, char * cdst, const int ne,
  6758. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6759. 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) {
  6760. GGML_ASSERT(ne % QK4_0 == 0);
  6761. const int num_blocks = ne / QK4_0;
  6762. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6763. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6764. }
  6765. static void ggml_cpy_f32_q4_1_cuda(
  6766. const char * cx, char * cdst, const int ne,
  6767. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6768. 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) {
  6769. GGML_ASSERT(ne % QK4_1 == 0);
  6770. const int num_blocks = ne / QK4_1;
  6771. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6772. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6773. }
  6774. static void ggml_cpy_f16_f16_cuda(
  6775. const char * cx, char * cdst, const int ne,
  6776. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6777. 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) {
  6778. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6779. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6780. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6781. }
  6782. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  6783. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  6784. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  6785. }
  6786. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  6787. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  6788. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  6789. }
  6790. template<typename T>
  6791. static void rope_cuda(
  6792. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6793. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6794. ) {
  6795. GGML_ASSERT(ncols % 2 == 0);
  6796. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6797. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6798. const dim3 block_nums(nrows, num_blocks_x, 1);
  6799. if (pos == nullptr) {
  6800. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6801. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6802. );
  6803. } else {
  6804. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6805. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6806. );
  6807. }
  6808. }
  6809. template<typename T>
  6810. static void rope_neox_cuda(
  6811. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6812. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6813. ) {
  6814. GGML_ASSERT(ncols % 2 == 0);
  6815. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6816. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6817. const dim3 block_nums(nrows, num_blocks_x, 1);
  6818. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6819. const float inv_ndims = -1.0f / n_dims;
  6820. if (pos == nullptr) {
  6821. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6822. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6823. theta_scale, inv_ndims
  6824. );
  6825. } else {
  6826. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6827. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6828. theta_scale, inv_ndims
  6829. );
  6830. }
  6831. }
  6832. static void rope_glm_f32_cuda(
  6833. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6834. float freq_base, int n_ctx, cudaStream_t stream
  6835. ) {
  6836. GGML_ASSERT(ncols % 4 == 0);
  6837. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6838. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6839. const dim3 block_nums(num_blocks_x, nrows, 1);
  6840. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6841. }
  6842. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6843. const int k_rows, const int n_heads_log2_floor, const float m0,
  6844. const float m1, cudaStream_t stream) {
  6845. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6846. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6847. const dim3 block_nums(num_blocks_x, nrows, 1);
  6848. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6849. }
  6850. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6851. const dim3 block_dims(WARP_SIZE, 1, 1);
  6852. const dim3 block_nums(nrows, 1, 1);
  6853. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6854. }
  6855. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6856. // bitonic sort requires ncols to be power of 2
  6857. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6858. const dim3 block_dims(ncols, 1, 1);
  6859. const dim3 block_nums(1, nrows, 1);
  6860. if (order == GGML_SORT_ORDER_ASC) {
  6861. k_argsort_f32_i32<GGML_SORT_ORDER_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6862. } else if (order == GGML_SORT_ORDER_DESC) {
  6863. k_argsort_f32_i32<GGML_SORT_ORDER_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6864. } else {
  6865. GGML_ASSERT(false);
  6866. }
  6867. }
  6868. 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) {
  6869. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6870. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6871. const dim3 block_nums(nrows_x, block_num_x, 1);
  6872. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6873. }
  6874. 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) {
  6875. int nth = WARP_SIZE;
  6876. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6877. const dim3 block_dims(nth, 1, 1);
  6878. const dim3 block_nums(nrows_x, 1, 1);
  6879. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6880. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6881. const uint32_t n_head_kv = nrows_x/nrows_y;
  6882. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6883. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6884. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6885. if (shmem < g_device_caps[g_main_device].smpb) {
  6886. switch (ncols_x) {
  6887. case 32:
  6888. 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);
  6889. break;
  6890. case 64:
  6891. 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);
  6892. break;
  6893. case 128:
  6894. 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);
  6895. break;
  6896. case 256:
  6897. 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);
  6898. break;
  6899. case 512:
  6900. 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);
  6901. break;
  6902. case 1024:
  6903. 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);
  6904. break;
  6905. case 2048:
  6906. 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);
  6907. break;
  6908. case 4096:
  6909. 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);
  6910. break;
  6911. default:
  6912. 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);
  6913. break;
  6914. }
  6915. } else {
  6916. const size_t shmem_low = WARP_SIZE*sizeof(float);
  6917. 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);
  6918. }
  6919. }
  6920. template <typename T>
  6921. static void im2col_cuda(const float* x, T* dst,
  6922. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  6923. int batch, int batch_offset, int offset_delta,
  6924. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  6925. const int parallel_elements = OW * KW * KH;
  6926. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  6927. dim3 block_nums(num_blocks, OH, batch * IC);
  6928. 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);
  6929. }
  6930. // buffer pool for cuda
  6931. #define MAX_CUDA_BUFFERS 256
  6932. struct scoped_spin_lock {
  6933. std::atomic_flag& lock;
  6934. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  6935. while (lock.test_and_set(std::memory_order_acquire)) {
  6936. ; // spin
  6937. }
  6938. }
  6939. ~scoped_spin_lock() {
  6940. lock.clear(std::memory_order_release);
  6941. }
  6942. scoped_spin_lock(const scoped_spin_lock&) = delete;
  6943. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  6944. };
  6945. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  6946. // #define DEBUG_CUDA_MALLOC
  6947. struct ggml_cuda_buffer {
  6948. void * ptr = nullptr;
  6949. size_t size = 0;
  6950. };
  6951. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  6952. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  6953. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  6954. scoped_spin_lock lock(g_cuda_pool_lock);
  6955. #ifdef DEBUG_CUDA_MALLOC
  6956. int nnz = 0;
  6957. size_t max_size = 0;
  6958. #endif
  6959. size_t best_diff = 1ull << 36;
  6960. int ibest = -1;
  6961. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6962. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6963. if (b.ptr != nullptr) {
  6964. #ifdef DEBUG_CUDA_MALLOC
  6965. ++nnz;
  6966. if (b.size > max_size) max_size = b.size;
  6967. #endif
  6968. if (b.size >= size) {
  6969. size_t diff = b.size - size;
  6970. if (diff < best_diff) {
  6971. best_diff = diff;
  6972. ibest = i;
  6973. if (!best_diff) {
  6974. void * ptr = b.ptr;
  6975. *actual_size = b.size;
  6976. b.ptr = nullptr;
  6977. b.size = 0;
  6978. return ptr;
  6979. }
  6980. }
  6981. }
  6982. }
  6983. }
  6984. if (ibest >= 0) {
  6985. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  6986. void * ptr = b.ptr;
  6987. *actual_size = b.size;
  6988. b.ptr = nullptr;
  6989. b.size = 0;
  6990. return ptr;
  6991. }
  6992. void * ptr;
  6993. size_t look_ahead_size = (size_t) (1.05 * size);
  6994. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  6995. ggml_cuda_set_device(device);
  6996. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  6997. *actual_size = look_ahead_size;
  6998. g_cuda_pool_size[device] += look_ahead_size;
  6999. #ifdef DEBUG_CUDA_MALLOC
  7000. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, device, nnz,
  7001. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[device]/1024/1024), (uint32_t)(size/1024/1024));
  7002. #endif
  7003. return ptr;
  7004. }
  7005. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  7006. scoped_spin_lock lock(g_cuda_pool_lock);
  7007. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  7008. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  7009. if (b.ptr == nullptr) {
  7010. b.ptr = ptr;
  7011. b.size = size;
  7012. return;
  7013. }
  7014. }
  7015. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  7016. ggml_cuda_set_device(device);
  7017. CUDA_CHECK(cudaFree(ptr));
  7018. g_cuda_pool_size[device] -= size;
  7019. }
  7020. #if !defined(GGML_USE_HIPBLAS)
  7021. // pool with virtual memory
  7022. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  7023. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  7024. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  7025. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  7026. scoped_spin_lock lock(g_cuda_pool_lock);
  7027. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  7028. const size_t alignment = 128;
  7029. size = alignment * ((size + alignment - 1) / alignment);
  7030. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  7031. if (size > avail) {
  7032. // round up to the next multiple of the granularity
  7033. size_t reserve_size = size - avail;
  7034. const size_t granularity = g_device_caps[device].vmm_granularity;
  7035. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  7036. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  7037. // allocate more physical memory
  7038. CUmemAllocationProp prop = {};
  7039. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  7040. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  7041. prop.location.id = device;
  7042. CUmemGenericAllocationHandle handle;
  7043. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  7044. // reserve virtual address space (if not already reserved)
  7045. if (g_cuda_pool_addr[device] == 0) {
  7046. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  7047. }
  7048. // map at the end of the pool
  7049. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  7050. // the memory allocation handle is no longer needed after mapping
  7051. CU_CHECK(cuMemRelease(handle));
  7052. // set access
  7053. CUmemAccessDesc access = {};
  7054. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  7055. access.location.id = device;
  7056. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  7057. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  7058. // add to the pool
  7059. g_cuda_pool_size[device] += reserve_size;
  7060. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  7061. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  7062. // (unsigned long long) (reserve_size/1024/1024));
  7063. }
  7064. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  7065. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  7066. *actual_size = size;
  7067. g_cuda_pool_used[device] += size;
  7068. #ifdef DEBUG_CUDA_MALLOC
  7069. printf("cuda pool[%d]: allocated %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  7070. #endif
  7071. return ptr;
  7072. }
  7073. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  7074. scoped_spin_lock lock(g_cuda_pool_lock);
  7075. #ifdef DEBUG_CUDA_MALLOC
  7076. printf("cuda pool[%d]: freed %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  7077. #endif
  7078. g_cuda_pool_used[device] -= size;
  7079. // all deallocations must be in reverse order of the allocations
  7080. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  7081. }
  7082. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  7083. if (g_device_caps[device].vmm) {
  7084. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  7085. } else {
  7086. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  7087. }
  7088. }
  7089. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  7090. if (g_device_caps[device].vmm) {
  7091. ggml_cuda_pool_free_vmm(device, ptr, size);
  7092. } else {
  7093. ggml_cuda_pool_free_leg(device, ptr, size);
  7094. }
  7095. }
  7096. #else
  7097. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  7098. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  7099. #endif // !defined(GGML_USE_HIPBLAS)
  7100. template<typename T>
  7101. struct cuda_pool_alloc {
  7102. int device = -1;
  7103. T * ptr = nullptr;
  7104. size_t actual_size = 0;
  7105. // size is in number of elements
  7106. T * alloc(size_t size) {
  7107. GGML_ASSERT(ptr == nullptr);
  7108. CUDA_CHECK(cudaGetDevice(&device));
  7109. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  7110. return ptr;
  7111. }
  7112. cuda_pool_alloc(size_t size) {
  7113. alloc(size);
  7114. }
  7115. ~cuda_pool_alloc() {
  7116. if (ptr != nullptr) {
  7117. ggml_cuda_pool_free(device, ptr, actual_size);
  7118. }
  7119. }
  7120. T * get() {
  7121. return ptr;
  7122. }
  7123. cuda_pool_alloc() = default;
  7124. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  7125. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  7126. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  7127. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  7128. };
  7129. static bool g_cublas_loaded = false;
  7130. GGML_CALL bool ggml_cublas_loaded(void) {
  7131. return g_cublas_loaded;
  7132. }
  7133. GGML_CALL void ggml_init_cublas() {
  7134. static bool initialized = false;
  7135. if (!initialized) {
  7136. #ifdef __HIP_PLATFORM_AMD__
  7137. // Workaround for a rocBLAS bug when using multiple graphics cards:
  7138. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  7139. rocblas_initialize();
  7140. CUDA_CHECK(cudaDeviceSynchronize());
  7141. #endif
  7142. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  7143. initialized = true;
  7144. g_cublas_loaded = false;
  7145. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  7146. return;
  7147. }
  7148. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  7149. int64_t total_vram = 0;
  7150. #if defined(GGML_CUDA_FORCE_MMQ)
  7151. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  7152. #else
  7153. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  7154. #endif
  7155. #if defined(CUDA_USE_TENSOR_CORES)
  7156. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  7157. #else
  7158. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  7159. #endif
  7160. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  7161. for (int id = 0; id < g_device_count; ++id) {
  7162. int device_vmm = 0;
  7163. #if !defined(GGML_USE_HIPBLAS)
  7164. CUdevice device;
  7165. CU_CHECK(cuDeviceGet(&device, id));
  7166. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  7167. if (device_vmm) {
  7168. CUmemAllocationProp alloc_prop = {};
  7169. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  7170. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  7171. alloc_prop.location.id = id;
  7172. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  7173. }
  7174. #endif // !defined(GGML_USE_HIPBLAS)
  7175. g_device_caps[id].vmm = !!device_vmm;
  7176. cudaDeviceProp prop;
  7177. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  7178. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  7179. g_default_tensor_split[id] = total_vram;
  7180. total_vram += prop.totalGlobalMem;
  7181. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7182. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  7183. #else
  7184. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  7185. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7186. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  7187. }
  7188. for (int id = 0; id < g_device_count; ++id) {
  7189. g_default_tensor_split[id] /= total_vram;
  7190. }
  7191. for (int id = 0; id < g_device_count; ++id) {
  7192. ggml_cuda_set_device(id);
  7193. // create cuda streams
  7194. for (int is = 0; is < MAX_STREAMS; ++is) {
  7195. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  7196. }
  7197. // create cublas handle
  7198. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  7199. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  7200. }
  7201. // configure logging to stdout
  7202. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  7203. initialized = true;
  7204. g_cublas_loaded = true;
  7205. }
  7206. }
  7207. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  7208. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  7209. return nullptr;
  7210. }
  7211. void * ptr = nullptr;
  7212. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  7213. if (err != cudaSuccess) {
  7214. // clear the error
  7215. cudaGetLastError();
  7216. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  7217. size/1024.0/1024.0, cudaGetErrorString(err));
  7218. return nullptr;
  7219. }
  7220. return ptr;
  7221. }
  7222. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  7223. CUDA_CHECK(cudaFreeHost(ptr));
  7224. }
  7225. static cudaError_t ggml_cuda_cpy_tensor_2d(
  7226. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  7227. cudaMemcpyKind kind;
  7228. char * src_ptr;
  7229. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  7230. kind = cudaMemcpyHostToDevice;
  7231. src_ptr = (char *) src->data;
  7232. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  7233. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  7234. kind = cudaMemcpyDeviceToDevice;
  7235. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  7236. int id;
  7237. CUDA_CHECK(cudaGetDevice(&id));
  7238. src_ptr = (char *) extra->data_device[id];
  7239. } else {
  7240. GGML_ASSERT(false);
  7241. }
  7242. char * dst_ptr = (char *) dst;
  7243. const int64_t ne0 = src->ne[0];
  7244. const int64_t nb0 = src->nb[0];
  7245. const int64_t nb1 = src->nb[1];
  7246. const int64_t nb2 = src->nb[2];
  7247. const int64_t nb3 = src->nb[3];
  7248. const enum ggml_type type = src->type;
  7249. const int64_t ts = ggml_type_size(type);
  7250. const int64_t bs = ggml_blck_size(type);
  7251. int64_t i1_diff = i1_high - i1_low;
  7252. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  7253. if (nb0 == ts && nb1 == ts*ne0/bs) {
  7254. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  7255. } else if (nb0 == ts) {
  7256. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  7257. } else {
  7258. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  7259. const void * rx = (const void *) ((const char *) x + i1*nb1);
  7260. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  7261. // pretend the row is a matrix with cols=1
  7262. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  7263. if (r != cudaSuccess) return r;
  7264. }
  7265. return cudaSuccess;
  7266. }
  7267. }
  7268. static void ggml_cuda_op_get_rows(
  7269. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7270. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  7271. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7272. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7273. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  7274. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  7275. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  7276. const int32_t * src1_i32 = (const int32_t *) src1_d;
  7277. switch (src0->type) {
  7278. case GGML_TYPE_F16:
  7279. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  7280. break;
  7281. case GGML_TYPE_F32:
  7282. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7283. break;
  7284. case GGML_TYPE_Q4_0:
  7285. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7286. break;
  7287. case GGML_TYPE_Q4_1:
  7288. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7289. break;
  7290. case GGML_TYPE_Q5_0:
  7291. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7292. break;
  7293. case GGML_TYPE_Q5_1:
  7294. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7295. break;
  7296. case GGML_TYPE_Q8_0:
  7297. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7298. break;
  7299. default:
  7300. // TODO: k-quants
  7301. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  7302. GGML_ASSERT(false);
  7303. break;
  7304. }
  7305. }
  7306. template<class op>
  7307. static void ggml_cuda_op_bin_bcast(
  7308. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7309. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7310. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7311. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7312. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7313. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7314. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  7315. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  7316. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  7317. } else {
  7318. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  7319. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  7320. GGML_ASSERT(false);
  7321. }
  7322. }
  7323. static void ggml_cuda_op_repeat(
  7324. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7325. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  7326. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  7327. (void) src1;
  7328. (void) src1_d;
  7329. }
  7330. static void ggml_cuda_op_add(
  7331. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7332. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7333. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7334. }
  7335. static void ggml_cuda_op_acc(
  7336. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7337. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7338. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7339. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7340. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7341. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  7342. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  7343. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  7344. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  7345. int offset = dst->op_params[3] / 4; // offset in bytes
  7346. 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);
  7347. (void) dst;
  7348. }
  7349. static void ggml_cuda_op_mul(
  7350. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7351. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7352. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7353. }
  7354. static void ggml_cuda_op_div(
  7355. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7356. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7357. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7358. }
  7359. static void ggml_cuda_op_gelu(
  7360. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7361. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7362. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7363. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7364. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7365. (void) src1;
  7366. (void) dst;
  7367. (void) src1_dd;
  7368. }
  7369. static void ggml_cuda_op_silu(
  7370. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7371. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7372. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7373. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7374. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7375. (void) src1;
  7376. (void) dst;
  7377. (void) src1_dd;
  7378. }
  7379. static void ggml_cuda_op_gelu_quick(
  7380. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7381. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7382. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7383. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7384. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7385. (void) src1;
  7386. (void) dst;
  7387. (void) src1_dd;
  7388. }
  7389. static void ggml_cuda_op_tanh(
  7390. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7391. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7392. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7393. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7394. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7395. (void) src1;
  7396. (void) dst;
  7397. (void) src1_dd;
  7398. }
  7399. static void ggml_cuda_op_relu(
  7400. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7401. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7402. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7403. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7404. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7405. (void) src1;
  7406. (void) dst;
  7407. (void) src1_dd;
  7408. }
  7409. static void ggml_cuda_op_hardsigmoid(
  7410. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7411. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7412. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7413. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7414. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7415. (void) src1;
  7416. (void) dst;
  7417. (void) src1_dd;
  7418. }
  7419. static void ggml_cuda_op_hardswish(
  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_ASSERT(src0->type == GGML_TYPE_F32);
  7423. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7424. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7425. (void) src1;
  7426. (void) dst;
  7427. (void) src1_dd;
  7428. }
  7429. static void ggml_cuda_op_leaky_relu(
  7430. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7431. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7432. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7433. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7434. float negative_slope;
  7435. memcpy(&negative_slope, dst->op_params, sizeof(float));
  7436. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  7437. (void) src1;
  7438. (void) dst;
  7439. (void) src1_dd;
  7440. }
  7441. static void ggml_cuda_op_sqr(
  7442. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7443. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7444. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7445. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7446. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7447. (void) src1;
  7448. (void) dst;
  7449. (void) src1_dd;
  7450. }
  7451. static void ggml_cuda_op_norm(
  7452. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7453. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7454. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7455. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7456. const int64_t ne00 = src0->ne[0];
  7457. const int64_t nrows = ggml_nrows(src0);
  7458. float eps;
  7459. memcpy(&eps, dst->op_params, sizeof(float));
  7460. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7461. (void) src1;
  7462. (void) dst;
  7463. (void) src1_dd;
  7464. }
  7465. static void ggml_cuda_op_group_norm(
  7466. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7467. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7468. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7469. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7470. int num_groups = dst->op_params[0];
  7471. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  7472. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  7473. (void) src1;
  7474. (void) dst;
  7475. (void) src1_dd;
  7476. }
  7477. static void ggml_cuda_op_concat(
  7478. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7479. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7480. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7481. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7482. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7483. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  7484. 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);
  7485. }
  7486. (void) src1;
  7487. (void) dst;
  7488. }
  7489. static void ggml_cuda_op_upscale(
  7490. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7491. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7492. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7493. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7494. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7495. const int scale_factor = dst->op_params[0];
  7496. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  7497. (void) src1;
  7498. (void) dst;
  7499. (void) src1_dd;
  7500. }
  7501. static void ggml_cuda_op_pad(
  7502. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7503. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7504. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7505. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7506. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7507. pad_f32_cuda(src0_dd, dst_dd,
  7508. src0->ne[0], src0->ne[1], src0->ne[2],
  7509. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  7510. (void) src1;
  7511. (void) dst;
  7512. (void) src1_dd;
  7513. }
  7514. static void ggml_cuda_op_rms_norm(
  7515. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7516. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7517. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7518. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7519. const int64_t ne00 = src0->ne[0];
  7520. const int64_t nrows = ggml_nrows(src0);
  7521. float eps;
  7522. memcpy(&eps, dst->op_params, sizeof(float));
  7523. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7524. (void) src1;
  7525. (void) dst;
  7526. (void) src1_dd;
  7527. }
  7528. static void ggml_cuda_op_mul_mat_q(
  7529. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7530. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7531. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7532. const int64_t ne00 = src0->ne[0];
  7533. const int64_t ne10 = src1->ne[0];
  7534. GGML_ASSERT(ne10 % QK8_1 == 0);
  7535. const int64_t ne0 = dst->ne[0];
  7536. const int64_t row_diff = row_high - row_low;
  7537. int id;
  7538. CUDA_CHECK(cudaGetDevice(&id));
  7539. // the main device has a larger memory buffer to hold the results from all GPUs
  7540. // nrows_dst == nrows of the matrix that the kernel writes into
  7541. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7542. switch (src0->type) {
  7543. case GGML_TYPE_Q4_0:
  7544. 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);
  7545. break;
  7546. case GGML_TYPE_Q4_1:
  7547. 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);
  7548. break;
  7549. case GGML_TYPE_Q5_0:
  7550. 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);
  7551. break;
  7552. case GGML_TYPE_Q5_1:
  7553. 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);
  7554. break;
  7555. case GGML_TYPE_Q8_0:
  7556. 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);
  7557. break;
  7558. case GGML_TYPE_Q2_K:
  7559. 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);
  7560. break;
  7561. case GGML_TYPE_Q3_K:
  7562. 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);
  7563. break;
  7564. case GGML_TYPE_Q4_K:
  7565. 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);
  7566. break;
  7567. case GGML_TYPE_Q5_K:
  7568. 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);
  7569. break;
  7570. case GGML_TYPE_Q6_K:
  7571. 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);
  7572. break;
  7573. default:
  7574. GGML_ASSERT(false);
  7575. break;
  7576. }
  7577. (void) src1;
  7578. (void) dst;
  7579. (void) src1_ddf_i;
  7580. }
  7581. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  7582. int64_t min_compute_capability = INT_MAX;
  7583. int64_t max_compute_capability = INT_MIN;
  7584. for (int id = 0; id < g_device_count; ++id) {
  7585. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  7586. if (min_compute_capability > g_device_caps[id].cc) {
  7587. min_compute_capability = g_device_caps[id].cc;
  7588. }
  7589. if (max_compute_capability < g_device_caps[id].cc) {
  7590. max_compute_capability = g_device_caps[id].cc;
  7591. }
  7592. }
  7593. }
  7594. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7595. switch(type) {
  7596. case GGML_TYPE_Q4_0:
  7597. case GGML_TYPE_Q4_1:
  7598. case GGML_TYPE_Q5_0:
  7599. case GGML_TYPE_Q5_1:
  7600. case GGML_TYPE_Q8_0:
  7601. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7602. case GGML_TYPE_F16:
  7603. case GGML_TYPE_F32:
  7604. return 1;
  7605. case GGML_TYPE_Q2_K:
  7606. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  7607. case GGML_TYPE_Q3_K:
  7608. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  7609. case GGML_TYPE_Q4_K:
  7610. case GGML_TYPE_Q5_K:
  7611. case GGML_TYPE_Q6_K:
  7612. case GGML_TYPE_IQ2_XXS:
  7613. case GGML_TYPE_IQ2_XS:
  7614. case GGML_TYPE_IQ2_S:
  7615. case GGML_TYPE_IQ3_XXS:
  7616. case GGML_TYPE_IQ1_S:
  7617. case GGML_TYPE_IQ4_NL:
  7618. case GGML_TYPE_IQ3_S:
  7619. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7620. default:
  7621. GGML_ASSERT(false);
  7622. }
  7623. #else
  7624. switch(type) {
  7625. case GGML_TYPE_Q4_0:
  7626. case GGML_TYPE_Q4_1:
  7627. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7628. case GGML_TYPE_Q5_0:
  7629. case GGML_TYPE_Q5_1:
  7630. case GGML_TYPE_Q8_0:
  7631. return 64;
  7632. case GGML_TYPE_F16:
  7633. case GGML_TYPE_F32:
  7634. return 1;
  7635. case GGML_TYPE_Q2_K:
  7636. case GGML_TYPE_Q3_K:
  7637. case GGML_TYPE_Q4_K:
  7638. case GGML_TYPE_Q5_K:
  7639. case GGML_TYPE_IQ2_XXS:
  7640. case GGML_TYPE_IQ2_XS:
  7641. case GGML_TYPE_IQ2_S:
  7642. case GGML_TYPE_IQ3_XXS:
  7643. case GGML_TYPE_IQ1_S:
  7644. case GGML_TYPE_IQ4_NL:
  7645. case GGML_TYPE_IQ3_S:
  7646. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7647. case GGML_TYPE_Q6_K:
  7648. return 64;
  7649. default:
  7650. GGML_ASSERT(false);
  7651. }
  7652. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7653. }
  7654. 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) {
  7655. const int64_t nrows = ggml_nrows(tensor);
  7656. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  7657. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  7658. *row_low -= *row_low % rounding;
  7659. if (id == g_device_count - 1) {
  7660. *row_high = nrows;
  7661. } else {
  7662. *row_high = nrows*tensor_split[id + 1];
  7663. *row_high -= *row_high % rounding;
  7664. }
  7665. }
  7666. static void ggml_cuda_op_mul_mat_vec_q(
  7667. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7668. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7669. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7670. const int64_t ne00 = src0->ne[0];
  7671. const int64_t row_diff = row_high - row_low;
  7672. const int64_t ne10 = src1->ne[0];
  7673. GGML_ASSERT(ne10 % QK8_1 == 0);
  7674. const int64_t ne0 = dst->ne[0];
  7675. int id;
  7676. CUDA_CHECK(cudaGetDevice(&id));
  7677. // the main device has a larger memory buffer to hold the results from all GPUs
  7678. // nrows_dst == nrows of the matrix that the kernel writes into
  7679. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7680. switch (src0->type) {
  7681. case GGML_TYPE_Q4_0:
  7682. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7683. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7684. break;
  7685. case GGML_TYPE_Q4_1:
  7686. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7687. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7688. break;
  7689. case GGML_TYPE_Q5_0:
  7690. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7691. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7692. break;
  7693. case GGML_TYPE_Q5_1:
  7694. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7695. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7696. break;
  7697. case GGML_TYPE_Q8_0:
  7698. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7699. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7700. break;
  7701. case GGML_TYPE_Q2_K:
  7702. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7703. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7704. break;
  7705. case GGML_TYPE_Q3_K:
  7706. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7707. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7708. break;
  7709. case GGML_TYPE_Q4_K:
  7710. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7711. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7712. break;
  7713. case GGML_TYPE_Q5_K:
  7714. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7715. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7716. break;
  7717. case GGML_TYPE_Q6_K:
  7718. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7719. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7720. break;
  7721. case GGML_TYPE_IQ2_XXS:
  7722. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7723. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7724. break;
  7725. case GGML_TYPE_IQ2_XS:
  7726. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7727. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7728. break;
  7729. case GGML_TYPE_IQ2_S:
  7730. mul_mat_vec_q_cuda<QK_K, QI2_S, block_iq2_s, 1, vec_dot_iq2_s_q8_1>
  7731. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7732. break;
  7733. case GGML_TYPE_IQ3_XXS:
  7734. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7735. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7736. break;
  7737. case GGML_TYPE_IQ1_S:
  7738. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7739. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7740. break;
  7741. case GGML_TYPE_IQ4_NL:
  7742. mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
  7743. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7744. break;
  7745. case GGML_TYPE_IQ3_S:
  7746. mul_mat_vec_q_cuda<QK_K, QI3_XS, block_iq3_s, 1, vec_dot_iq3_s_q8_1>
  7747. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7748. break;
  7749. default:
  7750. GGML_ASSERT(false);
  7751. break;
  7752. }
  7753. (void) src1;
  7754. (void) dst;
  7755. (void) src1_ddf_i;
  7756. (void) src1_ncols;
  7757. (void) src1_padded_row_size;
  7758. }
  7759. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7760. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7761. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7762. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7763. const int64_t ne00 = src0->ne[0];
  7764. const int64_t row_diff = row_high - row_low;
  7765. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7766. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7767. #ifdef GGML_CUDA_F16
  7768. cuda_pool_alloc<half> src1_dfloat_a;
  7769. half * src1_dfloat = nullptr; // dfloat == half
  7770. bool src1_convert_f16 =
  7771. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7772. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7773. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7774. if (src1_convert_f16) {
  7775. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7776. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7777. GGML_ASSERT(to_fp16_cuda != nullptr);
  7778. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7779. }
  7780. #else
  7781. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7782. #endif // GGML_CUDA_F16
  7783. switch (src0->type) {
  7784. case GGML_TYPE_Q4_0:
  7785. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7786. break;
  7787. case GGML_TYPE_Q4_1:
  7788. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7789. break;
  7790. case GGML_TYPE_Q5_0:
  7791. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7792. break;
  7793. case GGML_TYPE_Q5_1:
  7794. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7795. break;
  7796. case GGML_TYPE_Q8_0:
  7797. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7798. break;
  7799. case GGML_TYPE_Q2_K:
  7800. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7801. break;
  7802. case GGML_TYPE_Q3_K:
  7803. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7804. break;
  7805. case GGML_TYPE_Q4_K:
  7806. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7807. break;
  7808. case GGML_TYPE_Q5_K:
  7809. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7810. break;
  7811. case GGML_TYPE_Q6_K:
  7812. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7813. break;
  7814. case GGML_TYPE_F16:
  7815. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7816. break;
  7817. default:
  7818. GGML_ASSERT(false);
  7819. break;
  7820. }
  7821. (void) src1;
  7822. (void) dst;
  7823. (void) src1_ddq_i;
  7824. (void) src1_ncols;
  7825. (void) src1_padded_row_size;
  7826. }
  7827. static void ggml_cuda_op_mul_mat_cublas(
  7828. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7829. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7830. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7831. GGML_ASSERT(src0_dd_i != nullptr);
  7832. GGML_ASSERT(src1_ddf_i != nullptr);
  7833. GGML_ASSERT(dst_dd_i != nullptr);
  7834. const int64_t ne00 = src0->ne[0];
  7835. const int64_t ne10 = src1->ne[0];
  7836. const int64_t ne0 = dst->ne[0];
  7837. const int64_t row_diff = row_high - row_low;
  7838. int id;
  7839. CUDA_CHECK(cudaGetDevice(&id));
  7840. // the main device has a larger memory buffer to hold the results from all GPUs
  7841. // ldc == nrows of the matrix that cuBLAS writes into
  7842. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7843. const int compute_capability = g_device_caps[id].cc;
  7844. 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) {
  7845. //printf("this branch\n");
  7846. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7847. cuda_pool_alloc<half> src0_as_f16;
  7848. if (src0->type != GGML_TYPE_F16) {
  7849. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7850. GGML_ASSERT(to_fp16_cuda != nullptr);
  7851. size_t ne = row_diff*ne00;
  7852. src0_as_f16.alloc(ne);
  7853. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7854. }
  7855. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7856. cuda_pool_alloc<half> src1_as_f16;
  7857. if (src1->type != GGML_TYPE_F16) {
  7858. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7859. GGML_ASSERT(to_fp16_cuda != nullptr);
  7860. size_t ne = src1_ncols*ne10;
  7861. src1_as_f16.alloc(ne);
  7862. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7863. }
  7864. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7865. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7866. const half alpha_f16 = 1.0f;
  7867. const half beta_f16 = 0.0f;
  7868. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7869. CUBLAS_CHECK(
  7870. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7871. row_diff, src1_ncols, ne10,
  7872. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7873. src1_ptr, CUDA_R_16F, ne10,
  7874. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7875. CUBLAS_COMPUTE_16F,
  7876. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7877. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7878. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7879. } else {
  7880. cuda_pool_alloc<float> src0_ddq_as_f32;
  7881. cuda_pool_alloc<float> src1_ddq_as_f32;
  7882. if (src0->type != GGML_TYPE_F32) {
  7883. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7884. GGML_ASSERT(to_fp32_cuda != nullptr);
  7885. src0_ddq_as_f32.alloc(row_diff*ne00);
  7886. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7887. }
  7888. if (src1->type != GGML_TYPE_F32) {
  7889. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7890. GGML_ASSERT(to_fp32_cuda != nullptr);
  7891. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7892. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7893. }
  7894. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7895. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7896. const float alpha = 1.0f;
  7897. const float beta = 0.0f;
  7898. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7899. CUBLAS_CHECK(
  7900. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7901. row_diff, src1_ncols, ne10,
  7902. &alpha, src0_ddf_i, ne00,
  7903. src1_ddf1_i, ne10,
  7904. &beta, dst_dd_i, ldc));
  7905. }
  7906. (void) dst;
  7907. (void) src1_ddq_i;
  7908. (void) src1_padded_row_size;
  7909. }
  7910. static void ggml_cuda_op_rope(
  7911. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7912. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7913. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7914. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7915. GGML_ASSERT(src0->type == dst->type);
  7916. const int64_t ne00 = src0->ne[0];
  7917. const int64_t ne01 = src0->ne[1];
  7918. const int64_t ne2 = dst->ne[2];
  7919. const int64_t nrows = ggml_nrows(src0);
  7920. //const int n_past = ((int32_t *) dst->op_params)[0];
  7921. const int n_dims = ((int32_t *) dst->op_params)[1];
  7922. const int mode = ((int32_t *) dst->op_params)[2];
  7923. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7924. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7925. // RoPE alteration for extended context
  7926. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7927. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7928. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7929. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7930. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7931. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7932. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7933. const int32_t * pos = nullptr;
  7934. if ((mode & 1) == 0) {
  7935. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7936. GGML_ASSERT(src1->ne[0] == ne2);
  7937. pos = (const int32_t *) src1_dd;
  7938. }
  7939. const bool is_neox = mode & 2;
  7940. const bool is_glm = mode & 4;
  7941. rope_corr_dims corr_dims;
  7942. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7943. // compute
  7944. if (is_glm) {
  7945. GGML_ASSERT(false);
  7946. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7947. } else if (is_neox) {
  7948. if (src0->type == GGML_TYPE_F32) {
  7949. rope_neox_cuda(
  7950. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7951. attn_factor, corr_dims, main_stream
  7952. );
  7953. } else if (src0->type == GGML_TYPE_F16) {
  7954. rope_neox_cuda(
  7955. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7956. attn_factor, corr_dims, main_stream
  7957. );
  7958. } else {
  7959. GGML_ASSERT(false);
  7960. }
  7961. } else {
  7962. if (src0->type == GGML_TYPE_F32) {
  7963. rope_cuda(
  7964. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7965. attn_factor, corr_dims, main_stream
  7966. );
  7967. } else if (src0->type == GGML_TYPE_F16) {
  7968. rope_cuda(
  7969. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7970. attn_factor, corr_dims, main_stream
  7971. );
  7972. } else {
  7973. GGML_ASSERT(false);
  7974. }
  7975. }
  7976. (void) src1;
  7977. (void) dst;
  7978. (void) src1_dd;
  7979. }
  7980. static void ggml_cuda_op_alibi(
  7981. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7982. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7983. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7984. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7985. const int64_t ne00 = src0->ne[0];
  7986. const int64_t ne01 = src0->ne[1];
  7987. const int64_t ne02 = src0->ne[2];
  7988. const int64_t nrows = ggml_nrows(src0);
  7989. //const int n_past = ((int32_t *) dst->op_params)[0];
  7990. const int n_head = ((int32_t *) dst->op_params)[1];
  7991. float max_bias;
  7992. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7993. //GGML_ASSERT(ne01 + n_past == ne00);
  7994. GGML_ASSERT(n_head == ne02);
  7995. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7996. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7997. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7998. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7999. (void) src1;
  8000. (void) src1_dd;
  8001. }
  8002. static void ggml_cuda_op_pool2d(
  8003. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8004. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8005. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8006. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8007. const int32_t * opts = (const int32_t *)dst->op_params;
  8008. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  8009. const int k0 = opts[1];
  8010. const int k1 = opts[2];
  8011. const int s0 = opts[3];
  8012. const int s1 = opts[4];
  8013. const int p0 = opts[5];
  8014. const int p1 = opts[6];
  8015. const int64_t IH = src0->ne[1];
  8016. const int64_t IW = src0->ne[0];
  8017. const int64_t N = dst->ne[3];
  8018. const int64_t OC = dst->ne[2];
  8019. const int64_t OH = dst->ne[1];
  8020. const int64_t OW = dst->ne[0];
  8021. const int parallel_elements = N * OC * OH * OW;
  8022. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  8023. dim3 block_nums(num_blocks);
  8024. 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);
  8025. (void) src1;
  8026. (void) src1_dd;
  8027. }
  8028. static void ggml_cuda_op_im2col(
  8029. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8030. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8031. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8032. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8033. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  8034. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8035. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  8036. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  8037. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  8038. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  8039. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  8040. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  8041. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  8042. const int64_t IH = is_2D ? src1->ne[1] : 1;
  8043. const int64_t IW = src1->ne[0];
  8044. const int64_t KH = is_2D ? src0->ne[1] : 1;
  8045. const int64_t KW = src0->ne[0];
  8046. const int64_t OH = is_2D ? dst->ne[2] : 1;
  8047. const int64_t OW = dst->ne[1];
  8048. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  8049. const int64_t batch = src1->ne[3];
  8050. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  8051. if(dst->type == GGML_TYPE_F16) {
  8052. 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);
  8053. } else {
  8054. 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);
  8055. }
  8056. (void) src0;
  8057. (void) src0_dd;
  8058. }
  8059. static void ggml_cuda_op_sum_rows(
  8060. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8061. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8062. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8063. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8064. const int64_t ncols = src0->ne[0];
  8065. const int64_t nrows = ggml_nrows(src0);
  8066. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  8067. (void) src1;
  8068. (void) dst;
  8069. (void) src1_dd;
  8070. }
  8071. static void ggml_cuda_op_argsort(
  8072. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8073. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8074. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8075. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  8076. const int64_t ncols = src0->ne[0];
  8077. const int64_t nrows = ggml_nrows(src0);
  8078. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  8079. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  8080. (void) src1;
  8081. (void) dst;
  8082. (void) src1_dd;
  8083. }
  8084. static void ggml_cuda_op_diag_mask_inf(
  8085. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8086. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8087. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8088. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8089. const int64_t ne00 = src0->ne[0];
  8090. const int64_t ne01 = src0->ne[1];
  8091. const int nrows0 = ggml_nrows(src0);
  8092. const int n_past = ((int32_t *) dst->op_params)[0];
  8093. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  8094. (void) src1;
  8095. (void) dst;
  8096. (void) src1_dd;
  8097. }
  8098. static void ggml_cuda_op_soft_max(
  8099. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8100. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8101. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8102. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8103. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  8104. const int64_t ne00 = src0->ne[0];
  8105. const int64_t nrows_x = ggml_nrows(src0);
  8106. const int64_t nrows_y = src0->ne[1];
  8107. float scale = 1.0f;
  8108. float max_bias = 0.0f;
  8109. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  8110. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  8111. // positions tensor
  8112. float * src2_dd = nullptr;
  8113. cuda_pool_alloc<float> src2_f;
  8114. ggml_tensor * src2 = dst->src[2];
  8115. const bool use_src2 = src2 != nullptr;
  8116. if (use_src2) {
  8117. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  8118. if (src2_on_device) {
  8119. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  8120. src2_dd = (float *) src2_extra->data_device[g_main_device];
  8121. } else {
  8122. src2_dd = src2_f.alloc(ggml_nelements(src2));
  8123. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  8124. }
  8125. }
  8126. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  8127. }
  8128. static void ggml_cuda_op_scale(
  8129. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8130. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8131. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8132. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8133. float scale;
  8134. memcpy(&scale, dst->op_params, sizeof(float));
  8135. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  8136. CUDA_CHECK(cudaGetLastError());
  8137. (void) src1;
  8138. (void) dst;
  8139. (void) src1_dd;
  8140. }
  8141. static void ggml_cuda_op_clamp(
  8142. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8143. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8144. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8145. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8146. float min;
  8147. float max;
  8148. memcpy(&min, dst->op_params, sizeof(float));
  8149. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  8150. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  8151. CUDA_CHECK(cudaGetLastError());
  8152. (void) src1;
  8153. (void) dst;
  8154. (void) src1_dd;
  8155. }
  8156. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  8157. const int64_t nrows0 = ggml_nrows(src0);
  8158. const bool use_src1 = src1 != nullptr;
  8159. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  8160. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8161. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8162. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8163. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  8164. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8165. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8166. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  8167. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  8168. // dd = data device
  8169. float * src0_ddf = nullptr;
  8170. float * src1_ddf = nullptr;
  8171. float * dst_ddf = nullptr;
  8172. cuda_pool_alloc<float> src0_f;
  8173. cuda_pool_alloc<float> src1_f;
  8174. cuda_pool_alloc<float> dst_f;
  8175. ggml_cuda_set_device(g_main_device);
  8176. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8177. if (src0_on_device) {
  8178. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  8179. } else {
  8180. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  8181. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  8182. }
  8183. if (use_src1) {
  8184. if (src1_on_device) {
  8185. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8186. } else {
  8187. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  8188. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  8189. }
  8190. }
  8191. if (dst_on_device) {
  8192. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8193. } else {
  8194. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  8195. }
  8196. // do the computation
  8197. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  8198. CUDA_CHECK(cudaGetLastError());
  8199. // copy dst to host if necessary
  8200. if (!dst_on_device) {
  8201. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  8202. }
  8203. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8204. CUDA_CHECK(cudaDeviceSynchronize());
  8205. }
  8206. }
  8207. static void ggml_cuda_set_peer_access(const int n_tokens) {
  8208. static bool peer_access_enabled = false;
  8209. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  8210. if (peer_access_enabled == enable_peer_access) {
  8211. return;
  8212. }
  8213. #ifdef NDEBUG
  8214. for (int id = 0; id < g_device_count; ++id) {
  8215. ggml_cuda_set_device(id);
  8216. CUDA_CHECK(cudaDeviceSynchronize());
  8217. }
  8218. for (int id = 0; id < g_device_count; ++id) {
  8219. ggml_cuda_set_device(id);
  8220. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  8221. if (id == id_other) {
  8222. continue;
  8223. }
  8224. if (id != g_main_device && id_other != g_main_device) {
  8225. continue;
  8226. }
  8227. int can_access_peer;
  8228. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  8229. if (can_access_peer) {
  8230. if (enable_peer_access) {
  8231. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  8232. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  8233. CUDA_CHECK(err);
  8234. }
  8235. } else {
  8236. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  8237. if (err != cudaErrorPeerAccessNotEnabled) {
  8238. CUDA_CHECK(err);
  8239. }
  8240. }
  8241. }
  8242. }
  8243. }
  8244. #endif // NDEBUG
  8245. peer_access_enabled = enable_peer_access;
  8246. }
  8247. // FIXME: move this somewhere else
  8248. struct ggml_backend_cuda_split_buffer_type_context {
  8249. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  8250. };
  8251. static void ggml_cuda_op_mul_mat(
  8252. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  8253. const bool convert_src1_to_q8_1) {
  8254. const int64_t ne00 = src0->ne[0];
  8255. const int64_t ne01 = src0->ne[1];
  8256. const int64_t ne02 = src0->ne[2];
  8257. const int64_t ne03 = src0->ne[3];
  8258. const int64_t ne10 = src1->ne[0];
  8259. const int64_t ne11 = src1->ne[1];
  8260. const int64_t ne12 = src1->ne[2];
  8261. const int64_t ne13 = src1->ne[3];
  8262. const int64_t nrows1 = ggml_nrows(src1);
  8263. GGML_ASSERT(ne03 == ne13);
  8264. const int64_t ne0 = dst->ne[0];
  8265. const int64_t ne1 = dst->ne[1];
  8266. const int nb2 = dst->nb[2];
  8267. const int nb3 = dst->nb[3];
  8268. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8269. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8270. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  8271. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  8272. const int64_t i02_divisor = ne12 / ne02;
  8273. const size_t src0_ts = ggml_type_size(src0->type);
  8274. const size_t src0_bs = ggml_blck_size(src0->type);
  8275. const size_t q8_1_ts = sizeof(block_q8_1);
  8276. const size_t q8_1_bs = QK8_1;
  8277. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8278. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8279. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8280. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8281. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  8282. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  8283. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  8284. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8285. GGML_ASSERT(!(split && ne02 > 1));
  8286. GGML_ASSERT(!(split && ne03 > 1));
  8287. GGML_ASSERT(!(split && ne02 < ne12));
  8288. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  8289. if (split) {
  8290. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  8291. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  8292. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8293. tensor_split = buft_ctx->tensor_split;
  8294. }
  8295. struct dev_data {
  8296. cuda_pool_alloc<char> src0_dd_alloc;
  8297. cuda_pool_alloc<float> src1_ddf_alloc;
  8298. cuda_pool_alloc<char> src1_ddq_alloc;
  8299. cuda_pool_alloc<float> dst_dd_alloc;
  8300. char * src0_dd = nullptr;
  8301. float * src1_ddf = nullptr; // float
  8302. char * src1_ddq = nullptr; // q8_1
  8303. float * dst_dd = nullptr;
  8304. int64_t row_low;
  8305. int64_t row_high;
  8306. };
  8307. dev_data dev[GGML_CUDA_MAX_DEVICES];
  8308. int used_devices = 0;
  8309. for (int id = 0; id < g_device_count; ++id) {
  8310. // by default, use all rows
  8311. dev[id].row_low = 0;
  8312. dev[id].row_high = ne01;
  8313. // for multi GPU, get the row boundaries from tensor split
  8314. // and round to mul_mat_q tile sizes
  8315. if (split) {
  8316. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  8317. if (id != 0) {
  8318. dev[id].row_low = ne01*tensor_split[id];
  8319. if (dev[id].row_low < ne01) {
  8320. dev[id].row_low -= dev[id].row_low % rounding;
  8321. }
  8322. }
  8323. if (id != g_device_count - 1) {
  8324. dev[id].row_high = ne01*tensor_split[id + 1];
  8325. if (dev[id].row_high < ne01) {
  8326. dev[id].row_high -= dev[id].row_high % rounding;
  8327. }
  8328. }
  8329. }
  8330. }
  8331. for (int id = 0; id < g_device_count; ++id) {
  8332. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  8333. continue;
  8334. }
  8335. used_devices++;
  8336. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8337. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8338. ggml_cuda_set_device(id);
  8339. cudaStream_t stream = g_cudaStreams[id][0];
  8340. if (src0_on_device && src0_is_contiguous) {
  8341. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  8342. } else {
  8343. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  8344. }
  8345. if (src1_on_device && src1_is_contiguous) {
  8346. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  8347. } else {
  8348. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  8349. }
  8350. if (convert_src1_to_q8_1) {
  8351. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  8352. if (src1_on_device && src1_is_contiguous) {
  8353. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  8354. CUDA_CHECK(cudaGetLastError());
  8355. }
  8356. }
  8357. if (dst_on_device) {
  8358. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  8359. } else {
  8360. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  8361. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  8362. }
  8363. }
  8364. // if multiple devices are used they need to wait for the main device
  8365. // here an event is recorded that signals that the main device has finished calculating the input data
  8366. if (split && used_devices > 1) {
  8367. ggml_cuda_set_device(g_main_device);
  8368. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  8369. }
  8370. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  8371. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  8372. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  8373. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  8374. for (int id = 0; id < g_device_count; ++id) {
  8375. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  8376. continue;
  8377. }
  8378. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8379. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  8380. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  8381. ggml_cuda_set_device(id);
  8382. cudaStream_t stream = g_cudaStreams[id][is];
  8383. // wait for main GPU data if necessary
  8384. if (split && (id != g_main_device || is != 0)) {
  8385. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  8386. }
  8387. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  8388. const int64_t i03 = i0 / ne12;
  8389. const int64_t i02 = i0 % ne12;
  8390. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  8391. // for split tensors the data begins at i0 == i0_offset_low
  8392. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  8393. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  8394. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  8395. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  8396. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  8397. // in that case an offset on dst_ddf_i is needed
  8398. if (dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device) {
  8399. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  8400. }
  8401. // copy src0, src1 to device if necessary
  8402. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  8403. if (id != g_main_device) {
  8404. if (convert_src1_to_q8_1) {
  8405. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  8406. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  8407. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  8408. } else {
  8409. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  8410. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  8411. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  8412. src1_ncols*ne10*sizeof(float), stream));
  8413. }
  8414. }
  8415. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  8416. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  8417. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  8418. } else {
  8419. GGML_ASSERT(false);
  8420. }
  8421. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  8422. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  8423. CUDA_CHECK(cudaGetLastError());
  8424. }
  8425. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  8426. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  8427. }
  8428. // do the computation
  8429. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  8430. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  8431. CUDA_CHECK(cudaGetLastError());
  8432. // copy dst to host or other device if necessary
  8433. if (!dst_on_device) {
  8434. void * dst_off_device;
  8435. cudaMemcpyKind kind;
  8436. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8437. dst_off_device = dst->data;
  8438. kind = cudaMemcpyDeviceToHost;
  8439. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  8440. dst_off_device = dst_extra->data_device[g_main_device];
  8441. kind = cudaMemcpyDeviceToDevice;
  8442. } else {
  8443. GGML_ASSERT(false);
  8444. }
  8445. if (split) {
  8446. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  8447. // dst is NOT transposed.
  8448. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  8449. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  8450. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  8451. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  8452. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  8453. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  8454. #if !defined(GGML_USE_HIPBLAS)
  8455. if (kind == cudaMemcpyDeviceToDevice) {
  8456. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  8457. cudaMemcpy3DPeerParms p = {};
  8458. p.dstDevice = g_main_device;
  8459. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  8460. p.srcDevice = id;
  8461. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  8462. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  8463. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  8464. } else
  8465. #endif
  8466. {
  8467. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  8468. dst_dd_i, row_diff*sizeof(float),
  8469. row_diff*sizeof(float), src1_ncols,
  8470. kind, stream));
  8471. }
  8472. } else {
  8473. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  8474. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  8475. dhf_dst_i += src1_col_0*ne0;
  8476. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  8477. }
  8478. }
  8479. // add event for the main device to wait on until other device is done
  8480. if (split && (id != g_main_device || is != 0)) {
  8481. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  8482. }
  8483. }
  8484. }
  8485. }
  8486. // main device waits for all other devices to be finished
  8487. if (split && g_device_count > 1) {
  8488. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  8489. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  8490. ggml_cuda_set_device(g_main_device);
  8491. for (int id = 0; id < g_device_count; ++id) {
  8492. if (dev[id].row_low == dev[id].row_high) {
  8493. continue;
  8494. }
  8495. for (int64_t is = 0; is < is_max; ++is) {
  8496. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  8497. }
  8498. }
  8499. }
  8500. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8501. ggml_cuda_set_device(g_main_device);
  8502. CUDA_CHECK(cudaDeviceSynchronize());
  8503. }
  8504. }
  8505. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8506. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  8507. }
  8508. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8509. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  8510. }
  8511. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8512. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  8513. }
  8514. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8515. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  8516. }
  8517. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8518. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  8519. }
  8520. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8521. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  8522. }
  8523. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8524. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  8525. }
  8526. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8527. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  8528. }
  8529. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8530. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  8531. }
  8532. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8533. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  8534. }
  8535. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8536. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  8537. }
  8538. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8539. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  8540. }
  8541. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8542. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  8543. }
  8544. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8545. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  8546. }
  8547. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8548. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  8549. }
  8550. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8551. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  8552. }
  8553. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8554. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  8555. }
  8556. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8557. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  8558. }
  8559. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8560. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  8561. }
  8562. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8563. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  8564. }
  8565. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8566. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  8567. }
  8568. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  8569. if (!g_cublas_loaded) return false;
  8570. const int64_t ne10 = src1->ne[0];
  8571. const int64_t ne0 = dst->ne[0];
  8572. const int64_t ne1 = dst->ne[1];
  8573. // TODO: find the optimal values for these
  8574. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  8575. src1->type == GGML_TYPE_F32 &&
  8576. dst->type == GGML_TYPE_F32 &&
  8577. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  8578. }
  8579. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8580. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  8581. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8582. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  8583. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  8584. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8585. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8586. const int64_t ne00 = src0->ne[0];
  8587. const int64_t ne01 = src0->ne[1];
  8588. const int64_t ne02 = src0->ne[2];
  8589. const int64_t ne12 = src1->ne[2];
  8590. ggml_cuda_set_device(g_main_device);
  8591. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8592. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8593. void * src0_ddq = src0_extra->data_device[g_main_device];
  8594. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8595. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8596. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8597. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8598. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8599. }
  8600. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8601. GGML_ASSERT(!ggml_is_transposed(src0));
  8602. GGML_ASSERT(!ggml_is_transposed(src1));
  8603. GGML_ASSERT(!ggml_is_permuted(src0));
  8604. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8605. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8606. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8607. const int64_t ne00 = src0->ne[0];
  8608. const int64_t ne01 = src0->ne[1];
  8609. const int64_t ne02 = src0->ne[2];
  8610. const int64_t nb01 = src0->nb[1];
  8611. const int64_t nb02 = src0->nb[2];
  8612. const int64_t ne12 = src1->ne[2];
  8613. ggml_cuda_set_device(g_main_device);
  8614. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8615. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8616. void * src0_ddq = src0_extra->data_device[g_main_device];
  8617. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8618. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8619. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8620. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8621. const int64_t row_stride_x = nb01 / sizeof(half);
  8622. const int64_t channel_stride_x = nb02 / sizeof(half);
  8623. 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);
  8624. }
  8625. static __global__ void k_compute_batched_ptrs(
  8626. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8627. const void ** ptrs_src, void ** ptrs_dst,
  8628. int64_t ne12, int64_t ne13,
  8629. int64_t ne23,
  8630. size_t nb02, size_t nb03,
  8631. size_t nb12, size_t nb13,
  8632. size_t nbd2, size_t nbd3,
  8633. int64_t r2, int64_t r3) {
  8634. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8635. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8636. if (i13 >= ne13 || i12 >= ne12) {
  8637. return;
  8638. }
  8639. int64_t i03 = i13 / r3;
  8640. int64_t i02 = i12 / r2;
  8641. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8642. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8643. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8644. }
  8645. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8646. GGML_ASSERT(!ggml_is_transposed(src0));
  8647. GGML_ASSERT(!ggml_is_transposed(src1));
  8648. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8649. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8650. GGML_TENSOR_BINARY_OP_LOCALS
  8651. const int64_t ne_dst = ggml_nelements(dst);
  8652. ggml_cuda_set_device(g_main_device);
  8653. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8654. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8655. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8656. void * src0_ddq = src0_extra->data_device[g_main_device];
  8657. half * src0_f16 = (half *) src0_ddq;
  8658. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8659. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8660. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8661. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8662. // convert src1 to fp16
  8663. cuda_pool_alloc<half> src1_f16_alloc;
  8664. if (src1->type != GGML_TYPE_F16) {
  8665. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8666. const int64_t ne_src1 = ggml_nelements(src1);
  8667. src1_f16_alloc.alloc(ne_src1);
  8668. GGML_ASSERT(to_fp16_cuda != nullptr);
  8669. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8670. }
  8671. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8672. cuda_pool_alloc<half> dst_f16;
  8673. char * dst_t;
  8674. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8675. cudaDataType_t cu_data_type = CUDA_R_16F;
  8676. // dst strides
  8677. size_t nbd2 = dst->nb[2];
  8678. size_t nbd3 = dst->nb[3];
  8679. const half alpha_f16 = 1.0f;
  8680. const half beta_f16 = 0.0f;
  8681. const float alpha_f32 = 1.0f;
  8682. const float beta_f32 = 0.0f;
  8683. const void * alpha = &alpha_f16;
  8684. const void * beta = &beta_f16;
  8685. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8686. dst_t = (char *) dst_f16.alloc(ne_dst);
  8687. nbd2 /= sizeof(float) / sizeof(half);
  8688. nbd3 /= sizeof(float) / sizeof(half);
  8689. } else {
  8690. dst_t = (char *) dst_ddf;
  8691. cu_compute_type = CUBLAS_COMPUTE_32F;
  8692. cu_data_type = CUDA_R_32F;
  8693. alpha = &alpha_f32;
  8694. beta = &beta_f32;
  8695. }
  8696. GGML_ASSERT(ne12 % ne02 == 0);
  8697. GGML_ASSERT(ne13 % ne03 == 0);
  8698. // broadcast factors
  8699. const int64_t r2 = ne12/ne02;
  8700. const int64_t r3 = ne13/ne03;
  8701. #if 0
  8702. // use cublasGemmEx
  8703. {
  8704. for (int i13 = 0; i13 < ne13; ++i13) {
  8705. for (int i12 = 0; i12 < ne12; ++i12) {
  8706. int i03 = i13 / r3;
  8707. int i02 = i12 / r2;
  8708. CUBLAS_CHECK(
  8709. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8710. ne01, ne11, ne10,
  8711. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8712. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8713. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8714. cu_compute_type,
  8715. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8716. }
  8717. }
  8718. }
  8719. #else
  8720. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8721. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8722. // use cublasGemmStridedBatchedEx
  8723. CUBLAS_CHECK(
  8724. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8725. ne01, ne11, ne10,
  8726. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8727. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8728. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8729. ne12*ne13,
  8730. cu_compute_type,
  8731. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8732. } else {
  8733. // use cublasGemmBatchedEx
  8734. const int ne23 = ne12*ne13;
  8735. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8736. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8737. dim3 block_dims(ne13, ne12);
  8738. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8739. src0_f16, src1_f16, dst_t,
  8740. ptrs_src.get(), ptrs_dst.get(),
  8741. ne12, ne13,
  8742. ne23,
  8743. nb02, nb03,
  8744. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8745. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8746. nbd2, nbd3,
  8747. r2, r3);
  8748. CUDA_CHECK(cudaGetLastError());
  8749. CUBLAS_CHECK(
  8750. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8751. ne01, ne11, ne10,
  8752. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8753. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8754. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8755. ne23,
  8756. cu_compute_type,
  8757. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8758. }
  8759. #endif
  8760. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8761. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8762. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8763. }
  8764. }
  8765. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8766. const bool all_on_device =
  8767. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  8768. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  8769. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  8770. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8771. int64_t min_compute_capability = INT_MAX;
  8772. bool any_pascal_with_slow_fp16 = false;
  8773. if (split) {
  8774. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8775. auto & tensor_split = buft_ctx->tensor_split;
  8776. for (int id = 0; id < g_device_count; ++id) {
  8777. // skip devices that are not going to do any work:
  8778. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8779. continue;
  8780. }
  8781. if (min_compute_capability > g_device_caps[id].cc) {
  8782. min_compute_capability = g_device_caps[id].cc;
  8783. }
  8784. if (g_device_caps[id].cc == 610) {
  8785. any_pascal_with_slow_fp16 = true;
  8786. }
  8787. }
  8788. } else {
  8789. min_compute_capability = g_device_caps[g_main_device].cc;
  8790. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8791. }
  8792. // check data types and tensor shapes for custom matrix multiplication kernels:
  8793. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8794. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8795. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8796. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8797. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8798. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8799. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8800. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8801. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8802. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8803. #ifdef CUDA_USE_TENSOR_CORES
  8804. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8805. #endif // CUDA_USE_TENSOR_CORES
  8806. #else
  8807. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8808. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8809. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8810. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8811. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8812. #ifdef CUDA_USE_TENSOR_CORES
  8813. // when tensor cores are available, use them for large batch size
  8814. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8815. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8816. #endif // CUDA_USE_TENSOR_CORES
  8817. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8818. // if mmvq is available it's a better choice than dmmv:
  8819. #ifndef GGML_CUDA_FORCE_DMMV
  8820. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8821. #endif // GGML_CUDA_FORCE_DMMV
  8822. // debug helpers
  8823. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8824. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8825. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8826. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8827. //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);
  8828. //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);
  8829. 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) {
  8830. // KQ single-batch
  8831. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8832. } 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) {
  8833. // KQV single-batch
  8834. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8835. } 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) {
  8836. // KQ + KQV multi-batch
  8837. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8838. } else if (use_dequantize_mul_mat_vec) {
  8839. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8840. } else if (use_mul_mat_vec_q) {
  8841. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8842. } else if (use_mul_mat_q) {
  8843. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8844. } else {
  8845. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8846. }
  8847. }
  8848. #if 0
  8849. template<typename ... Srcs>
  8850. static __global__ void k_compute_batched_ptrs_id(
  8851. const void ** ptrs_src, void ** ptrs_dst,
  8852. int ne12, int ne13,
  8853. int ne23,
  8854. int nb02, int nb03,
  8855. int nb12, int nb13,
  8856. int nb2, int nb3,
  8857. int r2, int r3,
  8858. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8859. const half * src1_f16, half * dst_f16,
  8860. const int32_t * ids, const int id,
  8861. Srcs... src0s) {
  8862. int i = ids[id];
  8863. half * src0_f16;
  8864. const void * srcs_ar[] = { (const half *) src0s... };
  8865. if (src0_type == GGML_TYPE_F16) {
  8866. src0_f16 = (half *) srcs_ar[i];
  8867. } else {
  8868. src0_f16 = src0_as_f16;
  8869. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8870. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8871. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8872. }
  8873. }
  8874. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8875. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8876. if (i13 >= ne13 || i12 >= ne12) {
  8877. return;
  8878. }
  8879. int i03 = i13 / r3;
  8880. int i02 = i12 / r2;
  8881. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8882. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8883. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8884. }
  8885. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8886. const struct ggml_tensor * ids = dst->src[0];
  8887. const struct ggml_tensor * src1 = dst->src[1];
  8888. const struct ggml_tensor * src00 = dst->src[2];
  8889. const int id = dst->op_params[0];
  8890. GGML_ASSERT(!ggml_is_transposed(src00));
  8891. GGML_ASSERT(!ggml_is_transposed(src1));
  8892. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8893. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8894. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8895. const int64_t ne01 = src00->ne[1];
  8896. const int64_t ne02 = src00->ne[2];
  8897. const int64_t ne03 = src00->ne[3];
  8898. //const int64_t nb01 = src00->nb[1];
  8899. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8900. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8901. const int64_t ne10 = src1->ne[0];
  8902. const int64_t ne11 = src1->ne[1];
  8903. const int64_t ne12 = src1->ne[2];
  8904. const int64_t ne13 = src1->ne[3];
  8905. //const int64_t nb11 = src1->nb[1];
  8906. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8907. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8908. const int64_t ne1 = ggml_nelements(src1);
  8909. const int64_t ne = ggml_nelements(dst);
  8910. ggml_cuda_set_device(g_main_device);
  8911. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8912. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8913. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8914. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8915. //half * src0_as_f16 = (half *) src0_ddq;
  8916. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8917. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8918. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8919. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8920. // convert src1 to fp16
  8921. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8922. GGML_ASSERT(to_fp16_cuda != nullptr);
  8923. size_t src1_as = 0;
  8924. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8925. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8926. size_t dst_as = 0;
  8927. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8928. GGML_ASSERT(ne12 % ne02 == 0);
  8929. GGML_ASSERT(ne13 % ne03 == 0);
  8930. // broadcast factors
  8931. const int64_t r2 = ne12/ne02;
  8932. const int64_t r3 = ne13/ne03;
  8933. const half alpha_f16 = 1.0f;
  8934. const half beta_f16 = 0.0f;
  8935. // use cublasGemmBatchedEx
  8936. const int ne23 = ne12*ne13;
  8937. const void ** ptrs_src = nullptr;
  8938. void ** ptrs_dst = nullptr;
  8939. size_t ptrs_src_s = 0;
  8940. size_t ptrs_dst_s = 0;
  8941. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8942. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8943. int64_t src0_ne = ggml_nelements(src00);
  8944. half * src0_as_f16 = nullptr;
  8945. size_t src0_as = 0;
  8946. if (src00->type != GGML_TYPE_F16) {
  8947. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8948. }
  8949. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8950. dim3 block_dims(ne13, ne12);
  8951. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8952. ptrs_src, ptrs_dst,
  8953. ne12, ne13,
  8954. ne23,
  8955. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8956. nb12, nb13,
  8957. dst->nb[2], dst->nb[3],
  8958. r2, r3,
  8959. src00->type, src0_as_f16, src0_ne,
  8960. src1_as_f16, dst_f16,
  8961. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8962. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8963. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8964. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8965. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8966. );
  8967. CUDA_CHECK(cudaGetLastError());
  8968. CUBLAS_CHECK(
  8969. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8970. ne01, ne11, ne10,
  8971. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8972. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8973. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8974. ne23,
  8975. CUBLAS_COMPUTE_16F,
  8976. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8977. if (src0_as != 0) {
  8978. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8979. }
  8980. if (ptrs_src_s != 0) {
  8981. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8982. }
  8983. if (ptrs_dst_s != 0) {
  8984. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8985. }
  8986. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8987. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8988. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8989. ggml_cuda_pool_free(dst_f16, dst_as);
  8990. }
  8991. #endif
  8992. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8993. #if 0
  8994. ggml_cuda_mul_mat_id_cublas(dst);
  8995. // TODO: mmq/mmv support
  8996. #endif
  8997. const size_t nb11 = src1->nb[1];
  8998. const size_t nb1 = dst->nb[1];
  8999. const struct ggml_tensor * ids = src0;
  9000. const int32_t id = ((int32_t *) dst->op_params)[0];
  9001. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  9002. std::vector<char> ids_host(ggml_nbytes(ids));
  9003. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  9004. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  9005. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  9006. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  9007. CUDA_CHECK(cudaStreamSynchronize(stream));
  9008. } else {
  9009. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  9010. }
  9011. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  9012. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  9013. ggml_tensor_extra_gpu src1_row_extra;
  9014. ggml_tensor_extra_gpu dst_row_extra;
  9015. ggml_tensor src1_row = *src1;
  9016. ggml_tensor dst_row = *dst;
  9017. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  9018. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  9019. src1_row.extra = &src1_row_extra;
  9020. dst_row.extra = &dst_row_extra;
  9021. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  9022. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  9023. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  9024. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  9025. if (src1->ne[1] == 1) {
  9026. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  9027. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  9028. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  9029. //int32_t row_id;
  9030. //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]));
  9031. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  9032. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  9033. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  9034. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  9035. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  9036. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  9037. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  9038. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  9039. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  9040. }
  9041. } else {
  9042. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  9043. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  9044. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  9045. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  9046. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_TYPE_CPU ?
  9047. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  9048. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_TYPE_CPU ?
  9049. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  9050. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  9051. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  9052. int64_t num_src1_rows = 0;
  9053. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  9054. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  9055. if (row_id_i != row_id) {
  9056. continue;
  9057. }
  9058. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  9059. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  9060. nb11, src1_kind, stream));
  9061. num_src1_rows++;
  9062. }
  9063. if (num_src1_rows == 0) {
  9064. continue;
  9065. }
  9066. src1_row.ne[1] = num_src1_rows;
  9067. dst_row.ne[1] = num_src1_rows;
  9068. src1_row.nb[1] = nb11;
  9069. src1_row.nb[2] = num_src1_rows*nb11;
  9070. src1_row.nb[3] = num_src1_rows*nb11;
  9071. dst_row.nb[1] = nb1;
  9072. dst_row.nb[2] = num_src1_rows*nb1;
  9073. dst_row.nb[3] = num_src1_rows*nb1;
  9074. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  9075. num_src1_rows = 0;
  9076. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  9077. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  9078. if (row_id_i != row_id) {
  9079. continue;
  9080. }
  9081. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  9082. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  9083. nb1, dst_kind, stream));
  9084. num_src1_rows++;
  9085. }
  9086. }
  9087. }
  9088. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  9089. CUDA_CHECK(cudaStreamSynchronize(stream));
  9090. }
  9091. }
  9092. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9093. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  9094. }
  9095. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9096. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  9097. }
  9098. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9099. const int64_t ne = ggml_nelements(src0);
  9100. GGML_ASSERT(ne == ggml_nelements(src1));
  9101. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  9102. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  9103. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  9104. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  9105. const int64_t ne00 = src0->ne[0];
  9106. const int64_t ne01 = src0->ne[1];
  9107. const int64_t ne02 = src0->ne[2];
  9108. //GGML_ASSERT(src0->ne[3] == 1);
  9109. const int64_t nb00 = src0->nb[0];
  9110. const int64_t nb01 = src0->nb[1];
  9111. const int64_t nb02 = src0->nb[2];
  9112. const int64_t nb03 = src0->nb[3];
  9113. const int64_t ne10 = src1->ne[0];
  9114. const int64_t ne11 = src1->ne[1];
  9115. const int64_t ne12 = src1->ne[2];
  9116. //GGML_ASSERT(src1->ne[3] == 1);
  9117. const int64_t nb10 = src1->nb[0];
  9118. const int64_t nb11 = src1->nb[1];
  9119. const int64_t nb12 = src1->nb[2];
  9120. const int64_t nb13 = src1->nb[3];
  9121. ggml_cuda_set_device(g_main_device);
  9122. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  9123. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  9124. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  9125. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  9126. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  9127. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  9128. 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);
  9129. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  9130. 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);
  9131. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  9132. 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);
  9133. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  9134. 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);
  9135. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  9136. 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);
  9137. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  9138. 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);
  9139. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  9140. 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);
  9141. } else {
  9142. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  9143. ggml_type_name(src0->type), ggml_type_name(src1->type));
  9144. GGML_ASSERT(false);
  9145. }
  9146. (void) dst;
  9147. }
  9148. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9149. // TODO: why do we pass dst as src1 here?
  9150. ggml_cuda_cpy(src0, dst, nullptr);
  9151. (void) src1;
  9152. }
  9153. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9154. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  9155. }
  9156. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9157. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  9158. }
  9159. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9160. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  9161. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  9162. }
  9163. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9164. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  9165. }
  9166. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9167. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  9168. }
  9169. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9170. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  9171. }
  9172. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9173. GGML_ASSERT(ggml_is_contiguous(src0));
  9174. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  9175. }
  9176. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9177. GGML_ASSERT(ggml_is_contiguous(src0));
  9178. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  9179. }
  9180. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9181. (void) src0;
  9182. (void) src1;
  9183. (void) dst;
  9184. }
  9185. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  9186. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  9187. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  9188. }
  9189. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  9190. if (main_device >= g_device_count) {
  9191. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  9192. main_device, g_device_count, g_main_device);
  9193. return;
  9194. }
  9195. if (g_main_device != main_device && g_device_count > 1) {
  9196. g_main_device = main_device;
  9197. //cudaDeviceProp prop;
  9198. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  9199. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  9200. }
  9201. }
  9202. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  9203. if (!g_cublas_loaded) return false;
  9204. ggml_cuda_func_t func;
  9205. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  9206. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  9207. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  9208. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  9209. return false;
  9210. }
  9211. if (tensor->op == GGML_OP_MUL_MAT) {
  9212. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  9213. #ifndef NDEBUG
  9214. 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]);
  9215. #endif
  9216. return false;
  9217. }
  9218. }
  9219. switch (tensor->op) {
  9220. case GGML_OP_REPEAT:
  9221. func = ggml_cuda_repeat;
  9222. break;
  9223. case GGML_OP_GET_ROWS:
  9224. func = ggml_cuda_get_rows;
  9225. break;
  9226. case GGML_OP_DUP:
  9227. func = ggml_cuda_dup;
  9228. break;
  9229. case GGML_OP_ADD:
  9230. func = ggml_cuda_add;
  9231. break;
  9232. case GGML_OP_ACC:
  9233. func = ggml_cuda_acc;
  9234. break;
  9235. case GGML_OP_MUL:
  9236. func = ggml_cuda_mul;
  9237. break;
  9238. case GGML_OP_DIV:
  9239. func = ggml_cuda_div;
  9240. break;
  9241. case GGML_OP_UNARY:
  9242. switch (ggml_get_unary_op(tensor)) {
  9243. case GGML_UNARY_OP_GELU:
  9244. func = ggml_cuda_gelu;
  9245. break;
  9246. case GGML_UNARY_OP_SILU:
  9247. func = ggml_cuda_silu;
  9248. break;
  9249. case GGML_UNARY_OP_GELU_QUICK:
  9250. func = ggml_cuda_gelu_quick;
  9251. break;
  9252. case GGML_UNARY_OP_TANH:
  9253. func = ggml_cuda_tanh;
  9254. break;
  9255. case GGML_UNARY_OP_RELU:
  9256. func = ggml_cuda_relu;
  9257. break;
  9258. case GGML_UNARY_OP_HARDSIGMOID:
  9259. func = ggml_cuda_hardsigmoid;
  9260. break;
  9261. case GGML_UNARY_OP_HARDSWISH:
  9262. func = ggml_cuda_hardswish;
  9263. break;
  9264. default:
  9265. return false;
  9266. }
  9267. break;
  9268. case GGML_OP_NORM:
  9269. func = ggml_cuda_norm;
  9270. break;
  9271. case GGML_OP_GROUP_NORM:
  9272. func = ggml_cuda_group_norm;
  9273. break;
  9274. case GGML_OP_CONCAT:
  9275. func = ggml_cuda_concat;
  9276. break;
  9277. case GGML_OP_UPSCALE:
  9278. func = ggml_cuda_upscale;
  9279. break;
  9280. case GGML_OP_PAD:
  9281. func = ggml_cuda_pad;
  9282. break;
  9283. case GGML_OP_LEAKY_RELU:
  9284. func = ggml_cuda_leaky_relu;
  9285. break;
  9286. case GGML_OP_RMS_NORM:
  9287. func = ggml_cuda_rms_norm;
  9288. break;
  9289. case GGML_OP_MUL_MAT:
  9290. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  9291. return false;
  9292. }
  9293. func = ggml_cuda_mul_mat;
  9294. break;
  9295. case GGML_OP_MUL_MAT_ID:
  9296. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  9297. return false;
  9298. }
  9299. func = ggml_cuda_mul_mat_id;
  9300. break;
  9301. case GGML_OP_SCALE:
  9302. func = ggml_cuda_scale;
  9303. break;
  9304. case GGML_OP_SQR:
  9305. func = ggml_cuda_sqr;
  9306. break;
  9307. case GGML_OP_CLAMP:
  9308. func = ggml_cuda_clamp;
  9309. break;
  9310. case GGML_OP_CPY:
  9311. func = ggml_cuda_cpy;
  9312. break;
  9313. case GGML_OP_CONT:
  9314. func = ggml_cuda_dup;
  9315. break;
  9316. case GGML_OP_NONE:
  9317. case GGML_OP_RESHAPE:
  9318. case GGML_OP_VIEW:
  9319. case GGML_OP_PERMUTE:
  9320. case GGML_OP_TRANSPOSE:
  9321. func = ggml_cuda_nop;
  9322. break;
  9323. case GGML_OP_DIAG_MASK_INF:
  9324. func = ggml_cuda_diag_mask_inf;
  9325. break;
  9326. case GGML_OP_SOFT_MAX:
  9327. func = ggml_cuda_soft_max;
  9328. break;
  9329. case GGML_OP_ROPE:
  9330. func = ggml_cuda_rope;
  9331. break;
  9332. case GGML_OP_ALIBI:
  9333. func = ggml_cuda_alibi;
  9334. break;
  9335. case GGML_OP_IM2COL:
  9336. func = ggml_cuda_im2col;
  9337. break;
  9338. case GGML_OP_POOL_2D:
  9339. func = ggml_cuda_pool2d;
  9340. break;
  9341. case GGML_OP_SUM_ROWS:
  9342. func = ggml_cuda_sum_rows;
  9343. break;
  9344. case GGML_OP_ARGSORT:
  9345. func = ggml_cuda_argsort;
  9346. break;
  9347. default:
  9348. return false;
  9349. }
  9350. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  9351. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  9352. }
  9353. if (params->ith != 0) {
  9354. return true;
  9355. }
  9356. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  9357. return true;
  9358. }
  9359. func(tensor->src[0], tensor->src[1], tensor);
  9360. return true;
  9361. }
  9362. GGML_CALL int ggml_cuda_get_device_count() {
  9363. int device_count;
  9364. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  9365. return 0;
  9366. }
  9367. return device_count;
  9368. }
  9369. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  9370. cudaDeviceProp prop;
  9371. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  9372. snprintf(description, description_size, "%s", prop.name);
  9373. }
  9374. ////////////////////////////////////////////////////////////////////////////////
  9375. // backend interface
  9376. #define UNUSED GGML_UNUSED
  9377. struct ggml_backend_cuda_context {
  9378. int device;
  9379. std::string name;
  9380. };
  9381. // cuda buffer
  9382. struct ggml_backend_cuda_buffer_context {
  9383. int device;
  9384. void * dev_ptr = nullptr;
  9385. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  9386. size_t temp_tensor_extra_index = 0;
  9387. std::string name;
  9388. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  9389. device(device), dev_ptr(dev_ptr),
  9390. name(GGML_CUDA_NAME + std::to_string(device)) {
  9391. }
  9392. ~ggml_backend_cuda_buffer_context() {
  9393. delete[] temp_tensor_extras;
  9394. }
  9395. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  9396. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  9397. if (temp_tensor_extras == nullptr) {
  9398. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  9399. }
  9400. size_t alloc_index = temp_tensor_extra_index;
  9401. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  9402. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  9403. memset(extra, 0, sizeof(*extra));
  9404. return extra;
  9405. }
  9406. };
  9407. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  9408. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9409. return ctx->name.c_str();
  9410. }
  9411. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  9412. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  9413. }
  9414. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9415. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9416. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  9417. delete ctx;
  9418. }
  9419. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  9420. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9421. return ctx->dev_ptr;
  9422. }
  9423. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9424. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9425. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  9426. assert(tensor->view_src->buffer->buft == buffer->buft);
  9427. tensor->backend = tensor->view_src->backend;
  9428. tensor->extra = tensor->view_src->extra;
  9429. return;
  9430. }
  9431. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  9432. extra->data_device[ctx->device] = tensor->data;
  9433. tensor->backend = GGML_BACKEND_TYPE_GPU;
  9434. tensor->extra = extra;
  9435. if (ggml_is_quantized(tensor->type)) {
  9436. // initialize padding to 0 to avoid possible NaN values
  9437. size_t original_size = ggml_nbytes(tensor);
  9438. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  9439. if (padded_size > original_size && tensor->view_src == nullptr) {
  9440. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  9441. }
  9442. }
  9443. }
  9444. 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) {
  9445. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9446. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9447. ggml_cuda_set_device(ctx->device);
  9448. CUDA_CHECK(cudaDeviceSynchronize());
  9449. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  9450. CUDA_CHECK(cudaDeviceSynchronize());
  9451. }
  9452. 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) {
  9453. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9454. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9455. ggml_cuda_set_device(ctx->device);
  9456. CUDA_CHECK(cudaDeviceSynchronize());
  9457. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  9458. CUDA_CHECK(cudaDeviceSynchronize());
  9459. }
  9460. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  9461. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  9462. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  9463. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9464. ggml_cuda_set_device(src_ctx->device);
  9465. CUDA_CHECK(cudaDeviceSynchronize());
  9466. ggml_cuda_set_device(dst_ctx->device);
  9467. CUDA_CHECK(cudaDeviceSynchronize());
  9468. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  9469. CUDA_CHECK(cudaDeviceSynchronize());
  9470. return true;
  9471. }
  9472. return false;
  9473. }
  9474. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9475. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  9476. ggml_cuda_set_device(ctx->device);
  9477. CUDA_CHECK(cudaDeviceSynchronize());
  9478. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  9479. CUDA_CHECK(cudaDeviceSynchronize());
  9480. }
  9481. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  9482. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  9483. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  9484. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  9485. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  9486. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  9487. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  9488. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  9489. /* .clear = */ ggml_backend_cuda_buffer_clear,
  9490. /* .reset = */ NULL,
  9491. };
  9492. // cuda buffer type
  9493. struct ggml_backend_cuda_buffer_type_context {
  9494. int device;
  9495. std::string name;
  9496. };
  9497. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9498. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9499. return ctx->name.c_str();
  9500. }
  9501. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9502. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9503. ggml_cuda_set_device(buft_ctx->device);
  9504. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  9505. void * dev_ptr;
  9506. cudaError_t err = cudaMalloc(&dev_ptr, size);
  9507. if (err != cudaSuccess) {
  9508. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  9509. return nullptr;
  9510. }
  9511. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  9512. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  9513. }
  9514. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9515. return 128;
  9516. UNUSED(buft);
  9517. }
  9518. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9519. size_t size = ggml_nbytes(tensor);
  9520. int64_t ne0 = tensor->ne[0];
  9521. if (ggml_is_quantized(tensor->type)) {
  9522. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9523. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9524. }
  9525. }
  9526. return size;
  9527. UNUSED(buft);
  9528. }
  9529. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9530. if (!ggml_backend_is_cuda(backend)) {
  9531. return false;
  9532. }
  9533. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9534. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9535. return buft_ctx->device == cuda_ctx->device;
  9536. }
  9537. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  9538. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  9539. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  9540. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  9541. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9542. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  9543. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  9544. /* .is_host = */ NULL,
  9545. };
  9546. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  9547. // FIXME: this is not thread safe
  9548. if (device >= ggml_backend_cuda_get_device_count()) {
  9549. return nullptr;
  9550. }
  9551. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  9552. static bool ggml_backend_cuda_buffer_type_initialized = false;
  9553. if (!ggml_backend_cuda_buffer_type_initialized) {
  9554. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  9555. ggml_backend_cuda_buffer_types[i] = {
  9556. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  9557. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  9558. };
  9559. }
  9560. ggml_backend_cuda_buffer_type_initialized = true;
  9561. }
  9562. return &ggml_backend_cuda_buffer_types[device];
  9563. }
  9564. // cuda split buffer
  9565. struct ggml_backend_cuda_split_buffer_context {
  9566. ~ggml_backend_cuda_split_buffer_context() {
  9567. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  9568. for (int id = 0; id < g_device_count; ++id) {
  9569. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9570. if (extra->events[id][is] != nullptr) {
  9571. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  9572. }
  9573. }
  9574. if (extra->data_device[id] != nullptr) {
  9575. CUDA_CHECK(cudaFree(extra->data_device[id]));
  9576. }
  9577. }
  9578. delete extra;
  9579. }
  9580. }
  9581. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  9582. };
  9583. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  9584. return GGML_CUDA_NAME "_Split";
  9585. UNUSED(buffer);
  9586. }
  9587. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  9588. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  9589. UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  9590. }
  9591. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9592. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9593. delete ctx;
  9594. }
  9595. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  9596. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  9597. return (void *)0x1000;
  9598. UNUSED(buffer);
  9599. }
  9600. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9601. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  9602. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9603. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9604. const int64_t ne0 = tensor->ne[0];
  9605. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  9606. ctx->tensor_extras.push_back(extra);
  9607. for (int id = 0; id < g_device_count; ++id) {
  9608. int64_t row_low, row_high;
  9609. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9610. int64_t nrows_split = row_high - row_low;
  9611. if (nrows_split == 0) {
  9612. continue;
  9613. }
  9614. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9615. const size_t original_size = size;
  9616. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9617. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9618. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9619. }
  9620. // FIXME: do not crash if cudaMalloc fails
  9621. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  9622. ggml_cuda_set_device(id);
  9623. char * buf;
  9624. CUDA_CHECK(cudaMalloc(&buf, size));
  9625. // set padding to 0 to avoid possible NaN values
  9626. if (size > original_size) {
  9627. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  9628. }
  9629. extra->data_device[id] = buf;
  9630. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9631. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  9632. }
  9633. }
  9634. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  9635. tensor->extra = extra;
  9636. }
  9637. 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) {
  9638. // split tensors must always be set in their entirety at once
  9639. GGML_ASSERT(offset == 0);
  9640. GGML_ASSERT(size == ggml_nbytes(tensor));
  9641. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9642. const int64_t ne0 = tensor->ne[0];
  9643. const size_t nb1 = tensor->nb[1];
  9644. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9645. for (int id = 0; id < g_device_count; ++id) {
  9646. int64_t row_low, row_high;
  9647. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9648. int64_t nrows_split = row_high - row_low;
  9649. if (nrows_split == 0) {
  9650. continue;
  9651. }
  9652. const size_t offset_split = row_low*nb1;
  9653. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9654. const size_t original_size = size;
  9655. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9656. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9657. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9658. }
  9659. const char * buf_host = (const char *)data + offset_split;
  9660. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  9661. }
  9662. }
  9663. 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) {
  9664. // split tensors must always be set in their entirety at once
  9665. GGML_ASSERT(offset == 0);
  9666. GGML_ASSERT(size == ggml_nbytes(tensor));
  9667. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9668. const int64_t ne0 = tensor->ne[0];
  9669. const size_t nb1 = tensor->nb[1];
  9670. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9671. for (int id = 0; id < g_device_count; ++id) {
  9672. int64_t row_low, row_high;
  9673. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9674. int64_t nrows_split = row_high - row_low;
  9675. if (nrows_split == 0) {
  9676. continue;
  9677. }
  9678. const size_t offset_split = row_low*nb1;
  9679. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9680. const size_t original_size = size;
  9681. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9682. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9683. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9684. }
  9685. char * buf_host = (char *)data + offset_split;
  9686. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  9687. }
  9688. }
  9689. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9690. UNUSED(buffer);
  9691. UNUSED(value);
  9692. }
  9693. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  9694. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  9695. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  9696. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  9697. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  9698. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  9699. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  9700. /* .cpy_tensor = */ NULL,
  9701. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  9702. /* .reset = */ NULL,
  9703. };
  9704. // cuda split buffer type
  9705. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9706. return GGML_CUDA_NAME "_Split";
  9707. UNUSED(buft);
  9708. }
  9709. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9710. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  9711. // instead, we allocate them for each tensor separately in init_tensor
  9712. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  9713. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  9714. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  9715. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  9716. }
  9717. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9718. return 128;
  9719. UNUSED(buft);
  9720. }
  9721. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9722. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9723. size_t total_size = 0;
  9724. const int64_t ne0 = tensor->ne[0];
  9725. for (int id = 0; id < g_device_count; ++id) {
  9726. int64_t row_low, row_high;
  9727. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9728. int64_t nrows_split = row_high - row_low;
  9729. if (nrows_split == 0) {
  9730. continue;
  9731. }
  9732. total_size += ggml_nbytes_split(tensor, nrows_split);
  9733. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9734. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9735. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9736. }
  9737. }
  9738. return total_size;
  9739. }
  9740. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9741. return ggml_backend_is_cuda(backend);
  9742. UNUSED(buft);
  9743. }
  9744. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9745. return false;
  9746. UNUSED(buft);
  9747. }
  9748. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9749. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9750. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9751. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9752. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9753. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9754. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9755. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9756. };
  9757. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9758. // FIXME: this is not thread safe
  9759. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9760. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9761. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9762. if (all_zero) {
  9763. tensor_split_arr = g_default_tensor_split;
  9764. } else {
  9765. float split_sum = 0.0f;
  9766. for (int i = 0; i < g_device_count; ++i) {
  9767. tensor_split_arr[i] = split_sum;
  9768. split_sum += tensor_split[i];
  9769. }
  9770. for (int i = 0; i < g_device_count; ++i) {
  9771. tensor_split_arr[i] /= split_sum;
  9772. }
  9773. }
  9774. auto it = buft_map.find(tensor_split_arr);
  9775. if (it != buft_map.end()) {
  9776. return &it->second;
  9777. }
  9778. struct ggml_backend_buffer_type buft {
  9779. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9780. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9781. };
  9782. auto result = buft_map.emplace(tensor_split_arr, buft);
  9783. return &result.first->second;
  9784. }
  9785. // host buffer type
  9786. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9787. return GGML_CUDA_NAME "_Host";
  9788. UNUSED(buft);
  9789. }
  9790. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9791. return GGML_CUDA_NAME "_Host";
  9792. UNUSED(buffer);
  9793. }
  9794. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9795. ggml_cuda_host_free(buffer->context);
  9796. }
  9797. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9798. void * ptr = ggml_cuda_host_malloc(size);
  9799. if (ptr == nullptr) {
  9800. // fallback to cpu buffer
  9801. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9802. }
  9803. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9804. buffer->buft = buft;
  9805. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9806. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9807. return buffer;
  9808. }
  9809. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9810. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9811. /* .iface = */ {
  9812. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9813. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9814. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9815. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9816. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9817. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9818. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9819. },
  9820. /* .context = */ nullptr,
  9821. };
  9822. return &ggml_backend_cuda_buffer_type_host;
  9823. }
  9824. // backend
  9825. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9826. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9827. return cuda_ctx->name.c_str();
  9828. }
  9829. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9830. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9831. delete cuda_ctx;
  9832. delete backend;
  9833. }
  9834. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9835. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9836. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9837. }
  9838. 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) {
  9839. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9840. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9841. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9842. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9843. }
  9844. 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) {
  9845. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9846. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9847. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9848. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9849. }
  9850. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9851. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9852. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9853. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9854. return true;
  9855. }
  9856. return false;
  9857. }
  9858. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9859. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9860. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9861. UNUSED(backend);
  9862. }
  9863. GGML_CALL static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9864. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9865. ggml_cuda_set_main_device(cuda_ctx->device);
  9866. ggml_compute_params params = {};
  9867. params.type = GGML_TASK_TYPE_COMPUTE;
  9868. params.ith = 0;
  9869. for (int i = 0; i < cgraph->n_nodes; i++) {
  9870. ggml_tensor * node = cgraph->nodes[i];
  9871. 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) {
  9872. continue;
  9873. }
  9874. #ifndef NDEBUG
  9875. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9876. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9877. assert(node->extra != nullptr);
  9878. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9879. if (node->src[j] != nullptr) {
  9880. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9881. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9882. assert(node->src[j]->extra != nullptr);
  9883. }
  9884. }
  9885. #endif
  9886. bool ok = ggml_cuda_compute_forward(&params, node);
  9887. if (!ok) {
  9888. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9889. }
  9890. GGML_ASSERT(ok);
  9891. }
  9892. return true;
  9893. }
  9894. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9895. switch (op->op) {
  9896. case GGML_OP_UNARY:
  9897. switch (ggml_get_unary_op(op)) {
  9898. case GGML_UNARY_OP_GELU:
  9899. case GGML_UNARY_OP_SILU:
  9900. case GGML_UNARY_OP_RELU:
  9901. case GGML_UNARY_OP_HARDSIGMOID:
  9902. case GGML_UNARY_OP_HARDSWISH:
  9903. case GGML_UNARY_OP_GELU_QUICK:
  9904. case GGML_UNARY_OP_TANH:
  9905. return true;
  9906. default:
  9907. return false;
  9908. }
  9909. break;
  9910. case GGML_OP_MUL_MAT:
  9911. case GGML_OP_MUL_MAT_ID:
  9912. {
  9913. struct ggml_tensor * a;
  9914. struct ggml_tensor * b;
  9915. if (op->op == GGML_OP_MUL_MAT) {
  9916. a = op->src[0];
  9917. b = op->src[1];
  9918. } else {
  9919. a = op->src[2];
  9920. b = op->src[1];
  9921. }
  9922. if (a->ne[3] != b->ne[3]) {
  9923. return false;
  9924. }
  9925. ggml_type a_type = a->type;
  9926. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  9927. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ3_S ||
  9928. a_type == GGML_TYPE_IQ2_S) {
  9929. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9930. return false;
  9931. }
  9932. }
  9933. return true;
  9934. } break;
  9935. case GGML_OP_GET_ROWS:
  9936. {
  9937. switch (op->src[0]->type) {
  9938. case GGML_TYPE_F16:
  9939. case GGML_TYPE_F32:
  9940. case GGML_TYPE_Q4_0:
  9941. case GGML_TYPE_Q4_1:
  9942. case GGML_TYPE_Q5_0:
  9943. case GGML_TYPE_Q5_1:
  9944. case GGML_TYPE_Q8_0:
  9945. return true;
  9946. default:
  9947. return false;
  9948. }
  9949. } break;
  9950. case GGML_OP_CPY:
  9951. {
  9952. ggml_type src0_type = op->src[0]->type;
  9953. ggml_type src1_type = op->src[1]->type;
  9954. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9955. return true;
  9956. }
  9957. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9958. return true;
  9959. }
  9960. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9961. return true;
  9962. }
  9963. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9964. return true;
  9965. }
  9966. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9967. return true;
  9968. }
  9969. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9970. return true;
  9971. }
  9972. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9973. return true;
  9974. }
  9975. return false;
  9976. } break;
  9977. case GGML_OP_DUP:
  9978. case GGML_OP_REPEAT:
  9979. case GGML_OP_CONCAT:
  9980. {
  9981. ggml_type src0_type = op->src[0]->type;
  9982. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9983. } break;
  9984. case GGML_OP_NONE:
  9985. case GGML_OP_RESHAPE:
  9986. case GGML_OP_VIEW:
  9987. case GGML_OP_PERMUTE:
  9988. case GGML_OP_TRANSPOSE:
  9989. case GGML_OP_NORM:
  9990. case GGML_OP_ADD:
  9991. case GGML_OP_MUL:
  9992. case GGML_OP_DIV:
  9993. case GGML_OP_RMS_NORM:
  9994. case GGML_OP_SCALE:
  9995. case GGML_OP_SQR:
  9996. case GGML_OP_CLAMP:
  9997. case GGML_OP_CONT:
  9998. case GGML_OP_DIAG_MASK_INF:
  9999. case GGML_OP_SOFT_MAX:
  10000. case GGML_OP_ROPE:
  10001. case GGML_OP_ALIBI:
  10002. case GGML_OP_IM2COL:
  10003. case GGML_OP_POOL_2D:
  10004. case GGML_OP_SUM_ROWS:
  10005. case GGML_OP_ARGSORT:
  10006. case GGML_OP_ACC:
  10007. case GGML_OP_GROUP_NORM:
  10008. case GGML_OP_UPSCALE:
  10009. case GGML_OP_PAD:
  10010. case GGML_OP_LEAKY_RELU:
  10011. return true;
  10012. default:
  10013. return false;
  10014. }
  10015. UNUSED(backend);
  10016. }
  10017. static ggml_backend_i ggml_backend_cuda_interface = {
  10018. /* .get_name = */ ggml_backend_cuda_name,
  10019. /* .free = */ ggml_backend_cuda_free,
  10020. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  10021. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  10022. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  10023. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  10024. /* .synchronize = */ ggml_backend_cuda_synchronize,
  10025. /* .graph_plan_create = */ NULL,
  10026. /* .graph_plan_free = */ NULL,
  10027. /* .graph_plan_compute = */ NULL,
  10028. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  10029. /* .supports_op = */ ggml_backend_cuda_supports_op,
  10030. };
  10031. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  10032. ggml_init_cublas(); // TODO: remove from ggml.c
  10033. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  10034. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  10035. return nullptr;
  10036. }
  10037. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  10038. ggml_cuda_set_main_device(device);
  10039. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  10040. /* .device = */ device,
  10041. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  10042. };
  10043. ggml_backend_t cuda_backend = new ggml_backend {
  10044. /* .interface = */ ggml_backend_cuda_interface,
  10045. /* .context = */ ctx
  10046. };
  10047. return cuda_backend;
  10048. }
  10049. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  10050. return backend && backend->iface.get_name == ggml_backend_cuda_name;
  10051. }
  10052. GGML_CALL int ggml_backend_cuda_get_device_count() {
  10053. return ggml_cuda_get_device_count();
  10054. }
  10055. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  10056. ggml_cuda_get_device_description(device, description, description_size);
  10057. }
  10058. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  10059. ggml_cuda_set_device(device);
  10060. CUDA_CHECK(cudaMemGetInfo(free, total));
  10061. }
  10062. // backend registry
  10063. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  10064. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  10065. return cuda_backend;
  10066. UNUSED(params);
  10067. }
  10068. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  10069. GGML_CALL int ggml_backend_cuda_reg_devices() {
  10070. int device_count = ggml_cuda_get_device_count();
  10071. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  10072. for (int i = 0; i < device_count; i++) {
  10073. char name[128];
  10074. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  10075. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  10076. }
  10077. return device_count;
  10078. }