ggml-cuda.cu 449 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711
  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. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  160. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  161. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  162. #if __has_builtin(__builtin_elementwise_sub_sat)
  163. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  164. return reinterpret_cast<const int &>(c);
  165. #else
  166. int8x4_t c;
  167. int16_t tmp;
  168. #pragma unroll
  169. for (int i = 0; i < 4; i++) {
  170. tmp = va[i] - vb[i];
  171. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  172. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  173. c[i] = tmp;
  174. }
  175. return reinterpret_cast<int &>(c);
  176. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  177. }
  178. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  179. return __vsubss4(a, b);
  180. }
  181. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  182. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  183. c = __builtin_amdgcn_sdot4(a, b, c, false);
  184. #elif defined(RDNA3)
  185. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  186. #elif defined(__gfx1010__) || defined(__gfx900__)
  187. int tmp1;
  188. int tmp2;
  189. asm("\n \
  190. 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 \
  191. 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 \
  192. v_add3_u32 %0, %1, %2, %0 \n \
  193. 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 \
  194. 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 \
  195. v_add3_u32 %0, %1, %2, %0 \n \
  196. "
  197. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  198. : "v"(a), "v"(b)
  199. );
  200. #else
  201. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  202. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  203. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  204. #endif
  205. return c;
  206. }
  207. #endif // defined(GGML_USE_HIPBLAS)
  208. #if defined(_MSC_VER)
  209. #pragma warning(disable: 4244 4267) // possible loss of data
  210. #endif
  211. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  212. [[noreturn]]
  213. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  214. int id = -1; // in case cudaGetDevice fails
  215. cudaGetDevice(&id);
  216. fprintf(stderr, "CUDA error: %s\n", msg);
  217. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  218. fprintf(stderr, " %s\n", stmt);
  219. // abort with GGML_ASSERT to get a stack trace
  220. GGML_ASSERT(!"CUDA error");
  221. }
  222. #define CUDA_CHECK_GEN(err, success, error_fn) \
  223. do { \
  224. auto err_ = (err); \
  225. if (err_ != (success)) { \
  226. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  227. } \
  228. } while (0)
  229. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  230. #if CUDART_VERSION >= 12000
  231. static const char * cublas_get_error_str(const cublasStatus_t err) {
  232. return cublasGetStatusString(err);
  233. }
  234. #else
  235. static const char * cublas_get_error_str(const cublasStatus_t err) {
  236. switch (err) {
  237. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  238. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  239. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  240. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  241. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  242. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  243. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  244. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  245. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  246. default: return "unknown error";
  247. }
  248. }
  249. #endif // CUDART_VERSION >= 12000
  250. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  251. #if !defined(GGML_USE_HIPBLAS)
  252. static const char * cu_get_error_str(CUresult err) {
  253. const char * err_str;
  254. cuGetErrorString(err, &err_str);
  255. return err_str;
  256. }
  257. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  258. #endif
  259. #if CUDART_VERSION >= 11100
  260. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  261. #else
  262. #define GGML_CUDA_ASSUME(x)
  263. #endif // CUDART_VERSION >= 11100
  264. #ifdef GGML_CUDA_F16
  265. typedef half dfloat; // dequantize float
  266. typedef half2 dfloat2;
  267. #else
  268. typedef float dfloat; // dequantize float
  269. typedef float2 dfloat2;
  270. #endif //GGML_CUDA_F16
  271. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  272. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  273. int x32 = 0;
  274. x32 |= x16[0] << 0;
  275. x32 |= x16[1] << 16;
  276. return x32;
  277. }
  278. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  279. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  280. int x32 = 0;
  281. x32 |= x16[0] << 0;
  282. x32 |= x16[1] << 16;
  283. return x32;
  284. }
  285. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  286. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  287. }
  288. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  289. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  290. }
  291. template<typename T>
  292. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  293. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  294. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  295. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  296. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  297. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  298. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  299. typedef void (*ggml_cuda_op_mul_mat_t)(
  300. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  301. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  302. const int64_t src1_padded_row_size, cudaStream_t stream);
  303. typedef void (*ggml_cuda_op_flatten_t)(
  304. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  305. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  306. // QK = number of values after dequantization
  307. // QR = QK / number of values before dequantization
  308. // QI = number of 32 bit integers before dequantization
  309. #define QK4_0 32
  310. #define QR4_0 2
  311. #define QI4_0 (QK4_0 / (4 * QR4_0))
  312. typedef struct {
  313. half d; // delta
  314. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  315. } block_q4_0;
  316. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  317. #define QK4_1 32
  318. #define QR4_1 2
  319. #define QI4_1 (QK4_1 / (4 * QR4_1))
  320. typedef struct {
  321. half2 dm; // dm.x = delta, dm.y = min
  322. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  323. } block_q4_1;
  324. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  325. #define QK5_0 32
  326. #define QR5_0 2
  327. #define QI5_0 (QK5_0 / (4 * QR5_0))
  328. typedef struct {
  329. half d; // delta
  330. uint8_t qh[4]; // 5-th bit of quants
  331. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  332. } block_q5_0;
  333. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  334. #define QK5_1 32
  335. #define QR5_1 2
  336. #define QI5_1 (QK5_1 / (4 * QR5_1))
  337. typedef struct {
  338. half2 dm; // dm.x = delta, dm.y = min
  339. uint8_t qh[4]; // 5-th bit of quants
  340. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  341. } block_q5_1;
  342. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  343. #define QK8_0 32
  344. #define QR8_0 1
  345. #define QI8_0 (QK8_0 / (4 * QR8_0))
  346. typedef struct {
  347. half d; // delta
  348. int8_t qs[QK8_0]; // quants
  349. } block_q8_0;
  350. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  351. #define QK8_1 32
  352. #define QR8_1 1
  353. #define QI8_1 (QK8_1 / (4 * QR8_1))
  354. typedef struct {
  355. half2 ds; // ds.x = delta, ds.y = sum
  356. int8_t qs[QK8_0]; // quants
  357. } block_q8_1;
  358. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  359. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  360. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  361. typedef void (*load_tiles_cuda_t)(
  362. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  363. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  364. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  365. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  366. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  367. //================================= k-quants
  368. #ifdef GGML_QKK_64
  369. #define QK_K 64
  370. #define K_SCALE_SIZE 4
  371. #else
  372. #define QK_K 256
  373. #define K_SCALE_SIZE 12
  374. #endif
  375. #define QR2_K 4
  376. #define QI2_K (QK_K / (4*QR2_K))
  377. typedef struct {
  378. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  379. uint8_t qs[QK_K/4]; // quants
  380. half2 dm; // super-block scale for quantized scales/mins
  381. } block_q2_K;
  382. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  383. #define QR3_K 4
  384. #define QI3_K (QK_K / (4*QR3_K))
  385. typedef struct {
  386. uint8_t hmask[QK_K/8]; // quants - high bit
  387. uint8_t qs[QK_K/4]; // quants - low 2 bits
  388. #ifdef GGML_QKK_64
  389. uint8_t scales[2]; // scales, quantized with 8 bits
  390. #else
  391. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  392. #endif
  393. half d; // super-block scale
  394. } block_q3_K;
  395. //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");
  396. #define QR4_K 2
  397. #define QI4_K (QK_K / (4*QR4_K))
  398. #ifdef GGML_QKK_64
  399. typedef struct {
  400. half dm[2]; // super-block scales/mins
  401. uint8_t scales[2]; // 4-bit block scales/mins
  402. uint8_t qs[QK_K/2]; // 4--bit quants
  403. } block_q4_K;
  404. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  405. #else
  406. typedef struct {
  407. half2 dm; // super-block scale for quantized scales/mins
  408. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  409. uint8_t qs[QK_K/2]; // 4--bit quants
  410. } block_q4_K;
  411. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  412. #endif
  413. #define QR5_K 2
  414. #define QI5_K (QK_K / (4*QR5_K))
  415. #ifdef GGML_QKK_64
  416. typedef struct {
  417. half d; // super-block scale
  418. int8_t scales[QK_K/16]; // block scales
  419. uint8_t qh[QK_K/8]; // quants, high bit
  420. uint8_t qs[QK_K/2]; // quants, low 4 bits
  421. } block_q5_K;
  422. 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");
  423. #else
  424. typedef struct {
  425. half2 dm; // super-block scale for quantized scales/mins
  426. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  427. uint8_t qh[QK_K/8]; // quants, high bit
  428. uint8_t qs[QK_K/2]; // quants, low 4 bits
  429. } block_q5_K;
  430. 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");
  431. #endif
  432. #define QR6_K 2
  433. #define QI6_K (QK_K / (4*QR6_K))
  434. typedef struct {
  435. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  436. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  437. int8_t scales[QK_K/16]; // scales
  438. half d; // delta
  439. } block_q6_K;
  440. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  441. #define QR2_XXS 8
  442. #define QI2_XXS (QK_K / (4*QR2_XXS))
  443. typedef struct {
  444. half d;
  445. uint16_t qs[QK_K/8];
  446. } block_iq2_xxs;
  447. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  448. #define QR2_XS 8
  449. #define QI2_XS (QK_K / (4*QR2_XS))
  450. typedef struct {
  451. half d;
  452. uint16_t qs[QK_K/8];
  453. uint8_t scales[QK_K/32];
  454. } block_iq2_xs;
  455. 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");
  456. #define QR3_XXS 8
  457. #define QI3_XXS (QK_K / (4*QR3_XXS))
  458. typedef struct {
  459. half d;
  460. uint8_t qs[3*(QK_K/8)];
  461. } block_iq3_xxs;
  462. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_fp16_t) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  463. #define QR1_S 8
  464. #define QI1_S (QK_K / (4*QR1_S))
  465. typedef struct {
  466. half d;
  467. uint8_t qs[QK_K/8];
  468. uint8_t scales[QK_K/16];
  469. } block_iq1_s;
  470. static_assert(sizeof(block_iq1_s) == sizeof(ggml_fp16_t) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  471. #define QK4_NL 32
  472. #define QR4_NL 2
  473. #define QI4_NL (QK4_NL / (4*QR4_NL))
  474. typedef struct {
  475. half d;
  476. uint8_t qs[QK4_NL/2];
  477. } block_iq4_nl;
  478. static_assert(sizeof(block_iq4_nl) == sizeof(ggml_fp16_t) + QK4_NL/2, "wrong iq4_nl block size/padding");
  479. #define WARP_SIZE 32
  480. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  481. #define CUDA_GELU_BLOCK_SIZE 256
  482. #define CUDA_SILU_BLOCK_SIZE 256
  483. #define CUDA_TANH_BLOCK_SIZE 256
  484. #define CUDA_RELU_BLOCK_SIZE 256
  485. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  486. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  487. #define CUDA_SQR_BLOCK_SIZE 256
  488. #define CUDA_CPY_BLOCK_SIZE 32
  489. #define CUDA_SCALE_BLOCK_SIZE 256
  490. #define CUDA_CLAMP_BLOCK_SIZE 256
  491. #define CUDA_ROPE_BLOCK_SIZE 256
  492. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  493. #define CUDA_ALIBI_BLOCK_SIZE 32
  494. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  495. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  496. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  497. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  498. #define CUDA_UPSCALE_BLOCK_SIZE 256
  499. #define CUDA_CONCAT_BLOCK_SIZE 256
  500. #define CUDA_PAD_BLOCK_SIZE 256
  501. #define CUDA_ACC_BLOCK_SIZE 256
  502. #define CUDA_IM2COL_BLOCK_SIZE 256
  503. #define CUDA_POOL2D_BLOCK_SIZE 256
  504. #define CUDA_Q8_0_NE_ALIGN 2048
  505. // dmmv = dequantize_mul_mat_vec
  506. #ifndef GGML_CUDA_DMMV_X
  507. #define GGML_CUDA_DMMV_X 32
  508. #endif
  509. #ifndef GGML_CUDA_MMV_Y
  510. #define GGML_CUDA_MMV_Y 1
  511. #endif
  512. #ifndef K_QUANTS_PER_ITERATION
  513. #define K_QUANTS_PER_ITERATION 2
  514. #else
  515. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  516. #endif
  517. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  518. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  519. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  520. #define MUL_MAT_SRC1_COL_STRIDE 128
  521. #define MAX_STREAMS 8
  522. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  523. struct ggml_tensor_extra_gpu {
  524. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  525. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  526. };
  527. // this is faster on Windows
  528. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  529. static void ggml_cuda_set_device(const int device) {
  530. int current_device;
  531. CUDA_CHECK(cudaGetDevice(&current_device));
  532. if (device == current_device) {
  533. return;
  534. }
  535. CUDA_CHECK(cudaSetDevice(device));
  536. }
  537. static int g_device_count = -1;
  538. static int g_main_device = 0;
  539. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  540. struct cuda_device_capabilities {
  541. int cc; // compute capability
  542. size_t smpb; // max. shared memory per block
  543. bool vmm; // virtual memory support
  544. size_t vmm_granularity; // granularity of virtual memory
  545. };
  546. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  547. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  548. [[noreturn]]
  549. static __device__ void no_device_code(
  550. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  551. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  552. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  553. file_name, line, function_name, arch);
  554. (void) arch_list;
  555. #else
  556. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  557. file_name, line, function_name, arch, arch_list);
  558. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  559. __trap();
  560. (void) no_device_code; // suppress unused function warning
  561. }
  562. #ifdef __CUDA_ARCH__
  563. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  564. #else
  565. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  566. #endif // __CUDA_ARCH__
  567. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  568. #pragma unroll
  569. for (int mask = 16; mask > 0; mask >>= 1) {
  570. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  571. }
  572. return x;
  573. }
  574. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  575. #pragma unroll
  576. for (int mask = 16; mask > 0; mask >>= 1) {
  577. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  578. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  579. }
  580. return a;
  581. }
  582. //static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  583. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  584. //#pragma unroll
  585. // for (int mask = 16; mask > 0; mask >>= 1) {
  586. // a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  587. // }
  588. // return a;
  589. //#else
  590. // (void) a;
  591. // NO_DEVICE_CODE;
  592. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  593. //}
  594. static __device__ __forceinline__ float warp_reduce_max(float x) {
  595. #pragma unroll
  596. for (int mask = 16; mask > 0; mask >>= 1) {
  597. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  598. }
  599. return x;
  600. }
  601. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  602. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  603. //#pragma unroll
  604. // for (int mask = 16; mask > 0; mask >>= 1) {
  605. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  606. // }
  607. // return x;
  608. //#else
  609. // (void) x;
  610. // NO_DEVICE_CODE;
  611. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  612. //}
  613. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  614. return b;
  615. GGML_UNUSED(a);
  616. }
  617. static __device__ __forceinline__ float op_add(const float a, const float b) {
  618. return a + b;
  619. }
  620. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  621. return a * b;
  622. }
  623. static __device__ __forceinline__ float op_div(const float a, const float b) {
  624. return a / b;
  625. }
  626. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  627. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  628. int ne0, int ne1, int ne2, int ne3,
  629. int ne10, int ne11, int ne12, int ne13,
  630. /*int s0, */ int s1, int s2, int s3,
  631. /*int s10,*/ int s11, int s12, int s13) {
  632. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  633. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  634. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  635. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  636. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  637. return;
  638. }
  639. const int i11 = i1 % ne11;
  640. const int i12 = i2 % ne12;
  641. const int i13 = i3 % ne13;
  642. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  643. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  644. const size_t i_dst = i_src0;
  645. const src0_t * src0_row = src0 + i_src0;
  646. const src1_t * src1_row = src1 + i_src1;
  647. dst_t * dst_row = dst + i_dst;
  648. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  649. const int i10 = i0 % ne10;
  650. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  651. }
  652. }
  653. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  654. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  655. int ne0, int ne1, int ne2, int ne3,
  656. int ne10, int ne11, int ne12, int ne13,
  657. /*int s0, */ int s1, int s2, int s3,
  658. /*int s10,*/ int s11, int s12, int s13) {
  659. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  660. const int i3 = i/(ne2*ne1*ne0);
  661. const int i2 = (i/(ne1*ne0)) % ne2;
  662. const int i1 = (i/ne0) % ne1;
  663. const int i0 = i % ne0;
  664. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  665. return;
  666. }
  667. const int i11 = i1 % ne11;
  668. const int i12 = i2 % ne12;
  669. const int i13 = i3 % ne13;
  670. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  671. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  672. const size_t i_dst = i_src0;
  673. const src0_t * src0_row = src0 + i_src0;
  674. const src1_t * src1_row = src1 + i_src1;
  675. dst_t * dst_row = dst + i_dst;
  676. const int i10 = i0 % ne10;
  677. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  678. }
  679. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  680. const int ne10, const int ne11, const int ne12,
  681. const int nb1, const int nb2, int offset) {
  682. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  683. if (i >= ne) {
  684. return;
  685. }
  686. int src1_idx = i - offset;
  687. int oz = src1_idx / nb2;
  688. int oy = (src1_idx - (oz * nb2)) / nb1;
  689. int ox = src1_idx % nb1;
  690. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  691. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  692. } else {
  693. dst[i] = x[i];
  694. }
  695. }
  696. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  697. const float GELU_COEF_A = 0.044715f;
  698. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  699. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  700. if (i >= k) {
  701. return;
  702. }
  703. float xi = x[i];
  704. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  705. }
  706. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  707. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  708. if (i >= k) {
  709. return;
  710. }
  711. dst[i] = x[i] / (1.0f + expf(-x[i]));
  712. }
  713. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  714. const float GELU_QUICK_COEF = -1.702f;
  715. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  716. if (i >= k) {
  717. return;
  718. }
  719. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  720. }
  721. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  722. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  723. if (i >= k) {
  724. return;
  725. }
  726. dst[i] = tanhf(x[i]);
  727. }
  728. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  729. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  730. if (i >= k) {
  731. return;
  732. }
  733. dst[i] = fmaxf(x[i], 0);
  734. }
  735. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  736. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  737. if (i >= k) {
  738. return;
  739. }
  740. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  741. }
  742. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  743. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  744. if (i >= k) {
  745. return;
  746. }
  747. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  748. }
  749. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  750. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  751. if (i >= k) {
  752. return;
  753. }
  754. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  755. }
  756. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  757. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  758. if (i >= k) {
  759. return;
  760. }
  761. dst[i] = x[i] * x[i];
  762. }
  763. template <int block_size>
  764. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  765. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  766. const int tid = threadIdx.x;
  767. float2 mean_var = make_float2(0.f, 0.f);
  768. for (int col = tid; col < ncols; col += block_size) {
  769. const float xi = x[row*ncols + col];
  770. mean_var.x += xi;
  771. mean_var.y += xi * xi;
  772. }
  773. // sum up partial sums
  774. mean_var = warp_reduce_sum(mean_var);
  775. if (block_size > WARP_SIZE) {
  776. __shared__ float2 s_sum[32];
  777. int warp_id = threadIdx.x / WARP_SIZE;
  778. int lane_id = threadIdx.x % WARP_SIZE;
  779. if (lane_id == 0) {
  780. s_sum[warp_id] = mean_var;
  781. }
  782. __syncthreads();
  783. mean_var = s_sum[lane_id];
  784. mean_var = warp_reduce_sum(mean_var);
  785. }
  786. const float mean = mean_var.x / ncols;
  787. const float var = mean_var.y / ncols - mean * mean;
  788. const float inv_std = rsqrtf(var + eps);
  789. for (int col = tid; col < ncols; col += block_size) {
  790. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  791. }
  792. }
  793. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  794. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  795. if (nidx >= ne0) {
  796. return;
  797. }
  798. // operation
  799. int offset_dst =
  800. nidx +
  801. blockIdx.y * ne0 +
  802. blockIdx.z * ne0 * gridDim.y;
  803. if (blockIdx.z < ne02) { // src0
  804. int offset_src =
  805. nidx +
  806. blockIdx.y * ne0 +
  807. blockIdx.z * ne0 * gridDim.y;
  808. dst[offset_dst] = x[offset_src];
  809. } else {
  810. int offset_src =
  811. nidx +
  812. blockIdx.y * ne0 +
  813. (blockIdx.z - ne02) * ne0 * gridDim.y;
  814. dst[offset_dst] = y[offset_src];
  815. }
  816. }
  817. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  818. int ne0 = ne00 * scale_factor;
  819. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  820. if (nidx >= ne0) {
  821. return;
  822. }
  823. // operation
  824. int i00 = nidx / scale_factor;
  825. int i01 = blockIdx.y / scale_factor;
  826. int offset_src =
  827. i00 +
  828. i01 * ne00 +
  829. blockIdx.z * nb02;
  830. int offset_dst =
  831. nidx +
  832. blockIdx.y * ne0 +
  833. blockIdx.z * ne0 * gridDim.y;
  834. dst[offset_dst] = x[offset_src];
  835. }
  836. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  837. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  838. if (nidx >= ne0) {
  839. return;
  840. }
  841. // operation
  842. int offset_dst =
  843. nidx +
  844. blockIdx.y * ne0 +
  845. blockIdx.z * ne0 * gridDim.y;
  846. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  847. int offset_src =
  848. nidx +
  849. blockIdx.y * ne00 +
  850. blockIdx.z * ne00 * ne01;
  851. dst[offset_dst] = x[offset_src];
  852. } else {
  853. dst[offset_dst] = 0.0f;
  854. }
  855. }
  856. template <int block_size>
  857. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  858. int start = blockIdx.x * group_size;
  859. int end = start + group_size;
  860. start += threadIdx.x;
  861. if (end >= ne_elements) {
  862. end = ne_elements;
  863. }
  864. float tmp = 0.0f; // partial sum for thread in warp
  865. for (int j = start; j < end; j += block_size) {
  866. tmp += x[j];
  867. }
  868. tmp = warp_reduce_sum(tmp);
  869. if (block_size > WARP_SIZE) {
  870. __shared__ float s_sum[32];
  871. int warp_id = threadIdx.x / WARP_SIZE;
  872. int lane_id = threadIdx.x % WARP_SIZE;
  873. if (lane_id == 0) {
  874. s_sum[warp_id] = tmp;
  875. }
  876. __syncthreads();
  877. tmp = s_sum[lane_id];
  878. tmp = warp_reduce_sum(tmp);
  879. }
  880. float mean = tmp / group_size;
  881. tmp = 0.0f;
  882. for (int j = start; j < end; j += block_size) {
  883. float xi = x[j] - mean;
  884. dst[j] = xi;
  885. tmp += xi * xi;
  886. }
  887. tmp = warp_reduce_sum(tmp);
  888. if (block_size > WARP_SIZE) {
  889. __shared__ float s_sum[32];
  890. int warp_id = threadIdx.x / WARP_SIZE;
  891. int lane_id = threadIdx.x % WARP_SIZE;
  892. if (lane_id == 0) {
  893. s_sum[warp_id] = tmp;
  894. }
  895. __syncthreads();
  896. tmp = s_sum[lane_id];
  897. tmp = warp_reduce_sum(tmp);
  898. }
  899. float variance = tmp / group_size;
  900. float scale = rsqrtf(variance + eps);
  901. for (int j = start; j < end; j += block_size) {
  902. dst[j] *= scale;
  903. }
  904. }
  905. template <int block_size>
  906. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  907. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  908. const int tid = threadIdx.x;
  909. float tmp = 0.0f; // partial sum for thread in warp
  910. for (int col = tid; col < ncols; col += block_size) {
  911. const float xi = x[row*ncols + col];
  912. tmp += xi * xi;
  913. }
  914. // sum up partial sums
  915. tmp = warp_reduce_sum(tmp);
  916. if (block_size > WARP_SIZE) {
  917. __shared__ float s_sum[32];
  918. int warp_id = threadIdx.x / WARP_SIZE;
  919. int lane_id = threadIdx.x % WARP_SIZE;
  920. if (lane_id == 0) {
  921. s_sum[warp_id] = tmp;
  922. }
  923. __syncthreads();
  924. tmp = s_sum[lane_id];
  925. tmp = warp_reduce_sum(tmp);
  926. }
  927. const float mean = tmp / ncols;
  928. const float scale = rsqrtf(mean + eps);
  929. for (int col = tid; col < ncols; col += block_size) {
  930. dst[row*ncols + col] = scale * x[row*ncols + col];
  931. }
  932. }
  933. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  934. const block_q4_0 * x = (const block_q4_0 *) vx;
  935. const dfloat d = x[ib].d;
  936. const int vui = x[ib].qs[iqs];
  937. v.x = vui & 0xF;
  938. v.y = vui >> 4;
  939. #ifdef GGML_CUDA_F16
  940. v = __hsub2(v, {8.0f, 8.0f});
  941. v = __hmul2(v, {d, d});
  942. #else
  943. v.x = (v.x - 8.0f) * d;
  944. v.y = (v.y - 8.0f) * d;
  945. #endif // GGML_CUDA_F16
  946. }
  947. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  948. const block_q4_1 * x = (const block_q4_1 *) vx;
  949. const dfloat d = __low2half(x[ib].dm);
  950. const dfloat m = __high2half(x[ib].dm);
  951. const int vui = x[ib].qs[iqs];
  952. v.x = vui & 0xF;
  953. v.y = vui >> 4;
  954. #ifdef GGML_CUDA_F16
  955. v = __hmul2(v, {d, d});
  956. v = __hadd2(v, {m, m});
  957. #else
  958. v.x = (v.x * d) + m;
  959. v.y = (v.y * d) + m;
  960. #endif // GGML_CUDA_F16
  961. }
  962. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  963. const block_q5_0 * x = (const block_q5_0 *) vx;
  964. const dfloat d = x[ib].d;
  965. uint32_t qh;
  966. memcpy(&qh, x[ib].qh, sizeof(qh));
  967. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  968. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  969. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  970. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  971. #ifdef GGML_CUDA_F16
  972. v = __hsub2(v, {16.0f, 16.0f});
  973. v = __hmul2(v, {d, d});
  974. #else
  975. v.x = (v.x - 16.0f) * d;
  976. v.y = (v.y - 16.0f) * d;
  977. #endif // GGML_CUDA_F16
  978. }
  979. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  980. const block_q5_1 * x = (const block_q5_1 *) vx;
  981. const dfloat d = __low2half(x[ib].dm);
  982. const dfloat m = __high2half(x[ib].dm);
  983. uint32_t qh;
  984. memcpy(&qh, x[ib].qh, sizeof(qh));
  985. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  986. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  987. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  988. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  989. #ifdef GGML_CUDA_F16
  990. v = __hmul2(v, {d, d});
  991. v = __hadd2(v, {m, m});
  992. #else
  993. v.x = (v.x * d) + m;
  994. v.y = (v.y * d) + m;
  995. #endif // GGML_CUDA_F16
  996. }
  997. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  998. const block_q8_0 * x = (const block_q8_0 *) vx;
  999. const dfloat d = x[ib].d;
  1000. v.x = x[ib].qs[iqs + 0];
  1001. v.y = x[ib].qs[iqs + 1];
  1002. #ifdef GGML_CUDA_F16
  1003. v = __hmul2(v, {d, d});
  1004. #else
  1005. v.x *= d;
  1006. v.y *= d;
  1007. #endif // GGML_CUDA_F16
  1008. }
  1009. template<typename dst_t>
  1010. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1011. const int i = blockIdx.x;
  1012. // assume 32 threads
  1013. const int tid = threadIdx.x;
  1014. const int il = tid/8;
  1015. const int ir = tid%8;
  1016. const int ib = 8*i + ir;
  1017. if (ib >= nb32) {
  1018. return;
  1019. }
  1020. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1021. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1022. const float d = __half2float(x->d);
  1023. const float dm = -8*d;
  1024. const uint8_t * q = x->qs + 4*il;
  1025. for (int l = 0; l < 4; ++l) {
  1026. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1027. y[l+16] = d * (q[l] >> 4) + dm;
  1028. }
  1029. }
  1030. template<typename dst_t>
  1031. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1032. const int i = blockIdx.x;
  1033. // assume 32 threads
  1034. const int tid = threadIdx.x;
  1035. const int il = tid/8;
  1036. const int ir = tid%8;
  1037. const int ib = 8*i + ir;
  1038. if (ib >= nb32) {
  1039. return;
  1040. }
  1041. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1042. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1043. const float2 d = __half22float2(x->dm);
  1044. const uint8_t * q = x->qs + 4*il;
  1045. for (int l = 0; l < 4; ++l) {
  1046. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1047. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1048. }
  1049. }
  1050. //================================== k-quants
  1051. template<typename dst_t>
  1052. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1053. const int i = blockIdx.x;
  1054. const block_q2_K * x = (const block_q2_K *) vx;
  1055. const int tid = threadIdx.x;
  1056. #if QK_K == 256
  1057. const int n = tid/32;
  1058. const int l = tid - 32*n;
  1059. const int is = 8*n + l/16;
  1060. const uint8_t q = x[i].qs[32*n + l];
  1061. dst_t * y = yy + i*QK_K + 128*n;
  1062. float dall = __low2half(x[i].dm);
  1063. float dmin = __high2half(x[i].dm);
  1064. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1065. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1066. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1067. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1068. #else
  1069. const int is = tid/16; // 0 or 1
  1070. const int il = tid%16; // 0...15
  1071. const uint8_t q = x[i].qs[il] >> (2*is);
  1072. dst_t * y = yy + i*QK_K + 16*is + il;
  1073. float dall = __low2half(x[i].dm);
  1074. float dmin = __high2half(x[i].dm);
  1075. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1076. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1077. #endif
  1078. }
  1079. template<typename dst_t>
  1080. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1081. const int i = blockIdx.x;
  1082. const block_q3_K * x = (const block_q3_K *) vx;
  1083. #if QK_K == 256
  1084. const int r = threadIdx.x/4;
  1085. const int tid = r/2;
  1086. const int is0 = r%2;
  1087. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1088. const int n = tid / 4;
  1089. const int j = tid - 4*n;
  1090. uint8_t m = 1 << (4*n + j);
  1091. int is = 8*n + 2*j + is0;
  1092. int shift = 2*j;
  1093. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1094. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1095. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1096. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1097. float d_all = x[i].d;
  1098. float dl = d_all * (us - 32);
  1099. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1100. const uint8_t * q = x[i].qs + 32*n;
  1101. const uint8_t * hm = x[i].hmask;
  1102. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1103. #else
  1104. const int tid = threadIdx.x;
  1105. const int is = tid/16; // 0 or 1
  1106. const int il = tid%16; // 0...15
  1107. const int im = il/8; // 0...1
  1108. const int in = il%8; // 0...7
  1109. dst_t * y = yy + i*QK_K + 16*is + il;
  1110. const uint8_t q = x[i].qs[il] >> (2*is);
  1111. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1112. const float d = (float)x[i].d;
  1113. if (is == 0) {
  1114. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1115. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1116. } else {
  1117. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1118. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1119. }
  1120. #endif
  1121. }
  1122. #if QK_K == 256
  1123. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1124. if (j < 4) {
  1125. d = q[j] & 63; m = q[j + 4] & 63;
  1126. } else {
  1127. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1128. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1129. }
  1130. }
  1131. #endif
  1132. template<typename dst_t>
  1133. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1134. const block_q4_K * x = (const block_q4_K *) vx;
  1135. const int i = blockIdx.x;
  1136. #if QK_K == 256
  1137. // assume 32 threads
  1138. const int tid = threadIdx.x;
  1139. const int il = tid/8;
  1140. const int ir = tid%8;
  1141. const int is = 2*il;
  1142. const int n = 4;
  1143. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1144. const float dall = __low2half(x[i].dm);
  1145. const float dmin = __high2half(x[i].dm);
  1146. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1147. uint8_t sc, m;
  1148. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1149. const float d1 = dall * sc; const float m1 = dmin * m;
  1150. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1151. const float d2 = dall * sc; const float m2 = dmin * m;
  1152. for (int l = 0; l < n; ++l) {
  1153. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1154. y[l +32] = d2 * (q[l] >> 4) - m2;
  1155. }
  1156. #else
  1157. const int tid = threadIdx.x;
  1158. const uint8_t * q = x[i].qs;
  1159. dst_t * y = yy + i*QK_K;
  1160. const float d = (float)x[i].dm[0];
  1161. const float m = (float)x[i].dm[1];
  1162. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1163. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1164. #endif
  1165. }
  1166. template<typename dst_t>
  1167. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1168. const block_q5_K * x = (const block_q5_K *) vx;
  1169. const int i = blockIdx.x;
  1170. #if QK_K == 256
  1171. // assume 64 threads - this is very slightly better than the one below
  1172. const int tid = threadIdx.x;
  1173. const int il = tid/16; // il is in 0...3
  1174. const int ir = tid%16; // ir is in 0...15
  1175. const int is = 2*il; // is is in 0...6
  1176. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1177. const float dall = __low2half(x[i].dm);
  1178. const float dmin = __high2half(x[i].dm);
  1179. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1180. const uint8_t * qh = x[i].qh + 2*ir;
  1181. uint8_t sc, m;
  1182. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1183. const float d1 = dall * sc; const float m1 = dmin * m;
  1184. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1185. const float d2 = dall * sc; const float m2 = dmin * m;
  1186. uint8_t hm = 1 << (2*il);
  1187. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1188. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1189. hm <<= 1;
  1190. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1191. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1192. #else
  1193. const int tid = threadIdx.x;
  1194. const uint8_t q = x[i].qs[tid];
  1195. const int im = tid/8; // 0...3
  1196. const int in = tid%8; // 0...7
  1197. const int is = tid/16; // 0 or 1
  1198. const uint8_t h = x[i].qh[in] >> im;
  1199. const float d = x[i].d;
  1200. dst_t * y = yy + i*QK_K + tid;
  1201. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1202. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1203. #endif
  1204. }
  1205. template<typename dst_t>
  1206. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1207. const block_q6_K * x = (const block_q6_K *) vx;
  1208. const int i = blockIdx.x;
  1209. #if QK_K == 256
  1210. // assume 64 threads - this is very slightly better than the one below
  1211. const int tid = threadIdx.x;
  1212. const int ip = tid/32; // ip is 0 or 1
  1213. const int il = tid - 32*ip; // 0...32
  1214. const int is = 8*ip + il/16;
  1215. dst_t * y = yy + i*QK_K + 128*ip + il;
  1216. const float d = x[i].d;
  1217. const uint8_t * ql = x[i].ql + 64*ip + il;
  1218. const uint8_t qh = x[i].qh[32*ip + il];
  1219. const int8_t * sc = x[i].scales + is;
  1220. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1221. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1222. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1223. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1224. #else
  1225. // assume 32 threads
  1226. const int tid = threadIdx.x;
  1227. const int ip = tid/16; // 0 or 1
  1228. const int il = tid - 16*ip; // 0...15
  1229. dst_t * y = yy + i*QK_K + 16*ip + il;
  1230. const float d = x[i].d;
  1231. const uint8_t ql = x[i].ql[16*ip + il];
  1232. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1233. const int8_t * sc = x[i].scales;
  1234. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1235. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1236. #endif
  1237. }
  1238. static const __device__ uint64_t iq2xxs_grid[256] = {
  1239. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1240. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1241. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1242. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1243. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1244. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1245. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1246. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1247. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1248. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1249. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1250. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1251. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1252. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1253. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1254. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1255. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1256. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1257. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1258. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1259. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1260. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1261. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1262. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1263. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1264. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1265. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1266. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1267. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1268. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1269. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1270. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1271. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1272. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1273. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1274. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1275. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1276. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1277. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1278. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1279. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1280. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1281. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1282. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1283. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1284. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1285. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1286. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1287. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1288. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1289. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1290. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1291. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1292. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1293. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1294. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1295. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1296. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1297. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1298. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1299. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1300. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1301. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1302. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1303. };
  1304. static const __device__ uint64_t iq2xs_grid[512] = {
  1305. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1306. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1307. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1308. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1309. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1310. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  1311. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  1312. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  1313. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  1314. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  1315. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  1316. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  1317. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  1318. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  1319. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  1320. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  1321. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  1322. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  1323. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  1324. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  1325. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  1326. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  1327. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  1328. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  1329. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  1330. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  1331. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  1332. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  1333. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  1334. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  1335. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  1336. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  1337. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  1338. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  1339. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  1340. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  1341. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  1342. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  1343. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  1344. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  1345. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  1346. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  1347. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  1348. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  1349. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  1350. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  1351. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  1352. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  1353. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  1354. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  1355. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  1356. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  1357. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  1358. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  1359. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  1360. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  1361. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  1362. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  1363. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  1364. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  1365. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  1366. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  1367. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  1368. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  1369. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  1370. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  1371. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  1372. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  1373. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  1374. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  1375. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  1376. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  1377. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  1378. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  1379. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  1380. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  1381. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  1382. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  1383. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  1384. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  1385. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  1386. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  1387. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  1388. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  1389. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  1390. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  1391. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  1392. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  1393. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  1394. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  1395. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  1396. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  1397. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  1398. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  1399. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  1400. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  1401. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  1402. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  1403. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  1404. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  1405. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  1406. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  1407. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  1408. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  1409. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  1410. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  1411. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  1412. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  1413. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  1414. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  1415. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  1416. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  1417. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  1418. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  1419. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  1420. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  1421. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  1422. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  1423. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  1424. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  1425. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  1426. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  1427. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  1428. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  1429. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  1430. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  1431. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  1432. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  1433. };
  1434. static const __device__ uint32_t iq3xxs_grid[256] = {
  1435. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  1436. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  1437. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  1438. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  1439. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  1440. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  1441. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  1442. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  1443. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  1444. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  1445. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  1446. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  1447. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  1448. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  1449. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  1450. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  1451. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  1452. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  1453. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  1454. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  1455. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  1456. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  1457. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  1458. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  1459. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  1460. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  1461. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  1462. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  1463. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  1464. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  1465. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  1466. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  1467. };
  1468. static const __device__ uint64_t iq1s_grid[512] = {
  1469. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  1470. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  1471. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  1472. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  1473. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  1474. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  1475. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  1476. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  1477. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  1478. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  1479. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  1480. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  1481. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  1482. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  1483. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  1484. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  1485. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  1486. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  1487. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  1488. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  1489. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  1490. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  1491. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  1492. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  1493. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  1494. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  1495. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  1496. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  1497. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  1498. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  1499. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  1500. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  1501. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  1502. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  1503. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  1504. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  1505. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  1506. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  1507. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  1508. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  1509. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  1510. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  1511. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  1512. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  1513. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  1514. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  1515. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  1516. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  1517. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  1518. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  1519. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  1520. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  1521. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  1522. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  1523. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1524. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  1525. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  1526. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  1527. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1528. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  1529. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  1530. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  1531. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  1532. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1533. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  1534. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  1535. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  1536. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  1537. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  1538. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  1539. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  1540. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  1541. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  1542. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  1543. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  1544. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  1545. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  1546. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  1547. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  1548. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  1549. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  1550. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  1551. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  1552. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  1553. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  1554. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  1555. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  1556. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  1557. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  1558. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  1559. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  1560. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  1561. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  1562. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  1563. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  1564. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  1565. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  1566. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  1567. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  1568. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  1569. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  1570. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  1571. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  1572. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  1573. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  1574. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  1575. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  1576. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  1577. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  1578. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  1579. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  1580. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  1581. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  1582. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  1583. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  1584. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  1585. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  1586. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  1587. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  1588. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  1589. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  1590. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  1591. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  1592. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  1593. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  1594. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  1595. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  1596. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  1597. };
  1598. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1599. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1600. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1601. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1602. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1603. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1604. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1605. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1606. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1607. };
  1608. //#if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1609. static const __device__ uint64_t ksigns64[128] = {
  1610. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  1611. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  1612. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  1613. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  1614. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  1615. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  1616. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  1617. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  1618. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  1619. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  1620. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  1621. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  1622. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  1623. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  1624. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  1625. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  1626. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  1627. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  1628. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  1629. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  1630. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  1631. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  1632. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  1633. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  1634. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  1635. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  1636. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  1637. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  1638. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  1639. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  1640. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  1641. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  1642. };
  1643. //#endif
  1644. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  1645. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1646. switch (type) {
  1647. case GGML_TYPE_Q4_0:
  1648. case GGML_TYPE_Q4_1:
  1649. case GGML_TYPE_Q5_0:
  1650. case GGML_TYPE_Q5_1:
  1651. case GGML_TYPE_Q8_0:
  1652. case GGML_TYPE_Q2_K:
  1653. case GGML_TYPE_Q3_K:
  1654. case GGML_TYPE_Q4_K:
  1655. case GGML_TYPE_Q5_K:
  1656. case GGML_TYPE_Q6_K:
  1657. return true;
  1658. default:
  1659. return false;
  1660. }
  1661. }
  1662. template<typename dst_t>
  1663. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1664. const int i = blockIdx.x;
  1665. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1666. const int tid = threadIdx.x;
  1667. #if QK_K == 256
  1668. const int il = tid/8; // 0...3
  1669. const int ib = tid%8; // 0...7
  1670. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1671. const uint16_t * q2 = x[i].qs + 4*ib;
  1672. const uint8_t * aux8 = (const uint8_t *)q2;
  1673. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  1674. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1675. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1676. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1677. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1678. #else
  1679. assert(false);
  1680. #endif
  1681. }
  1682. template<typename dst_t>
  1683. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1684. const int i = blockIdx.x;
  1685. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  1686. const int tid = threadIdx.x;
  1687. #if QK_K == 256
  1688. const int il = tid/8; // 0...3
  1689. const int ib = tid%8; // 0...7
  1690. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1691. const uint16_t * q2 = x[i].qs + 4*ib;
  1692. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  1693. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1694. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  1695. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1696. #else
  1697. assert(false);
  1698. #endif
  1699. }
  1700. template<typename dst_t>
  1701. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1702. const int i = blockIdx.x;
  1703. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  1704. const int tid = threadIdx.x;
  1705. #if QK_K == 256
  1706. const int il = tid/8; // 0...3
  1707. const int ib = tid%8; // 0...7
  1708. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1709. const uint8_t * q3 = x[i].qs + 8*ib;
  1710. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  1711. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  1712. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  1713. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  1714. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  1715. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1716. for (int j = 0; j < 4; ++j) {
  1717. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1718. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1719. }
  1720. #else
  1721. assert(false);
  1722. #endif
  1723. }
  1724. template<typename dst_t>
  1725. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1726. const int i = blockIdx.x;
  1727. const block_iq1_s * x = (const block_iq1_s *) vx;
  1728. const int tid = threadIdx.x;
  1729. #if QK_K == 256
  1730. const int il = tid/8; // 0...3
  1731. const int ib = tid%8; // 0...7
  1732. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1733. const int i8 = 4*ib+il;
  1734. uint8_t h = x[i].scales[i8/2] >> 4*(i8%2);
  1735. const int8_t * grid = (const int8_t *)(iq1s_grid + (x[i].qs[i8] | ((h & 8) << 5)));
  1736. const float d = (float)x[i].d * (2*(h & 7) + 1);
  1737. for (int j = 0; j < 8; ++j) y[j] = d * grid[j];
  1738. #else
  1739. assert(false);
  1740. #endif
  1741. }
  1742. static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  1743. template<typename dst_t>
  1744. static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1745. const int i = blockIdx.x;
  1746. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  1747. const int tid = threadIdx.x;
  1748. const int il = tid/8; // 0...3
  1749. const int ib = tid%8; // 0...7
  1750. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  1751. const uint8_t * q4 = x[ib].qs + 4*il;
  1752. const float d = (float)x[ib].d;
  1753. for (int j = 0; j < 4; ++j) {
  1754. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  1755. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  1756. }
  1757. }
  1758. 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) {
  1759. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1760. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1761. if (row > nrows) return;
  1762. const int num_blocks_per_row = ncols / QK_K;
  1763. const int ib0 = row*num_blocks_per_row;
  1764. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1765. float tmp = 0; // partial sum for thread in warp
  1766. #if QK_K == 256
  1767. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1768. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1769. const int step = 16/K_QUANTS_PER_ITERATION;
  1770. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1771. const int in = tid - step*im; // 0...15 or 0...7
  1772. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1773. const int q_offset = 32*im + l0;
  1774. const int s_offset = 8*im;
  1775. const int y_offset = 128*im + l0;
  1776. uint32_t aux[4];
  1777. const uint8_t * d = (const uint8_t *)aux;
  1778. const uint8_t * m = (const uint8_t *)(aux + 2);
  1779. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1780. const float * y = yy + i * QK_K + y_offset;
  1781. const uint8_t * q = x[i].qs + q_offset;
  1782. const float dall = __low2half(x[i].dm);
  1783. const float dmin = __high2half(x[i].dm);
  1784. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1785. aux[0] = a[0] & 0x0f0f0f0f;
  1786. aux[1] = a[1] & 0x0f0f0f0f;
  1787. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1788. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1789. float sum1 = 0, sum2 = 0;
  1790. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1791. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1792. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1793. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1794. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1795. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1796. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1797. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1798. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1799. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1800. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1801. }
  1802. tmp += dall * sum1 - dmin * sum2;
  1803. }
  1804. #else
  1805. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1806. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1807. const int offset = tid * K_QUANTS_PER_ITERATION;
  1808. uint32_t uaux[2];
  1809. const uint8_t * d = (const uint8_t *)uaux;
  1810. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1811. const float * y = yy + i * QK_K + offset;
  1812. const uint8_t * q = x[i].qs + offset;
  1813. const uint32_t * s = (const uint32_t *)x[i].scales;
  1814. uaux[0] = s[0] & 0x0f0f0f0f;
  1815. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1816. const float2 dall = __half22float2(x[i].dm);
  1817. float sum1 = 0, sum2 = 0;
  1818. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1819. const uint8_t ql = q[l];
  1820. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1821. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1822. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1823. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1824. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1825. }
  1826. tmp += dall.x * sum1 - dall.y * sum2;
  1827. }
  1828. #endif
  1829. // sum up partial sums and write back result
  1830. #pragma unroll
  1831. for (int mask = 16; mask > 0; mask >>= 1) {
  1832. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1833. }
  1834. if (threadIdx.x == 0) {
  1835. dst[row] = tmp;
  1836. }
  1837. }
  1838. 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) {
  1839. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1840. if (row > nrows) return;
  1841. const int num_blocks_per_row = ncols / QK_K;
  1842. const int ib0 = row*num_blocks_per_row;
  1843. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1844. float tmp = 0; // partial sum for thread in warp
  1845. #if QK_K == 256
  1846. const uint16_t kmask1 = 0x0303;
  1847. const uint16_t kmask2 = 0x0f0f;
  1848. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1849. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1850. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1851. const int step = 16/K_QUANTS_PER_ITERATION;
  1852. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1853. const int in = tid - step*im; // 0....15 or 0...7
  1854. const uint8_t m = 1 << (4*im);
  1855. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1856. const int q_offset = 32*im + l0;
  1857. const int y_offset = 128*im + l0;
  1858. uint16_t utmp[4];
  1859. const int8_t * s = (const int8_t *)utmp;
  1860. const uint16_t s_shift = 4*im;
  1861. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1862. const float * y = yy + i * QK_K + y_offset;
  1863. const uint8_t * q = x[i].qs + q_offset;
  1864. const uint8_t * h = x[i].hmask + l0;
  1865. const uint16_t * a = (const uint16_t *)x[i].scales;
  1866. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1867. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1868. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1869. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1870. const float d = x[i].d;
  1871. float sum = 0;
  1872. for (int l = 0; l < n; ++l) {
  1873. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1874. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1875. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1876. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1877. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1878. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1879. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1880. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1881. }
  1882. tmp += d * sum;
  1883. }
  1884. #else
  1885. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1886. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1887. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1888. const int in = offset/8; // 0 or 1
  1889. const int im = offset%8; // 0...7
  1890. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1891. const float * y = yy + i * QK_K + offset;
  1892. const uint8_t * q = x[i].qs + offset;
  1893. const uint8_t * s = x[i].scales;
  1894. const float dall = (float)x[i].d;
  1895. float sum = 0;
  1896. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1897. const uint8_t hl = x[i].hmask[im+l] >> in;
  1898. const uint8_t ql = q[l];
  1899. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1900. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1901. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1902. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1903. }
  1904. tmp += sum;
  1905. }
  1906. #endif
  1907. // sum up partial sums and write back result
  1908. #pragma unroll
  1909. for (int mask = 16; mask > 0; mask >>= 1) {
  1910. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1911. }
  1912. if (threadIdx.x == 0) {
  1913. dst[row] = tmp;
  1914. }
  1915. }
  1916. 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) {
  1917. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1918. if (row > nrows) return;
  1919. const int num_blocks_per_row = ncols / QK_K;
  1920. const int ib0 = row*num_blocks_per_row;
  1921. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1922. #if QK_K == 256
  1923. const uint16_t kmask1 = 0x3f3f;
  1924. const uint16_t kmask2 = 0x0f0f;
  1925. const uint16_t kmask3 = 0xc0c0;
  1926. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1927. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1928. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1929. const int il = tid/step; // 0...3
  1930. const int ir = tid - step*il; // 0...7 or 0...3
  1931. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1932. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1933. const int in = il%2;
  1934. const int l0 = n*(2*ir + in);
  1935. const int q_offset = 32*im + l0;
  1936. const int y_offset = 64*im + l0;
  1937. uint16_t aux[4];
  1938. const uint8_t * sc = (const uint8_t *)aux;
  1939. #if K_QUANTS_PER_ITERATION == 2
  1940. uint32_t q32[4];
  1941. const uint8_t * q4 = (const uint8_t *)q32;
  1942. #else
  1943. uint16_t q16[4];
  1944. const uint8_t * q4 = (const uint8_t *)q16;
  1945. #endif
  1946. float tmp = 0; // partial sum for thread in warp
  1947. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1948. const float * y1 = yy + i*QK_K + y_offset;
  1949. const float * y2 = y1 + 128;
  1950. const float dall = __low2half(x[i].dm);
  1951. const float dmin = __high2half(x[i].dm);
  1952. const uint16_t * a = (const uint16_t *)x[i].scales;
  1953. aux[0] = a[im+0] & kmask1;
  1954. aux[1] = a[im+2] & kmask1;
  1955. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1956. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1957. #if K_QUANTS_PER_ITERATION == 2
  1958. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1959. const uint32_t * q2 = q1 + 16;
  1960. q32[0] = q1[0] & 0x0f0f0f0f;
  1961. q32[1] = q1[0] & 0xf0f0f0f0;
  1962. q32[2] = q2[0] & 0x0f0f0f0f;
  1963. q32[3] = q2[0] & 0xf0f0f0f0;
  1964. float4 s = {0.f, 0.f, 0.f, 0.f};
  1965. float smin = 0;
  1966. for (int l = 0; l < 4; ++l) {
  1967. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1968. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1969. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1970. }
  1971. 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;
  1972. #else
  1973. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1974. const uint16_t * q2 = q1 + 32;
  1975. q16[0] = q1[0] & 0x0f0f;
  1976. q16[1] = q1[0] & 0xf0f0;
  1977. q16[2] = q2[0] & 0x0f0f;
  1978. q16[3] = q2[0] & 0xf0f0;
  1979. float4 s = {0.f, 0.f, 0.f, 0.f};
  1980. float smin = 0;
  1981. for (int l = 0; l < 2; ++l) {
  1982. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1983. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1984. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1985. }
  1986. 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;
  1987. #endif
  1988. }
  1989. #else
  1990. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1991. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1992. const int step = tid * K_QUANTS_PER_ITERATION;
  1993. uint16_t aux16[2];
  1994. const uint8_t * s = (const uint8_t *)aux16;
  1995. float tmp = 0;
  1996. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1997. const uint8_t * q = x[i].qs + step;
  1998. const float * y = yy + i*QK_K + step;
  1999. const uint16_t * a = (const uint16_t *)x[i].scales;
  2000. aux16[0] = a[0] & 0x0f0f;
  2001. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2002. const float d = (float)x[i].dm[0];
  2003. const float m = (float)x[i].dm[1];
  2004. float sum = 0.f;
  2005. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2006. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  2007. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  2008. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  2009. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  2010. }
  2011. tmp += sum;
  2012. }
  2013. #endif
  2014. // sum up partial sums and write back result
  2015. #pragma unroll
  2016. for (int mask = 16; mask > 0; mask >>= 1) {
  2017. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2018. }
  2019. if (tid == 0) {
  2020. dst[row] = tmp;
  2021. }
  2022. }
  2023. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  2024. const int row = blockIdx.x;
  2025. const int num_blocks_per_row = ncols / QK_K;
  2026. const int ib0 = row*num_blocks_per_row;
  2027. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  2028. float tmp = 0; // partial sum for thread in warp
  2029. #if QK_K == 256
  2030. const uint16_t kmask1 = 0x3f3f;
  2031. const uint16_t kmask2 = 0x0f0f;
  2032. const uint16_t kmask3 = 0xc0c0;
  2033. const int tid = threadIdx.x/2; // 0...15
  2034. const int ix = threadIdx.x%2;
  2035. const int il = tid/4; // 0...3
  2036. const int ir = tid - 4*il;// 0...3
  2037. const int n = 2;
  2038. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2039. const int in = il%2;
  2040. const int l0 = n*(2*ir + in);
  2041. const int q_offset = 32*im + l0;
  2042. const int y_offset = 64*im + l0;
  2043. const uint8_t hm1 = 1 << (2*im);
  2044. const uint8_t hm2 = hm1 << 4;
  2045. uint16_t aux[4];
  2046. const uint8_t * sc = (const uint8_t *)aux;
  2047. uint16_t q16[8];
  2048. const uint8_t * q4 = (const uint8_t *)q16;
  2049. for (int i = ix; i < num_blocks_per_row; i += 2) {
  2050. const uint8_t * ql1 = x[i].qs + q_offset;
  2051. const uint8_t * qh = x[i].qh + l0;
  2052. const float * y1 = yy + i*QK_K + y_offset;
  2053. const float * y2 = y1 + 128;
  2054. const float dall = __low2half(x[i].dm);
  2055. const float dmin = __high2half(x[i].dm);
  2056. const uint16_t * a = (const uint16_t *)x[i].scales;
  2057. aux[0] = a[im+0] & kmask1;
  2058. aux[1] = a[im+2] & kmask1;
  2059. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2060. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2061. float4 sum = {0.f, 0.f, 0.f, 0.f};
  2062. float smin = 0;
  2063. const uint16_t * q1 = (const uint16_t *)ql1;
  2064. const uint16_t * q2 = q1 + 32;
  2065. q16[0] = q1[0] & 0x0f0f;
  2066. q16[1] = q1[8] & 0x0f0f;
  2067. q16[2] = (q1[0] >> 4) & 0x0f0f;
  2068. q16[3] = (q1[8] >> 4) & 0x0f0f;
  2069. q16[4] = q2[0] & 0x0f0f;
  2070. q16[5] = q2[8] & 0x0f0f;
  2071. q16[6] = (q2[0] >> 4) & 0x0f0f;
  2072. q16[7] = (q2[8] >> 4) & 0x0f0f;
  2073. for (int l = 0; l < n; ++l) {
  2074. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  2075. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  2076. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  2077. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  2078. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  2079. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  2080. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  2081. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  2082. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  2083. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  2084. }
  2085. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  2086. }
  2087. #else
  2088. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2089. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2090. const int step = tid * K_QUANTS_PER_ITERATION;
  2091. const int im = step/8;
  2092. const int in = step%8;
  2093. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2094. const uint8_t * q = x[i].qs + step;
  2095. const int8_t * s = x[i].scales;
  2096. const float * y = yy + i*QK_K + step;
  2097. const float d = x[i].d;
  2098. float sum = 0.f;
  2099. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2100. const uint8_t h = x[i].qh[in+j] >> im;
  2101. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  2102. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  2103. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  2104. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  2105. }
  2106. tmp += sum;
  2107. }
  2108. #endif
  2109. // sum up partial sums and write back result
  2110. #pragma unroll
  2111. for (int mask = 16; mask > 0; mask >>= 1) {
  2112. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2113. }
  2114. if (threadIdx.x == 0) {
  2115. dst[row] = tmp;
  2116. }
  2117. }
  2118. 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) {
  2119. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2120. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2121. if (row > nrows) return;
  2122. const int num_blocks_per_row = ncols / QK_K;
  2123. const int ib0 = row*num_blocks_per_row;
  2124. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  2125. #if QK_K == 256
  2126. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2127. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  2128. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  2129. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2130. const int in = tid - step*im; // 0...15 or 0...7
  2131. #if K_QUANTS_PER_ITERATION == 1
  2132. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  2133. const int is = 0;
  2134. #else
  2135. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  2136. const int is = in / 4;
  2137. #endif
  2138. const int ql_offset = 64*im + l0;
  2139. const int qh_offset = 32*im + l0;
  2140. const int s_offset = 8*im + is;
  2141. const int y_offset = 128*im + l0;
  2142. float tmp = 0; // partial sum for thread in warp
  2143. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2144. const float * y = yy + i * QK_K + y_offset;
  2145. const uint8_t * ql = x[i].ql + ql_offset;
  2146. const uint8_t * qh = x[i].qh + qh_offset;
  2147. const int8_t * s = x[i].scales + s_offset;
  2148. const float d = x[i].d;
  2149. #if K_QUANTS_PER_ITERATION == 1
  2150. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  2151. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  2152. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  2153. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  2154. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  2155. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  2156. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  2157. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  2158. tmp += sum;
  2159. #else
  2160. float sum = 0;
  2161. for (int l = 0; l < 4; ++l) {
  2162. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  2163. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  2164. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  2165. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  2166. }
  2167. tmp += sum;
  2168. #endif
  2169. }
  2170. #else
  2171. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  2172. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  2173. const int step = tid * K_QUANTS_PER_ITERATION;
  2174. float tmp = 0; // partial sum for thread in warp
  2175. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2176. const float * y = yy + i * QK_K + step;
  2177. const uint8_t * ql = x[i].ql + step;
  2178. const uint8_t * qh = x[i].qh + step;
  2179. const int8_t * s = x[i].scales;
  2180. const float d = x[i+0].d;
  2181. float sum = 0;
  2182. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2183. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  2184. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  2185. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  2186. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  2187. }
  2188. tmp += sum;
  2189. }
  2190. #endif
  2191. // sum up partial sums and write back result
  2192. #pragma unroll
  2193. for (int mask = 16; mask > 0; mask >>= 1) {
  2194. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2195. }
  2196. if (tid == 0) {
  2197. dst[row] = tmp;
  2198. }
  2199. }
  2200. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  2201. const half * x = (const half *) vx;
  2202. // automatic half -> float type cast if dfloat == float
  2203. v.x = x[ib + iqs + 0];
  2204. v.y = x[ib + iqs + 1];
  2205. }
  2206. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  2207. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  2208. if (ix >= kx_padded) {
  2209. return;
  2210. }
  2211. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  2212. const int i_padded = iy*kx_padded + ix;
  2213. block_q8_1 * y = (block_q8_1 *) vy;
  2214. const int ib = i_padded / QK8_1; // block index
  2215. const int iqs = i_padded % QK8_1; // quant index
  2216. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  2217. float amax = fabsf(xi);
  2218. float sum = xi;
  2219. #pragma unroll
  2220. for (int mask = 16; mask > 0; mask >>= 1) {
  2221. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  2222. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  2223. }
  2224. const float d = amax / 127;
  2225. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  2226. y[ib].qs[iqs] = q;
  2227. if (iqs > 0) {
  2228. return;
  2229. }
  2230. reinterpret_cast<half&>(y[ib].ds.x) = d;
  2231. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  2232. }
  2233. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2234. static __global__ void k_get_rows(
  2235. const void * src0, const int32_t * src1, dst_t * dst,
  2236. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2237. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2238. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2239. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2240. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2241. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  2242. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2243. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2244. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2245. if (i00 >= ne00) {
  2246. return;
  2247. }
  2248. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2249. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2250. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  2251. const int ib = i00/qk; // block index
  2252. const int iqs = (i00%qk)/qr; // quant index
  2253. const int iybs = i00 - i00%qk; // dst block start index
  2254. const int y_offset = qr == 1 ? 1 : qk/2;
  2255. // dequantize
  2256. dfloat2 v;
  2257. dequantize_kernel(src0_row, ib, iqs, v);
  2258. dst_row[iybs + iqs + 0] = v.x;
  2259. dst_row[iybs + iqs + y_offset] = v.y;
  2260. }
  2261. template<typename src0_t, typename dst_t>
  2262. static __global__ void k_get_rows_float(
  2263. const src0_t * src0, const int32_t * src1, dst_t * dst,
  2264. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2265. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2266. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2267. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2268. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2269. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2270. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2271. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2272. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2273. if (i00 >= ne00) {
  2274. return;
  2275. }
  2276. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2277. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2278. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2279. dst_row[i00] = src0_row[i00];
  2280. }
  2281. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2282. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2283. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2284. if (i >= k) {
  2285. return;
  2286. }
  2287. const int ib = i/qk; // block index
  2288. const int iqs = (i%qk)/qr; // quant index
  2289. const int iybs = i - i%qk; // y block start index
  2290. const int y_offset = qr == 1 ? 1 : qk/2;
  2291. // dequantize
  2292. dfloat2 v;
  2293. dequantize_kernel(vx, ib, iqs, v);
  2294. y[iybs + iqs + 0] = v.x;
  2295. y[iybs + iqs + y_offset] = v.y;
  2296. }
  2297. template <typename src_t, typename dst_t>
  2298. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2299. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2300. if (i >= k) {
  2301. return;
  2302. }
  2303. const src_t * x = (src_t *) vx;
  2304. y[i] = x[i];
  2305. }
  2306. template <bool need_check>
  2307. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2308. #if __CUDA_ARCH__ >= CC_PASCAL
  2309. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2310. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2311. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2312. half2 * y2 = (half2 *) (y + i0);
  2313. __shared__ int vals[nint];
  2314. #pragma unroll
  2315. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2316. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2317. break;
  2318. }
  2319. const int ix = ix0 + threadIdx.x;
  2320. vals[ix] = x0[ix];
  2321. }
  2322. #pragma unroll
  2323. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2324. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2325. return;
  2326. }
  2327. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2328. const half d = *b0;
  2329. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2330. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2331. }
  2332. #else
  2333. (void) vx; (void) y; (void) k;
  2334. NO_DEVICE_CODE;
  2335. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2336. }
  2337. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2338. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2339. #define VDR_Q4_0_Q8_1_MMVQ 2
  2340. #define VDR_Q4_0_Q8_1_MMQ 4
  2341. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2342. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2343. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2344. int sumi = 0;
  2345. #pragma unroll
  2346. for (int i = 0; i < vdr; ++i) {
  2347. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2348. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2349. // SIMD dot product of quantized values
  2350. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2351. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2352. }
  2353. const float2 ds8f = __half22float2(ds8);
  2354. // second part effectively subtracts 8 from each quant value
  2355. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2356. #else
  2357. NO_DEVICE_CODE;
  2358. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2359. }
  2360. #define VDR_Q4_1_Q8_1_MMVQ 2
  2361. #define VDR_Q4_1_Q8_1_MMQ 4
  2362. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2363. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2364. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2365. int sumi = 0;
  2366. #pragma unroll
  2367. for (int i = 0; i < vdr; ++i) {
  2368. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2369. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2370. // SIMD dot product of quantized values
  2371. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2372. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2373. }
  2374. #ifdef GGML_CUDA_F16
  2375. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2376. const float d4d8 = tmp.x;
  2377. const float m4s8 = tmp.y;
  2378. #else
  2379. const float2 dm4f = __half22float2(dm4);
  2380. const float2 ds8f = __half22float2(ds8);
  2381. const float d4d8 = dm4f.x * ds8f.x;
  2382. const float m4s8 = dm4f.y * ds8f.y;
  2383. #endif // GGML_CUDA_F16
  2384. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2385. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2386. #else
  2387. NO_DEVICE_CODE;
  2388. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2389. }
  2390. #define VDR_Q5_0_Q8_1_MMVQ 2
  2391. #define VDR_Q5_0_Q8_1_MMQ 4
  2392. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2393. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2394. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2395. int sumi = 0;
  2396. #pragma unroll
  2397. for (int i = 0; i < vdr; ++i) {
  2398. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2399. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2400. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2401. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2402. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2403. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2404. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2405. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2406. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2407. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2408. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2409. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2410. }
  2411. const float2 ds8f = __half22float2(ds8);
  2412. // second part effectively subtracts 16 from each quant value
  2413. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2414. #else
  2415. NO_DEVICE_CODE;
  2416. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2417. }
  2418. #define VDR_Q5_1_Q8_1_MMVQ 2
  2419. #define VDR_Q5_1_Q8_1_MMQ 4
  2420. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2421. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2422. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2423. int sumi = 0;
  2424. #pragma unroll
  2425. for (int i = 0; i < vdr; ++i) {
  2426. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2427. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2428. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2429. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2430. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2431. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2432. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2433. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2434. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2435. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2436. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2437. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2438. }
  2439. #ifdef GGML_CUDA_F16
  2440. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2441. const float d5d8 = tmp.x;
  2442. const float m5s8 = tmp.y;
  2443. #else
  2444. const float2 dm5f = __half22float2(dm5);
  2445. const float2 ds8f = __half22float2(ds8);
  2446. const float d5d8 = dm5f.x * ds8f.x;
  2447. const float m5s8 = dm5f.y * ds8f.y;
  2448. #endif // GGML_CUDA_F16
  2449. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2450. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2451. #else
  2452. NO_DEVICE_CODE;
  2453. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2454. }
  2455. #define VDR_Q8_0_Q8_1_MMVQ 2
  2456. #define VDR_Q8_0_Q8_1_MMQ 8
  2457. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2458. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2459. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2460. int sumi = 0;
  2461. #pragma unroll
  2462. for (int i = 0; i < vdr; ++i) {
  2463. // SIMD dot product of quantized values
  2464. sumi = __dp4a(v[i], u[i], sumi);
  2465. }
  2466. return d8_0*d8_1 * sumi;
  2467. #else
  2468. NO_DEVICE_CODE;
  2469. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2470. }
  2471. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2472. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2473. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2474. int sumi = 0;
  2475. #pragma unroll
  2476. for (int i = 0; i < vdr; ++i) {
  2477. // SIMD dot product of quantized values
  2478. sumi = __dp4a(v[i], u[i], sumi);
  2479. }
  2480. #ifdef GGML_CUDA_F16
  2481. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2482. const float d8d8 = tmp.x;
  2483. const float m8s8 = tmp.y;
  2484. #else
  2485. const float2 dm8f = __half22float2(dm8);
  2486. const float2 ds8f = __half22float2(ds8);
  2487. const float d8d8 = dm8f.x * ds8f.x;
  2488. const float m8s8 = dm8f.y * ds8f.y;
  2489. #endif // GGML_CUDA_F16
  2490. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2491. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2492. #else
  2493. NO_DEVICE_CODE;
  2494. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2495. }
  2496. #define VDR_Q2_K_Q8_1_MMVQ 1
  2497. #define VDR_Q2_K_Q8_1_MMQ 2
  2498. // contiguous v/x values
  2499. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2500. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2501. const half2 & dm2, const float * __restrict__ d8) {
  2502. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2503. float sumf_d = 0.0f;
  2504. float sumf_m = 0.0f;
  2505. #pragma unroll
  2506. for (int i = 0; i < QR2_K; ++i) {
  2507. const int sc = scales[2*i];
  2508. const int vi = (v >> (2*i)) & 0x03030303;
  2509. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2510. // fill int with 4x m
  2511. int m = sc >> 4;
  2512. m |= m << 8;
  2513. m |= m << 16;
  2514. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2515. }
  2516. const float2 dm2f = __half22float2(dm2);
  2517. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2518. #else
  2519. NO_DEVICE_CODE;
  2520. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2521. }
  2522. // contiguous u/y values
  2523. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2524. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2525. const half2 & dm2, const float & d8) {
  2526. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2527. int sumi_d = 0;
  2528. int sumi_m = 0;
  2529. #pragma unroll
  2530. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2531. int sumi_d_sc = 0;
  2532. const int sc = scales[i0 / (QI8_1/2)];
  2533. // fill int with 4x m
  2534. int m = sc >> 4;
  2535. m |= m << 8;
  2536. m |= m << 16;
  2537. #pragma unroll
  2538. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2539. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2540. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2541. }
  2542. sumi_d += sumi_d_sc * (sc & 0xF);
  2543. }
  2544. const float2 dm2f = __half22float2(dm2);
  2545. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2546. #else
  2547. NO_DEVICE_CODE;
  2548. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2549. }
  2550. #define VDR_Q3_K_Q8_1_MMVQ 1
  2551. #define VDR_Q3_K_Q8_1_MMQ 2
  2552. // contiguous v/x values
  2553. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2554. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2555. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2556. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2557. float sumf = 0.0f;
  2558. #pragma unroll
  2559. for (int i = 0; i < QR3_K; ++i) {
  2560. const int isc = scale_offset + 2*i;
  2561. const int isc_low = isc % (QK_K/32);
  2562. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2563. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2564. const int isc_high = isc % (QK_K/64);
  2565. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2566. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2567. const int sc = (sc_low | sc_high) - 32;
  2568. const int vil = (vl >> (2*i)) & 0x03030303;
  2569. const int vih = ((vh >> i) << 2) & 0x04040404;
  2570. const int vi = __vsubss4(vil, vih);
  2571. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2572. }
  2573. return d3 * sumf;
  2574. #else
  2575. NO_DEVICE_CODE;
  2576. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2577. }
  2578. // contiguous u/y values
  2579. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2580. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2581. const float & d3, const float & d8) {
  2582. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2583. int sumi = 0;
  2584. #pragma unroll
  2585. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2586. int sumi_sc = 0;
  2587. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2588. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2589. }
  2590. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2591. }
  2592. return d3*d8 * sumi;
  2593. #else
  2594. NO_DEVICE_CODE;
  2595. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2596. }
  2597. #define VDR_Q4_K_Q8_1_MMVQ 2
  2598. #define VDR_Q4_K_Q8_1_MMQ 8
  2599. // contiguous v/x values
  2600. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2601. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2602. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2603. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2604. float sumf_d = 0.0f;
  2605. float sumf_m = 0.0f;
  2606. #pragma unroll
  2607. for (int i = 0; i < QR4_K; ++i) {
  2608. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2609. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2610. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2611. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2612. sumf_d += d8[i] * (dot1 * sc[i]);
  2613. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2614. }
  2615. const float2 dm4f = __half22float2(dm4);
  2616. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2617. #else
  2618. NO_DEVICE_CODE;
  2619. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2620. }
  2621. // contiguous u/y values
  2622. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2623. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2624. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2625. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2626. float sumf_d = 0.0f;
  2627. float sumf_m = 0.0f;
  2628. #pragma unroll
  2629. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2630. int sumi_d = 0;
  2631. #pragma unroll
  2632. for (int j = 0; j < QI8_1; ++j) {
  2633. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2634. }
  2635. const float2 ds8f = __half22float2(ds8[i]);
  2636. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2637. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2638. }
  2639. const float2 dm4f = __half22float2(dm4);
  2640. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2641. #else
  2642. NO_DEVICE_CODE;
  2643. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2644. }
  2645. #define VDR_Q5_K_Q8_1_MMVQ 2
  2646. #define VDR_Q5_K_Q8_1_MMQ 8
  2647. // contiguous v/x values
  2648. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2649. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2650. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2651. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2652. float sumf_d = 0.0f;
  2653. float sumf_m = 0.0f;
  2654. #pragma unroll
  2655. for (int i = 0; i < QR5_K; ++i) {
  2656. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2657. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2658. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2659. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2660. const int v0i = vl0i | vh0i;
  2661. const int v1i = vl1i | vh1i;
  2662. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2663. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2664. sumf_d += d8[i] * (dot1 * sc[i]);
  2665. sumf_m += d8[i] * (dot2 * m[i]);
  2666. }
  2667. const float2 dm5f = __half22float2(dm5);
  2668. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2669. #else
  2670. NO_DEVICE_CODE;
  2671. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2672. }
  2673. // contiguous u/y values
  2674. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2675. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2676. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2677. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2678. float sumf_d = 0.0f;
  2679. float sumf_m = 0.0f;
  2680. #pragma unroll
  2681. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2682. int sumi_d = 0;
  2683. #pragma unroll
  2684. for (int j = 0; j < QI8_1; ++j) {
  2685. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2686. }
  2687. const float2 ds8f = __half22float2(ds8[i]);
  2688. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2689. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2690. }
  2691. const float2 dm4f = __half22float2(dm4);
  2692. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2693. #else
  2694. NO_DEVICE_CODE;
  2695. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2696. }
  2697. #define VDR_Q6_K_Q8_1_MMVQ 1
  2698. #define VDR_Q6_K_Q8_1_MMQ 8
  2699. // contiguous v/x values
  2700. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2701. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2702. const float & d, const float * __restrict__ d8) {
  2703. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2704. float sumf = 0.0f;
  2705. #pragma unroll
  2706. for (int i = 0; i < QR6_K; ++i) {
  2707. const int sc = scales[4*i];
  2708. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2709. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2710. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2711. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2712. }
  2713. return d*sumf;
  2714. #else
  2715. NO_DEVICE_CODE;
  2716. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2717. }
  2718. // contiguous u/y values
  2719. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2720. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2721. const float & d6, const float * __restrict__ d8) {
  2722. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2723. float sumf_d = 0.0f;
  2724. #pragma unroll
  2725. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2726. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2727. #pragma unroll
  2728. for (int i = i0; i < i0 + 2; ++i) {
  2729. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2730. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2731. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2732. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2733. }
  2734. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2735. }
  2736. return d6 * sumf_d;
  2737. #else
  2738. NO_DEVICE_CODE;
  2739. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2740. }
  2741. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2742. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2743. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2744. int v[VDR_Q4_0_Q8_1_MMVQ];
  2745. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2746. #pragma unroll
  2747. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2748. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2749. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2750. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2751. }
  2752. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2753. }
  2754. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2755. (void)x_qh; (void)x_sc;
  2756. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2757. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2758. *x_ql = tile_x_qs;
  2759. *x_dm = (half2 *) tile_x_d;
  2760. }
  2761. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2762. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2763. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2764. (void)x_qh; (void)x_sc;
  2765. GGML_CUDA_ASSUME(i_offset >= 0);
  2766. GGML_CUDA_ASSUME(i_offset < nwarps);
  2767. GGML_CUDA_ASSUME(k >= 0);
  2768. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2769. const int kbx = k / QI4_0;
  2770. const int kqsx = k % QI4_0;
  2771. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2772. float * x_dmf = (float *) x_dm;
  2773. #pragma unroll
  2774. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2775. int i = i0 + i_offset;
  2776. if (need_check) {
  2777. i = min(i, i_max);
  2778. }
  2779. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2780. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2781. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2782. }
  2783. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2784. const int kbxd = k % blocks_per_tile_x_row;
  2785. #pragma unroll
  2786. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2787. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2788. if (need_check) {
  2789. i = min(i, i_max);
  2790. }
  2791. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2792. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2793. }
  2794. }
  2795. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2796. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2797. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2798. (void)x_qh; (void)x_sc;
  2799. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2800. const float * x_dmf = (const float *) x_dm;
  2801. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2802. #pragma unroll
  2803. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2804. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2805. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2806. }
  2807. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2808. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2809. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2810. }
  2811. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2812. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2813. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2814. int v[VDR_Q4_1_Q8_1_MMVQ];
  2815. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2816. #pragma unroll
  2817. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2818. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2819. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2820. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2821. }
  2822. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2823. }
  2824. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2825. (void)x_qh; (void)x_sc;
  2826. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2827. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2828. *x_ql = tile_x_qs;
  2829. *x_dm = tile_x_dm;
  2830. }
  2831. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2832. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2833. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2834. (void)x_qh; (void)x_sc;
  2835. GGML_CUDA_ASSUME(i_offset >= 0);
  2836. GGML_CUDA_ASSUME(i_offset < nwarps);
  2837. GGML_CUDA_ASSUME(k >= 0);
  2838. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2839. const int kbx = k / QI4_1;
  2840. const int kqsx = k % QI4_1;
  2841. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2842. #pragma unroll
  2843. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2844. int i = i0 + i_offset;
  2845. if (need_check) {
  2846. i = min(i, i_max);
  2847. }
  2848. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2849. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2850. }
  2851. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2852. const int kbxd = k % blocks_per_tile_x_row;
  2853. #pragma unroll
  2854. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2855. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2856. if (need_check) {
  2857. i = min(i, i_max);
  2858. }
  2859. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2860. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2861. }
  2862. }
  2863. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2864. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2865. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2866. (void)x_qh; (void)x_sc;
  2867. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2868. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2869. #pragma unroll
  2870. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2871. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2872. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2873. }
  2874. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2875. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2876. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2877. }
  2878. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2879. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2880. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2881. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2882. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2883. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2884. #pragma unroll
  2885. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2886. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2887. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2888. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2889. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2890. }
  2891. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2892. }
  2893. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2894. (void)x_qh; (void)x_sc;
  2895. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2896. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2897. *x_ql = tile_x_ql;
  2898. *x_dm = (half2 *) tile_x_d;
  2899. }
  2900. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2901. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2902. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2903. (void)x_qh; (void)x_sc;
  2904. GGML_CUDA_ASSUME(i_offset >= 0);
  2905. GGML_CUDA_ASSUME(i_offset < nwarps);
  2906. GGML_CUDA_ASSUME(k >= 0);
  2907. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2908. const int kbx = k / QI5_0;
  2909. const int kqsx = k % QI5_0;
  2910. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2911. #pragma unroll
  2912. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2913. int i = i0 + i_offset;
  2914. if (need_check) {
  2915. i = min(i, i_max);
  2916. }
  2917. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2918. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2919. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2920. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2921. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2922. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2923. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2924. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2925. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2926. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2927. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2928. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2929. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2930. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2931. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2932. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2933. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2934. }
  2935. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2936. const int kbxd = k % blocks_per_tile_x_row;
  2937. float * x_dmf = (float *) x_dm;
  2938. #pragma unroll
  2939. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2940. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2941. if (need_check) {
  2942. i = min(i, i_max);
  2943. }
  2944. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2945. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2946. }
  2947. }
  2948. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2949. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2950. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2951. (void)x_qh; (void)x_sc;
  2952. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2953. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2954. const float * x_dmf = (const float *) x_dm;
  2955. const float * y_df = (const float *) y_ds;
  2956. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2957. #pragma unroll
  2958. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2959. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2960. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2961. }
  2962. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2963. (&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)]);
  2964. }
  2965. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2966. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2967. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2968. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2969. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2970. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2971. #pragma unroll
  2972. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2973. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2974. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2975. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2976. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2977. }
  2978. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2979. }
  2980. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2981. (void)x_qh; (void)x_sc;
  2982. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2983. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2984. *x_ql = tile_x_ql;
  2985. *x_dm = tile_x_dm;
  2986. }
  2987. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2988. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2989. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2990. (void)x_qh; (void)x_sc;
  2991. GGML_CUDA_ASSUME(i_offset >= 0);
  2992. GGML_CUDA_ASSUME(i_offset < nwarps);
  2993. GGML_CUDA_ASSUME(k >= 0);
  2994. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2995. const int kbx = k / QI5_1;
  2996. const int kqsx = k % QI5_1;
  2997. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2998. #pragma unroll
  2999. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3000. int i = i0 + i_offset;
  3001. if (need_check) {
  3002. i = min(i, i_max);
  3003. }
  3004. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3005. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3006. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  3007. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3008. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3009. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3010. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3011. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3012. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3013. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3014. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3015. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3016. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3017. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3018. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3019. }
  3020. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  3021. const int kbxd = k % blocks_per_tile_x_row;
  3022. #pragma unroll
  3023. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  3024. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  3025. if (need_check) {
  3026. i = min(i, i_max);
  3027. }
  3028. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3029. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  3030. }
  3031. }
  3032. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  3033. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3034. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3035. (void)x_qh; (void)x_sc;
  3036. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3037. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  3038. int u[2*VDR_Q5_1_Q8_1_MMQ];
  3039. #pragma unroll
  3040. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  3041. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3042. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  3043. }
  3044. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  3045. (&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)]);
  3046. }
  3047. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  3048. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3049. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  3050. int v[VDR_Q8_0_Q8_1_MMVQ];
  3051. int u[VDR_Q8_0_Q8_1_MMVQ];
  3052. #pragma unroll
  3053. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  3054. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  3055. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3056. }
  3057. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  3058. }
  3059. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3060. (void)x_qh; (void)x_sc;
  3061. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3062. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  3063. *x_ql = tile_x_qs;
  3064. *x_dm = (half2 *) tile_x_d;
  3065. }
  3066. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  3067. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3068. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3069. (void)x_qh; (void)x_sc;
  3070. GGML_CUDA_ASSUME(i_offset >= 0);
  3071. GGML_CUDA_ASSUME(i_offset < nwarps);
  3072. GGML_CUDA_ASSUME(k >= 0);
  3073. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3074. const int kbx = k / QI8_0;
  3075. const int kqsx = k % QI8_0;
  3076. float * x_dmf = (float *) x_dm;
  3077. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  3078. #pragma unroll
  3079. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3080. int i = i0 + i_offset;
  3081. if (need_check) {
  3082. i = min(i, i_max);
  3083. }
  3084. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3085. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  3086. }
  3087. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  3088. const int kbxd = k % blocks_per_tile_x_row;
  3089. #pragma unroll
  3090. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  3091. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  3092. if (need_check) {
  3093. i = min(i, i_max);
  3094. }
  3095. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3096. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  3097. }
  3098. }
  3099. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  3100. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3101. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3102. (void)x_qh; (void)x_sc;
  3103. const float * x_dmf = (const float *) x_dm;
  3104. const float * y_df = (const float *) y_ds;
  3105. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  3106. (&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],
  3107. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  3108. }
  3109. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  3110. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3111. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  3112. const int bq8_offset = QR2_K * (iqs / QI8_1);
  3113. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3114. const uint8_t * scales = bq2_K->scales + scale_offset;
  3115. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  3116. int u[QR2_K];
  3117. float d8[QR2_K];
  3118. #pragma unroll
  3119. for (int i = 0; i < QR2_K; ++ i) {
  3120. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3121. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3122. }
  3123. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  3124. }
  3125. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3126. (void)x_qh;
  3127. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3128. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  3129. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3130. *x_ql = tile_x_ql;
  3131. *x_dm = tile_x_dm;
  3132. *x_sc = tile_x_sc;
  3133. }
  3134. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  3135. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3136. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3137. (void)x_qh;
  3138. GGML_CUDA_ASSUME(i_offset >= 0);
  3139. GGML_CUDA_ASSUME(i_offset < nwarps);
  3140. GGML_CUDA_ASSUME(k >= 0);
  3141. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3142. const int kbx = k / QI2_K;
  3143. const int kqsx = k % QI2_K;
  3144. const block_q2_K * bx0 = (const block_q2_K *) vx;
  3145. #pragma unroll
  3146. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3147. int i = i0 + i_offset;
  3148. if (need_check) {
  3149. i = min(i, i_max);
  3150. }
  3151. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  3152. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3153. }
  3154. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  3155. const int kbxd = k % blocks_per_tile_x_row;
  3156. #pragma unroll
  3157. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  3158. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  3159. if (need_check) {
  3160. i = min(i, i_max);
  3161. }
  3162. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3163. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  3164. }
  3165. #pragma unroll
  3166. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3167. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3168. if (need_check) {
  3169. i = min(i, i_max);
  3170. }
  3171. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  3172. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  3173. }
  3174. }
  3175. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  3176. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3177. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3178. (void)x_qh;
  3179. const int kbx = k / QI2_K;
  3180. const int ky = (k % QI2_K) * QR2_K;
  3181. const float * y_df = (const float *) y_ds;
  3182. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  3183. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  3184. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  3185. #pragma unroll
  3186. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  3187. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3188. }
  3189. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  3190. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  3191. 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]);
  3192. }
  3193. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  3194. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3195. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  3196. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  3197. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3198. const float d = bq3_K->d;
  3199. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  3200. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3201. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  3202. int u[QR3_K];
  3203. float d8[QR3_K];
  3204. #pragma unroll
  3205. for (int i = 0; i < QR3_K; ++i) {
  3206. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3207. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3208. }
  3209. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  3210. }
  3211. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3212. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3213. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  3214. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  3215. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3216. *x_ql = tile_x_ql;
  3217. *x_dm = tile_x_dm;
  3218. *x_qh = tile_x_qh;
  3219. *x_sc = tile_x_sc;
  3220. }
  3221. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  3222. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3223. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3224. GGML_CUDA_ASSUME(i_offset >= 0);
  3225. GGML_CUDA_ASSUME(i_offset < nwarps);
  3226. GGML_CUDA_ASSUME(k >= 0);
  3227. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3228. const int kbx = k / QI3_K;
  3229. const int kqsx = k % QI3_K;
  3230. const block_q3_K * bx0 = (const block_q3_K *) vx;
  3231. #pragma unroll
  3232. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3233. int i = i0 + i_offset;
  3234. if (need_check) {
  3235. i = min(i, i_max);
  3236. }
  3237. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  3238. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3239. }
  3240. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  3241. const int kbxd = k % blocks_per_tile_x_row;
  3242. float * x_dmf = (float *) x_dm;
  3243. #pragma unroll
  3244. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  3245. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  3246. if (need_check) {
  3247. i = min(i, i_max);
  3248. }
  3249. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3250. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  3251. }
  3252. #pragma unroll
  3253. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  3254. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  3255. if (need_check) {
  3256. i = min(i, i_max);
  3257. }
  3258. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  3259. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3260. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  3261. }
  3262. #pragma unroll
  3263. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3264. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3265. if (need_check) {
  3266. i = min(i, i_max);
  3267. }
  3268. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  3269. const int ksc = k % (QI3_K/4);
  3270. const int ksc_low = ksc % (QI3_K/8);
  3271. const int shift_low = 4 * (ksc / (QI3_K/8));
  3272. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3273. const int ksc_high = QI3_K/8;
  3274. const int shift_high = 2 * ksc;
  3275. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3276. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3277. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3278. }
  3279. }
  3280. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3281. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3282. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3283. const int kbx = k / QI3_K;
  3284. const int ky = (k % QI3_K) * QR3_K;
  3285. const float * x_dmf = (const float *) x_dm;
  3286. const float * y_df = (const float *) y_ds;
  3287. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3288. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3289. #pragma unroll
  3290. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3291. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3292. const int shift = 2 * ((ky % 32) / 8);
  3293. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3294. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3295. const int vlh = (vh << 2) & 0x04040404;
  3296. v[l] = __vsubss4(vll, vlh);
  3297. }
  3298. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3299. 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]);
  3300. }
  3301. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3302. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3303. #ifndef GGML_QKK_64
  3304. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3305. int v[2];
  3306. int u[2*QR4_K];
  3307. float d8[QR4_K];
  3308. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3309. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3310. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3311. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3312. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3313. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3314. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3315. v[0] = q4[0];
  3316. v[1] = q4[4];
  3317. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3318. uint16_t aux[2];
  3319. const int j = bq8_offset/2;
  3320. if (j < 2) {
  3321. aux[0] = scales[j+0] & 0x3f3f;
  3322. aux[1] = scales[j+2] & 0x3f3f;
  3323. } else {
  3324. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3325. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3326. }
  3327. const uint8_t * sc = (const uint8_t *)aux;
  3328. const uint8_t * m = sc + 2;
  3329. for (int i = 0; i < QR4_K; ++i) {
  3330. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3331. d8[i] = __low2half(bq8i->ds);
  3332. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3333. u[2*i+0] = q8[0];
  3334. u[2*i+1] = q8[4];
  3335. }
  3336. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3337. #else
  3338. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3339. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3340. float sumf_d = 0.0f;
  3341. float sumf_m = 0.0f;
  3342. uint16_t aux16[2];
  3343. const uint8_t * s = (const uint8_t *)aux16;
  3344. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3345. aux16[0] = a[0] & 0x0f0f;
  3346. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3347. const float dall = bq4_K->dm[0];
  3348. const float dmin = bq4_K->dm[1];
  3349. const float d8_1 = __low2float(bq8_1[0].ds);
  3350. const float d8_2 = __low2float(bq8_1[1].ds);
  3351. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3352. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3353. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3354. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3355. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3356. const int v1 = q4[0];
  3357. const int v2 = q4[4];
  3358. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3359. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3360. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3361. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3362. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3363. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3364. return dall * sumf_d - dmin * sumf_m;
  3365. #else
  3366. NO_DEVICE_CODE;
  3367. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3368. #endif
  3369. }
  3370. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3371. (void)x_qh;
  3372. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3373. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3374. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3375. *x_ql = tile_x_ql;
  3376. *x_dm = tile_x_dm;
  3377. *x_sc = tile_x_sc;
  3378. }
  3379. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3380. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3381. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3382. (void)x_qh;
  3383. GGML_CUDA_ASSUME(i_offset >= 0);
  3384. GGML_CUDA_ASSUME(i_offset < nwarps);
  3385. GGML_CUDA_ASSUME(k >= 0);
  3386. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3387. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3388. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3389. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3390. #pragma unroll
  3391. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3392. int i = i0 + i_offset;
  3393. if (need_check) {
  3394. i = min(i, i_max);
  3395. }
  3396. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3397. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3398. }
  3399. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3400. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3401. #pragma unroll
  3402. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3403. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3404. if (need_check) {
  3405. i = min(i, i_max);
  3406. }
  3407. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3408. #if QK_K == 256
  3409. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3410. #else
  3411. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3412. #endif
  3413. }
  3414. #pragma unroll
  3415. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3416. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3417. if (need_check) {
  3418. i = min(i, i_max);
  3419. }
  3420. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3421. const int * scales = (const int *) bxi->scales;
  3422. const int ksc = k % (WARP_SIZE/8);
  3423. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3424. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3425. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3426. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3427. }
  3428. }
  3429. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3430. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3431. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3432. (void)x_qh;
  3433. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3434. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3435. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3436. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3437. }
  3438. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3439. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3440. #ifndef GGML_QKK_64
  3441. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3442. int vl[2];
  3443. int vh[2];
  3444. int u[2*QR5_K];
  3445. float d8[QR5_K];
  3446. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3447. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3448. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3449. vl[0] = ql[0];
  3450. vl[1] = ql[4];
  3451. vh[0] = qh[0] >> bq8_offset;
  3452. vh[1] = qh[4] >> bq8_offset;
  3453. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3454. uint16_t aux[2];
  3455. const int j = bq8_offset/2;
  3456. if (j < 2) {
  3457. aux[0] = scales[j+0] & 0x3f3f;
  3458. aux[1] = scales[j+2] & 0x3f3f;
  3459. } else {
  3460. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3461. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3462. }
  3463. const uint8_t * sc = (const uint8_t *)aux;
  3464. const uint8_t * m = sc + 2;
  3465. #pragma unroll
  3466. for (int i = 0; i < QR5_K; ++i) {
  3467. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3468. d8[i] = __low2float(bq8i->ds);
  3469. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3470. u[2*i+0] = q8[0];
  3471. u[2*i+1] = q8[4];
  3472. }
  3473. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3474. #else
  3475. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3476. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3477. const int8_t * s = bq5_K->scales;
  3478. const float d = bq5_K->d;
  3479. const float d8_1 = __low2half(bq8_1[0].ds);
  3480. const float d8_2 = __low2half(bq8_1[1].ds);
  3481. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3482. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3483. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3484. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3485. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3486. const int vl1 = ql[0];
  3487. const int vl2 = ql[4];
  3488. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3489. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3490. const int in = step%8; // 0, 4, 0, 4
  3491. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3492. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3493. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3494. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3495. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3496. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3497. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3498. return d * sumf_d;
  3499. #else
  3500. NO_DEVICE_CODE;
  3501. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3502. #endif
  3503. }
  3504. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3505. (void)x_qh;
  3506. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3507. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3508. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3509. *x_ql = tile_x_ql;
  3510. *x_dm = tile_x_dm;
  3511. *x_sc = tile_x_sc;
  3512. }
  3513. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3514. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3515. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3516. (void)x_qh;
  3517. GGML_CUDA_ASSUME(i_offset >= 0);
  3518. GGML_CUDA_ASSUME(i_offset < nwarps);
  3519. GGML_CUDA_ASSUME(k >= 0);
  3520. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3521. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3522. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3523. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3524. #pragma unroll
  3525. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3526. int i = i0 + i_offset;
  3527. if (need_check) {
  3528. i = min(i, i_max);
  3529. }
  3530. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3531. const int ky = QR5_K*kqsx;
  3532. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3533. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3534. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3535. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3536. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3537. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3538. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3539. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3540. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3541. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3542. }
  3543. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3544. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3545. #pragma unroll
  3546. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3547. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3548. if (need_check) {
  3549. i = min(i, i_max);
  3550. }
  3551. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3552. #if QK_K == 256
  3553. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3554. #endif
  3555. }
  3556. #pragma unroll
  3557. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3558. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3559. if (need_check) {
  3560. i = min(i, i_max);
  3561. }
  3562. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3563. const int * scales = (const int *) bxi->scales;
  3564. const int ksc = k % (WARP_SIZE/8);
  3565. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3566. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3567. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3568. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3569. }
  3570. }
  3571. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3572. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3573. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3574. (void)x_qh;
  3575. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3576. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3577. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3578. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3579. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3580. }
  3581. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3582. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3583. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3584. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3585. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3586. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3587. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3588. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3589. const int8_t * scales = bq6_K->scales + scale_offset;
  3590. int u[QR6_K];
  3591. float d8[QR6_K];
  3592. #pragma unroll
  3593. for (int i = 0; i < QR6_K; ++i) {
  3594. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3595. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3596. }
  3597. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3598. }
  3599. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3600. (void)x_qh;
  3601. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3602. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3603. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3604. *x_ql = tile_x_ql;
  3605. *x_dm = tile_x_dm;
  3606. *x_sc = tile_x_sc;
  3607. }
  3608. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3609. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3610. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3611. (void)x_qh;
  3612. GGML_CUDA_ASSUME(i_offset >= 0);
  3613. GGML_CUDA_ASSUME(i_offset < nwarps);
  3614. GGML_CUDA_ASSUME(k >= 0);
  3615. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3616. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3617. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3618. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3619. #pragma unroll
  3620. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3621. int i = i0 + i_offset;
  3622. if (need_check) {
  3623. i = min(i, i_max);
  3624. }
  3625. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3626. const int ky = QR6_K*kqsx;
  3627. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3628. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3629. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3630. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3631. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3632. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3633. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3634. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3635. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3636. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3637. }
  3638. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3639. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3640. float * x_dmf = (float *) x_dm;
  3641. #pragma unroll
  3642. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3643. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3644. if (need_check) {
  3645. i = min(i, i_max);
  3646. }
  3647. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3648. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3649. }
  3650. #pragma unroll
  3651. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3652. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3653. if (need_check) {
  3654. i = min(i, i_max);
  3655. }
  3656. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3657. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3658. }
  3659. }
  3660. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3661. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3662. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3663. (void)x_qh;
  3664. const float * x_dmf = (const float *) x_dm;
  3665. const float * y_df = (const float *) y_ds;
  3666. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3667. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3668. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3669. 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]);
  3670. }
  3671. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3672. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3673. #if QK_K == 256
  3674. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3675. #if QR2_XXS == 8
  3676. const int ib32 = iqs;
  3677. const uint16_t * q2 = bq2->qs + 4*ib32;
  3678. const uint8_t * aux8 = (const uint8_t *)q2;
  3679. const int8_t * q8 = bq8_1[ib32].qs;
  3680. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3681. int sumi = 0;
  3682. for (int l = 0; l < 4; ++l) {
  3683. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3684. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3685. for (int j = 0; j < 8; ++j) {
  3686. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3687. }
  3688. q8 += 8;
  3689. aux32 >>= 7;
  3690. }
  3691. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3692. return d * sumi;
  3693. #else
  3694. // iqs is 0...15
  3695. const int ib32 = iqs/2;
  3696. const int il = iqs%2;
  3697. const uint16_t * q2 = bq2->qs + 4*ib32;
  3698. const uint8_t * aux8 = (const uint8_t *)q2;
  3699. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  3700. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  3701. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3702. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3703. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3704. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3705. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3706. int sumi1 = 0, sumi2 = 0;
  3707. for (int j = 0; j < 8; ++j) {
  3708. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3709. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3710. }
  3711. return d * (sumi1 + sumi2);
  3712. #endif
  3713. #else
  3714. assert(false);
  3715. return 0.f;
  3716. #endif
  3717. }
  3718. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  3719. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3720. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3721. #if QK_K == 256
  3722. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  3723. const int ib32 = iqs;
  3724. const uint16_t * q2 = bq2->qs + 4*ib32;
  3725. const int8_t * q8 = bq8_1[ib32].qs;
  3726. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3727. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3728. int sumi1 = 0;
  3729. for (int l = 0; l < 2; ++l) {
  3730. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3731. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3732. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3733. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3734. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3735. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3736. q8 += 8;
  3737. }
  3738. int sumi2 = 0;
  3739. for (int l = 2; l < 4; ++l) {
  3740. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3741. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3742. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3743. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3744. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3745. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3746. q8 += 8;
  3747. }
  3748. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3749. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3750. #else
  3751. (void) ksigns64;
  3752. assert(false);
  3753. return 0.f;
  3754. #endif
  3755. #else
  3756. (void) ksigns64;
  3757. assert(false);
  3758. return 0.f;
  3759. #endif
  3760. }
  3761. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  3762. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3763. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3764. #if QK_K == 256
  3765. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  3766. const int ib32 = iqs;
  3767. const uint8_t * q3 = bq2->qs + 8*ib32;
  3768. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  3769. const int8_t * q8 = bq8_1[ib32].qs;
  3770. uint32_t aux32 = gas[0] | (gas[1] << 16);
  3771. int sumi = 0;
  3772. for (int l = 0; l < 4; ++l) {
  3773. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  3774. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  3775. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  3776. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  3777. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  3778. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3779. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3780. q8 += 8;
  3781. aux32 >>= 7;
  3782. }
  3783. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  3784. return d * sumi;
  3785. #else
  3786. assert(false);
  3787. return 0.f;
  3788. #endif
  3789. #else
  3790. assert(false);
  3791. return 0.f;
  3792. #endif
  3793. }
  3794. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  3795. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3796. #if QK_K == 256
  3797. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  3798. const int ib32 = iqs;
  3799. int sumi1 = 0, sumi2 = 0, sumi3 = 0, sumi4 = 0;
  3800. const uint8_t h1 = bq1->scales[2*ib32+0];
  3801. const uint8_t h2 = bq1->scales[2*ib32+1];
  3802. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3803. const int * q8 = (const int *)bq8_1[ib32].qs;
  3804. const int * grid1 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3805. const int * grid2 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3806. const int * grid3 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3807. const int * grid4 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3808. for (int j = 0; j < 2; ++j) {
  3809. sumi1 = __dp4a(q8[j+0], grid1[j], sumi1);
  3810. sumi2 = __dp4a(q8[j+2], grid2[j], sumi2);
  3811. sumi3 = __dp4a(q8[j+4], grid3[j], sumi3);
  3812. sumi4 = __dp4a(q8[j+6], grid4[j], sumi4);
  3813. }
  3814. #else
  3815. const int8_t * q8 = bq8_1[ib32].qs;
  3816. const int8_t * grid1 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3817. const int8_t * grid2 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3818. const int8_t * grid3 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3819. const int8_t * grid4 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3820. for (int j = 0; j < 8; ++j) {
  3821. sumi1 += q8[j+ 0] * grid1[j];
  3822. sumi2 += q8[j+ 8] * grid2[j];
  3823. sumi3 += q8[j+16] * grid3[j];
  3824. sumi4 += q8[j+24] * grid4[j];
  3825. }
  3826. #endif
  3827. const float d = (float)bq1->d * __low2float(bq8_1[ib32].ds);
  3828. return d * (sumi1 * (2*(h1 & 7) + 1) + sumi2 * (2*((h1 >> 4) & 7) + 1) +
  3829. sumi3 * (2*(h2 & 7) + 1) + sumi4 * (2*((h2 >> 4) & 7) + 1));
  3830. #else
  3831. assert(false);
  3832. return 0.f;
  3833. #endif
  3834. }
  3835. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3836. static __device__ __forceinline__ void get_int_from_table_16(const uint32_t & q4, const uint8_t * values,
  3837. int & val1, int & val2) {
  3838. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  3839. aux32 = q4 & 0x0f0f0f0f;
  3840. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  3841. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  3842. val1 = v1 | (v2 << 16);
  3843. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  3844. v1 = values[q8[0]] | (values[q8[1]] << 8);
  3845. v2 = values[q8[2]] | (values[q8[3]] << 8);
  3846. val2 = v1 | (v2 << 16);
  3847. }
  3848. #endif
  3849. static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
  3850. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3851. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  3852. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3853. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  3854. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  3855. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  3856. int v1, v2;
  3857. int sumi1 = 0, sumi2 = 0;
  3858. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3859. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  3860. get_int_from_table_16(aux, values, v1, v2);
  3861. sumi1 = __dp4a(v1, q8[l+0], sumi1);
  3862. sumi2 = __dp4a(v2, q8[l+4], sumi2);
  3863. }
  3864. #else
  3865. const uint8_t * q4 = bq->qs + 4*iqs;
  3866. const int8_t * q8 = bq8_1->qs + 4*iqs;
  3867. int sumi1 = 0, sumi2 = 0;
  3868. for (int l = 0; l < 4*VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3869. sumi1 += q8[l+ 0] * kvalues_iq4nl[q4[l] & 0xf];
  3870. sumi2 += q8[l+16] * kvalues_iq4nl[q4[l] >> 4];
  3871. }
  3872. #endif
  3873. const float d = (float)bq->d * __low2float(bq8_1->ds);
  3874. return d * (sumi1 + sumi2);
  3875. }
  3876. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3877. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3878. static __device__ __forceinline__ void mul_mat_q(
  3879. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3880. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3881. const block_q_t * x = (const block_q_t *) vx;
  3882. const block_q8_1 * y = (const block_q8_1 *) vy;
  3883. const int blocks_per_row_x = ncols_x / qk;
  3884. const int blocks_per_col_y = nrows_y / QK8_1;
  3885. const int blocks_per_warp = WARP_SIZE / qi;
  3886. const int & ncols_dst = ncols_y;
  3887. const int row_dst_0 = blockIdx.x*mmq_y;
  3888. const int & row_x_0 = row_dst_0;
  3889. const int col_dst_0 = blockIdx.y*mmq_x;
  3890. const int & col_y_0 = col_dst_0;
  3891. int * tile_x_ql = nullptr;
  3892. half2 * tile_x_dm = nullptr;
  3893. int * tile_x_qh = nullptr;
  3894. int * tile_x_sc = nullptr;
  3895. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3896. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3897. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3898. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3899. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3900. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3901. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3902. #pragma unroll
  3903. for (int ir = 0; ir < qr; ++ir) {
  3904. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3905. const int kbxd = kqs / QI8_1;
  3906. #pragma unroll
  3907. for (int i = 0; i < mmq_x; i += nwarps) {
  3908. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3909. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3910. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3911. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3912. }
  3913. #pragma unroll
  3914. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3915. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3916. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3917. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3918. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3919. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3920. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3921. if (need_sum) {
  3922. *dsi_dst = *dsi_src;
  3923. } else {
  3924. float * dfi_dst = (float *) dsi_dst;
  3925. *dfi_dst = __low2half(*dsi_src);
  3926. }
  3927. }
  3928. __syncthreads();
  3929. // #pragma unroll // unrolling this loop causes too much register pressure
  3930. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3931. #pragma unroll
  3932. for (int j = 0; j < mmq_x; j += nwarps) {
  3933. #pragma unroll
  3934. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3935. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3936. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3937. threadIdx.x + i, threadIdx.y + j, k);
  3938. }
  3939. }
  3940. }
  3941. __syncthreads();
  3942. }
  3943. }
  3944. #pragma unroll
  3945. for (int j = 0; j < mmq_x; j += nwarps) {
  3946. const int col_dst = col_dst_0 + j + threadIdx.y;
  3947. if (col_dst >= ncols_dst) {
  3948. return;
  3949. }
  3950. #pragma unroll
  3951. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3952. const int row_dst = row_dst_0 + threadIdx.x + i;
  3953. if (row_dst >= nrows_dst) {
  3954. continue;
  3955. }
  3956. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3957. }
  3958. }
  3959. }
  3960. #define MMQ_X_Q4_0_RDNA2 64
  3961. #define MMQ_Y_Q4_0_RDNA2 128
  3962. #define NWARPS_Q4_0_RDNA2 8
  3963. #define MMQ_X_Q4_0_RDNA1 64
  3964. #define MMQ_Y_Q4_0_RDNA1 64
  3965. #define NWARPS_Q4_0_RDNA1 8
  3966. #if defined(CUDA_USE_TENSOR_CORES)
  3967. #define MMQ_X_Q4_0_AMPERE 4
  3968. #define MMQ_Y_Q4_0_AMPERE 32
  3969. #define NWARPS_Q4_0_AMPERE 4
  3970. #else
  3971. #define MMQ_X_Q4_0_AMPERE 64
  3972. #define MMQ_Y_Q4_0_AMPERE 128
  3973. #define NWARPS_Q4_0_AMPERE 4
  3974. #endif
  3975. #define MMQ_X_Q4_0_PASCAL 64
  3976. #define MMQ_Y_Q4_0_PASCAL 64
  3977. #define NWARPS_Q4_0_PASCAL 8
  3978. template <bool need_check> static __global__ void
  3979. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3980. #if defined(RDNA3) || defined(RDNA2)
  3981. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3982. #endif // defined(RDNA3) || defined(RDNA2)
  3983. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3984. mul_mat_q4_0(
  3985. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3986. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3987. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3988. #if defined(RDNA3) || defined(RDNA2)
  3989. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3990. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3991. const int nwarps = NWARPS_Q4_0_RDNA2;
  3992. #else
  3993. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3994. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3995. const int nwarps = NWARPS_Q4_0_RDNA1;
  3996. #endif // defined(RDNA3) || defined(RDNA2)
  3997. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3998. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3999. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4000. #elif __CUDA_ARCH__ >= CC_VOLTA
  4001. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  4002. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  4003. const int nwarps = NWARPS_Q4_0_AMPERE;
  4004. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4005. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4006. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4007. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4008. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  4009. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  4010. const int nwarps = NWARPS_Q4_0_PASCAL;
  4011. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4012. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4013. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4014. #else
  4015. (void) vec_dot_q4_0_q8_1_mul_mat;
  4016. NO_DEVICE_CODE;
  4017. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4018. }
  4019. #define MMQ_X_Q4_1_RDNA2 64
  4020. #define MMQ_Y_Q4_1_RDNA2 128
  4021. #define NWARPS_Q4_1_RDNA2 8
  4022. #define MMQ_X_Q4_1_RDNA1 64
  4023. #define MMQ_Y_Q4_1_RDNA1 64
  4024. #define NWARPS_Q4_1_RDNA1 8
  4025. #if defined(CUDA_USE_TENSOR_CORES)
  4026. #define MMQ_X_Q4_1_AMPERE 4
  4027. #define MMQ_Y_Q4_1_AMPERE 32
  4028. #define NWARPS_Q4_1_AMPERE 4
  4029. #else
  4030. #define MMQ_X_Q4_1_AMPERE 64
  4031. #define MMQ_Y_Q4_1_AMPERE 128
  4032. #define NWARPS_Q4_1_AMPERE 4
  4033. #endif
  4034. #define MMQ_X_Q4_1_PASCAL 64
  4035. #define MMQ_Y_Q4_1_PASCAL 64
  4036. #define NWARPS_Q4_1_PASCAL 8
  4037. template <bool need_check> static __global__ void
  4038. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4039. #if defined(RDNA3) || defined(RDNA2)
  4040. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  4041. #endif // defined(RDNA3) || defined(RDNA2)
  4042. #elif __CUDA_ARCH__ < CC_VOLTA
  4043. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  4044. #endif // __CUDA_ARCH__ < CC_VOLTA
  4045. mul_mat_q4_1(
  4046. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4047. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4048. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4049. #if defined(RDNA3) || defined(RDNA2)
  4050. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  4051. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  4052. const int nwarps = NWARPS_Q4_1_RDNA2;
  4053. #else
  4054. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  4055. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  4056. const int nwarps = NWARPS_Q4_1_RDNA1;
  4057. #endif // defined(RDNA3) || defined(RDNA2)
  4058. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4059. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4060. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4061. #elif __CUDA_ARCH__ >= CC_VOLTA
  4062. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  4063. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  4064. const int nwarps = NWARPS_Q4_1_AMPERE;
  4065. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4066. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4067. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4068. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4069. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  4070. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  4071. const int nwarps = NWARPS_Q4_1_PASCAL;
  4072. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4073. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4074. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4075. #else
  4076. (void) vec_dot_q4_1_q8_1_mul_mat;
  4077. NO_DEVICE_CODE;
  4078. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4079. }
  4080. #define MMQ_X_Q5_0_RDNA2 64
  4081. #define MMQ_Y_Q5_0_RDNA2 128
  4082. #define NWARPS_Q5_0_RDNA2 8
  4083. #define MMQ_X_Q5_0_RDNA1 64
  4084. #define MMQ_Y_Q5_0_RDNA1 64
  4085. #define NWARPS_Q5_0_RDNA1 8
  4086. #if defined(CUDA_USE_TENSOR_CORES)
  4087. #define MMQ_X_Q5_0_AMPERE 4
  4088. #define MMQ_Y_Q5_0_AMPERE 32
  4089. #define NWARPS_Q5_0_AMPERE 4
  4090. #else
  4091. #define MMQ_X_Q5_0_AMPERE 128
  4092. #define MMQ_Y_Q5_0_AMPERE 64
  4093. #define NWARPS_Q5_0_AMPERE 4
  4094. #endif
  4095. #define MMQ_X_Q5_0_PASCAL 64
  4096. #define MMQ_Y_Q5_0_PASCAL 64
  4097. #define NWARPS_Q5_0_PASCAL 8
  4098. template <bool need_check> static __global__ void
  4099. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4100. #if defined(RDNA3) || defined(RDNA2)
  4101. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  4102. #endif // defined(RDNA3) || defined(RDNA2)
  4103. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4104. mul_mat_q5_0(
  4105. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4106. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4107. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4108. #if defined(RDNA3) || defined(RDNA2)
  4109. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  4110. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  4111. const int nwarps = NWARPS_Q5_0_RDNA2;
  4112. #else
  4113. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  4114. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  4115. const int nwarps = NWARPS_Q5_0_RDNA1;
  4116. #endif // defined(RDNA3) || defined(RDNA2)
  4117. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4118. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4119. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4120. #elif __CUDA_ARCH__ >= CC_VOLTA
  4121. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  4122. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  4123. const int nwarps = NWARPS_Q5_0_AMPERE;
  4124. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4125. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4126. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4127. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4128. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  4129. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  4130. const int nwarps = NWARPS_Q5_0_PASCAL;
  4131. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4132. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4133. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4134. #else
  4135. (void) vec_dot_q5_0_q8_1_mul_mat;
  4136. NO_DEVICE_CODE;
  4137. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4138. }
  4139. #define MMQ_X_Q5_1_RDNA2 64
  4140. #define MMQ_Y_Q5_1_RDNA2 128
  4141. #define NWARPS_Q5_1_RDNA2 8
  4142. #define MMQ_X_Q5_1_RDNA1 64
  4143. #define MMQ_Y_Q5_1_RDNA1 64
  4144. #define NWARPS_Q5_1_RDNA1 8
  4145. #if defined(CUDA_USE_TENSOR_CORES)
  4146. #define MMQ_X_Q5_1_AMPERE 4
  4147. #define MMQ_Y_Q5_1_AMPERE 32
  4148. #define NWARPS_Q5_1_AMPERE 4
  4149. #else
  4150. #define MMQ_X_Q5_1_AMPERE 128
  4151. #define MMQ_Y_Q5_1_AMPERE 64
  4152. #define NWARPS_Q5_1_AMPERE 4
  4153. #endif
  4154. #define MMQ_X_Q5_1_PASCAL 64
  4155. #define MMQ_Y_Q5_1_PASCAL 64
  4156. #define NWARPS_Q5_1_PASCAL 8
  4157. template <bool need_check> static __global__ void
  4158. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4159. #if defined(RDNA3) || defined(RDNA2)
  4160. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4161. #endif // defined(RDNA3) || defined(RDNA2)
  4162. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4163. mul_mat_q5_1(
  4164. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4165. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4166. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4167. #if defined(RDNA3) || defined(RDNA2)
  4168. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4169. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4170. const int nwarps = NWARPS_Q5_1_RDNA2;
  4171. #else
  4172. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4173. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4174. const int nwarps = NWARPS_Q5_1_RDNA1;
  4175. #endif // defined(RDNA3) || defined(RDNA2)
  4176. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4177. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4178. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4179. #elif __CUDA_ARCH__ >= CC_VOLTA
  4180. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4181. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4182. const int nwarps = NWARPS_Q5_1_AMPERE;
  4183. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4184. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4185. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4186. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4187. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4188. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4189. const int nwarps = NWARPS_Q5_1_PASCAL;
  4190. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4191. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4192. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4193. #else
  4194. (void) vec_dot_q5_1_q8_1_mul_mat;
  4195. NO_DEVICE_CODE;
  4196. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4197. }
  4198. #define MMQ_X_Q8_0_RDNA2 64
  4199. #define MMQ_Y_Q8_0_RDNA2 128
  4200. #define NWARPS_Q8_0_RDNA2 8
  4201. #define MMQ_X_Q8_0_RDNA1 64
  4202. #define MMQ_Y_Q8_0_RDNA1 64
  4203. #define NWARPS_Q8_0_RDNA1 8
  4204. #if defined(CUDA_USE_TENSOR_CORES)
  4205. #define MMQ_X_Q8_0_AMPERE 4
  4206. #define MMQ_Y_Q8_0_AMPERE 32
  4207. #define NWARPS_Q8_0_AMPERE 4
  4208. #else
  4209. #define MMQ_X_Q8_0_AMPERE 128
  4210. #define MMQ_Y_Q8_0_AMPERE 64
  4211. #define NWARPS_Q8_0_AMPERE 4
  4212. #endif
  4213. #define MMQ_X_Q8_0_PASCAL 64
  4214. #define MMQ_Y_Q8_0_PASCAL 64
  4215. #define NWARPS_Q8_0_PASCAL 8
  4216. template <bool need_check> static __global__ void
  4217. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4218. #if defined(RDNA3) || defined(RDNA2)
  4219. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4220. #endif // defined(RDNA3) || defined(RDNA2)
  4221. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4222. mul_mat_q8_0(
  4223. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4224. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4225. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4226. #if defined(RDNA3) || defined(RDNA2)
  4227. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4228. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4229. const int nwarps = NWARPS_Q8_0_RDNA2;
  4230. #else
  4231. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4232. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4233. const int nwarps = NWARPS_Q8_0_RDNA1;
  4234. #endif // defined(RDNA3) || defined(RDNA2)
  4235. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4236. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4237. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4238. #elif __CUDA_ARCH__ >= CC_VOLTA
  4239. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4240. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4241. const int nwarps = NWARPS_Q8_0_AMPERE;
  4242. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4243. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4244. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4245. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4246. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4247. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4248. const int nwarps = NWARPS_Q8_0_PASCAL;
  4249. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4250. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4251. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4252. #else
  4253. (void) vec_dot_q8_0_q8_1_mul_mat;
  4254. NO_DEVICE_CODE;
  4255. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4256. }
  4257. #define MMQ_X_Q2_K_RDNA2 64
  4258. #define MMQ_Y_Q2_K_RDNA2 128
  4259. #define NWARPS_Q2_K_RDNA2 8
  4260. #define MMQ_X_Q2_K_RDNA1 128
  4261. #define MMQ_Y_Q2_K_RDNA1 32
  4262. #define NWARPS_Q2_K_RDNA1 8
  4263. #if defined(CUDA_USE_TENSOR_CORES)
  4264. #define MMQ_X_Q2_K_AMPERE 4
  4265. #define MMQ_Y_Q2_K_AMPERE 32
  4266. #define NWARPS_Q2_K_AMPERE 4
  4267. #else
  4268. #define MMQ_X_Q2_K_AMPERE 64
  4269. #define MMQ_Y_Q2_K_AMPERE 128
  4270. #define NWARPS_Q2_K_AMPERE 4
  4271. #endif
  4272. #define MMQ_X_Q2_K_PASCAL 64
  4273. #define MMQ_Y_Q2_K_PASCAL 64
  4274. #define NWARPS_Q2_K_PASCAL 8
  4275. template <bool need_check> static __global__ void
  4276. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4277. #if defined(RDNA3) || defined(RDNA2)
  4278. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4279. #endif // defined(RDNA3) || defined(RDNA2)
  4280. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4281. mul_mat_q2_K(
  4282. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4283. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4284. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4285. #if defined(RDNA3) || defined(RDNA2)
  4286. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4287. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4288. const int nwarps = NWARPS_Q2_K_RDNA2;
  4289. #else
  4290. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4291. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4292. const int nwarps = NWARPS_Q2_K_RDNA1;
  4293. #endif // defined(RDNA3) || defined(RDNA2)
  4294. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4295. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4296. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4297. #elif __CUDA_ARCH__ >= CC_VOLTA
  4298. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4299. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4300. const int nwarps = NWARPS_Q2_K_AMPERE;
  4301. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4302. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4303. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4304. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4305. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4306. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4307. const int nwarps = NWARPS_Q2_K_PASCAL;
  4308. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4309. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4310. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4311. #else
  4312. (void) vec_dot_q2_K_q8_1_mul_mat;
  4313. NO_DEVICE_CODE;
  4314. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4315. }
  4316. #define MMQ_X_Q3_K_RDNA2 128
  4317. #define MMQ_Y_Q3_K_RDNA2 64
  4318. #define NWARPS_Q3_K_RDNA2 8
  4319. #define MMQ_X_Q3_K_RDNA1 32
  4320. #define MMQ_Y_Q3_K_RDNA1 128
  4321. #define NWARPS_Q3_K_RDNA1 8
  4322. #if defined(CUDA_USE_TENSOR_CORES)
  4323. #define MMQ_X_Q3_K_AMPERE 4
  4324. #define MMQ_Y_Q3_K_AMPERE 32
  4325. #define NWARPS_Q3_K_AMPERE 4
  4326. #else
  4327. #define MMQ_X_Q3_K_AMPERE 128
  4328. #define MMQ_Y_Q3_K_AMPERE 128
  4329. #define NWARPS_Q3_K_AMPERE 4
  4330. #endif
  4331. #define MMQ_X_Q3_K_PASCAL 64
  4332. #define MMQ_Y_Q3_K_PASCAL 64
  4333. #define NWARPS_Q3_K_PASCAL 8
  4334. template <bool need_check> static __global__ void
  4335. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4336. #if defined(RDNA3) || defined(RDNA2)
  4337. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4338. #endif // defined(RDNA3) || defined(RDNA2)
  4339. #elif __CUDA_ARCH__ < CC_VOLTA
  4340. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4341. #endif // __CUDA_ARCH__ < CC_VOLTA
  4342. mul_mat_q3_K(
  4343. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4344. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4345. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4346. #if defined(RDNA3) || defined(RDNA2)
  4347. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4348. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4349. const int nwarps = NWARPS_Q3_K_RDNA2;
  4350. #else
  4351. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4352. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4353. const int nwarps = NWARPS_Q3_K_RDNA1;
  4354. #endif // defined(RDNA3) || defined(RDNA2)
  4355. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4356. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4357. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4358. #elif __CUDA_ARCH__ >= CC_VOLTA
  4359. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4360. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4361. const int nwarps = NWARPS_Q3_K_AMPERE;
  4362. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4363. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4364. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4365. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4366. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4367. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4368. const int nwarps = NWARPS_Q3_K_PASCAL;
  4369. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4370. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4371. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4372. #else
  4373. (void) vec_dot_q3_K_q8_1_mul_mat;
  4374. NO_DEVICE_CODE;
  4375. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4376. }
  4377. #define MMQ_X_Q4_K_RDNA2 64
  4378. #define MMQ_Y_Q4_K_RDNA2 128
  4379. #define NWARPS_Q4_K_RDNA2 8
  4380. #define MMQ_X_Q4_K_RDNA1 32
  4381. #define MMQ_Y_Q4_K_RDNA1 64
  4382. #define NWARPS_Q4_K_RDNA1 8
  4383. #if defined(CUDA_USE_TENSOR_CORES)
  4384. #define MMQ_X_Q4_K_AMPERE 4
  4385. #define MMQ_Y_Q4_K_AMPERE 32
  4386. #define NWARPS_Q4_K_AMPERE 4
  4387. #else
  4388. #define MMQ_X_Q4_K_AMPERE 64
  4389. #define MMQ_Y_Q4_K_AMPERE 128
  4390. #define NWARPS_Q4_K_AMPERE 4
  4391. #endif
  4392. #define MMQ_X_Q4_K_PASCAL 64
  4393. #define MMQ_Y_Q4_K_PASCAL 64
  4394. #define NWARPS_Q4_K_PASCAL 8
  4395. template <bool need_check> static __global__ void
  4396. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4397. #if defined(RDNA3) || defined(RDNA2)
  4398. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4399. #endif // defined(RDNA3) || defined(RDNA2)
  4400. #elif __CUDA_ARCH__ < CC_VOLTA
  4401. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4402. #endif // __CUDA_ARCH__ < CC_VOLTA
  4403. mul_mat_q4_K(
  4404. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4405. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4406. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4407. #if defined(RDNA3) || defined(RDNA2)
  4408. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4409. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4410. const int nwarps = NWARPS_Q4_K_RDNA2;
  4411. #else
  4412. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4413. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4414. const int nwarps = NWARPS_Q4_K_RDNA1;
  4415. #endif // defined(RDNA3) || defined(RDNA2)
  4416. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4417. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4418. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4419. #elif __CUDA_ARCH__ >= CC_VOLTA
  4420. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4421. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4422. const int nwarps = NWARPS_Q4_K_AMPERE;
  4423. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4424. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4425. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4426. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4427. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4428. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4429. const int nwarps = NWARPS_Q4_K_PASCAL;
  4430. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4431. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4432. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4433. #else
  4434. (void) vec_dot_q4_K_q8_1_mul_mat;
  4435. NO_DEVICE_CODE;
  4436. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4437. }
  4438. #define MMQ_X_Q5_K_RDNA2 64
  4439. #define MMQ_Y_Q5_K_RDNA2 128
  4440. #define NWARPS_Q5_K_RDNA2 8
  4441. #define MMQ_X_Q5_K_RDNA1 32
  4442. #define MMQ_Y_Q5_K_RDNA1 64
  4443. #define NWARPS_Q5_K_RDNA1 8
  4444. #if defined(CUDA_USE_TENSOR_CORES)
  4445. #define MMQ_X_Q5_K_AMPERE 4
  4446. #define MMQ_Y_Q5_K_AMPERE 32
  4447. #define NWARPS_Q5_K_AMPERE 4
  4448. #else
  4449. #define MMQ_X_Q5_K_AMPERE 64
  4450. #define MMQ_Y_Q5_K_AMPERE 128
  4451. #define NWARPS_Q5_K_AMPERE 4
  4452. #endif
  4453. #define MMQ_X_Q5_K_PASCAL 64
  4454. #define MMQ_Y_Q5_K_PASCAL 64
  4455. #define NWARPS_Q5_K_PASCAL 8
  4456. template <bool need_check> static __global__ void
  4457. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4458. #if defined(RDNA3) || defined(RDNA2)
  4459. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  4460. #endif // defined(RDNA3) || defined(RDNA2)
  4461. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4462. mul_mat_q5_K(
  4463. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4464. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4465. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4466. #if defined(RDNA3) || defined(RDNA2)
  4467. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  4468. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  4469. const int nwarps = NWARPS_Q5_K_RDNA2;
  4470. #else
  4471. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  4472. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  4473. const int nwarps = NWARPS_Q5_K_RDNA1;
  4474. #endif // defined(RDNA3) || defined(RDNA2)
  4475. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4476. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4477. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4478. #elif __CUDA_ARCH__ >= CC_VOLTA
  4479. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  4480. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  4481. const int nwarps = NWARPS_Q5_K_AMPERE;
  4482. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4483. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4484. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4485. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4486. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  4487. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  4488. const int nwarps = NWARPS_Q5_K_PASCAL;
  4489. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4490. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4491. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4492. #else
  4493. (void) vec_dot_q5_K_q8_1_mul_mat;
  4494. NO_DEVICE_CODE;
  4495. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4496. }
  4497. #define MMQ_X_Q6_K_RDNA2 64
  4498. #define MMQ_Y_Q6_K_RDNA2 128
  4499. #define NWARPS_Q6_K_RDNA2 8
  4500. #define MMQ_X_Q6_K_RDNA1 32
  4501. #define MMQ_Y_Q6_K_RDNA1 64
  4502. #define NWARPS_Q6_K_RDNA1 8
  4503. #if defined(CUDA_USE_TENSOR_CORES)
  4504. #define MMQ_X_Q6_K_AMPERE 4
  4505. #define MMQ_Y_Q6_K_AMPERE 32
  4506. #define NWARPS_Q6_K_AMPERE 4
  4507. #else
  4508. #define MMQ_X_Q6_K_AMPERE 64
  4509. #define MMQ_Y_Q6_K_AMPERE 64
  4510. #define NWARPS_Q6_K_AMPERE 4
  4511. #endif
  4512. #define MMQ_X_Q6_K_PASCAL 64
  4513. #define MMQ_Y_Q6_K_PASCAL 64
  4514. #define NWARPS_Q6_K_PASCAL 8
  4515. template <bool need_check> static __global__ void
  4516. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4517. #if defined(RDNA3) || defined(RDNA2)
  4518. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4519. #endif // defined(RDNA3) || defined(RDNA2)
  4520. #elif __CUDA_ARCH__ < CC_VOLTA
  4521. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4522. #endif // __CUDA_ARCH__ < CC_VOLTA
  4523. mul_mat_q6_K(
  4524. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4525. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4526. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4527. #if defined(RDNA3) || defined(RDNA2)
  4528. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  4529. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  4530. const int nwarps = NWARPS_Q6_K_RDNA2;
  4531. #else
  4532. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  4533. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  4534. const int nwarps = NWARPS_Q6_K_RDNA1;
  4535. #endif // defined(RDNA3) || defined(RDNA2)
  4536. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4537. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4538. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4539. #elif __CUDA_ARCH__ >= CC_VOLTA
  4540. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  4541. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  4542. const int nwarps = NWARPS_Q6_K_AMPERE;
  4543. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4544. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4545. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4546. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4547. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  4548. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  4549. const int nwarps = NWARPS_Q6_K_PASCAL;
  4550. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4551. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4552. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4553. #else
  4554. (void) vec_dot_q6_K_q8_1_mul_mat;
  4555. NO_DEVICE_CODE;
  4556. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4557. }
  4558. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  4559. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4560. // tell the compiler to use as many registers as it wants, see nwarps definition below
  4561. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  4562. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4563. static __global__ void mul_mat_vec_q(
  4564. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4565. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  4566. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  4567. constexpr int nwarps = 1;
  4568. constexpr int rows_per_cuda_block = 1;
  4569. #else
  4570. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  4571. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  4572. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  4573. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  4574. const int row0 = rows_per_cuda_block*blockIdx.x;
  4575. const int blocks_per_row_x = ncols_x / qk;
  4576. const int blocks_per_col_y = nrows_y / QK8_1;
  4577. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  4578. // partial sum for each thread
  4579. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  4580. const block_q_t * x = (const block_q_t *) vx;
  4581. const block_q8_1 * y = (const block_q8_1 *) vy;
  4582. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  4583. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  4584. // x block quant index when casting the quants to int
  4585. const int kqs = vdr * (tid % (qi/vdr));
  4586. #pragma unroll
  4587. for (int j = 0; j < ncols_y; ++j) {
  4588. #pragma unroll
  4589. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4590. tmp[j][i] += vec_dot_q_cuda(
  4591. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  4592. }
  4593. }
  4594. }
  4595. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  4596. if (threadIdx.y > 0) {
  4597. #pragma unroll
  4598. for (int j = 0; j < ncols_y; ++j) {
  4599. #pragma unroll
  4600. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4601. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  4602. }
  4603. }
  4604. }
  4605. __syncthreads();
  4606. if (threadIdx.y > 0) {
  4607. return;
  4608. }
  4609. // sum up partial sums and write back result
  4610. #pragma unroll
  4611. for (int j = 0; j < ncols_y; ++j) {
  4612. #pragma unroll
  4613. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4614. #pragma unroll
  4615. for (int l = 0; l < nwarps-1; ++l) {
  4616. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  4617. }
  4618. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  4619. }
  4620. if (threadIdx.x < rows_per_cuda_block) {
  4621. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  4622. }
  4623. }
  4624. }
  4625. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  4626. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  4627. // qk = quantized weights per x block
  4628. // qr = number of quantized weights per data value in x block
  4629. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4630. if (row >= nrows) {
  4631. return;
  4632. }
  4633. const int tid = threadIdx.x;
  4634. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  4635. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  4636. const int y_offset = qr == 1 ? 1 : qk/2;
  4637. // partial sum for each thread
  4638. #ifdef GGML_CUDA_F16
  4639. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  4640. #else
  4641. float tmp = 0.0f;
  4642. #endif // GGML_CUDA_F16
  4643. for (int i = 0; i < ncols; i += iter_stride) {
  4644. const int col = i + vals_per_iter*tid;
  4645. const int ib = (row*ncols + col)/qk; // x block index
  4646. const int iqs = (col%qk)/qr; // x quant index
  4647. const int iybs = col - col%qk; // y block start index
  4648. // processing >2 values per i iter is faster for fast GPUs
  4649. #pragma unroll
  4650. for (int j = 0; j < vals_per_iter; j += 2) {
  4651. // process 2 vals per j iter
  4652. // dequantize
  4653. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  4654. dfloat2 v;
  4655. dequantize_kernel(vx, ib, iqs + j/qr, v);
  4656. // matrix multiplication
  4657. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  4658. #ifdef GGML_CUDA_F16
  4659. tmp += __hmul2(v, {
  4660. y[iybs + iqs + j/qr + 0],
  4661. y[iybs + iqs + j/qr + y_offset]
  4662. });
  4663. #else
  4664. tmp += v.x * y[iybs + iqs + j/qr + 0];
  4665. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  4666. #endif // GGML_CUDA_F16
  4667. }
  4668. }
  4669. // sum up partial sums and write back result
  4670. #pragma unroll
  4671. for (int mask = 16; mask > 0; mask >>= 1) {
  4672. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4673. }
  4674. if (tid == 0) {
  4675. #ifdef GGML_CUDA_F16
  4676. dst[row] = tmp.x + tmp.y;
  4677. #else
  4678. dst[row] = tmp;
  4679. #endif // GGML_CUDA_F16
  4680. }
  4681. }
  4682. static __global__ void mul_mat_p021_f16_f32(
  4683. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  4684. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  4685. const half * x = (const half *) vx;
  4686. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4687. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4688. const int channel_x = channel / (nchannels_y / nchannels_x);
  4689. const int nrows_y = ncols_x;
  4690. const int nrows_dst = nrows_x;
  4691. const int row_dst = row_x;
  4692. float tmp = 0.0f;
  4693. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4694. const int col_x = col_x0 + threadIdx.x;
  4695. if (col_x >= ncols_x) {
  4696. break;
  4697. }
  4698. // x is transposed and permuted
  4699. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  4700. const float xi = __half2float(x[ix]);
  4701. const int row_y = col_x;
  4702. // y is not transposed but permuted
  4703. const int iy = channel*nrows_y + row_y;
  4704. tmp += xi * y[iy];
  4705. }
  4706. // dst is not transposed and not permuted
  4707. const int idst = channel*nrows_dst + row_dst;
  4708. // sum up partial sums and write back result
  4709. #pragma unroll
  4710. for (int mask = 16; mask > 0; mask >>= 1) {
  4711. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4712. }
  4713. if (threadIdx.x == 0) {
  4714. dst[idst] = tmp;
  4715. }
  4716. }
  4717. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  4718. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  4719. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  4720. const half * x = (const half *) vx;
  4721. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4722. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4723. const int channel_x = channel / channel_x_divisor;
  4724. const int nrows_y = ncols_x;
  4725. const int nrows_dst = nrows_x;
  4726. const int row_dst = row_x;
  4727. const int idst = channel*nrows_dst + row_dst;
  4728. float tmp = 0.0f;
  4729. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4730. const int col_x = col_x0 + threadIdx.x;
  4731. if (col_x >= ncols_x) {
  4732. break;
  4733. }
  4734. const int row_y = col_x;
  4735. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  4736. const int iy = channel*nrows_y + row_y;
  4737. const float xi = __half2float(x[ix]);
  4738. tmp += xi * y[iy];
  4739. }
  4740. // sum up partial sums and write back result
  4741. #pragma unroll
  4742. for (int mask = 16; mask > 0; mask >>= 1) {
  4743. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4744. }
  4745. if (threadIdx.x == 0) {
  4746. dst[idst] = tmp;
  4747. }
  4748. }
  4749. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4750. const float * xi = (const float *) cxi;
  4751. float * dsti = (float *) cdsti;
  4752. *dsti = *xi;
  4753. }
  4754. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4755. const float * xi = (const float *) cxi;
  4756. half * dsti = (half *) cdsti;
  4757. *dsti = __float2half(*xi);
  4758. }
  4759. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4760. const half * xi = (const half *) cxi;
  4761. half * dsti = (half *) cdsti;
  4762. *dsti = *xi;
  4763. }
  4764. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  4765. const half * xi = (const half *) cxi;
  4766. float * dsti = (float *) cdsti;
  4767. *dsti = *xi;
  4768. }
  4769. template <cpy_kernel_t cpy_1>
  4770. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4771. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4772. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4773. const int nb12, const int nb13) {
  4774. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4775. if (i >= ne) {
  4776. return;
  4777. }
  4778. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4779. // then combine those indices with the corresponding byte offsets to get the total offsets
  4780. const int i03 = i/(ne00 * ne01 * ne02);
  4781. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4782. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4783. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4784. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4785. const int i13 = i/(ne10 * ne11 * ne12);
  4786. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4787. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4788. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4789. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  4790. cpy_1(cx + x_offset, cdst + dst_offset);
  4791. }
  4792. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4793. const float * xi = (const float *) cxi;
  4794. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4795. float amax = 0.0f; // absolute max
  4796. for (int j = 0; j < QK8_0; j++) {
  4797. const float v = xi[j];
  4798. amax = fmaxf(amax, fabsf(v));
  4799. }
  4800. const float d = amax / ((1 << 7) - 1);
  4801. const float id = d ? 1.0f/d : 0.0f;
  4802. dsti->d = d;
  4803. for (int j = 0; j < QK8_0; ++j) {
  4804. const float x0 = xi[j]*id;
  4805. dsti->qs[j] = roundf(x0);
  4806. }
  4807. }
  4808. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4809. const float * xi = (const float *) cxi;
  4810. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4811. float amax = 0.0f;
  4812. float vmax = 0.0f;
  4813. for (int j = 0; j < QK4_0; ++j) {
  4814. const float v = xi[j];
  4815. if (amax < fabsf(v)) {
  4816. amax = fabsf(v);
  4817. vmax = v;
  4818. }
  4819. }
  4820. const float d = vmax / -8;
  4821. const float id = d ? 1.0f/d : 0.0f;
  4822. dsti->d = d;
  4823. for (int j = 0; j < QK4_0/2; ++j) {
  4824. const float x0 = xi[0 + j]*id;
  4825. const float x1 = xi[QK4_0/2 + j]*id;
  4826. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4827. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4828. dsti->qs[j] = xi0;
  4829. dsti->qs[j] |= xi1 << 4;
  4830. }
  4831. }
  4832. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4833. const float * xi = (const float *) cxi;
  4834. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4835. float vmin = FLT_MAX;
  4836. float vmax = -FLT_MAX;
  4837. for (int j = 0; j < QK4_1; ++j) {
  4838. const float v = xi[j];
  4839. if (v < vmin) vmin = v;
  4840. if (v > vmax) vmax = v;
  4841. }
  4842. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4843. const float id = d ? 1.0f/d : 0.0f;
  4844. dsti->dm.x = d;
  4845. dsti->dm.y = vmin;
  4846. for (int j = 0; j < QK4_1/2; ++j) {
  4847. const float x0 = (xi[0 + j] - vmin)*id;
  4848. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4849. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4850. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4851. dsti->qs[j] = xi0;
  4852. dsti->qs[j] |= xi1 << 4;
  4853. }
  4854. }
  4855. template <cpy_kernel_t cpy_blck, int qk>
  4856. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4857. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4858. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4859. const int nb12, const int nb13) {
  4860. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4861. if (i >= ne) {
  4862. return;
  4863. }
  4864. const int i03 = i/(ne00 * ne01 * ne02);
  4865. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4866. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4867. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4868. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4869. const int i13 = i/(ne10 * ne11 * ne12);
  4870. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4871. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4872. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4873. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  4874. cpy_blck(cx + x_offset, cdst + dst_offset);
  4875. }
  4876. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4877. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4878. return 1.0f - min(1.0f, max(0.0f, y));
  4879. }
  4880. struct rope_corr_dims {
  4881. float v[4];
  4882. };
  4883. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4884. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4885. static __device__ void rope_yarn(
  4886. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4887. float * cos_theta, float * sin_theta
  4888. ) {
  4889. // Get n-d rotational scaling corrected for extrapolation
  4890. float theta_interp = freq_scale * theta_extrap;
  4891. float theta = theta_interp;
  4892. if (ext_factor != 0.0f) {
  4893. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4894. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4895. // Get n-d magnitude scaling corrected for interpolation
  4896. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4897. }
  4898. *cos_theta = cosf(theta) * mscale;
  4899. *sin_theta = sinf(theta) * mscale;
  4900. }
  4901. // rope == RoPE == rotary positional embedding
  4902. template<typename T, bool has_pos>
  4903. static __global__ void rope(
  4904. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4905. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4906. ) {
  4907. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4908. if (col >= ncols) {
  4909. return;
  4910. }
  4911. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4912. const int i = row*ncols + col;
  4913. const int i2 = row/p_delta_rows;
  4914. const int p = has_pos ? pos[i2] : 0;
  4915. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4916. float cos_theta, sin_theta;
  4917. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4918. const float x0 = x[i + 0];
  4919. const float x1 = x[i + 1];
  4920. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4921. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4922. }
  4923. template<typename T, bool has_pos>
  4924. static __global__ void rope_neox(
  4925. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4926. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4927. ) {
  4928. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4929. if (col >= ncols) {
  4930. return;
  4931. }
  4932. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4933. const int ib = col / n_dims;
  4934. const int ic = col % n_dims;
  4935. if (ib > 0) {
  4936. const int i = row*ncols + ib*n_dims + ic;
  4937. dst[i + 0] = x[i + 0];
  4938. dst[i + 1] = x[i + 1];
  4939. return;
  4940. }
  4941. const int i = row*ncols + ib*n_dims + ic/2;
  4942. const int i2 = row/p_delta_rows;
  4943. float cur_rot = inv_ndims * ic - ib;
  4944. const int p = has_pos ? pos[i2] : 0;
  4945. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4946. float cos_theta, sin_theta;
  4947. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4948. const float x0 = x[i + 0];
  4949. const float x1 = x[i + n_dims/2];
  4950. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4951. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4952. }
  4953. static __global__ void rope_glm_f32(
  4954. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4955. int n_ctx
  4956. ) {
  4957. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4958. const int half_n_dims = ncols/4;
  4959. if (col >= half_n_dims) {
  4960. return;
  4961. }
  4962. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4963. const int i = row*ncols + col;
  4964. const int i2 = row/p_delta_rows;
  4965. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4966. // FIXME: this is likely wrong
  4967. const int p = pos != nullptr ? pos[i2] : 0;
  4968. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4969. const float sin_theta = sinf(theta);
  4970. const float cos_theta = cosf(theta);
  4971. const float x0 = x[i + 0];
  4972. const float x1 = x[i + half_n_dims];
  4973. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4974. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4975. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4976. const float sin_block_theta = sinf(block_theta);
  4977. const float cos_block_theta = cosf(block_theta);
  4978. const float x2 = x[i + half_n_dims * 2];
  4979. const float x3 = x[i + half_n_dims * 3];
  4980. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4981. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4982. }
  4983. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4984. const int n_heads_log2_floor, const float m0, const float m1) {
  4985. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4986. if (col >= ncols) {
  4987. return;
  4988. }
  4989. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4990. const int i = row*ncols + col;
  4991. const int k = row/k_rows;
  4992. float m_k;
  4993. if (k < n_heads_log2_floor) {
  4994. m_k = powf(m0, k + 1);
  4995. } else {
  4996. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4997. }
  4998. dst[i] = col * m_k + x[i];
  4999. }
  5000. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  5001. const int row = blockIdx.x;
  5002. const int col = threadIdx.x;
  5003. float sum = 0.0f;
  5004. for (int i = col; i < ncols; i += blockDim.x) {
  5005. sum += x[row * ncols + i];
  5006. }
  5007. sum = warp_reduce_sum(sum);
  5008. if (col == 0) {
  5009. dst[row] = sum;
  5010. }
  5011. }
  5012. template<typename T>
  5013. static inline __device__ void swap(T & a, T & b) {
  5014. T tmp = a;
  5015. a = b;
  5016. b = tmp;
  5017. }
  5018. template<ggml_sort_order order>
  5019. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  5020. // bitonic sort
  5021. int col = threadIdx.x;
  5022. int row = blockIdx.y;
  5023. if (col >= ncols) return;
  5024. const float * x_row = x + row * ncols;
  5025. int * dst_row = dst + row * ncols;
  5026. // initialize indices
  5027. if (col < ncols) {
  5028. dst_row[col] = col;
  5029. }
  5030. __syncthreads();
  5031. for (int k = 2; k <= ncols; k *= 2) {
  5032. for (int j = k / 2; j > 0; j /= 2) {
  5033. int ixj = col ^ j;
  5034. if (ixj > col) {
  5035. if ((col & k) == 0) {
  5036. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  5037. swap(dst_row[col], dst_row[ixj]);
  5038. }
  5039. } else {
  5040. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  5041. swap(dst_row[col], dst_row[ixj]);
  5042. }
  5043. }
  5044. }
  5045. __syncthreads();
  5046. }
  5047. }
  5048. }
  5049. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  5050. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  5051. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5052. if (col >= ncols) {
  5053. return;
  5054. }
  5055. const int i = row*ncols + col;
  5056. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  5057. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  5058. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  5059. }
  5060. template <bool vals_smem, int ncols_template, int block_size_template>
  5061. 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) {
  5062. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  5063. const int tid = threadIdx.x;
  5064. const int rowx = blockIdx.x;
  5065. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  5066. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  5067. const int warp_id = threadIdx.x / WARP_SIZE;
  5068. const int lane_id = threadIdx.x % WARP_SIZE;
  5069. float slope = 0.0f;
  5070. // ALiBi
  5071. if (max_bias > 0.0f) {
  5072. const int h = rowx/nrows_y; // head index
  5073. const float base = h < n_head_log2 ? m0 : m1;
  5074. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  5075. slope = powf(base, exp);
  5076. }
  5077. extern __shared__ float data_soft_max_f32[];
  5078. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  5079. // shared memory buffer to cache values between iterations:
  5080. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  5081. float max_val = -INFINITY;
  5082. #pragma unroll
  5083. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5084. const int col = col0 + tid;
  5085. if (ncols_template == 0 && col >= ncols) {
  5086. break;
  5087. }
  5088. const int ix = rowx*ncols + col;
  5089. const int iy = rowy*ncols + col;
  5090. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  5091. vals[col] = val;
  5092. max_val = max(max_val, val);
  5093. }
  5094. // find the max value in the block
  5095. max_val = warp_reduce_max(max_val);
  5096. if (block_size > WARP_SIZE) {
  5097. if (warp_id == 0) {
  5098. buf_iw[lane_id] = -INFINITY;
  5099. }
  5100. __syncthreads();
  5101. if (lane_id == 0) {
  5102. buf_iw[warp_id] = max_val;
  5103. }
  5104. __syncthreads();
  5105. max_val = buf_iw[lane_id];
  5106. max_val = warp_reduce_max(max_val);
  5107. }
  5108. float tmp = 0.0f; // partial sum
  5109. #pragma unroll
  5110. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5111. const int col = col0 + tid;
  5112. if (ncols_template == 0 && col >= ncols) {
  5113. break;
  5114. }
  5115. const float val = expf(vals[col] - max_val);
  5116. tmp += val;
  5117. vals[col] = val;
  5118. }
  5119. // find the sum of exps in the block
  5120. tmp = warp_reduce_sum(tmp);
  5121. if (block_size > WARP_SIZE) {
  5122. if (warp_id == 0) {
  5123. buf_iw[lane_id] = 0.0f;
  5124. }
  5125. __syncthreads();
  5126. if (lane_id == 0) {
  5127. buf_iw[warp_id] = tmp;
  5128. }
  5129. __syncthreads();
  5130. tmp = buf_iw[lane_id];
  5131. tmp = warp_reduce_sum(tmp);
  5132. }
  5133. const float inv_sum = 1.0f / tmp;
  5134. #pragma unroll
  5135. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5136. const int col = col0 + tid;
  5137. if (ncols_template == 0 && col >= ncols) {
  5138. return;
  5139. }
  5140. const int idst = rowx*ncols + col;
  5141. dst[idst] = vals[col] * inv_sum;
  5142. }
  5143. }
  5144. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5145. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5146. if (i >= k) {
  5147. return;
  5148. }
  5149. dst[i] = scale * x[i];
  5150. }
  5151. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5152. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5153. if (i >= k) {
  5154. return;
  5155. }
  5156. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5157. }
  5158. template <typename T>
  5159. static __global__ void im2col_kernel(
  5160. const float * x, T * dst, int batch_offset,
  5161. int offset_delta, int IC, int IW, int IH, int OH, int OW, int KW, int KH, int pelements, int CHW,
  5162. int s0, int s1, int p0, int p1, int d0, int d1) {
  5163. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  5164. if (i >= pelements) {
  5165. return;
  5166. }
  5167. const int ksize = OW * (KH > 1 ? KW : 1);
  5168. const int kx = i / ksize;
  5169. const int kd = kx * ksize;
  5170. const int ky = (i - kd) / OW;
  5171. const int ix = i % OW;
  5172. const int oh = blockIdx.y;
  5173. const int batch = blockIdx.z / IC;
  5174. const int ic = blockIdx.z % IC;
  5175. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5176. const int64_t iih = oh * s1 + ky * d1 - p1;
  5177. const int64_t offset_dst =
  5178. ((batch * OH + oh) * OW + ix) * CHW +
  5179. (ic * (KW * KH) + ky * KW + kx);
  5180. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5181. dst[offset_dst] = 0.0f;
  5182. } else {
  5183. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5184. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5185. }
  5186. }
  5187. template <typename Ti, typename To>
  5188. static __global__ void pool2d_nchw_kernel(
  5189. const int ih, const int iw, const int oh, const int ow,
  5190. const int kh, const int kw, const int sh, const int sw,
  5191. const int ph, const int pw, const int parallel_elements,
  5192. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5193. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5194. if (idx >= parallel_elements) {
  5195. return;
  5196. }
  5197. const int I_HW = ih * iw;
  5198. const int O_HW = oh * ow;
  5199. const int nc = idx / O_HW;
  5200. const int cur_oh = idx % O_HW / ow;
  5201. const int cur_ow = idx % O_HW % ow;
  5202. const Ti* i_ptr = src + nc * I_HW;
  5203. To* o_ptr = dst + nc * O_HW;
  5204. const int start_h = cur_oh * sh - ph;
  5205. const int bh = max(0, start_h);
  5206. const int eh = min(ih, start_h + kh);
  5207. const int start_w = cur_ow * sw - pw;
  5208. const int bw = max(0, start_w);
  5209. const int ew = min(iw, start_w + kw);
  5210. const To scale = 1. / (kh * kw);
  5211. To res = 0;
  5212. switch (op) {
  5213. case GGML_OP_POOL_AVG: res = 0; break;
  5214. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5215. }
  5216. for (int i = bh; i < eh; i += 1) {
  5217. for (int j = bw; j < ew; j += 1) {
  5218. #if __CUDA_ARCH__ >= 350
  5219. Ti cur = __ldg(i_ptr + i * iw + j);
  5220. #else
  5221. Ti cur = i_ptr[i * iw + j];
  5222. #endif
  5223. switch (op) {
  5224. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5225. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5226. }
  5227. }
  5228. }
  5229. o_ptr[cur_oh * ow + cur_ow] = res;
  5230. }
  5231. template<int qk, int qr, dequantize_kernel_t dq>
  5232. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5233. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5234. GGML_TENSOR_BINARY_OP_LOCALS
  5235. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5236. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5237. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5238. // strides in elements
  5239. //const size_t s0 = nb0 / ggml_element_size(dst);
  5240. const size_t s1 = nb1 / ggml_element_size(dst);
  5241. const size_t s2 = nb2 / ggml_element_size(dst);
  5242. const size_t s3 = nb3 / ggml_element_size(dst);
  5243. const size_t s10 = nb10 / ggml_element_size(src1);
  5244. const size_t s11 = nb11 / ggml_element_size(src1);
  5245. const size_t s12 = nb12 / ggml_element_size(src1);
  5246. //const size_t s13 = nb13 / ggml_element_size(src1);
  5247. GGML_ASSERT(ne00 % 2 == 0);
  5248. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5249. src0_dd, src1_dd, dst_dd,
  5250. ne00, /*ne01, ne02, ne03,*/
  5251. /*ne10, ne11,*/ ne12, /*ne13,*/
  5252. /* s0,*/ s1, s2, s3,
  5253. /* nb00,*/ nb01, nb02, nb03,
  5254. s10, s11, s12/*, s13*/);
  5255. (void) dst;
  5256. }
  5257. template<typename src0_t>
  5258. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5259. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5260. GGML_TENSOR_BINARY_OP_LOCALS
  5261. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5262. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5263. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5264. // strides in elements
  5265. //const size_t s0 = nb0 / ggml_element_size(dst);
  5266. const size_t s1 = nb1 / ggml_element_size(dst);
  5267. const size_t s2 = nb2 / ggml_element_size(dst);
  5268. const size_t s3 = nb3 / ggml_element_size(dst);
  5269. const size_t s10 = nb10 / ggml_element_size(src1);
  5270. const size_t s11 = nb11 / ggml_element_size(src1);
  5271. const size_t s12 = nb12 / ggml_element_size(src1);
  5272. //const size_t s13 = nb13 / ggml_element_size(src1);
  5273. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5274. src0_dd, src1_dd, dst_dd,
  5275. ne00, /*ne01, ne02, ne03,*/
  5276. /*ne10, ne11,*/ ne12, /*ne13,*/
  5277. /* s0,*/ s1, s2, s3,
  5278. /* nb00,*/ nb01, nb02, nb03,
  5279. s10, s11, s12/*, s13*/);
  5280. (void) dst;
  5281. }
  5282. template<float (*bin_op)(const float, const float)>
  5283. struct bin_bcast_cuda {
  5284. template<typename src0_t, typename src1_t, typename dst_t>
  5285. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5286. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5287. cudaStream_t stream) {
  5288. GGML_TENSOR_BINARY_OP_LOCALS
  5289. int nr0 = ne10/ne0;
  5290. int nr1 = ne11/ne1;
  5291. int nr2 = ne12/ne2;
  5292. int nr3 = ne13/ne3;
  5293. int nr[4] = { nr0, nr1, nr2, nr3 };
  5294. // collapse dimensions until first broadcast dimension
  5295. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5296. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5297. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5298. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5299. auto collapse = [](int64_t cne[]) {
  5300. cne[0] *= cne[1];
  5301. cne[1] = cne[2];
  5302. cne[2] = cne[3];
  5303. cne[3] = 1;
  5304. };
  5305. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5306. cnb[1] *= cne[1];
  5307. cnb[2] *= cne[2];
  5308. cnb[3] *= cne[3];
  5309. };
  5310. for (int i = 0; i < 4; i++) {
  5311. if (nr[i] != 1) {
  5312. break;
  5313. }
  5314. if (i > 0) {
  5315. collapse_nb(cnb0, cne0);
  5316. collapse_nb(cnb1, cne1);
  5317. collapse(cne0);
  5318. collapse(cne1);
  5319. }
  5320. }
  5321. {
  5322. int64_t ne0 = cne0[0];
  5323. int64_t ne1 = cne0[1];
  5324. int64_t ne2 = cne0[2];
  5325. int64_t ne3 = cne0[3];
  5326. int64_t ne10 = cne1[0];
  5327. int64_t ne11 = cne1[1];
  5328. int64_t ne12 = cne1[2];
  5329. int64_t ne13 = cne1[3];
  5330. size_t nb0 = cnb0[0];
  5331. size_t nb1 = cnb0[1];
  5332. size_t nb2 = cnb0[2];
  5333. size_t nb3 = cnb0[3];
  5334. size_t nb10 = cnb1[0];
  5335. size_t nb11 = cnb1[1];
  5336. size_t nb12 = cnb1[2];
  5337. size_t nb13 = cnb1[3];
  5338. size_t s0 = nb0 / sizeof(dst_t);
  5339. size_t s1 = nb1 / sizeof(dst_t);
  5340. size_t s2 = nb2 / sizeof(dst_t);
  5341. size_t s3 = nb3 / sizeof(dst_t);
  5342. size_t s10 = nb10 / sizeof(src1_t);
  5343. size_t s11 = nb11 / sizeof(src1_t);
  5344. size_t s12 = nb12 / sizeof(src1_t);
  5345. size_t s13 = nb13 / sizeof(src1_t);
  5346. GGML_ASSERT(s0 == 1);
  5347. GGML_ASSERT(s10 == 1);
  5348. const int block_size = 128;
  5349. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5350. dim3 block_dims;
  5351. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5352. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5353. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5354. dim3 block_nums(
  5355. (hne0 + block_dims.x - 1) / block_dims.x,
  5356. (ne1 + block_dims.y - 1) / block_dims.y,
  5357. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5358. );
  5359. if (block_nums.z > 65535) {
  5360. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5361. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5362. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5363. src0_dd, src1_dd, dst_dd,
  5364. ne0, ne1, ne2, ne3,
  5365. ne10, ne11, ne12, ne13,
  5366. /* s0, */ s1, s2, s3,
  5367. /* s10, */ s11, s12, s13);
  5368. } else {
  5369. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5370. src0_dd, src1_dd, dst_dd,
  5371. ne0, ne1, ne2, ne3,
  5372. ne10, ne11, ne12, ne13,
  5373. /* s0, */ s1, s2, s3,
  5374. /* s10, */ s11, s12, s13);
  5375. }
  5376. }
  5377. }
  5378. };
  5379. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5380. const int ne10, const int ne11, const int ne12,
  5381. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5382. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5383. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5384. }
  5385. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5386. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5387. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5388. }
  5389. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5390. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5391. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5392. }
  5393. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5394. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5395. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5396. }
  5397. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5398. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5399. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5400. }
  5401. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5402. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5403. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5404. }
  5405. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5406. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5407. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5408. }
  5409. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5410. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5411. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5412. }
  5413. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5414. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5415. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5416. }
  5417. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5418. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5419. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5420. }
  5421. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5422. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5423. if (ncols < 1024) {
  5424. const dim3 block_dims(WARP_SIZE, 1, 1);
  5425. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5426. } else {
  5427. const dim3 block_dims(1024, 1, 1);
  5428. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5429. }
  5430. }
  5431. 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) {
  5432. static const float eps = 1e-6f;
  5433. if (group_size < 1024) {
  5434. const dim3 block_dims(WARP_SIZE, 1, 1);
  5435. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5436. } else {
  5437. const dim3 block_dims(1024, 1, 1);
  5438. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5439. }
  5440. }
  5441. 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) {
  5442. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5443. dim3 gridDim(num_blocks, ne1, ne2);
  5444. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5445. }
  5446. 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) {
  5447. int ne0 = (ne00 * scale_factor);
  5448. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5449. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  5450. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  5451. }
  5452. static void pad_f32_cuda(const float * x, float * dst,
  5453. const int ne00, const int ne01, const int ne02,
  5454. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  5455. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  5456. dim3 gridDim(num_blocks, ne1, ne2);
  5457. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  5458. }
  5459. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5460. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5461. if (ncols < 1024) {
  5462. const dim3 block_dims(WARP_SIZE, 1, 1);
  5463. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5464. } else {
  5465. const dim3 block_dims(1024, 1, 1);
  5466. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5467. }
  5468. }
  5469. 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) {
  5470. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  5471. const dim3 num_blocks(block_num_x, ky, 1);
  5472. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  5473. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  5474. }
  5475. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5476. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5477. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  5478. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5479. }
  5480. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  5481. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  5482. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  5483. const bool need_check = false;
  5484. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5485. } else {
  5486. const bool need_check = true;
  5487. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5488. }
  5489. }
  5490. template<typename dst_t>
  5491. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5492. const int nb = k / QK_K;
  5493. #if QK_K == 256
  5494. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  5495. #else
  5496. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  5497. #endif
  5498. }
  5499. template<typename dst_t>
  5500. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5501. const int nb = k / QK_K;
  5502. #if QK_K == 256
  5503. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  5504. #else
  5505. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  5506. #endif
  5507. }
  5508. template<typename dst_t>
  5509. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5510. const int nb32 = k / 32;
  5511. const int nb = (k + 255) / 256;
  5512. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5513. }
  5514. template<typename dst_t>
  5515. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5516. const int nb32 = k / 32;
  5517. const int nb = (k + 255) / 256;
  5518. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5519. }
  5520. template<typename dst_t>
  5521. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5522. const int nb = k / QK_K;
  5523. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  5524. }
  5525. template<typename dst_t>
  5526. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5527. const int nb = k / QK_K;
  5528. #if QK_K == 256
  5529. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  5530. #else
  5531. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  5532. #endif
  5533. }
  5534. template<typename dst_t>
  5535. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5536. const int nb = k / QK_K;
  5537. #if QK_K == 256
  5538. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  5539. #else
  5540. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  5541. #endif
  5542. }
  5543. template<typename dst_t>
  5544. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5545. const int nb = k / QK_K;
  5546. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5547. }
  5548. template<typename dst_t>
  5549. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5550. const int nb = k / QK_K;
  5551. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  5552. }
  5553. template<typename dst_t>
  5554. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5555. const int nb = k / QK_K;
  5556. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5557. }
  5558. template<typename dst_t>
  5559. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5560. const int nb = k / QK_K;
  5561. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  5562. }
  5563. template<typename dst_t>
  5564. static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5565. const int nb = (k + QK_K - 1) / QK_K;
  5566. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  5567. }
  5568. template <typename src_t, typename dst_t>
  5569. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5570. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  5571. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5572. }
  5573. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  5574. int id;
  5575. switch (type) {
  5576. case GGML_TYPE_Q4_0:
  5577. return dequantize_row_q4_0_cuda;
  5578. case GGML_TYPE_Q4_1:
  5579. return dequantize_row_q4_1_cuda;
  5580. case GGML_TYPE_Q5_0:
  5581. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5582. case GGML_TYPE_Q5_1:
  5583. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5584. case GGML_TYPE_Q8_0:
  5585. CUDA_CHECK(cudaGetDevice(&id));
  5586. if (g_device_caps[id].cc >= CC_PASCAL) {
  5587. return dequantize_block_q8_0_f16_cuda;
  5588. }
  5589. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5590. case GGML_TYPE_Q2_K:
  5591. return dequantize_row_q2_K_cuda;
  5592. case GGML_TYPE_Q3_K:
  5593. return dequantize_row_q3_K_cuda;
  5594. case GGML_TYPE_Q4_K:
  5595. return dequantize_row_q4_K_cuda;
  5596. case GGML_TYPE_Q5_K:
  5597. return dequantize_row_q5_K_cuda;
  5598. case GGML_TYPE_Q6_K:
  5599. return dequantize_row_q6_K_cuda;
  5600. case GGML_TYPE_IQ2_XXS:
  5601. return dequantize_row_iq2_xxs_cuda;
  5602. case GGML_TYPE_IQ2_XS:
  5603. return dequantize_row_iq2_xs_cuda;
  5604. case GGML_TYPE_IQ3_XXS:
  5605. return dequantize_row_iq3_xxs_cuda;
  5606. case GGML_TYPE_IQ1_S:
  5607. return dequantize_row_iq1_s_cuda;
  5608. case GGML_TYPE_IQ4_NL:
  5609. return dequantize_row_iq4_nl_cuda;
  5610. case GGML_TYPE_F32:
  5611. return convert_unary_cuda<float>;
  5612. default:
  5613. return nullptr;
  5614. }
  5615. }
  5616. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  5617. switch (type) {
  5618. case GGML_TYPE_Q4_0:
  5619. return dequantize_row_q4_0_cuda;
  5620. case GGML_TYPE_Q4_1:
  5621. return dequantize_row_q4_1_cuda;
  5622. case GGML_TYPE_Q5_0:
  5623. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5624. case GGML_TYPE_Q5_1:
  5625. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5626. case GGML_TYPE_Q8_0:
  5627. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5628. case GGML_TYPE_Q2_K:
  5629. return dequantize_row_q2_K_cuda;
  5630. case GGML_TYPE_Q3_K:
  5631. return dequantize_row_q3_K_cuda;
  5632. case GGML_TYPE_Q4_K:
  5633. return dequantize_row_q4_K_cuda;
  5634. case GGML_TYPE_Q5_K:
  5635. return dequantize_row_q5_K_cuda;
  5636. case GGML_TYPE_Q6_K:
  5637. return dequantize_row_q6_K_cuda;
  5638. case GGML_TYPE_IQ2_XXS:
  5639. return dequantize_row_iq2_xxs_cuda;
  5640. case GGML_TYPE_IQ2_XS:
  5641. return dequantize_row_iq2_xs_cuda;
  5642. case GGML_TYPE_IQ3_XXS:
  5643. return dequantize_row_iq3_xxs_cuda;
  5644. case GGML_TYPE_IQ1_S:
  5645. return dequantize_row_iq1_s_cuda;
  5646. case GGML_TYPE_IQ4_NL:
  5647. return dequantize_row_iq4_nl_cuda;
  5648. case GGML_TYPE_F16:
  5649. return convert_unary_cuda<half>;
  5650. default:
  5651. return nullptr;
  5652. }
  5653. }
  5654. 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) {
  5655. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5656. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5657. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  5658. const dim3 block_nums(block_num_y, 1, 1);
  5659. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5660. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  5661. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5662. }
  5663. 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) {
  5664. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5665. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5666. const dim3 block_nums(block_num_y, 1, 1);
  5667. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5668. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  5669. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5670. }
  5671. 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) {
  5672. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5673. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5674. const dim3 block_nums(block_num_y, 1, 1);
  5675. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5676. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  5677. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5678. }
  5679. 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) {
  5680. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5681. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5682. const dim3 block_nums(block_num_y, 1, 1);
  5683. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5684. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  5685. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5686. }
  5687. 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) {
  5688. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5689. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5690. const dim3 block_nums(block_num_y, 1, 1);
  5691. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5692. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  5693. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5694. }
  5695. 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) {
  5696. GGML_ASSERT(ncols % QK_K == 0);
  5697. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  5698. const int block_num_y = (nrows + ny - 1) / ny;
  5699. const dim3 block_nums(block_num_y, 1, 1);
  5700. const dim3 block_dims(32, ny, 1);
  5701. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5702. }
  5703. 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) {
  5704. GGML_ASSERT(ncols % QK_K == 0);
  5705. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5706. const int block_num_y = (nrows + ny - 1) / ny;
  5707. const dim3 block_nums(block_num_y, 1, 1);
  5708. const dim3 block_dims(32, ny, 1);
  5709. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5710. }
  5711. 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) {
  5712. GGML_ASSERT(ncols % QK_K == 0);
  5713. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5714. const int block_num_y = (nrows + ny - 1) / ny;
  5715. const dim3 block_nums(block_num_y, 1, 1);
  5716. const dim3 block_dims(32, ny, 1);
  5717. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5718. }
  5719. 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) {
  5720. GGML_ASSERT(ncols % QK_K == 0);
  5721. const dim3 block_dims(32, 1, 1);
  5722. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  5723. }
  5724. 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) {
  5725. GGML_ASSERT(ncols % QK_K == 0);
  5726. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5727. const int block_num_y = (nrows + ny - 1) / ny;
  5728. const dim3 block_nums(block_num_y, 1, 1);
  5729. const dim3 block_dims(32, ny, 1);
  5730. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5731. }
  5732. 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) {
  5733. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5734. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5735. const dim3 block_nums(block_num_y, 1, 1);
  5736. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5737. dequantize_mul_mat_vec<1, 1, convert_f16>
  5738. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5739. }
  5740. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  5741. static void mul_mat_vec_q_cuda(
  5742. const void * vx, const void * vy, float * dst,
  5743. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  5744. GGML_ASSERT(ncols_x % qk == 0);
  5745. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  5746. int id;
  5747. CUDA_CHECK(cudaGetDevice(&id));
  5748. int64_t nwarps = 1;
  5749. int64_t rows_per_cuda_block = 1;
  5750. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  5751. switch(ncols_y) {
  5752. case 1:
  5753. nwarps = 4;
  5754. rows_per_cuda_block = 1;
  5755. break;
  5756. case 2:
  5757. case 3:
  5758. case 4:
  5759. nwarps = 4;
  5760. rows_per_cuda_block = 2;
  5761. break;
  5762. case 5:
  5763. case 6:
  5764. case 7:
  5765. case 8:
  5766. nwarps = 2;
  5767. rows_per_cuda_block = 2;
  5768. break;
  5769. default:
  5770. GGML_ASSERT(false);
  5771. break;
  5772. }
  5773. }
  5774. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  5775. const dim3 block_nums(nblocks, 1, 1);
  5776. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5777. switch (ncols_y) {
  5778. case 1:
  5779. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  5780. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5781. break;
  5782. case 2:
  5783. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  5784. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5785. break;
  5786. case 3:
  5787. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  5788. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5789. break;
  5790. case 4:
  5791. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  5792. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5793. break;
  5794. case 5:
  5795. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  5796. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5797. break;
  5798. case 6:
  5799. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  5800. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5801. break;
  5802. case 7:
  5803. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  5804. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5805. break;
  5806. case 8:
  5807. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  5808. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5809. break;
  5810. default:
  5811. GGML_ASSERT(false);
  5812. break;
  5813. }
  5814. }
  5815. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5816. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5817. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5818. int id;
  5819. CUDA_CHECK(cudaGetDevice(&id));
  5820. const int compute_capability = g_device_caps[id].cc;
  5821. int mmq_x, mmq_y, nwarps;
  5822. if (compute_capability >= CC_RDNA2) {
  5823. mmq_x = MMQ_X_Q4_0_RDNA2;
  5824. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5825. nwarps = NWARPS_Q4_0_RDNA2;
  5826. } else if (compute_capability >= CC_OFFSET_AMD) {
  5827. mmq_x = MMQ_X_Q4_0_RDNA1;
  5828. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5829. nwarps = NWARPS_Q4_0_RDNA1;
  5830. } else if (compute_capability >= CC_VOLTA) {
  5831. mmq_x = MMQ_X_Q4_0_AMPERE;
  5832. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5833. nwarps = NWARPS_Q4_0_AMPERE;
  5834. } else if (compute_capability >= MIN_CC_DP4A) {
  5835. mmq_x = MMQ_X_Q4_0_PASCAL;
  5836. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5837. nwarps = NWARPS_Q4_0_PASCAL;
  5838. } else {
  5839. GGML_ASSERT(false);
  5840. }
  5841. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5842. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5843. const dim3 block_nums(block_num_x, block_num_y, 1);
  5844. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5845. if (nrows_x % mmq_y == 0) {
  5846. const bool need_check = false;
  5847. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5848. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5849. } else {
  5850. const bool need_check = true;
  5851. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5852. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5853. }
  5854. }
  5855. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5856. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5857. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5858. int id;
  5859. CUDA_CHECK(cudaGetDevice(&id));
  5860. const int compute_capability = g_device_caps[id].cc;
  5861. int mmq_x, mmq_y, nwarps;
  5862. if (compute_capability >= CC_RDNA2) {
  5863. mmq_x = MMQ_X_Q4_1_RDNA2;
  5864. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5865. nwarps = NWARPS_Q4_1_RDNA2;
  5866. } else if (compute_capability >= CC_OFFSET_AMD) {
  5867. mmq_x = MMQ_X_Q4_1_RDNA1;
  5868. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5869. nwarps = NWARPS_Q4_1_RDNA1;
  5870. } else if (compute_capability >= CC_VOLTA) {
  5871. mmq_x = MMQ_X_Q4_1_AMPERE;
  5872. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5873. nwarps = NWARPS_Q4_1_AMPERE;
  5874. } else if (compute_capability >= MIN_CC_DP4A) {
  5875. mmq_x = MMQ_X_Q4_1_PASCAL;
  5876. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5877. nwarps = NWARPS_Q4_1_PASCAL;
  5878. } else {
  5879. GGML_ASSERT(false);
  5880. }
  5881. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5882. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5883. const dim3 block_nums(block_num_x, block_num_y, 1);
  5884. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5885. if (nrows_x % mmq_y == 0) {
  5886. const bool need_check = false;
  5887. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5888. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5889. } else {
  5890. const bool need_check = true;
  5891. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5892. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5893. }
  5894. }
  5895. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5896. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5897. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5898. int id;
  5899. CUDA_CHECK(cudaGetDevice(&id));
  5900. const int compute_capability = g_device_caps[id].cc;
  5901. int mmq_x, mmq_y, nwarps;
  5902. if (compute_capability >= CC_RDNA2) {
  5903. mmq_x = MMQ_X_Q5_0_RDNA2;
  5904. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5905. nwarps = NWARPS_Q5_0_RDNA2;
  5906. } else if (compute_capability >= CC_OFFSET_AMD) {
  5907. mmq_x = MMQ_X_Q5_0_RDNA1;
  5908. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5909. nwarps = NWARPS_Q5_0_RDNA1;
  5910. } else if (compute_capability >= CC_VOLTA) {
  5911. mmq_x = MMQ_X_Q5_0_AMPERE;
  5912. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5913. nwarps = NWARPS_Q5_0_AMPERE;
  5914. } else if (compute_capability >= MIN_CC_DP4A) {
  5915. mmq_x = MMQ_X_Q5_0_PASCAL;
  5916. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5917. nwarps = NWARPS_Q5_0_PASCAL;
  5918. } else {
  5919. GGML_ASSERT(false);
  5920. }
  5921. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5922. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5923. const dim3 block_nums(block_num_x, block_num_y, 1);
  5924. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5925. if (nrows_x % mmq_y == 0) {
  5926. const bool need_check = false;
  5927. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5928. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5929. } else {
  5930. const bool need_check = true;
  5931. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5932. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5933. }
  5934. }
  5935. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5936. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5937. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5938. int id;
  5939. CUDA_CHECK(cudaGetDevice(&id));
  5940. const int compute_capability = g_device_caps[id].cc;
  5941. int mmq_x, mmq_y, nwarps;
  5942. if (compute_capability >= CC_RDNA2) {
  5943. mmq_x = MMQ_X_Q5_1_RDNA2;
  5944. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5945. nwarps = NWARPS_Q5_1_RDNA2;
  5946. } else if (compute_capability >= CC_OFFSET_AMD) {
  5947. mmq_x = MMQ_X_Q5_1_RDNA1;
  5948. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5949. nwarps = NWARPS_Q5_1_RDNA1;
  5950. } else if (compute_capability >= CC_VOLTA) {
  5951. mmq_x = MMQ_X_Q5_1_AMPERE;
  5952. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5953. nwarps = NWARPS_Q5_1_AMPERE;
  5954. } else if (compute_capability >= MIN_CC_DP4A) {
  5955. mmq_x = MMQ_X_Q5_1_PASCAL;
  5956. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5957. nwarps = NWARPS_Q5_1_PASCAL;
  5958. } else {
  5959. GGML_ASSERT(false);
  5960. }
  5961. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5962. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5963. const dim3 block_nums(block_num_x, block_num_y, 1);
  5964. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5965. if (nrows_x % mmq_y == 0) {
  5966. const bool need_check = false;
  5967. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5968. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5969. } else {
  5970. const bool need_check = true;
  5971. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5972. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5973. }
  5974. }
  5975. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5976. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5977. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5978. int id;
  5979. CUDA_CHECK(cudaGetDevice(&id));
  5980. const int compute_capability = g_device_caps[id].cc;
  5981. int mmq_x, mmq_y, nwarps;
  5982. if (compute_capability >= CC_RDNA2) {
  5983. mmq_x = MMQ_X_Q8_0_RDNA2;
  5984. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5985. nwarps = NWARPS_Q8_0_RDNA2;
  5986. } else if (compute_capability >= CC_OFFSET_AMD) {
  5987. mmq_x = MMQ_X_Q8_0_RDNA1;
  5988. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5989. nwarps = NWARPS_Q8_0_RDNA1;
  5990. } else if (compute_capability >= CC_VOLTA) {
  5991. mmq_x = MMQ_X_Q8_0_AMPERE;
  5992. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5993. nwarps = NWARPS_Q8_0_AMPERE;
  5994. } else if (compute_capability >= MIN_CC_DP4A) {
  5995. mmq_x = MMQ_X_Q8_0_PASCAL;
  5996. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5997. nwarps = NWARPS_Q8_0_PASCAL;
  5998. } else {
  5999. GGML_ASSERT(false);
  6000. }
  6001. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6002. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6003. const dim3 block_nums(block_num_x, block_num_y, 1);
  6004. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6005. if (nrows_x % mmq_y == 0) {
  6006. const bool need_check = false;
  6007. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6008. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6009. } else {
  6010. const bool need_check = true;
  6011. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6012. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6013. }
  6014. }
  6015. static void ggml_mul_mat_q2_K_q8_1_cuda(
  6016. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6017. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6018. int id;
  6019. CUDA_CHECK(cudaGetDevice(&id));
  6020. const int compute_capability = g_device_caps[id].cc;
  6021. int mmq_x, mmq_y, nwarps;
  6022. if (compute_capability >= CC_RDNA2) {
  6023. mmq_x = MMQ_X_Q2_K_RDNA2;
  6024. mmq_y = MMQ_Y_Q2_K_RDNA2;
  6025. nwarps = NWARPS_Q2_K_RDNA2;
  6026. } else if (compute_capability >= CC_OFFSET_AMD) {
  6027. mmq_x = MMQ_X_Q2_K_RDNA1;
  6028. mmq_y = MMQ_Y_Q2_K_RDNA1;
  6029. nwarps = NWARPS_Q2_K_RDNA1;
  6030. } else if (compute_capability >= CC_VOLTA) {
  6031. mmq_x = MMQ_X_Q2_K_AMPERE;
  6032. mmq_y = MMQ_Y_Q2_K_AMPERE;
  6033. nwarps = NWARPS_Q2_K_AMPERE;
  6034. } else if (compute_capability >= MIN_CC_DP4A) {
  6035. mmq_x = MMQ_X_Q2_K_PASCAL;
  6036. mmq_y = MMQ_Y_Q2_K_PASCAL;
  6037. nwarps = NWARPS_Q2_K_PASCAL;
  6038. } else {
  6039. GGML_ASSERT(false);
  6040. }
  6041. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6042. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6043. const dim3 block_nums(block_num_x, block_num_y, 1);
  6044. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6045. if (nrows_x % mmq_y == 0) {
  6046. const bool need_check = false;
  6047. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6048. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6049. } else {
  6050. const bool need_check = true;
  6051. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6052. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6053. }
  6054. }
  6055. static void ggml_mul_mat_q3_K_q8_1_cuda(
  6056. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6057. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6058. #if QK_K == 256
  6059. int id;
  6060. CUDA_CHECK(cudaGetDevice(&id));
  6061. const int compute_capability = g_device_caps[id].cc;
  6062. int mmq_x, mmq_y, nwarps;
  6063. if (compute_capability >= CC_RDNA2) {
  6064. mmq_x = MMQ_X_Q3_K_RDNA2;
  6065. mmq_y = MMQ_Y_Q3_K_RDNA2;
  6066. nwarps = NWARPS_Q3_K_RDNA2;
  6067. } else if (compute_capability >= CC_OFFSET_AMD) {
  6068. mmq_x = MMQ_X_Q3_K_RDNA1;
  6069. mmq_y = MMQ_Y_Q3_K_RDNA1;
  6070. nwarps = NWARPS_Q3_K_RDNA1;
  6071. } else if (compute_capability >= CC_VOLTA) {
  6072. mmq_x = MMQ_X_Q3_K_AMPERE;
  6073. mmq_y = MMQ_Y_Q3_K_AMPERE;
  6074. nwarps = NWARPS_Q3_K_AMPERE;
  6075. } else if (compute_capability >= MIN_CC_DP4A) {
  6076. mmq_x = MMQ_X_Q3_K_PASCAL;
  6077. mmq_y = MMQ_Y_Q3_K_PASCAL;
  6078. nwarps = NWARPS_Q3_K_PASCAL;
  6079. } else {
  6080. GGML_ASSERT(false);
  6081. }
  6082. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6083. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6084. const dim3 block_nums(block_num_x, block_num_y, 1);
  6085. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6086. if (nrows_x % mmq_y == 0) {
  6087. const bool need_check = false;
  6088. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6089. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6090. } else {
  6091. const bool need_check = true;
  6092. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6093. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6094. }
  6095. #endif
  6096. }
  6097. static void ggml_mul_mat_q4_K_q8_1_cuda(
  6098. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6099. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6100. int id;
  6101. CUDA_CHECK(cudaGetDevice(&id));
  6102. const int compute_capability = g_device_caps[id].cc;
  6103. int mmq_x, mmq_y, nwarps;
  6104. if (compute_capability >= CC_RDNA2) {
  6105. mmq_x = MMQ_X_Q4_K_RDNA2;
  6106. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6107. nwarps = NWARPS_Q4_K_RDNA2;
  6108. } else if (compute_capability >= CC_OFFSET_AMD) {
  6109. mmq_x = MMQ_X_Q4_K_RDNA1;
  6110. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6111. nwarps = NWARPS_Q4_K_RDNA1;
  6112. } else if (compute_capability >= CC_VOLTA) {
  6113. mmq_x = MMQ_X_Q4_K_AMPERE;
  6114. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6115. nwarps = NWARPS_Q4_K_AMPERE;
  6116. } else if (compute_capability >= MIN_CC_DP4A) {
  6117. mmq_x = MMQ_X_Q4_K_PASCAL;
  6118. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6119. nwarps = NWARPS_Q4_K_PASCAL;
  6120. } else {
  6121. GGML_ASSERT(false);
  6122. }
  6123. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6124. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6125. const dim3 block_nums(block_num_x, block_num_y, 1);
  6126. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6127. if (nrows_x % mmq_y == 0) {
  6128. const bool need_check = false;
  6129. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6130. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6131. } else {
  6132. const bool need_check = true;
  6133. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6134. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6135. }
  6136. }
  6137. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6138. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6139. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6140. int id;
  6141. CUDA_CHECK(cudaGetDevice(&id));
  6142. const int compute_capability = g_device_caps[id].cc;
  6143. int mmq_x, mmq_y, nwarps;
  6144. if (compute_capability >= CC_RDNA2) {
  6145. mmq_x = MMQ_X_Q5_K_RDNA2;
  6146. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6147. nwarps = NWARPS_Q5_K_RDNA2;
  6148. } else if (compute_capability >= CC_OFFSET_AMD) {
  6149. mmq_x = MMQ_X_Q5_K_RDNA1;
  6150. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6151. nwarps = NWARPS_Q5_K_RDNA1;
  6152. } else if (compute_capability >= CC_VOLTA) {
  6153. mmq_x = MMQ_X_Q5_K_AMPERE;
  6154. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6155. nwarps = NWARPS_Q5_K_AMPERE;
  6156. } else if (compute_capability >= MIN_CC_DP4A) {
  6157. mmq_x = MMQ_X_Q5_K_PASCAL;
  6158. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6159. nwarps = NWARPS_Q5_K_PASCAL;
  6160. } else {
  6161. GGML_ASSERT(false);
  6162. }
  6163. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6164. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6165. const dim3 block_nums(block_num_x, block_num_y, 1);
  6166. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6167. if (nrows_x % mmq_y == 0) {
  6168. const bool need_check = false;
  6169. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6170. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6171. } else {
  6172. const bool need_check = true;
  6173. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6174. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6175. }
  6176. }
  6177. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6178. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6179. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6180. int id;
  6181. CUDA_CHECK(cudaGetDevice(&id));
  6182. const int compute_capability = g_device_caps[id].cc;
  6183. int mmq_x, mmq_y, nwarps;
  6184. if (compute_capability >= CC_RDNA2) {
  6185. mmq_x = MMQ_X_Q6_K_RDNA2;
  6186. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6187. nwarps = NWARPS_Q6_K_RDNA2;
  6188. } else if (compute_capability >= CC_OFFSET_AMD) {
  6189. mmq_x = MMQ_X_Q6_K_RDNA1;
  6190. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6191. nwarps = NWARPS_Q6_K_RDNA1;
  6192. } else if (compute_capability >= CC_VOLTA) {
  6193. mmq_x = MMQ_X_Q6_K_AMPERE;
  6194. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6195. nwarps = NWARPS_Q6_K_AMPERE;
  6196. } else if (compute_capability >= MIN_CC_DP4A) {
  6197. mmq_x = MMQ_X_Q6_K_PASCAL;
  6198. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6199. nwarps = NWARPS_Q6_K_PASCAL;
  6200. } else {
  6201. GGML_ASSERT(false);
  6202. }
  6203. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6204. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6205. const dim3 block_nums(block_num_x, block_num_y, 1);
  6206. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6207. if (nrows_x % mmq_y == 0) {
  6208. const bool need_check = false;
  6209. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6210. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6211. } else {
  6212. const bool need_check = true;
  6213. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6214. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6215. }
  6216. }
  6217. static void ggml_mul_mat_p021_f16_f32_cuda(
  6218. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6219. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6220. const dim3 block_nums(1, nrows_x, nchannels_y);
  6221. const dim3 block_dims(WARP_SIZE, 1, 1);
  6222. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6223. }
  6224. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6225. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6226. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6227. const dim3 block_nums(1, nrows_x, nchannels_y);
  6228. const dim3 block_dims(WARP_SIZE, 1, 1);
  6229. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6230. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6231. }
  6232. static void ggml_cpy_f16_f32_cuda(
  6233. const char * cx, char * cdst, const int ne,
  6234. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6235. 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) {
  6236. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6237. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6238. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6239. }
  6240. static void ggml_cpy_f32_f32_cuda(
  6241. const char * cx, char * cdst, const int ne,
  6242. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6243. 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) {
  6244. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6245. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6246. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6247. }
  6248. static void ggml_cpy_f32_f16_cuda(
  6249. const char * cx, char * cdst, const int ne,
  6250. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6251. 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) {
  6252. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6253. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6254. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6255. }
  6256. static void ggml_cpy_f32_q8_0_cuda(
  6257. const char * cx, char * cdst, const int ne,
  6258. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6259. 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) {
  6260. GGML_ASSERT(ne % QK8_0 == 0);
  6261. const int num_blocks = ne / QK8_0;
  6262. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6263. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6264. }
  6265. static void ggml_cpy_f32_q4_0_cuda(
  6266. const char * cx, char * cdst, const int ne,
  6267. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6268. 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) {
  6269. GGML_ASSERT(ne % QK4_0 == 0);
  6270. const int num_blocks = ne / QK4_0;
  6271. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6272. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6273. }
  6274. static void ggml_cpy_f32_q4_1_cuda(
  6275. const char * cx, char * cdst, const int ne,
  6276. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6277. 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) {
  6278. GGML_ASSERT(ne % QK4_1 == 0);
  6279. const int num_blocks = ne / QK4_1;
  6280. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6281. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6282. }
  6283. static void ggml_cpy_f16_f16_cuda(
  6284. const char * cx, char * cdst, const int ne,
  6285. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6286. 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) {
  6287. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6288. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6289. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6290. }
  6291. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  6292. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  6293. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  6294. }
  6295. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  6296. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  6297. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  6298. }
  6299. template<typename T>
  6300. static void rope_cuda(
  6301. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6302. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6303. ) {
  6304. GGML_ASSERT(ncols % 2 == 0);
  6305. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6306. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6307. const dim3 block_nums(nrows, num_blocks_x, 1);
  6308. if (pos == nullptr) {
  6309. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6310. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6311. );
  6312. } else {
  6313. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6314. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6315. );
  6316. }
  6317. }
  6318. template<typename T>
  6319. static void rope_neox_cuda(
  6320. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6321. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6322. ) {
  6323. GGML_ASSERT(ncols % 2 == 0);
  6324. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6325. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6326. const dim3 block_nums(nrows, num_blocks_x, 1);
  6327. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6328. const float inv_ndims = -1.0f / n_dims;
  6329. if (pos == nullptr) {
  6330. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6331. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6332. theta_scale, inv_ndims
  6333. );
  6334. } else {
  6335. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6336. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6337. theta_scale, inv_ndims
  6338. );
  6339. }
  6340. }
  6341. static void rope_glm_f32_cuda(
  6342. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6343. float freq_base, int n_ctx, cudaStream_t stream
  6344. ) {
  6345. GGML_ASSERT(ncols % 4 == 0);
  6346. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6347. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6348. const dim3 block_nums(num_blocks_x, nrows, 1);
  6349. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6350. }
  6351. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6352. const int k_rows, const int n_heads_log2_floor, const float m0,
  6353. const float m1, cudaStream_t stream) {
  6354. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6355. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6356. const dim3 block_nums(num_blocks_x, nrows, 1);
  6357. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6358. }
  6359. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6360. const dim3 block_dims(WARP_SIZE, 1, 1);
  6361. const dim3 block_nums(nrows, 1, 1);
  6362. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6363. }
  6364. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6365. // bitonic sort requires ncols to be power of 2
  6366. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6367. const dim3 block_dims(ncols, 1, 1);
  6368. const dim3 block_nums(1, nrows, 1);
  6369. if (order == GGML_SORT_ASC) {
  6370. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6371. } else if (order == GGML_SORT_DESC) {
  6372. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6373. } else {
  6374. GGML_ASSERT(false);
  6375. }
  6376. }
  6377. 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) {
  6378. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6379. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6380. const dim3 block_nums(nrows_x, block_num_x, 1);
  6381. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6382. }
  6383. 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) {
  6384. int nth = WARP_SIZE;
  6385. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6386. const dim3 block_dims(nth, 1, 1);
  6387. const dim3 block_nums(nrows_x, 1, 1);
  6388. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6389. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6390. const uint32_t n_head_kv = nrows_x/nrows_y;
  6391. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6392. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6393. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6394. if (shmem < g_device_caps[g_main_device].smpb) {
  6395. switch (ncols_x) {
  6396. case 32:
  6397. 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);
  6398. break;
  6399. case 64:
  6400. 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);
  6401. break;
  6402. case 128:
  6403. 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);
  6404. break;
  6405. case 256:
  6406. 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);
  6407. break;
  6408. case 512:
  6409. 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);
  6410. break;
  6411. case 1024:
  6412. 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);
  6413. break;
  6414. case 2048:
  6415. 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);
  6416. break;
  6417. case 4096:
  6418. 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);
  6419. break;
  6420. default:
  6421. 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);
  6422. break;
  6423. }
  6424. } else {
  6425. const size_t shmem_low = WARP_SIZE*sizeof(float);
  6426. 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);
  6427. }
  6428. }
  6429. template <typename T>
  6430. static void im2col_cuda(const float* x, T* dst,
  6431. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  6432. int batch, int batch_offset, int offset_delta,
  6433. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  6434. const int parallel_elements = OW * KW * KH;
  6435. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  6436. dim3 block_nums(num_blocks, OH, batch * IC);
  6437. 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);
  6438. }
  6439. // buffer pool for cuda
  6440. #define MAX_CUDA_BUFFERS 256
  6441. struct scoped_spin_lock {
  6442. std::atomic_flag& lock;
  6443. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  6444. while (lock.test_and_set(std::memory_order_acquire)) {
  6445. ; // spin
  6446. }
  6447. }
  6448. ~scoped_spin_lock() {
  6449. lock.clear(std::memory_order_release);
  6450. }
  6451. scoped_spin_lock(const scoped_spin_lock&) = delete;
  6452. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  6453. };
  6454. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  6455. // #define DEBUG_CUDA_MALLOC
  6456. struct ggml_cuda_buffer {
  6457. void * ptr = nullptr;
  6458. size_t size = 0;
  6459. };
  6460. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  6461. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  6462. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  6463. scoped_spin_lock lock(g_cuda_pool_lock);
  6464. #ifdef DEBUG_CUDA_MALLOC
  6465. int nnz = 0;
  6466. size_t max_size = 0;
  6467. #endif
  6468. size_t best_diff = 1ull << 36;
  6469. int ibest = -1;
  6470. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6471. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6472. if (b.ptr != nullptr) {
  6473. #ifdef DEBUG_CUDA_MALLOC
  6474. ++nnz;
  6475. if (b.size > max_size) max_size = b.size;
  6476. #endif
  6477. if (b.size >= size) {
  6478. size_t diff = b.size - size;
  6479. if (diff < best_diff) {
  6480. best_diff = diff;
  6481. ibest = i;
  6482. if (!best_diff) {
  6483. void * ptr = b.ptr;
  6484. *actual_size = b.size;
  6485. b.ptr = nullptr;
  6486. b.size = 0;
  6487. return ptr;
  6488. }
  6489. }
  6490. }
  6491. }
  6492. }
  6493. if (ibest >= 0) {
  6494. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  6495. void * ptr = b.ptr;
  6496. *actual_size = b.size;
  6497. b.ptr = nullptr;
  6498. b.size = 0;
  6499. return ptr;
  6500. }
  6501. void * ptr;
  6502. size_t look_ahead_size = (size_t) (1.05 * size);
  6503. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  6504. ggml_cuda_set_device(device);
  6505. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  6506. *actual_size = look_ahead_size;
  6507. g_cuda_pool_size[device] += look_ahead_size;
  6508. #ifdef DEBUG_CUDA_MALLOC
  6509. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  6510. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  6511. #endif
  6512. return ptr;
  6513. }
  6514. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  6515. scoped_spin_lock lock(g_cuda_pool_lock);
  6516. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6517. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6518. if (b.ptr == nullptr) {
  6519. b.ptr = ptr;
  6520. b.size = size;
  6521. return;
  6522. }
  6523. }
  6524. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  6525. ggml_cuda_set_device(device);
  6526. CUDA_CHECK(cudaFree(ptr));
  6527. g_cuda_pool_size[device] -= size;
  6528. }
  6529. #if !defined(GGML_USE_HIPBLAS)
  6530. // pool with virtual memory
  6531. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  6532. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  6533. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  6534. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  6535. scoped_spin_lock lock(g_cuda_pool_lock);
  6536. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  6537. const size_t alignment = 128;
  6538. size = alignment * ((size + alignment - 1) / alignment);
  6539. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  6540. if (size > avail) {
  6541. // round up to the next multiple of the granularity
  6542. size_t reserve_size = size - avail;
  6543. const size_t granularity = g_device_caps[device].vmm_granularity;
  6544. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  6545. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  6546. // allocate more physical memory
  6547. CUmemAllocationProp prop = {};
  6548. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6549. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6550. prop.location.id = device;
  6551. CUmemGenericAllocationHandle handle;
  6552. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  6553. // reserve virtual address space (if not already reserved)
  6554. if (g_cuda_pool_addr[device] == 0) {
  6555. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  6556. }
  6557. // map at the end of the pool
  6558. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  6559. // the memory allocation handle is no longer needed after mapping
  6560. CU_CHECK(cuMemRelease(handle));
  6561. // set access
  6562. CUmemAccessDesc access = {};
  6563. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6564. access.location.id = device;
  6565. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  6566. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  6567. // add to the pool
  6568. g_cuda_pool_size[device] += reserve_size;
  6569. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  6570. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  6571. // (unsigned long long) (reserve_size/1024/1024));
  6572. }
  6573. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  6574. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  6575. *actual_size = size;
  6576. g_cuda_pool_used[device] += size;
  6577. #ifdef DEBUG_CUDA_MALLOC
  6578. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  6579. #endif
  6580. return ptr;
  6581. }
  6582. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  6583. scoped_spin_lock lock(g_cuda_pool_lock);
  6584. #ifdef DEBUG_CUDA_MALLOC
  6585. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  6586. #endif
  6587. g_cuda_pool_used[device] -= size;
  6588. // all deallocations must be in reverse order of the allocations
  6589. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  6590. }
  6591. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  6592. if (g_device_caps[device].vmm) {
  6593. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  6594. } else {
  6595. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  6596. }
  6597. }
  6598. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  6599. if (g_device_caps[device].vmm) {
  6600. ggml_cuda_pool_free_vmm(device, ptr, size);
  6601. } else {
  6602. ggml_cuda_pool_free_leg(device, ptr, size);
  6603. }
  6604. }
  6605. #else
  6606. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  6607. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  6608. #endif // !defined(GGML_USE_HIPBLAS)
  6609. template<typename T>
  6610. struct cuda_pool_alloc {
  6611. int device = -1;
  6612. T * ptr = nullptr;
  6613. size_t actual_size = 0;
  6614. // size is in number of elements
  6615. T * alloc(size_t size) {
  6616. GGML_ASSERT(ptr == nullptr);
  6617. CUDA_CHECK(cudaGetDevice(&device));
  6618. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  6619. return ptr;
  6620. }
  6621. cuda_pool_alloc(size_t size) {
  6622. alloc(size);
  6623. }
  6624. ~cuda_pool_alloc() {
  6625. if (ptr != nullptr) {
  6626. ggml_cuda_pool_free(device, ptr, actual_size);
  6627. }
  6628. }
  6629. T * get() {
  6630. return ptr;
  6631. }
  6632. cuda_pool_alloc() = default;
  6633. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  6634. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  6635. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  6636. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  6637. };
  6638. static bool g_cublas_loaded = false;
  6639. GGML_CALL bool ggml_cublas_loaded(void) {
  6640. return g_cublas_loaded;
  6641. }
  6642. GGML_CALL void ggml_init_cublas() {
  6643. static bool initialized = false;
  6644. if (!initialized) {
  6645. #ifdef __HIP_PLATFORM_AMD__
  6646. // Workaround for a rocBLAS bug when using multiple graphics cards:
  6647. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  6648. rocblas_initialize();
  6649. CUDA_CHECK(cudaDeviceSynchronize());
  6650. #endif
  6651. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  6652. initialized = true;
  6653. g_cublas_loaded = false;
  6654. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  6655. return;
  6656. }
  6657. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  6658. int64_t total_vram = 0;
  6659. #if defined(GGML_CUDA_FORCE_MMQ)
  6660. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  6661. #else
  6662. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  6663. #endif
  6664. #if defined(CUDA_USE_TENSOR_CORES)
  6665. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  6666. #else
  6667. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  6668. #endif
  6669. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  6670. for (int id = 0; id < g_device_count; ++id) {
  6671. int device_vmm = 0;
  6672. #if !defined(GGML_USE_HIPBLAS)
  6673. CUdevice device;
  6674. CU_CHECK(cuDeviceGet(&device, id));
  6675. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  6676. if (device_vmm) {
  6677. CUmemAllocationProp alloc_prop = {};
  6678. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6679. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6680. alloc_prop.location.id = id;
  6681. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  6682. }
  6683. #endif // !defined(GGML_USE_HIPBLAS)
  6684. g_device_caps[id].vmm = !!device_vmm;
  6685. cudaDeviceProp prop;
  6686. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  6687. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  6688. g_default_tensor_split[id] = total_vram;
  6689. total_vram += prop.totalGlobalMem;
  6690. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6691. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  6692. #else
  6693. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  6694. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6695. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  6696. }
  6697. for (int id = 0; id < g_device_count; ++id) {
  6698. g_default_tensor_split[id] /= total_vram;
  6699. }
  6700. for (int id = 0; id < g_device_count; ++id) {
  6701. ggml_cuda_set_device(id);
  6702. // create cuda streams
  6703. for (int is = 0; is < MAX_STREAMS; ++is) {
  6704. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  6705. }
  6706. // create cublas handle
  6707. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  6708. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  6709. }
  6710. // configure logging to stdout
  6711. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  6712. initialized = true;
  6713. g_cublas_loaded = true;
  6714. }
  6715. }
  6716. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  6717. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6718. return nullptr;
  6719. }
  6720. void * ptr = nullptr;
  6721. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6722. if (err != cudaSuccess) {
  6723. // clear the error
  6724. cudaGetLastError();
  6725. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6726. size/1024.0/1024.0, cudaGetErrorString(err));
  6727. return nullptr;
  6728. }
  6729. return ptr;
  6730. }
  6731. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  6732. CUDA_CHECK(cudaFreeHost(ptr));
  6733. }
  6734. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6735. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6736. cudaMemcpyKind kind;
  6737. char * src_ptr;
  6738. if (src->backend == GGML_BACKEND_CPU) {
  6739. kind = cudaMemcpyHostToDevice;
  6740. src_ptr = (char *) src->data;
  6741. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  6742. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6743. kind = cudaMemcpyDeviceToDevice;
  6744. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6745. int id;
  6746. CUDA_CHECK(cudaGetDevice(&id));
  6747. src_ptr = (char *) extra->data_device[id];
  6748. } else {
  6749. GGML_ASSERT(false);
  6750. }
  6751. char * dst_ptr = (char *) dst;
  6752. const int64_t ne0 = src->ne[0];
  6753. const int64_t nb0 = src->nb[0];
  6754. const int64_t nb1 = src->nb[1];
  6755. const int64_t nb2 = src->nb[2];
  6756. const int64_t nb3 = src->nb[3];
  6757. const enum ggml_type type = src->type;
  6758. const int64_t ts = ggml_type_size(type);
  6759. const int64_t bs = ggml_blck_size(type);
  6760. int64_t i1_diff = i1_high - i1_low;
  6761. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6762. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6763. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6764. } else if (nb0 == ts) {
  6765. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6766. } else {
  6767. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6768. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6769. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6770. // pretend the row is a matrix with cols=1
  6771. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6772. if (r != cudaSuccess) return r;
  6773. }
  6774. return cudaSuccess;
  6775. }
  6776. }
  6777. static void ggml_cuda_op_get_rows(
  6778. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6779. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6780. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6781. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6782. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6783. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6784. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6785. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6786. switch (src0->type) {
  6787. case GGML_TYPE_F16:
  6788. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6789. break;
  6790. case GGML_TYPE_F32:
  6791. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6792. break;
  6793. case GGML_TYPE_Q4_0:
  6794. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6795. break;
  6796. case GGML_TYPE_Q4_1:
  6797. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6798. break;
  6799. case GGML_TYPE_Q5_0:
  6800. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6801. break;
  6802. case GGML_TYPE_Q5_1:
  6803. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6804. break;
  6805. case GGML_TYPE_Q8_0:
  6806. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6807. break;
  6808. default:
  6809. // TODO: k-quants
  6810. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6811. GGML_ASSERT(false);
  6812. break;
  6813. }
  6814. }
  6815. template<class op>
  6816. static void ggml_cuda_op_bin_bcast(
  6817. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6818. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6819. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6820. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6821. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6822. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6823. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6824. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6825. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6826. } else {
  6827. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6828. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6829. GGML_ASSERT(false);
  6830. }
  6831. }
  6832. static void ggml_cuda_op_repeat(
  6833. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6834. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6835. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6836. (void) src1;
  6837. (void) src1_d;
  6838. }
  6839. static void ggml_cuda_op_add(
  6840. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6841. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6842. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6843. }
  6844. static void ggml_cuda_op_acc(
  6845. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6846. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6847. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6848. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6849. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6850. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6851. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6852. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6853. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6854. int offset = dst->op_params[3] / 4; // offset in bytes
  6855. 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);
  6856. (void) dst;
  6857. }
  6858. static void ggml_cuda_op_mul(
  6859. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6860. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6861. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6862. }
  6863. static void ggml_cuda_op_div(
  6864. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6865. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6866. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6867. }
  6868. static void ggml_cuda_op_gelu(
  6869. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6870. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6871. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6872. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6873. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6874. (void) src1;
  6875. (void) dst;
  6876. (void) src1_dd;
  6877. }
  6878. static void ggml_cuda_op_silu(
  6879. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6880. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6881. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6882. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6883. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6884. (void) src1;
  6885. (void) dst;
  6886. (void) src1_dd;
  6887. }
  6888. static void ggml_cuda_op_gelu_quick(
  6889. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6890. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6891. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6892. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6893. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6894. (void) src1;
  6895. (void) dst;
  6896. (void) src1_dd;
  6897. }
  6898. static void ggml_cuda_op_tanh(
  6899. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6900. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6901. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6902. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6903. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6904. (void) src1;
  6905. (void) dst;
  6906. (void) src1_dd;
  6907. }
  6908. static void ggml_cuda_op_relu(
  6909. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6910. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6911. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6912. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6913. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6914. (void) src1;
  6915. (void) dst;
  6916. (void) src1_dd;
  6917. }
  6918. static void ggml_cuda_op_hardsigmoid(
  6919. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6920. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6921. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6922. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6923. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6924. (void) src1;
  6925. (void) dst;
  6926. (void) src1_dd;
  6927. }
  6928. static void ggml_cuda_op_hardswish(
  6929. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6930. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6931. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6932. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6933. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6934. (void) src1;
  6935. (void) dst;
  6936. (void) src1_dd;
  6937. }
  6938. static void ggml_cuda_op_leaky_relu(
  6939. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6940. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6941. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6942. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6943. float negative_slope;
  6944. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6945. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6946. (void) src1;
  6947. (void) dst;
  6948. (void) src1_dd;
  6949. }
  6950. static void ggml_cuda_op_sqr(
  6951. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6952. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6953. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6954. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6955. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6956. (void) src1;
  6957. (void) dst;
  6958. (void) src1_dd;
  6959. }
  6960. static void ggml_cuda_op_norm(
  6961. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6962. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6963. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6964. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6965. const int64_t ne00 = src0->ne[0];
  6966. const int64_t nrows = ggml_nrows(src0);
  6967. float eps;
  6968. memcpy(&eps, dst->op_params, sizeof(float));
  6969. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6970. (void) src1;
  6971. (void) dst;
  6972. (void) src1_dd;
  6973. }
  6974. static void ggml_cuda_op_group_norm(
  6975. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6976. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6977. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6978. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6979. int num_groups = dst->op_params[0];
  6980. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6981. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  6982. (void) src1;
  6983. (void) dst;
  6984. (void) src1_dd;
  6985. }
  6986. static void ggml_cuda_op_concat(
  6987. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6988. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6989. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6990. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6991. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6992. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6993. 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);
  6994. }
  6995. (void) src1;
  6996. (void) dst;
  6997. }
  6998. static void ggml_cuda_op_upscale(
  6999. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7000. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7001. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7002. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7003. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7004. const int scale_factor = dst->op_params[0];
  7005. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  7006. (void) src1;
  7007. (void) dst;
  7008. (void) src1_dd;
  7009. }
  7010. static void ggml_cuda_op_pad(
  7011. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7012. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7013. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7014. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7015. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7016. pad_f32_cuda(src0_dd, dst_dd,
  7017. src0->ne[0], src0->ne[1], src0->ne[2],
  7018. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  7019. (void) src1;
  7020. (void) dst;
  7021. (void) src1_dd;
  7022. }
  7023. static void ggml_cuda_op_rms_norm(
  7024. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7025. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7026. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7027. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7028. const int64_t ne00 = src0->ne[0];
  7029. const int64_t nrows = ggml_nrows(src0);
  7030. float eps;
  7031. memcpy(&eps, dst->op_params, sizeof(float));
  7032. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7033. (void) src1;
  7034. (void) dst;
  7035. (void) src1_dd;
  7036. }
  7037. static void ggml_cuda_op_mul_mat_q(
  7038. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7039. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7040. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7041. const int64_t ne00 = src0->ne[0];
  7042. const int64_t ne10 = src1->ne[0];
  7043. GGML_ASSERT(ne10 % QK8_1 == 0);
  7044. const int64_t ne0 = dst->ne[0];
  7045. const int64_t row_diff = row_high - row_low;
  7046. int id;
  7047. CUDA_CHECK(cudaGetDevice(&id));
  7048. // the main device has a larger memory buffer to hold the results from all GPUs
  7049. // nrows_dst == nrows of the matrix that the kernel writes into
  7050. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7051. switch (src0->type) {
  7052. case GGML_TYPE_Q4_0:
  7053. 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);
  7054. break;
  7055. case GGML_TYPE_Q4_1:
  7056. 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);
  7057. break;
  7058. case GGML_TYPE_Q5_0:
  7059. 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);
  7060. break;
  7061. case GGML_TYPE_Q5_1:
  7062. 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);
  7063. break;
  7064. case GGML_TYPE_Q8_0:
  7065. 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);
  7066. break;
  7067. case GGML_TYPE_Q2_K:
  7068. 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);
  7069. break;
  7070. case GGML_TYPE_Q3_K:
  7071. 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);
  7072. break;
  7073. case GGML_TYPE_Q4_K:
  7074. 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);
  7075. break;
  7076. case GGML_TYPE_Q5_K:
  7077. 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);
  7078. break;
  7079. case GGML_TYPE_Q6_K:
  7080. 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);
  7081. break;
  7082. default:
  7083. GGML_ASSERT(false);
  7084. break;
  7085. }
  7086. (void) src1;
  7087. (void) dst;
  7088. (void) src1_ddf_i;
  7089. }
  7090. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  7091. int64_t min_compute_capability = INT_MAX;
  7092. int64_t max_compute_capability = INT_MIN;
  7093. for (int id = 0; id < g_device_count; ++id) {
  7094. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  7095. if (min_compute_capability > g_device_caps[id].cc) {
  7096. min_compute_capability = g_device_caps[id].cc;
  7097. }
  7098. if (max_compute_capability < g_device_caps[id].cc) {
  7099. max_compute_capability = g_device_caps[id].cc;
  7100. }
  7101. }
  7102. }
  7103. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7104. switch(type) {
  7105. case GGML_TYPE_Q4_0:
  7106. case GGML_TYPE_Q4_1:
  7107. case GGML_TYPE_Q5_0:
  7108. case GGML_TYPE_Q5_1:
  7109. case GGML_TYPE_Q8_0:
  7110. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7111. case GGML_TYPE_F16:
  7112. case GGML_TYPE_F32:
  7113. return 1;
  7114. case GGML_TYPE_Q2_K:
  7115. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  7116. case GGML_TYPE_Q3_K:
  7117. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  7118. case GGML_TYPE_Q4_K:
  7119. case GGML_TYPE_Q5_K:
  7120. case GGML_TYPE_Q6_K:
  7121. case GGML_TYPE_IQ2_XXS:
  7122. case GGML_TYPE_IQ2_XS:
  7123. case GGML_TYPE_IQ3_XXS:
  7124. case GGML_TYPE_IQ1_S:
  7125. case GGML_TYPE_IQ4_NL:
  7126. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7127. default:
  7128. GGML_ASSERT(false);
  7129. }
  7130. #else
  7131. switch(type) {
  7132. case GGML_TYPE_Q4_0:
  7133. case GGML_TYPE_Q4_1:
  7134. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7135. case GGML_TYPE_Q5_0:
  7136. case GGML_TYPE_Q5_1:
  7137. case GGML_TYPE_Q8_0:
  7138. return 64;
  7139. case GGML_TYPE_F16:
  7140. case GGML_TYPE_F32:
  7141. return 1;
  7142. case GGML_TYPE_Q2_K:
  7143. case GGML_TYPE_Q3_K:
  7144. case GGML_TYPE_Q4_K:
  7145. case GGML_TYPE_Q5_K:
  7146. case GGML_TYPE_IQ2_XXS:
  7147. case GGML_TYPE_IQ2_XS:
  7148. case GGML_TYPE_IQ3_XXS:
  7149. case GGML_TYPE_IQ1_S:
  7150. case GGML_TYPE_IQ4_NL:
  7151. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7152. case GGML_TYPE_Q6_K:
  7153. return 64;
  7154. default:
  7155. GGML_ASSERT(false);
  7156. }
  7157. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7158. }
  7159. 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) {
  7160. const int64_t nrows = ggml_nrows(tensor);
  7161. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  7162. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  7163. *row_low -= *row_low % rounding;
  7164. if (id == g_device_count - 1) {
  7165. *row_high = nrows;
  7166. } else {
  7167. *row_high = nrows*tensor_split[id + 1];
  7168. *row_high -= *row_high % rounding;
  7169. }
  7170. }
  7171. static void ggml_cuda_op_mul_mat_vec_q(
  7172. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7173. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7174. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7175. const int64_t ne00 = src0->ne[0];
  7176. const int64_t row_diff = row_high - row_low;
  7177. const int64_t ne10 = src1->ne[0];
  7178. GGML_ASSERT(ne10 % QK8_1 == 0);
  7179. const int64_t ne0 = dst->ne[0];
  7180. int id;
  7181. CUDA_CHECK(cudaGetDevice(&id));
  7182. // the main device has a larger memory buffer to hold the results from all GPUs
  7183. // nrows_dst == nrows of the matrix that the kernel writes into
  7184. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7185. switch (src0->type) {
  7186. case GGML_TYPE_Q4_0:
  7187. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7188. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7189. break;
  7190. case GGML_TYPE_Q4_1:
  7191. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7192. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7193. break;
  7194. case GGML_TYPE_Q5_0:
  7195. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7196. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7197. break;
  7198. case GGML_TYPE_Q5_1:
  7199. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7200. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7201. break;
  7202. case GGML_TYPE_Q8_0:
  7203. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7204. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7205. break;
  7206. case GGML_TYPE_Q2_K:
  7207. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7208. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7209. break;
  7210. case GGML_TYPE_Q3_K:
  7211. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7212. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7213. break;
  7214. case GGML_TYPE_Q4_K:
  7215. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7216. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7217. break;
  7218. case GGML_TYPE_Q5_K:
  7219. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7220. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7221. break;
  7222. case GGML_TYPE_Q6_K:
  7223. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7224. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7225. break;
  7226. case GGML_TYPE_IQ2_XXS:
  7227. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7228. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7229. break;
  7230. case GGML_TYPE_IQ2_XS:
  7231. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7232. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7233. break;
  7234. case GGML_TYPE_IQ3_XXS:
  7235. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7236. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7237. break;
  7238. case GGML_TYPE_IQ1_S:
  7239. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7240. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7241. break;
  7242. case GGML_TYPE_IQ4_NL:
  7243. mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
  7244. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7245. break;
  7246. default:
  7247. GGML_ASSERT(false);
  7248. break;
  7249. }
  7250. (void) src1;
  7251. (void) dst;
  7252. (void) src1_ddf_i;
  7253. (void) src1_ncols;
  7254. (void) src1_padded_row_size;
  7255. }
  7256. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7257. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7258. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7259. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7260. const int64_t ne00 = src0->ne[0];
  7261. const int64_t row_diff = row_high - row_low;
  7262. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7263. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7264. #ifdef GGML_CUDA_F16
  7265. cuda_pool_alloc<half> src1_dfloat_a;
  7266. half * src1_dfloat = nullptr; // dfloat == half
  7267. bool src1_convert_f16 =
  7268. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7269. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7270. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7271. if (src1_convert_f16) {
  7272. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7273. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7274. GGML_ASSERT(to_fp16_cuda != nullptr);
  7275. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7276. }
  7277. #else
  7278. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7279. #endif // GGML_CUDA_F16
  7280. switch (src0->type) {
  7281. case GGML_TYPE_Q4_0:
  7282. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7283. break;
  7284. case GGML_TYPE_Q4_1:
  7285. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7286. break;
  7287. case GGML_TYPE_Q5_0:
  7288. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7289. break;
  7290. case GGML_TYPE_Q5_1:
  7291. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7292. break;
  7293. case GGML_TYPE_Q8_0:
  7294. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7295. break;
  7296. case GGML_TYPE_Q2_K:
  7297. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7298. break;
  7299. case GGML_TYPE_Q3_K:
  7300. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7301. break;
  7302. case GGML_TYPE_Q4_K:
  7303. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7304. break;
  7305. case GGML_TYPE_Q5_K:
  7306. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7307. break;
  7308. case GGML_TYPE_Q6_K:
  7309. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7310. break;
  7311. case GGML_TYPE_F16:
  7312. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7313. break;
  7314. default:
  7315. GGML_ASSERT(false);
  7316. break;
  7317. }
  7318. (void) src1;
  7319. (void) dst;
  7320. (void) src1_ddq_i;
  7321. (void) src1_ncols;
  7322. (void) src1_padded_row_size;
  7323. }
  7324. static void ggml_cuda_op_mul_mat_cublas(
  7325. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7326. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7327. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7328. GGML_ASSERT(src0_dd_i != nullptr);
  7329. GGML_ASSERT(src1_ddf_i != nullptr);
  7330. GGML_ASSERT(dst_dd_i != nullptr);
  7331. const int64_t ne00 = src0->ne[0];
  7332. const int64_t ne10 = src1->ne[0];
  7333. const int64_t ne0 = dst->ne[0];
  7334. const int64_t row_diff = row_high - row_low;
  7335. int id;
  7336. CUDA_CHECK(cudaGetDevice(&id));
  7337. // the main device has a larger memory buffer to hold the results from all GPUs
  7338. // ldc == nrows of the matrix that cuBLAS writes into
  7339. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7340. const int compute_capability = g_device_caps[id].cc;
  7341. 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) {
  7342. //printf("this branch\n");
  7343. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7344. cuda_pool_alloc<half> src0_as_f16;
  7345. if (src0->type != GGML_TYPE_F16) {
  7346. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7347. GGML_ASSERT(to_fp16_cuda != nullptr);
  7348. size_t ne = row_diff*ne00;
  7349. src0_as_f16.alloc(ne);
  7350. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7351. }
  7352. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7353. cuda_pool_alloc<half> src1_as_f16;
  7354. if (src1->type != GGML_TYPE_F16) {
  7355. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7356. GGML_ASSERT(to_fp16_cuda != nullptr);
  7357. size_t ne = src1_ncols*ne10;
  7358. src1_as_f16.alloc(ne);
  7359. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7360. }
  7361. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7362. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7363. const half alpha_f16 = 1.0f;
  7364. const half beta_f16 = 0.0f;
  7365. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7366. CUBLAS_CHECK(
  7367. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7368. row_diff, src1_ncols, ne10,
  7369. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7370. src1_ptr, CUDA_R_16F, ne10,
  7371. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7372. CUBLAS_COMPUTE_16F,
  7373. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7374. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7375. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7376. } else {
  7377. cuda_pool_alloc<float> src0_ddq_as_f32;
  7378. cuda_pool_alloc<float> src1_ddq_as_f32;
  7379. if (src0->type != GGML_TYPE_F32) {
  7380. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7381. GGML_ASSERT(to_fp32_cuda != nullptr);
  7382. src0_ddq_as_f32.alloc(row_diff*ne00);
  7383. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7384. }
  7385. if (src1->type != GGML_TYPE_F32) {
  7386. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7387. GGML_ASSERT(to_fp32_cuda != nullptr);
  7388. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7389. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7390. }
  7391. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7392. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7393. const float alpha = 1.0f;
  7394. const float beta = 0.0f;
  7395. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7396. CUBLAS_CHECK(
  7397. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7398. row_diff, src1_ncols, ne10,
  7399. &alpha, src0_ddf_i, ne00,
  7400. src1_ddf1_i, ne10,
  7401. &beta, dst_dd_i, ldc));
  7402. }
  7403. (void) dst;
  7404. (void) src1_ddq_i;
  7405. (void) src1_padded_row_size;
  7406. }
  7407. static void ggml_cuda_op_rope(
  7408. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7409. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7410. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7411. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7412. GGML_ASSERT(src0->type == dst->type);
  7413. const int64_t ne00 = src0->ne[0];
  7414. const int64_t ne01 = src0->ne[1];
  7415. const int64_t ne2 = dst->ne[2];
  7416. const int64_t nrows = ggml_nrows(src0);
  7417. //const int n_past = ((int32_t *) dst->op_params)[0];
  7418. const int n_dims = ((int32_t *) dst->op_params)[1];
  7419. const int mode = ((int32_t *) dst->op_params)[2];
  7420. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7421. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7422. // RoPE alteration for extended context
  7423. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7424. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7425. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7426. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7427. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7428. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7429. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7430. const int32_t * pos = nullptr;
  7431. if ((mode & 1) == 0) {
  7432. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7433. GGML_ASSERT(src1->ne[0] == ne2);
  7434. pos = (const int32_t *) src1_dd;
  7435. }
  7436. const bool is_neox = mode & 2;
  7437. const bool is_glm = mode & 4;
  7438. rope_corr_dims corr_dims;
  7439. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7440. // compute
  7441. if (is_glm) {
  7442. GGML_ASSERT(false);
  7443. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7444. } else if (is_neox) {
  7445. if (src0->type == GGML_TYPE_F32) {
  7446. rope_neox_cuda(
  7447. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7448. attn_factor, corr_dims, main_stream
  7449. );
  7450. } else if (src0->type == GGML_TYPE_F16) {
  7451. rope_neox_cuda(
  7452. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7453. attn_factor, corr_dims, main_stream
  7454. );
  7455. } else {
  7456. GGML_ASSERT(false);
  7457. }
  7458. } else {
  7459. if (src0->type == GGML_TYPE_F32) {
  7460. rope_cuda(
  7461. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7462. attn_factor, corr_dims, main_stream
  7463. );
  7464. } else if (src0->type == GGML_TYPE_F16) {
  7465. rope_cuda(
  7466. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7467. attn_factor, corr_dims, main_stream
  7468. );
  7469. } else {
  7470. GGML_ASSERT(false);
  7471. }
  7472. }
  7473. (void) src1;
  7474. (void) dst;
  7475. (void) src1_dd;
  7476. }
  7477. static void ggml_cuda_op_alibi(
  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( dst->type == GGML_TYPE_F32);
  7482. const int64_t ne00 = src0->ne[0];
  7483. const int64_t ne01 = src0->ne[1];
  7484. const int64_t ne02 = src0->ne[2];
  7485. const int64_t nrows = ggml_nrows(src0);
  7486. //const int n_past = ((int32_t *) dst->op_params)[0];
  7487. const int n_head = ((int32_t *) dst->op_params)[1];
  7488. float max_bias;
  7489. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7490. //GGML_ASSERT(ne01 + n_past == ne00);
  7491. GGML_ASSERT(n_head == ne02);
  7492. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7493. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7494. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7495. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7496. (void) src1;
  7497. (void) src1_dd;
  7498. }
  7499. static void ggml_cuda_op_pool2d(
  7500. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7501. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7502. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7503. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7504. const int32_t * opts = (const int32_t *)dst->op_params;
  7505. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  7506. const int k0 = opts[1];
  7507. const int k1 = opts[2];
  7508. const int s0 = opts[3];
  7509. const int s1 = opts[4];
  7510. const int p0 = opts[5];
  7511. const int p1 = opts[6];
  7512. const int64_t IH = src0->ne[1];
  7513. const int64_t IW = src0->ne[0];
  7514. const int64_t N = dst->ne[3];
  7515. const int64_t OC = dst->ne[2];
  7516. const int64_t OH = dst->ne[1];
  7517. const int64_t OW = dst->ne[0];
  7518. const int parallel_elements = N * OC * OH * OW;
  7519. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  7520. dim3 block_nums(num_blocks);
  7521. 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);
  7522. (void) src1;
  7523. (void) src1_dd;
  7524. }
  7525. static void ggml_cuda_op_im2col(
  7526. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7527. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7528. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7529. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7530. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  7531. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7532. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  7533. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  7534. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  7535. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  7536. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  7537. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  7538. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  7539. const int64_t IH = is_2D ? src1->ne[1] : 1;
  7540. const int64_t IW = src1->ne[0];
  7541. const int64_t KH = is_2D ? src0->ne[1] : 1;
  7542. const int64_t KW = src0->ne[0];
  7543. const int64_t OH = is_2D ? dst->ne[2] : 1;
  7544. const int64_t OW = dst->ne[1];
  7545. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7546. const int64_t batch = src1->ne[3];
  7547. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  7548. if(dst->type == GGML_TYPE_F16) {
  7549. 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);
  7550. } else {
  7551. 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);
  7552. }
  7553. (void) src0;
  7554. (void) src0_dd;
  7555. }
  7556. static void ggml_cuda_op_sum_rows(
  7557. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7558. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7559. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7560. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7561. const int64_t ncols = src0->ne[0];
  7562. const int64_t nrows = ggml_nrows(src0);
  7563. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  7564. (void) src1;
  7565. (void) dst;
  7566. (void) src1_dd;
  7567. }
  7568. static void ggml_cuda_op_argsort(
  7569. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7570. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7571. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7572. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  7573. const int64_t ncols = src0->ne[0];
  7574. const int64_t nrows = ggml_nrows(src0);
  7575. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  7576. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  7577. (void) src1;
  7578. (void) dst;
  7579. (void) src1_dd;
  7580. }
  7581. static void ggml_cuda_op_diag_mask_inf(
  7582. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7583. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7584. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7585. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7586. const int64_t ne00 = src0->ne[0];
  7587. const int64_t ne01 = src0->ne[1];
  7588. const int nrows0 = ggml_nrows(src0);
  7589. const int n_past = ((int32_t *) dst->op_params)[0];
  7590. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  7591. (void) src1;
  7592. (void) dst;
  7593. (void) src1_dd;
  7594. }
  7595. static void ggml_cuda_op_soft_max(
  7596. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7597. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7598. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7599. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7600. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  7601. const int64_t ne00 = src0->ne[0];
  7602. const int64_t nrows_x = ggml_nrows(src0);
  7603. const int64_t nrows_y = src0->ne[1];
  7604. float scale = 1.0f;
  7605. float max_bias = 0.0f;
  7606. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7607. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7608. // positions tensor
  7609. float * src2_dd = nullptr;
  7610. cuda_pool_alloc<float> src2_f;
  7611. ggml_tensor * src2 = dst->src[2];
  7612. const bool use_src2 = src2 != nullptr;
  7613. if (use_src2) {
  7614. const bool src2_on_device = src2->backend == GGML_BACKEND_GPU;
  7615. if (src2_on_device) {
  7616. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  7617. src2_dd = (float *) src2_extra->data_device[g_main_device];
  7618. } else {
  7619. src2_dd = src2_f.alloc(ggml_nelements(src2));
  7620. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  7621. }
  7622. }
  7623. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  7624. }
  7625. static void ggml_cuda_op_scale(
  7626. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7627. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7628. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7629. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7630. float scale;
  7631. memcpy(&scale, dst->op_params, sizeof(float));
  7632. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  7633. CUDA_CHECK(cudaGetLastError());
  7634. (void) src1;
  7635. (void) dst;
  7636. (void) src1_dd;
  7637. }
  7638. static void ggml_cuda_op_clamp(
  7639. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7640. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7641. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7642. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7643. float min;
  7644. float max;
  7645. memcpy(&min, dst->op_params, sizeof(float));
  7646. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7647. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  7648. CUDA_CHECK(cudaGetLastError());
  7649. (void) src1;
  7650. (void) dst;
  7651. (void) src1_dd;
  7652. }
  7653. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  7654. const int64_t nrows0 = ggml_nrows(src0);
  7655. const bool use_src1 = src1 != nullptr;
  7656. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  7657. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  7658. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  7659. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7660. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  7661. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7662. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7663. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  7664. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  7665. // dd = data device
  7666. float * src0_ddf = nullptr;
  7667. float * src1_ddf = nullptr;
  7668. float * dst_ddf = nullptr;
  7669. cuda_pool_alloc<float> src0_f;
  7670. cuda_pool_alloc<float> src1_f;
  7671. cuda_pool_alloc<float> dst_f;
  7672. ggml_cuda_set_device(g_main_device);
  7673. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7674. if (src0_on_device) {
  7675. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  7676. } else {
  7677. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  7678. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  7679. }
  7680. if (use_src1) {
  7681. if (src1_on_device) {
  7682. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7683. } else {
  7684. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  7685. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  7686. }
  7687. }
  7688. if (dst_on_device) {
  7689. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7690. } else {
  7691. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7692. }
  7693. // do the computation
  7694. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7695. CUDA_CHECK(cudaGetLastError());
  7696. // copy dst to host if necessary
  7697. if (!dst_on_device) {
  7698. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7699. }
  7700. if (dst->backend == GGML_BACKEND_CPU) {
  7701. CUDA_CHECK(cudaDeviceSynchronize());
  7702. }
  7703. }
  7704. static void ggml_cuda_set_peer_access(const int n_tokens) {
  7705. static bool peer_access_enabled = false;
  7706. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  7707. if (peer_access_enabled == enable_peer_access) {
  7708. return;
  7709. }
  7710. #ifdef NDEBUG
  7711. for (int id = 0; id < g_device_count; ++id) {
  7712. ggml_cuda_set_device(id);
  7713. CUDA_CHECK(cudaDeviceSynchronize());
  7714. }
  7715. for (int id = 0; id < g_device_count; ++id) {
  7716. ggml_cuda_set_device(id);
  7717. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  7718. if (id == id_other) {
  7719. continue;
  7720. }
  7721. if (id != g_main_device && id_other != g_main_device) {
  7722. continue;
  7723. }
  7724. int can_access_peer;
  7725. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  7726. if (can_access_peer) {
  7727. if (enable_peer_access) {
  7728. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  7729. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  7730. CUDA_CHECK(err);
  7731. }
  7732. } else {
  7733. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  7734. if (err != cudaErrorPeerAccessNotEnabled) {
  7735. CUDA_CHECK(err);
  7736. }
  7737. }
  7738. }
  7739. }
  7740. }
  7741. #endif // NDEBUG
  7742. peer_access_enabled = enable_peer_access;
  7743. }
  7744. // FIXME: move this somewhere else
  7745. struct ggml_backend_cuda_split_buffer_type_context {
  7746. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7747. };
  7748. static void ggml_cuda_op_mul_mat(
  7749. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  7750. const bool convert_src1_to_q8_1) {
  7751. const int64_t ne00 = src0->ne[0];
  7752. const int64_t ne01 = src0->ne[1];
  7753. const int64_t ne02 = src0->ne[2];
  7754. const int64_t ne03 = src0->ne[3];
  7755. const int64_t ne10 = src1->ne[0];
  7756. const int64_t ne11 = src1->ne[1];
  7757. const int64_t ne12 = src1->ne[2];
  7758. const int64_t ne13 = src1->ne[3];
  7759. const int64_t nrows1 = ggml_nrows(src1);
  7760. GGML_ASSERT(ne03 == ne13);
  7761. const int64_t ne0 = dst->ne[0];
  7762. const int64_t ne1 = dst->ne[1];
  7763. const int nb2 = dst->nb[2];
  7764. const int nb3 = dst->nb[3];
  7765. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  7766. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  7767. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  7768. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  7769. const int64_t i02_divisor = ne12 / ne02;
  7770. const size_t src0_ts = ggml_type_size(src0->type);
  7771. const size_t src0_bs = ggml_blck_size(src0->type);
  7772. const size_t q8_1_ts = sizeof(block_q8_1);
  7773. const size_t q8_1_bs = QK8_1;
  7774. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7775. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7776. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7777. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7778. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  7779. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  7780. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  7781. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  7782. GGML_ASSERT(!(split && ne02 > 1));
  7783. GGML_ASSERT(!(split && ne03 > 1));
  7784. GGML_ASSERT(!(split && ne02 < ne12));
  7785. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7786. if (split) {
  7787. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_GPU_SPLIT check
  7788. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  7789. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  7790. tensor_split = buft_ctx->tensor_split;
  7791. }
  7792. struct dev_data {
  7793. cuda_pool_alloc<char> src0_dd_alloc;
  7794. cuda_pool_alloc<float> src1_ddf_alloc;
  7795. cuda_pool_alloc<char> src1_ddq_alloc;
  7796. cuda_pool_alloc<float> dst_dd_alloc;
  7797. char * src0_dd = nullptr;
  7798. float * src1_ddf = nullptr; // float
  7799. char * src1_ddq = nullptr; // q8_1
  7800. float * dst_dd = nullptr;
  7801. int64_t row_low;
  7802. int64_t row_high;
  7803. };
  7804. dev_data dev[GGML_CUDA_MAX_DEVICES];
  7805. int used_devices = 0;
  7806. for (int id = 0; id < g_device_count; ++id) {
  7807. // by default, use all rows
  7808. dev[id].row_low = 0;
  7809. dev[id].row_high = ne01;
  7810. // for multi GPU, get the row boundaries from tensor split
  7811. // and round to mul_mat_q tile sizes
  7812. if (split) {
  7813. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  7814. if (id != 0) {
  7815. dev[id].row_low = ne01*tensor_split[id];
  7816. if (dev[id].row_low < ne01) {
  7817. dev[id].row_low -= dev[id].row_low % rounding;
  7818. }
  7819. }
  7820. if (id != g_device_count - 1) {
  7821. dev[id].row_high = ne01*tensor_split[id + 1];
  7822. if (dev[id].row_high < ne01) {
  7823. dev[id].row_high -= dev[id].row_high % rounding;
  7824. }
  7825. }
  7826. }
  7827. }
  7828. for (int id = 0; id < g_device_count; ++id) {
  7829. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7830. continue;
  7831. }
  7832. used_devices++;
  7833. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7834. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7835. ggml_cuda_set_device(id);
  7836. cudaStream_t stream = g_cudaStreams[id][0];
  7837. if (src0_on_device && src0_is_contiguous) {
  7838. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  7839. } else {
  7840. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  7841. }
  7842. if (src1_on_device && src1_is_contiguous) {
  7843. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7844. } else {
  7845. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7846. }
  7847. if (convert_src1_to_q8_1) {
  7848. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7849. if (src1_on_device && src1_is_contiguous) {
  7850. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7851. CUDA_CHECK(cudaGetLastError());
  7852. }
  7853. }
  7854. if (dst_on_device) {
  7855. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7856. } else {
  7857. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7858. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7859. }
  7860. }
  7861. // if multiple devices are used they need to wait for the main device
  7862. // here an event is recorded that signals that the main device has finished calculating the input data
  7863. if (split && used_devices > 1) {
  7864. ggml_cuda_set_device(g_main_device);
  7865. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7866. }
  7867. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7868. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7869. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7870. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7871. for (int id = 0; id < g_device_count; ++id) {
  7872. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7873. continue;
  7874. }
  7875. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7876. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7877. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7878. ggml_cuda_set_device(id);
  7879. cudaStream_t stream = g_cudaStreams[id][is];
  7880. // wait for main GPU data if necessary
  7881. if (split && (id != g_main_device || is != 0)) {
  7882. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7883. }
  7884. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7885. const int64_t i03 = i0 / ne12;
  7886. const int64_t i02 = i0 % ne12;
  7887. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7888. // for split tensors the data begins at i0 == i0_offset_low
  7889. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7890. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7891. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7892. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7893. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7894. // in that case an offset on dst_ddf_i is needed
  7895. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  7896. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7897. }
  7898. // copy src0, src1 to device if necessary
  7899. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  7900. if (id != g_main_device) {
  7901. if (convert_src1_to_q8_1) {
  7902. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7903. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7904. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7905. } else {
  7906. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7907. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7908. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7909. src1_ncols*ne10*sizeof(float), stream));
  7910. }
  7911. }
  7912. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  7913. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7914. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7915. } else {
  7916. GGML_ASSERT(false);
  7917. }
  7918. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  7919. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7920. CUDA_CHECK(cudaGetLastError());
  7921. }
  7922. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7923. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7924. }
  7925. // do the computation
  7926. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7927. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7928. CUDA_CHECK(cudaGetLastError());
  7929. // copy dst to host or other device if necessary
  7930. if (!dst_on_device) {
  7931. void * dst_off_device;
  7932. cudaMemcpyKind kind;
  7933. if (dst->backend == GGML_BACKEND_CPU) {
  7934. dst_off_device = dst->data;
  7935. kind = cudaMemcpyDeviceToHost;
  7936. } else if (dst->backend == GGML_BACKEND_GPU) {
  7937. dst_off_device = dst_extra->data_device[g_main_device];
  7938. kind = cudaMemcpyDeviceToDevice;
  7939. } else {
  7940. GGML_ASSERT(false);
  7941. }
  7942. if (split) {
  7943. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7944. // dst is NOT transposed.
  7945. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7946. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7947. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7948. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7949. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7950. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7951. #if !defined(GGML_USE_HIPBLAS)
  7952. if (kind == cudaMemcpyDeviceToDevice) {
  7953. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7954. cudaMemcpy3DPeerParms p = {};
  7955. p.dstDevice = g_main_device;
  7956. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7957. p.srcDevice = id;
  7958. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7959. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7960. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7961. } else
  7962. #endif
  7963. {
  7964. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7965. dst_dd_i, row_diff*sizeof(float),
  7966. row_diff*sizeof(float), src1_ncols,
  7967. kind, stream));
  7968. }
  7969. } else {
  7970. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7971. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7972. dhf_dst_i += src1_col_0*ne0;
  7973. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7974. }
  7975. }
  7976. // add event for the main device to wait on until other device is done
  7977. if (split && (id != g_main_device || is != 0)) {
  7978. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7979. }
  7980. }
  7981. }
  7982. }
  7983. // main device waits for all other devices to be finished
  7984. if (split && g_device_count > 1) {
  7985. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7986. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7987. ggml_cuda_set_device(g_main_device);
  7988. for (int id = 0; id < g_device_count; ++id) {
  7989. if (dev[id].row_low == dev[id].row_high) {
  7990. continue;
  7991. }
  7992. for (int64_t is = 0; is < is_max; ++is) {
  7993. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7994. }
  7995. }
  7996. }
  7997. if (dst->backend == GGML_BACKEND_CPU) {
  7998. ggml_cuda_set_device(g_main_device);
  7999. CUDA_CHECK(cudaDeviceSynchronize());
  8000. }
  8001. }
  8002. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8003. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  8004. }
  8005. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8006. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  8007. }
  8008. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8009. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  8010. }
  8011. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8012. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  8013. }
  8014. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8015. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  8016. }
  8017. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8018. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  8019. }
  8020. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8021. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  8022. }
  8023. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8024. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  8025. }
  8026. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8027. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  8028. }
  8029. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8030. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  8031. }
  8032. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8033. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  8034. }
  8035. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8036. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  8037. }
  8038. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8039. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  8040. }
  8041. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8042. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  8043. }
  8044. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8045. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  8046. }
  8047. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8048. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  8049. }
  8050. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8051. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  8052. }
  8053. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8054. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  8055. }
  8056. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8057. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  8058. }
  8059. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8060. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  8061. }
  8062. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8063. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  8064. }
  8065. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  8066. if (!g_cublas_loaded) return false;
  8067. const int64_t ne10 = src1->ne[0];
  8068. const int64_t ne0 = dst->ne[0];
  8069. const int64_t ne1 = dst->ne[1];
  8070. // TODO: find the optimal values for these
  8071. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  8072. src1->type == GGML_TYPE_F32 &&
  8073. dst->type == GGML_TYPE_F32 &&
  8074. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  8075. }
  8076. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8077. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  8078. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8079. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  8080. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  8081. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8082. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8083. const int64_t ne00 = src0->ne[0];
  8084. const int64_t ne01 = src0->ne[1];
  8085. const int64_t ne02 = src0->ne[2];
  8086. const int64_t ne12 = src1->ne[2];
  8087. ggml_cuda_set_device(g_main_device);
  8088. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8089. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8090. void * src0_ddq = src0_extra->data_device[g_main_device];
  8091. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8092. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8093. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8094. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8095. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8096. }
  8097. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8098. GGML_ASSERT(!ggml_is_transposed(src0));
  8099. GGML_ASSERT(!ggml_is_transposed(src1));
  8100. GGML_ASSERT(!ggml_is_permuted(src0));
  8101. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8102. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8103. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8104. const int64_t ne00 = src0->ne[0];
  8105. const int64_t ne01 = src0->ne[1];
  8106. const int64_t ne02 = src0->ne[2];
  8107. const int64_t nb01 = src0->nb[1];
  8108. const int64_t nb02 = src0->nb[2];
  8109. const int64_t ne12 = src1->ne[2];
  8110. ggml_cuda_set_device(g_main_device);
  8111. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8112. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8113. void * src0_ddq = src0_extra->data_device[g_main_device];
  8114. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8115. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8116. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8117. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8118. const int64_t row_stride_x = nb01 / sizeof(half);
  8119. const int64_t channel_stride_x = nb02 / sizeof(half);
  8120. 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);
  8121. }
  8122. static __global__ void k_compute_batched_ptrs(
  8123. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8124. const void ** ptrs_src, void ** ptrs_dst,
  8125. int64_t ne12, int64_t ne13,
  8126. int64_t ne23,
  8127. size_t nb02, size_t nb03,
  8128. size_t nb12, size_t nb13,
  8129. size_t nbd2, size_t nbd3,
  8130. int64_t r2, int64_t r3) {
  8131. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8132. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8133. if (i13 >= ne13 || i12 >= ne12) {
  8134. return;
  8135. }
  8136. int64_t i03 = i13 / r3;
  8137. int64_t i02 = i12 / r2;
  8138. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8139. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8140. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8141. }
  8142. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8143. GGML_ASSERT(!ggml_is_transposed(src0));
  8144. GGML_ASSERT(!ggml_is_transposed(src1));
  8145. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8146. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8147. GGML_TENSOR_BINARY_OP_LOCALS
  8148. const int64_t ne_dst = ggml_nelements(dst);
  8149. ggml_cuda_set_device(g_main_device);
  8150. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8151. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8152. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8153. void * src0_ddq = src0_extra->data_device[g_main_device];
  8154. half * src0_f16 = (half *) src0_ddq;
  8155. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8156. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8157. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8158. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8159. // convert src1 to fp16
  8160. cuda_pool_alloc<half> src1_f16_alloc;
  8161. if (src1->type != GGML_TYPE_F16) {
  8162. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8163. const int64_t ne_src1 = ggml_nelements(src1);
  8164. src1_f16_alloc.alloc(ne_src1);
  8165. GGML_ASSERT(to_fp16_cuda != nullptr);
  8166. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8167. }
  8168. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8169. cuda_pool_alloc<half> dst_f16;
  8170. char * dst_t;
  8171. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8172. cudaDataType_t cu_data_type = CUDA_R_16F;
  8173. // dst strides
  8174. size_t nbd2 = dst->nb[2];
  8175. size_t nbd3 = dst->nb[3];
  8176. const half alpha_f16 = 1.0f;
  8177. const half beta_f16 = 0.0f;
  8178. const float alpha_f32 = 1.0f;
  8179. const float beta_f32 = 0.0f;
  8180. const void * alpha = &alpha_f16;
  8181. const void * beta = &beta_f16;
  8182. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8183. dst_t = (char *) dst_f16.alloc(ne_dst);
  8184. nbd2 /= sizeof(float) / sizeof(half);
  8185. nbd3 /= sizeof(float) / sizeof(half);
  8186. } else {
  8187. dst_t = (char *) dst_ddf;
  8188. cu_compute_type = CUBLAS_COMPUTE_32F;
  8189. cu_data_type = CUDA_R_32F;
  8190. alpha = &alpha_f32;
  8191. beta = &beta_f32;
  8192. }
  8193. GGML_ASSERT(ne12 % ne02 == 0);
  8194. GGML_ASSERT(ne13 % ne03 == 0);
  8195. // broadcast factors
  8196. const int64_t r2 = ne12/ne02;
  8197. const int64_t r3 = ne13/ne03;
  8198. #if 0
  8199. // use cublasGemmEx
  8200. {
  8201. for (int i13 = 0; i13 < ne13; ++i13) {
  8202. for (int i12 = 0; i12 < ne12; ++i12) {
  8203. int i03 = i13 / r3;
  8204. int i02 = i12 / r2;
  8205. CUBLAS_CHECK(
  8206. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8207. ne01, ne11, ne10,
  8208. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8209. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8210. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8211. cu_compute_type,
  8212. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8213. }
  8214. }
  8215. }
  8216. #else
  8217. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8218. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8219. // use cublasGemmStridedBatchedEx
  8220. CUBLAS_CHECK(
  8221. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8222. ne01, ne11, ne10,
  8223. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8224. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8225. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8226. ne12*ne13,
  8227. cu_compute_type,
  8228. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8229. } else {
  8230. // use cublasGemmBatchedEx
  8231. const int ne23 = ne12*ne13;
  8232. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8233. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8234. dim3 block_dims(ne13, ne12);
  8235. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8236. src0_f16, src1_f16, dst_t,
  8237. ptrs_src.get(), ptrs_dst.get(),
  8238. ne12, ne13,
  8239. ne23,
  8240. nb02, nb03,
  8241. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8242. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8243. nbd2, nbd3,
  8244. r2, r3);
  8245. CUDA_CHECK(cudaGetLastError());
  8246. CUBLAS_CHECK(
  8247. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8248. ne01, ne11, ne10,
  8249. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8250. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8251. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8252. ne23,
  8253. cu_compute_type,
  8254. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8255. }
  8256. #endif
  8257. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8258. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8259. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8260. }
  8261. }
  8262. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8263. const bool all_on_device =
  8264. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  8265. (src1->backend == GGML_BACKEND_GPU) &&
  8266. ( dst->backend == GGML_BACKEND_GPU);
  8267. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  8268. int64_t min_compute_capability = INT_MAX;
  8269. bool any_pascal_with_slow_fp16 = false;
  8270. if (split) {
  8271. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8272. auto & tensor_split = buft_ctx->tensor_split;
  8273. for (int id = 0; id < g_device_count; ++id) {
  8274. // skip devices that are not going to do any work:
  8275. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8276. continue;
  8277. }
  8278. if (min_compute_capability > g_device_caps[id].cc) {
  8279. min_compute_capability = g_device_caps[id].cc;
  8280. }
  8281. if (g_device_caps[id].cc == 610) {
  8282. any_pascal_with_slow_fp16 = true;
  8283. }
  8284. }
  8285. } else {
  8286. min_compute_capability = g_device_caps[g_main_device].cc;
  8287. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8288. }
  8289. // check data types and tensor shapes for custom matrix multiplication kernels:
  8290. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8291. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8292. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8293. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8294. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8295. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8296. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8297. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8298. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8299. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8300. #ifdef CUDA_USE_TENSOR_CORES
  8301. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8302. #endif // CUDA_USE_TENSOR_CORES
  8303. #else
  8304. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8305. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8306. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8307. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8308. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8309. #ifdef CUDA_USE_TENSOR_CORES
  8310. // when tensor cores are available, use them for large batch size
  8311. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8312. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8313. #endif // CUDA_USE_TENSOR_CORES
  8314. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8315. // if mmvq is available it's a better choice than dmmv:
  8316. #ifndef GGML_CUDA_FORCE_DMMV
  8317. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8318. #endif // GGML_CUDA_FORCE_DMMV
  8319. // debug helpers
  8320. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8321. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8322. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8323. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8324. //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);
  8325. //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);
  8326. 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) {
  8327. // KQ single-batch
  8328. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8329. } 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) {
  8330. // KQV single-batch
  8331. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8332. } 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) {
  8333. // KQ + KQV multi-batch
  8334. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8335. } else if (use_dequantize_mul_mat_vec) {
  8336. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8337. } else if (use_mul_mat_vec_q) {
  8338. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8339. } else if (use_mul_mat_q) {
  8340. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8341. } else {
  8342. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8343. }
  8344. }
  8345. #if 0
  8346. template<typename ... Srcs>
  8347. static __global__ void k_compute_batched_ptrs_id(
  8348. const void ** ptrs_src, void ** ptrs_dst,
  8349. int ne12, int ne13,
  8350. int ne23,
  8351. int nb02, int nb03,
  8352. int nb12, int nb13,
  8353. int nb2, int nb3,
  8354. int r2, int r3,
  8355. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8356. const half * src1_f16, half * dst_f16,
  8357. const int32_t * ids, const int id,
  8358. Srcs... src0s) {
  8359. int i = ids[id];
  8360. half * src0_f16;
  8361. const void * srcs_ar[] = { (const half *) src0s... };
  8362. if (src0_type == GGML_TYPE_F16) {
  8363. src0_f16 = (half *) srcs_ar[i];
  8364. } else {
  8365. src0_f16 = src0_as_f16;
  8366. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8367. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8368. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8369. }
  8370. }
  8371. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8372. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8373. if (i13 >= ne13 || i12 >= ne12) {
  8374. return;
  8375. }
  8376. int i03 = i13 / r3;
  8377. int i02 = i12 / r2;
  8378. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8379. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8380. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8381. }
  8382. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8383. const struct ggml_tensor * ids = dst->src[0];
  8384. const struct ggml_tensor * src1 = dst->src[1];
  8385. const struct ggml_tensor * src00 = dst->src[2];
  8386. const int id = dst->op_params[0];
  8387. GGML_ASSERT(!ggml_is_transposed(src00));
  8388. GGML_ASSERT(!ggml_is_transposed(src1));
  8389. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  8390. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8391. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8392. const int64_t ne01 = src00->ne[1];
  8393. const int64_t ne02 = src00->ne[2];
  8394. const int64_t ne03 = src00->ne[3];
  8395. //const int64_t nb01 = src00->nb[1];
  8396. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8397. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8398. const int64_t ne10 = src1->ne[0];
  8399. const int64_t ne11 = src1->ne[1];
  8400. const int64_t ne12 = src1->ne[2];
  8401. const int64_t ne13 = src1->ne[3];
  8402. //const int64_t nb11 = src1->nb[1];
  8403. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8404. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8405. const int64_t ne1 = ggml_nelements(src1);
  8406. const int64_t ne = ggml_nelements(dst);
  8407. ggml_cuda_set_device(g_main_device);
  8408. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8409. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8410. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8411. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8412. //half * src0_as_f16 = (half *) src0_ddq;
  8413. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8414. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8415. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8416. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8417. // convert src1 to fp16
  8418. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8419. GGML_ASSERT(to_fp16_cuda != nullptr);
  8420. size_t src1_as = 0;
  8421. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8422. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8423. size_t dst_as = 0;
  8424. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8425. GGML_ASSERT(ne12 % ne02 == 0);
  8426. GGML_ASSERT(ne13 % ne03 == 0);
  8427. // broadcast factors
  8428. const int64_t r2 = ne12/ne02;
  8429. const int64_t r3 = ne13/ne03;
  8430. const half alpha_f16 = 1.0f;
  8431. const half beta_f16 = 0.0f;
  8432. // use cublasGemmBatchedEx
  8433. const int ne23 = ne12*ne13;
  8434. const void ** ptrs_src = nullptr;
  8435. void ** ptrs_dst = nullptr;
  8436. size_t ptrs_src_s = 0;
  8437. size_t ptrs_dst_s = 0;
  8438. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8439. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8440. int64_t src0_ne = ggml_nelements(src00);
  8441. half * src0_as_f16 = nullptr;
  8442. size_t src0_as = 0;
  8443. if (src00->type != GGML_TYPE_F16) {
  8444. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8445. }
  8446. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8447. dim3 block_dims(ne13, ne12);
  8448. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8449. ptrs_src, ptrs_dst,
  8450. ne12, ne13,
  8451. ne23,
  8452. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8453. nb12, nb13,
  8454. dst->nb[2], dst->nb[3],
  8455. r2, r3,
  8456. src00->type, src0_as_f16, src0_ne,
  8457. src1_as_f16, dst_f16,
  8458. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8459. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8460. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8461. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8462. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8463. );
  8464. CUDA_CHECK(cudaGetLastError());
  8465. CUBLAS_CHECK(
  8466. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8467. ne01, ne11, ne10,
  8468. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8469. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8470. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8471. ne23,
  8472. CUBLAS_COMPUTE_16F,
  8473. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8474. if (src0_as != 0) {
  8475. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8476. }
  8477. if (ptrs_src_s != 0) {
  8478. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8479. }
  8480. if (ptrs_dst_s != 0) {
  8481. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8482. }
  8483. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8484. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8485. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8486. ggml_cuda_pool_free(dst_f16, dst_as);
  8487. }
  8488. #endif
  8489. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8490. #if 0
  8491. ggml_cuda_mul_mat_id_cublas(dst);
  8492. // TODO: mmq/mmv support
  8493. #endif
  8494. const size_t nb11 = src1->nb[1];
  8495. const size_t nb1 = dst->nb[1];
  8496. const struct ggml_tensor * ids = src0;
  8497. const int32_t id = ((int32_t *) dst->op_params)[0];
  8498. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  8499. std::vector<char> ids_host(ggml_nbytes(ids));
  8500. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  8501. if (ids->backend == GGML_BACKEND_GPU) {
  8502. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  8503. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  8504. CUDA_CHECK(cudaStreamSynchronize(stream));
  8505. } else {
  8506. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  8507. }
  8508. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  8509. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  8510. ggml_tensor_extra_gpu src1_row_extra;
  8511. ggml_tensor_extra_gpu dst_row_extra;
  8512. ggml_tensor src1_row = *src1;
  8513. ggml_tensor dst_row = *dst;
  8514. src1_row.backend = GGML_BACKEND_GPU;
  8515. dst_row.backend = GGML_BACKEND_GPU;
  8516. src1_row.extra = &src1_row_extra;
  8517. dst_row.extra = &dst_row_extra;
  8518. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  8519. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  8520. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  8521. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  8522. if (src1->ne[1] == 1) {
  8523. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8524. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  8525. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8526. //int32_t row_id;
  8527. //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]));
  8528. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  8529. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8530. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8531. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8532. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  8533. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  8534. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  8535. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  8536. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8537. }
  8538. } else {
  8539. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  8540. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  8541. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  8542. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  8543. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  8544. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  8545. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  8546. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  8547. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  8548. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8549. int64_t num_src1_rows = 0;
  8550. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8551. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8552. if (row_id_i != row_id) {
  8553. continue;
  8554. }
  8555. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8556. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  8557. nb11, src1_kind, stream));
  8558. num_src1_rows++;
  8559. }
  8560. if (num_src1_rows == 0) {
  8561. continue;
  8562. }
  8563. src1_row.ne[1] = num_src1_rows;
  8564. dst_row.ne[1] = num_src1_rows;
  8565. src1_row.nb[1] = nb11;
  8566. src1_row.nb[2] = num_src1_rows*nb11;
  8567. src1_row.nb[3] = num_src1_rows*nb11;
  8568. dst_row.nb[1] = nb1;
  8569. dst_row.nb[2] = num_src1_rows*nb1;
  8570. dst_row.nb[3] = num_src1_rows*nb1;
  8571. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8572. num_src1_rows = 0;
  8573. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8574. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8575. if (row_id_i != row_id) {
  8576. continue;
  8577. }
  8578. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8579. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  8580. nb1, dst_kind, stream));
  8581. num_src1_rows++;
  8582. }
  8583. }
  8584. }
  8585. if (dst->backend == GGML_BACKEND_CPU) {
  8586. CUDA_CHECK(cudaStreamSynchronize(stream));
  8587. }
  8588. }
  8589. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8590. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  8591. }
  8592. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8593. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  8594. }
  8595. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8596. const int64_t ne = ggml_nelements(src0);
  8597. GGML_ASSERT(ne == ggml_nelements(src1));
  8598. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  8599. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8600. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  8601. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  8602. const int64_t ne00 = src0->ne[0];
  8603. const int64_t ne01 = src0->ne[1];
  8604. const int64_t ne02 = src0->ne[2];
  8605. //GGML_ASSERT(src0->ne[3] == 1);
  8606. const int64_t nb00 = src0->nb[0];
  8607. const int64_t nb01 = src0->nb[1];
  8608. const int64_t nb02 = src0->nb[2];
  8609. const int64_t nb03 = src0->nb[3];
  8610. const int64_t ne10 = src1->ne[0];
  8611. const int64_t ne11 = src1->ne[1];
  8612. const int64_t ne12 = src1->ne[2];
  8613. //GGML_ASSERT(src1->ne[3] == 1);
  8614. const int64_t nb10 = src1->nb[0];
  8615. const int64_t nb11 = src1->nb[1];
  8616. const int64_t nb12 = src1->nb[2];
  8617. const int64_t nb13 = src1->nb[3];
  8618. ggml_cuda_set_device(g_main_device);
  8619. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8620. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8621. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8622. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8623. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  8624. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  8625. 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);
  8626. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  8627. 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);
  8628. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  8629. 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);
  8630. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  8631. 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);
  8632. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  8633. 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);
  8634. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  8635. 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);
  8636. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  8637. 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);
  8638. } else {
  8639. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  8640. ggml_type_name(src0->type), ggml_type_name(src1->type));
  8641. GGML_ASSERT(false);
  8642. }
  8643. (void) dst;
  8644. }
  8645. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8646. // TODO: why do we pass dst as src1 here?
  8647. ggml_cuda_cpy(src0, dst, nullptr);
  8648. (void) src1;
  8649. }
  8650. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8651. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  8652. }
  8653. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8654. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  8655. }
  8656. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8657. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8658. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  8659. }
  8660. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8661. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  8662. }
  8663. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8664. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  8665. }
  8666. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8667. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  8668. }
  8669. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8670. GGML_ASSERT(ggml_is_contiguous(src0));
  8671. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  8672. }
  8673. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8674. GGML_ASSERT(ggml_is_contiguous(src0));
  8675. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  8676. }
  8677. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8678. (void) src0;
  8679. (void) src1;
  8680. (void) dst;
  8681. }
  8682. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  8683. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  8684. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  8685. }
  8686. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  8687. if (main_device >= g_device_count) {
  8688. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8689. main_device, g_device_count, g_main_device);
  8690. return;
  8691. }
  8692. if (g_main_device != main_device && g_device_count > 1) {
  8693. g_main_device = main_device;
  8694. //cudaDeviceProp prop;
  8695. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8696. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8697. }
  8698. }
  8699. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8700. if (!g_cublas_loaded) return false;
  8701. ggml_cuda_func_t func;
  8702. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  8703. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  8704. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  8705. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8706. return false;
  8707. }
  8708. if (tensor->op == GGML_OP_MUL_MAT) {
  8709. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8710. #ifndef NDEBUG
  8711. 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]);
  8712. #endif
  8713. return false;
  8714. }
  8715. }
  8716. switch (tensor->op) {
  8717. case GGML_OP_REPEAT:
  8718. func = ggml_cuda_repeat;
  8719. break;
  8720. case GGML_OP_GET_ROWS:
  8721. func = ggml_cuda_get_rows;
  8722. break;
  8723. case GGML_OP_DUP:
  8724. func = ggml_cuda_dup;
  8725. break;
  8726. case GGML_OP_ADD:
  8727. func = ggml_cuda_add;
  8728. break;
  8729. case GGML_OP_ACC:
  8730. func = ggml_cuda_acc;
  8731. break;
  8732. case GGML_OP_MUL:
  8733. func = ggml_cuda_mul;
  8734. break;
  8735. case GGML_OP_DIV:
  8736. func = ggml_cuda_div;
  8737. break;
  8738. case GGML_OP_UNARY:
  8739. switch (ggml_get_unary_op(tensor)) {
  8740. case GGML_UNARY_OP_GELU:
  8741. func = ggml_cuda_gelu;
  8742. break;
  8743. case GGML_UNARY_OP_SILU:
  8744. func = ggml_cuda_silu;
  8745. break;
  8746. case GGML_UNARY_OP_GELU_QUICK:
  8747. func = ggml_cuda_gelu_quick;
  8748. break;
  8749. case GGML_UNARY_OP_TANH:
  8750. func = ggml_cuda_tanh;
  8751. break;
  8752. case GGML_UNARY_OP_RELU:
  8753. func = ggml_cuda_relu;
  8754. break;
  8755. case GGML_UNARY_OP_HARDSIGMOID:
  8756. func = ggml_cuda_hardsigmoid;
  8757. break;
  8758. case GGML_UNARY_OP_HARDSWISH:
  8759. func = ggml_cuda_hardswish;
  8760. break;
  8761. default:
  8762. return false;
  8763. }
  8764. break;
  8765. case GGML_OP_NORM:
  8766. func = ggml_cuda_norm;
  8767. break;
  8768. case GGML_OP_GROUP_NORM:
  8769. func = ggml_cuda_group_norm;
  8770. break;
  8771. case GGML_OP_CONCAT:
  8772. func = ggml_cuda_concat;
  8773. break;
  8774. case GGML_OP_UPSCALE:
  8775. func = ggml_cuda_upscale;
  8776. break;
  8777. case GGML_OP_PAD:
  8778. func = ggml_cuda_pad;
  8779. break;
  8780. case GGML_OP_LEAKY_RELU:
  8781. func = ggml_cuda_leaky_relu;
  8782. break;
  8783. case GGML_OP_RMS_NORM:
  8784. func = ggml_cuda_rms_norm;
  8785. break;
  8786. case GGML_OP_MUL_MAT:
  8787. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8788. return false;
  8789. }
  8790. func = ggml_cuda_mul_mat;
  8791. break;
  8792. case GGML_OP_MUL_MAT_ID:
  8793. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8794. return false;
  8795. }
  8796. func = ggml_cuda_mul_mat_id;
  8797. break;
  8798. case GGML_OP_SCALE:
  8799. func = ggml_cuda_scale;
  8800. break;
  8801. case GGML_OP_SQR:
  8802. func = ggml_cuda_sqr;
  8803. break;
  8804. case GGML_OP_CLAMP:
  8805. func = ggml_cuda_clamp;
  8806. break;
  8807. case GGML_OP_CPY:
  8808. func = ggml_cuda_cpy;
  8809. break;
  8810. case GGML_OP_CONT:
  8811. func = ggml_cuda_dup;
  8812. break;
  8813. case GGML_OP_NONE:
  8814. case GGML_OP_RESHAPE:
  8815. case GGML_OP_VIEW:
  8816. case GGML_OP_PERMUTE:
  8817. case GGML_OP_TRANSPOSE:
  8818. func = ggml_cuda_nop;
  8819. break;
  8820. case GGML_OP_DIAG_MASK_INF:
  8821. func = ggml_cuda_diag_mask_inf;
  8822. break;
  8823. case GGML_OP_SOFT_MAX:
  8824. func = ggml_cuda_soft_max;
  8825. break;
  8826. case GGML_OP_ROPE:
  8827. func = ggml_cuda_rope;
  8828. break;
  8829. case GGML_OP_ALIBI:
  8830. func = ggml_cuda_alibi;
  8831. break;
  8832. case GGML_OP_IM2COL:
  8833. func = ggml_cuda_im2col;
  8834. break;
  8835. case GGML_OP_POOL_2D:
  8836. func = ggml_cuda_pool2d;
  8837. break;
  8838. case GGML_OP_SUM_ROWS:
  8839. func = ggml_cuda_sum_rows;
  8840. break;
  8841. case GGML_OP_ARGSORT:
  8842. func = ggml_cuda_argsort;
  8843. break;
  8844. default:
  8845. return false;
  8846. }
  8847. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  8848. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8849. }
  8850. if (params->ith != 0) {
  8851. return true;
  8852. }
  8853. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  8854. return true;
  8855. }
  8856. func(tensor->src[0], tensor->src[1], tensor);
  8857. return true;
  8858. }
  8859. GGML_CALL int ggml_cuda_get_device_count() {
  8860. int device_count;
  8861. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8862. return 0;
  8863. }
  8864. return device_count;
  8865. }
  8866. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8867. cudaDeviceProp prop;
  8868. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8869. snprintf(description, description_size, "%s", prop.name);
  8870. }
  8871. ////////////////////////////////////////////////////////////////////////////////
  8872. // backend interface
  8873. #define UNUSED GGML_UNUSED
  8874. struct ggml_backend_cuda_context {
  8875. int device;
  8876. std::string name;
  8877. };
  8878. // cuda buffer
  8879. struct ggml_backend_cuda_buffer_context {
  8880. int device;
  8881. void * dev_ptr = nullptr;
  8882. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8883. size_t temp_tensor_extra_index = 0;
  8884. std::string name;
  8885. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  8886. device(device), dev_ptr(dev_ptr),
  8887. name(GGML_CUDA_NAME + std::to_string(device)) {
  8888. }
  8889. ~ggml_backend_cuda_buffer_context() {
  8890. delete[] temp_tensor_extras;
  8891. }
  8892. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8893. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  8894. if (temp_tensor_extras == nullptr) {
  8895. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8896. }
  8897. size_t alloc_index = temp_tensor_extra_index;
  8898. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8899. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8900. memset(extra, 0, sizeof(*extra));
  8901. return extra;
  8902. }
  8903. };
  8904. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  8905. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8906. return ctx->name.c_str();
  8907. }
  8908. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  8909. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  8910. }
  8911. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8912. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8913. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8914. delete ctx;
  8915. }
  8916. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8917. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8918. return ctx->dev_ptr;
  8919. }
  8920. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8921. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8922. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8923. assert(tensor->view_src->buffer->buft == buffer->buft);
  8924. tensor->backend = tensor->view_src->backend;
  8925. tensor->extra = tensor->view_src->extra;
  8926. return;
  8927. }
  8928. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8929. extra->data_device[ctx->device] = tensor->data;
  8930. tensor->backend = GGML_BACKEND_GPU;
  8931. tensor->extra = extra;
  8932. if (ggml_is_quantized(tensor->type)) {
  8933. // initialize padding to 0 to avoid possible NaN values
  8934. size_t original_size = ggml_nbytes(tensor);
  8935. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8936. if (padded_size > original_size && tensor->view_src == nullptr) {
  8937. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  8938. }
  8939. }
  8940. }
  8941. 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) {
  8942. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8943. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8944. ggml_cuda_set_device(ctx->device);
  8945. CUDA_CHECK(cudaDeviceSynchronize());
  8946. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8947. CUDA_CHECK(cudaDeviceSynchronize());
  8948. }
  8949. 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) {
  8950. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8951. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8952. ggml_cuda_set_device(ctx->device);
  8953. CUDA_CHECK(cudaDeviceSynchronize());
  8954. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8955. CUDA_CHECK(cudaDeviceSynchronize());
  8956. }
  8957. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8958. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  8959. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  8960. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8961. ggml_cuda_set_device(src_ctx->device);
  8962. CUDA_CHECK(cudaDeviceSynchronize());
  8963. ggml_cuda_set_device(dst_ctx->device);
  8964. CUDA_CHECK(cudaDeviceSynchronize());
  8965. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  8966. CUDA_CHECK(cudaDeviceSynchronize());
  8967. return true;
  8968. }
  8969. return false;
  8970. }
  8971. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8972. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8973. ggml_cuda_set_device(ctx->device);
  8974. CUDA_CHECK(cudaDeviceSynchronize());
  8975. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8976. CUDA_CHECK(cudaDeviceSynchronize());
  8977. }
  8978. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  8979. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  8980. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8981. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8982. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8983. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8984. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8985. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  8986. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8987. /* .reset = */ NULL,
  8988. };
  8989. // cuda buffer type
  8990. struct ggml_backend_cuda_buffer_type_context {
  8991. int device;
  8992. std::string name;
  8993. };
  8994. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8995. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8996. return ctx->name.c_str();
  8997. }
  8998. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8999. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9000. ggml_cuda_set_device(buft_ctx->device);
  9001. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  9002. void * dev_ptr;
  9003. cudaError_t err = cudaMalloc(&dev_ptr, size);
  9004. if (err != cudaSuccess) {
  9005. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  9006. return nullptr;
  9007. }
  9008. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  9009. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  9010. }
  9011. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9012. return 128;
  9013. UNUSED(buft);
  9014. }
  9015. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9016. size_t size = ggml_nbytes(tensor);
  9017. int64_t ne0 = tensor->ne[0];
  9018. if (ggml_is_quantized(tensor->type)) {
  9019. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9020. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9021. }
  9022. }
  9023. return size;
  9024. UNUSED(buft);
  9025. }
  9026. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9027. if (!ggml_backend_is_cuda(backend)) {
  9028. return false;
  9029. }
  9030. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9031. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9032. return buft_ctx->device == cuda_ctx->device;
  9033. }
  9034. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  9035. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  9036. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  9037. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  9038. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9039. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  9040. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  9041. /* .is_host = */ NULL,
  9042. };
  9043. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  9044. // FIXME: this is not thread safe
  9045. if (device >= ggml_backend_cuda_get_device_count()) {
  9046. return nullptr;
  9047. }
  9048. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  9049. static bool ggml_backend_cuda_buffer_type_initialized = false;
  9050. if (!ggml_backend_cuda_buffer_type_initialized) {
  9051. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  9052. ggml_backend_cuda_buffer_types[i] = {
  9053. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  9054. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  9055. };
  9056. }
  9057. ggml_backend_cuda_buffer_type_initialized = true;
  9058. }
  9059. return &ggml_backend_cuda_buffer_types[device];
  9060. }
  9061. // cuda split buffer
  9062. struct ggml_backend_cuda_split_buffer_context {
  9063. ~ggml_backend_cuda_split_buffer_context() {
  9064. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  9065. for (int id = 0; id < g_device_count; ++id) {
  9066. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9067. if (extra->events[id][is] != nullptr) {
  9068. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  9069. }
  9070. }
  9071. if (extra->data_device[id] != nullptr) {
  9072. CUDA_CHECK(cudaFree(extra->data_device[id]));
  9073. }
  9074. }
  9075. delete extra;
  9076. }
  9077. }
  9078. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  9079. };
  9080. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  9081. return GGML_CUDA_NAME "_Split";
  9082. UNUSED(buffer);
  9083. }
  9084. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  9085. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  9086. UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  9087. }
  9088. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9089. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9090. delete ctx;
  9091. }
  9092. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  9093. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  9094. return (void *)0x1000;
  9095. UNUSED(buffer);
  9096. }
  9097. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9098. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  9099. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9100. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9101. const int64_t ne0 = tensor->ne[0];
  9102. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  9103. ctx->tensor_extras.push_back(extra);
  9104. for (int id = 0; id < g_device_count; ++id) {
  9105. int64_t row_low, row_high;
  9106. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9107. int64_t nrows_split = row_high - row_low;
  9108. if (nrows_split == 0) {
  9109. continue;
  9110. }
  9111. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9112. const size_t original_size = size;
  9113. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9114. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9115. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9116. }
  9117. // FIXME: do not crash if cudaMalloc fails
  9118. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  9119. ggml_cuda_set_device(id);
  9120. char * buf;
  9121. CUDA_CHECK(cudaMalloc(&buf, size));
  9122. // set padding to 0 to avoid possible NaN values
  9123. if (size > original_size) {
  9124. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  9125. }
  9126. extra->data_device[id] = buf;
  9127. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9128. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  9129. }
  9130. }
  9131. tensor->backend = GGML_BACKEND_GPU_SPLIT;
  9132. tensor->extra = extra;
  9133. }
  9134. 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) {
  9135. // split tensors must always be set in their entirety at once
  9136. GGML_ASSERT(offset == 0);
  9137. GGML_ASSERT(size == ggml_nbytes(tensor));
  9138. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9139. const int64_t ne0 = tensor->ne[0];
  9140. const size_t nb1 = tensor->nb[1];
  9141. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9142. for (int id = 0; id < g_device_count; ++id) {
  9143. int64_t row_low, row_high;
  9144. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9145. int64_t nrows_split = row_high - row_low;
  9146. if (nrows_split == 0) {
  9147. continue;
  9148. }
  9149. const size_t offset_split = row_low*nb1;
  9150. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9151. const size_t original_size = size;
  9152. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9153. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9154. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9155. }
  9156. const char * buf_host = (const char *)data + offset_split;
  9157. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  9158. }
  9159. }
  9160. 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) {
  9161. // split tensors must always be set in their entirety at once
  9162. GGML_ASSERT(offset == 0);
  9163. GGML_ASSERT(size == ggml_nbytes(tensor));
  9164. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9165. const int64_t ne0 = tensor->ne[0];
  9166. const size_t nb1 = tensor->nb[1];
  9167. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9168. for (int id = 0; id < g_device_count; ++id) {
  9169. int64_t row_low, row_high;
  9170. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9171. int64_t nrows_split = row_high - row_low;
  9172. if (nrows_split == 0) {
  9173. continue;
  9174. }
  9175. const size_t offset_split = row_low*nb1;
  9176. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9177. const size_t original_size = size;
  9178. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9179. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9180. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9181. }
  9182. char * buf_host = (char *)data + offset_split;
  9183. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  9184. }
  9185. }
  9186. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9187. UNUSED(buffer);
  9188. UNUSED(value);
  9189. }
  9190. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  9191. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  9192. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  9193. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  9194. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  9195. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  9196. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  9197. /* .cpy_tensor = */ NULL,
  9198. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  9199. /* .reset = */ NULL,
  9200. };
  9201. // cuda split buffer type
  9202. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9203. return GGML_CUDA_NAME "_Split";
  9204. UNUSED(buft);
  9205. }
  9206. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9207. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  9208. // instead, we allocate them for each tensor separately in init_tensor
  9209. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  9210. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  9211. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  9212. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  9213. }
  9214. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9215. return 128;
  9216. UNUSED(buft);
  9217. }
  9218. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9219. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9220. size_t total_size = 0;
  9221. const int64_t ne0 = tensor->ne[0];
  9222. for (int id = 0; id < g_device_count; ++id) {
  9223. int64_t row_low, row_high;
  9224. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9225. int64_t nrows_split = row_high - row_low;
  9226. if (nrows_split == 0) {
  9227. continue;
  9228. }
  9229. total_size += ggml_nbytes_split(tensor, nrows_split);
  9230. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9231. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9232. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9233. }
  9234. }
  9235. return total_size;
  9236. }
  9237. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9238. return ggml_backend_is_cuda(backend);
  9239. UNUSED(buft);
  9240. }
  9241. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9242. return false;
  9243. UNUSED(buft);
  9244. }
  9245. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9246. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9247. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9248. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9249. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9250. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9251. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9252. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9253. };
  9254. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9255. // FIXME: this is not thread safe
  9256. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9257. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9258. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9259. if (all_zero) {
  9260. tensor_split_arr = g_default_tensor_split;
  9261. } else {
  9262. float split_sum = 0.0f;
  9263. for (int i = 0; i < g_device_count; ++i) {
  9264. tensor_split_arr[i] = split_sum;
  9265. split_sum += tensor_split[i];
  9266. }
  9267. for (int i = 0; i < g_device_count; ++i) {
  9268. tensor_split_arr[i] /= split_sum;
  9269. }
  9270. }
  9271. auto it = buft_map.find(tensor_split_arr);
  9272. if (it != buft_map.end()) {
  9273. return &it->second;
  9274. }
  9275. struct ggml_backend_buffer_type buft {
  9276. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9277. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9278. };
  9279. auto result = buft_map.emplace(tensor_split_arr, buft);
  9280. return &result.first->second;
  9281. }
  9282. // host buffer type
  9283. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9284. return GGML_CUDA_NAME "_Host";
  9285. UNUSED(buft);
  9286. }
  9287. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9288. return GGML_CUDA_NAME "_Host";
  9289. UNUSED(buffer);
  9290. }
  9291. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9292. ggml_cuda_host_free(buffer->context);
  9293. }
  9294. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9295. void * ptr = ggml_cuda_host_malloc(size);
  9296. if (ptr == nullptr) {
  9297. // fallback to cpu buffer
  9298. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9299. }
  9300. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9301. buffer->buft = buft;
  9302. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9303. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9304. return buffer;
  9305. }
  9306. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9307. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9308. /* .iface = */ {
  9309. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9310. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9311. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9312. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9313. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9314. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9315. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9316. },
  9317. /* .context = */ nullptr,
  9318. };
  9319. return &ggml_backend_cuda_buffer_type_host;
  9320. }
  9321. // backend
  9322. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9323. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9324. return cuda_ctx->name.c_str();
  9325. }
  9326. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9327. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9328. delete cuda_ctx;
  9329. delete backend;
  9330. }
  9331. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9332. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9333. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9334. }
  9335. 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) {
  9336. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9337. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9338. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9339. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9340. }
  9341. 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) {
  9342. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9343. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9344. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9345. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9346. }
  9347. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9348. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9349. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9350. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9351. return true;
  9352. }
  9353. return false;
  9354. }
  9355. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9356. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9357. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9358. UNUSED(backend);
  9359. }
  9360. GGML_CALL static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9361. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9362. ggml_cuda_set_main_device(cuda_ctx->device);
  9363. ggml_compute_params params = {};
  9364. params.type = GGML_TASK_COMPUTE;
  9365. params.ith = 0;
  9366. for (int i = 0; i < cgraph->n_nodes; i++) {
  9367. ggml_tensor * node = cgraph->nodes[i];
  9368. 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) {
  9369. continue;
  9370. }
  9371. #ifndef NDEBUG
  9372. assert(node->backend == GGML_BACKEND_GPU || node->backend == GGML_BACKEND_GPU_SPLIT);
  9373. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9374. assert(node->extra != nullptr);
  9375. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9376. if (node->src[j] != nullptr) {
  9377. assert(node->src[j]->backend == GGML_BACKEND_GPU || node->src[j]->backend == GGML_BACKEND_GPU_SPLIT);
  9378. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9379. assert(node->src[j]->extra != nullptr);
  9380. }
  9381. }
  9382. #endif
  9383. bool ok = ggml_cuda_compute_forward(&params, node);
  9384. if (!ok) {
  9385. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9386. }
  9387. GGML_ASSERT(ok);
  9388. }
  9389. return true;
  9390. }
  9391. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9392. switch (op->op) {
  9393. case GGML_OP_UNARY:
  9394. switch (ggml_get_unary_op(op)) {
  9395. case GGML_UNARY_OP_GELU:
  9396. case GGML_UNARY_OP_SILU:
  9397. case GGML_UNARY_OP_RELU:
  9398. case GGML_UNARY_OP_HARDSIGMOID:
  9399. case GGML_UNARY_OP_HARDSWISH:
  9400. case GGML_UNARY_OP_GELU_QUICK:
  9401. case GGML_UNARY_OP_TANH:
  9402. return true;
  9403. default:
  9404. return false;
  9405. }
  9406. break;
  9407. case GGML_OP_MUL_MAT:
  9408. case GGML_OP_MUL_MAT_ID:
  9409. {
  9410. struct ggml_tensor * a;
  9411. struct ggml_tensor * b;
  9412. if (op->op == GGML_OP_MUL_MAT) {
  9413. a = op->src[0];
  9414. b = op->src[1];
  9415. } else {
  9416. a = op->src[2];
  9417. b = op->src[1];
  9418. }
  9419. if (a->ne[3] != b->ne[3]) {
  9420. return false;
  9421. }
  9422. ggml_type a_type = a->type;
  9423. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  9424. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL) {
  9425. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9426. return false;
  9427. }
  9428. }
  9429. return true;
  9430. } break;
  9431. case GGML_OP_GET_ROWS:
  9432. {
  9433. switch (op->src[0]->type) {
  9434. case GGML_TYPE_F16:
  9435. case GGML_TYPE_F32:
  9436. case GGML_TYPE_Q4_0:
  9437. case GGML_TYPE_Q4_1:
  9438. case GGML_TYPE_Q5_0:
  9439. case GGML_TYPE_Q5_1:
  9440. case GGML_TYPE_Q8_0:
  9441. return true;
  9442. default:
  9443. return false;
  9444. }
  9445. } break;
  9446. case GGML_OP_CPY:
  9447. {
  9448. ggml_type src0_type = op->src[0]->type;
  9449. ggml_type src1_type = op->src[1]->type;
  9450. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9451. return true;
  9452. }
  9453. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9454. return true;
  9455. }
  9456. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9457. return true;
  9458. }
  9459. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9460. return true;
  9461. }
  9462. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9463. return true;
  9464. }
  9465. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9466. return true;
  9467. }
  9468. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9469. return true;
  9470. }
  9471. return false;
  9472. } break;
  9473. case GGML_OP_DUP:
  9474. case GGML_OP_REPEAT:
  9475. case GGML_OP_CONCAT:
  9476. {
  9477. ggml_type src0_type = op->src[0]->type;
  9478. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9479. } break;
  9480. case GGML_OP_NONE:
  9481. case GGML_OP_RESHAPE:
  9482. case GGML_OP_VIEW:
  9483. case GGML_OP_PERMUTE:
  9484. case GGML_OP_TRANSPOSE:
  9485. case GGML_OP_NORM:
  9486. case GGML_OP_ADD:
  9487. case GGML_OP_MUL:
  9488. case GGML_OP_DIV:
  9489. case GGML_OP_RMS_NORM:
  9490. case GGML_OP_SCALE:
  9491. case GGML_OP_SQR:
  9492. case GGML_OP_CLAMP:
  9493. case GGML_OP_CONT:
  9494. case GGML_OP_DIAG_MASK_INF:
  9495. case GGML_OP_SOFT_MAX:
  9496. case GGML_OP_ROPE:
  9497. case GGML_OP_ALIBI:
  9498. case GGML_OP_IM2COL:
  9499. case GGML_OP_POOL_2D:
  9500. case GGML_OP_SUM_ROWS:
  9501. case GGML_OP_ARGSORT:
  9502. case GGML_OP_ACC:
  9503. case GGML_OP_GROUP_NORM:
  9504. case GGML_OP_UPSCALE:
  9505. case GGML_OP_PAD:
  9506. case GGML_OP_LEAKY_RELU:
  9507. return true;
  9508. default:
  9509. return false;
  9510. }
  9511. UNUSED(backend);
  9512. }
  9513. static ggml_backend_i ggml_backend_cuda_interface = {
  9514. /* .get_name = */ ggml_backend_cuda_name,
  9515. /* .free = */ ggml_backend_cuda_free,
  9516. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  9517. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  9518. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  9519. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  9520. /* .synchronize = */ ggml_backend_cuda_synchronize,
  9521. /* .graph_plan_create = */ NULL,
  9522. /* .graph_plan_free = */ NULL,
  9523. /* .graph_plan_compute = */ NULL,
  9524. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  9525. /* .supports_op = */ ggml_backend_cuda_supports_op,
  9526. };
  9527. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  9528. ggml_init_cublas(); // TODO: remove from ggml.c
  9529. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  9530. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  9531. return nullptr;
  9532. }
  9533. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  9534. ggml_cuda_set_main_device(device);
  9535. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  9536. /* .device = */ device,
  9537. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  9538. };
  9539. ggml_backend_t cuda_backend = new ggml_backend {
  9540. /* .interface = */ ggml_backend_cuda_interface,
  9541. /* .context = */ ctx
  9542. };
  9543. return cuda_backend;
  9544. }
  9545. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  9546. return backend && backend->iface.get_name == ggml_backend_cuda_name;
  9547. }
  9548. GGML_CALL int ggml_backend_cuda_get_device_count() {
  9549. return ggml_cuda_get_device_count();
  9550. }
  9551. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  9552. ggml_cuda_get_device_description(device, description, description_size);
  9553. }
  9554. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  9555. ggml_cuda_set_device(device);
  9556. CUDA_CHECK(cudaMemGetInfo(free, total));
  9557. }
  9558. // backend registry
  9559. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  9560. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  9561. return cuda_backend;
  9562. UNUSED(params);
  9563. }
  9564. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  9565. GGML_CALL int ggml_backend_cuda_reg_devices() {
  9566. int device_count = ggml_cuda_get_device_count();
  9567. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  9568. for (int i = 0; i < device_count; i++) {
  9569. char name[128];
  9570. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  9571. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  9572. }
  9573. return device_count;
  9574. }