ggml-cuda.cu 433 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708
  1. #include "ggml-cuda.h"
  2. #include "ggml.h"
  3. #include "ggml-backend-impl.h"
  4. #if defined(GGML_USE_HIPBLAS)
  5. #define GGML_COMMON_DECL_HIP
  6. #define GGML_COMMON_IMPL_HIP
  7. #else
  8. #define GGML_COMMON_DECL_CUDA
  9. #define GGML_COMMON_IMPL_CUDA
  10. #endif
  11. #include "ggml-common.h"
  12. #include <algorithm>
  13. #include <array>
  14. #include <assert.h>
  15. #include <atomic>
  16. #include <cinttypes>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <float.h>
  20. #include <limits>
  21. #include <map>
  22. #include <memory>
  23. #include <mutex>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <string>
  27. #include <vector>
  28. // stringize macro for converting __CUDA_ARCH_LIST__ (list of integers) to string
  29. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  30. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  31. #if defined(GGML_USE_HIPBLAS)
  32. #include <hip/hip_runtime.h>
  33. #include <hipblas/hipblas.h>
  34. #include <hip/hip_fp16.h>
  35. #ifdef __HIP_PLATFORM_AMD__
  36. // for rocblas_initialize()
  37. #include "rocblas/rocblas.h"
  38. #endif // __HIP_PLATFORM_AMD__
  39. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  40. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  41. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  42. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  43. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  44. #define CUBLAS_OP_N HIPBLAS_OP_N
  45. #define CUBLAS_OP_T HIPBLAS_OP_T
  46. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  47. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  48. #define CUDA_R_16F HIPBLAS_R_16F
  49. #define CUDA_R_32F HIPBLAS_R_32F
  50. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  51. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  52. #define cublasCreate hipblasCreate
  53. #define cublasDestroy hipblasDestroy
  54. #define cublasGemmEx hipblasGemmEx
  55. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  56. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  57. #define cublasHandle_t hipblasHandle_t
  58. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  59. #define cublasSetStream hipblasSetStream
  60. #define cublasSgemm hipblasSgemm
  61. #define cublasStatus_t hipblasStatus_t
  62. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  63. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  64. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  65. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  66. #define cudaDeviceProp hipDeviceProp_t
  67. #define cudaDeviceSynchronize hipDeviceSynchronize
  68. #define cudaError_t hipError_t
  69. #define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
  70. #define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
  71. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  72. #define cudaEventDisableTiming hipEventDisableTiming
  73. #define cudaEventRecord hipEventRecord
  74. #define cudaEventSynchronize hipEventSynchronize
  75. #define cudaEvent_t hipEvent_t
  76. #define cudaEventDestroy hipEventDestroy
  77. #define cudaFree hipFree
  78. #define cudaFreeHost hipHostFree
  79. #define cudaGetDevice hipGetDevice
  80. #define cudaGetDeviceCount hipGetDeviceCount
  81. #define cudaGetDeviceProperties hipGetDeviceProperties
  82. #define cudaGetErrorString hipGetErrorString
  83. #define cudaGetLastError hipGetLastError
  84. #define cudaHostRegister hipHostRegister
  85. #define cudaHostRegisterPortable hipHostRegisterPortable
  86. #define cudaHostRegisterReadOnly hipHostRegisterReadOnly
  87. #define cudaHostUnregister hipHostUnregister
  88. #define cudaLaunchHostFunc hipLaunchHostFunc
  89. #ifdef GGML_HIP_UMA
  90. #define cudaMalloc hipMallocManaged
  91. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  92. #else
  93. #define cudaMalloc hipMalloc
  94. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  95. #endif
  96. #define cudaMemcpy hipMemcpy
  97. #define cudaMemcpyAsync hipMemcpyAsync
  98. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  99. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  100. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  101. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  102. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  103. #define cudaMemcpyKind hipMemcpyKind
  104. #define cudaMemset hipMemset
  105. #define cudaMemsetAsync hipMemsetAsync
  106. #define cudaMemGetInfo hipMemGetInfo
  107. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  108. #define cudaSetDevice hipSetDevice
  109. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  110. #define cudaStreamDestroy hipStreamDestroy
  111. #define cudaStreamFireAndForget hipStreamFireAndForget
  112. #define cudaStreamNonBlocking hipStreamNonBlocking
  113. #define cudaStreamPerThread hipStreamPerThread
  114. #define cudaStreamSynchronize hipStreamSynchronize
  115. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  116. #define cudaStream_t hipStream_t
  117. #define cudaSuccess hipSuccess
  118. #define __trap abort
  119. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  120. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  121. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  122. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  123. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  124. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  125. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  126. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  127. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  128. #else
  129. #include <cuda_runtime.h>
  130. #include <cuda.h>
  131. #include <cublas_v2.h>
  132. #include <cuda_fp16.h>
  133. #if CUDART_VERSION < 11020
  134. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  135. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  136. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  137. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  138. #define cublasComputeType_t cudaDataType_t
  139. #endif // CUDART_VERSION < 11020
  140. #endif // defined(GGML_USE_HIPBLAS)
  141. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  142. #define CC_PASCAL 600
  143. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  144. #define CC_VOLTA 700
  145. #define CC_OFFSET_AMD 1000000
  146. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  147. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  148. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  149. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  150. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  151. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  152. // - 7B quantum model: +100-200 MB
  153. // - 13B quantum model: +200-400 MB
  154. //
  155. //#define GGML_CUDA_FORCE_MMQ
  156. // TODO: improve this to be correct for more hardware
  157. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  158. #if !defined(GGML_CUDA_FORCE_MMQ)
  159. #define CUDA_USE_TENSOR_CORES
  160. #endif
  161. #define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
  162. #define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
  163. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  164. #if defined(_MSC_VER)
  165. #pragma warning(disable: 4244 4267) // possible loss of data
  166. #endif
  167. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  168. [[noreturn]]
  169. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  170. int id = -1; // in case cudaGetDevice fails
  171. cudaGetDevice(&id);
  172. fprintf(stderr, "CUDA error: %s\n", msg);
  173. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  174. fprintf(stderr, " %s\n", stmt);
  175. // abort with GGML_ASSERT to get a stack trace
  176. GGML_ASSERT(!"CUDA error");
  177. }
  178. #define CUDA_CHECK_GEN(err, success, error_fn) \
  179. do { \
  180. auto err_ = (err); \
  181. if (err_ != (success)) { \
  182. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  183. } \
  184. } while (0)
  185. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  186. #if CUDART_VERSION >= 12000
  187. static const char * cublas_get_error_str(const cublasStatus_t err) {
  188. return cublasGetStatusString(err);
  189. }
  190. #else
  191. static const char * cublas_get_error_str(const cublasStatus_t err) {
  192. switch (err) {
  193. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  194. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  195. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  196. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  197. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  198. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  199. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  200. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  201. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  202. default: return "unknown error";
  203. }
  204. }
  205. #endif // CUDART_VERSION >= 12000
  206. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  207. #if !defined(GGML_USE_HIPBLAS)
  208. static const char * cu_get_error_str(CUresult err) {
  209. const char * err_str;
  210. cuGetErrorString(err, &err_str);
  211. return err_str;
  212. }
  213. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  214. #endif
  215. #if CUDART_VERSION >= 11100
  216. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  217. #else
  218. #define GGML_CUDA_ASSUME(x)
  219. #endif // CUDART_VERSION >= 11100
  220. #define GGML_CUDA_MAX_STREAMS 8
  221. struct ggml_tensor_extra_gpu {
  222. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  223. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS]; // events for synchronizing multiple GPUs
  224. };
  225. // this is faster on Windows
  226. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  227. static void ggml_cuda_set_device(const int device) {
  228. int current_device;
  229. CUDA_CHECK(cudaGetDevice(&current_device));
  230. if (device == current_device) {
  231. return;
  232. }
  233. CUDA_CHECK(cudaSetDevice(device));
  234. }
  235. static int ggml_cuda_get_device() {
  236. int id;
  237. CUDA_CHECK(cudaGetDevice(&id));
  238. return id;
  239. }
  240. struct ggml_cuda_device_info {
  241. int device_count;
  242. struct cuda_device_info {
  243. int cc; // compute capability
  244. size_t smpb; // max. shared memory per block
  245. bool vmm; // virtual memory support
  246. size_t vmm_granularity; // granularity of virtual memory
  247. size_t total_vram;
  248. };
  249. cuda_device_info devices[GGML_CUDA_MAX_DEVICES] = {};
  250. std::array<float, GGML_CUDA_MAX_DEVICES> default_tensor_split = {};
  251. };
  252. static ggml_cuda_device_info ggml_cuda_init() {
  253. #ifdef __HIP_PLATFORM_AMD__
  254. // Workaround for a rocBLAS bug when using multiple graphics cards:
  255. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  256. rocblas_initialize();
  257. CUDA_CHECK(cudaDeviceSynchronize());
  258. #endif
  259. ggml_cuda_device_info info = {};
  260. cudaError_t err = cudaGetDeviceCount(&info.device_count);
  261. if (err != cudaSuccess) {
  262. fprintf(stderr, "%s: failed to initialize " GGML_CUDA_NAME ": %s\n", __func__, cudaGetErrorString(err));
  263. return info;
  264. }
  265. GGML_ASSERT(info.device_count <= GGML_CUDA_MAX_DEVICES);
  266. int64_t total_vram = 0;
  267. #if defined(GGML_CUDA_FORCE_MMQ)
  268. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  269. #else
  270. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  271. #endif
  272. #if defined(CUDA_USE_TENSOR_CORES)
  273. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  274. #else
  275. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  276. #endif
  277. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, info.device_count);
  278. for (int id = 0; id < info.device_count; ++id) {
  279. int device_vmm = 0;
  280. #if !defined(GGML_USE_HIPBLAS)
  281. CUdevice device;
  282. CU_CHECK(cuDeviceGet(&device, id));
  283. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  284. if (device_vmm) {
  285. CUmemAllocationProp alloc_prop = {};
  286. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  287. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  288. alloc_prop.location.id = id;
  289. CU_CHECK(cuMemGetAllocationGranularity(&info.devices[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  290. }
  291. #endif // !defined(GGML_USE_HIPBLAS)
  292. info.devices[id].vmm = !!device_vmm;
  293. cudaDeviceProp prop;
  294. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  295. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  296. info.default_tensor_split[id] = total_vram;
  297. total_vram += prop.totalGlobalMem;
  298. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  299. info.devices[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  300. #else
  301. info.devices[id].cc = 100*prop.major + 10*prop.minor;
  302. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  303. info.devices[id].smpb = prop.sharedMemPerBlock;
  304. }
  305. for (int id = 0; id < info.device_count; ++id) {
  306. info.default_tensor_split[id] /= total_vram;
  307. }
  308. // configure logging to stdout
  309. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  310. return info;
  311. }
  312. static const ggml_cuda_device_info & get_cuda_global_info() {
  313. static ggml_cuda_device_info info = ggml_cuda_init();
  314. return info;
  315. }
  316. // #define DEBUG_CUDA_MALLOC
  317. // buffer pool for cuda (legacy)
  318. struct ggml_cuda_pool {
  319. virtual ~ggml_cuda_pool() = default;
  320. virtual void * alloc(size_t size, size_t * actual_size) = 0;
  321. virtual void free(void * ptr, size_t size) = 0;
  322. };
  323. struct ggml_cuda_pool_leg : public ggml_cuda_pool {
  324. static const int MAX_BUFFERS = 256;
  325. int device;
  326. struct ggml_cuda_buffer {
  327. void * ptr = nullptr;
  328. size_t size = 0;
  329. };
  330. ggml_cuda_buffer buffer_pool[MAX_BUFFERS] = {};
  331. size_t pool_size = 0;
  332. explicit ggml_cuda_pool_leg(int device) :
  333. device(device) {
  334. }
  335. ~ggml_cuda_pool_leg() {
  336. ggml_cuda_set_device(device);
  337. for (int i = 0; i < MAX_BUFFERS; ++i) {
  338. ggml_cuda_buffer & b = buffer_pool[i];
  339. if (b.ptr != nullptr) {
  340. CUDA_CHECK(cudaFree(b.ptr));
  341. pool_size -= b.size;
  342. }
  343. }
  344. GGML_ASSERT(pool_size == 0);
  345. }
  346. void * alloc(size_t size, size_t * actual_size) override {
  347. #ifdef DEBUG_CUDA_MALLOC
  348. int nnz = 0;
  349. size_t max_size = 0;
  350. #endif
  351. size_t best_diff = 1ull << 36;
  352. int ibest = -1;
  353. for (int i = 0; i < MAX_BUFFERS; ++i) {
  354. ggml_cuda_buffer& b = buffer_pool[i];
  355. if (b.ptr != nullptr) {
  356. #ifdef DEBUG_CUDA_MALLOC
  357. ++nnz;
  358. if (b.size > max_size) max_size = b.size;
  359. #endif
  360. if (b.size >= size) {
  361. size_t diff = b.size - size;
  362. if (diff < best_diff) {
  363. best_diff = diff;
  364. ibest = i;
  365. if (!best_diff) {
  366. void * ptr = b.ptr;
  367. *actual_size = b.size;
  368. b.ptr = nullptr;
  369. b.size = 0;
  370. return ptr;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. if (ibest >= 0) {
  377. ggml_cuda_buffer& b = buffer_pool[ibest];
  378. void * ptr = b.ptr;
  379. *actual_size = b.size;
  380. b.ptr = nullptr;
  381. b.size = 0;
  382. return ptr;
  383. }
  384. void * ptr;
  385. size_t look_ahead_size = (size_t) (1.05 * size);
  386. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  387. ggml_cuda_set_device(device);
  388. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  389. *actual_size = look_ahead_size;
  390. pool_size += look_ahead_size;
  391. #ifdef DEBUG_CUDA_MALLOC
  392. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, device, nnz,
  393. (uint32_t)(max_size/1024/1024), (uint32_t)(pool_size/1024/1024), (uint32_t)(size/1024/1024));
  394. #endif
  395. return ptr;
  396. }
  397. void free(void * ptr, size_t size) override {
  398. for (int i = 0; i < MAX_BUFFERS; ++i) {
  399. ggml_cuda_buffer& b = buffer_pool[i];
  400. if (b.ptr == nullptr) {
  401. b.ptr = ptr;
  402. b.size = size;
  403. return;
  404. }
  405. }
  406. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  407. ggml_cuda_set_device(device);
  408. CUDA_CHECK(cudaFree(ptr));
  409. pool_size -= size;
  410. }
  411. };
  412. // pool with virtual memory
  413. #if !defined(GGML_USE_HIPBLAS)
  414. struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
  415. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  416. int device;
  417. CUdeviceptr pool_addr = 0;
  418. size_t pool_used = 0;
  419. size_t pool_size = 0;
  420. size_t granularity;
  421. explicit ggml_cuda_pool_vmm(int device) :
  422. device(device),
  423. granularity(get_cuda_global_info().devices[device].vmm_granularity) {
  424. }
  425. ~ggml_cuda_pool_vmm() {
  426. if (pool_addr != 0) {
  427. CU_CHECK(cuMemUnmap(pool_addr, pool_size));
  428. CU_CHECK(cuMemAddressFree(pool_addr, CUDA_POOL_VMM_MAX_SIZE));
  429. }
  430. }
  431. void * alloc(size_t size, size_t * actual_size) override {
  432. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  433. const size_t alignment = 128;
  434. size = alignment * ((size + alignment - 1) / alignment);
  435. size_t avail = pool_size - pool_used;
  436. if (size > avail) {
  437. // round up to the next multiple of the granularity
  438. size_t reserve_size = size - avail;
  439. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  440. GGML_ASSERT(pool_size + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  441. // allocate more physical memory
  442. CUmemAllocationProp prop = {};
  443. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  444. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  445. prop.location.id = device;
  446. CUmemGenericAllocationHandle handle;
  447. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  448. // reserve virtual address space (if not already reserved)
  449. if (pool_addr == 0) {
  450. CU_CHECK(cuMemAddressReserve(&pool_addr, CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  451. }
  452. // map at the end of the pool
  453. CU_CHECK(cuMemMap(pool_addr + pool_size, reserve_size, 0, handle, 0));
  454. // the memory allocation handle is no longer needed after mapping
  455. CU_CHECK(cuMemRelease(handle));
  456. // set access
  457. CUmemAccessDesc access = {};
  458. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  459. access.location.id = device;
  460. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  461. CU_CHECK(cuMemSetAccess(pool_addr + pool_size, reserve_size, &access, 1));
  462. // add to the pool
  463. pool_size += reserve_size;
  464. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  465. // id, (unsigned long long) (pool_size[id]/1024/1024),
  466. // (unsigned long long) (reserve_size/1024/1024));
  467. }
  468. GGML_ASSERT(pool_addr != 0);
  469. void * ptr = (void *) (pool_addr + pool_used);
  470. *actual_size = size;
  471. pool_used += size;
  472. #ifdef DEBUG_CUDA_MALLOC
  473. printf("cuda pool[%d]: allocated %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  474. #endif
  475. return ptr;
  476. }
  477. void free(void * ptr, size_t size) override {
  478. #ifdef DEBUG_CUDA_MALLOC
  479. printf("cuda pool[%d]: freed %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  480. #endif
  481. pool_used -= size;
  482. // all deallocations must be in reverse order of the allocations
  483. GGML_ASSERT(ptr == (void *) (pool_addr + pool_used));
  484. }
  485. };
  486. #endif // !defined(GGML_USE_HIPBLAS)
  487. template<typename T>
  488. struct ggml_cuda_pool_alloc {
  489. ggml_cuda_pool * pool = nullptr;
  490. T * ptr = nullptr;
  491. size_t actual_size = 0;
  492. ggml_cuda_pool_alloc() = default;
  493. explicit ggml_cuda_pool_alloc(ggml_cuda_pool & pool) : pool(&pool) {
  494. }
  495. ggml_cuda_pool_alloc(ggml_cuda_pool & pool, size_t size) : pool(&pool) {
  496. alloc(size);
  497. }
  498. ~ggml_cuda_pool_alloc() {
  499. if (ptr != nullptr) {
  500. pool->free(ptr, actual_size);
  501. }
  502. }
  503. // size is in number of elements
  504. T * alloc(size_t size) {
  505. GGML_ASSERT(pool != nullptr);
  506. GGML_ASSERT(ptr == nullptr);
  507. ptr = (T *) pool->alloc(size * sizeof(T), &this->actual_size);
  508. return ptr;
  509. }
  510. T * alloc(ggml_cuda_pool & pool, size_t size) {
  511. this->pool = &pool;
  512. return alloc(size);
  513. }
  514. T * get() {
  515. return ptr;
  516. }
  517. ggml_cuda_pool_alloc(const ggml_cuda_pool_alloc &) = delete;
  518. ggml_cuda_pool_alloc(ggml_cuda_pool_alloc &&) = delete;
  519. ggml_cuda_pool_alloc& operator=(const ggml_cuda_pool_alloc &) = delete;
  520. ggml_cuda_pool_alloc& operator=(ggml_cuda_pool_alloc &&) = delete;
  521. };
  522. // backend interface
  523. struct ggml_backend_cuda_context {
  524. int device;
  525. std::string name;
  526. cudaEvent_t copy_event = nullptr;
  527. cudaStream_t streams[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS] = { { nullptr } };
  528. cublasHandle_t cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  529. explicit ggml_backend_cuda_context(int device) :
  530. device(device),
  531. name(GGML_CUDA_NAME + std::to_string(device)) {
  532. }
  533. ~ggml_backend_cuda_context() {
  534. if (copy_event != nullptr) {
  535. CUDA_CHECK(cudaEventDestroy(copy_event));
  536. }
  537. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; ++i) {
  538. for (int j = 0; j < GGML_CUDA_MAX_STREAMS; ++j) {
  539. if (streams[i][j] != nullptr) {
  540. CUDA_CHECK(cudaStreamDestroy(streams[i][j]));
  541. }
  542. }
  543. if (cublas_handles[i] != nullptr) {
  544. CUBLAS_CHECK(cublasDestroy(cublas_handles[i]));
  545. }
  546. }
  547. }
  548. cudaStream_t stream(int device, int stream) {
  549. if (streams[device][stream] == nullptr) {
  550. ggml_cuda_set_device(device);
  551. CUDA_CHECK(cudaStreamCreateWithFlags(&streams[device][stream], cudaStreamNonBlocking));
  552. }
  553. return streams[device][stream];
  554. }
  555. cudaStream_t stream() {
  556. return stream(device, 0);
  557. }
  558. cublasHandle_t cublas_handle(int device) {
  559. if (cublas_handles[device] == nullptr) {
  560. ggml_cuda_set_device(device);
  561. CUBLAS_CHECK(cublasCreate(&cublas_handles[device]));
  562. CUBLAS_CHECK(cublasSetMathMode(cublas_handles[device], CUBLAS_TF32_TENSOR_OP_MATH));
  563. }
  564. return cublas_handles[device];
  565. }
  566. cublasHandle_t cublas_handle() {
  567. return cublas_handle(device);
  568. }
  569. // pool
  570. std::unique_ptr<ggml_cuda_pool> pools[GGML_CUDA_MAX_DEVICES];
  571. static std::unique_ptr<ggml_cuda_pool> new_pool_for_device(int device) {
  572. #if !defined(GGML_USE_HIPBLAS)
  573. if (get_cuda_global_info().devices[device].vmm) {
  574. return std::unique_ptr<ggml_cuda_pool>(new ggml_cuda_pool_vmm(device));
  575. }
  576. #endif
  577. return std::unique_ptr<ggml_cuda_pool>(new ggml_cuda_pool_leg(device));
  578. }
  579. ggml_cuda_pool & pool(int device) {
  580. if (pools[device] == nullptr) {
  581. pools[device] = new_pool_for_device(device);
  582. }
  583. return *pools[device];
  584. }
  585. ggml_cuda_pool & pool() {
  586. return pool(device);
  587. }
  588. };
  589. // cuda buffer
  590. struct ggml_backend_cuda_buffer_context {
  591. int device;
  592. void * dev_ptr = nullptr;
  593. std::string name;
  594. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  595. device(device), dev_ptr(dev_ptr),
  596. name(GGML_CUDA_NAME + std::to_string(device)) {
  597. }
  598. ~ggml_backend_cuda_buffer_context() {
  599. CUDA_CHECK(cudaFree(dev_ptr));
  600. }
  601. };
  602. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  603. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  604. return ctx->name.c_str();
  605. }
  606. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  607. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  608. }
  609. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  610. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  611. delete ctx;
  612. }
  613. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  614. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  615. return ctx->dev_ptr;
  616. }
  617. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  618. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  619. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  620. assert(tensor->view_src->buffer->buft == buffer->buft);
  621. tensor->backend = tensor->view_src->backend;
  622. tensor->extra = tensor->view_src->extra;
  623. return;
  624. }
  625. if (ggml_is_quantized(tensor->type)) {
  626. // initialize padding to 0 to avoid possible NaN values
  627. size_t original_size = ggml_nbytes(tensor);
  628. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  629. if (padded_size > original_size && tensor->view_src == nullptr) {
  630. ggml_cuda_set_device(ctx->device);
  631. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  632. }
  633. }
  634. }
  635. 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) {
  636. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  637. ggml_cuda_set_device(ctx->device);
  638. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, cudaStreamPerThread));
  639. CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread));
  640. }
  641. 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) {
  642. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  643. ggml_cuda_set_device(ctx->device);
  644. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, cudaStreamPerThread));
  645. CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread));
  646. }
  647. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  648. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  649. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  650. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)dst->buffer->context;
  651. if (src_ctx->device == dst_ctx->device) {
  652. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice, cudaStreamPerThread));
  653. } else {
  654. #ifdef GGML_CUDA_NO_PEER_COPY
  655. return false;
  656. #else
  657. CUDA_CHECK(cudaMemcpyPeerAsync(dst->data, dst_ctx->device, src->data, src_ctx->device, ggml_nbytes(src), cudaStreamPerThread));
  658. #endif
  659. }
  660. CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread));
  661. return true;
  662. }
  663. return false;
  664. GGML_UNUSED(buffer);
  665. }
  666. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  667. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  668. ggml_cuda_set_device(ctx->device);
  669. CUDA_CHECK(cudaDeviceSynchronize());
  670. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  671. CUDA_CHECK(cudaDeviceSynchronize());
  672. }
  673. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  674. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  675. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  676. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  677. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  678. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  679. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  680. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  681. /* .clear = */ ggml_backend_cuda_buffer_clear,
  682. /* .reset = */ NULL,
  683. };
  684. // cuda buffer type
  685. struct ggml_backend_cuda_buffer_type_context {
  686. int device;
  687. std::string name;
  688. };
  689. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  690. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  691. return ctx->name.c_str();
  692. }
  693. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  694. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  695. ggml_cuda_set_device(buft_ctx->device);
  696. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  697. void * dev_ptr;
  698. cudaError_t err = cudaMalloc(&dev_ptr, size);
  699. if (err != cudaSuccess) {
  700. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  701. return nullptr;
  702. }
  703. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  704. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  705. }
  706. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  707. return 128;
  708. GGML_UNUSED(buft);
  709. }
  710. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  711. size_t size = ggml_nbytes(tensor);
  712. int64_t ne0 = tensor->ne[0];
  713. if (ggml_is_quantized(tensor->type)) {
  714. if (ne0 % MATRIX_ROW_PADDING != 0) {
  715. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  716. }
  717. }
  718. return size;
  719. GGML_UNUSED(buft);
  720. }
  721. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  722. if (!ggml_backend_is_cuda(backend)) {
  723. return false;
  724. }
  725. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  726. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  727. return buft_ctx->device == cuda_ctx->device;
  728. }
  729. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  730. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  731. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  732. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  733. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  734. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  735. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  736. /* .is_host = */ NULL,
  737. };
  738. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  739. static std::mutex mutex;
  740. std::lock_guard<std::mutex> lock(mutex);
  741. if (device >= ggml_backend_cuda_get_device_count()) {
  742. return nullptr;
  743. }
  744. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  745. static bool ggml_backend_cuda_buffer_type_initialized = false;
  746. if (!ggml_backend_cuda_buffer_type_initialized) {
  747. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  748. ggml_backend_cuda_buffer_types[i] = {
  749. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  750. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  751. };
  752. }
  753. ggml_backend_cuda_buffer_type_initialized = true;
  754. }
  755. return &ggml_backend_cuda_buffer_types[device];
  756. }
  757. // cuda split buffer
  758. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  759. int64_t min_compute_capability = INT_MAX;
  760. int64_t max_compute_capability = INT_MIN;
  761. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  762. if (tensor_split[id] < (id + 1 < ggml_backend_cuda_get_device_count() ? tensor_split[id + 1] : 1.0f)) {
  763. if (min_compute_capability > get_cuda_global_info().devices[id].cc) {
  764. min_compute_capability = get_cuda_global_info().devices[id].cc;
  765. }
  766. if (max_compute_capability < get_cuda_global_info().devices[id].cc) {
  767. max_compute_capability = get_cuda_global_info().devices[id].cc;
  768. }
  769. }
  770. }
  771. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  772. switch(type) {
  773. case GGML_TYPE_Q4_0:
  774. case GGML_TYPE_Q4_1:
  775. case GGML_TYPE_Q5_0:
  776. case GGML_TYPE_Q5_1:
  777. case GGML_TYPE_Q8_0:
  778. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  779. case GGML_TYPE_F16:
  780. case GGML_TYPE_F32:
  781. return 1;
  782. case GGML_TYPE_Q2_K:
  783. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  784. case GGML_TYPE_Q3_K:
  785. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  786. case GGML_TYPE_Q4_K:
  787. case GGML_TYPE_Q5_K:
  788. case GGML_TYPE_Q6_K:
  789. case GGML_TYPE_IQ2_XXS:
  790. case GGML_TYPE_IQ2_XS:
  791. case GGML_TYPE_IQ2_S:
  792. case GGML_TYPE_IQ3_XXS:
  793. case GGML_TYPE_IQ1_S:
  794. case GGML_TYPE_IQ4_NL:
  795. case GGML_TYPE_IQ4_XS:
  796. case GGML_TYPE_IQ3_S:
  797. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  798. default:
  799. GGML_ASSERT(false);
  800. }
  801. #else
  802. switch(type) {
  803. case GGML_TYPE_Q4_0:
  804. case GGML_TYPE_Q4_1:
  805. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  806. case GGML_TYPE_Q5_0:
  807. case GGML_TYPE_Q5_1:
  808. case GGML_TYPE_Q8_0:
  809. return 64;
  810. case GGML_TYPE_F16:
  811. case GGML_TYPE_F32:
  812. return 1;
  813. case GGML_TYPE_Q2_K:
  814. case GGML_TYPE_Q3_K:
  815. case GGML_TYPE_Q4_K:
  816. case GGML_TYPE_Q5_K:
  817. case GGML_TYPE_IQ2_XXS:
  818. case GGML_TYPE_IQ2_XS:
  819. case GGML_TYPE_IQ2_S:
  820. case GGML_TYPE_IQ3_XXS:
  821. case GGML_TYPE_IQ1_S:
  822. case GGML_TYPE_IQ4_NL:
  823. case GGML_TYPE_IQ4_XS:
  824. case GGML_TYPE_IQ3_S:
  825. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  826. case GGML_TYPE_Q6_K:
  827. return 64;
  828. default:
  829. GGML_ASSERT(false);
  830. }
  831. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  832. }
  833. 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) {
  834. const int64_t nrows = ggml_nrows(tensor);
  835. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  836. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  837. *row_low -= *row_low % rounding;
  838. if (id == ggml_backend_cuda_get_device_count() - 1) {
  839. *row_high = nrows;
  840. } else {
  841. *row_high = nrows*tensor_split[id + 1];
  842. *row_high -= *row_high % rounding;
  843. }
  844. }
  845. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  846. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  847. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  848. }
  849. struct ggml_backend_cuda_split_buffer_type_context {
  850. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  851. };
  852. struct ggml_backend_cuda_split_buffer_context {
  853. ~ggml_backend_cuda_split_buffer_context() {
  854. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  855. for (int id = 0; id < GGML_CUDA_MAX_DEVICES; ++id) {
  856. for (int64_t is = 0; is < GGML_CUDA_MAX_STREAMS; ++is) {
  857. if (extra->events[id][is] != nullptr) {
  858. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  859. }
  860. }
  861. if (extra->data_device[id] != nullptr) {
  862. CUDA_CHECK(cudaFree(extra->data_device[id]));
  863. }
  864. }
  865. delete extra;
  866. }
  867. }
  868. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  869. };
  870. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  871. return GGML_CUDA_NAME "_Split";
  872. GGML_UNUSED(buffer);
  873. }
  874. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  875. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  876. GGML_UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  877. }
  878. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  879. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  880. delete ctx;
  881. }
  882. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  883. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  884. return (void *)0x1000;
  885. GGML_UNUSED(buffer);
  886. }
  887. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  888. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  889. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  890. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  891. const int64_t ne0 = tensor->ne[0];
  892. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  893. ctx->tensor_extras.push_back(extra);
  894. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  895. int64_t row_low, row_high;
  896. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  897. int64_t nrows_split = row_high - row_low;
  898. if (nrows_split == 0) {
  899. continue;
  900. }
  901. size_t size = ggml_nbytes_split(tensor, nrows_split);
  902. const size_t original_size = size;
  903. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  904. if (ne0 % MATRIX_ROW_PADDING != 0) {
  905. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  906. }
  907. // FIXME: do not crash if cudaMalloc fails
  908. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  909. ggml_cuda_set_device(id);
  910. char * buf;
  911. CUDA_CHECK(cudaMalloc(&buf, size));
  912. // set padding to 0 to avoid possible NaN values
  913. if (size > original_size) {
  914. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  915. }
  916. extra->data_device[id] = buf;
  917. for (int64_t is = 0; is < GGML_CUDA_MAX_STREAMS; ++is) {
  918. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  919. }
  920. }
  921. tensor->extra = extra;
  922. }
  923. 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) {
  924. // split tensors must always be set in their entirety at once
  925. GGML_ASSERT(offset == 0);
  926. GGML_ASSERT(size == ggml_nbytes(tensor));
  927. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  928. const int64_t ne0 = tensor->ne[0];
  929. const size_t nb1 = tensor->nb[1];
  930. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  931. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  932. int64_t row_low, row_high;
  933. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  934. int64_t nrows_split = row_high - row_low;
  935. if (nrows_split == 0) {
  936. continue;
  937. }
  938. const size_t offset_split = row_low*nb1;
  939. size_t size = ggml_nbytes_split(tensor, nrows_split);
  940. const size_t original_size = size;
  941. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  942. if (ne0 % MATRIX_ROW_PADDING != 0) {
  943. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  944. }
  945. const char * buf_host = (const char *)data + offset_split;
  946. CUDA_CHECK(cudaMemcpyAsync(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice, cudaStreamPerThread));
  947. }
  948. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  949. CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread));
  950. }
  951. }
  952. 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) {
  953. // split tensors must always be set in their entirety at once
  954. GGML_ASSERT(offset == 0);
  955. GGML_ASSERT(size == ggml_nbytes(tensor));
  956. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  957. const int64_t ne0 = tensor->ne[0];
  958. const size_t nb1 = tensor->nb[1];
  959. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  960. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  961. int64_t row_low, row_high;
  962. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  963. int64_t nrows_split = row_high - row_low;
  964. if (nrows_split == 0) {
  965. continue;
  966. }
  967. const size_t offset_split = row_low*nb1;
  968. size_t size = ggml_nbytes_split(tensor, nrows_split);
  969. const size_t original_size = size;
  970. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  971. if (ne0 % MATRIX_ROW_PADDING != 0) {
  972. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  973. }
  974. char * buf_host = (char *)data + offset_split;
  975. CUDA_CHECK(cudaMemcpyAsync(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost, cudaStreamPerThread));
  976. }
  977. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  978. CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread));
  979. }
  980. }
  981. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  982. GGML_UNUSED(buffer);
  983. GGML_UNUSED(value);
  984. }
  985. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  986. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  987. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  988. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  989. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  990. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  991. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  992. /* .cpy_tensor = */ NULL,
  993. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  994. /* .reset = */ NULL,
  995. };
  996. // cuda split buffer type
  997. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  998. return GGML_CUDA_NAME "_Split";
  999. GGML_UNUSED(buft);
  1000. }
  1001. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  1002. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  1003. // instead, we allocate them for each tensor separately in init_tensor
  1004. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  1005. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  1006. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  1007. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  1008. }
  1009. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  1010. return 128;
  1011. GGML_UNUSED(buft);
  1012. }
  1013. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  1014. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  1015. size_t total_size = 0;
  1016. const int64_t ne0 = tensor->ne[0];
  1017. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  1018. int64_t row_low, row_high;
  1019. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  1020. int64_t nrows_split = row_high - row_low;
  1021. if (nrows_split == 0) {
  1022. continue;
  1023. }
  1024. total_size += ggml_nbytes_split(tensor, nrows_split);
  1025. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  1026. if (ne0 % MATRIX_ROW_PADDING != 0) {
  1027. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  1028. }
  1029. }
  1030. return total_size;
  1031. }
  1032. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  1033. return ggml_backend_is_cuda(backend);
  1034. GGML_UNUSED(buft);
  1035. }
  1036. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  1037. return false;
  1038. GGML_UNUSED(buft);
  1039. }
  1040. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  1041. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  1042. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  1043. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  1044. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  1045. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  1046. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  1047. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  1048. };
  1049. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  1050. static std::mutex mutex;
  1051. std::lock_guard<std::mutex> lock(mutex);
  1052. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  1053. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  1054. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  1055. if (all_zero) {
  1056. tensor_split_arr = get_cuda_global_info().default_tensor_split;
  1057. } else {
  1058. float split_sum = 0.0f;
  1059. for (int i = 0; i < ggml_backend_cuda_get_device_count(); ++i) {
  1060. tensor_split_arr[i] = split_sum;
  1061. split_sum += tensor_split[i];
  1062. }
  1063. for (int i = 0; i < ggml_backend_cuda_get_device_count(); ++i) {
  1064. tensor_split_arr[i] /= split_sum;
  1065. }
  1066. }
  1067. auto it = buft_map.find(tensor_split_arr);
  1068. if (it != buft_map.end()) {
  1069. return &it->second;
  1070. }
  1071. struct ggml_backend_buffer_type buft {
  1072. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  1073. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  1074. };
  1075. auto result = buft_map.emplace(tensor_split_arr, buft);
  1076. return &result.first->second;
  1077. }
  1078. // host buffer type
  1079. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  1080. return GGML_CUDA_NAME "_Host";
  1081. GGML_UNUSED(buft);
  1082. }
  1083. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  1084. return GGML_CUDA_NAME "_Host";
  1085. GGML_UNUSED(buffer);
  1086. }
  1087. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  1088. CUDA_CHECK(cudaFreeHost(buffer->context));
  1089. }
  1090. static void * ggml_cuda_host_malloc(size_t size) {
  1091. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  1092. return nullptr;
  1093. }
  1094. void * ptr = nullptr;
  1095. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  1096. if (err != cudaSuccess) {
  1097. // clear the error
  1098. cudaGetLastError();
  1099. fprintf(stderr, "%s: warning: failed to allocate %.2f MiB of pinned memory: %s\n", __func__,
  1100. size/1024.0/1024.0, cudaGetErrorString(err));
  1101. return nullptr;
  1102. }
  1103. return ptr;
  1104. }
  1105. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  1106. void * ptr = ggml_cuda_host_malloc(size);
  1107. if (ptr == nullptr) {
  1108. // fallback to cpu buffer
  1109. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  1110. }
  1111. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  1112. buffer->buft = buft;
  1113. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  1114. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  1115. return buffer;
  1116. }
  1117. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  1118. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  1119. /* .iface = */ {
  1120. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  1121. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  1122. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  1123. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  1124. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  1125. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  1126. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  1127. },
  1128. /* .context = */ nullptr,
  1129. };
  1130. return &ggml_backend_cuda_buffer_type_host;
  1131. }
  1132. //static bool ggml_backend_buffer_is_cuda_host(ggml_backend_buffer_t buffer) {
  1133. // return buffer->buft->iface.get_name == ggml_backend_cuda_host_buffer_type_name;
  1134. //}
  1135. /// kernels
  1136. #if defined(GGML_USE_HIPBLAS)
  1137. #define __CUDA_ARCH__ 1300
  1138. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  1139. defined(__gfx1150__) || defined(__gfx1151__)
  1140. #define RDNA3
  1141. #endif
  1142. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  1143. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  1144. #define RDNA2
  1145. #endif
  1146. #ifndef __has_builtin
  1147. #define __has_builtin(x) 0
  1148. #endif
  1149. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  1150. typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4)));
  1151. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  1152. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  1153. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  1154. #if __has_builtin(__builtin_elementwise_sub_sat)
  1155. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  1156. return reinterpret_cast<const int &>(c);
  1157. #else
  1158. int8x4_t c;
  1159. int16_t tmp;
  1160. #pragma unroll
  1161. for (int i = 0; i < 4; i++) {
  1162. tmp = va[i] - vb[i];
  1163. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  1164. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  1165. c[i] = tmp;
  1166. }
  1167. return reinterpret_cast<int &>(c);
  1168. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  1169. }
  1170. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  1171. return __vsubss4(a, b);
  1172. }
  1173. static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) {
  1174. const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a);
  1175. const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b);
  1176. unsigned int c;
  1177. uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c);
  1178. #pragma unroll
  1179. for (int i = 0; i < 4; ++i) {
  1180. vc[i] = va[i] == vb[i] ? 0xff : 0x00;
  1181. }
  1182. return c;
  1183. }
  1184. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  1185. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  1186. c = __builtin_amdgcn_sdot4(a, b, c, false);
  1187. #elif defined(RDNA3)
  1188. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  1189. #elif defined(__gfx1010__) || defined(__gfx900__)
  1190. int tmp1;
  1191. int tmp2;
  1192. asm("\n \
  1193. 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 \
  1194. 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 \
  1195. v_add3_u32 %0, %1, %2, %0 \n \
  1196. 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 \
  1197. 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 \
  1198. v_add3_u32 %0, %1, %2, %0 \n \
  1199. "
  1200. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  1201. : "v"(a), "v"(b)
  1202. );
  1203. #else
  1204. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  1205. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  1206. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  1207. #endif
  1208. return c;
  1209. }
  1210. #endif // defined(GGML_USE_HIPBLAS)
  1211. #ifdef GGML_CUDA_F16
  1212. typedef half dfloat; // dequantize float
  1213. typedef half2 dfloat2;
  1214. #else
  1215. typedef float dfloat; // dequantize float
  1216. typedef float2 dfloat2;
  1217. #endif //GGML_CUDA_F16
  1218. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  1219. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  1220. int x32 = 0;
  1221. x32 |= x16[0] << 0;
  1222. x32 |= x16[1] << 16;
  1223. return x32;
  1224. }
  1225. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  1226. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  1227. int x32 = 0;
  1228. x32 |= x16[0] << 0;
  1229. x32 |= x16[1] << 16;
  1230. return x32;
  1231. }
  1232. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  1233. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  1234. }
  1235. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  1236. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  1237. }
  1238. template<typename T>
  1239. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  1240. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  1241. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  1242. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  1243. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  1244. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  1245. typedef void (*ggml_cuda_func_t)(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  1246. typedef void (*ggml_cuda_op_mul_mat_t)(
  1247. ggml_backend_cuda_context & ctx,
  1248. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  1249. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  1250. const int64_t src1_padded_row_size, cudaStream_t stream);
  1251. typedef void (*ggml_cuda_op_flatten_t)(
  1252. ggml_backend_cuda_context & ctx,
  1253. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  1254. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  1255. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  1256. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  1257. typedef void (*load_tiles_cuda_t)(
  1258. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1259. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  1260. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  1261. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1262. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  1263. #define WARP_SIZE 32
  1264. #define CUDA_GELU_BLOCK_SIZE 256
  1265. #define CUDA_SILU_BLOCK_SIZE 256
  1266. #define CUDA_TANH_BLOCK_SIZE 256
  1267. #define CUDA_RELU_BLOCK_SIZE 256
  1268. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  1269. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  1270. #define CUDA_SQR_BLOCK_SIZE 256
  1271. #define CUDA_CPY_BLOCK_SIZE 32
  1272. #define CUDA_SCALE_BLOCK_SIZE 256
  1273. #define CUDA_CLAMP_BLOCK_SIZE 256
  1274. #define CUDA_ROPE_BLOCK_SIZE 256
  1275. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  1276. #define CUDA_ALIBI_BLOCK_SIZE 32
  1277. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  1278. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  1279. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  1280. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  1281. #define CUDA_UPSCALE_BLOCK_SIZE 256
  1282. #define CUDA_CONCAT_BLOCK_SIZE 256
  1283. #define CUDA_PAD_BLOCK_SIZE 256
  1284. #define CUDA_ARANGE_BLOCK_SIZE 256
  1285. #define CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE 256
  1286. #define CUDA_ACC_BLOCK_SIZE 256
  1287. #define CUDA_IM2COL_BLOCK_SIZE 256
  1288. #define CUDA_POOL2D_BLOCK_SIZE 256
  1289. #define CUDA_Q8_0_NE_ALIGN 2048
  1290. // dmmv = dequantize_mul_mat_vec
  1291. #ifndef GGML_CUDA_DMMV_X
  1292. #define GGML_CUDA_DMMV_X 32
  1293. #endif
  1294. #ifndef GGML_CUDA_MMV_Y
  1295. #define GGML_CUDA_MMV_Y 1
  1296. #endif
  1297. #ifndef K_QUANTS_PER_ITERATION
  1298. #define K_QUANTS_PER_ITERATION 2
  1299. #else
  1300. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  1301. #endif
  1302. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  1303. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  1304. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  1305. #define MUL_MAT_SRC1_COL_STRIDE 128
  1306. [[noreturn]]
  1307. static __device__ void no_device_code(
  1308. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  1309. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  1310. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  1311. file_name, line, function_name, arch);
  1312. GGML_UNUSED(arch_list);
  1313. #else
  1314. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  1315. file_name, line, function_name, arch, arch_list);
  1316. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  1317. __trap();
  1318. GGML_UNUSED(no_device_code); // suppress unused function warning
  1319. }
  1320. #ifdef __CUDA_ARCH__
  1321. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  1322. #else
  1323. //#define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  1324. #define NO_DEVICE_CODE
  1325. #endif // __CUDA_ARCH__
  1326. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  1327. #pragma unroll
  1328. for (int mask = 16; mask > 0; mask >>= 1) {
  1329. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  1330. }
  1331. return x;
  1332. }
  1333. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  1334. #pragma unroll
  1335. for (int mask = 16; mask > 0; mask >>= 1) {
  1336. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  1337. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  1338. }
  1339. return a;
  1340. }
  1341. #ifdef GGML_CUDA_F16
  1342. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  1343. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  1344. #pragma unroll
  1345. for (int mask = 16; mask > 0; mask >>= 1) {
  1346. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  1347. }
  1348. return a;
  1349. #else
  1350. GGML_UNUSED(a);
  1351. NO_DEVICE_CODE;
  1352. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  1353. }
  1354. #endif // GGML_CUDA_F16
  1355. static __device__ __forceinline__ float warp_reduce_max(float x) {
  1356. #pragma unroll
  1357. for (int mask = 16; mask > 0; mask >>= 1) {
  1358. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  1359. }
  1360. return x;
  1361. }
  1362. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  1363. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  1364. //#pragma unroll
  1365. // for (int mask = 16; mask > 0; mask >>= 1) {
  1366. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  1367. // }
  1368. // return x;
  1369. //#else
  1370. // GGML_UNUSED(x);
  1371. // NO_DEVICE_CODE;
  1372. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  1373. //}
  1374. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  1375. return b;
  1376. GGML_UNUSED(a);
  1377. }
  1378. static __device__ __forceinline__ float op_add(const float a, const float b) {
  1379. return a + b;
  1380. }
  1381. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  1382. return a * b;
  1383. }
  1384. static __device__ __forceinline__ float op_div(const float a, const float b) {
  1385. return a / b;
  1386. }
  1387. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  1388. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  1389. int ne0, int ne1, int ne2, int ne3,
  1390. int ne10, int ne11, int ne12, int ne13,
  1391. /*int s0, */ int s1, int s2, int s3,
  1392. /*int s10,*/ int s11, int s12, int s13) {
  1393. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  1394. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  1395. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  1396. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  1397. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  1398. return;
  1399. }
  1400. const int i11 = i1 % ne11;
  1401. const int i12 = i2 % ne12;
  1402. const int i13 = i3 % ne13;
  1403. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  1404. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  1405. const size_t i_dst = i_src0;
  1406. const src0_t * src0_row = src0 + i_src0;
  1407. const src1_t * src1_row = src1 + i_src1;
  1408. dst_t * dst_row = dst + i_dst;
  1409. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  1410. const int i10 = i0 % ne10;
  1411. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  1412. }
  1413. }
  1414. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  1415. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  1416. int ne0, int ne1, int ne2, int ne3,
  1417. int ne10, int ne11, int ne12, int ne13,
  1418. /*int s0, */ int s1, int s2, int s3,
  1419. /*int s10,*/ int s11, int s12, int s13) {
  1420. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1421. const int i3 = i/(ne2*ne1*ne0);
  1422. const int i2 = (i/(ne1*ne0)) % ne2;
  1423. const int i1 = (i/ne0) % ne1;
  1424. const int i0 = i % ne0;
  1425. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  1426. return;
  1427. }
  1428. const int i11 = i1 % ne11;
  1429. const int i12 = i2 % ne12;
  1430. const int i13 = i3 % ne13;
  1431. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  1432. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  1433. const size_t i_dst = i_src0;
  1434. const src0_t * src0_row = src0 + i_src0;
  1435. const src1_t * src1_row = src1 + i_src1;
  1436. dst_t * dst_row = dst + i_dst;
  1437. const int i10 = i0 % ne10;
  1438. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  1439. }
  1440. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  1441. const int ne10, const int ne11, const int ne12,
  1442. const int nb1, const int nb2, int offset) {
  1443. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  1444. if (i >= ne) {
  1445. return;
  1446. }
  1447. int src1_idx = i - offset;
  1448. int oz = src1_idx / nb2;
  1449. int oy = (src1_idx - (oz * nb2)) / nb1;
  1450. int ox = src1_idx % nb1;
  1451. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  1452. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  1453. } else {
  1454. dst[i] = x[i];
  1455. }
  1456. }
  1457. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  1458. const float GELU_COEF_A = 0.044715f;
  1459. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  1460. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1461. if (i >= k) {
  1462. return;
  1463. }
  1464. float xi = x[i];
  1465. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  1466. }
  1467. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  1468. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1469. if (i >= k) {
  1470. return;
  1471. }
  1472. dst[i] = x[i] / (1.0f + expf(-x[i]));
  1473. }
  1474. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  1475. const float GELU_QUICK_COEF = -1.702f;
  1476. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1477. if (i >= k) {
  1478. return;
  1479. }
  1480. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  1481. }
  1482. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  1483. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1484. if (i >= k) {
  1485. return;
  1486. }
  1487. dst[i] = tanhf(x[i]);
  1488. }
  1489. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  1490. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1491. if (i >= k) {
  1492. return;
  1493. }
  1494. dst[i] = fmaxf(x[i], 0);
  1495. }
  1496. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  1497. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1498. if (i >= k) {
  1499. return;
  1500. }
  1501. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  1502. }
  1503. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  1504. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1505. if (i >= k) {
  1506. return;
  1507. }
  1508. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  1509. }
  1510. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  1511. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1512. if (i >= k) {
  1513. return;
  1514. }
  1515. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  1516. }
  1517. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  1518. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1519. if (i >= k) {
  1520. return;
  1521. }
  1522. dst[i] = x[i] * x[i];
  1523. }
  1524. template <int block_size>
  1525. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  1526. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1527. const int tid = threadIdx.x;
  1528. float2 mean_var = make_float2(0.f, 0.f);
  1529. for (int col = tid; col < ncols; col += block_size) {
  1530. const float xi = x[row*ncols + col];
  1531. mean_var.x += xi;
  1532. mean_var.y += xi * xi;
  1533. }
  1534. // sum up partial sums
  1535. mean_var = warp_reduce_sum(mean_var);
  1536. if (block_size > WARP_SIZE) {
  1537. __shared__ float2 s_sum[32];
  1538. int warp_id = threadIdx.x / WARP_SIZE;
  1539. int lane_id = threadIdx.x % WARP_SIZE;
  1540. if (lane_id == 0) {
  1541. s_sum[warp_id] = mean_var;
  1542. }
  1543. __syncthreads();
  1544. mean_var = s_sum[lane_id];
  1545. mean_var = warp_reduce_sum(mean_var);
  1546. }
  1547. const float mean = mean_var.x / ncols;
  1548. const float var = mean_var.y / ncols - mean * mean;
  1549. const float inv_std = rsqrtf(var + eps);
  1550. for (int col = tid; col < ncols; col += block_size) {
  1551. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  1552. }
  1553. }
  1554. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  1555. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  1556. if (nidx >= ne0) {
  1557. return;
  1558. }
  1559. // operation
  1560. int offset_dst =
  1561. nidx +
  1562. blockIdx.y * ne0 +
  1563. blockIdx.z * ne0 * gridDim.y;
  1564. if (blockIdx.z < ne02) { // src0
  1565. int offset_src =
  1566. nidx +
  1567. blockIdx.y * ne0 +
  1568. blockIdx.z * ne0 * gridDim.y;
  1569. dst[offset_dst] = x[offset_src];
  1570. } else {
  1571. int offset_src =
  1572. nidx +
  1573. blockIdx.y * ne0 +
  1574. (blockIdx.z - ne02) * ne0 * gridDim.y;
  1575. dst[offset_dst] = y[offset_src];
  1576. }
  1577. }
  1578. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int ne00xne01, const int scale_factor) {
  1579. // blockIdx.z: idx of ne02*ne03
  1580. // blockIdx.y: idx of ne01*scale_factor, aka ne1
  1581. // blockIDx.x: idx of ne00*scale_factor / BLOCK_SIZE
  1582. // ne00xne01: ne00 * ne01
  1583. int ne0 = ne00 * scale_factor;
  1584. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  1585. if (nidx >= ne0) {
  1586. return;
  1587. }
  1588. // operation
  1589. int i00 = nidx / scale_factor;
  1590. int i01 = blockIdx.y / scale_factor;
  1591. int offset_src =
  1592. i00 +
  1593. i01 * ne00 +
  1594. blockIdx.z * ne00xne01;
  1595. int offset_dst =
  1596. nidx +
  1597. blockIdx.y * ne0 +
  1598. blockIdx.z * ne0 * gridDim.y;
  1599. dst[offset_dst] = x[offset_src];
  1600. }
  1601. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02, const int ne03) {
  1602. // blockIdx.z: idx of ne2*ne3, aka ne02*ne03
  1603. // blockIdx.y: idx of ne1
  1604. // blockIDx.x: idx of ne0 / BLOCK_SIZE
  1605. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  1606. if (nidx >= ne0) {
  1607. return;
  1608. }
  1609. // operation
  1610. int offset_dst =
  1611. nidx +
  1612. blockIdx.y * ne0 +
  1613. blockIdx.z * ne0 * gridDim.y;
  1614. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02*ne03) {
  1615. int offset_src =
  1616. nidx +
  1617. blockIdx.y * ne00 +
  1618. blockIdx.z * ne00 * ne01;
  1619. dst[offset_dst] = x[offset_src];
  1620. } else {
  1621. dst[offset_dst] = 0.0f;
  1622. }
  1623. }
  1624. static __global__ void arange_f32(float * dst, const int ne0, const float start, const float step) {
  1625. // blockIDx.x: idx of ne0 / BLOCK_SIZE
  1626. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  1627. if (nidx >= ne0) {
  1628. return;
  1629. }
  1630. dst[nidx] = start + step * nidx;
  1631. }
  1632. static __global__ void timestep_embedding_f32(const float * timesteps, float * dst, const int nb1, const int dim, const int max_period) {
  1633. // blockIDx.y: idx of timesteps->ne[0]
  1634. // blockIDx.x: idx of ((dim + 1) / 2) / BLOCK_SIZE
  1635. int i = blockIdx.y;
  1636. int j = threadIdx.x + blockIdx.x * blockDim.x;
  1637. float * embed_data = (float *)((char *)dst + i*nb1);
  1638. if (dim % 2 != 0 && j == ((dim + 1) / 2)) {
  1639. embed_data[dim] = 0.f;
  1640. }
  1641. int half = dim / 2;
  1642. if (j >= half) {
  1643. return;
  1644. }
  1645. float timestep = timesteps[i];
  1646. float freq = (float)expf(-logf(max_period) * j / half);
  1647. float arg = timestep * freq;
  1648. embed_data[j] = cosf(arg);
  1649. embed_data[j + half] = sinf(arg);
  1650. }
  1651. template <int block_size>
  1652. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  1653. // blockIdx.x: num_groups idx
  1654. // threadIdx.x: block_size idx
  1655. int start = blockIdx.x * group_size;
  1656. int end = start + group_size;
  1657. start += threadIdx.x;
  1658. if (end >= ne_elements) {
  1659. end = ne_elements;
  1660. }
  1661. float tmp = 0.0f; // partial sum for thread in warp
  1662. for (int j = start; j < end; j += block_size) {
  1663. tmp += x[j];
  1664. }
  1665. tmp = warp_reduce_sum(tmp);
  1666. if (block_size > WARP_SIZE) {
  1667. __shared__ float s_sum[32];
  1668. int warp_id = threadIdx.x / WARP_SIZE;
  1669. int lane_id = threadIdx.x % WARP_SIZE;
  1670. if (lane_id == 0) {
  1671. s_sum[warp_id] = tmp;
  1672. }
  1673. __syncthreads();
  1674. tmp = s_sum[lane_id];
  1675. tmp = warp_reduce_sum(tmp);
  1676. }
  1677. float mean = tmp / group_size;
  1678. tmp = 0.0f;
  1679. for (int j = start; j < end; j += block_size) {
  1680. float xi = x[j] - mean;
  1681. dst[j] = xi;
  1682. tmp += xi * xi;
  1683. }
  1684. tmp = warp_reduce_sum(tmp);
  1685. if (block_size > WARP_SIZE) {
  1686. __shared__ float s_sum[32];
  1687. int warp_id = threadIdx.x / WARP_SIZE;
  1688. int lane_id = threadIdx.x % WARP_SIZE;
  1689. if (lane_id == 0) {
  1690. s_sum[warp_id] = tmp;
  1691. }
  1692. __syncthreads();
  1693. tmp = s_sum[lane_id];
  1694. tmp = warp_reduce_sum(tmp);
  1695. }
  1696. float variance = tmp / group_size;
  1697. float scale = rsqrtf(variance + eps);
  1698. for (int j = start; j < end; j += block_size) {
  1699. dst[j] *= scale;
  1700. }
  1701. }
  1702. template <int block_size>
  1703. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  1704. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1705. const int tid = threadIdx.x;
  1706. float tmp = 0.0f; // partial sum for thread in warp
  1707. for (int col = tid; col < ncols; col += block_size) {
  1708. const float xi = x[row*ncols + col];
  1709. tmp += xi * xi;
  1710. }
  1711. // sum up partial sums
  1712. tmp = warp_reduce_sum(tmp);
  1713. if (block_size > WARP_SIZE) {
  1714. __shared__ float s_sum[32];
  1715. int warp_id = threadIdx.x / WARP_SIZE;
  1716. int lane_id = threadIdx.x % WARP_SIZE;
  1717. if (lane_id == 0) {
  1718. s_sum[warp_id] = tmp;
  1719. }
  1720. __syncthreads();
  1721. tmp = s_sum[lane_id];
  1722. tmp = warp_reduce_sum(tmp);
  1723. }
  1724. const float mean = tmp / ncols;
  1725. const float scale = rsqrtf(mean + eps);
  1726. for (int col = tid; col < ncols; col += block_size) {
  1727. dst[row*ncols + col] = scale * x[row*ncols + col];
  1728. }
  1729. }
  1730. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1731. const block_q4_0 * x = (const block_q4_0 *) vx;
  1732. const dfloat d = x[ib].d;
  1733. const int vui = x[ib].qs[iqs];
  1734. v.x = vui & 0xF;
  1735. v.y = vui >> 4;
  1736. #ifdef GGML_CUDA_F16
  1737. v = __hsub2(v, {8.0f, 8.0f});
  1738. v = __hmul2(v, {d, d});
  1739. #else
  1740. v.x = (v.x - 8.0f) * d;
  1741. v.y = (v.y - 8.0f) * d;
  1742. #endif // GGML_CUDA_F16
  1743. }
  1744. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1745. const block_q4_1 * x = (const block_q4_1 *) vx;
  1746. const dfloat d = __low2half(x[ib].dm);
  1747. const dfloat m = __high2half(x[ib].dm);
  1748. const int vui = x[ib].qs[iqs];
  1749. v.x = vui & 0xF;
  1750. v.y = vui >> 4;
  1751. #ifdef GGML_CUDA_F16
  1752. v = __hmul2(v, {d, d});
  1753. v = __hadd2(v, {m, m});
  1754. #else
  1755. v.x = (v.x * d) + m;
  1756. v.y = (v.y * d) + m;
  1757. #endif // GGML_CUDA_F16
  1758. }
  1759. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1760. const block_q5_0 * x = (const block_q5_0 *) vx;
  1761. const dfloat d = x[ib].d;
  1762. uint32_t qh;
  1763. memcpy(&qh, x[ib].qh, sizeof(qh));
  1764. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1765. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1766. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1767. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1768. #ifdef GGML_CUDA_F16
  1769. v = __hsub2(v, {16.0f, 16.0f});
  1770. v = __hmul2(v, {d, d});
  1771. #else
  1772. v.x = (v.x - 16.0f) * d;
  1773. v.y = (v.y - 16.0f) * d;
  1774. #endif // GGML_CUDA_F16
  1775. }
  1776. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1777. const block_q5_1 * x = (const block_q5_1 *) vx;
  1778. const dfloat d = __low2half(x[ib].dm);
  1779. const dfloat m = __high2half(x[ib].dm);
  1780. uint32_t qh;
  1781. memcpy(&qh, x[ib].qh, sizeof(qh));
  1782. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1783. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1784. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1785. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1786. #ifdef GGML_CUDA_F16
  1787. v = __hmul2(v, {d, d});
  1788. v = __hadd2(v, {m, m});
  1789. #else
  1790. v.x = (v.x * d) + m;
  1791. v.y = (v.y * d) + m;
  1792. #endif // GGML_CUDA_F16
  1793. }
  1794. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1795. const block_q8_0 * x = (const block_q8_0 *) vx;
  1796. const dfloat d = x[ib].d;
  1797. v.x = x[ib].qs[iqs + 0];
  1798. v.y = x[ib].qs[iqs + 1];
  1799. #ifdef GGML_CUDA_F16
  1800. v = __hmul2(v, {d, d});
  1801. #else
  1802. v.x *= d;
  1803. v.y *= d;
  1804. #endif // GGML_CUDA_F16
  1805. }
  1806. template<typename dst_t>
  1807. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1808. const int i = blockIdx.x;
  1809. // assume 32 threads
  1810. const int tid = threadIdx.x;
  1811. const int il = tid/8;
  1812. const int ir = tid%8;
  1813. const int ib = 8*i + ir;
  1814. if (ib >= nb32) {
  1815. return;
  1816. }
  1817. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1818. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1819. const float d = __half2float(x->d);
  1820. const float dm = -8*d;
  1821. const uint8_t * q = x->qs + 4*il;
  1822. for (int l = 0; l < 4; ++l) {
  1823. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1824. y[l+16] = d * (q[l] >> 4) + dm;
  1825. }
  1826. }
  1827. template<typename dst_t>
  1828. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1829. const int i = blockIdx.x;
  1830. // assume 32 threads
  1831. const int tid = threadIdx.x;
  1832. const int il = tid/8;
  1833. const int ir = tid%8;
  1834. const int ib = 8*i + ir;
  1835. if (ib >= nb32) {
  1836. return;
  1837. }
  1838. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1839. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1840. const float2 d = __half22float2(x->dm);
  1841. const uint8_t * q = x->qs + 4*il;
  1842. for (int l = 0; l < 4; ++l) {
  1843. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1844. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1845. }
  1846. }
  1847. //================================== k-quants
  1848. template<typename dst_t>
  1849. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1850. const int i = blockIdx.x;
  1851. const block_q2_K * x = (const block_q2_K *) vx;
  1852. const int tid = threadIdx.x;
  1853. #if QK_K == 256
  1854. const int n = tid/32;
  1855. const int l = tid - 32*n;
  1856. const int is = 8*n + l/16;
  1857. const uint8_t q = x[i].qs[32*n + l];
  1858. dst_t * y = yy + i*QK_K + 128*n;
  1859. float dall = __low2half(x[i].dm);
  1860. float dmin = __high2half(x[i].dm);
  1861. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1862. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1863. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1864. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1865. #else
  1866. const int is = tid/16; // 0 or 1
  1867. const int il = tid%16; // 0...15
  1868. const uint8_t q = x[i].qs[il] >> (2*is);
  1869. dst_t * y = yy + i*QK_K + 16*is + il;
  1870. float dall = __low2half(x[i].dm);
  1871. float dmin = __high2half(x[i].dm);
  1872. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1873. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1874. #endif
  1875. }
  1876. template<typename dst_t>
  1877. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1878. const int i = blockIdx.x;
  1879. const block_q3_K * x = (const block_q3_K *) vx;
  1880. #if QK_K == 256
  1881. const int r = threadIdx.x/4;
  1882. const int tid = r/2;
  1883. const int is0 = r%2;
  1884. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1885. const int n = tid / 4;
  1886. const int j = tid - 4*n;
  1887. uint8_t m = 1 << (4*n + j);
  1888. int is = 8*n + 2*j + is0;
  1889. int shift = 2*j;
  1890. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1891. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1892. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1893. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1894. float d_all = x[i].d;
  1895. float dl = d_all * (us - 32);
  1896. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1897. const uint8_t * q = x[i].qs + 32*n;
  1898. const uint8_t * hm = x[i].hmask;
  1899. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1900. #else
  1901. const int tid = threadIdx.x;
  1902. const int is = tid/16; // 0 or 1
  1903. const int il = tid%16; // 0...15
  1904. const int im = il/8; // 0...1
  1905. const int in = il%8; // 0...7
  1906. dst_t * y = yy + i*QK_K + 16*is + il;
  1907. const uint8_t q = x[i].qs[il] >> (2*is);
  1908. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1909. const float d = (float)x[i].d;
  1910. if (is == 0) {
  1911. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1912. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1913. } else {
  1914. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1915. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1916. }
  1917. #endif
  1918. }
  1919. #if QK_K == 256
  1920. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1921. if (j < 4) {
  1922. d = q[j] & 63; m = q[j + 4] & 63;
  1923. } else {
  1924. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1925. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1926. }
  1927. }
  1928. #endif
  1929. template<typename dst_t>
  1930. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1931. const block_q4_K * x = (const block_q4_K *) vx;
  1932. const int i = blockIdx.x;
  1933. #if QK_K == 256
  1934. // assume 32 threads
  1935. const int tid = threadIdx.x;
  1936. const int il = tid/8;
  1937. const int ir = tid%8;
  1938. const int is = 2*il;
  1939. const int n = 4;
  1940. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1941. const float dall = __low2half(x[i].dm);
  1942. const float dmin = __high2half(x[i].dm);
  1943. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1944. uint8_t sc, m;
  1945. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1946. const float d1 = dall * sc; const float m1 = dmin * m;
  1947. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1948. const float d2 = dall * sc; const float m2 = dmin * m;
  1949. for (int l = 0; l < n; ++l) {
  1950. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1951. y[l +32] = d2 * (q[l] >> 4) - m2;
  1952. }
  1953. #else
  1954. const int tid = threadIdx.x;
  1955. const uint8_t * q = x[i].qs;
  1956. dst_t * y = yy + i*QK_K;
  1957. const float d = (float)x[i].dm[0];
  1958. const float m = (float)x[i].dm[1];
  1959. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1960. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1961. #endif
  1962. }
  1963. template<typename dst_t>
  1964. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1965. const block_q5_K * x = (const block_q5_K *) vx;
  1966. const int i = blockIdx.x;
  1967. #if QK_K == 256
  1968. // assume 64 threads - this is very slightly better than the one below
  1969. const int tid = threadIdx.x;
  1970. const int il = tid/16; // il is in 0...3
  1971. const int ir = tid%16; // ir is in 0...15
  1972. const int is = 2*il; // is is in 0...6
  1973. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1974. const float dall = __low2half(x[i].dm);
  1975. const float dmin = __high2half(x[i].dm);
  1976. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1977. const uint8_t * qh = x[i].qh + 2*ir;
  1978. uint8_t sc, m;
  1979. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1980. const float d1 = dall * sc; const float m1 = dmin * m;
  1981. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1982. const float d2 = dall * sc; const float m2 = dmin * m;
  1983. uint8_t hm = 1 << (2*il);
  1984. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1985. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1986. hm <<= 1;
  1987. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1988. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1989. #else
  1990. const int tid = threadIdx.x;
  1991. const uint8_t q = x[i].qs[tid];
  1992. const int im = tid/8; // 0...3
  1993. const int in = tid%8; // 0...7
  1994. const int is = tid/16; // 0 or 1
  1995. const uint8_t h = x[i].qh[in] >> im;
  1996. const float d = x[i].d;
  1997. dst_t * y = yy + i*QK_K + tid;
  1998. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1999. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  2000. #endif
  2001. }
  2002. template<typename dst_t>
  2003. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2004. const block_q6_K * x = (const block_q6_K *) vx;
  2005. const int i = blockIdx.x;
  2006. #if QK_K == 256
  2007. // assume 64 threads - this is very slightly better than the one below
  2008. const int tid = threadIdx.x;
  2009. const int ip = tid/32; // ip is 0 or 1
  2010. const int il = tid - 32*ip; // 0...32
  2011. const int is = 8*ip + il/16;
  2012. dst_t * y = yy + i*QK_K + 128*ip + il;
  2013. const float d = x[i].d;
  2014. const uint8_t * ql = x[i].ql + 64*ip + il;
  2015. const uint8_t qh = x[i].qh[32*ip + il];
  2016. const int8_t * sc = x[i].scales + is;
  2017. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  2018. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  2019. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  2020. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  2021. #else
  2022. // assume 32 threads
  2023. const int tid = threadIdx.x;
  2024. const int ip = tid/16; // 0 or 1
  2025. const int il = tid - 16*ip; // 0...15
  2026. dst_t * y = yy + i*QK_K + 16*ip + il;
  2027. const float d = x[i].d;
  2028. const uint8_t ql = x[i].ql[16*ip + il];
  2029. const uint8_t qh = x[i].qh[il] >> (2*ip);
  2030. const int8_t * sc = x[i].scales;
  2031. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  2032. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  2033. #endif
  2034. }
  2035. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  2036. switch (type) {
  2037. case GGML_TYPE_Q4_0:
  2038. case GGML_TYPE_Q4_1:
  2039. case GGML_TYPE_Q5_0:
  2040. case GGML_TYPE_Q5_1:
  2041. case GGML_TYPE_Q8_0:
  2042. case GGML_TYPE_Q2_K:
  2043. case GGML_TYPE_Q3_K:
  2044. case GGML_TYPE_Q4_K:
  2045. case GGML_TYPE_Q5_K:
  2046. case GGML_TYPE_Q6_K:
  2047. return true;
  2048. default:
  2049. return false;
  2050. }
  2051. }
  2052. template<typename dst_t>
  2053. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2054. const int i = blockIdx.x;
  2055. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  2056. const int tid = threadIdx.x;
  2057. #if QK_K == 256
  2058. const int il = tid/8; // 0...3
  2059. const int ib = tid%8; // 0...7
  2060. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2061. const uint16_t * q2 = x[i].qs + 4*ib;
  2062. const uint8_t * aux8 = (const uint8_t *)q2;
  2063. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  2064. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  2065. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  2066. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  2067. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2068. #else
  2069. assert(false);
  2070. #endif
  2071. }
  2072. template<typename dst_t>
  2073. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2074. const int i = blockIdx.x;
  2075. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  2076. const int tid = threadIdx.x;
  2077. #if QK_K == 256
  2078. const int il = tid/8; // 0...3
  2079. const int ib = tid%8; // 0...7
  2080. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2081. const uint16_t * q2 = x[i].qs + 4*ib;
  2082. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  2083. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  2084. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  2085. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2086. #else
  2087. assert(false);
  2088. #endif
  2089. }
  2090. template<typename dst_t>
  2091. static __global__ void dequantize_block_iq2_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2092. const int i = blockIdx.x;
  2093. const block_iq2_s * x = (const block_iq2_s *) vx;
  2094. const int tid = threadIdx.x;
  2095. #if QK_K == 256
  2096. const int il = tid/8; // 0...3
  2097. const int ib = tid%8; // 0...7
  2098. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2099. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  2100. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  2101. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  2102. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2103. #else
  2104. assert(false);
  2105. #endif
  2106. }
  2107. template<typename dst_t>
  2108. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2109. const int i = blockIdx.x;
  2110. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  2111. const int tid = threadIdx.x;
  2112. #if QK_K == 256
  2113. const int il = tid/8; // 0...3
  2114. const int ib = tid%8; // 0...7
  2115. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2116. const uint8_t * q3 = x[i].qs + 8*ib;
  2117. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  2118. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  2119. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  2120. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  2121. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  2122. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  2123. for (int j = 0; j < 4; ++j) {
  2124. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2125. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2126. }
  2127. #else
  2128. assert(false);
  2129. #endif
  2130. }
  2131. template<typename dst_t>
  2132. static __global__ void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2133. const int i = blockIdx.x;
  2134. const block_iq3_s * x = (const block_iq3_s *) vx;
  2135. const int tid = threadIdx.x;
  2136. #if QK_K == 256
  2137. const int il = tid/8; // 0...3
  2138. const int ib = tid%8; // 0...7
  2139. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2140. const uint8_t * qs = x[i].qs + 8*ib;
  2141. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  2142. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  2143. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  2144. const uint8_t signs = x[i].signs[4*ib + il];
  2145. for (int j = 0; j < 4; ++j) {
  2146. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2147. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2148. }
  2149. #else
  2150. assert(false);
  2151. #endif
  2152. }
  2153. template<typename dst_t>
  2154. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2155. const int i = blockIdx.x;
  2156. const block_iq1_s * x = (const block_iq1_s *) vx;
  2157. const int tid = threadIdx.x;
  2158. #if QK_K == 256
  2159. const int il = tid/8; // 0...3
  2160. const int ib = tid%8; // 0...7
  2161. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  2162. const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
  2163. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
  2164. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  2165. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
  2166. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  2167. grid32[0] &= 0x0f0f0f0f;
  2168. for (int j = 0; j < 8; ++j) {
  2169. y[j] = d * (q[j] + delta);
  2170. }
  2171. #else
  2172. assert(false);
  2173. #endif
  2174. }
  2175. static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2176. template<typename dst_t>
  2177. static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2178. const int i = blockIdx.x;
  2179. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  2180. const int tid = threadIdx.x;
  2181. const int il = tid/8; // 0...3
  2182. const int ib = tid%8; // 0...7
  2183. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  2184. const uint8_t * q4 = x[ib].qs + 4*il;
  2185. const float d = (float)x[ib].d;
  2186. for (int j = 0; j < 4; ++j) {
  2187. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  2188. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  2189. }
  2190. }
  2191. #if QK_K != 64
  2192. template<typename dst_t>
  2193. static __global__ void dequantize_block_iq4_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  2194. const int i = blockIdx.x;
  2195. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  2196. const int tid = threadIdx.x;
  2197. const int il = tid/8; // 0...3
  2198. const int ib = tid%8; // 0...7
  2199. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  2200. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  2201. const float d = (float)x[i].d * ((((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4)) - 32);
  2202. for (int j = 0; j < 4; ++j) {
  2203. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  2204. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  2205. }
  2206. }
  2207. #endif
  2208. 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) {
  2209. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2210. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2211. if (row > nrows) return;
  2212. const int num_blocks_per_row = ncols / QK_K;
  2213. const int ib0 = row*num_blocks_per_row;
  2214. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  2215. float tmp = 0; // partial sum for thread in warp
  2216. #if QK_K == 256
  2217. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  2218. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2219. const int step = 16/K_QUANTS_PER_ITERATION;
  2220. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2221. const int in = tid - step*im; // 0...15 or 0...7
  2222. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  2223. const int q_offset = 32*im + l0;
  2224. const int s_offset = 8*im;
  2225. const int y_offset = 128*im + l0;
  2226. uint32_t aux[4];
  2227. const uint8_t * d = (const uint8_t *)aux;
  2228. const uint8_t * m = (const uint8_t *)(aux + 2);
  2229. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2230. const float * y = yy + i * QK_K + y_offset;
  2231. const uint8_t * q = x[i].qs + q_offset;
  2232. const float dall = __low2half(x[i].dm);
  2233. const float dmin = __high2half(x[i].dm);
  2234. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  2235. aux[0] = a[0] & 0x0f0f0f0f;
  2236. aux[1] = a[1] & 0x0f0f0f0f;
  2237. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  2238. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  2239. float sum1 = 0, sum2 = 0;
  2240. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2241. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  2242. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  2243. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  2244. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  2245. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  2246. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  2247. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  2248. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  2249. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  2250. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  2251. }
  2252. tmp += dall * sum1 - dmin * sum2;
  2253. }
  2254. #else
  2255. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  2256. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  2257. const int offset = tid * K_QUANTS_PER_ITERATION;
  2258. uint32_t uaux[2];
  2259. const uint8_t * d = (const uint8_t *)uaux;
  2260. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2261. const float * y = yy + i * QK_K + offset;
  2262. const uint8_t * q = x[i].qs + offset;
  2263. const uint32_t * s = (const uint32_t *)x[i].scales;
  2264. uaux[0] = s[0] & 0x0f0f0f0f;
  2265. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  2266. const float2 dall = __half22float2(x[i].dm);
  2267. float sum1 = 0, sum2 = 0;
  2268. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2269. const uint8_t ql = q[l];
  2270. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  2271. + y[l+16] * d[1] * ((ql >> 2) & 3)
  2272. + y[l+32] * d[2] * ((ql >> 4) & 3)
  2273. + y[l+48] * d[3] * ((ql >> 6) & 3);
  2274. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  2275. }
  2276. tmp += dall.x * sum1 - dall.y * sum2;
  2277. }
  2278. #endif
  2279. // sum up partial sums and write back result
  2280. tmp = warp_reduce_sum(tmp);
  2281. if (threadIdx.x == 0) {
  2282. dst[row] = tmp;
  2283. }
  2284. }
  2285. 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) {
  2286. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2287. if (row > nrows) return;
  2288. const int num_blocks_per_row = ncols / QK_K;
  2289. const int ib0 = row*num_blocks_per_row;
  2290. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  2291. float tmp = 0; // partial sum for thread in warp
  2292. #if QK_K == 256
  2293. const uint16_t kmask1 = 0x0303;
  2294. const uint16_t kmask2 = 0x0f0f;
  2295. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2296. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2297. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  2298. const int step = 16/K_QUANTS_PER_ITERATION;
  2299. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2300. const int in = tid - step*im; // 0....15 or 0...7
  2301. const uint8_t m = 1 << (4*im);
  2302. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  2303. const int q_offset = 32*im + l0;
  2304. const int y_offset = 128*im + l0;
  2305. uint16_t utmp[4];
  2306. const int8_t * s = (const int8_t *)utmp;
  2307. const uint16_t s_shift = 4*im;
  2308. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2309. const float * y = yy + i * QK_K + y_offset;
  2310. const uint8_t * q = x[i].qs + q_offset;
  2311. const uint8_t * h = x[i].hmask + l0;
  2312. const uint16_t * a = (const uint16_t *)x[i].scales;
  2313. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  2314. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  2315. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  2316. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  2317. const float d = x[i].d;
  2318. float sum = 0;
  2319. for (int l = 0; l < n; ++l) {
  2320. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  2321. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  2322. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  2323. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  2324. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  2325. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  2326. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  2327. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  2328. }
  2329. tmp += d * sum;
  2330. }
  2331. #else
  2332. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  2333. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  2334. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  2335. const int in = offset/8; // 0 or 1
  2336. const int im = offset%8; // 0...7
  2337. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2338. const float * y = yy + i * QK_K + offset;
  2339. const uint8_t * q = x[i].qs + offset;
  2340. const uint8_t * s = x[i].scales;
  2341. const float dall = (float)x[i].d;
  2342. float sum = 0;
  2343. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  2344. const uint8_t hl = x[i].hmask[im+l] >> in;
  2345. const uint8_t ql = q[l];
  2346. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  2347. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  2348. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  2349. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  2350. }
  2351. tmp += sum;
  2352. }
  2353. #endif
  2354. // sum up partial sums and write back result
  2355. tmp = warp_reduce_sum(tmp);
  2356. if (threadIdx.x == 0) {
  2357. dst[row] = tmp;
  2358. }
  2359. }
  2360. 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) {
  2361. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2362. if (row > nrows) return;
  2363. const int num_blocks_per_row = ncols / QK_K;
  2364. const int ib0 = row*num_blocks_per_row;
  2365. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  2366. #if QK_K == 256
  2367. const uint16_t kmask1 = 0x3f3f;
  2368. const uint16_t kmask2 = 0x0f0f;
  2369. const uint16_t kmask3 = 0xc0c0;
  2370. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2371. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  2372. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  2373. const int il = tid/step; // 0...3
  2374. const int ir = tid - step*il; // 0...7 or 0...3
  2375. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  2376. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2377. const int in = il%2;
  2378. const int l0 = n*(2*ir + in);
  2379. const int q_offset = 32*im + l0;
  2380. const int y_offset = 64*im + l0;
  2381. uint16_t aux[4];
  2382. const uint8_t * sc = (const uint8_t *)aux;
  2383. #if K_QUANTS_PER_ITERATION == 2
  2384. uint32_t q32[4];
  2385. const uint8_t * q4 = (const uint8_t *)q32;
  2386. #else
  2387. uint16_t q16[4];
  2388. const uint8_t * q4 = (const uint8_t *)q16;
  2389. #endif
  2390. float tmp = 0; // partial sum for thread in warp
  2391. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2392. const float * y1 = yy + i*QK_K + y_offset;
  2393. const float * y2 = y1 + 128;
  2394. const float dall = __low2half(x[i].dm);
  2395. const float dmin = __high2half(x[i].dm);
  2396. const uint16_t * a = (const uint16_t *)x[i].scales;
  2397. aux[0] = a[im+0] & kmask1;
  2398. aux[1] = a[im+2] & kmask1;
  2399. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2400. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2401. #if K_QUANTS_PER_ITERATION == 2
  2402. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  2403. const uint32_t * q2 = q1 + 16;
  2404. q32[0] = q1[0] & 0x0f0f0f0f;
  2405. q32[1] = q1[0] & 0xf0f0f0f0;
  2406. q32[2] = q2[0] & 0x0f0f0f0f;
  2407. q32[3] = q2[0] & 0xf0f0f0f0;
  2408. float4 s = {0.f, 0.f, 0.f, 0.f};
  2409. float smin = 0;
  2410. for (int l = 0; l < 4; ++l) {
  2411. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  2412. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  2413. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  2414. }
  2415. tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
  2416. #else
  2417. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  2418. const uint16_t * q2 = q1 + 32;
  2419. q16[0] = q1[0] & 0x0f0f;
  2420. q16[1] = q1[0] & 0xf0f0;
  2421. q16[2] = q2[0] & 0x0f0f;
  2422. q16[3] = q2[0] & 0xf0f0;
  2423. float4 s = {0.f, 0.f, 0.f, 0.f};
  2424. float smin = 0;
  2425. for (int l = 0; l < 2; ++l) {
  2426. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  2427. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  2428. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  2429. }
  2430. 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;
  2431. #endif
  2432. }
  2433. #else
  2434. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2435. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2436. const int step = tid * K_QUANTS_PER_ITERATION;
  2437. uint16_t aux16[2];
  2438. const uint8_t * s = (const uint8_t *)aux16;
  2439. float tmp = 0;
  2440. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2441. const uint8_t * q = x[i].qs + step;
  2442. const float * y = yy + i*QK_K + step;
  2443. const uint16_t * a = (const uint16_t *)x[i].scales;
  2444. aux16[0] = a[0] & 0x0f0f;
  2445. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2446. const float d = (float)x[i].dm[0];
  2447. const float m = (float)x[i].dm[1];
  2448. float sum = 0.f;
  2449. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2450. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  2451. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  2452. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  2453. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  2454. }
  2455. tmp += sum;
  2456. }
  2457. #endif
  2458. // sum up partial sums and write back result
  2459. tmp = warp_reduce_sum(tmp);
  2460. if (tid == 0) {
  2461. dst[row] = tmp;
  2462. }
  2463. }
  2464. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  2465. const int row = blockIdx.x;
  2466. const int num_blocks_per_row = ncols / QK_K;
  2467. const int ib0 = row*num_blocks_per_row;
  2468. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  2469. float tmp = 0; // partial sum for thread in warp
  2470. #if QK_K == 256
  2471. const uint16_t kmask1 = 0x3f3f;
  2472. const uint16_t kmask2 = 0x0f0f;
  2473. const uint16_t kmask3 = 0xc0c0;
  2474. const int tid = threadIdx.x/2; // 0...15
  2475. const int ix = threadIdx.x%2;
  2476. const int il = tid/4; // 0...3
  2477. const int ir = tid - 4*il;// 0...3
  2478. const int n = 2;
  2479. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2480. const int in = il%2;
  2481. const int l0 = n*(2*ir + in);
  2482. const int q_offset = 32*im + l0;
  2483. const int y_offset = 64*im + l0;
  2484. const uint8_t hm1 = 1 << (2*im);
  2485. const uint8_t hm2 = hm1 << 4;
  2486. uint16_t aux[4];
  2487. const uint8_t * sc = (const uint8_t *)aux;
  2488. uint16_t q16[8];
  2489. const uint8_t * q4 = (const uint8_t *)q16;
  2490. for (int i = ix; i < num_blocks_per_row; i += 2) {
  2491. const uint8_t * ql1 = x[i].qs + q_offset;
  2492. const uint8_t * qh = x[i].qh + l0;
  2493. const float * y1 = yy + i*QK_K + y_offset;
  2494. const float * y2 = y1 + 128;
  2495. const float dall = __low2half(x[i].dm);
  2496. const float dmin = __high2half(x[i].dm);
  2497. const uint16_t * a = (const uint16_t *)x[i].scales;
  2498. aux[0] = a[im+0] & kmask1;
  2499. aux[1] = a[im+2] & kmask1;
  2500. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2501. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2502. float4 sum = {0.f, 0.f, 0.f, 0.f};
  2503. float smin = 0;
  2504. const uint16_t * q1 = (const uint16_t *)ql1;
  2505. const uint16_t * q2 = q1 + 32;
  2506. q16[0] = q1[0] & 0x0f0f;
  2507. q16[1] = q1[8] & 0x0f0f;
  2508. q16[2] = (q1[0] >> 4) & 0x0f0f;
  2509. q16[3] = (q1[8] >> 4) & 0x0f0f;
  2510. q16[4] = q2[0] & 0x0f0f;
  2511. q16[5] = q2[8] & 0x0f0f;
  2512. q16[6] = (q2[0] >> 4) & 0x0f0f;
  2513. q16[7] = (q2[8] >> 4) & 0x0f0f;
  2514. for (int l = 0; l < n; ++l) {
  2515. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  2516. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  2517. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  2518. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  2519. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  2520. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  2521. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  2522. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  2523. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  2524. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  2525. }
  2526. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  2527. }
  2528. #else
  2529. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2530. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2531. const int step = tid * K_QUANTS_PER_ITERATION;
  2532. const int im = step/8;
  2533. const int in = step%8;
  2534. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2535. const uint8_t * q = x[i].qs + step;
  2536. const int8_t * s = x[i].scales;
  2537. const float * y = yy + i*QK_K + step;
  2538. const float d = x[i].d;
  2539. float sum = 0.f;
  2540. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2541. const uint8_t h = x[i].qh[in+j] >> im;
  2542. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  2543. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  2544. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  2545. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  2546. }
  2547. tmp += sum;
  2548. }
  2549. #endif
  2550. // sum up partial sums and write back result
  2551. tmp = warp_reduce_sum(tmp);
  2552. if (threadIdx.x == 0) {
  2553. dst[row] = tmp;
  2554. }
  2555. }
  2556. 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) {
  2557. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2558. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2559. if (row > nrows) return;
  2560. const int num_blocks_per_row = ncols / QK_K;
  2561. const int ib0 = row*num_blocks_per_row;
  2562. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  2563. #if QK_K == 256
  2564. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2565. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  2566. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  2567. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2568. const int in = tid - step*im; // 0...15 or 0...7
  2569. #if K_QUANTS_PER_ITERATION == 1
  2570. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  2571. const int is = 0;
  2572. #else
  2573. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  2574. const int is = in / 4;
  2575. #endif
  2576. const int ql_offset = 64*im + l0;
  2577. const int qh_offset = 32*im + l0;
  2578. const int s_offset = 8*im + is;
  2579. const int y_offset = 128*im + l0;
  2580. float tmp = 0; // partial sum for thread in warp
  2581. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2582. const float * y = yy + i * QK_K + y_offset;
  2583. const uint8_t * ql = x[i].ql + ql_offset;
  2584. const uint8_t * qh = x[i].qh + qh_offset;
  2585. const int8_t * s = x[i].scales + s_offset;
  2586. const float d = x[i].d;
  2587. #if K_QUANTS_PER_ITERATION == 1
  2588. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  2589. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  2590. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  2591. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  2592. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  2593. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  2594. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  2595. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  2596. tmp += sum;
  2597. #else
  2598. float sum = 0;
  2599. for (int l = 0; l < 4; ++l) {
  2600. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  2601. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  2602. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  2603. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  2604. }
  2605. tmp += sum;
  2606. #endif
  2607. }
  2608. #else
  2609. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  2610. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  2611. const int step = tid * K_QUANTS_PER_ITERATION;
  2612. float tmp = 0; // partial sum for thread in warp
  2613. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2614. const float * y = yy + i * QK_K + step;
  2615. const uint8_t * ql = x[i].ql + step;
  2616. const uint8_t * qh = x[i].qh + step;
  2617. const int8_t * s = x[i].scales;
  2618. const float d = x[i+0].d;
  2619. float sum = 0;
  2620. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2621. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  2622. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  2623. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  2624. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  2625. }
  2626. tmp += sum;
  2627. }
  2628. #endif
  2629. // sum up partial sums and write back result
  2630. tmp = warp_reduce_sum(tmp);
  2631. if (tid == 0) {
  2632. dst[row] = tmp;
  2633. }
  2634. }
  2635. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  2636. const half * x = (const half *) vx;
  2637. // automatic half -> float type cast if dfloat == float
  2638. v.x = x[ib + iqs + 0];
  2639. v.y = x[ib + iqs + 1];
  2640. }
  2641. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  2642. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  2643. if (ix >= kx_padded) {
  2644. return;
  2645. }
  2646. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  2647. const int i_padded = iy*kx_padded + ix;
  2648. block_q8_1 * y = (block_q8_1 *) vy;
  2649. const int ib = i_padded / QK8_1; // block index
  2650. const int iqs = i_padded % QK8_1; // quant index
  2651. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  2652. float amax = fabsf(xi);
  2653. float sum = xi;
  2654. amax = warp_reduce_max(amax);
  2655. sum = warp_reduce_sum(sum);
  2656. const float d = amax / 127;
  2657. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  2658. y[ib].qs[iqs] = q;
  2659. if (iqs > 0) {
  2660. return;
  2661. }
  2662. reinterpret_cast<half&>(y[ib].ds.x) = d;
  2663. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  2664. }
  2665. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2666. static __global__ void k_get_rows(
  2667. const void * src0, const int32_t * src1, dst_t * dst,
  2668. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2669. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2670. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2671. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2672. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2673. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  2674. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2675. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2676. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2677. if (i00 >= ne00) {
  2678. return;
  2679. }
  2680. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2681. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2682. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  2683. const int ib = i00/qk; // block index
  2684. const int iqs = (i00%qk)/qr; // quant index
  2685. const int iybs = i00 - i00%qk; // dst block start index
  2686. const int y_offset = qr == 1 ? 1 : qk/2;
  2687. // dequantize
  2688. dfloat2 v;
  2689. dequantize_kernel(src0_row, ib, iqs, v);
  2690. dst_row[iybs + iqs + 0] = v.x;
  2691. dst_row[iybs + iqs + y_offset] = v.y;
  2692. }
  2693. template<typename src0_t, typename dst_t>
  2694. static __global__ void k_get_rows_float(
  2695. const src0_t * src0, const int32_t * src1, dst_t * dst,
  2696. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2697. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2698. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2699. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2700. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2701. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2702. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2703. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2704. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2705. if (i00 >= ne00) {
  2706. return;
  2707. }
  2708. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2709. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2710. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2711. dst_row[i00] = src0_row[i00];
  2712. }
  2713. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2714. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2715. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2716. if (i >= k) {
  2717. return;
  2718. }
  2719. const int ib = i/qk; // block index
  2720. const int iqs = (i%qk)/qr; // quant index
  2721. const int iybs = i - i%qk; // y block start index
  2722. const int y_offset = qr == 1 ? 1 : qk/2;
  2723. // dequantize
  2724. dfloat2 v;
  2725. dequantize_kernel(vx, ib, iqs, v);
  2726. y[iybs + iqs + 0] = v.x;
  2727. y[iybs + iqs + y_offset] = v.y;
  2728. }
  2729. template <typename src_t, typename dst_t>
  2730. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2731. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2732. if (i >= k) {
  2733. return;
  2734. }
  2735. const src_t * x = (src_t *) vx;
  2736. y[i] = x[i];
  2737. }
  2738. template <bool need_check>
  2739. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2740. #if __CUDA_ARCH__ >= CC_PASCAL
  2741. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2742. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2743. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2744. half2 * y2 = (half2 *) (y + i0);
  2745. __shared__ int vals[nint];
  2746. #pragma unroll
  2747. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2748. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2749. break;
  2750. }
  2751. const int ix = ix0 + threadIdx.x;
  2752. vals[ix] = x0[ix];
  2753. }
  2754. #pragma unroll
  2755. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2756. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2757. return;
  2758. }
  2759. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2760. const half d = *b0;
  2761. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2762. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2763. }
  2764. #else
  2765. GGML_UNUSED(vx);
  2766. GGML_UNUSED(y);
  2767. GGML_UNUSED(k);
  2768. NO_DEVICE_CODE;
  2769. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2770. }
  2771. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2772. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2773. #define VDR_Q4_0_Q8_1_MMVQ 2
  2774. #define VDR_Q4_0_Q8_1_MMQ 4
  2775. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2776. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2777. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2778. int sumi = 0;
  2779. #pragma unroll
  2780. for (int i = 0; i < vdr; ++i) {
  2781. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2782. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2783. // SIMD dot product of quantized values
  2784. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2785. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2786. }
  2787. const float2 ds8f = __half22float2(ds8);
  2788. // second part effectively subtracts 8 from each quant value
  2789. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2790. #else
  2791. NO_DEVICE_CODE;
  2792. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2793. }
  2794. #define VDR_Q4_1_Q8_1_MMVQ 2
  2795. #define VDR_Q4_1_Q8_1_MMQ 4
  2796. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2797. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2798. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2799. int sumi = 0;
  2800. #pragma unroll
  2801. for (int i = 0; i < vdr; ++i) {
  2802. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2803. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2804. // SIMD dot product of quantized values
  2805. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2806. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2807. }
  2808. #ifdef GGML_CUDA_F16
  2809. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2810. const float d4d8 = tmp.x;
  2811. const float m4s8 = tmp.y;
  2812. #else
  2813. const float2 dm4f = __half22float2(dm4);
  2814. const float2 ds8f = __half22float2(ds8);
  2815. const float d4d8 = dm4f.x * ds8f.x;
  2816. const float m4s8 = dm4f.y * ds8f.y;
  2817. #endif // GGML_CUDA_F16
  2818. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2819. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2820. #else
  2821. NO_DEVICE_CODE;
  2822. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2823. }
  2824. #define VDR_Q5_0_Q8_1_MMVQ 2
  2825. #define VDR_Q5_0_Q8_1_MMQ 4
  2826. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2827. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2828. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2829. int sumi = 0;
  2830. #pragma unroll
  2831. for (int i = 0; i < vdr; ++i) {
  2832. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2833. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2834. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2835. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2836. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2837. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2838. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2839. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2840. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2841. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2842. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2843. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2844. }
  2845. const float2 ds8f = __half22float2(ds8);
  2846. // second part effectively subtracts 16 from each quant value
  2847. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2848. #else
  2849. NO_DEVICE_CODE;
  2850. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2851. }
  2852. #define VDR_Q5_1_Q8_1_MMVQ 2
  2853. #define VDR_Q5_1_Q8_1_MMQ 4
  2854. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2855. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2856. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2857. int sumi = 0;
  2858. #pragma unroll
  2859. for (int i = 0; i < vdr; ++i) {
  2860. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2861. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2862. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2863. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2864. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2865. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2866. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2867. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2868. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2869. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2870. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2871. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2872. }
  2873. #ifdef GGML_CUDA_F16
  2874. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2875. const float d5d8 = tmp.x;
  2876. const float m5s8 = tmp.y;
  2877. #else
  2878. const float2 dm5f = __half22float2(dm5);
  2879. const float2 ds8f = __half22float2(ds8);
  2880. const float d5d8 = dm5f.x * ds8f.x;
  2881. const float m5s8 = dm5f.y * ds8f.y;
  2882. #endif // GGML_CUDA_F16
  2883. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2884. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2885. #else
  2886. NO_DEVICE_CODE;
  2887. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2888. }
  2889. #define VDR_Q8_0_Q8_1_MMVQ 2
  2890. #define VDR_Q8_0_Q8_1_MMQ 8
  2891. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2892. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2893. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2894. int sumi = 0;
  2895. #pragma unroll
  2896. for (int i = 0; i < vdr; ++i) {
  2897. // SIMD dot product of quantized values
  2898. sumi = __dp4a(v[i], u[i], sumi);
  2899. }
  2900. return d8_0*d8_1 * sumi;
  2901. #else
  2902. NO_DEVICE_CODE;
  2903. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2904. }
  2905. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2906. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2907. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2908. int sumi = 0;
  2909. #pragma unroll
  2910. for (int i = 0; i < vdr; ++i) {
  2911. // SIMD dot product of quantized values
  2912. sumi = __dp4a(v[i], u[i], sumi);
  2913. }
  2914. #ifdef GGML_CUDA_F16
  2915. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2916. const float d8d8 = tmp.x;
  2917. const float m8s8 = tmp.y;
  2918. #else
  2919. const float2 dm8f = __half22float2(dm8);
  2920. const float2 ds8f = __half22float2(ds8);
  2921. const float d8d8 = dm8f.x * ds8f.x;
  2922. const float m8s8 = dm8f.y * ds8f.y;
  2923. #endif // GGML_CUDA_F16
  2924. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2925. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2926. #else
  2927. NO_DEVICE_CODE;
  2928. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2929. }
  2930. #define VDR_Q2_K_Q8_1_MMVQ 1
  2931. #define VDR_Q2_K_Q8_1_MMQ 2
  2932. // contiguous v/x values
  2933. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2934. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2935. const half2 & dm2, const float * __restrict__ d8) {
  2936. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2937. float sumf_d = 0.0f;
  2938. float sumf_m = 0.0f;
  2939. #pragma unroll
  2940. for (int i = 0; i < QR2_K; ++i) {
  2941. const int sc = scales[2*i];
  2942. const int vi = (v >> (2*i)) & 0x03030303;
  2943. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2944. // fill int with 4x m
  2945. int m = sc >> 4;
  2946. m |= m << 8;
  2947. m |= m << 16;
  2948. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2949. }
  2950. const float2 dm2f = __half22float2(dm2);
  2951. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2952. #else
  2953. NO_DEVICE_CODE;
  2954. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2955. }
  2956. // contiguous u/y values
  2957. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2958. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2959. const half2 & dm2, const float & d8) {
  2960. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2961. int sumi_d = 0;
  2962. int sumi_m = 0;
  2963. #pragma unroll
  2964. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2965. int sumi_d_sc = 0;
  2966. const int sc = scales[i0 / (QI8_1/2)];
  2967. // fill int with 4x m
  2968. int m = sc >> 4;
  2969. m |= m << 8;
  2970. m |= m << 16;
  2971. #pragma unroll
  2972. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2973. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2974. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2975. }
  2976. sumi_d += sumi_d_sc * (sc & 0xF);
  2977. }
  2978. const float2 dm2f = __half22float2(dm2);
  2979. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2980. #else
  2981. NO_DEVICE_CODE;
  2982. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2983. }
  2984. #define VDR_Q3_K_Q8_1_MMVQ 1
  2985. #define VDR_Q3_K_Q8_1_MMQ 2
  2986. // contiguous v/x values
  2987. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2988. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2989. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2990. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2991. float sumf = 0.0f;
  2992. #pragma unroll
  2993. for (int i = 0; i < QR3_K; ++i) {
  2994. const int isc = scale_offset + 2*i;
  2995. const int isc_low = isc % (QK_K/32);
  2996. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2997. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2998. const int isc_high = isc % (QK_K/64);
  2999. const int sc_shift_high = 2 * (isc / (QK_K/64));
  3000. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  3001. const int sc = (sc_low | sc_high) - 32;
  3002. const int vil = (vl >> (2*i)) & 0x03030303;
  3003. const int vih = ((vh >> i) << 2) & 0x04040404;
  3004. const int vi = __vsubss4(vil, vih);
  3005. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  3006. }
  3007. return d3 * sumf;
  3008. #else
  3009. NO_DEVICE_CODE;
  3010. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3011. }
  3012. // contiguous u/y values
  3013. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  3014. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  3015. const float & d3, const float & d8) {
  3016. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3017. int sumi = 0;
  3018. #pragma unroll
  3019. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  3020. int sumi_sc = 0;
  3021. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  3022. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  3023. }
  3024. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  3025. }
  3026. return d3*d8 * sumi;
  3027. #else
  3028. NO_DEVICE_CODE;
  3029. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3030. }
  3031. #define VDR_Q4_K_Q8_1_MMVQ 2
  3032. #define VDR_Q4_K_Q8_1_MMQ 8
  3033. // contiguous v/x values
  3034. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  3035. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3036. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  3037. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3038. float sumf_d = 0.0f;
  3039. float sumf_m = 0.0f;
  3040. #pragma unroll
  3041. for (int i = 0; i < QR4_K; ++i) {
  3042. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  3043. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  3044. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  3045. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  3046. sumf_d += d8[i] * (dot1 * sc[i]);
  3047. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  3048. }
  3049. const float2 dm4f = __half22float2(dm4);
  3050. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3051. #else
  3052. NO_DEVICE_CODE;
  3053. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3054. }
  3055. // contiguous u/y values
  3056. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  3057. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3058. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  3059. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3060. float sumf_d = 0.0f;
  3061. float sumf_m = 0.0f;
  3062. #pragma unroll
  3063. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  3064. int sumi_d = 0;
  3065. #pragma unroll
  3066. for (int j = 0; j < QI8_1; ++j) {
  3067. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  3068. }
  3069. const float2 ds8f = __half22float2(ds8[i]);
  3070. sumf_d += ds8f.x * (sc[i] * sumi_d);
  3071. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  3072. }
  3073. const float2 dm4f = __half22float2(dm4);
  3074. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3075. #else
  3076. NO_DEVICE_CODE;
  3077. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3078. }
  3079. #define VDR_Q5_K_Q8_1_MMVQ 2
  3080. #define VDR_Q5_K_Q8_1_MMQ 8
  3081. // contiguous v/x values
  3082. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  3083. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3084. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  3085. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3086. float sumf_d = 0.0f;
  3087. float sumf_m = 0.0f;
  3088. #pragma unroll
  3089. for (int i = 0; i < QR5_K; ++i) {
  3090. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  3091. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  3092. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  3093. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  3094. const int v0i = vl0i | vh0i;
  3095. const int v1i = vl1i | vh1i;
  3096. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  3097. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  3098. sumf_d += d8[i] * (dot1 * sc[i]);
  3099. sumf_m += d8[i] * (dot2 * m[i]);
  3100. }
  3101. const float2 dm5f = __half22float2(dm5);
  3102. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  3103. #else
  3104. NO_DEVICE_CODE;
  3105. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3106. }
  3107. // contiguous u/y values
  3108. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  3109. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  3110. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  3111. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3112. float sumf_d = 0.0f;
  3113. float sumf_m = 0.0f;
  3114. #pragma unroll
  3115. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  3116. int sumi_d = 0;
  3117. #pragma unroll
  3118. for (int j = 0; j < QI8_1; ++j) {
  3119. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  3120. }
  3121. const float2 ds8f = __half22float2(ds8[i]);
  3122. sumf_d += ds8f.x * (sc[i] * sumi_d);
  3123. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  3124. }
  3125. const float2 dm4f = __half22float2(dm4);
  3126. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  3127. #else
  3128. NO_DEVICE_CODE;
  3129. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3130. }
  3131. #define VDR_Q6_K_Q8_1_MMVQ 1
  3132. #define VDR_Q6_K_Q8_1_MMQ 8
  3133. // contiguous v/x values
  3134. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  3135. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  3136. const float & d, const float * __restrict__ d8) {
  3137. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3138. float sumf = 0.0f;
  3139. #pragma unroll
  3140. for (int i = 0; i < QR6_K; ++i) {
  3141. const int sc = scales[4*i];
  3142. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  3143. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  3144. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  3145. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  3146. }
  3147. return d*sumf;
  3148. #else
  3149. NO_DEVICE_CODE;
  3150. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3151. }
  3152. // contiguous u/y values
  3153. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  3154. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  3155. const float & d6, const float * __restrict__ d8) {
  3156. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3157. float sumf_d = 0.0f;
  3158. #pragma unroll
  3159. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  3160. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  3161. #pragma unroll
  3162. for (int i = i0; i < i0 + 2; ++i) {
  3163. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  3164. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  3165. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  3166. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  3167. }
  3168. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  3169. }
  3170. return d6 * sumf_d;
  3171. #else
  3172. NO_DEVICE_CODE;
  3173. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3174. }
  3175. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  3176. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3177. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  3178. int v[VDR_Q4_0_Q8_1_MMVQ];
  3179. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  3180. #pragma unroll
  3181. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  3182. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  3183. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3184. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  3185. }
  3186. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  3187. }
  3188. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3189. GGML_UNUSED(x_qh);
  3190. GGML_UNUSED(x_sc);
  3191. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3192. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  3193. *x_ql = tile_x_qs;
  3194. *x_dm = (half2 *) tile_x_d;
  3195. }
  3196. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  3197. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3198. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3199. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3200. GGML_CUDA_ASSUME(i_offset >= 0);
  3201. GGML_CUDA_ASSUME(i_offset < nwarps);
  3202. GGML_CUDA_ASSUME(k >= 0);
  3203. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3204. const int kbx = k / QI4_0;
  3205. const int kqsx = k % QI4_0;
  3206. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  3207. float * x_dmf = (float *) x_dm;
  3208. #pragma unroll
  3209. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3210. int i = i0 + i_offset;
  3211. if (need_check) {
  3212. i = min(i, i_max);
  3213. }
  3214. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3215. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3216. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  3217. }
  3218. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  3219. const int kbxd = k % blocks_per_tile_x_row;
  3220. #pragma unroll
  3221. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  3222. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  3223. if (need_check) {
  3224. i = min(i, i_max);
  3225. }
  3226. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3227. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  3228. }
  3229. }
  3230. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  3231. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3232. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3233. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3234. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3235. const float * x_dmf = (const float *) x_dm;
  3236. int u[2*VDR_Q4_0_Q8_1_MMQ];
  3237. #pragma unroll
  3238. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  3239. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3240. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  3241. }
  3242. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  3243. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  3244. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  3245. }
  3246. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  3247. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3248. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  3249. int v[VDR_Q4_1_Q8_1_MMVQ];
  3250. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  3251. #pragma unroll
  3252. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  3253. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  3254. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3255. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  3256. }
  3257. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  3258. }
  3259. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3260. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3261. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  3262. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  3263. *x_ql = tile_x_qs;
  3264. *x_dm = tile_x_dm;
  3265. }
  3266. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  3267. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3268. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3269. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3270. GGML_CUDA_ASSUME(i_offset >= 0);
  3271. GGML_CUDA_ASSUME(i_offset < nwarps);
  3272. GGML_CUDA_ASSUME(k >= 0);
  3273. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3274. const int kbx = k / QI4_1;
  3275. const int kqsx = k % QI4_1;
  3276. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  3277. #pragma unroll
  3278. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3279. int i = i0 + i_offset;
  3280. if (need_check) {
  3281. i = min(i, i_max);
  3282. }
  3283. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3284. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3285. }
  3286. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  3287. const int kbxd = k % blocks_per_tile_x_row;
  3288. #pragma unroll
  3289. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  3290. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  3291. if (need_check) {
  3292. i = min(i, i_max);
  3293. }
  3294. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3295. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  3296. }
  3297. }
  3298. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  3299. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3300. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3301. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3302. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3303. int u[2*VDR_Q4_1_Q8_1_MMQ];
  3304. #pragma unroll
  3305. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  3306. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3307. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  3308. }
  3309. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  3310. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  3311. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  3312. }
  3313. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  3314. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3315. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  3316. int vl[VDR_Q5_0_Q8_1_MMVQ];
  3317. int vh[VDR_Q5_0_Q8_1_MMVQ];
  3318. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  3319. #pragma unroll
  3320. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  3321. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  3322. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  3323. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3324. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  3325. }
  3326. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  3327. }
  3328. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3329. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3330. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3331. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  3332. *x_ql = tile_x_ql;
  3333. *x_dm = (half2 *) tile_x_d;
  3334. }
  3335. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  3336. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3337. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3338. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3339. GGML_CUDA_ASSUME(i_offset >= 0);
  3340. GGML_CUDA_ASSUME(i_offset < nwarps);
  3341. GGML_CUDA_ASSUME(k >= 0);
  3342. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3343. const int kbx = k / QI5_0;
  3344. const int kqsx = k % QI5_0;
  3345. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  3346. #pragma unroll
  3347. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3348. int i = i0 + i_offset;
  3349. if (need_check) {
  3350. i = min(i, i_max);
  3351. }
  3352. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3353. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  3354. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  3355. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3356. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3357. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3358. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3359. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3360. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  3361. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3362. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3363. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3364. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3365. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3366. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3367. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  3368. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3369. }
  3370. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  3371. const int kbxd = k % blocks_per_tile_x_row;
  3372. float * x_dmf = (float *) x_dm;
  3373. #pragma unroll
  3374. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  3375. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  3376. if (need_check) {
  3377. i = min(i, i_max);
  3378. }
  3379. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3380. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  3381. }
  3382. }
  3383. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  3384. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3385. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3386. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3387. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3388. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  3389. const float * x_dmf = (const float *) x_dm;
  3390. const float * y_df = (const float *) y_ds;
  3391. int u[2*VDR_Q5_0_Q8_1_MMQ];
  3392. #pragma unroll
  3393. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  3394. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3395. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  3396. }
  3397. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  3398. (&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)]);
  3399. }
  3400. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  3401. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3402. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  3403. int vl[VDR_Q5_1_Q8_1_MMVQ];
  3404. int vh[VDR_Q5_1_Q8_1_MMVQ];
  3405. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  3406. #pragma unroll
  3407. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  3408. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  3409. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  3410. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3411. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  3412. }
  3413. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  3414. }
  3415. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3416. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3417. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3418. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  3419. *x_ql = tile_x_ql;
  3420. *x_dm = tile_x_dm;
  3421. }
  3422. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  3423. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3424. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3425. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3426. GGML_CUDA_ASSUME(i_offset >= 0);
  3427. GGML_CUDA_ASSUME(i_offset < nwarps);
  3428. GGML_CUDA_ASSUME(k >= 0);
  3429. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3430. const int kbx = k / QI5_1;
  3431. const int kqsx = k % QI5_1;
  3432. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  3433. #pragma unroll
  3434. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3435. int i = i0 + i_offset;
  3436. if (need_check) {
  3437. i = min(i, i_max);
  3438. }
  3439. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3440. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3441. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  3442. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3443. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3444. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3445. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3446. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3447. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3448. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3449. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3450. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3451. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3452. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3453. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3454. }
  3455. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  3456. const int kbxd = k % blocks_per_tile_x_row;
  3457. #pragma unroll
  3458. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  3459. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  3460. if (need_check) {
  3461. i = min(i, i_max);
  3462. }
  3463. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3464. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  3465. }
  3466. }
  3467. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  3468. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3469. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3470. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3471. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3472. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  3473. int u[2*VDR_Q5_1_Q8_1_MMQ];
  3474. #pragma unroll
  3475. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  3476. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3477. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  3478. }
  3479. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  3480. (&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)]);
  3481. }
  3482. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  3483. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3484. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  3485. int v[VDR_Q8_0_Q8_1_MMVQ];
  3486. int u[VDR_Q8_0_Q8_1_MMVQ];
  3487. #pragma unroll
  3488. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  3489. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  3490. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3491. }
  3492. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  3493. }
  3494. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3495. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3496. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3497. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  3498. *x_ql = tile_x_qs;
  3499. *x_dm = (half2 *) tile_x_d;
  3500. }
  3501. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  3502. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3503. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3504. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3505. GGML_CUDA_ASSUME(i_offset >= 0);
  3506. GGML_CUDA_ASSUME(i_offset < nwarps);
  3507. GGML_CUDA_ASSUME(k >= 0);
  3508. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3509. const int kbx = k / QI8_0;
  3510. const int kqsx = k % QI8_0;
  3511. float * x_dmf = (float *) x_dm;
  3512. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  3513. #pragma unroll
  3514. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3515. int i = i0 + i_offset;
  3516. if (need_check) {
  3517. i = min(i, i_max);
  3518. }
  3519. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3520. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  3521. }
  3522. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  3523. const int kbxd = k % blocks_per_tile_x_row;
  3524. #pragma unroll
  3525. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  3526. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  3527. if (need_check) {
  3528. i = min(i, i_max);
  3529. }
  3530. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3531. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  3532. }
  3533. }
  3534. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  3535. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3536. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3537. GGML_UNUSED(x_qh); GGML_UNUSED(x_sc);
  3538. const float * x_dmf = (const float *) x_dm;
  3539. const float * y_df = (const float *) y_ds;
  3540. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  3541. (&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],
  3542. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  3543. }
  3544. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  3545. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3546. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  3547. const int bq8_offset = QR2_K * (iqs / QI8_1);
  3548. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3549. const uint8_t * scales = bq2_K->scales + scale_offset;
  3550. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  3551. int u[QR2_K];
  3552. float d8[QR2_K];
  3553. #pragma unroll
  3554. for (int i = 0; i < QR2_K; ++ i) {
  3555. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3556. d8[i] = __low2float(bq8_1[bq8_offset + i].ds);
  3557. }
  3558. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  3559. }
  3560. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3561. GGML_UNUSED(x_qh);
  3562. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3563. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  3564. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3565. *x_ql = tile_x_ql;
  3566. *x_dm = tile_x_dm;
  3567. *x_sc = tile_x_sc;
  3568. }
  3569. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  3570. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3571. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3572. GGML_UNUSED(x_qh);
  3573. GGML_CUDA_ASSUME(i_offset >= 0);
  3574. GGML_CUDA_ASSUME(i_offset < nwarps);
  3575. GGML_CUDA_ASSUME(k >= 0);
  3576. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3577. const int kbx = k / QI2_K;
  3578. const int kqsx = k % QI2_K;
  3579. const block_q2_K * bx0 = (const block_q2_K *) vx;
  3580. #pragma unroll
  3581. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3582. int i = i0 + i_offset;
  3583. if (need_check) {
  3584. i = min(i, i_max);
  3585. }
  3586. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  3587. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3588. }
  3589. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  3590. const int kbxd = k % blocks_per_tile_x_row;
  3591. #pragma unroll
  3592. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  3593. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  3594. if (need_check) {
  3595. i = min(i, i_max);
  3596. }
  3597. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3598. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  3599. }
  3600. #pragma unroll
  3601. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3602. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3603. if (need_check) {
  3604. i = min(i, i_max);
  3605. }
  3606. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  3607. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  3608. }
  3609. }
  3610. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  3611. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3612. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3613. GGML_UNUSED(x_qh);
  3614. const int kbx = k / QI2_K;
  3615. const int ky = (k % QI2_K) * QR2_K;
  3616. const float * y_df = (const float *) y_ds;
  3617. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  3618. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  3619. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  3620. #pragma unroll
  3621. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  3622. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3623. }
  3624. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  3625. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  3626. 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]);
  3627. }
  3628. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  3629. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3630. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  3631. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  3632. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3633. const float d = bq3_K->d;
  3634. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  3635. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3636. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  3637. int u[QR3_K];
  3638. float d8[QR3_K];
  3639. #pragma unroll
  3640. for (int i = 0; i < QR3_K; ++i) {
  3641. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3642. d8[i] = __low2float(bq8_1[bq8_offset + i].ds);
  3643. }
  3644. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  3645. }
  3646. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3647. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3648. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  3649. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  3650. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3651. *x_ql = tile_x_ql;
  3652. *x_dm = tile_x_dm;
  3653. *x_qh = tile_x_qh;
  3654. *x_sc = tile_x_sc;
  3655. }
  3656. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  3657. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3658. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3659. GGML_CUDA_ASSUME(i_offset >= 0);
  3660. GGML_CUDA_ASSUME(i_offset < nwarps);
  3661. GGML_CUDA_ASSUME(k >= 0);
  3662. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3663. const int kbx = k / QI3_K;
  3664. const int kqsx = k % QI3_K;
  3665. const block_q3_K * bx0 = (const block_q3_K *) vx;
  3666. #pragma unroll
  3667. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3668. int i = i0 + i_offset;
  3669. if (need_check) {
  3670. i = min(i, i_max);
  3671. }
  3672. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  3673. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3674. }
  3675. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  3676. const int kbxd = k % blocks_per_tile_x_row;
  3677. float * x_dmf = (float *) x_dm;
  3678. #pragma unroll
  3679. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  3680. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  3681. if (need_check) {
  3682. i = min(i, i_max);
  3683. }
  3684. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3685. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  3686. }
  3687. #pragma unroll
  3688. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  3689. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  3690. if (need_check) {
  3691. i = min(i, i_max);
  3692. }
  3693. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  3694. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3695. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  3696. }
  3697. #pragma unroll
  3698. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3699. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3700. if (need_check) {
  3701. i = min(i, i_max);
  3702. }
  3703. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  3704. const int ksc = k % (QI3_K/4);
  3705. const int ksc_low = ksc % (QI3_K/8);
  3706. const int shift_low = 4 * (ksc / (QI3_K/8));
  3707. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3708. const int ksc_high = QI3_K/8;
  3709. const int shift_high = 2 * ksc;
  3710. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3711. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3712. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3713. }
  3714. }
  3715. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3716. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3717. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3718. const int kbx = k / QI3_K;
  3719. const int ky = (k % QI3_K) * QR3_K;
  3720. const float * x_dmf = (const float *) x_dm;
  3721. const float * y_df = (const float *) y_ds;
  3722. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3723. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3724. #pragma unroll
  3725. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3726. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3727. const int shift = 2 * ((ky % 32) / 8);
  3728. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3729. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3730. const int vlh = (vh << 2) & 0x04040404;
  3731. v[l] = __vsubss4(vll, vlh);
  3732. }
  3733. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3734. 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]);
  3735. }
  3736. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3737. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3738. #ifndef GGML_QKK_64
  3739. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3740. int v[2];
  3741. int u[2*QR4_K];
  3742. float d8[QR4_K];
  3743. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3744. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3745. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3746. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3747. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3748. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3749. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3750. v[0] = q4[0];
  3751. v[1] = q4[4];
  3752. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3753. uint16_t aux[2];
  3754. const int j = bq8_offset/2;
  3755. if (j < 2) {
  3756. aux[0] = scales[j+0] & 0x3f3f;
  3757. aux[1] = scales[j+2] & 0x3f3f;
  3758. } else {
  3759. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3760. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3761. }
  3762. const uint8_t * sc = (const uint8_t *)aux;
  3763. const uint8_t * m = sc + 2;
  3764. for (int i = 0; i < QR4_K; ++i) {
  3765. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3766. d8[i] = __low2float(bq8i->ds);
  3767. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3768. u[2*i+0] = q8[0];
  3769. u[2*i+1] = q8[4];
  3770. }
  3771. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3772. #else
  3773. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3774. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3775. float sumf_d = 0.0f;
  3776. float sumf_m = 0.0f;
  3777. uint16_t aux16[2];
  3778. const uint8_t * s = (const uint8_t *)aux16;
  3779. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3780. aux16[0] = a[0] & 0x0f0f;
  3781. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3782. const float dall = bq4_K->dm[0];
  3783. const float dmin = bq4_K->dm[1];
  3784. const float d8_1 = __low2float(bq8_1[0].ds);
  3785. const float d8_2 = __low2float(bq8_1[1].ds);
  3786. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3787. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3788. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3789. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3790. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3791. const int v1 = q4[0];
  3792. const int v2 = q4[4];
  3793. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3794. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3795. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3796. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3797. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3798. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3799. return dall * sumf_d - dmin * sumf_m;
  3800. #else
  3801. NO_DEVICE_CODE;
  3802. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3803. #endif
  3804. }
  3805. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3806. GGML_UNUSED(x_qh);
  3807. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3808. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3809. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3810. *x_ql = tile_x_ql;
  3811. *x_dm = tile_x_dm;
  3812. *x_sc = tile_x_sc;
  3813. }
  3814. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3815. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3816. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3817. GGML_UNUSED(x_qh);
  3818. GGML_CUDA_ASSUME(i_offset >= 0);
  3819. GGML_CUDA_ASSUME(i_offset < nwarps);
  3820. GGML_CUDA_ASSUME(k >= 0);
  3821. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3822. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3823. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3824. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3825. #pragma unroll
  3826. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3827. int i = i0 + i_offset;
  3828. if (need_check) {
  3829. i = min(i, i_max);
  3830. }
  3831. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3832. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3833. }
  3834. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3835. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3836. #pragma unroll
  3837. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3838. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3839. if (need_check) {
  3840. i = min(i, i_max);
  3841. }
  3842. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3843. #if QK_K == 256
  3844. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3845. #else
  3846. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3847. #endif
  3848. }
  3849. #pragma unroll
  3850. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3851. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3852. if (need_check) {
  3853. i = min(i, i_max);
  3854. }
  3855. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3856. const int * scales = (const int *) bxi->scales;
  3857. const int ksc = k % (WARP_SIZE/8);
  3858. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3859. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3860. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3861. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3862. }
  3863. }
  3864. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3865. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3866. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3867. GGML_UNUSED(x_qh);
  3868. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3869. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3870. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3871. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3872. }
  3873. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3874. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3875. #ifndef GGML_QKK_64
  3876. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3877. int vl[2];
  3878. int vh[2];
  3879. int u[2*QR5_K];
  3880. float d8[QR5_K];
  3881. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3882. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3883. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3884. vl[0] = ql[0];
  3885. vl[1] = ql[4];
  3886. vh[0] = qh[0] >> bq8_offset;
  3887. vh[1] = qh[4] >> bq8_offset;
  3888. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3889. uint16_t aux[2];
  3890. const int j = bq8_offset/2;
  3891. if (j < 2) {
  3892. aux[0] = scales[j+0] & 0x3f3f;
  3893. aux[1] = scales[j+2] & 0x3f3f;
  3894. } else {
  3895. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3896. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3897. }
  3898. const uint8_t * sc = (const uint8_t *)aux;
  3899. const uint8_t * m = sc + 2;
  3900. #pragma unroll
  3901. for (int i = 0; i < QR5_K; ++i) {
  3902. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3903. d8[i] = __low2float(bq8i->ds);
  3904. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3905. u[2*i+0] = q8[0];
  3906. u[2*i+1] = q8[4];
  3907. }
  3908. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3909. #else
  3910. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3911. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3912. const int8_t * s = bq5_K->scales;
  3913. const float d = bq5_K->d;
  3914. const float d8_1 = __low2half(bq8_1[0].ds);
  3915. const float d8_2 = __low2half(bq8_1[1].ds);
  3916. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3917. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3918. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3919. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3920. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3921. const int vl1 = ql[0];
  3922. const int vl2 = ql[4];
  3923. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3924. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3925. const int in = step%8; // 0, 4, 0, 4
  3926. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3927. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3928. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3929. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3930. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3931. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3932. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3933. return d * sumf_d;
  3934. #else
  3935. NO_DEVICE_CODE;
  3936. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3937. #endif
  3938. }
  3939. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3940. GGML_UNUSED(x_qh);
  3941. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3942. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3943. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3944. *x_ql = tile_x_ql;
  3945. *x_dm = tile_x_dm;
  3946. *x_sc = tile_x_sc;
  3947. }
  3948. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3949. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3950. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3951. GGML_UNUSED(x_qh);
  3952. GGML_CUDA_ASSUME(i_offset >= 0);
  3953. GGML_CUDA_ASSUME(i_offset < nwarps);
  3954. GGML_CUDA_ASSUME(k >= 0);
  3955. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3956. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3957. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3958. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3959. #pragma unroll
  3960. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3961. int i = i0 + i_offset;
  3962. if (need_check) {
  3963. i = min(i, i_max);
  3964. }
  3965. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3966. const int ky = QR5_K*kqsx;
  3967. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3968. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3969. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3970. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3971. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3972. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3973. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3974. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3975. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3976. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3977. }
  3978. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3979. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3980. #pragma unroll
  3981. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3982. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3983. if (need_check) {
  3984. i = min(i, i_max);
  3985. }
  3986. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3987. #if QK_K == 256
  3988. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3989. #endif
  3990. }
  3991. #pragma unroll
  3992. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3993. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3994. if (need_check) {
  3995. i = min(i, i_max);
  3996. }
  3997. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3998. const int * scales = (const int *) bxi->scales;
  3999. const int ksc = k % (WARP_SIZE/8);
  4000. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  4001. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  4002. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  4003. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  4004. }
  4005. }
  4006. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  4007. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  4008. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  4009. GGML_UNUSED(x_qh);
  4010. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  4011. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  4012. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  4013. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  4014. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  4015. }
  4016. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  4017. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4018. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  4019. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  4020. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  4021. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  4022. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  4023. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  4024. const int8_t * scales = bq6_K->scales + scale_offset;
  4025. int u[QR6_K];
  4026. float d8[QR6_K];
  4027. #pragma unroll
  4028. for (int i = 0; i < QR6_K; ++i) {
  4029. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  4030. d8[i] = __low2float(bq8_1[bq8_offset + 2*i].ds);
  4031. }
  4032. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  4033. }
  4034. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  4035. GGML_UNUSED(x_qh);
  4036. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  4037. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  4038. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  4039. *x_ql = tile_x_ql;
  4040. *x_dm = tile_x_dm;
  4041. *x_sc = tile_x_sc;
  4042. }
  4043. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  4044. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  4045. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  4046. GGML_UNUSED(x_qh);
  4047. GGML_CUDA_ASSUME(i_offset >= 0);
  4048. GGML_CUDA_ASSUME(i_offset < nwarps);
  4049. GGML_CUDA_ASSUME(k >= 0);
  4050. GGML_CUDA_ASSUME(k < WARP_SIZE);
  4051. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  4052. const int kqsx = k % QI6_K; // == k if QK_K == 256
  4053. const block_q6_K * bx0 = (const block_q6_K *) vx;
  4054. #pragma unroll
  4055. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  4056. int i = i0 + i_offset;
  4057. if (need_check) {
  4058. i = min(i, i_max);
  4059. }
  4060. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  4061. const int ky = QR6_K*kqsx;
  4062. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  4063. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  4064. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  4065. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  4066. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  4067. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  4068. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  4069. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  4070. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  4071. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  4072. }
  4073. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  4074. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  4075. float * x_dmf = (float *) x_dm;
  4076. #pragma unroll
  4077. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  4078. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  4079. if (need_check) {
  4080. i = min(i, i_max);
  4081. }
  4082. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  4083. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  4084. }
  4085. #pragma unroll
  4086. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  4087. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  4088. if (need_check) {
  4089. i = min(i, i_max);
  4090. }
  4091. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  4092. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  4093. }
  4094. }
  4095. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  4096. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  4097. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  4098. GGML_UNUSED(x_qh);
  4099. const float * x_dmf = (const float *) x_dm;
  4100. const float * y_df = (const float *) y_ds;
  4101. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  4102. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  4103. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  4104. 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]);
  4105. }
  4106. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  4107. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4108. #if QK_K == 256
  4109. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  4110. #if QR2_XXS == 8
  4111. const int ib32 = iqs;
  4112. const uint16_t * q2 = bq2->qs + 4*ib32;
  4113. const uint8_t * aux8 = (const uint8_t *)q2;
  4114. const int8_t * q8 = bq8_1[ib32].qs;
  4115. uint32_t aux32 = q2[2] | (q2[3] << 16);
  4116. int sumi = 0;
  4117. for (int l = 0; l < 4; ++l) {
  4118. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  4119. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  4120. for (int j = 0; j < 8; ++j) {
  4121. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  4122. }
  4123. q8 += 8;
  4124. aux32 >>= 7;
  4125. }
  4126. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  4127. return d * sumi;
  4128. #else
  4129. // iqs is 0...15
  4130. const int ib32 = iqs/2;
  4131. const int il = iqs%2;
  4132. const uint16_t * q2 = bq2->qs + 4*ib32;
  4133. const uint8_t * aux8 = (const uint8_t *)q2;
  4134. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  4135. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  4136. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  4137. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  4138. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  4139. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  4140. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  4141. int sumi1 = 0, sumi2 = 0;
  4142. for (int j = 0; j < 8; ++j) {
  4143. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  4144. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  4145. }
  4146. return d * (sumi1 + sumi2);
  4147. #endif
  4148. #else
  4149. assert(false);
  4150. return 0.f;
  4151. #endif
  4152. }
  4153. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  4154. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4155. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4156. #if QK_K == 256
  4157. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  4158. const int ib32 = iqs;
  4159. const uint16_t * q2 = bq2->qs + 4*ib32;
  4160. const int8_t * q8 = bq8_1[ib32].qs;
  4161. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  4162. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  4163. int sumi1 = 0;
  4164. for (int l = 0; l < 2; ++l) {
  4165. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  4166. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  4167. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  4168. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  4169. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  4170. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  4171. q8 += 8;
  4172. }
  4173. int sumi2 = 0;
  4174. for (int l = 2; l < 4; ++l) {
  4175. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  4176. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  4177. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  4178. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  4179. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  4180. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  4181. q8 += 8;
  4182. }
  4183. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  4184. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  4185. #else
  4186. GGML_UNUSED(ksigns64);
  4187. assert(false);
  4188. return 0.f;
  4189. #endif
  4190. #else
  4191. GGML_UNUSED(ksigns64);
  4192. assert(false);
  4193. return 0.f;
  4194. #endif
  4195. }
  4196. // TODO
  4197. static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(
  4198. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4199. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4200. #if QK_K == 256
  4201. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  4202. const int ib32 = iqs;
  4203. const int8_t * q8 = bq8_1[ib32].qs;
  4204. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  4205. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  4206. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  4207. int sumi1 = 0;
  4208. for (int l = 0; l < 2; ++l) {
  4209. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  4210. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4211. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4212. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  4213. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  4214. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  4215. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  4216. q8 += 8;
  4217. }
  4218. int sumi2 = 0;
  4219. for (int l = 2; l < 4; ++l) {
  4220. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  4221. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4222. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4223. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  4224. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  4225. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  4226. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  4227. q8 += 8;
  4228. }
  4229. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  4230. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  4231. #else
  4232. GGML_UNUSED(ksigns64);
  4233. assert(false);
  4234. return 0.f;
  4235. #endif
  4236. #else
  4237. GGML_UNUSED(ksigns64);
  4238. assert(false);
  4239. return 0.f;
  4240. #endif
  4241. }
  4242. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  4243. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4244. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4245. #if QK_K == 256
  4246. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  4247. const int ib32 = iqs;
  4248. const uint8_t * q3 = bq2->qs + 8*ib32;
  4249. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  4250. const int8_t * q8 = bq8_1[ib32].qs;
  4251. uint32_t aux32 = gas[0] | (gas[1] << 16);
  4252. int sumi = 0;
  4253. for (int l = 0; l < 4; ++l) {
  4254. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  4255. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  4256. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  4257. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  4258. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  4259. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  4260. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  4261. q8 += 8;
  4262. aux32 >>= 7;
  4263. }
  4264. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  4265. return d * sumi;
  4266. #else
  4267. assert(false);
  4268. return 0.f;
  4269. #endif
  4270. #else
  4271. assert(false);
  4272. return 0.f;
  4273. #endif
  4274. }
  4275. // TODO: don't use lookup table for signs
  4276. static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(
  4277. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4278. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4279. #if QK_K == 256
  4280. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  4281. const int ib32 = iqs;
  4282. const uint8_t * qs = bq2->qs + 8*ib32;
  4283. const int8_t * q8 = bq8_1[ib32].qs;
  4284. int sumi = 0;
  4285. for (int l = 0; l < 4; ++l) {
  4286. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  4287. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  4288. uint32_t signs0 = __vcmpeq4(((bq2->signs[4*ib32+l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  4289. uint32_t signs1 = __vcmpeq4(((bq2->signs[4*ib32+l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  4290. const int grid_l = __vsub4(grid1[0] ^ signs0, signs0);
  4291. const int grid_h = __vsub4(grid2[0] ^ signs1, signs1);
  4292. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  4293. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  4294. q8 += 8;
  4295. }
  4296. const float d = (float)bq2->d * (1 + 2*((bq2->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * __low2float(bq8_1[ib32].ds);
  4297. return d * sumi;
  4298. #else
  4299. assert(false);
  4300. return 0.f;
  4301. #endif
  4302. #else
  4303. assert(false);
  4304. return 0.f;
  4305. #endif
  4306. }
  4307. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  4308. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4309. #if QK_K == 256
  4310. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  4311. const int ib32 = iqs;
  4312. int sumi = 0;
  4313. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4314. const int * q8 = (const int *)bq8_1[ib32].qs;
  4315. for (int l = 0; l < 4; ++l) {
  4316. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  4317. int grid0 = grid[0] & 0x0f0f0f0f;
  4318. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  4319. sumi = __dp4a(q8[2*l+1], grid1, __dp4a(q8[2*l+0], grid0, sumi));
  4320. }
  4321. #else
  4322. const int8_t * q8 = bq8_1[ib32].qs;
  4323. for (int l = 0; l < 4; ++l) {
  4324. const uint8_t * grid = (const uint8_t *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  4325. for (int j = 0; j < 4; ++j) {
  4326. sumi += q8[j] * (grid[j] & 0xf) + q8[j+4] * (grid[j] >> 4);
  4327. }
  4328. q8 += 8;
  4329. }
  4330. #endif
  4331. const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA;
  4332. const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1);
  4333. const float d = d1q * __low2float (bq8_1[ib32].ds);
  4334. const float m = d1q * __high2float(bq8_1[ib32].ds);
  4335. return d * sumi + m * delta;
  4336. #else
  4337. assert(false);
  4338. return 0.f;
  4339. #endif
  4340. }
  4341. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4342. static __device__ __forceinline__ void get_int_from_table_16(const uint32_t & q4, const uint8_t * values,
  4343. int & val1, int & val2) {
  4344. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  4345. aux32 = q4 & 0x0f0f0f0f;
  4346. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  4347. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  4348. val1 = v1 | (v2 << 16);
  4349. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  4350. v1 = values[q8[0]] | (values[q8[1]] << 8);
  4351. v2 = values[q8[2]] | (values[q8[3]] << 8);
  4352. val2 = v1 | (v2 << 16);
  4353. }
  4354. #endif
  4355. static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
  4356. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4357. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  4358. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4359. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  4360. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  4361. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  4362. int v1, v2;
  4363. int sumi1 = 0, sumi2 = 0;
  4364. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  4365. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  4366. get_int_from_table_16(aux, values, v1, v2);
  4367. sumi1 = __dp4a(v1, q8[l+0], sumi1);
  4368. sumi2 = __dp4a(v2, q8[l+4], sumi2);
  4369. }
  4370. #else
  4371. const uint8_t * q4 = bq->qs + 4*iqs;
  4372. const int8_t * q8 = bq8_1->qs + 4*iqs;
  4373. int sumi1 = 0, sumi2 = 0;
  4374. for (int l = 0; l < 4*VDR_Q4_0_Q8_1_MMVQ; ++l) {
  4375. sumi1 += q8[l+ 0] * kvalues_iq4nl[q4[l] & 0xf];
  4376. sumi2 += q8[l+16] * kvalues_iq4nl[q4[l] >> 4];
  4377. }
  4378. #endif
  4379. const float d = (float)bq->d * __low2float(bq8_1->ds);
  4380. return d * (sumi1 + sumi2);
  4381. }
  4382. static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(
  4383. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  4384. #if QK_K == 256
  4385. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  4386. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  4387. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  4388. //// iqs is 0...7
  4389. //const int ib64 = iqs/2;
  4390. //const int il = iqs%2;
  4391. //const int32_t * q8_1 = (const int *)bq8_1[2*ib64+0].qs + 2*il;
  4392. //const int32_t * q8_2 = (const int *)bq8_1[2*ib64+1].qs + 2*il;
  4393. //const uint32_t * q4_1 = (const uint32_t *)bq4->qs + 8*ib64 + 2*il;
  4394. //const uint32_t * q4_2 = q4_1 + 4;
  4395. //const int8_t ls1 = (bq4->scales_l[ib64] & 0xf) | (((bq4->scales_h >> (4*ib64+0)) & 3) << 4);
  4396. //const int8_t ls2 = (bq4->scales_l[ib64] >> 4) | (((bq4->scales_h >> (4*ib64+2)) & 3) << 4);
  4397. //const float d1 = (float)bq4->d * (ls1 - 32) * __low2float(bq8_1[2*ib64+0].ds);
  4398. //const float d2 = (float)bq4->d * (ls2 - 32) * __low2float(bq8_1[2*ib64+1].ds);
  4399. //int v1, v2;
  4400. //int sumi1 = 0, sumi2 = 0;
  4401. //for (int j = 0; j < 2; ++j) {
  4402. // get_int_from_table_16(q4_1[j], values, v1, v2);
  4403. // sumi1 = __dp4a(v2, q8_1[j+4], __dp4a(v1, q8_1[j+0], sumi1));
  4404. // get_int_from_table_16(q4_2[j], values, v1, v2);
  4405. // sumi2 = __dp4a(v2, q8_2[j+4], __dp4a(v1, q8_2[j+0], sumi2));
  4406. //}
  4407. //return d1 * sumi1 + d2 * sumi2;
  4408. // iqs is 0...7
  4409. const int ib32 = iqs;
  4410. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  4411. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  4412. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  4413. const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  4414. int v1, v2;
  4415. int sumi1 = 0, sumi2 = 0;
  4416. for (int j = 0; j < 4; ++j) {
  4417. get_int_from_table_16(q4[j], values, v1, v2);
  4418. sumi1 = __dp4a(v1, q8[j+0], sumi1);
  4419. sumi2 = __dp4a(v2, q8[j+4], sumi2);
  4420. }
  4421. return d * (sumi1 + sumi2);
  4422. //// iqs is 0...15
  4423. //const int ib32 = iqs/2;
  4424. //const int il = iqs%2;
  4425. //const int32_t * q8 = (const int *)bq8_1[ib32].qs + 2*il;
  4426. //const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32 + 2*il;
  4427. //const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  4428. //const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  4429. //int v1, v2;
  4430. //int sumi1 = 0, sumi2 = 0;
  4431. //for (int j = 0; j < 2; ++j) {
  4432. // get_int_from_table_16(q4[j], values, v1, v2);
  4433. // sumi1 = __dp4a(v1, q8[j+0], sumi1);
  4434. // sumi2 = __dp4a(v2, q8[j+4], sumi2);
  4435. //}
  4436. //return d * (sumi1 + sumi2);
  4437. #else
  4438. assert(false);
  4439. return 0.f;
  4440. #endif
  4441. #else
  4442. return vec_dot_iq4_xs_q8_1(vbq, bq8_1, iqs);
  4443. #endif
  4444. }
  4445. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  4446. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  4447. static __device__ __forceinline__ void mul_mat_q(
  4448. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4449. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4450. const block_q_t * x = (const block_q_t *) vx;
  4451. const block_q8_1 * y = (const block_q8_1 *) vy;
  4452. const int blocks_per_row_x = ncols_x / qk;
  4453. const int blocks_per_col_y = nrows_y / QK8_1;
  4454. const int blocks_per_warp = WARP_SIZE / qi;
  4455. const int & ncols_dst = ncols_y;
  4456. const int row_dst_0 = blockIdx.x*mmq_y;
  4457. const int & row_x_0 = row_dst_0;
  4458. const int col_dst_0 = blockIdx.y*mmq_x;
  4459. const int & col_y_0 = col_dst_0;
  4460. int * tile_x_ql = nullptr;
  4461. half2 * tile_x_dm = nullptr;
  4462. int * tile_x_qh = nullptr;
  4463. int * tile_x_sc = nullptr;
  4464. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  4465. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  4466. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  4467. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  4468. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  4469. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  4470. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  4471. #pragma unroll
  4472. for (int ir = 0; ir < qr; ++ir) {
  4473. const int kqs = ir*WARP_SIZE + threadIdx.x;
  4474. const int kbxd = kqs / QI8_1;
  4475. #pragma unroll
  4476. for (int i = 0; i < mmq_x; i += nwarps) {
  4477. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  4478. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  4479. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  4480. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  4481. }
  4482. #pragma unroll
  4483. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  4484. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  4485. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  4486. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  4487. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  4488. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  4489. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  4490. if (need_sum) {
  4491. *dsi_dst = *dsi_src;
  4492. } else {
  4493. float * dfi_dst = (float *) dsi_dst;
  4494. *dfi_dst = __low2float(*dsi_src);
  4495. }
  4496. }
  4497. __syncthreads();
  4498. // #pragma unroll // unrolling this loop causes too much register pressure
  4499. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  4500. #pragma unroll
  4501. for (int j = 0; j < mmq_x; j += nwarps) {
  4502. #pragma unroll
  4503. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  4504. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  4505. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  4506. threadIdx.x + i, threadIdx.y + j, k);
  4507. }
  4508. }
  4509. }
  4510. __syncthreads();
  4511. }
  4512. }
  4513. #pragma unroll
  4514. for (int j = 0; j < mmq_x; j += nwarps) {
  4515. const int col_dst = col_dst_0 + j + threadIdx.y;
  4516. if (col_dst >= ncols_dst) {
  4517. return;
  4518. }
  4519. #pragma unroll
  4520. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  4521. const int row_dst = row_dst_0 + threadIdx.x + i;
  4522. if (row_dst >= nrows_dst) {
  4523. continue;
  4524. }
  4525. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  4526. }
  4527. }
  4528. }
  4529. #define MMQ_X_Q4_0_RDNA2 64
  4530. #define MMQ_Y_Q4_0_RDNA2 128
  4531. #define NWARPS_Q4_0_RDNA2 8
  4532. #define MMQ_X_Q4_0_RDNA1 64
  4533. #define MMQ_Y_Q4_0_RDNA1 64
  4534. #define NWARPS_Q4_0_RDNA1 8
  4535. #if defined(CUDA_USE_TENSOR_CORES)
  4536. #define MMQ_X_Q4_0_AMPERE 4
  4537. #define MMQ_Y_Q4_0_AMPERE 32
  4538. #define NWARPS_Q4_0_AMPERE 4
  4539. #else
  4540. #define MMQ_X_Q4_0_AMPERE 64
  4541. #define MMQ_Y_Q4_0_AMPERE 128
  4542. #define NWARPS_Q4_0_AMPERE 4
  4543. #endif
  4544. #define MMQ_X_Q4_0_PASCAL 64
  4545. #define MMQ_Y_Q4_0_PASCAL 64
  4546. #define NWARPS_Q4_0_PASCAL 8
  4547. template <bool need_check> static __global__ void
  4548. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4549. #if defined(RDNA3) || defined(RDNA2)
  4550. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  4551. #endif // defined(RDNA3) || defined(RDNA2)
  4552. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4553. mul_mat_q4_0(
  4554. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4555. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4556. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4557. #if defined(RDNA3) || defined(RDNA2)
  4558. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  4559. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  4560. const int nwarps = NWARPS_Q4_0_RDNA2;
  4561. #else
  4562. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  4563. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  4564. const int nwarps = NWARPS_Q4_0_RDNA1;
  4565. #endif // defined(RDNA3) || defined(RDNA2)
  4566. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4567. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4568. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4569. #elif __CUDA_ARCH__ >= CC_VOLTA
  4570. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  4571. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  4572. const int nwarps = NWARPS_Q4_0_AMPERE;
  4573. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4574. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4575. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4576. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4577. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  4578. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  4579. const int nwarps = NWARPS_Q4_0_PASCAL;
  4580. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4581. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4582. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4583. #else
  4584. GGML_UNUSED(vec_dot_q4_0_q8_1_mul_mat);
  4585. NO_DEVICE_CODE;
  4586. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4587. }
  4588. #define MMQ_X_Q4_1_RDNA2 64
  4589. #define MMQ_Y_Q4_1_RDNA2 128
  4590. #define NWARPS_Q4_1_RDNA2 8
  4591. #define MMQ_X_Q4_1_RDNA1 64
  4592. #define MMQ_Y_Q4_1_RDNA1 64
  4593. #define NWARPS_Q4_1_RDNA1 8
  4594. #if defined(CUDA_USE_TENSOR_CORES)
  4595. #define MMQ_X_Q4_1_AMPERE 4
  4596. #define MMQ_Y_Q4_1_AMPERE 32
  4597. #define NWARPS_Q4_1_AMPERE 4
  4598. #else
  4599. #define MMQ_X_Q4_1_AMPERE 64
  4600. #define MMQ_Y_Q4_1_AMPERE 128
  4601. #define NWARPS_Q4_1_AMPERE 4
  4602. #endif
  4603. #define MMQ_X_Q4_1_PASCAL 64
  4604. #define MMQ_Y_Q4_1_PASCAL 64
  4605. #define NWARPS_Q4_1_PASCAL 8
  4606. template <bool need_check> static __global__ void
  4607. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4608. #if defined(RDNA3) || defined(RDNA2)
  4609. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  4610. #endif // defined(RDNA3) || defined(RDNA2)
  4611. #elif __CUDA_ARCH__ < CC_VOLTA
  4612. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  4613. #endif // __CUDA_ARCH__ < CC_VOLTA
  4614. mul_mat_q4_1(
  4615. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4616. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4617. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4618. #if defined(RDNA3) || defined(RDNA2)
  4619. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  4620. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  4621. const int nwarps = NWARPS_Q4_1_RDNA2;
  4622. #else
  4623. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  4624. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  4625. const int nwarps = NWARPS_Q4_1_RDNA1;
  4626. #endif // defined(RDNA3) || defined(RDNA2)
  4627. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4628. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4629. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4630. #elif __CUDA_ARCH__ >= CC_VOLTA
  4631. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  4632. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  4633. const int nwarps = NWARPS_Q4_1_AMPERE;
  4634. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4635. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4636. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4637. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4638. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  4639. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  4640. const int nwarps = NWARPS_Q4_1_PASCAL;
  4641. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4642. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4643. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4644. #else
  4645. GGML_UNUSED(vec_dot_q4_1_q8_1_mul_mat);
  4646. NO_DEVICE_CODE;
  4647. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4648. }
  4649. #define MMQ_X_Q5_0_RDNA2 64
  4650. #define MMQ_Y_Q5_0_RDNA2 128
  4651. #define NWARPS_Q5_0_RDNA2 8
  4652. #define MMQ_X_Q5_0_RDNA1 64
  4653. #define MMQ_Y_Q5_0_RDNA1 64
  4654. #define NWARPS_Q5_0_RDNA1 8
  4655. #if defined(CUDA_USE_TENSOR_CORES)
  4656. #define MMQ_X_Q5_0_AMPERE 4
  4657. #define MMQ_Y_Q5_0_AMPERE 32
  4658. #define NWARPS_Q5_0_AMPERE 4
  4659. #else
  4660. #define MMQ_X_Q5_0_AMPERE 128
  4661. #define MMQ_Y_Q5_0_AMPERE 64
  4662. #define NWARPS_Q5_0_AMPERE 4
  4663. #endif
  4664. #define MMQ_X_Q5_0_PASCAL 64
  4665. #define MMQ_Y_Q5_0_PASCAL 64
  4666. #define NWARPS_Q5_0_PASCAL 8
  4667. template <bool need_check> static __global__ void
  4668. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4669. #if defined(RDNA3) || defined(RDNA2)
  4670. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  4671. #endif // defined(RDNA3) || defined(RDNA2)
  4672. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4673. mul_mat_q5_0(
  4674. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4675. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4676. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4677. #if defined(RDNA3) || defined(RDNA2)
  4678. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  4679. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  4680. const int nwarps = NWARPS_Q5_0_RDNA2;
  4681. #else
  4682. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  4683. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  4684. const int nwarps = NWARPS_Q5_0_RDNA1;
  4685. #endif // defined(RDNA3) || defined(RDNA2)
  4686. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4687. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4688. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4689. #elif __CUDA_ARCH__ >= CC_VOLTA
  4690. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  4691. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  4692. const int nwarps = NWARPS_Q5_0_AMPERE;
  4693. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4694. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4695. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4696. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4697. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  4698. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  4699. const int nwarps = NWARPS_Q5_0_PASCAL;
  4700. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4701. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4702. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4703. #else
  4704. GGML_UNUSED(vec_dot_q5_0_q8_1_mul_mat);
  4705. NO_DEVICE_CODE;
  4706. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4707. }
  4708. #define MMQ_X_Q5_1_RDNA2 64
  4709. #define MMQ_Y_Q5_1_RDNA2 128
  4710. #define NWARPS_Q5_1_RDNA2 8
  4711. #define MMQ_X_Q5_1_RDNA1 64
  4712. #define MMQ_Y_Q5_1_RDNA1 64
  4713. #define NWARPS_Q5_1_RDNA1 8
  4714. #if defined(CUDA_USE_TENSOR_CORES)
  4715. #define MMQ_X_Q5_1_AMPERE 4
  4716. #define MMQ_Y_Q5_1_AMPERE 32
  4717. #define NWARPS_Q5_1_AMPERE 4
  4718. #else
  4719. #define MMQ_X_Q5_1_AMPERE 128
  4720. #define MMQ_Y_Q5_1_AMPERE 64
  4721. #define NWARPS_Q5_1_AMPERE 4
  4722. #endif
  4723. #define MMQ_X_Q5_1_PASCAL 64
  4724. #define MMQ_Y_Q5_1_PASCAL 64
  4725. #define NWARPS_Q5_1_PASCAL 8
  4726. template <bool need_check> static __global__ void
  4727. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4728. #if defined(RDNA3) || defined(RDNA2)
  4729. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4730. #endif // defined(RDNA3) || defined(RDNA2)
  4731. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4732. mul_mat_q5_1(
  4733. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4734. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4735. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4736. #if defined(RDNA3) || defined(RDNA2)
  4737. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4738. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4739. const int nwarps = NWARPS_Q5_1_RDNA2;
  4740. #else
  4741. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4742. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4743. const int nwarps = NWARPS_Q5_1_RDNA1;
  4744. #endif // defined(RDNA3) || defined(RDNA2)
  4745. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4746. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4747. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4748. #elif __CUDA_ARCH__ >= CC_VOLTA
  4749. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4750. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4751. const int nwarps = NWARPS_Q5_1_AMPERE;
  4752. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4753. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4754. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4755. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4756. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4757. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4758. const int nwarps = NWARPS_Q5_1_PASCAL;
  4759. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4760. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4761. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4762. #else
  4763. GGML_UNUSED(vec_dot_q5_1_q8_1_mul_mat);
  4764. NO_DEVICE_CODE;
  4765. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4766. }
  4767. #define MMQ_X_Q8_0_RDNA2 64
  4768. #define MMQ_Y_Q8_0_RDNA2 128
  4769. #define NWARPS_Q8_0_RDNA2 8
  4770. #define MMQ_X_Q8_0_RDNA1 64
  4771. #define MMQ_Y_Q8_0_RDNA1 64
  4772. #define NWARPS_Q8_0_RDNA1 8
  4773. #if defined(CUDA_USE_TENSOR_CORES)
  4774. #define MMQ_X_Q8_0_AMPERE 4
  4775. #define MMQ_Y_Q8_0_AMPERE 32
  4776. #define NWARPS_Q8_0_AMPERE 4
  4777. #else
  4778. #define MMQ_X_Q8_0_AMPERE 128
  4779. #define MMQ_Y_Q8_0_AMPERE 64
  4780. #define NWARPS_Q8_0_AMPERE 4
  4781. #endif
  4782. #define MMQ_X_Q8_0_PASCAL 64
  4783. #define MMQ_Y_Q8_0_PASCAL 64
  4784. #define NWARPS_Q8_0_PASCAL 8
  4785. template <bool need_check> static __global__ void
  4786. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4787. #if defined(RDNA3) || defined(RDNA2)
  4788. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4789. #endif // defined(RDNA3) || defined(RDNA2)
  4790. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4791. mul_mat_q8_0(
  4792. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4793. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4794. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4795. #if defined(RDNA3) || defined(RDNA2)
  4796. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4797. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4798. const int nwarps = NWARPS_Q8_0_RDNA2;
  4799. #else
  4800. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4801. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4802. const int nwarps = NWARPS_Q8_0_RDNA1;
  4803. #endif // defined(RDNA3) || defined(RDNA2)
  4804. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4805. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4806. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4807. #elif __CUDA_ARCH__ >= CC_VOLTA
  4808. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4809. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4810. const int nwarps = NWARPS_Q8_0_AMPERE;
  4811. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4812. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4813. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4814. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4815. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4816. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4817. const int nwarps = NWARPS_Q8_0_PASCAL;
  4818. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4819. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4820. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4821. #else
  4822. GGML_UNUSED(vec_dot_q8_0_q8_1_mul_mat);
  4823. NO_DEVICE_CODE;
  4824. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4825. }
  4826. #define MMQ_X_Q2_K_RDNA2 64
  4827. #define MMQ_Y_Q2_K_RDNA2 128
  4828. #define NWARPS_Q2_K_RDNA2 8
  4829. #define MMQ_X_Q2_K_RDNA1 128
  4830. #define MMQ_Y_Q2_K_RDNA1 32
  4831. #define NWARPS_Q2_K_RDNA1 8
  4832. #if defined(CUDA_USE_TENSOR_CORES)
  4833. #define MMQ_X_Q2_K_AMPERE 4
  4834. #define MMQ_Y_Q2_K_AMPERE 32
  4835. #define NWARPS_Q2_K_AMPERE 4
  4836. #else
  4837. #define MMQ_X_Q2_K_AMPERE 64
  4838. #define MMQ_Y_Q2_K_AMPERE 128
  4839. #define NWARPS_Q2_K_AMPERE 4
  4840. #endif
  4841. #define MMQ_X_Q2_K_PASCAL 64
  4842. #define MMQ_Y_Q2_K_PASCAL 64
  4843. #define NWARPS_Q2_K_PASCAL 8
  4844. template <bool need_check> static __global__ void
  4845. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4846. #if defined(RDNA3) || defined(RDNA2)
  4847. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4848. #endif // defined(RDNA3) || defined(RDNA2)
  4849. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4850. mul_mat_q2_K(
  4851. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4852. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4853. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4854. #if defined(RDNA3) || defined(RDNA2)
  4855. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4856. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4857. const int nwarps = NWARPS_Q2_K_RDNA2;
  4858. #else
  4859. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4860. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4861. const int nwarps = NWARPS_Q2_K_RDNA1;
  4862. #endif // defined(RDNA3) || defined(RDNA2)
  4863. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4864. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4865. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4866. #elif __CUDA_ARCH__ >= CC_VOLTA
  4867. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4868. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4869. const int nwarps = NWARPS_Q2_K_AMPERE;
  4870. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4871. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4872. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4873. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4874. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4875. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4876. const int nwarps = NWARPS_Q2_K_PASCAL;
  4877. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4878. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4879. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4880. #else
  4881. GGML_UNUSED(vec_dot_q2_K_q8_1_mul_mat);
  4882. NO_DEVICE_CODE;
  4883. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4884. }
  4885. #define MMQ_X_Q3_K_RDNA2 128
  4886. #define MMQ_Y_Q3_K_RDNA2 64
  4887. #define NWARPS_Q3_K_RDNA2 8
  4888. #define MMQ_X_Q3_K_RDNA1 32
  4889. #define MMQ_Y_Q3_K_RDNA1 128
  4890. #define NWARPS_Q3_K_RDNA1 8
  4891. #if defined(CUDA_USE_TENSOR_CORES)
  4892. #define MMQ_X_Q3_K_AMPERE 4
  4893. #define MMQ_Y_Q3_K_AMPERE 32
  4894. #define NWARPS_Q3_K_AMPERE 4
  4895. #else
  4896. #define MMQ_X_Q3_K_AMPERE 128
  4897. #define MMQ_Y_Q3_K_AMPERE 128
  4898. #define NWARPS_Q3_K_AMPERE 4
  4899. #endif
  4900. #define MMQ_X_Q3_K_PASCAL 64
  4901. #define MMQ_Y_Q3_K_PASCAL 64
  4902. #define NWARPS_Q3_K_PASCAL 8
  4903. template <bool need_check> static __global__ void
  4904. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4905. #if defined(RDNA3) || defined(RDNA2)
  4906. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4907. #endif // defined(RDNA3) || defined(RDNA2)
  4908. #elif __CUDA_ARCH__ < CC_VOLTA
  4909. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4910. #endif // __CUDA_ARCH__ < CC_VOLTA
  4911. mul_mat_q3_K(
  4912. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4913. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4914. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4915. #if defined(RDNA3) || defined(RDNA2)
  4916. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4917. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4918. const int nwarps = NWARPS_Q3_K_RDNA2;
  4919. #else
  4920. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4921. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4922. const int nwarps = NWARPS_Q3_K_RDNA1;
  4923. #endif // defined(RDNA3) || defined(RDNA2)
  4924. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4925. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4926. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4927. #elif __CUDA_ARCH__ >= CC_VOLTA
  4928. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4929. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4930. const int nwarps = NWARPS_Q3_K_AMPERE;
  4931. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4932. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4933. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4934. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4935. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4936. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4937. const int nwarps = NWARPS_Q3_K_PASCAL;
  4938. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4939. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4940. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4941. #else
  4942. GGML_UNUSED(vec_dot_q3_K_q8_1_mul_mat);
  4943. NO_DEVICE_CODE;
  4944. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4945. }
  4946. #define MMQ_X_Q4_K_RDNA2 64
  4947. #define MMQ_Y_Q4_K_RDNA2 128
  4948. #define NWARPS_Q4_K_RDNA2 8
  4949. #define MMQ_X_Q4_K_RDNA1 32
  4950. #define MMQ_Y_Q4_K_RDNA1 64
  4951. #define NWARPS_Q4_K_RDNA1 8
  4952. #if defined(CUDA_USE_TENSOR_CORES)
  4953. #define MMQ_X_Q4_K_AMPERE 4
  4954. #define MMQ_Y_Q4_K_AMPERE 32
  4955. #define NWARPS_Q4_K_AMPERE 4
  4956. #else
  4957. #define MMQ_X_Q4_K_AMPERE 64
  4958. #define MMQ_Y_Q4_K_AMPERE 128
  4959. #define NWARPS_Q4_K_AMPERE 4
  4960. #endif
  4961. #define MMQ_X_Q4_K_PASCAL 64
  4962. #define MMQ_Y_Q4_K_PASCAL 64
  4963. #define NWARPS_Q4_K_PASCAL 8
  4964. template <bool need_check> static __global__ void
  4965. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4966. #if defined(RDNA3) || defined(RDNA2)
  4967. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4968. #endif // defined(RDNA3) || defined(RDNA2)
  4969. #elif __CUDA_ARCH__ < CC_VOLTA
  4970. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4971. #endif // __CUDA_ARCH__ < CC_VOLTA
  4972. mul_mat_q4_K(
  4973. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4974. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4975. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4976. #if defined(RDNA3) || defined(RDNA2)
  4977. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4978. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4979. const int nwarps = NWARPS_Q4_K_RDNA2;
  4980. #else
  4981. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4982. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4983. const int nwarps = NWARPS_Q4_K_RDNA1;
  4984. #endif // defined(RDNA3) || defined(RDNA2)
  4985. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4986. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4987. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4988. #elif __CUDA_ARCH__ >= CC_VOLTA
  4989. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4990. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4991. const int nwarps = NWARPS_Q4_K_AMPERE;
  4992. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4993. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4994. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4995. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4996. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4997. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4998. const int nwarps = NWARPS_Q4_K_PASCAL;
  4999. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  5000. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  5001. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5002. #else
  5003. GGML_UNUSED(vec_dot_q4_K_q8_1_mul_mat);
  5004. NO_DEVICE_CODE;
  5005. #endif // __CUDA_ARCH__ >= CC_VOLTA
  5006. }
  5007. #define MMQ_X_Q5_K_RDNA2 64
  5008. #define MMQ_Y_Q5_K_RDNA2 128
  5009. #define NWARPS_Q5_K_RDNA2 8
  5010. #define MMQ_X_Q5_K_RDNA1 32
  5011. #define MMQ_Y_Q5_K_RDNA1 64
  5012. #define NWARPS_Q5_K_RDNA1 8
  5013. #if defined(CUDA_USE_TENSOR_CORES)
  5014. #define MMQ_X_Q5_K_AMPERE 4
  5015. #define MMQ_Y_Q5_K_AMPERE 32
  5016. #define NWARPS_Q5_K_AMPERE 4
  5017. #else
  5018. #define MMQ_X_Q5_K_AMPERE 64
  5019. #define MMQ_Y_Q5_K_AMPERE 128
  5020. #define NWARPS_Q5_K_AMPERE 4
  5021. #endif
  5022. #define MMQ_X_Q5_K_PASCAL 64
  5023. #define MMQ_Y_Q5_K_PASCAL 64
  5024. #define NWARPS_Q5_K_PASCAL 8
  5025. template <bool need_check> static __global__ void
  5026. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5027. #if defined(RDNA3) || defined(RDNA2)
  5028. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  5029. #endif // defined(RDNA3) || defined(RDNA2)
  5030. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5031. mul_mat_q5_K(
  5032. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5033. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  5034. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5035. #if defined(RDNA3) || defined(RDNA2)
  5036. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  5037. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  5038. const int nwarps = NWARPS_Q5_K_RDNA2;
  5039. #else
  5040. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  5041. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  5042. const int nwarps = NWARPS_Q5_K_RDNA1;
  5043. #endif // defined(RDNA3) || defined(RDNA2)
  5044. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  5045. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  5046. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5047. #elif __CUDA_ARCH__ >= CC_VOLTA
  5048. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  5049. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  5050. const int nwarps = NWARPS_Q5_K_AMPERE;
  5051. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  5052. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  5053. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5054. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  5055. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  5056. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  5057. const int nwarps = NWARPS_Q5_K_PASCAL;
  5058. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  5059. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  5060. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5061. #else
  5062. GGML_UNUSED(vec_dot_q5_K_q8_1_mul_mat);
  5063. NO_DEVICE_CODE;
  5064. #endif // __CUDA_ARCH__ >= CC_VOLTA
  5065. }
  5066. #define MMQ_X_Q6_K_RDNA2 64
  5067. #define MMQ_Y_Q6_K_RDNA2 128
  5068. #define NWARPS_Q6_K_RDNA2 8
  5069. #define MMQ_X_Q6_K_RDNA1 32
  5070. #define MMQ_Y_Q6_K_RDNA1 64
  5071. #define NWARPS_Q6_K_RDNA1 8
  5072. #if defined(CUDA_USE_TENSOR_CORES)
  5073. #define MMQ_X_Q6_K_AMPERE 4
  5074. #define MMQ_Y_Q6_K_AMPERE 32
  5075. #define NWARPS_Q6_K_AMPERE 4
  5076. #else
  5077. #define MMQ_X_Q6_K_AMPERE 64
  5078. #define MMQ_Y_Q6_K_AMPERE 64
  5079. #define NWARPS_Q6_K_AMPERE 4
  5080. #endif
  5081. #define MMQ_X_Q6_K_PASCAL 64
  5082. #define MMQ_Y_Q6_K_PASCAL 64
  5083. #define NWARPS_Q6_K_PASCAL 8
  5084. template <bool need_check> static __global__ void
  5085. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5086. #if defined(RDNA3) || defined(RDNA2)
  5087. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  5088. #endif // defined(RDNA3) || defined(RDNA2)
  5089. #elif __CUDA_ARCH__ < CC_VOLTA
  5090. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  5091. #endif // __CUDA_ARCH__ < CC_VOLTA
  5092. mul_mat_q6_K(
  5093. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5094. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  5095. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5096. #if defined(RDNA3) || defined(RDNA2)
  5097. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  5098. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  5099. const int nwarps = NWARPS_Q6_K_RDNA2;
  5100. #else
  5101. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  5102. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  5103. const int nwarps = NWARPS_Q6_K_RDNA1;
  5104. #endif // defined(RDNA3) || defined(RDNA2)
  5105. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5106. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5107. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5108. #elif __CUDA_ARCH__ >= CC_VOLTA
  5109. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  5110. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  5111. const int nwarps = NWARPS_Q6_K_AMPERE;
  5112. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5113. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5114. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5115. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  5116. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  5117. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  5118. const int nwarps = NWARPS_Q6_K_PASCAL;
  5119. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  5120. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  5121. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5122. #else
  5123. GGML_UNUSED(vec_dot_q6_K_q8_1_mul_mat);
  5124. NO_DEVICE_CODE;
  5125. #endif // __CUDA_ARCH__ >= CC_VOLTA
  5126. }
  5127. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  5128. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  5129. // tell the compiler to use as many registers as it wants, see nwarps definition below
  5130. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  5131. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  5132. static __global__ void mul_mat_vec_q(
  5133. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5134. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  5135. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  5136. constexpr int nwarps = 1;
  5137. constexpr int rows_per_cuda_block = 1;
  5138. #else
  5139. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  5140. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  5141. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  5142. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  5143. const int row0 = rows_per_cuda_block*blockIdx.x;
  5144. const int blocks_per_row_x = ncols_x / qk;
  5145. const int blocks_per_col_y = nrows_y / QK8_1;
  5146. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  5147. // partial sum for each thread
  5148. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  5149. const block_q_t * x = (const block_q_t *) vx;
  5150. const block_q8_1 * y = (const block_q8_1 *) vy;
  5151. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  5152. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  5153. // x block quant index when casting the quants to int
  5154. const int kqs = vdr * (tid % (qi/vdr));
  5155. #pragma unroll
  5156. for (int j = 0; j < ncols_y; ++j) {
  5157. #pragma unroll
  5158. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5159. tmp[j][i] += vec_dot_q_cuda(
  5160. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  5161. }
  5162. }
  5163. }
  5164. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  5165. if (threadIdx.y > 0) {
  5166. #pragma unroll
  5167. for (int j = 0; j < ncols_y; ++j) {
  5168. #pragma unroll
  5169. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5170. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  5171. }
  5172. }
  5173. }
  5174. __syncthreads();
  5175. if (threadIdx.y > 0) {
  5176. return;
  5177. }
  5178. // sum up partial sums and write back result
  5179. #pragma unroll
  5180. for (int j = 0; j < ncols_y; ++j) {
  5181. #pragma unroll
  5182. for (int i = 0; i < rows_per_cuda_block; ++i) {
  5183. #pragma unroll
  5184. for (int l = 0; l < nwarps-1; ++l) {
  5185. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  5186. }
  5187. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  5188. }
  5189. if (threadIdx.x < rows_per_cuda_block) {
  5190. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  5191. }
  5192. }
  5193. }
  5194. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  5195. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  5196. // qk = quantized weights per x block
  5197. // qr = number of quantized weights per data value in x block
  5198. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  5199. if (row >= nrows) {
  5200. return;
  5201. }
  5202. const int tid = threadIdx.x;
  5203. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  5204. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  5205. const int y_offset = qr == 1 ? 1 : qk/2;
  5206. // partial sum for each thread
  5207. #ifdef GGML_CUDA_F16
  5208. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  5209. #else
  5210. float tmp = 0.0f;
  5211. #endif // GGML_CUDA_F16
  5212. for (int i = 0; i < ncols; i += iter_stride) {
  5213. const int col = i + vals_per_iter*tid;
  5214. const int ib = (row*ncols + col)/qk; // x block index
  5215. const int iqs = (col%qk)/qr; // x quant index
  5216. const int iybs = col - col%qk; // y block start index
  5217. // processing >2 values per i iter is faster for fast GPUs
  5218. #pragma unroll
  5219. for (int j = 0; j < vals_per_iter; j += 2) {
  5220. // process 2 vals per j iter
  5221. // dequantize
  5222. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  5223. dfloat2 v;
  5224. dequantize_kernel(vx, ib, iqs + j/qr, v);
  5225. // matrix multiplication
  5226. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  5227. #ifdef GGML_CUDA_F16
  5228. tmp += __hmul2(v, {
  5229. y[iybs + iqs + j/qr + 0],
  5230. y[iybs + iqs + j/qr + y_offset]
  5231. });
  5232. #else
  5233. tmp += v.x * y[iybs + iqs + j/qr + 0];
  5234. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  5235. #endif // GGML_CUDA_F16
  5236. }
  5237. }
  5238. // sum up partial sums and write back result
  5239. tmp = warp_reduce_sum(tmp);
  5240. if (tid == 0) {
  5241. #ifdef GGML_CUDA_F16
  5242. dst[row] = tmp.x + tmp.y;
  5243. #else
  5244. dst[row] = tmp;
  5245. #endif // GGML_CUDA_F16
  5246. }
  5247. }
  5248. static __global__ void mul_mat_p021_f16_f32(
  5249. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  5250. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  5251. const half * x = (const half *) vx;
  5252. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  5253. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  5254. const int channel_x = channel / (nchannels_y / nchannels_x);
  5255. const int nrows_y = ncols_x;
  5256. const int nrows_dst = nrows_x;
  5257. const int row_dst = row_x;
  5258. float tmp = 0.0f;
  5259. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  5260. const int col_x = col_x0 + threadIdx.x;
  5261. if (col_x >= ncols_x) {
  5262. break;
  5263. }
  5264. // x is transposed and permuted
  5265. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  5266. const float xi = __half2float(x[ix]);
  5267. const int row_y = col_x;
  5268. // y is not transposed but permuted
  5269. const int iy = channel*nrows_y + row_y;
  5270. tmp += xi * y[iy];
  5271. }
  5272. // dst is not transposed and not permuted
  5273. const int idst = channel*nrows_dst + row_dst;
  5274. // sum up partial sums and write back result
  5275. tmp = warp_reduce_sum(tmp);
  5276. if (threadIdx.x == 0) {
  5277. dst[idst] = tmp;
  5278. }
  5279. }
  5280. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  5281. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  5282. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  5283. const half * x = (const half *) vx;
  5284. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  5285. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  5286. const int channel_x = channel / channel_x_divisor;
  5287. const int nrows_y = ncols_x;
  5288. const int nrows_dst = nrows_x;
  5289. const int row_dst = row_x;
  5290. const int idst = channel*nrows_dst + row_dst;
  5291. float tmp = 0.0f;
  5292. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  5293. const int col_x = col_x0 + threadIdx.x;
  5294. if (col_x >= ncols_x) {
  5295. break;
  5296. }
  5297. const int row_y = col_x;
  5298. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  5299. const int iy = channel*nrows_y + row_y;
  5300. const float xi = __half2float(x[ix]);
  5301. tmp += xi * y[iy];
  5302. }
  5303. // sum up partial sums and write back result
  5304. tmp = warp_reduce_sum(tmp);
  5305. if (threadIdx.x == 0) {
  5306. dst[idst] = tmp;
  5307. }
  5308. }
  5309. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  5310. const float * xi = (const float *) cxi;
  5311. float * dsti = (float *) cdsti;
  5312. *dsti = *xi;
  5313. }
  5314. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  5315. const float * xi = (const float *) cxi;
  5316. half * dsti = (half *) cdsti;
  5317. *dsti = __float2half(*xi);
  5318. }
  5319. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  5320. const half * xi = (const half *) cxi;
  5321. half * dsti = (half *) cdsti;
  5322. *dsti = *xi;
  5323. }
  5324. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  5325. const half * xi = (const half *) cxi;
  5326. float * dsti = (float *) cdsti;
  5327. *dsti = *xi;
  5328. }
  5329. template <cpy_kernel_t cpy_1>
  5330. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  5331. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5332. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  5333. const int nb12, const int nb13) {
  5334. const int64_t i = blockDim.x*blockIdx.x + threadIdx.x;
  5335. if (i >= ne) {
  5336. return;
  5337. }
  5338. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  5339. // then combine those indices with the corresponding byte offsets to get the total offsets
  5340. const int64_t i03 = i/(ne00 * ne01 * ne02);
  5341. const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  5342. const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  5343. const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  5344. const int64_t x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  5345. const int64_t i13 = i/(ne10 * ne11 * ne12);
  5346. const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  5347. const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  5348. const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  5349. const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  5350. cpy_1(cx + x_offset, cdst + dst_offset);
  5351. }
  5352. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  5353. const float * xi = (const float *) cxi;
  5354. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  5355. float amax = 0.0f; // absolute max
  5356. for (int j = 0; j < QK8_0; j++) {
  5357. const float v = xi[j];
  5358. amax = fmaxf(amax, fabsf(v));
  5359. }
  5360. const float d = amax / ((1 << 7) - 1);
  5361. const float id = d ? 1.0f/d : 0.0f;
  5362. dsti->d = d;
  5363. for (int j = 0; j < QK8_0; ++j) {
  5364. const float x0 = xi[j]*id;
  5365. dsti->qs[j] = roundf(x0);
  5366. }
  5367. }
  5368. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  5369. const float * xi = (const float *) cxi;
  5370. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  5371. float amax = 0.0f;
  5372. float vmax = 0.0f;
  5373. for (int j = 0; j < QK4_0; ++j) {
  5374. const float v = xi[j];
  5375. if (amax < fabsf(v)) {
  5376. amax = fabsf(v);
  5377. vmax = v;
  5378. }
  5379. }
  5380. const float d = vmax / -8;
  5381. const float id = d ? 1.0f/d : 0.0f;
  5382. dsti->d = d;
  5383. for (int j = 0; j < QK4_0/2; ++j) {
  5384. const float x0 = xi[0 + j]*id;
  5385. const float x1 = xi[QK4_0/2 + j]*id;
  5386. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  5387. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  5388. dsti->qs[j] = xi0;
  5389. dsti->qs[j] |= xi1 << 4;
  5390. }
  5391. }
  5392. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  5393. const float * xi = (const float *) cxi;
  5394. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  5395. float vmin = FLT_MAX;
  5396. float vmax = -FLT_MAX;
  5397. for (int j = 0; j < QK4_1; ++j) {
  5398. const float v = xi[j];
  5399. if (v < vmin) vmin = v;
  5400. if (v > vmax) vmax = v;
  5401. }
  5402. const float d = (vmax - vmin) / ((1 << 4) - 1);
  5403. const float id = d ? 1.0f/d : 0.0f;
  5404. dsti->dm.x = d;
  5405. dsti->dm.y = vmin;
  5406. for (int j = 0; j < QK4_1/2; ++j) {
  5407. const float x0 = (xi[0 + j] - vmin)*id;
  5408. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  5409. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  5410. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  5411. dsti->qs[j] = xi0;
  5412. dsti->qs[j] |= xi1 << 4;
  5413. }
  5414. }
  5415. static __device__ void cpy_blck_f32_q5_0(const char * cxi, char * cdsti) {
  5416. const float * xi = (const float *) cxi;
  5417. block_q5_0 * dsti = (block_q5_0 *) cdsti;
  5418. float amax = 0.0f;
  5419. float vmax = 0.0f;
  5420. for (int j = 0; j < QK5_0; ++j) {
  5421. const float v = xi[j];
  5422. if (amax < fabsf(v)) {
  5423. amax = fabsf(v);
  5424. vmax = v;
  5425. }
  5426. }
  5427. const float d = vmax / -16;
  5428. const float id = d ? 1.0f/d : 0.0f;
  5429. dsti->d = d;
  5430. uint32_t qh = 0;
  5431. for (int j = 0; j < QK5_0/2; ++j) {
  5432. const float x0 = xi[0 + j]*id;
  5433. const float x1 = xi[QK5_0/2 + j]*id;
  5434. const uint8_t xi0 = min(31, (int8_t)(x0 + 16.5f));
  5435. const uint8_t xi1 = min(31, (int8_t)(x1 + 16.5f));
  5436. dsti->qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  5437. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  5438. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  5439. }
  5440. memcpy(dsti->qh, &qh, sizeof(qh));
  5441. }
  5442. static __device__ void cpy_blck_f32_q5_1(const char * cxi, char * cdsti) {
  5443. const float * xi = (const float *) cxi;
  5444. block_q5_1 * dsti = (block_q5_1 *) cdsti;
  5445. float min = xi[0];
  5446. float max = xi[0];
  5447. for (int j = 1; j < QK5_1; ++j) {
  5448. const float v = xi[j];
  5449. min = v < min ? v : min;
  5450. max = v > max ? v : max;
  5451. }
  5452. const float d = (max - min) / 31;
  5453. const float id = d ? 1.0f/d : 0.0f;
  5454. dsti->dm.x = d;
  5455. dsti->dm.y = min;
  5456. uint32_t qh = 0;
  5457. for (int j = 0; j < QK5_1/2; ++j) {
  5458. const float x0 = (xi[0 + j] - min)*id;
  5459. const float x1 = (xi[QK5_1/2 + j] - min)*id;
  5460. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  5461. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  5462. dsti->qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  5463. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  5464. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2);
  5465. }
  5466. memcpy(dsti->qh, &qh, sizeof(qh));
  5467. }
  5468. static __device__ __forceinline__ int best_index_int8(int n, const int8_t * val, float x) {
  5469. if (x <= val[0]) return 0;
  5470. if (x >= val[n-1]) return n-1;
  5471. int ml = 0, mu = n-1;
  5472. while (mu-ml > 1) {
  5473. int mav = (ml+mu)/2;
  5474. if (x < val[mav]) mu = mav; else ml = mav;
  5475. }
  5476. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  5477. }
  5478. static __device__ void cpy_blck_f32_iq4_nl(const char * cxi, char * cdsti) {
  5479. const float * xi = (const float *) cxi;
  5480. block_iq4_nl * dsti = (block_iq4_nl *) cdsti;
  5481. float amax = 0.0f;
  5482. float vmax = 0.0f;
  5483. for (int j = 0; j < QK4_NL; ++j) {
  5484. const float v = xi[j];
  5485. if (amax < fabsf(v)) {
  5486. amax = fabsf(v);
  5487. vmax = v;
  5488. }
  5489. }
  5490. float d = vmax / kvalues_iq4nl[0];
  5491. const float id = d ? 1.0f/d : 0.0f;
  5492. float sumqx = 0, sumq2 = 0;
  5493. for (int j = 0; j < QK4_NL/2; ++j) {
  5494. const float x0 = xi[0 + j]*id;
  5495. const float x1 = xi[QK4_NL/2 + j]*id;
  5496. const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl, x0);
  5497. const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl, x1);
  5498. dsti->qs[j] = xi0 | (xi1 << 4);
  5499. const float v0 = kvalues_iq4nl[xi0];
  5500. const float v1 = kvalues_iq4nl[xi1];
  5501. const float w0 = xi[0 + j]*xi[0 + j];
  5502. const float w1 = xi[QK4_NL/2 + j]*xi[QK4_NL/2 + j];
  5503. sumqx += w0*v0*xi[j] + w1*v1*xi[QK4_NL/2 + j];
  5504. sumq2 += w0*v0*v0 + w1*v1*v1;
  5505. }
  5506. dsti->d = sumq2 > 0 ? sumqx/sumq2 : d;
  5507. }
  5508. template <cpy_kernel_t cpy_blck, int qk>
  5509. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  5510. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5511. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  5512. const int nb12, const int nb13) {
  5513. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  5514. if (i >= ne) {
  5515. return;
  5516. }
  5517. const int i03 = i/(ne00 * ne01 * ne02);
  5518. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  5519. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  5520. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  5521. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  5522. const int i13 = i/(ne10 * ne11 * ne12);
  5523. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  5524. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  5525. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  5526. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  5527. cpy_blck(cx + x_offset, cdst + dst_offset);
  5528. }
  5529. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  5530. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  5531. return 1.0f - min(1.0f, max(0.0f, y));
  5532. }
  5533. struct rope_corr_dims {
  5534. float v[4];
  5535. };
  5536. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  5537. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  5538. static __device__ void rope_yarn(
  5539. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  5540. float * cos_theta, float * sin_theta
  5541. ) {
  5542. // Get n-d rotational scaling corrected for extrapolation
  5543. float theta_interp = freq_scale * theta_extrap;
  5544. float theta = theta_interp;
  5545. if (ext_factor != 0.0f) {
  5546. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  5547. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  5548. // Get n-d magnitude scaling corrected for interpolation
  5549. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  5550. }
  5551. *cos_theta = cosf(theta) * mscale;
  5552. *sin_theta = sinf(theta) * mscale;
  5553. }
  5554. // rope == RoPE == rotary positional embedding
  5555. template<typename T, bool has_pos>
  5556. static __global__ void rope(
  5557. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  5558. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  5559. ) {
  5560. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  5561. if (col >= ncols) {
  5562. return;
  5563. }
  5564. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5565. const int i = row*ncols + col;
  5566. const int i2 = row/p_delta_rows;
  5567. const int p = has_pos ? pos[i2] : 0;
  5568. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  5569. float cos_theta, sin_theta;
  5570. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  5571. const float x0 = x[i + 0];
  5572. const float x1 = x[i + 1];
  5573. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5574. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  5575. }
  5576. template<typename T, bool has_pos>
  5577. static __global__ void rope_neox(
  5578. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  5579. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  5580. ) {
  5581. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  5582. if (col >= ncols) {
  5583. return;
  5584. }
  5585. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5586. const int ib = col / n_dims;
  5587. const int ic = col % n_dims;
  5588. if (ib > 0) {
  5589. const int i = row*ncols + ib*n_dims + ic;
  5590. dst[i + 0] = x[i + 0];
  5591. dst[i + 1] = x[i + 1];
  5592. return;
  5593. }
  5594. const int i = row*ncols + ib*n_dims + ic/2;
  5595. const int i2 = row/p_delta_rows;
  5596. float cur_rot = inv_ndims * ic - ib;
  5597. const int p = has_pos ? pos[i2] : 0;
  5598. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  5599. float cos_theta, sin_theta;
  5600. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  5601. const float x0 = x[i + 0];
  5602. const float x1 = x[i + n_dims/2];
  5603. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5604. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  5605. }
  5606. static __global__ void rope_glm_f32(
  5607. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  5608. int n_ctx
  5609. ) {
  5610. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  5611. const int half_n_dims = ncols/4;
  5612. if (col >= half_n_dims) {
  5613. return;
  5614. }
  5615. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  5616. const int i = row*ncols + col;
  5617. const int i2 = row/p_delta_rows;
  5618. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  5619. // FIXME: this is likely wrong
  5620. const int p = pos != nullptr ? pos[i2] : 0;
  5621. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  5622. const float sin_theta = sinf(theta);
  5623. const float cos_theta = cosf(theta);
  5624. const float x0 = x[i + 0];
  5625. const float x1 = x[i + half_n_dims];
  5626. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  5627. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  5628. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  5629. const float sin_block_theta = sinf(block_theta);
  5630. const float cos_block_theta = cosf(block_theta);
  5631. const float x2 = x[i + half_n_dims * 2];
  5632. const float x3 = x[i + half_n_dims * 3];
  5633. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  5634. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  5635. }
  5636. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  5637. const int n_heads_log2_floor, const float m0, const float m1) {
  5638. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  5639. if (col >= ncols) {
  5640. return;
  5641. }
  5642. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  5643. const int i = row*ncols + col;
  5644. const int k = row/k_rows;
  5645. float m_k;
  5646. if (k < n_heads_log2_floor) {
  5647. m_k = powf(m0, k + 1);
  5648. } else {
  5649. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  5650. }
  5651. dst[i] = col * m_k + x[i];
  5652. }
  5653. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  5654. const int row = blockIdx.x;
  5655. const int col = threadIdx.x;
  5656. float sum = 0.0f;
  5657. for (int i = col; i < ncols; i += blockDim.x) {
  5658. sum += x[row * ncols + i];
  5659. }
  5660. sum = warp_reduce_sum(sum);
  5661. if (col == 0) {
  5662. dst[row] = sum;
  5663. }
  5664. }
  5665. template<typename T>
  5666. static inline __device__ void ggml_cuda_swap(T & a, T & b) {
  5667. T tmp = a;
  5668. a = b;
  5669. b = tmp;
  5670. }
  5671. template<ggml_sort_order order>
  5672. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  5673. // bitonic sort
  5674. int col = threadIdx.x;
  5675. int row = blockIdx.y;
  5676. if (col >= ncols) return;
  5677. const float * x_row = x + row * ncols;
  5678. int * dst_row = dst + row * ncols;
  5679. // initialize indices
  5680. if (col < ncols) {
  5681. dst_row[col] = col;
  5682. }
  5683. __syncthreads();
  5684. for (int k = 2; k <= ncols; k *= 2) {
  5685. for (int j = k / 2; j > 0; j /= 2) {
  5686. int ixj = col ^ j;
  5687. if (ixj > col) {
  5688. if ((col & k) == 0) {
  5689. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  5690. ggml_cuda_swap(dst_row[col], dst_row[ixj]);
  5691. }
  5692. } else {
  5693. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  5694. ggml_cuda_swap(dst_row[col], dst_row[ixj]);
  5695. }
  5696. }
  5697. }
  5698. __syncthreads();
  5699. }
  5700. }
  5701. }
  5702. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  5703. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  5704. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5705. if (col >= ncols) {
  5706. return;
  5707. }
  5708. const int i = row*ncols + col;
  5709. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  5710. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  5711. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  5712. }
  5713. template <bool vals_smem, int ncols_template, int block_size_template>
  5714. 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) {
  5715. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  5716. const int tid = threadIdx.x;
  5717. const int rowx = blockIdx.x;
  5718. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  5719. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  5720. const int warp_id = threadIdx.x / WARP_SIZE;
  5721. const int lane_id = threadIdx.x % WARP_SIZE;
  5722. float slope = 0.0f;
  5723. // ALiBi
  5724. if (max_bias > 0.0f) {
  5725. const int h = rowx/nrows_y; // head index
  5726. const float base = h < n_head_log2 ? m0 : m1;
  5727. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  5728. slope = powf(base, exp);
  5729. }
  5730. extern __shared__ float data_soft_max_f32[];
  5731. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  5732. // shared memory buffer to cache values between iterations:
  5733. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  5734. float max_val = -INFINITY;
  5735. #pragma unroll
  5736. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5737. const int col = col0 + tid;
  5738. if (ncols_template == 0 && col >= ncols) {
  5739. break;
  5740. }
  5741. const int ix = rowx*ncols + col;
  5742. const int iy = rowy*ncols + col;
  5743. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  5744. vals[col] = val;
  5745. max_val = max(max_val, val);
  5746. }
  5747. // find the max value in the block
  5748. max_val = warp_reduce_max(max_val);
  5749. if (block_size > WARP_SIZE) {
  5750. if (warp_id == 0) {
  5751. buf_iw[lane_id] = -INFINITY;
  5752. }
  5753. __syncthreads();
  5754. if (lane_id == 0) {
  5755. buf_iw[warp_id] = max_val;
  5756. }
  5757. __syncthreads();
  5758. max_val = buf_iw[lane_id];
  5759. max_val = warp_reduce_max(max_val);
  5760. }
  5761. float tmp = 0.0f; // partial sum
  5762. #pragma unroll
  5763. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5764. const int col = col0 + tid;
  5765. if (ncols_template == 0 && col >= ncols) {
  5766. break;
  5767. }
  5768. const float val = expf(vals[col] - max_val);
  5769. tmp += val;
  5770. vals[col] = val;
  5771. }
  5772. // find the sum of exps in the block
  5773. tmp = warp_reduce_sum(tmp);
  5774. if (block_size > WARP_SIZE) {
  5775. __syncthreads();
  5776. if (warp_id == 0) {
  5777. buf_iw[lane_id] = 0.0f;
  5778. }
  5779. __syncthreads();
  5780. if (lane_id == 0) {
  5781. buf_iw[warp_id] = tmp;
  5782. }
  5783. __syncthreads();
  5784. tmp = buf_iw[lane_id];
  5785. tmp = warp_reduce_sum(tmp);
  5786. }
  5787. const float inv_sum = 1.0f / tmp;
  5788. #pragma unroll
  5789. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5790. const int col = col0 + tid;
  5791. if (ncols_template == 0 && col >= ncols) {
  5792. return;
  5793. }
  5794. const int idst = rowx*ncols + col;
  5795. dst[idst] = vals[col] * inv_sum;
  5796. }
  5797. }
  5798. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5799. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5800. if (i >= k) {
  5801. return;
  5802. }
  5803. dst[i] = scale * x[i];
  5804. }
  5805. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5806. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5807. if (i >= k) {
  5808. return;
  5809. }
  5810. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5811. }
  5812. template <typename T>
  5813. static __global__ void im2col_kernel(
  5814. const float * x, T * dst, int64_t batch_offset,
  5815. int64_t offset_delta, int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH, int64_t pelements, int64_t CHW,
  5816. int s0, int s1, int p0, int p1, int d0, int d1) {
  5817. const int64_t i = threadIdx.x + blockIdx.x * blockDim.x;
  5818. if (i >= pelements) {
  5819. return;
  5820. }
  5821. const int64_t ksize = OW * (KH > 1 ? KW : 1);
  5822. const int64_t kx = i / ksize;
  5823. const int64_t kd = kx * ksize;
  5824. const int64_t ky = (i - kd) / OW;
  5825. const int64_t ix = i % OW;
  5826. const int64_t oh = blockIdx.y;
  5827. const int64_t batch = blockIdx.z / IC;
  5828. const int64_t ic = blockIdx.z % IC;
  5829. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5830. const int64_t iih = oh * s1 + ky * d1 - p1;
  5831. const int64_t offset_dst =
  5832. ((batch * OH + oh) * OW + ix) * CHW +
  5833. (ic * (KW * KH) + ky * KW + kx);
  5834. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5835. dst[offset_dst] = 0.0f;
  5836. } else {
  5837. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5838. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5839. }
  5840. }
  5841. template <typename Ti, typename To>
  5842. static __global__ void pool2d_nchw_kernel(
  5843. const int ih, const int iw, const int oh, const int ow,
  5844. const int kh, const int kw, const int sh, const int sw,
  5845. const int ph, const int pw, const int parallel_elements,
  5846. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5847. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5848. if (idx >= parallel_elements) {
  5849. return;
  5850. }
  5851. const int I_HW = ih * iw;
  5852. const int O_HW = oh * ow;
  5853. const int nc = idx / O_HW;
  5854. const int cur_oh = idx % O_HW / ow;
  5855. const int cur_ow = idx % O_HW % ow;
  5856. const Ti* i_ptr = src + nc * I_HW;
  5857. To* o_ptr = dst + nc * O_HW;
  5858. const int start_h = cur_oh * sh - ph;
  5859. const int bh = max(0, start_h);
  5860. const int eh = min(ih, start_h + kh);
  5861. const int start_w = cur_ow * sw - pw;
  5862. const int bw = max(0, start_w);
  5863. const int ew = min(iw, start_w + kw);
  5864. const To scale = 1. / (kh * kw);
  5865. To res = 0;
  5866. switch (op) {
  5867. case GGML_OP_POOL_AVG: res = 0; break;
  5868. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5869. default: assert(false);
  5870. }
  5871. for (int i = bh; i < eh; i += 1) {
  5872. for (int j = bw; j < ew; j += 1) {
  5873. #if __CUDA_ARCH__ >= 350
  5874. Ti cur = __ldg(i_ptr + i * iw + j);
  5875. #else
  5876. Ti cur = i_ptr[i * iw + j];
  5877. #endif
  5878. switch (op) {
  5879. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5880. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5881. default: assert(false);
  5882. }
  5883. }
  5884. }
  5885. o_ptr[cur_oh * ow + cur_ow] = res;
  5886. }
  5887. template<int qk, int qr, dequantize_kernel_t dq>
  5888. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5889. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5890. GGML_TENSOR_BINARY_OP_LOCALS
  5891. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5892. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5893. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5894. // strides in elements
  5895. //const size_t s0 = nb0 / ggml_element_size(dst);
  5896. const size_t s1 = nb1 / ggml_element_size(dst);
  5897. const size_t s2 = nb2 / ggml_element_size(dst);
  5898. const size_t s3 = nb3 / ggml_element_size(dst);
  5899. const size_t s10 = nb10 / ggml_element_size(src1);
  5900. const size_t s11 = nb11 / ggml_element_size(src1);
  5901. const size_t s12 = nb12 / ggml_element_size(src1);
  5902. //const size_t s13 = nb13 / ggml_element_size(src1);
  5903. GGML_ASSERT(ne00 % 2 == 0);
  5904. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5905. src0_dd, src1_dd, dst_dd,
  5906. ne00, /*ne01, ne02, ne03,*/
  5907. /*ne10, ne11,*/ ne12, /*ne13,*/
  5908. /* s0,*/ s1, s2, s3,
  5909. /* nb00,*/ nb01, nb02, nb03,
  5910. s10, s11, s12/*, s13*/);
  5911. GGML_UNUSED(dst);
  5912. }
  5913. template<typename src0_t>
  5914. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5915. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5916. GGML_TENSOR_BINARY_OP_LOCALS
  5917. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5918. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5919. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5920. // strides in elements
  5921. //const size_t s0 = nb0 / ggml_element_size(dst);
  5922. const size_t s1 = nb1 / ggml_element_size(dst);
  5923. const size_t s2 = nb2 / ggml_element_size(dst);
  5924. const size_t s3 = nb3 / ggml_element_size(dst);
  5925. const size_t s10 = nb10 / ggml_element_size(src1);
  5926. const size_t s11 = nb11 / ggml_element_size(src1);
  5927. const size_t s12 = nb12 / ggml_element_size(src1);
  5928. //const size_t s13 = nb13 / ggml_element_size(src1);
  5929. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5930. src0_dd, src1_dd, dst_dd,
  5931. ne00, /*ne01, ne02, ne03,*/
  5932. /*ne10, ne11,*/ ne12, /*ne13,*/
  5933. /* s0,*/ s1, s2, s3,
  5934. /* nb00,*/ nb01, nb02, nb03,
  5935. s10, s11, s12/*, s13*/);
  5936. GGML_UNUSED(dst);
  5937. }
  5938. template<float (*bin_op)(const float, const float)>
  5939. struct bin_bcast_cuda {
  5940. template<typename src0_t, typename src1_t, typename dst_t>
  5941. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5942. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5943. cudaStream_t stream) {
  5944. GGML_TENSOR_BINARY_OP_LOCALS
  5945. int nr0 = ne10/ne0;
  5946. int nr1 = ne11/ne1;
  5947. int nr2 = ne12/ne2;
  5948. int nr3 = ne13/ne3;
  5949. int nr[4] = { nr0, nr1, nr2, nr3 };
  5950. // collapse dimensions until first broadcast dimension
  5951. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5952. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5953. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5954. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5955. auto collapse = [](int64_t cne[]) {
  5956. cne[0] *= cne[1];
  5957. cne[1] = cne[2];
  5958. cne[2] = cne[3];
  5959. cne[3] = 1;
  5960. };
  5961. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5962. cnb[1] *= cne[1];
  5963. cnb[2] *= cne[2];
  5964. cnb[3] *= cne[3];
  5965. };
  5966. for (int i = 0; i < 4; i++) {
  5967. if (nr[i] != 1) {
  5968. break;
  5969. }
  5970. if (i > 0) {
  5971. collapse_nb(cnb0, cne0);
  5972. collapse_nb(cnb1, cne1);
  5973. collapse(cne0);
  5974. collapse(cne1);
  5975. }
  5976. }
  5977. {
  5978. int64_t ne0 = cne0[0];
  5979. int64_t ne1 = cne0[1];
  5980. int64_t ne2 = cne0[2];
  5981. int64_t ne3 = cne0[3];
  5982. int64_t ne10 = cne1[0];
  5983. int64_t ne11 = cne1[1];
  5984. int64_t ne12 = cne1[2];
  5985. int64_t ne13 = cne1[3];
  5986. size_t nb0 = cnb0[0];
  5987. size_t nb1 = cnb0[1];
  5988. size_t nb2 = cnb0[2];
  5989. size_t nb3 = cnb0[3];
  5990. size_t nb10 = cnb1[0];
  5991. size_t nb11 = cnb1[1];
  5992. size_t nb12 = cnb1[2];
  5993. size_t nb13 = cnb1[3];
  5994. size_t s0 = nb0 / sizeof(dst_t);
  5995. size_t s1 = nb1 / sizeof(dst_t);
  5996. size_t s2 = nb2 / sizeof(dst_t);
  5997. size_t s3 = nb3 / sizeof(dst_t);
  5998. size_t s10 = nb10 / sizeof(src1_t);
  5999. size_t s11 = nb11 / sizeof(src1_t);
  6000. size_t s12 = nb12 / sizeof(src1_t);
  6001. size_t s13 = nb13 / sizeof(src1_t);
  6002. GGML_ASSERT(s0 == 1);
  6003. GGML_ASSERT(s10 == 1);
  6004. const int block_size = 128;
  6005. int64_t hne0 = std::max(ne0/2LL, 1LL);
  6006. dim3 block_dims;
  6007. block_dims.x = std::min<unsigned int>(hne0, block_size);
  6008. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  6009. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  6010. dim3 block_nums(
  6011. (hne0 + block_dims.x - 1) / block_dims.x,
  6012. (ne1 + block_dims.y - 1) / block_dims.y,
  6013. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  6014. );
  6015. if (block_nums.z > 65535) {
  6016. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  6017. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  6018. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  6019. src0_dd, src1_dd, dst_dd,
  6020. ne0, ne1, ne2, ne3,
  6021. ne10, ne11, ne12, ne13,
  6022. /* s0, */ s1, s2, s3,
  6023. /* s10, */ s11, s12, s13);
  6024. } else {
  6025. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  6026. src0_dd, src1_dd, dst_dd,
  6027. ne0, ne1, ne2, ne3,
  6028. ne10, ne11, ne12, ne13,
  6029. /* s0, */ s1, s2, s3,
  6030. /* s10, */ s11, s12, s13);
  6031. }
  6032. }
  6033. }
  6034. };
  6035. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  6036. const int ne10, const int ne11, const int ne12,
  6037. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  6038. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  6039. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  6040. }
  6041. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6042. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  6043. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6044. }
  6045. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6046. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  6047. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6048. }
  6049. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6050. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  6051. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6052. }
  6053. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6054. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  6055. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6056. }
  6057. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6058. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  6059. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6060. }
  6061. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6062. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  6063. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6064. }
  6065. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6066. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  6067. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6068. }
  6069. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  6070. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  6071. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  6072. }
  6073. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  6074. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  6075. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  6076. }
  6077. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  6078. GGML_ASSERT(ncols % WARP_SIZE == 0);
  6079. if (ncols < 1024) {
  6080. const dim3 block_dims(WARP_SIZE, 1, 1);
  6081. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  6082. } else {
  6083. const dim3 block_dims(1024, 1, 1);
  6084. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  6085. }
  6086. }
  6087. 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) {
  6088. static const float eps = 1e-6f;
  6089. if (group_size < 1024) {
  6090. const dim3 block_dims(WARP_SIZE, 1, 1);
  6091. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  6092. } else {
  6093. const dim3 block_dims(1024, 1, 1);
  6094. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  6095. }
  6096. }
  6097. 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) {
  6098. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  6099. dim3 gridDim(num_blocks, ne1, ne2);
  6100. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  6101. }
  6102. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int ne03,
  6103. const int scale_factor, cudaStream_t stream) {
  6104. int ne0 = (ne00 * scale_factor);
  6105. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  6106. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02*ne03);
  6107. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  6108. }
  6109. static void pad_f32_cuda(const float * x, float * dst,
  6110. const int ne00, const int ne01, const int ne02, const int ne03,
  6111. const int ne0, const int ne1, const int ne2, const int ne3, cudaStream_t stream) {
  6112. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  6113. dim3 gridDim(num_blocks, ne1, ne2*ne3);
  6114. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02, ne03);
  6115. }
  6116. static void arange_f32_cuda(float * dst, const int ne0, const float start, const float step, cudaStream_t stream) {
  6117. int num_blocks = (ne0 + CUDA_ARANGE_BLOCK_SIZE - 1) / CUDA_ARANGE_BLOCK_SIZE;
  6118. arange_f32<<<num_blocks, CUDA_ARANGE_BLOCK_SIZE, 0, stream>>>(dst, ne0, start, step);
  6119. }
  6120. static void timestep_embedding_f32_cuda(const float * x, float * dst, const int ne00, const int nb1,
  6121. const int dim, const int max_period, cudaStream_t stream) {
  6122. int half_ceil = (dim + 1) / 2;
  6123. int num_blocks = (half_ceil + CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE - 1) / CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE;
  6124. dim3 gridDim(num_blocks, ne00, 1);
  6125. timestep_embedding_f32<<<gridDim, CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE, 0, stream>>>(x, dst, nb1, dim, max_period);
  6126. }
  6127. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  6128. GGML_ASSERT(ncols % WARP_SIZE == 0);
  6129. if (ncols < 1024) {
  6130. const dim3 block_dims(WARP_SIZE, 1, 1);
  6131. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  6132. } else {
  6133. const dim3 block_dims(1024, 1, 1);
  6134. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  6135. }
  6136. }
  6137. 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) {
  6138. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  6139. const dim3 num_blocks(block_num_x, ky, 1);
  6140. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  6141. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  6142. }
  6143. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  6144. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  6145. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  6146. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  6147. }
  6148. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  6149. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  6150. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  6151. const bool need_check = false;
  6152. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  6153. } else {
  6154. const bool need_check = true;
  6155. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  6156. }
  6157. }
  6158. template<typename dst_t>
  6159. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6160. const int nb = k / QK_K;
  6161. #if QK_K == 256
  6162. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  6163. #else
  6164. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  6165. #endif
  6166. }
  6167. template<typename dst_t>
  6168. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6169. const int nb = k / QK_K;
  6170. #if QK_K == 256
  6171. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  6172. #else
  6173. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  6174. #endif
  6175. }
  6176. template<typename dst_t>
  6177. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6178. const int nb32 = k / 32;
  6179. const int nb = (k + 255) / 256;
  6180. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  6181. }
  6182. template<typename dst_t>
  6183. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6184. const int nb32 = k / 32;
  6185. const int nb = (k + 255) / 256;
  6186. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  6187. }
  6188. template<typename dst_t>
  6189. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6190. const int nb = k / QK_K;
  6191. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  6192. }
  6193. template<typename dst_t>
  6194. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6195. const int nb = k / QK_K;
  6196. #if QK_K == 256
  6197. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  6198. #else
  6199. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  6200. #endif
  6201. }
  6202. template<typename dst_t>
  6203. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6204. const int nb = k / QK_K;
  6205. #if QK_K == 256
  6206. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  6207. #else
  6208. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  6209. #endif
  6210. }
  6211. template<typename dst_t>
  6212. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6213. const int nb = k / QK_K;
  6214. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  6215. }
  6216. template<typename dst_t>
  6217. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6218. const int nb = k / QK_K;
  6219. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  6220. }
  6221. template<typename dst_t>
  6222. static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6223. const int nb = k / QK_K;
  6224. dequantize_block_iq2_s<<<nb, 32, 0, stream>>>(vx, y);
  6225. }
  6226. template<typename dst_t>
  6227. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6228. const int nb = k / QK_K;
  6229. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  6230. }
  6231. template<typename dst_t>
  6232. static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6233. const int nb = k / QK_K;
  6234. dequantize_block_iq3_s<<<nb, 32, 0, stream>>>(vx, y);
  6235. }
  6236. template<typename dst_t>
  6237. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6238. const int nb = k / QK_K;
  6239. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  6240. }
  6241. template<typename dst_t>
  6242. static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6243. const int nb = (k + QK_K - 1) / QK_K;
  6244. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  6245. }
  6246. template<typename dst_t>
  6247. static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  6248. const int nb = (k + QK_K - 1) / QK_K;
  6249. #if QK_K == 64
  6250. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  6251. #else
  6252. dequantize_block_iq4_xs<<<nb, 32, 0, stream>>>(vx, y);
  6253. #endif
  6254. }
  6255. template <typename src_t, typename dst_t>
  6256. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  6257. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  6258. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  6259. }
  6260. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  6261. int id;
  6262. switch (type) {
  6263. case GGML_TYPE_Q4_0:
  6264. return dequantize_row_q4_0_cuda;
  6265. case GGML_TYPE_Q4_1:
  6266. return dequantize_row_q4_1_cuda;
  6267. case GGML_TYPE_Q5_0:
  6268. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  6269. case GGML_TYPE_Q5_1:
  6270. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  6271. case GGML_TYPE_Q8_0:
  6272. CUDA_CHECK(cudaGetDevice(&id));
  6273. if (get_cuda_global_info().devices[id].cc >= CC_PASCAL) {
  6274. return dequantize_block_q8_0_f16_cuda;
  6275. }
  6276. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  6277. case GGML_TYPE_Q2_K:
  6278. return dequantize_row_q2_K_cuda;
  6279. case GGML_TYPE_Q3_K:
  6280. return dequantize_row_q3_K_cuda;
  6281. case GGML_TYPE_Q4_K:
  6282. return dequantize_row_q4_K_cuda;
  6283. case GGML_TYPE_Q5_K:
  6284. return dequantize_row_q5_K_cuda;
  6285. case GGML_TYPE_Q6_K:
  6286. return dequantize_row_q6_K_cuda;
  6287. case GGML_TYPE_IQ2_XXS:
  6288. return dequantize_row_iq2_xxs_cuda;
  6289. case GGML_TYPE_IQ2_XS:
  6290. return dequantize_row_iq2_xs_cuda;
  6291. case GGML_TYPE_IQ2_S:
  6292. return dequantize_row_iq2_s_cuda;
  6293. case GGML_TYPE_IQ3_XXS:
  6294. return dequantize_row_iq3_xxs_cuda;
  6295. case GGML_TYPE_IQ1_S:
  6296. return dequantize_row_iq1_s_cuda;
  6297. case GGML_TYPE_IQ4_NL:
  6298. return dequantize_row_iq4_nl_cuda;
  6299. case GGML_TYPE_IQ4_XS:
  6300. return dequantize_row_iq4_xs_cuda;
  6301. case GGML_TYPE_IQ3_S:
  6302. return dequantize_row_iq3_s_cuda;
  6303. case GGML_TYPE_F32:
  6304. return convert_unary_cuda<float>;
  6305. default:
  6306. return nullptr;
  6307. }
  6308. }
  6309. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  6310. switch (type) {
  6311. case GGML_TYPE_Q4_0:
  6312. return dequantize_row_q4_0_cuda;
  6313. case GGML_TYPE_Q4_1:
  6314. return dequantize_row_q4_1_cuda;
  6315. case GGML_TYPE_Q5_0:
  6316. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  6317. case GGML_TYPE_Q5_1:
  6318. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  6319. case GGML_TYPE_Q8_0:
  6320. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  6321. case GGML_TYPE_Q2_K:
  6322. return dequantize_row_q2_K_cuda;
  6323. case GGML_TYPE_Q3_K:
  6324. return dequantize_row_q3_K_cuda;
  6325. case GGML_TYPE_Q4_K:
  6326. return dequantize_row_q4_K_cuda;
  6327. case GGML_TYPE_Q5_K:
  6328. return dequantize_row_q5_K_cuda;
  6329. case GGML_TYPE_Q6_K:
  6330. return dequantize_row_q6_K_cuda;
  6331. case GGML_TYPE_IQ2_XXS:
  6332. return dequantize_row_iq2_xxs_cuda;
  6333. case GGML_TYPE_IQ2_XS:
  6334. return dequantize_row_iq2_xs_cuda;
  6335. case GGML_TYPE_IQ2_S:
  6336. return dequantize_row_iq2_s_cuda;
  6337. case GGML_TYPE_IQ3_XXS:
  6338. return dequantize_row_iq3_xxs_cuda;
  6339. case GGML_TYPE_IQ1_S:
  6340. return dequantize_row_iq1_s_cuda;
  6341. case GGML_TYPE_IQ4_NL:
  6342. return dequantize_row_iq4_nl_cuda;
  6343. case GGML_TYPE_IQ4_XS:
  6344. return dequantize_row_iq4_xs_cuda;
  6345. case GGML_TYPE_IQ3_S:
  6346. return dequantize_row_iq3_s_cuda;
  6347. case GGML_TYPE_F16:
  6348. return convert_unary_cuda<half>;
  6349. default:
  6350. return nullptr;
  6351. }
  6352. }
  6353. 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) {
  6354. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6355. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6356. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  6357. const dim3 block_nums(block_num_y, 1, 1);
  6358. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6359. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  6360. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6361. }
  6362. 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) {
  6363. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6364. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6365. const dim3 block_nums(block_num_y, 1, 1);
  6366. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6367. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  6368. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6369. }
  6370. 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) {
  6371. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6372. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6373. const dim3 block_nums(block_num_y, 1, 1);
  6374. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6375. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  6376. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6377. }
  6378. 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) {
  6379. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6380. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6381. const dim3 block_nums(block_num_y, 1, 1);
  6382. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6383. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  6384. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6385. }
  6386. 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) {
  6387. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6388. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6389. const dim3 block_nums(block_num_y, 1, 1);
  6390. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6391. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  6392. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6393. }
  6394. 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) {
  6395. GGML_ASSERT(ncols % QK_K == 0);
  6396. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  6397. const int block_num_y = (nrows + ny - 1) / ny;
  6398. const dim3 block_nums(block_num_y, 1, 1);
  6399. const dim3 block_dims(32, ny, 1);
  6400. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6401. }
  6402. 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) {
  6403. GGML_ASSERT(ncols % QK_K == 0);
  6404. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6405. const int block_num_y = (nrows + ny - 1) / ny;
  6406. const dim3 block_nums(block_num_y, 1, 1);
  6407. const dim3 block_dims(32, ny, 1);
  6408. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6409. }
  6410. 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) {
  6411. GGML_ASSERT(ncols % QK_K == 0);
  6412. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6413. const int block_num_y = (nrows + ny - 1) / ny;
  6414. const dim3 block_nums(block_num_y, 1, 1);
  6415. const dim3 block_dims(32, ny, 1);
  6416. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6417. }
  6418. 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) {
  6419. GGML_ASSERT(ncols % QK_K == 0);
  6420. const dim3 block_dims(32, 1, 1);
  6421. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  6422. }
  6423. 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) {
  6424. GGML_ASSERT(ncols % QK_K == 0);
  6425. const int ny = 2 / K_QUANTS_PER_ITERATION;
  6426. const int block_num_y = (nrows + ny - 1) / ny;
  6427. const dim3 block_nums(block_num_y, 1, 1);
  6428. const dim3 block_dims(32, ny, 1);
  6429. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6430. }
  6431. 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) {
  6432. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  6433. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  6434. const dim3 block_nums(block_num_y, 1, 1);
  6435. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  6436. dequantize_mul_mat_vec<1, 1, convert_f16>
  6437. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  6438. }
  6439. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  6440. static void mul_mat_vec_q_cuda(
  6441. const void * vx, const void * vy, float * dst,
  6442. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  6443. GGML_ASSERT(ncols_x % qk == 0);
  6444. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  6445. int id;
  6446. CUDA_CHECK(cudaGetDevice(&id));
  6447. int64_t nwarps = 1;
  6448. int64_t rows_per_cuda_block = 1;
  6449. if (get_cuda_global_info().devices[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  6450. switch(ncols_y) {
  6451. case 1:
  6452. nwarps = 4;
  6453. rows_per_cuda_block = 1;
  6454. break;
  6455. case 2:
  6456. case 3:
  6457. case 4:
  6458. nwarps = 4;
  6459. rows_per_cuda_block = 2;
  6460. break;
  6461. case 5:
  6462. case 6:
  6463. case 7:
  6464. case 8:
  6465. nwarps = 2;
  6466. rows_per_cuda_block = 2;
  6467. break;
  6468. default:
  6469. GGML_ASSERT(false);
  6470. break;
  6471. }
  6472. }
  6473. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  6474. const dim3 block_nums(nblocks, 1, 1);
  6475. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6476. switch (ncols_y) {
  6477. case 1:
  6478. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  6479. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6480. break;
  6481. case 2:
  6482. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  6483. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6484. break;
  6485. case 3:
  6486. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  6487. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6488. break;
  6489. case 4:
  6490. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  6491. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6492. break;
  6493. case 5:
  6494. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  6495. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6496. break;
  6497. case 6:
  6498. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  6499. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6500. break;
  6501. case 7:
  6502. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  6503. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6504. break;
  6505. case 8:
  6506. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  6507. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  6508. break;
  6509. default:
  6510. GGML_ASSERT(false);
  6511. break;
  6512. }
  6513. }
  6514. static void ggml_mul_mat_q4_0_q8_1_cuda(
  6515. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6516. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6517. int id;
  6518. CUDA_CHECK(cudaGetDevice(&id));
  6519. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6520. int mmq_x, mmq_y, nwarps;
  6521. if (compute_capability >= CC_RDNA2) {
  6522. mmq_x = MMQ_X_Q4_0_RDNA2;
  6523. mmq_y = MMQ_Y_Q4_0_RDNA2;
  6524. nwarps = NWARPS_Q4_0_RDNA2;
  6525. } else if (compute_capability >= CC_OFFSET_AMD) {
  6526. mmq_x = MMQ_X_Q4_0_RDNA1;
  6527. mmq_y = MMQ_Y_Q4_0_RDNA1;
  6528. nwarps = NWARPS_Q4_0_RDNA1;
  6529. } else if (compute_capability >= CC_VOLTA) {
  6530. mmq_x = MMQ_X_Q4_0_AMPERE;
  6531. mmq_y = MMQ_Y_Q4_0_AMPERE;
  6532. nwarps = NWARPS_Q4_0_AMPERE;
  6533. } else if (compute_capability >= MIN_CC_DP4A) {
  6534. mmq_x = MMQ_X_Q4_0_PASCAL;
  6535. mmq_y = MMQ_Y_Q4_0_PASCAL;
  6536. nwarps = NWARPS_Q4_0_PASCAL;
  6537. } else {
  6538. GGML_ASSERT(false);
  6539. }
  6540. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6541. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6542. const dim3 block_nums(block_num_x, block_num_y, 1);
  6543. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6544. if (nrows_x % mmq_y == 0) {
  6545. const bool need_check = false;
  6546. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6547. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6548. } else {
  6549. const bool need_check = true;
  6550. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6551. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6552. }
  6553. }
  6554. static void ggml_mul_mat_q4_1_q8_1_cuda(
  6555. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6556. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6557. int id;
  6558. CUDA_CHECK(cudaGetDevice(&id));
  6559. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6560. int mmq_x, mmq_y, nwarps;
  6561. if (compute_capability >= CC_RDNA2) {
  6562. mmq_x = MMQ_X_Q4_1_RDNA2;
  6563. mmq_y = MMQ_Y_Q4_1_RDNA2;
  6564. nwarps = NWARPS_Q4_1_RDNA2;
  6565. } else if (compute_capability >= CC_OFFSET_AMD) {
  6566. mmq_x = MMQ_X_Q4_1_RDNA1;
  6567. mmq_y = MMQ_Y_Q4_1_RDNA1;
  6568. nwarps = NWARPS_Q4_1_RDNA1;
  6569. } else if (compute_capability >= CC_VOLTA) {
  6570. mmq_x = MMQ_X_Q4_1_AMPERE;
  6571. mmq_y = MMQ_Y_Q4_1_AMPERE;
  6572. nwarps = NWARPS_Q4_1_AMPERE;
  6573. } else if (compute_capability >= MIN_CC_DP4A) {
  6574. mmq_x = MMQ_X_Q4_1_PASCAL;
  6575. mmq_y = MMQ_Y_Q4_1_PASCAL;
  6576. nwarps = NWARPS_Q4_1_PASCAL;
  6577. } else {
  6578. GGML_ASSERT(false);
  6579. }
  6580. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6581. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6582. const dim3 block_nums(block_num_x, block_num_y, 1);
  6583. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6584. if (nrows_x % mmq_y == 0) {
  6585. const bool need_check = false;
  6586. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6587. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6588. } else {
  6589. const bool need_check = true;
  6590. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6591. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6592. }
  6593. }
  6594. static void ggml_mul_mat_q5_0_q8_1_cuda(
  6595. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6596. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6597. int id;
  6598. CUDA_CHECK(cudaGetDevice(&id));
  6599. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6600. int mmq_x, mmq_y, nwarps;
  6601. if (compute_capability >= CC_RDNA2) {
  6602. mmq_x = MMQ_X_Q5_0_RDNA2;
  6603. mmq_y = MMQ_Y_Q5_0_RDNA2;
  6604. nwarps = NWARPS_Q5_0_RDNA2;
  6605. } else if (compute_capability >= CC_OFFSET_AMD) {
  6606. mmq_x = MMQ_X_Q5_0_RDNA1;
  6607. mmq_y = MMQ_Y_Q5_0_RDNA1;
  6608. nwarps = NWARPS_Q5_0_RDNA1;
  6609. } else if (compute_capability >= CC_VOLTA) {
  6610. mmq_x = MMQ_X_Q5_0_AMPERE;
  6611. mmq_y = MMQ_Y_Q5_0_AMPERE;
  6612. nwarps = NWARPS_Q5_0_AMPERE;
  6613. } else if (compute_capability >= MIN_CC_DP4A) {
  6614. mmq_x = MMQ_X_Q5_0_PASCAL;
  6615. mmq_y = MMQ_Y_Q5_0_PASCAL;
  6616. nwarps = NWARPS_Q5_0_PASCAL;
  6617. } else {
  6618. GGML_ASSERT(false);
  6619. }
  6620. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6621. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6622. const dim3 block_nums(block_num_x, block_num_y, 1);
  6623. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6624. if (nrows_x % mmq_y == 0) {
  6625. const bool need_check = false;
  6626. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6627. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6628. } else {
  6629. const bool need_check = true;
  6630. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6631. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6632. }
  6633. }
  6634. static void ggml_mul_mat_q5_1_q8_1_cuda(
  6635. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6636. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6637. int id;
  6638. CUDA_CHECK(cudaGetDevice(&id));
  6639. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6640. int mmq_x, mmq_y, nwarps;
  6641. if (compute_capability >= CC_RDNA2) {
  6642. mmq_x = MMQ_X_Q5_1_RDNA2;
  6643. mmq_y = MMQ_Y_Q5_1_RDNA2;
  6644. nwarps = NWARPS_Q5_1_RDNA2;
  6645. } else if (compute_capability >= CC_OFFSET_AMD) {
  6646. mmq_x = MMQ_X_Q5_1_RDNA1;
  6647. mmq_y = MMQ_Y_Q5_1_RDNA1;
  6648. nwarps = NWARPS_Q5_1_RDNA1;
  6649. } else if (compute_capability >= CC_VOLTA) {
  6650. mmq_x = MMQ_X_Q5_1_AMPERE;
  6651. mmq_y = MMQ_Y_Q5_1_AMPERE;
  6652. nwarps = NWARPS_Q5_1_AMPERE;
  6653. } else if (compute_capability >= MIN_CC_DP4A) {
  6654. mmq_x = MMQ_X_Q5_1_PASCAL;
  6655. mmq_y = MMQ_Y_Q5_1_PASCAL;
  6656. nwarps = NWARPS_Q5_1_PASCAL;
  6657. } else {
  6658. GGML_ASSERT(false);
  6659. }
  6660. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6661. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6662. const dim3 block_nums(block_num_x, block_num_y, 1);
  6663. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6664. if (nrows_x % mmq_y == 0) {
  6665. const bool need_check = false;
  6666. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6667. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6668. } else {
  6669. const bool need_check = true;
  6670. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  6671. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6672. }
  6673. }
  6674. static void ggml_mul_mat_q8_0_q8_1_cuda(
  6675. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6676. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6677. int id;
  6678. CUDA_CHECK(cudaGetDevice(&id));
  6679. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6680. int mmq_x, mmq_y, nwarps;
  6681. if (compute_capability >= CC_RDNA2) {
  6682. mmq_x = MMQ_X_Q8_0_RDNA2;
  6683. mmq_y = MMQ_Y_Q8_0_RDNA2;
  6684. nwarps = NWARPS_Q8_0_RDNA2;
  6685. } else if (compute_capability >= CC_OFFSET_AMD) {
  6686. mmq_x = MMQ_X_Q8_0_RDNA1;
  6687. mmq_y = MMQ_Y_Q8_0_RDNA1;
  6688. nwarps = NWARPS_Q8_0_RDNA1;
  6689. } else if (compute_capability >= CC_VOLTA) {
  6690. mmq_x = MMQ_X_Q8_0_AMPERE;
  6691. mmq_y = MMQ_Y_Q8_0_AMPERE;
  6692. nwarps = NWARPS_Q8_0_AMPERE;
  6693. } else if (compute_capability >= MIN_CC_DP4A) {
  6694. mmq_x = MMQ_X_Q8_0_PASCAL;
  6695. mmq_y = MMQ_Y_Q8_0_PASCAL;
  6696. nwarps = NWARPS_Q8_0_PASCAL;
  6697. } else {
  6698. GGML_ASSERT(false);
  6699. }
  6700. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6701. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6702. const dim3 block_nums(block_num_x, block_num_y, 1);
  6703. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6704. if (nrows_x % mmq_y == 0) {
  6705. const bool need_check = false;
  6706. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6707. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6708. } else {
  6709. const bool need_check = true;
  6710. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6711. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6712. }
  6713. }
  6714. static void ggml_mul_mat_q2_K_q8_1_cuda(
  6715. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6716. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6717. int id;
  6718. CUDA_CHECK(cudaGetDevice(&id));
  6719. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6720. int mmq_x, mmq_y, nwarps;
  6721. if (compute_capability >= CC_RDNA2) {
  6722. mmq_x = MMQ_X_Q2_K_RDNA2;
  6723. mmq_y = MMQ_Y_Q2_K_RDNA2;
  6724. nwarps = NWARPS_Q2_K_RDNA2;
  6725. } else if (compute_capability >= CC_OFFSET_AMD) {
  6726. mmq_x = MMQ_X_Q2_K_RDNA1;
  6727. mmq_y = MMQ_Y_Q2_K_RDNA1;
  6728. nwarps = NWARPS_Q2_K_RDNA1;
  6729. } else if (compute_capability >= CC_VOLTA) {
  6730. mmq_x = MMQ_X_Q2_K_AMPERE;
  6731. mmq_y = MMQ_Y_Q2_K_AMPERE;
  6732. nwarps = NWARPS_Q2_K_AMPERE;
  6733. } else if (compute_capability >= MIN_CC_DP4A) {
  6734. mmq_x = MMQ_X_Q2_K_PASCAL;
  6735. mmq_y = MMQ_Y_Q2_K_PASCAL;
  6736. nwarps = NWARPS_Q2_K_PASCAL;
  6737. } else {
  6738. GGML_ASSERT(false);
  6739. }
  6740. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6741. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6742. const dim3 block_nums(block_num_x, block_num_y, 1);
  6743. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6744. if (nrows_x % mmq_y == 0) {
  6745. const bool need_check = false;
  6746. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6747. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6748. } else {
  6749. const bool need_check = true;
  6750. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6751. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6752. }
  6753. }
  6754. static void ggml_mul_mat_q3_K_q8_1_cuda(
  6755. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6756. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6757. #if QK_K == 256
  6758. int id;
  6759. CUDA_CHECK(cudaGetDevice(&id));
  6760. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6761. int mmq_x, mmq_y, nwarps;
  6762. if (compute_capability >= CC_RDNA2) {
  6763. mmq_x = MMQ_X_Q3_K_RDNA2;
  6764. mmq_y = MMQ_Y_Q3_K_RDNA2;
  6765. nwarps = NWARPS_Q3_K_RDNA2;
  6766. } else if (compute_capability >= CC_OFFSET_AMD) {
  6767. mmq_x = MMQ_X_Q3_K_RDNA1;
  6768. mmq_y = MMQ_Y_Q3_K_RDNA1;
  6769. nwarps = NWARPS_Q3_K_RDNA1;
  6770. } else if (compute_capability >= CC_VOLTA) {
  6771. mmq_x = MMQ_X_Q3_K_AMPERE;
  6772. mmq_y = MMQ_Y_Q3_K_AMPERE;
  6773. nwarps = NWARPS_Q3_K_AMPERE;
  6774. } else if (compute_capability >= MIN_CC_DP4A) {
  6775. mmq_x = MMQ_X_Q3_K_PASCAL;
  6776. mmq_y = MMQ_Y_Q3_K_PASCAL;
  6777. nwarps = NWARPS_Q3_K_PASCAL;
  6778. } else {
  6779. GGML_ASSERT(false);
  6780. }
  6781. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6782. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6783. const dim3 block_nums(block_num_x, block_num_y, 1);
  6784. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6785. if (nrows_x % mmq_y == 0) {
  6786. const bool need_check = false;
  6787. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6788. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6789. } else {
  6790. const bool need_check = true;
  6791. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6792. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6793. }
  6794. #endif
  6795. }
  6796. static void ggml_mul_mat_q4_K_q8_1_cuda(
  6797. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6798. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6799. int id;
  6800. CUDA_CHECK(cudaGetDevice(&id));
  6801. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6802. int mmq_x, mmq_y, nwarps;
  6803. if (compute_capability >= CC_RDNA2) {
  6804. mmq_x = MMQ_X_Q4_K_RDNA2;
  6805. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6806. nwarps = NWARPS_Q4_K_RDNA2;
  6807. } else if (compute_capability >= CC_OFFSET_AMD) {
  6808. mmq_x = MMQ_X_Q4_K_RDNA1;
  6809. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6810. nwarps = NWARPS_Q4_K_RDNA1;
  6811. } else if (compute_capability >= CC_VOLTA) {
  6812. mmq_x = MMQ_X_Q4_K_AMPERE;
  6813. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6814. nwarps = NWARPS_Q4_K_AMPERE;
  6815. } else if (compute_capability >= MIN_CC_DP4A) {
  6816. mmq_x = MMQ_X_Q4_K_PASCAL;
  6817. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6818. nwarps = NWARPS_Q4_K_PASCAL;
  6819. } else {
  6820. GGML_ASSERT(false);
  6821. }
  6822. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6823. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6824. const dim3 block_nums(block_num_x, block_num_y, 1);
  6825. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6826. if (nrows_x % mmq_y == 0) {
  6827. const bool need_check = false;
  6828. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6829. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6830. } else {
  6831. const bool need_check = true;
  6832. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6833. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6834. }
  6835. }
  6836. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6837. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6838. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6839. int id;
  6840. CUDA_CHECK(cudaGetDevice(&id));
  6841. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6842. int mmq_x, mmq_y, nwarps;
  6843. if (compute_capability >= CC_RDNA2) {
  6844. mmq_x = MMQ_X_Q5_K_RDNA2;
  6845. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6846. nwarps = NWARPS_Q5_K_RDNA2;
  6847. } else if (compute_capability >= CC_OFFSET_AMD) {
  6848. mmq_x = MMQ_X_Q5_K_RDNA1;
  6849. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6850. nwarps = NWARPS_Q5_K_RDNA1;
  6851. } else if (compute_capability >= CC_VOLTA) {
  6852. mmq_x = MMQ_X_Q5_K_AMPERE;
  6853. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6854. nwarps = NWARPS_Q5_K_AMPERE;
  6855. } else if (compute_capability >= MIN_CC_DP4A) {
  6856. mmq_x = MMQ_X_Q5_K_PASCAL;
  6857. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6858. nwarps = NWARPS_Q5_K_PASCAL;
  6859. } else {
  6860. GGML_ASSERT(false);
  6861. }
  6862. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6863. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6864. const dim3 block_nums(block_num_x, block_num_y, 1);
  6865. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6866. if (nrows_x % mmq_y == 0) {
  6867. const bool need_check = false;
  6868. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6869. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6870. } else {
  6871. const bool need_check = true;
  6872. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6873. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6874. }
  6875. }
  6876. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6877. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6878. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6879. int id;
  6880. CUDA_CHECK(cudaGetDevice(&id));
  6881. const int compute_capability = get_cuda_global_info().devices[id].cc;
  6882. int mmq_x, mmq_y, nwarps;
  6883. if (compute_capability >= CC_RDNA2) {
  6884. mmq_x = MMQ_X_Q6_K_RDNA2;
  6885. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6886. nwarps = NWARPS_Q6_K_RDNA2;
  6887. } else if (compute_capability >= CC_OFFSET_AMD) {
  6888. mmq_x = MMQ_X_Q6_K_RDNA1;
  6889. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6890. nwarps = NWARPS_Q6_K_RDNA1;
  6891. } else if (compute_capability >= CC_VOLTA) {
  6892. mmq_x = MMQ_X_Q6_K_AMPERE;
  6893. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6894. nwarps = NWARPS_Q6_K_AMPERE;
  6895. } else if (compute_capability >= MIN_CC_DP4A) {
  6896. mmq_x = MMQ_X_Q6_K_PASCAL;
  6897. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6898. nwarps = NWARPS_Q6_K_PASCAL;
  6899. } else {
  6900. GGML_ASSERT(false);
  6901. }
  6902. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6903. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6904. const dim3 block_nums(block_num_x, block_num_y, 1);
  6905. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6906. if (nrows_x % mmq_y == 0) {
  6907. const bool need_check = false;
  6908. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6909. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6910. } else {
  6911. const bool need_check = true;
  6912. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6913. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6914. }
  6915. }
  6916. static void ggml_mul_mat_p021_f16_f32_cuda(
  6917. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6918. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6919. const dim3 block_nums(1, nrows_x, nchannels_y);
  6920. const dim3 block_dims(WARP_SIZE, 1, 1);
  6921. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6922. }
  6923. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6924. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6925. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6926. const dim3 block_nums(1, nrows_x, nchannels_y);
  6927. const dim3 block_dims(WARP_SIZE, 1, 1);
  6928. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6929. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6930. }
  6931. static void ggml_cpy_f16_f32_cuda(
  6932. const char * cx, char * cdst, const int ne,
  6933. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6934. 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) {
  6935. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6936. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6937. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6938. }
  6939. static void ggml_cpy_f32_f32_cuda(
  6940. const char * cx, char * cdst, const int ne,
  6941. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6942. 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) {
  6943. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6944. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6945. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6946. }
  6947. static void ggml_cpy_f32_f16_cuda(
  6948. const char * cx, char * cdst, const int ne,
  6949. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6950. 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) {
  6951. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6952. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6953. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6954. }
  6955. static void ggml_cpy_f32_q8_0_cuda(
  6956. const char * cx, char * cdst, const int ne,
  6957. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6958. 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) {
  6959. GGML_ASSERT(ne % QK8_0 == 0);
  6960. const int num_blocks = ne / QK8_0;
  6961. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6962. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6963. }
  6964. static void ggml_cpy_f32_q4_0_cuda(
  6965. const char * cx, char * cdst, const int ne,
  6966. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6967. 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) {
  6968. GGML_ASSERT(ne % QK4_0 == 0);
  6969. const int num_blocks = ne / QK4_0;
  6970. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6971. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6972. }
  6973. static void ggml_cpy_f32_q4_1_cuda(
  6974. const char * cx, char * cdst, const int ne,
  6975. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6976. 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) {
  6977. GGML_ASSERT(ne % QK4_1 == 0);
  6978. const int num_blocks = ne / QK4_1;
  6979. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6980. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6981. }
  6982. static void ggml_cpy_f32_q5_0_cuda(
  6983. const char * cx, char * cdst, const int ne,
  6984. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6985. 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) {
  6986. GGML_ASSERT(ne % QK5_0 == 0);
  6987. const int num_blocks = ne / QK5_0;
  6988. cpy_f32_q<cpy_blck_f32_q5_0, QK5_0><<<num_blocks, 1, 0, stream>>>
  6989. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6990. }
  6991. static void ggml_cpy_f32_q5_1_cuda(
  6992. const char * cx, char * cdst, const int ne,
  6993. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6994. 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) {
  6995. GGML_ASSERT(ne % QK5_1 == 0);
  6996. const int num_blocks = ne / QK5_1;
  6997. cpy_f32_q<cpy_blck_f32_q5_1, QK5_1><<<num_blocks, 1, 0, stream>>>
  6998. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6999. }
  7000. static void ggml_cpy_f32_iq4_nl_cuda(
  7001. const char * cx, char * cdst, const int ne,
  7002. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7003. 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) {
  7004. GGML_ASSERT(ne % QK4_NL == 0);
  7005. const int num_blocks = ne / QK4_NL;
  7006. cpy_f32_q<cpy_blck_f32_iq4_nl, QK4_NL><<<num_blocks, 1, 0, stream>>>
  7007. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  7008. }
  7009. static void ggml_cpy_f16_f16_cuda(
  7010. const char * cx, char * cdst, const int ne,
  7011. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7012. 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) {
  7013. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  7014. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  7015. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  7016. }
  7017. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  7018. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  7019. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  7020. }
  7021. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  7022. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  7023. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  7024. }
  7025. template<typename T>
  7026. static void rope_cuda(
  7027. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  7028. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  7029. ) {
  7030. GGML_ASSERT(ncols % 2 == 0);
  7031. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  7032. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  7033. const dim3 block_nums(nrows, num_blocks_x, 1);
  7034. if (pos == nullptr) {
  7035. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  7036. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  7037. );
  7038. } else {
  7039. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  7040. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  7041. );
  7042. }
  7043. }
  7044. template<typename T>
  7045. static void rope_neox_cuda(
  7046. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  7047. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  7048. ) {
  7049. GGML_ASSERT(ncols % 2 == 0);
  7050. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  7051. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  7052. const dim3 block_nums(nrows, num_blocks_x, 1);
  7053. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7054. const float inv_ndims = -1.0f / n_dims;
  7055. if (pos == nullptr) {
  7056. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  7057. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  7058. theta_scale, inv_ndims
  7059. );
  7060. } else {
  7061. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  7062. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  7063. theta_scale, inv_ndims
  7064. );
  7065. }
  7066. }
  7067. static void rope_glm_f32_cuda(
  7068. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  7069. float freq_base, int n_ctx, cudaStream_t stream
  7070. ) {
  7071. GGML_ASSERT(ncols % 4 == 0);
  7072. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  7073. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  7074. const dim3 block_nums(num_blocks_x, nrows, 1);
  7075. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  7076. }
  7077. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  7078. const int k_rows, const int n_heads_log2_floor, const float m0,
  7079. const float m1, cudaStream_t stream) {
  7080. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  7081. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  7082. const dim3 block_nums(num_blocks_x, nrows, 1);
  7083. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  7084. }
  7085. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  7086. const dim3 block_dims(WARP_SIZE, 1, 1);
  7087. const dim3 block_nums(nrows, 1, 1);
  7088. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  7089. }
  7090. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  7091. // bitonic sort requires ncols to be power of 2
  7092. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  7093. const dim3 block_dims(ncols, 1, 1);
  7094. const dim3 block_nums(1, nrows, 1);
  7095. if (order == GGML_SORT_ORDER_ASC) {
  7096. k_argsort_f32_i32<GGML_SORT_ORDER_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  7097. } else if (order == GGML_SORT_ORDER_DESC) {
  7098. k_argsort_f32_i32<GGML_SORT_ORDER_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  7099. } else {
  7100. GGML_ASSERT(false);
  7101. }
  7102. }
  7103. 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) {
  7104. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  7105. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  7106. const dim3 block_nums(nrows_x, block_num_x, 1);
  7107. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  7108. }
  7109. 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) {
  7110. int nth = WARP_SIZE;
  7111. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  7112. const dim3 block_dims(nth, 1, 1);
  7113. const dim3 block_nums(nrows_x, 1, 1);
  7114. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  7115. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  7116. const uint32_t n_head_kv = nrows_x/nrows_y;
  7117. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  7118. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7119. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7120. if (shmem < get_cuda_global_info().devices[ggml_cuda_get_device()].smpb) {
  7121. switch (ncols_x) {
  7122. case 32:
  7123. 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);
  7124. break;
  7125. case 64:
  7126. 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);
  7127. break;
  7128. case 128:
  7129. 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);
  7130. break;
  7131. case 256:
  7132. 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);
  7133. break;
  7134. case 512:
  7135. 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);
  7136. break;
  7137. case 1024:
  7138. 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);
  7139. break;
  7140. case 2048:
  7141. 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);
  7142. break;
  7143. case 4096:
  7144. 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);
  7145. break;
  7146. default:
  7147. 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);
  7148. break;
  7149. }
  7150. } else {
  7151. const size_t shmem_low = WARP_SIZE*sizeof(float);
  7152. 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);
  7153. }
  7154. }
  7155. template <typename T>
  7156. static void im2col_cuda(const float * x, T* dst,
  7157. int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
  7158. int64_t batch, int64_t batch_offset, int64_t offset_delta,
  7159. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  7160. const int parallel_elements = OW * KW * KH;
  7161. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  7162. dim3 block_nums(num_blocks, OH, batch * IC);
  7163. 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);
  7164. }
  7165. static cudaError_t ggml_cuda_cpy_tensor_2d(
  7166. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  7167. GGML_ASSERT(ggml_backend_buffer_is_cuda(src->buffer));
  7168. char * src_ptr = (char *) src->data;
  7169. char * dst_ptr = (char *) dst;
  7170. const int64_t ne0 = src->ne[0];
  7171. const int64_t nb0 = src->nb[0];
  7172. const int64_t nb1 = src->nb[1];
  7173. const int64_t nb2 = src->nb[2];
  7174. const int64_t nb3 = src->nb[3];
  7175. const enum ggml_type type = src->type;
  7176. const int64_t ts = ggml_type_size(type);
  7177. const int64_t bs = ggml_blck_size(type);
  7178. int64_t i1_diff = i1_high - i1_low;
  7179. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  7180. if (nb0 == ts && nb1 == ts*ne0/bs) {
  7181. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, cudaMemcpyDeviceToDevice, stream);
  7182. } else if (nb0 == ts) {
  7183. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, cudaMemcpyDeviceToDevice, stream);
  7184. } else {
  7185. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  7186. const void * rx = (const void *) ((const char *) x + i1*nb1);
  7187. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  7188. // pretend the row is a matrix with cols=1
  7189. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, cudaMemcpyDeviceToDevice, stream);
  7190. if (r != cudaSuccess) {
  7191. return r;
  7192. }
  7193. }
  7194. return cudaSuccess;
  7195. }
  7196. }
  7197. static void ggml_cuda_op_get_rows(
  7198. ggml_backend_cuda_context & ctx,
  7199. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7200. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  7201. GGML_UNUSED(ctx);
  7202. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7203. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7204. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  7205. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  7206. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  7207. const int32_t * src1_i32 = (const int32_t *) src1_d;
  7208. switch (src0->type) {
  7209. case GGML_TYPE_F16:
  7210. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  7211. break;
  7212. case GGML_TYPE_F32:
  7213. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7214. break;
  7215. case GGML_TYPE_Q4_0:
  7216. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7217. break;
  7218. case GGML_TYPE_Q4_1:
  7219. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7220. break;
  7221. case GGML_TYPE_Q5_0:
  7222. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7223. break;
  7224. case GGML_TYPE_Q5_1:
  7225. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7226. break;
  7227. case GGML_TYPE_Q8_0:
  7228. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  7229. break;
  7230. default:
  7231. // TODO: k-quants
  7232. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  7233. GGML_ASSERT(false);
  7234. break;
  7235. }
  7236. }
  7237. template<class op>
  7238. static void ggml_cuda_op_bin_bcast(
  7239. ggml_backend_cuda_context & ctx,
  7240. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7241. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7242. GGML_UNUSED(ctx);
  7243. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7244. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7245. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7246. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7247. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  7248. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  7249. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  7250. } else {
  7251. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  7252. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  7253. GGML_ASSERT(false);
  7254. }
  7255. }
  7256. static void ggml_cuda_op_repeat(
  7257. ggml_backend_cuda_context & ctx,
  7258. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7259. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  7260. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(ctx, dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  7261. GGML_UNUSED(src1);
  7262. GGML_UNUSED(src1_d);
  7263. }
  7264. static void ggml_cuda_op_add(
  7265. ggml_backend_cuda_context & ctx,
  7266. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7267. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7268. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7269. }
  7270. static void ggml_cuda_op_acc(
  7271. ggml_backend_cuda_context & ctx,
  7272. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7273. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7274. GGML_UNUSED(ctx);
  7275. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7276. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7277. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7278. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  7279. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  7280. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  7281. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  7282. int offset = dst->op_params[3] / 4; // offset in bytes
  7283. 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);
  7284. GGML_UNUSED(dst);
  7285. }
  7286. static void ggml_cuda_op_mul(
  7287. ggml_backend_cuda_context & ctx,
  7288. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7289. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7290. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7291. }
  7292. static void ggml_cuda_op_div(
  7293. ggml_backend_cuda_context & ctx,
  7294. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7295. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7296. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  7297. }
  7298. static void ggml_cuda_op_gelu(
  7299. ggml_backend_cuda_context & ctx,
  7300. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7301. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7302. GGML_UNUSED(ctx);
  7303. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7304. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7305. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7306. GGML_UNUSED(src1);
  7307. GGML_UNUSED(dst);
  7308. GGML_UNUSED(src1_dd);
  7309. }
  7310. static void ggml_cuda_op_silu(
  7311. ggml_backend_cuda_context & ctx,
  7312. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7313. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7314. GGML_UNUSED(ctx);
  7315. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7316. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7317. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7318. GGML_UNUSED(src1);
  7319. GGML_UNUSED(dst);
  7320. GGML_UNUSED(src1_dd);
  7321. }
  7322. static void ggml_cuda_op_gelu_quick(
  7323. ggml_backend_cuda_context & ctx,
  7324. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7325. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7326. GGML_UNUSED(ctx);
  7327. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7328. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7329. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7330. GGML_UNUSED(src1);
  7331. GGML_UNUSED(dst);
  7332. GGML_UNUSED(src1_dd);
  7333. }
  7334. static void ggml_cuda_op_tanh(
  7335. ggml_backend_cuda_context & ctx,
  7336. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7337. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7338. GGML_UNUSED(ctx);
  7339. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7340. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7341. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7342. GGML_UNUSED(src1);
  7343. GGML_UNUSED(dst);
  7344. GGML_UNUSED(src1_dd);
  7345. }
  7346. static void ggml_cuda_op_relu(
  7347. ggml_backend_cuda_context & ctx,
  7348. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7349. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7350. GGML_UNUSED(ctx);
  7351. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7352. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7353. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7354. GGML_UNUSED(src1);
  7355. GGML_UNUSED(dst);
  7356. GGML_UNUSED(src1_dd);
  7357. }
  7358. static void ggml_cuda_op_hardsigmoid(
  7359. ggml_backend_cuda_context & ctx,
  7360. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7361. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7362. GGML_UNUSED(ctx);
  7363. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7364. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7365. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7366. GGML_UNUSED(src1);
  7367. GGML_UNUSED(dst);
  7368. GGML_UNUSED(src1_dd);
  7369. }
  7370. static void ggml_cuda_op_hardswish(
  7371. ggml_backend_cuda_context & ctx,
  7372. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7373. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7374. GGML_UNUSED(ctx);
  7375. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7376. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7377. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7378. GGML_UNUSED(src1);
  7379. GGML_UNUSED(dst);
  7380. GGML_UNUSED(src1_dd);
  7381. }
  7382. static void ggml_cuda_op_leaky_relu(
  7383. ggml_backend_cuda_context & ctx,
  7384. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7385. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7386. GGML_UNUSED(ctx);
  7387. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7388. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7389. float negative_slope;
  7390. memcpy(&negative_slope, dst->op_params, sizeof(float));
  7391. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  7392. GGML_UNUSED(src1);
  7393. GGML_UNUSED(dst);
  7394. GGML_UNUSED(src1_dd);
  7395. }
  7396. static void ggml_cuda_op_sqr(
  7397. ggml_backend_cuda_context & ctx,
  7398. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7399. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7400. GGML_UNUSED(ctx);
  7401. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7402. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7403. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  7404. GGML_UNUSED(src1);
  7405. GGML_UNUSED(dst);
  7406. GGML_UNUSED(src1_dd);
  7407. }
  7408. static void ggml_cuda_op_norm(
  7409. ggml_backend_cuda_context & ctx,
  7410. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7411. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7412. GGML_UNUSED(ctx);
  7413. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7414. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7415. const int64_t ne00 = src0->ne[0];
  7416. const int64_t nrows = ggml_nrows(src0);
  7417. float eps;
  7418. memcpy(&eps, dst->op_params, sizeof(float));
  7419. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7420. GGML_UNUSED(src1);
  7421. GGML_UNUSED(dst);
  7422. GGML_UNUSED(src1_dd);
  7423. }
  7424. static void ggml_cuda_op_group_norm(
  7425. ggml_backend_cuda_context & ctx,
  7426. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7427. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7428. GGML_UNUSED(ctx);
  7429. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7430. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7431. int num_groups = dst->op_params[0];
  7432. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  7433. group_norm_f32_cuda(src0_dd, dst_dd, num_groups * src0->ne[3], group_size, ggml_nelements(src0), main_stream);
  7434. GGML_UNUSED(src1);
  7435. GGML_UNUSED(dst);
  7436. GGML_UNUSED(src1_dd);
  7437. }
  7438. static void ggml_cuda_op_concat(
  7439. ggml_backend_cuda_context & ctx,
  7440. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7441. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7442. GGML_UNUSED(ctx);
  7443. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7444. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7445. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7446. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  7447. 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);
  7448. }
  7449. GGML_UNUSED(src1);
  7450. GGML_UNUSED(dst);
  7451. }
  7452. static void ggml_cuda_op_upscale(
  7453. ggml_backend_cuda_context & ctx,
  7454. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7455. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7456. GGML_UNUSED(ctx);
  7457. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7458. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7459. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7460. const int scale_factor = dst->op_params[0];
  7461. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], scale_factor, main_stream);
  7462. GGML_UNUSED(src1);
  7463. GGML_UNUSED(dst);
  7464. GGML_UNUSED(src1_dd);
  7465. }
  7466. static void ggml_cuda_op_pad(
  7467. ggml_backend_cuda_context & ctx,
  7468. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7469. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7470. GGML_UNUSED(ctx);
  7471. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7472. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7473. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7474. pad_f32_cuda(src0_dd, dst_dd,
  7475. src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
  7476. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], main_stream);
  7477. GGML_UNUSED(src1);
  7478. GGML_UNUSED(dst);
  7479. GGML_UNUSED(src1_dd);
  7480. }
  7481. static void ggml_cuda_op_arange(
  7482. ggml_backend_cuda_context & ctx,
  7483. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7484. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7485. GGML_UNUSED(ctx);
  7486. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7487. float start;
  7488. float stop;
  7489. float step;
  7490. memcpy(&start, (float *)dst->op_params + 0, sizeof(float));
  7491. memcpy(&stop, (float *)dst->op_params + 1, sizeof(float));
  7492. memcpy(&step, (float *)dst->op_params + 2, sizeof(float));
  7493. int64_t steps = (int64_t)ceil((stop - start) / step);
  7494. GGML_ASSERT(ggml_nelements(dst) == steps);
  7495. arange_f32_cuda(dst_dd, dst->ne[0], start, step, main_stream);
  7496. GGML_UNUSED(src0);
  7497. GGML_UNUSED(src1);
  7498. GGML_UNUSED(src0_dd);
  7499. GGML_UNUSED(src1_dd);
  7500. }
  7501. static void ggml_cuda_op_timestep_embedding(
  7502. ggml_backend_cuda_context & ctx,
  7503. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7504. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7505. GGML_UNUSED(ctx);
  7506. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7507. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7508. const int dim = dst->op_params[0];
  7509. const int max_period = dst->op_params[1];
  7510. timestep_embedding_f32_cuda(src0_dd, dst_dd, src0->ne[0], dst->nb[1], dim, max_period, main_stream);
  7511. GGML_UNUSED(src1);
  7512. GGML_UNUSED(dst);
  7513. GGML_UNUSED(src1_dd);
  7514. }
  7515. static void ggml_cuda_op_rms_norm(
  7516. ggml_backend_cuda_context & ctx,
  7517. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7518. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7519. GGML_UNUSED(ctx);
  7520. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7521. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7522. const int64_t ne00 = src0->ne[0];
  7523. const int64_t nrows = ggml_nrows(src0);
  7524. float eps;
  7525. memcpy(&eps, dst->op_params, sizeof(float));
  7526. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7527. GGML_UNUSED(src1);
  7528. GGML_UNUSED(dst);
  7529. GGML_UNUSED(src1_dd);
  7530. }
  7531. static void ggml_cuda_op_mul_mat_q(
  7532. ggml_backend_cuda_context & ctx,
  7533. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7534. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7535. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7536. const int64_t ne00 = src0->ne[0];
  7537. const int64_t ne10 = src1->ne[0];
  7538. GGML_ASSERT(ne10 % QK8_1 == 0);
  7539. const int64_t ne0 = dst->ne[0];
  7540. const int64_t row_diff = row_high - row_low;
  7541. int id = ggml_cuda_get_device();
  7542. // the main device has a larger memory buffer to hold the results from all GPUs
  7543. // nrows_dst == nrows of the matrix that the kernel writes into
  7544. const int64_t nrows_dst = id == ctx.device ? ne0 : row_diff;
  7545. switch (src0->type) {
  7546. case GGML_TYPE_Q4_0:
  7547. 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);
  7548. break;
  7549. case GGML_TYPE_Q4_1:
  7550. 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);
  7551. break;
  7552. case GGML_TYPE_Q5_0:
  7553. 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);
  7554. break;
  7555. case GGML_TYPE_Q5_1:
  7556. 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);
  7557. break;
  7558. case GGML_TYPE_Q8_0:
  7559. 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);
  7560. break;
  7561. case GGML_TYPE_Q2_K:
  7562. 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);
  7563. break;
  7564. case GGML_TYPE_Q3_K:
  7565. 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);
  7566. break;
  7567. case GGML_TYPE_Q4_K:
  7568. 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);
  7569. break;
  7570. case GGML_TYPE_Q5_K:
  7571. 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);
  7572. break;
  7573. case GGML_TYPE_Q6_K:
  7574. 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);
  7575. break;
  7576. default:
  7577. GGML_ASSERT(false);
  7578. break;
  7579. }
  7580. GGML_UNUSED(src1);
  7581. GGML_UNUSED(dst);
  7582. GGML_UNUSED(src1_ddf_i);
  7583. }
  7584. static void ggml_cuda_op_mul_mat_vec_q(
  7585. ggml_backend_cuda_context & ctx,
  7586. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7587. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7588. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7589. const int64_t ne00 = src0->ne[0];
  7590. const int64_t row_diff = row_high - row_low;
  7591. const int64_t ne10 = src1->ne[0];
  7592. GGML_ASSERT(ne10 % QK8_1 == 0);
  7593. const int64_t ne0 = dst->ne[0];
  7594. int id;
  7595. CUDA_CHECK(cudaGetDevice(&id));
  7596. // the main device has a larger memory buffer to hold the results from all GPUs
  7597. // nrows_dst == nrows of the matrix that the kernel writes into
  7598. const int64_t nrows_dst = id == ctx.device ? ne0 : row_diff;
  7599. switch (src0->type) {
  7600. case GGML_TYPE_Q4_0:
  7601. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7602. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7603. break;
  7604. case GGML_TYPE_Q4_1:
  7605. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7606. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7607. break;
  7608. case GGML_TYPE_Q5_0:
  7609. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7610. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7611. break;
  7612. case GGML_TYPE_Q5_1:
  7613. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7614. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7615. break;
  7616. case GGML_TYPE_Q8_0:
  7617. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7618. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7619. break;
  7620. case GGML_TYPE_Q2_K:
  7621. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7622. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7623. break;
  7624. case GGML_TYPE_Q3_K:
  7625. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7626. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7627. break;
  7628. case GGML_TYPE_Q4_K:
  7629. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7630. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7631. break;
  7632. case GGML_TYPE_Q5_K:
  7633. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7634. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7635. break;
  7636. case GGML_TYPE_Q6_K:
  7637. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7638. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7639. break;
  7640. case GGML_TYPE_IQ2_XXS:
  7641. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7642. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7643. break;
  7644. case GGML_TYPE_IQ2_XS:
  7645. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7646. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7647. break;
  7648. case GGML_TYPE_IQ2_S:
  7649. mul_mat_vec_q_cuda<QK_K, QI2_S, block_iq2_s, 1, vec_dot_iq2_s_q8_1>
  7650. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7651. break;
  7652. case GGML_TYPE_IQ3_XXS:
  7653. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7654. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7655. break;
  7656. case GGML_TYPE_IQ1_S:
  7657. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7658. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7659. break;
  7660. case GGML_TYPE_IQ4_NL:
  7661. mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
  7662. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7663. break;
  7664. case GGML_TYPE_IQ4_XS:
  7665. mul_mat_vec_q_cuda<QK_K, QI4_XS, block_iq4_xs, 1, vec_dot_iq4_xs_q8_1>
  7666. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7667. break;
  7668. case GGML_TYPE_IQ3_S:
  7669. mul_mat_vec_q_cuda<QK_K, QI3_XS, block_iq3_s, 1, vec_dot_iq3_s_q8_1>
  7670. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7671. break;
  7672. default:
  7673. GGML_ASSERT(false);
  7674. break;
  7675. }
  7676. GGML_UNUSED(src1);
  7677. GGML_UNUSED(dst);
  7678. GGML_UNUSED(src1_ddf_i);
  7679. GGML_UNUSED(src1_ncols);
  7680. GGML_UNUSED(src1_padded_row_size);
  7681. }
  7682. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7683. ggml_backend_cuda_context & ctx,
  7684. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7685. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7686. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7687. GGML_UNUSED(ctx);
  7688. const int64_t ne00 = src0->ne[0];
  7689. const int64_t row_diff = row_high - row_low;
  7690. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7691. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7692. #ifdef GGML_CUDA_F16
  7693. ggml_cuda_pool_alloc<half> src1_dfloat_a(ctx.pool());
  7694. half * src1_dfloat = nullptr; // dfloat == half
  7695. bool src1_convert_f16 =
  7696. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7697. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7698. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7699. if (src1_convert_f16) {
  7700. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7701. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7702. GGML_ASSERT(to_fp16_cuda != nullptr);
  7703. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7704. }
  7705. #else
  7706. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7707. #endif // GGML_CUDA_F16
  7708. switch (src0->type) {
  7709. case GGML_TYPE_Q4_0:
  7710. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7711. break;
  7712. case GGML_TYPE_Q4_1:
  7713. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7714. break;
  7715. case GGML_TYPE_Q5_0:
  7716. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7717. break;
  7718. case GGML_TYPE_Q5_1:
  7719. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7720. break;
  7721. case GGML_TYPE_Q8_0:
  7722. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7723. break;
  7724. case GGML_TYPE_Q2_K:
  7725. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7726. break;
  7727. case GGML_TYPE_Q3_K:
  7728. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7729. break;
  7730. case GGML_TYPE_Q4_K:
  7731. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7732. break;
  7733. case GGML_TYPE_Q5_K:
  7734. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7735. break;
  7736. case GGML_TYPE_Q6_K:
  7737. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7738. break;
  7739. case GGML_TYPE_F16:
  7740. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7741. break;
  7742. default:
  7743. GGML_ASSERT(false);
  7744. break;
  7745. }
  7746. GGML_UNUSED(src1);
  7747. GGML_UNUSED(dst);
  7748. GGML_UNUSED(src1_ddq_i);
  7749. GGML_UNUSED(src1_ncols);
  7750. GGML_UNUSED(src1_padded_row_size);
  7751. }
  7752. static void ggml_cuda_op_mul_mat_cublas(
  7753. ggml_backend_cuda_context & ctx,
  7754. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7755. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7756. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7757. GGML_ASSERT(src0_dd_i != nullptr);
  7758. GGML_ASSERT(src1_ddf_i != nullptr);
  7759. GGML_ASSERT(dst_dd_i != nullptr);
  7760. const int64_t ne00 = src0->ne[0];
  7761. const int64_t ne10 = src1->ne[0];
  7762. const int64_t ne0 = dst->ne[0];
  7763. const int64_t row_diff = row_high - row_low;
  7764. int id = ggml_cuda_get_device();
  7765. // the main device has a larger memory buffer to hold the results from all GPUs
  7766. // ldc == nrows of the matrix that cuBLAS writes into
  7767. int ldc = id == ctx.device ? ne0 : row_diff;
  7768. const int compute_capability = get_cuda_global_info().devices[id].cc;
  7769. 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) {
  7770. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7771. ggml_cuda_pool_alloc<half> src0_as_f16(ctx.pool());
  7772. if (src0->type != GGML_TYPE_F16) {
  7773. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7774. GGML_ASSERT(to_fp16_cuda != nullptr);
  7775. size_t ne = row_diff*ne00;
  7776. src0_as_f16.alloc(ne);
  7777. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7778. }
  7779. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7780. ggml_cuda_pool_alloc<half> src1_as_f16(ctx.pool());
  7781. if (src1->type != GGML_TYPE_F16) {
  7782. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7783. GGML_ASSERT(to_fp16_cuda != nullptr);
  7784. size_t ne = src1_ncols*ne10;
  7785. src1_as_f16.alloc(ne);
  7786. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7787. }
  7788. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7789. ggml_cuda_pool_alloc<half> dst_f16(ctx.pool(), row_diff*src1_ncols);
  7790. const half alpha_f16 = 1.0f;
  7791. const half beta_f16 = 0.0f;
  7792. CUBLAS_CHECK(cublasSetStream(ctx.cublas_handle(id), stream));
  7793. CUBLAS_CHECK(
  7794. cublasGemmEx(ctx.cublas_handle(id), CUBLAS_OP_T, CUBLAS_OP_N,
  7795. row_diff, src1_ncols, ne10,
  7796. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7797. src1_ptr, CUDA_R_16F, ne10,
  7798. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7799. CUBLAS_COMPUTE_16F,
  7800. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7801. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7802. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7803. } else {
  7804. ggml_cuda_pool_alloc<float> src0_ddq_as_f32(ctx.pool(id));
  7805. ggml_cuda_pool_alloc<float> src1_ddq_as_f32(ctx.pool(id));
  7806. if (src0->type != GGML_TYPE_F32) {
  7807. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7808. GGML_ASSERT(to_fp32_cuda != nullptr);
  7809. src0_ddq_as_f32.alloc(row_diff*ne00);
  7810. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7811. }
  7812. if (src1->type != GGML_TYPE_F32) {
  7813. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7814. GGML_ASSERT(to_fp32_cuda != nullptr);
  7815. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7816. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7817. }
  7818. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7819. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7820. const float alpha = 1.0f;
  7821. const float beta = 0.0f;
  7822. CUBLAS_CHECK(cublasSetStream(ctx.cublas_handle(id), stream));
  7823. CUBLAS_CHECK(
  7824. cublasSgemm(ctx.cublas_handle(id), CUBLAS_OP_T, CUBLAS_OP_N,
  7825. row_diff, src1_ncols, ne10,
  7826. &alpha, src0_ddf_i, ne00,
  7827. src1_ddf1_i, ne10,
  7828. &beta, dst_dd_i, ldc));
  7829. }
  7830. GGML_UNUSED(dst);
  7831. GGML_UNUSED(src1_ddq_i);
  7832. GGML_UNUSED(src1_padded_row_size);
  7833. }
  7834. static void ggml_cuda_op_rope(
  7835. ggml_backend_cuda_context & ctx,
  7836. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7837. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7838. GGML_UNUSED(ctx);
  7839. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7840. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7841. GGML_ASSERT(src0->type == dst->type);
  7842. const int64_t ne00 = src0->ne[0];
  7843. const int64_t ne01 = src0->ne[1];
  7844. const int64_t ne2 = dst->ne[2];
  7845. const int64_t nrows = ggml_nrows(src0);
  7846. //const int n_past = ((int32_t *) dst->op_params)[0];
  7847. const int n_dims = ((int32_t *) dst->op_params)[1];
  7848. const int mode = ((int32_t *) dst->op_params)[2];
  7849. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7850. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7851. // RoPE alteration for extended context
  7852. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7853. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7854. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7855. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7856. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7857. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7858. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7859. const int32_t * pos = nullptr;
  7860. if ((mode & 1) == 0) {
  7861. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7862. GGML_ASSERT(src1->ne[0] == ne2);
  7863. pos = (const int32_t *) src1_dd;
  7864. }
  7865. const bool is_neox = mode & 2;
  7866. const bool is_glm = mode & 4;
  7867. rope_corr_dims corr_dims;
  7868. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7869. // compute
  7870. if (is_glm) {
  7871. GGML_ASSERT(false);
  7872. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7873. } else if (is_neox) {
  7874. if (src0->type == GGML_TYPE_F32) {
  7875. rope_neox_cuda(
  7876. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7877. attn_factor, corr_dims, main_stream
  7878. );
  7879. } else if (src0->type == GGML_TYPE_F16) {
  7880. rope_neox_cuda(
  7881. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7882. attn_factor, corr_dims, main_stream
  7883. );
  7884. } else {
  7885. GGML_ASSERT(false);
  7886. }
  7887. } else {
  7888. if (src0->type == GGML_TYPE_F32) {
  7889. rope_cuda(
  7890. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7891. attn_factor, corr_dims, main_stream
  7892. );
  7893. } else if (src0->type == GGML_TYPE_F16) {
  7894. rope_cuda(
  7895. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7896. attn_factor, corr_dims, main_stream
  7897. );
  7898. } else {
  7899. GGML_ASSERT(false);
  7900. }
  7901. }
  7902. GGML_UNUSED(src1);
  7903. GGML_UNUSED(dst);
  7904. GGML_UNUSED(src1_dd);
  7905. }
  7906. static void ggml_cuda_op_alibi(
  7907. ggml_backend_cuda_context & ctx,
  7908. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7909. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7910. GGML_UNUSED(ctx);
  7911. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7912. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7913. const int64_t ne00 = src0->ne[0];
  7914. const int64_t ne01 = src0->ne[1];
  7915. const int64_t ne02 = src0->ne[2];
  7916. const int64_t nrows = ggml_nrows(src0);
  7917. //const int n_past = ((int32_t *) dst->op_params)[0];
  7918. const int n_head = ((int32_t *) dst->op_params)[1];
  7919. float max_bias;
  7920. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7921. //GGML_ASSERT(ne01 + n_past == ne00);
  7922. GGML_ASSERT(n_head == ne02);
  7923. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7924. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7925. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7926. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7927. GGML_UNUSED(src1);
  7928. GGML_UNUSED(src1_dd);
  7929. }
  7930. static void ggml_cuda_op_pool2d(
  7931. ggml_backend_cuda_context & ctx,
  7932. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7933. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7934. GGML_UNUSED(ctx);
  7935. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7936. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7937. const int32_t * opts = (const int32_t *)dst->op_params;
  7938. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  7939. const int k0 = opts[1];
  7940. const int k1 = opts[2];
  7941. const int s0 = opts[3];
  7942. const int s1 = opts[4];
  7943. const int p0 = opts[5];
  7944. const int p1 = opts[6];
  7945. const int64_t IH = src0->ne[1];
  7946. const int64_t IW = src0->ne[0];
  7947. const int64_t N = dst->ne[3];
  7948. const int64_t OC = dst->ne[2];
  7949. const int64_t OH = dst->ne[1];
  7950. const int64_t OW = dst->ne[0];
  7951. const int parallel_elements = N * OC * OH * OW;
  7952. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  7953. dim3 block_nums(num_blocks);
  7954. 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);
  7955. GGML_UNUSED(src1);
  7956. GGML_UNUSED(src1_dd);
  7957. }
  7958. static void ggml_cuda_op_im2col(
  7959. ggml_backend_cuda_context & ctx,
  7960. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7961. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7962. GGML_UNUSED(ctx);
  7963. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7964. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7965. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  7966. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7967. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  7968. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  7969. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  7970. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  7971. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  7972. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  7973. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  7974. const int64_t IH = is_2D ? src1->ne[1] : 1;
  7975. const int64_t IW = src1->ne[0];
  7976. const int64_t KH = is_2D ? src0->ne[1] : 1;
  7977. const int64_t KW = src0->ne[0];
  7978. const int64_t OH = is_2D ? dst->ne[2] : 1;
  7979. const int64_t OW = dst->ne[1];
  7980. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7981. const int64_t batch = src1->ne[3];
  7982. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  7983. if(dst->type == GGML_TYPE_F16) {
  7984. 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);
  7985. } else {
  7986. 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);
  7987. }
  7988. GGML_UNUSED(src0);
  7989. GGML_UNUSED(src0_dd);
  7990. }
  7991. static void ggml_cuda_op_sum_rows(
  7992. ggml_backend_cuda_context & ctx,
  7993. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7994. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7995. GGML_UNUSED(ctx);
  7996. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7997. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7998. const int64_t ncols = src0->ne[0];
  7999. const int64_t nrows = ggml_nrows(src0);
  8000. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  8001. GGML_UNUSED(src1);
  8002. GGML_UNUSED(dst);
  8003. GGML_UNUSED(src1_dd);
  8004. }
  8005. static void ggml_cuda_op_argsort(
  8006. ggml_backend_cuda_context & ctx,
  8007. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8008. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8009. GGML_UNUSED(ctx);
  8010. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8011. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  8012. const int64_t ncols = src0->ne[0];
  8013. const int64_t nrows = ggml_nrows(src0);
  8014. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  8015. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  8016. GGML_UNUSED(src1);
  8017. GGML_UNUSED(dst);
  8018. GGML_UNUSED(src1_dd);
  8019. }
  8020. static void ggml_cuda_op_diag_mask_inf(
  8021. ggml_backend_cuda_context & ctx,
  8022. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8023. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8024. GGML_UNUSED(ctx);
  8025. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8026. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8027. const int64_t ne00 = src0->ne[0];
  8028. const int64_t ne01 = src0->ne[1];
  8029. const int nrows0 = ggml_nrows(src0);
  8030. const int n_past = ((int32_t *) dst->op_params)[0];
  8031. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  8032. GGML_UNUSED(src1);
  8033. GGML_UNUSED(dst);
  8034. GGML_UNUSED(src1_dd);
  8035. }
  8036. static void ggml_cuda_op_soft_max(
  8037. ggml_backend_cuda_context & ctx,
  8038. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8039. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8040. GGML_UNUSED(ctx);
  8041. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8042. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8043. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  8044. const int64_t ne00 = src0->ne[0];
  8045. const int64_t nrows_x = ggml_nrows(src0);
  8046. const int64_t nrows_y = src0->ne[1];
  8047. float scale = 1.0f;
  8048. float max_bias = 0.0f;
  8049. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  8050. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  8051. // positions tensor
  8052. float * src2_dd = nullptr;
  8053. ggml_tensor * src2 = dst->src[2];
  8054. const bool use_src2 = src2 != nullptr;
  8055. if (use_src2) {
  8056. src2_dd = (float *)src2->data;
  8057. }
  8058. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  8059. }
  8060. static void ggml_cuda_op_scale(
  8061. ggml_backend_cuda_context & ctx,
  8062. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8063. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8064. GGML_UNUSED(ctx);
  8065. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8066. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8067. float scale;
  8068. memcpy(&scale, dst->op_params, sizeof(float));
  8069. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  8070. CUDA_CHECK(cudaGetLastError());
  8071. GGML_UNUSED(src1);
  8072. GGML_UNUSED(dst);
  8073. GGML_UNUSED(src1_dd);
  8074. }
  8075. static void ggml_cuda_op_clamp(
  8076. ggml_backend_cuda_context & ctx,
  8077. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  8078. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  8079. GGML_UNUSED(ctx);
  8080. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8081. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8082. float min;
  8083. float max;
  8084. memcpy(&min, dst->op_params, sizeof(float));
  8085. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  8086. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  8087. CUDA_CHECK(cudaGetLastError());
  8088. GGML_UNUSED(src1);
  8089. GGML_UNUSED(dst);
  8090. GGML_UNUSED(src1_dd);
  8091. }
  8092. // TODO: remove this function
  8093. static void ggml_cuda_op_flatten(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  8094. GGML_ASSERT(!src0 || ggml_backend_buffer_is_cuda(src0->buffer));
  8095. GGML_ASSERT(!src1 || ggml_backend_buffer_is_cuda(src1->buffer));
  8096. GGML_ASSERT( ggml_backend_buffer_is_cuda(dst->buffer));
  8097. // dd = data device
  8098. float * src0_ddf = src0 ? (float *) src0->data : nullptr;
  8099. float * src1_ddf = src1 ? (float *) src1->data : nullptr;
  8100. float * dst_ddf = (float *) dst->data;
  8101. ggml_cuda_set_device(ctx.device);
  8102. // do the computation
  8103. op(ctx, src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, ctx.stream());
  8104. CUDA_CHECK(cudaGetLastError());
  8105. }
  8106. static void ggml_cuda_set_peer_access(const int n_tokens, int main_device) {
  8107. static bool peer_access_enabled = false;
  8108. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  8109. if (peer_access_enabled == enable_peer_access) {
  8110. return;
  8111. }
  8112. #ifdef NDEBUG
  8113. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  8114. ggml_cuda_set_device(id);
  8115. CUDA_CHECK(cudaDeviceSynchronize());
  8116. }
  8117. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  8118. ggml_cuda_set_device(id);
  8119. for (int id_other = 0; id_other < ggml_backend_cuda_get_device_count(); ++id_other) {
  8120. if (id == id_other) {
  8121. continue;
  8122. }
  8123. if (id != main_device && id_other != main_device) {
  8124. continue;
  8125. }
  8126. int can_access_peer;
  8127. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  8128. if (can_access_peer) {
  8129. if (enable_peer_access) {
  8130. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  8131. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  8132. CUDA_CHECK(err);
  8133. }
  8134. } else {
  8135. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  8136. if (err != cudaErrorPeerAccessNotEnabled) {
  8137. CUDA_CHECK(err);
  8138. }
  8139. }
  8140. }
  8141. }
  8142. }
  8143. #endif // NDEBUG
  8144. peer_access_enabled = enable_peer_access;
  8145. GGML_UNUSED(main_device);
  8146. }
  8147. static void ggml_cuda_op_mul_mat(
  8148. ggml_backend_cuda_context & ctx,
  8149. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  8150. const bool convert_src1_to_q8_1) {
  8151. const int64_t ne00 = src0->ne[0];
  8152. const int64_t ne01 = src0->ne[1];
  8153. const int64_t ne02 = src0->ne[2];
  8154. const int64_t ne03 = src0->ne[3];
  8155. const int64_t ne10 = src1->ne[0];
  8156. const int64_t ne11 = src1->ne[1];
  8157. const int64_t ne12 = src1->ne[2];
  8158. const int64_t ne13 = src1->ne[3];
  8159. const int64_t nrows1 = ggml_nrows(src1);
  8160. GGML_ASSERT(ne03 == ne13);
  8161. const int64_t ne0 = dst->ne[0];
  8162. const int64_t ne1 = dst->ne[1];
  8163. const int nb2 = dst->nb[2];
  8164. const int nb3 = dst->nb[3];
  8165. GGML_ASSERT(ggml_backend_buffer_is_cuda(dst->buffer));
  8166. GGML_ASSERT(ggml_backend_buffer_is_cuda(src1->buffer));
  8167. ggml_backend_cuda_buffer_context * src1_ctx = (ggml_backend_cuda_buffer_context *) src1->buffer->context;
  8168. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *) dst->buffer->context;
  8169. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  8170. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  8171. const int64_t i02_divisor = ne12 / ne02;
  8172. const size_t src0_ts = ggml_type_size(src0->type);
  8173. const size_t src0_bs = ggml_blck_size(src0->type);
  8174. const size_t q8_1_ts = sizeof(block_q8_1);
  8175. const size_t q8_1_bs = QK8_1;
  8176. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  8177. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  8178. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  8179. const bool split = ggml_backend_buffer_is_cuda_split(src0->buffer);
  8180. GGML_ASSERT(!(split && ne02 > 1));
  8181. GGML_ASSERT(!(split && ne03 > 1));
  8182. GGML_ASSERT(!(split && ne02 < ne12));
  8183. ggml_tensor_extra_gpu * src0_extra = split ? (ggml_tensor_extra_gpu *) src0->extra : nullptr;
  8184. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  8185. if (split) {
  8186. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8187. tensor_split = buft_ctx->tensor_split;
  8188. }
  8189. struct dev_data {
  8190. ggml_cuda_pool_alloc<char> src0_dd_alloc;
  8191. ggml_cuda_pool_alloc<float> src1_ddf_alloc;
  8192. ggml_cuda_pool_alloc<char> src1_ddq_alloc;
  8193. ggml_cuda_pool_alloc<float> dst_dd_alloc;
  8194. char * src0_dd = nullptr;
  8195. float * src1_ddf = nullptr; // float
  8196. char * src1_ddq = nullptr; // q8_1
  8197. float * dst_dd = nullptr;
  8198. int64_t row_low;
  8199. int64_t row_high;
  8200. };
  8201. dev_data dev[GGML_CUDA_MAX_DEVICES];
  8202. int used_devices = 0;
  8203. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  8204. // by default, use all rows
  8205. dev[id].row_low = 0;
  8206. dev[id].row_high = ne01;
  8207. // for multi GPU, get the row boundaries from tensor split
  8208. // and round to mul_mat_q tile sizes
  8209. if (split) {
  8210. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  8211. if (id != 0) {
  8212. dev[id].row_low = ne01*tensor_split[id];
  8213. if (dev[id].row_low < ne01) {
  8214. dev[id].row_low -= dev[id].row_low % rounding;
  8215. }
  8216. }
  8217. if (id != ggml_backend_cuda_get_device_count() - 1) {
  8218. dev[id].row_high = ne01*tensor_split[id + 1];
  8219. if (dev[id].row_high < ne01) {
  8220. dev[id].row_high -= dev[id].row_high % rounding;
  8221. }
  8222. }
  8223. }
  8224. }
  8225. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  8226. if ((!split && id != ctx.device) || dev[id].row_low == dev[id].row_high) {
  8227. continue;
  8228. }
  8229. used_devices++;
  8230. const bool src1_on_device = id == src1_ctx->device;
  8231. const bool dst_on_device = id == dst_ctx->device;
  8232. ggml_cuda_set_device(id);
  8233. cudaStream_t stream = ctx.stream(id, 0);
  8234. if (src0_is_contiguous) {
  8235. dev[id].src0_dd = split ? (char *) src0_extra->data_device[id] : (char *) src0->data;
  8236. } else {
  8237. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ctx.pool(id), ggml_nbytes(src0));
  8238. }
  8239. if (src1_on_device && src1_is_contiguous) {
  8240. dev[id].src1_ddf = (float *) src1->data;
  8241. } else {
  8242. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ctx.pool(id), ggml_nelements(src1));
  8243. }
  8244. if (convert_src1_to_q8_1) {
  8245. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(ctx.pool(id), nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  8246. if (src1_on_device && src1_is_contiguous) {
  8247. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  8248. CUDA_CHECK(cudaGetLastError());
  8249. }
  8250. }
  8251. if (dst_on_device) {
  8252. dev[id].dst_dd = (float *) dst->data;
  8253. } else {
  8254. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  8255. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(ctx.pool(id), size_dst_ddf);
  8256. }
  8257. }
  8258. // if multiple devices are used they need to wait for the main device
  8259. // here an event is recorded that signals that the main device has finished calculating the input data
  8260. if (split && used_devices > 1) {
  8261. ggml_cuda_set_device(ctx.device);
  8262. CUDA_CHECK(cudaEventRecord(src0_extra->events[ctx.device][0], ctx.stream()));
  8263. }
  8264. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  8265. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  8266. const int64_t is = split ? (src1_col_0/src1_col_stride) % GGML_CUDA_MAX_STREAMS : 0;
  8267. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  8268. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  8269. if ((!split && id != ctx.device) || dev[id].row_low == dev[id].row_high) {
  8270. continue;
  8271. }
  8272. const bool src1_on_device = id == src1_ctx->device;
  8273. const bool dst_on_device = id == dst_ctx->device;
  8274. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  8275. ggml_cuda_set_device(id);
  8276. cudaStream_t stream = ctx.stream(id, is);
  8277. // wait for main GPU data if necessary
  8278. if (split && (id != ctx.device || is != 0)) {
  8279. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[ctx.device][0], 0));
  8280. }
  8281. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  8282. const int64_t i03 = i0 / ne12;
  8283. const int64_t i02 = i0 % ne12;
  8284. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  8285. // for split tensors the data begins at i0 == i0_offset_low
  8286. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  8287. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  8288. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  8289. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  8290. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  8291. // in that case an offset on dst_ddf_i is needed
  8292. if (id == ctx.device) {
  8293. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  8294. }
  8295. // copy src0, src1 to device if necessary
  8296. if (src1_is_contiguous) {
  8297. if (id != ctx.device) {
  8298. if (convert_src1_to_q8_1) {
  8299. char * src1_ddq_i_source = dev[ctx.device].src1_ddq + src1_ddq_i_offset;
  8300. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, ctx.device,
  8301. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  8302. } else {
  8303. float * src1_ddf_i_source = (float *) src1->data;
  8304. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  8305. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, ctx.device,
  8306. src1_ncols*ne10*sizeof(float), stream));
  8307. }
  8308. }
  8309. } else if (src1_on_device && !src1_is_contiguous) {
  8310. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  8311. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  8312. } else {
  8313. GGML_ASSERT(false);
  8314. }
  8315. if (convert_src1_to_q8_1 && !src1_is_contiguous) {
  8316. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  8317. CUDA_CHECK(cudaGetLastError());
  8318. }
  8319. if (src1_col_0 == 0 && !src0_is_contiguous && i02 % i02_divisor == 0) {
  8320. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  8321. }
  8322. // do the computation
  8323. op(ctx, src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  8324. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  8325. CUDA_CHECK(cudaGetLastError());
  8326. // copy dst to host or other device if necessary
  8327. if (!dst_on_device) {
  8328. void * dst_off_device = dst->data;
  8329. if (split) {
  8330. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  8331. // dst is NOT transposed.
  8332. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  8333. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  8334. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  8335. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  8336. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  8337. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  8338. #if !defined(GGML_USE_HIPBLAS)
  8339. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  8340. cudaMemcpy3DPeerParms p = {};
  8341. p.dstDevice = ctx.device;
  8342. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  8343. p.srcDevice = id;
  8344. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  8345. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  8346. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  8347. #else
  8348. // HIP does not support cudaMemcpy3DPeerAsync or vmm pools
  8349. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  8350. dst_dd_i, row_diff*sizeof(float),
  8351. row_diff*sizeof(float), src1_ncols,
  8352. cudaMemcpyDeviceToDevice, stream));
  8353. #endif
  8354. } else {
  8355. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  8356. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  8357. dhf_dst_i += src1_col_0*ne0;
  8358. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), cudaMemcpyDeviceToDevice, stream));
  8359. }
  8360. }
  8361. // add event for the main device to wait on until other device is done
  8362. if (split && (id != ctx.device || is != 0)) {
  8363. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  8364. }
  8365. }
  8366. }
  8367. }
  8368. // main device waits for all other devices to be finished
  8369. if (split && ggml_backend_cuda_get_device_count() > 1) {
  8370. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  8371. is_max = is_max <= GGML_CUDA_MAX_STREAMS ? is_max : GGML_CUDA_MAX_STREAMS;
  8372. ggml_cuda_set_device(ctx.device);
  8373. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  8374. if (dev[id].row_low == dev[id].row_high) {
  8375. continue;
  8376. }
  8377. for (int64_t is = 0; is < is_max; ++is) {
  8378. CUDA_CHECK(cudaStreamWaitEvent(ctx.stream(), src0_extra->events[id][is], 0));
  8379. }
  8380. }
  8381. }
  8382. }
  8383. static void ggml_cuda_repeat(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8384. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_repeat);
  8385. }
  8386. static void ggml_cuda_get_rows(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8387. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_get_rows);
  8388. }
  8389. static void ggml_cuda_add(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8390. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_add);
  8391. }
  8392. static void ggml_cuda_acc(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8393. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_acc);
  8394. }
  8395. static void ggml_cuda_mul(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8396. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_mul);
  8397. }
  8398. static void ggml_cuda_div(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8399. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_div);
  8400. }
  8401. static void ggml_cuda_gelu(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8402. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_gelu);
  8403. }
  8404. static void ggml_cuda_silu(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8405. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_silu);
  8406. }
  8407. static void ggml_cuda_gelu_quick(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8408. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_gelu_quick);
  8409. }
  8410. static void ggml_cuda_tanh(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8411. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_tanh);
  8412. }
  8413. static void ggml_cuda_relu(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8414. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_relu);
  8415. }
  8416. static void ggml_cuda_hardsigmoid(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8417. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_hardsigmoid);
  8418. }
  8419. static void ggml_cuda_hardswish(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8420. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_hardswish);
  8421. }
  8422. static void ggml_cuda_leaky_relu(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8423. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_leaky_relu);
  8424. }
  8425. static void ggml_cuda_sqr(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8426. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_sqr);
  8427. }
  8428. static void ggml_cuda_norm(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8429. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_norm);
  8430. }
  8431. static void ggml_cuda_group_norm(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8432. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_group_norm);
  8433. }
  8434. static void ggml_cuda_concat(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8435. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_concat);
  8436. }
  8437. static void ggml_cuda_upscale(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8438. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_upscale);
  8439. }
  8440. static void ggml_cuda_pad(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8441. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_pad);
  8442. }
  8443. static void ggml_cuda_arange(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8444. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_arange);
  8445. }
  8446. static void ggml_cuda_timestep_embedding(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8447. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_timestep_embedding);
  8448. }
  8449. static void ggml_cuda_rms_norm(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8450. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_rms_norm);
  8451. }
  8452. static void ggml_cuda_mul_mat_vec_p021(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8453. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  8454. GGML_ASSERT(ggml_backend_buffer_is_cuda(src0->buffer));
  8455. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  8456. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  8457. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8458. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8459. const int64_t ne00 = src0->ne[0];
  8460. const int64_t ne01 = src0->ne[1];
  8461. const int64_t ne02 = src0->ne[2];
  8462. const int64_t ne12 = src1->ne[2];
  8463. ggml_cuda_set_device(ctx.device);
  8464. cudaStream_t main_stream = ctx.stream();
  8465. void * src0_ddq = src0->data;
  8466. float * src1_ddf = (float *) src1->data;
  8467. float * dst_ddf = (float *) dst->data;
  8468. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8469. }
  8470. static void ggml_cuda_mul_mat_vec_nc(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8471. GGML_ASSERT(!ggml_is_transposed(src0));
  8472. GGML_ASSERT(!ggml_is_transposed(src1));
  8473. GGML_ASSERT(!ggml_is_permuted(src0));
  8474. GGML_ASSERT(ggml_backend_buffer_is_cuda(src0->buffer));
  8475. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8476. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8477. const int64_t ne00 = src0->ne[0];
  8478. const int64_t ne01 = src0->ne[1];
  8479. const int64_t ne02 = src0->ne[2];
  8480. const int64_t nb01 = src0->nb[1];
  8481. const int64_t nb02 = src0->nb[2];
  8482. const int64_t ne12 = src1->ne[2];
  8483. ggml_cuda_set_device(ctx.device);
  8484. cudaStream_t main_stream = ctx.stream();
  8485. void * src0_ddq = src0->data;
  8486. float * src1_ddf = (float *) src1->data;
  8487. float * dst_ddf = (float *) dst->data;
  8488. const int64_t row_stride_x = nb01 / sizeof(half);
  8489. const int64_t channel_stride_x = nb02 / sizeof(half);
  8490. 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);
  8491. }
  8492. static __global__ void k_compute_batched_ptrs(
  8493. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8494. const void ** ptrs_src, void ** ptrs_dst,
  8495. int64_t ne12, int64_t ne13,
  8496. int64_t ne23,
  8497. size_t nb02, size_t nb03,
  8498. size_t nb12, size_t nb13,
  8499. size_t nbd2, size_t nbd3,
  8500. int64_t r2, int64_t r3) {
  8501. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8502. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8503. if (i13 >= ne13 || i12 >= ne12) {
  8504. return;
  8505. }
  8506. int64_t i03 = i13 / r3;
  8507. int64_t i02 = i12 / r2;
  8508. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8509. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8510. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8511. }
  8512. static void ggml_cuda_mul_mat_batched_cublas(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8513. GGML_ASSERT(!ggml_is_transposed(src0));
  8514. GGML_ASSERT(!ggml_is_transposed(src1));
  8515. GGML_ASSERT(ggml_backend_buffer_is_cuda(src0->buffer));
  8516. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8517. GGML_TENSOR_BINARY_OP_LOCALS
  8518. const int64_t ne_dst = ggml_nelements(dst);
  8519. ggml_cuda_set_device(ctx.device);
  8520. cudaStream_t main_stream = ctx.stream();
  8521. CUBLAS_CHECK(cublasSetStream(ctx.cublas_handle(), main_stream));
  8522. void * src0_ddq = src0->data;
  8523. half * src0_f16 = (half *) src0_ddq;
  8524. float * src1_ddf = (float *) src1->data;
  8525. float * dst_ddf = (float *) dst->data;
  8526. // convert src1 to fp16
  8527. ggml_cuda_pool_alloc<half> src1_f16_alloc(ctx.pool());
  8528. if (src1->type != GGML_TYPE_F16) {
  8529. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8530. const int64_t ne_src1 = ggml_nelements(src1);
  8531. src1_f16_alloc.alloc(ne_src1);
  8532. GGML_ASSERT(to_fp16_cuda != nullptr);
  8533. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8534. }
  8535. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8536. ggml_cuda_pool_alloc<half> dst_f16(ctx.pool());
  8537. char * dst_t;
  8538. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8539. cudaDataType_t cu_data_type = CUDA_R_16F;
  8540. // dst strides
  8541. size_t nbd2 = dst->nb[2];
  8542. size_t nbd3 = dst->nb[3];
  8543. const half alpha_f16 = 1.0f;
  8544. const half beta_f16 = 0.0f;
  8545. const float alpha_f32 = 1.0f;
  8546. const float beta_f32 = 0.0f;
  8547. const void * alpha = &alpha_f16;
  8548. const void * beta = &beta_f16;
  8549. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8550. dst_t = (char *) dst_f16.alloc(ne_dst);
  8551. nbd2 /= sizeof(float) / sizeof(half);
  8552. nbd3 /= sizeof(float) / sizeof(half);
  8553. } else {
  8554. dst_t = (char *) dst_ddf;
  8555. cu_compute_type = CUBLAS_COMPUTE_32F;
  8556. cu_data_type = CUDA_R_32F;
  8557. alpha = &alpha_f32;
  8558. beta = &beta_f32;
  8559. }
  8560. GGML_ASSERT(ne12 % ne02 == 0);
  8561. GGML_ASSERT(ne13 % ne03 == 0);
  8562. // broadcast factors
  8563. const int64_t r2 = ne12/ne02;
  8564. const int64_t r3 = ne13/ne03;
  8565. #if 0
  8566. // use cublasGemmEx
  8567. {
  8568. for (int i13 = 0; i13 < ne13; ++i13) {
  8569. for (int i12 = 0; i12 < ne12; ++i12) {
  8570. int i03 = i13 / r3;
  8571. int i02 = i12 / r2;
  8572. CUBLAS_CHECK(
  8573. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8574. ne01, ne11, ne10,
  8575. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8576. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8577. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8578. cu_compute_type,
  8579. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8580. }
  8581. }
  8582. }
  8583. #else
  8584. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8585. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8586. // use cublasGemmStridedBatchedEx
  8587. CUBLAS_CHECK(
  8588. cublasGemmStridedBatchedEx(ctx.cublas_handle(), CUBLAS_OP_T, CUBLAS_OP_N,
  8589. ne01, ne11, ne10,
  8590. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8591. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8592. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8593. ne12*ne13,
  8594. cu_compute_type,
  8595. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8596. } else {
  8597. // use cublasGemmBatchedEx
  8598. const int ne23 = ne12*ne13;
  8599. ggml_cuda_pool_alloc<const void *> ptrs_src(ctx.pool(), 2*ne23);
  8600. ggml_cuda_pool_alloc< void *> ptrs_dst(ctx.pool(), 1*ne23);
  8601. dim3 block_dims(ne13, ne12);
  8602. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8603. src0_f16, src1_f16, dst_t,
  8604. ptrs_src.get(), ptrs_dst.get(),
  8605. ne12, ne13,
  8606. ne23,
  8607. nb02, nb03,
  8608. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8609. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8610. nbd2, nbd3,
  8611. r2, r3);
  8612. CUDA_CHECK(cudaGetLastError());
  8613. CUBLAS_CHECK(
  8614. cublasGemmBatchedEx(ctx.cublas_handle(), CUBLAS_OP_T, CUBLAS_OP_N,
  8615. ne01, ne11, ne10,
  8616. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8617. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8618. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8619. ne23,
  8620. cu_compute_type,
  8621. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8622. }
  8623. #endif
  8624. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8625. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8626. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8627. }
  8628. }
  8629. static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8630. const bool split = ggml_backend_buffer_is_cuda_split(src0->buffer);
  8631. int64_t min_compute_capability = INT_MAX;
  8632. bool any_pascal_with_slow_fp16 = false;
  8633. if (split) {
  8634. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8635. auto & tensor_split = buft_ctx->tensor_split;
  8636. for (int id = 0; id < ggml_backend_cuda_get_device_count(); ++id) {
  8637. // skip devices that are not going to do any work:
  8638. if (tensor_split[id] >= (id + 1 < ggml_backend_cuda_get_device_count() ? tensor_split[id + 1] : 1.0f)) {
  8639. continue;
  8640. }
  8641. if (min_compute_capability > get_cuda_global_info().devices[id].cc) {
  8642. min_compute_capability = get_cuda_global_info().devices[id].cc;
  8643. }
  8644. if (get_cuda_global_info().devices[id].cc == 610) {
  8645. any_pascal_with_slow_fp16 = true;
  8646. }
  8647. }
  8648. } else {
  8649. min_compute_capability = get_cuda_global_info().devices[ctx.device].cc;
  8650. any_pascal_with_slow_fp16 = get_cuda_global_info().devices[ctx.device].cc == 610;
  8651. }
  8652. // check data types and tensor shapes for custom matrix multiplication kernels:
  8653. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8654. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8655. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8656. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8657. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8658. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8659. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8660. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8661. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8662. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8663. #ifdef CUDA_USE_TENSOR_CORES
  8664. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8665. #endif // CUDA_USE_TENSOR_CORES
  8666. #else
  8667. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8668. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8669. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8670. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8671. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8672. #ifdef CUDA_USE_TENSOR_CORES
  8673. // when tensor cores are available, use them for large batch size
  8674. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8675. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8676. #endif // CUDA_USE_TENSOR_CORES
  8677. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8678. // if mmvq is available it's a better choice than dmmv:
  8679. #ifndef GGML_CUDA_FORCE_DMMV
  8680. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8681. #endif // GGML_CUDA_FORCE_DMMV
  8682. // debug helpers
  8683. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8684. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8685. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8686. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8687. //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);
  8688. //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);
  8689. if (!split && !fp16_performance_good && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  8690. // KQ single-batch
  8691. ggml_cuda_mul_mat_vec_p021(ctx, src0, src1, dst);
  8692. } else if (!split && !fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  8693. // KQV single-batch
  8694. ggml_cuda_mul_mat_vec_nc(ctx, src0, src1, dst);
  8695. } else if (!split && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  8696. // KQ + KQV multi-batch
  8697. ggml_cuda_mul_mat_batched_cublas(ctx, src0, src1, dst);
  8698. } else if (use_dequantize_mul_mat_vec) {
  8699. ggml_cuda_op_mul_mat(ctx, src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8700. } else if (use_mul_mat_vec_q) {
  8701. ggml_cuda_op_mul_mat(ctx, src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8702. } else if (use_mul_mat_q) {
  8703. ggml_cuda_op_mul_mat(ctx, src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8704. } else {
  8705. ggml_cuda_op_mul_mat(ctx, src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8706. }
  8707. }
  8708. #if 0
  8709. template<typename ... Srcs>
  8710. static __global__ void k_compute_batched_ptrs_id(
  8711. const void ** ptrs_src, void ** ptrs_dst,
  8712. int ne12, int ne13,
  8713. int ne23,
  8714. int nb02, int nb03,
  8715. int nb12, int nb13,
  8716. int nb2, int nb3,
  8717. int r2, int r3,
  8718. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8719. const half * src1_f16, half * dst_f16,
  8720. const int32_t * ids, const int id,
  8721. Srcs... src0s) {
  8722. int i = ids[id];
  8723. half * src0_f16;
  8724. const void * srcs_ar[] = { (const half *) src0s... };
  8725. if (src0_type == GGML_TYPE_F16) {
  8726. src0_f16 = (half *) srcs_ar[i];
  8727. } else {
  8728. src0_f16 = src0_as_f16;
  8729. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8730. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8731. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8732. }
  8733. }
  8734. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8735. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8736. if (i13 >= ne13 || i12 >= ne12) {
  8737. return;
  8738. }
  8739. int i03 = i13 / r3;
  8740. int i02 = i12 / r2;
  8741. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8742. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8743. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8744. }
  8745. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8746. const struct ggml_tensor * ids = dst->src[0];
  8747. const struct ggml_tensor * src1 = dst->src[1];
  8748. const struct ggml_tensor * src00 = dst->src[2];
  8749. const int id = dst->op_params[0];
  8750. GGML_ASSERT(!ggml_is_transposed(src00));
  8751. GGML_ASSERT(!ggml_is_transposed(src1));
  8752. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8753. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8754. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8755. const int64_t ne01 = src00->ne[1];
  8756. const int64_t ne02 = src00->ne[2];
  8757. const int64_t ne03 = src00->ne[3];
  8758. //const int64_t nb01 = src00->nb[1];
  8759. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8760. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8761. const int64_t ne10 = src1->ne[0];
  8762. const int64_t ne11 = src1->ne[1];
  8763. const int64_t ne12 = src1->ne[2];
  8764. const int64_t ne13 = src1->ne[3];
  8765. //const int64_t nb11 = src1->nb[1];
  8766. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8767. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8768. const int64_t ne1 = ggml_nelements(src1);
  8769. const int64_t ne = ggml_nelements(dst);
  8770. ggml_cuda_set_device(g_main_device);
  8771. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8772. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8773. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8774. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8775. //half * src0_as_f16 = (half *) src0_ddq;
  8776. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8777. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8778. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8779. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8780. // convert src1 to fp16
  8781. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8782. GGML_ASSERT(to_fp16_cuda != nullptr);
  8783. size_t src1_as = 0;
  8784. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8785. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8786. size_t dst_as = 0;
  8787. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8788. GGML_ASSERT(ne12 % ne02 == 0);
  8789. GGML_ASSERT(ne13 % ne03 == 0);
  8790. // broadcast factors
  8791. const int64_t r2 = ne12/ne02;
  8792. const int64_t r3 = ne13/ne03;
  8793. const half alpha_f16 = 1.0f;
  8794. const half beta_f16 = 0.0f;
  8795. // use cublasGemmBatchedEx
  8796. const int ne23 = ne12*ne13;
  8797. const void ** ptrs_src = nullptr;
  8798. void ** ptrs_dst = nullptr;
  8799. size_t ptrs_src_s = 0;
  8800. size_t ptrs_dst_s = 0;
  8801. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8802. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8803. int64_t src0_ne = ggml_nelements(src00);
  8804. half * src0_as_f16 = nullptr;
  8805. size_t src0_as = 0;
  8806. if (src00->type != GGML_TYPE_F16) {
  8807. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8808. }
  8809. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8810. dim3 block_dims(ne13, ne12);
  8811. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8812. ptrs_src, ptrs_dst,
  8813. ne12, ne13,
  8814. ne23,
  8815. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8816. nb12, nb13,
  8817. dst->nb[2], dst->nb[3],
  8818. r2, r3,
  8819. src00->type, src0_as_f16, src0_ne,
  8820. src1_as_f16, dst_f16,
  8821. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8822. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8823. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8824. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8825. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8826. );
  8827. CUDA_CHECK(cudaGetLastError());
  8828. CUBLAS_CHECK(
  8829. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8830. ne01, ne11, ne10,
  8831. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8832. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8833. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8834. ne23,
  8835. CUBLAS_COMPUTE_16F,
  8836. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8837. if (src0_as != 0) {
  8838. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8839. }
  8840. if (ptrs_src_s != 0) {
  8841. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8842. }
  8843. if (ptrs_dst_s != 0) {
  8844. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8845. }
  8846. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8847. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8848. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8849. ggml_cuda_pool_free(dst_f16, dst_as);
  8850. }
  8851. #endif
  8852. static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8853. #if 0
  8854. ggml_cuda_mul_mat_id_cublas(dst);
  8855. // TODO: mmq/mmv support
  8856. #endif
  8857. cudaStream_t stream = ctx.stream();
  8858. const size_t nb11 = src1->nb[1];
  8859. const size_t nb1 = dst->nb[1];
  8860. const struct ggml_tensor * ids = src0;
  8861. const int32_t id = ((int32_t *) dst->op_params)[0];
  8862. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  8863. std::vector<char> ids_host(ggml_nbytes(ids));
  8864. const char * ids_dev = (const char *) ids->data;
  8865. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  8866. CUDA_CHECK(cudaStreamSynchronize(stream));
  8867. ggml_tensor src1_row = *src1;
  8868. ggml_tensor dst_row = *dst;
  8869. char * src1_original = (char *) src1->data;
  8870. char * dst_original = (char *) dst->data;
  8871. if (src1->ne[1] == 1) {
  8872. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8873. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8874. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8875. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8876. src1_row.data = src1_original + i01*src1->nb[1];
  8877. dst_row.data = dst_original + i01*dst->nb[1];
  8878. ggml_cuda_mul_mat(ctx, src0_row, &src1_row, &dst_row);
  8879. }
  8880. } else {
  8881. ggml_cuda_pool_alloc<char> src1_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(src1));
  8882. ggml_cuda_pool_alloc<char> dst_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(dst));
  8883. src1_row.data = src1_contiguous.get();
  8884. dst_row.data = dst_contiguous.get();
  8885. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  8886. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8887. int64_t num_src1_rows = 0;
  8888. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8889. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8890. if (row_id_i != row_id) {
  8891. continue;
  8892. }
  8893. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8894. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  8895. nb11, cudaMemcpyDeviceToDevice, stream));
  8896. num_src1_rows++;
  8897. }
  8898. if (num_src1_rows == 0) {
  8899. continue;
  8900. }
  8901. src1_row.ne[1] = num_src1_rows;
  8902. dst_row.ne[1] = num_src1_rows;
  8903. src1_row.nb[1] = nb11;
  8904. src1_row.nb[2] = num_src1_rows*nb11;
  8905. src1_row.nb[3] = num_src1_rows*nb11;
  8906. dst_row.nb[1] = nb1;
  8907. dst_row.nb[2] = num_src1_rows*nb1;
  8908. dst_row.nb[3] = num_src1_rows*nb1;
  8909. ggml_cuda_mul_mat(ctx, src0_row, &src1_row, &dst_row);
  8910. num_src1_rows = 0;
  8911. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8912. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8913. if (row_id_i != row_id) {
  8914. continue;
  8915. }
  8916. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8917. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  8918. nb1, cudaMemcpyDeviceToDevice, stream));
  8919. num_src1_rows++;
  8920. }
  8921. }
  8922. }
  8923. }
  8924. static void ggml_cuda_scale(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8925. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_scale);
  8926. }
  8927. static void ggml_cuda_clamp(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8928. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_clamp);
  8929. }
  8930. static void ggml_cuda_cpy(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8931. const int64_t ne = ggml_nelements(src0);
  8932. GGML_ASSERT(ne == ggml_nelements(src1));
  8933. GGML_ASSERT(ggml_backend_buffer_is_cuda(src0->buffer));
  8934. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  8935. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  8936. const int64_t ne00 = src0->ne[0];
  8937. const int64_t ne01 = src0->ne[1];
  8938. const int64_t ne02 = src0->ne[2];
  8939. //GGML_ASSERT(src0->ne[3] == 1);
  8940. const int64_t nb00 = src0->nb[0];
  8941. const int64_t nb01 = src0->nb[1];
  8942. const int64_t nb02 = src0->nb[2];
  8943. const int64_t nb03 = src0->nb[3];
  8944. const int64_t ne10 = src1->ne[0];
  8945. const int64_t ne11 = src1->ne[1];
  8946. const int64_t ne12 = src1->ne[2];
  8947. //GGML_ASSERT(src1->ne[3] == 1);
  8948. const int64_t nb10 = src1->nb[0];
  8949. const int64_t nb11 = src1->nb[1];
  8950. const int64_t nb12 = src1->nb[2];
  8951. const int64_t nb13 = src1->nb[3];
  8952. ggml_cuda_set_device(ctx.device);
  8953. cudaStream_t main_stream = ctx.stream();
  8954. char * src0_ddc = (char *) src0->data;
  8955. char * src1_ddc = (char *) src1->data;
  8956. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  8957. 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);
  8958. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  8959. 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);
  8960. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  8961. 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);
  8962. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  8963. 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);
  8964. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  8965. 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);
  8966. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q5_0) {
  8967. ggml_cpy_f32_q5_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8968. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_IQ4_NL) {
  8969. ggml_cpy_f32_iq4_nl_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8970. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q5_1) {
  8971. ggml_cpy_f32_q5_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8972. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  8973. 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);
  8974. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  8975. 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);
  8976. } else {
  8977. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  8978. ggml_type_name(src0->type), ggml_type_name(src1->type));
  8979. GGML_ASSERT(false);
  8980. }
  8981. GGML_UNUSED(dst);
  8982. }
  8983. static void ggml_cuda_dup(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8984. // TODO: why do we pass dst as src1 here?
  8985. ggml_cuda_cpy(ctx, src0, dst, nullptr);
  8986. GGML_UNUSED(src1);
  8987. }
  8988. static void ggml_cuda_diag_mask_inf(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8989. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  8990. }
  8991. static void ggml_cuda_soft_max(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8992. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_soft_max);
  8993. }
  8994. static void ggml_cuda_rope(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8995. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8996. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_rope);
  8997. }
  8998. static void ggml_cuda_alibi(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8999. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_alibi);
  9000. }
  9001. static void ggml_cuda_pool2d(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9002. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_pool2d);
  9003. }
  9004. static void ggml_cuda_im2col(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9005. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_im2col);
  9006. }
  9007. static void ggml_cuda_sum_rows(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9008. GGML_ASSERT(ggml_is_contiguous(src0));
  9009. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_sum_rows);
  9010. }
  9011. static void ggml_cuda_argsort(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9012. GGML_ASSERT(ggml_is_contiguous(src0));
  9013. ggml_cuda_op_flatten(ctx, src0, src1, dst, ggml_cuda_op_argsort);
  9014. }
  9015. static void ggml_cuda_nop(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  9016. GGML_UNUSED(ctx);
  9017. GGML_UNUSED(src0);
  9018. GGML_UNUSED(src1);
  9019. GGML_UNUSED(dst);
  9020. }
  9021. static bool ggml_cuda_compute_forward(ggml_backend_cuda_context & ctx, struct ggml_tensor * tensor) {
  9022. // FIXME: where should this be?
  9023. if (tensor->src[0] != nullptr && ggml_backend_buffer_is_cuda_split(tensor->src[0]->buffer)) {
  9024. ggml_cuda_set_peer_access(tensor->src[1]->ne[1], ctx.device);
  9025. }
  9026. ggml_cuda_func_t func;
  9027. switch (tensor->op) {
  9028. case GGML_OP_REPEAT:
  9029. func = ggml_cuda_repeat;
  9030. break;
  9031. case GGML_OP_GET_ROWS:
  9032. func = ggml_cuda_get_rows;
  9033. break;
  9034. case GGML_OP_DUP:
  9035. func = ggml_cuda_dup;
  9036. break;
  9037. case GGML_OP_ADD:
  9038. func = ggml_cuda_add;
  9039. break;
  9040. case GGML_OP_ACC:
  9041. func = ggml_cuda_acc;
  9042. break;
  9043. case GGML_OP_MUL:
  9044. func = ggml_cuda_mul;
  9045. break;
  9046. case GGML_OP_DIV:
  9047. func = ggml_cuda_div;
  9048. break;
  9049. case GGML_OP_UNARY:
  9050. switch (ggml_get_unary_op(tensor)) {
  9051. case GGML_UNARY_OP_GELU:
  9052. func = ggml_cuda_gelu;
  9053. break;
  9054. case GGML_UNARY_OP_SILU:
  9055. func = ggml_cuda_silu;
  9056. break;
  9057. case GGML_UNARY_OP_GELU_QUICK:
  9058. func = ggml_cuda_gelu_quick;
  9059. break;
  9060. case GGML_UNARY_OP_TANH:
  9061. func = ggml_cuda_tanh;
  9062. break;
  9063. case GGML_UNARY_OP_RELU:
  9064. func = ggml_cuda_relu;
  9065. break;
  9066. case GGML_UNARY_OP_HARDSIGMOID:
  9067. func = ggml_cuda_hardsigmoid;
  9068. break;
  9069. case GGML_UNARY_OP_HARDSWISH:
  9070. func = ggml_cuda_hardswish;
  9071. break;
  9072. default:
  9073. return false;
  9074. }
  9075. break;
  9076. case GGML_OP_NORM:
  9077. func = ggml_cuda_norm;
  9078. break;
  9079. case GGML_OP_GROUP_NORM:
  9080. func = ggml_cuda_group_norm;
  9081. break;
  9082. case GGML_OP_CONCAT:
  9083. func = ggml_cuda_concat;
  9084. break;
  9085. case GGML_OP_UPSCALE:
  9086. func = ggml_cuda_upscale;
  9087. break;
  9088. case GGML_OP_PAD:
  9089. func = ggml_cuda_pad;
  9090. break;
  9091. case GGML_OP_ARANGE:
  9092. func = ggml_cuda_arange;
  9093. break;
  9094. case GGML_OP_TIMESTEP_EMBEDDING:
  9095. func = ggml_cuda_timestep_embedding;
  9096. break;
  9097. case GGML_OP_LEAKY_RELU:
  9098. func = ggml_cuda_leaky_relu;
  9099. break;
  9100. case GGML_OP_RMS_NORM:
  9101. func = ggml_cuda_rms_norm;
  9102. break;
  9103. case GGML_OP_MUL_MAT:
  9104. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  9105. 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]);
  9106. return false;
  9107. } else {
  9108. func = ggml_cuda_mul_mat;
  9109. }
  9110. break;
  9111. case GGML_OP_MUL_MAT_ID:
  9112. func = ggml_cuda_mul_mat_id;
  9113. break;
  9114. case GGML_OP_SCALE:
  9115. func = ggml_cuda_scale;
  9116. break;
  9117. case GGML_OP_SQR:
  9118. func = ggml_cuda_sqr;
  9119. break;
  9120. case GGML_OP_CLAMP:
  9121. func = ggml_cuda_clamp;
  9122. break;
  9123. case GGML_OP_CPY:
  9124. func = ggml_cuda_cpy;
  9125. break;
  9126. case GGML_OP_CONT:
  9127. func = ggml_cuda_dup;
  9128. break;
  9129. case GGML_OP_NONE:
  9130. case GGML_OP_RESHAPE:
  9131. case GGML_OP_VIEW:
  9132. case GGML_OP_PERMUTE:
  9133. case GGML_OP_TRANSPOSE:
  9134. func = ggml_cuda_nop;
  9135. break;
  9136. case GGML_OP_DIAG_MASK_INF:
  9137. func = ggml_cuda_diag_mask_inf;
  9138. break;
  9139. case GGML_OP_SOFT_MAX:
  9140. func = ggml_cuda_soft_max;
  9141. break;
  9142. case GGML_OP_ROPE:
  9143. func = ggml_cuda_rope;
  9144. break;
  9145. case GGML_OP_ALIBI:
  9146. func = ggml_cuda_alibi;
  9147. break;
  9148. case GGML_OP_IM2COL:
  9149. func = ggml_cuda_im2col;
  9150. break;
  9151. case GGML_OP_POOL_2D:
  9152. func = ggml_cuda_pool2d;
  9153. break;
  9154. case GGML_OP_SUM_ROWS:
  9155. func = ggml_cuda_sum_rows;
  9156. break;
  9157. case GGML_OP_ARGSORT:
  9158. func = ggml_cuda_argsort;
  9159. break;
  9160. default:
  9161. return false;
  9162. }
  9163. func(ctx, tensor->src[0], tensor->src[1], tensor);
  9164. return true;
  9165. }
  9166. ////////////////////////////////////////////////////////////////////////////////
  9167. // backend
  9168. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9169. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9170. return cuda_ctx->name.c_str();
  9171. }
  9172. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9173. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9174. delete cuda_ctx;
  9175. delete backend;
  9176. }
  9177. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9178. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9179. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9180. }
  9181. 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) {
  9182. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9183. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  9184. GGML_ASSERT(buf->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9185. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, cuda_ctx->stream()));
  9186. }
  9187. 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) {
  9188. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9189. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  9190. GGML_ASSERT(buf->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9191. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, cuda_ctx->stream()));
  9192. }
  9193. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend_src, ggml_backend_t backend_dst, const ggml_tensor * src, ggml_tensor * dst) {
  9194. GGML_ASSERT(ggml_backend_is_cuda(backend_src) || ggml_backend_is_cuda(backend_dst));
  9195. ggml_backend_buffer_t buf_src = src->view_src ? src->view_src->buffer : src->buffer;
  9196. ggml_backend_buffer_t buf_dst = dst->view_src ? dst->view_src->buffer : dst->buffer;
  9197. if (!ggml_backend_buffer_is_cuda(src->buffer)) {
  9198. return false;
  9199. }
  9200. if (!ggml_backend_buffer_is_cuda(dst->buffer)) {
  9201. return false;
  9202. }
  9203. // device -> device
  9204. ggml_backend_cuda_context * cuda_ctx_src = (ggml_backend_cuda_context *)backend_src->context;
  9205. ggml_backend_cuda_context * cuda_ctx_dst = (ggml_backend_cuda_context *)backend_dst->context;
  9206. if (backend_src != backend_dst) {
  9207. ggml_backend_cuda_buffer_context * buf_ctx_src = (ggml_backend_cuda_buffer_context *)buf_src->context;
  9208. ggml_backend_cuda_buffer_context * buf_ctx_dst = (ggml_backend_cuda_buffer_context *)buf_dst->context;
  9209. GGML_ASSERT(cuda_ctx_src->device == buf_ctx_src->device);
  9210. GGML_ASSERT(cuda_ctx_dst->device == buf_ctx_dst->device);
  9211. // copy on src stream
  9212. if (cuda_ctx_src->device == cuda_ctx_dst->device) {
  9213. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, cuda_ctx_dst->stream()));
  9214. } else {
  9215. #ifdef GGML_CUDA_NO_PEER_COPY
  9216. return false;
  9217. #else
  9218. CUDA_CHECK(cudaMemcpyPeerAsync(dst->data, cuda_ctx_dst->device, src->data, cuda_ctx_src->device, ggml_nbytes(dst), cuda_ctx_src->stream()));
  9219. #endif
  9220. }
  9221. // record event on src stream
  9222. if (!cuda_ctx_src->copy_event) {
  9223. ggml_cuda_set_device(cuda_ctx_src->device);
  9224. CUDA_CHECK(cudaEventCreateWithFlags(&cuda_ctx_src->copy_event, cudaEventDisableTiming));
  9225. }
  9226. CUDA_CHECK(cudaEventRecord(cuda_ctx_src->copy_event, cuda_ctx_src->stream()));
  9227. // wait on dst stream for the copy to complete
  9228. CUDA_CHECK(cudaStreamWaitEvent(cuda_ctx_dst->stream(), cuda_ctx_src->copy_event, 0));
  9229. } else {
  9230. // src and dst are on the same backend
  9231. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, cuda_ctx_dst->stream()));
  9232. }
  9233. return true;
  9234. }
  9235. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9236. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9237. CUDA_CHECK(cudaStreamSynchronize(cuda_ctx->stream()));
  9238. GGML_UNUSED(backend);
  9239. }
  9240. GGML_CALL static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9241. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9242. ggml_cuda_set_device(cuda_ctx->device);
  9243. for (int i = 0; i < cgraph->n_nodes; i++) {
  9244. ggml_tensor * node = cgraph->nodes[i];
  9245. 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) {
  9246. continue;
  9247. }
  9248. #ifndef NDEBUG
  9249. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9250. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9251. if (node->src[j] != nullptr) {
  9252. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9253. }
  9254. }
  9255. #endif
  9256. bool ok = ggml_cuda_compute_forward(*cuda_ctx, node);
  9257. if (!ok) {
  9258. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9259. }
  9260. GGML_ASSERT(ok);
  9261. }
  9262. return GGML_STATUS_SUCCESS;
  9263. }
  9264. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9265. switch (op->op) {
  9266. case GGML_OP_UNARY:
  9267. switch (ggml_get_unary_op(op)) {
  9268. case GGML_UNARY_OP_GELU:
  9269. case GGML_UNARY_OP_SILU:
  9270. case GGML_UNARY_OP_RELU:
  9271. case GGML_UNARY_OP_HARDSIGMOID:
  9272. case GGML_UNARY_OP_HARDSWISH:
  9273. case GGML_UNARY_OP_GELU_QUICK:
  9274. case GGML_UNARY_OP_TANH:
  9275. return true;
  9276. default:
  9277. return false;
  9278. }
  9279. break;
  9280. case GGML_OP_MUL_MAT:
  9281. case GGML_OP_MUL_MAT_ID:
  9282. {
  9283. struct ggml_tensor * a;
  9284. struct ggml_tensor * b;
  9285. if (op->op == GGML_OP_MUL_MAT) {
  9286. a = op->src[0];
  9287. b = op->src[1];
  9288. } else {
  9289. a = op->src[2];
  9290. b = op->src[1];
  9291. }
  9292. if (a->ne[3] != b->ne[3]) {
  9293. return false;
  9294. }
  9295. ggml_type a_type = a->type;
  9296. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  9297. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ3_S ||
  9298. a_type == GGML_TYPE_IQ2_S || a_type == GGML_TYPE_IQ4_XS) {
  9299. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9300. return false;
  9301. }
  9302. }
  9303. return true;
  9304. } break;
  9305. case GGML_OP_GET_ROWS:
  9306. {
  9307. switch (op->src[0]->type) {
  9308. case GGML_TYPE_F16:
  9309. case GGML_TYPE_F32:
  9310. case GGML_TYPE_Q4_0:
  9311. case GGML_TYPE_Q4_1:
  9312. case GGML_TYPE_Q5_0:
  9313. case GGML_TYPE_Q5_1:
  9314. case GGML_TYPE_Q8_0:
  9315. return true;
  9316. default:
  9317. return false;
  9318. }
  9319. } break;
  9320. case GGML_OP_CPY:
  9321. {
  9322. ggml_type src0_type = op->src[0]->type;
  9323. ggml_type src1_type = op->src[1]->type;
  9324. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9325. return true;
  9326. }
  9327. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9328. return true;
  9329. }
  9330. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9331. return true;
  9332. }
  9333. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9334. return true;
  9335. }
  9336. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9337. return true;
  9338. }
  9339. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q5_0) {
  9340. return true;
  9341. }
  9342. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q5_1) {
  9343. return true;
  9344. }
  9345. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_IQ4_NL) {
  9346. return true;
  9347. }
  9348. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9349. return true;
  9350. }
  9351. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9352. return true;
  9353. }
  9354. return false;
  9355. } break;
  9356. case GGML_OP_DUP:
  9357. case GGML_OP_REPEAT:
  9358. case GGML_OP_CONCAT:
  9359. {
  9360. ggml_type src0_type = op->src[0]->type;
  9361. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9362. } break;
  9363. case GGML_OP_NONE:
  9364. case GGML_OP_RESHAPE:
  9365. case GGML_OP_VIEW:
  9366. case GGML_OP_PERMUTE:
  9367. case GGML_OP_TRANSPOSE:
  9368. case GGML_OP_NORM:
  9369. case GGML_OP_ADD:
  9370. case GGML_OP_MUL:
  9371. case GGML_OP_DIV:
  9372. case GGML_OP_RMS_NORM:
  9373. case GGML_OP_SCALE:
  9374. case GGML_OP_SQR:
  9375. case GGML_OP_CLAMP:
  9376. case GGML_OP_CONT:
  9377. case GGML_OP_DIAG_MASK_INF:
  9378. case GGML_OP_SOFT_MAX:
  9379. case GGML_OP_ROPE:
  9380. case GGML_OP_ALIBI:
  9381. case GGML_OP_IM2COL:
  9382. case GGML_OP_POOL_2D:
  9383. case GGML_OP_SUM_ROWS:
  9384. case GGML_OP_ARGSORT:
  9385. case GGML_OP_ACC:
  9386. case GGML_OP_GROUP_NORM:
  9387. case GGML_OP_UPSCALE:
  9388. case GGML_OP_PAD:
  9389. case GGML_OP_ARANGE:
  9390. case GGML_OP_TIMESTEP_EMBEDDING:
  9391. case GGML_OP_LEAKY_RELU:
  9392. return true;
  9393. default:
  9394. return false;
  9395. }
  9396. GGML_UNUSED(backend);
  9397. }
  9398. GGML_CALL static bool ggml_backend_cuda_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  9399. const int min_batch_size = 32;
  9400. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS;
  9401. GGML_UNUSED(backend);
  9402. }
  9403. static ggml_backend_event_t ggml_backend_cuda_event_new(ggml_backend_t backend) {
  9404. #ifdef GGML_CUDA_NO_PEER_COPY
  9405. return nullptr;
  9406. #else
  9407. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9408. ggml_cuda_set_device(cuda_ctx->device);
  9409. cudaEvent_t event;
  9410. CUDA_CHECK(cudaEventCreateWithFlags(&event, cudaEventDisableTiming));
  9411. return new ggml_backend_event {
  9412. /* .backend = */ backend,
  9413. /* .context = */ event,
  9414. };
  9415. #endif
  9416. }
  9417. static void ggml_backend_cuda_event_free(ggml_backend_event_t event) {
  9418. CUDA_CHECK(cudaEventDestroy((cudaEvent_t)event->context));
  9419. delete event;
  9420. }
  9421. static void ggml_backend_cuda_event_record(ggml_backend_event_t event) {
  9422. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)event->backend->context;
  9423. CUDA_CHECK(cudaEventRecord((cudaEvent_t)event->context, cuda_ctx->stream()));
  9424. }
  9425. static void ggml_backend_cuda_event_wait(ggml_backend_t backend, ggml_backend_event_t event) {
  9426. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9427. if (ggml_backend_is_cuda(event->backend)) {
  9428. CUDA_CHECK(cudaStreamWaitEvent(cuda_ctx->stream(), (cudaEvent_t)event->context, 0));
  9429. } else {
  9430. #if 0
  9431. // untested
  9432. auto wait_fn = [](void * user_data) {
  9433. ggml_backend_event_t event = (ggml_backend_event_t)user_data;
  9434. ggml_backend_event_synchronize(event);
  9435. };
  9436. CUDA_CHECK(cudaLaunchHostFunc(cuda_ctx->stream(), wait_fn, event));
  9437. #endif
  9438. GGML_ASSERT(false);
  9439. }
  9440. }
  9441. static void ggml_backend_cuda_event_synchronize(ggml_backend_event_t event) {
  9442. CUDA_CHECK(cudaEventSynchronize((cudaEvent_t)event->context));
  9443. }
  9444. static ggml_backend_i ggml_backend_cuda_interface = {
  9445. /* .get_name = */ ggml_backend_cuda_name,
  9446. /* .free = */ ggml_backend_cuda_free,
  9447. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  9448. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  9449. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  9450. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  9451. /* .synchronize = */ ggml_backend_cuda_synchronize,
  9452. /* .graph_plan_create = */ NULL,
  9453. /* .graph_plan_free = */ NULL,
  9454. /* .graph_plan_compute = */ NULL,
  9455. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  9456. /* .supports_op = */ ggml_backend_cuda_supports_op,
  9457. /* .offload_op = */ ggml_backend_cuda_offload_op,
  9458. /* .event_new = */ ggml_backend_cuda_event_new,
  9459. /* .event_free = */ ggml_backend_cuda_event_free,
  9460. /* .event_record = */ ggml_backend_cuda_event_record,
  9461. /* .event_wait = */ ggml_backend_cuda_event_wait,
  9462. /* .event_synchronize = */ ggml_backend_cuda_event_synchronize,
  9463. };
  9464. static ggml_guid_t ggml_backend_cuda_guid() {
  9465. static ggml_guid guid = { 0x2c, 0xdd, 0xe8, 0x1c, 0x65, 0xb3, 0x65, 0x73, 0x6a, 0x12, 0x88, 0x61, 0x1c, 0xc9, 0xdc, 0x25 };
  9466. return &guid;
  9467. }
  9468. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  9469. if (device < 0 || device >= ggml_backend_cuda_get_device_count()) {
  9470. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  9471. return nullptr;
  9472. }
  9473. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context(device);
  9474. if (ctx == nullptr) {
  9475. fprintf(stderr, "%s: error: failed to allocate context\n", __func__);
  9476. return nullptr;
  9477. }
  9478. ggml_backend_t cuda_backend = new ggml_backend {
  9479. /* .guid = */ ggml_backend_cuda_guid(),
  9480. /* .interface = */ ggml_backend_cuda_interface,
  9481. /* .context = */ ctx
  9482. };
  9483. return cuda_backend;
  9484. }
  9485. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  9486. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cuda_guid());
  9487. }
  9488. GGML_CALL int ggml_backend_cuda_get_device_count() {
  9489. return get_cuda_global_info().device_count;
  9490. }
  9491. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  9492. cudaDeviceProp prop;
  9493. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  9494. snprintf(description, description_size, "%s", prop.name);
  9495. }
  9496. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  9497. ggml_cuda_set_device(device);
  9498. CUDA_CHECK(cudaMemGetInfo(free, total));
  9499. }
  9500. GGML_CALL bool ggml_backend_cuda_register_host_buffer(void * buffer, size_t size) {
  9501. if (getenv("GGML_CUDA_REGISTER_HOST") == nullptr) {
  9502. return false;
  9503. }
  9504. cudaError_t err = cudaHostRegister(buffer, size, cudaHostRegisterPortable | cudaHostRegisterReadOnly);
  9505. if (err != cudaSuccess) {
  9506. // clear the error
  9507. cudaGetLastError();
  9508. fprintf(stderr, "%s: warning: failed to register %.2f MiB of pinned memory: %s\n", __func__,
  9509. size/1024.0/1024.0, cudaGetErrorString(err));
  9510. return false;
  9511. }
  9512. return true;
  9513. }
  9514. GGML_CALL void ggml_backend_cuda_unregister_host_buffer(void * buffer) {
  9515. if (getenv("GGML_CUDA_REGISTER_HOST") == nullptr) {
  9516. return;
  9517. }
  9518. cudaError_t err = cudaHostUnregister(buffer);
  9519. if (err != cudaSuccess) {
  9520. // clear the error
  9521. cudaGetLastError();
  9522. }
  9523. }
  9524. // backend registry
  9525. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  9526. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  9527. return cuda_backend;
  9528. GGML_UNUSED(params);
  9529. }
  9530. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  9531. GGML_CALL int ggml_backend_cuda_reg_devices() {
  9532. int device_count = ggml_backend_cuda_get_device_count();
  9533. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  9534. for (int i = 0; i < device_count; i++) {
  9535. char name[128];
  9536. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  9537. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  9538. }
  9539. return device_count;
  9540. }