ggml-cuda.cu 392 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641
  1. #include <algorithm>
  2. #include <assert.h>
  3. #include <atomic>
  4. #include <cinttypes>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <float.h>
  8. #include <limits>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <vector>
  12. #if defined(GGML_USE_HIPBLAS)
  13. #include <hip/hip_runtime.h>
  14. #include <hipblas/hipblas.h>
  15. #include <hip/hip_fp16.h>
  16. #ifdef __HIP_PLATFORM_AMD__
  17. // for rocblas_initialize()
  18. #include "rocblas/rocblas.h"
  19. #endif // __HIP_PLATFORM_AMD__
  20. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  21. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  22. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  23. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  24. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  25. #define CUBLAS_OP_N HIPBLAS_OP_N
  26. #define CUBLAS_OP_T HIPBLAS_OP_T
  27. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  28. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  29. #define CUDA_R_16F HIPBLAS_R_16F
  30. #define CUDA_R_32F HIPBLAS_R_32F
  31. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  32. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  33. #define cublasCreate hipblasCreate
  34. #define cublasGemmEx hipblasGemmEx
  35. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  36. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  37. #define cublasHandle_t hipblasHandle_t
  38. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  39. #define cublasSetStream hipblasSetStream
  40. #define cublasSgemm hipblasSgemm
  41. #define cublasStatus_t hipblasStatus_t
  42. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  43. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  44. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  45. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  46. #define cudaDeviceProp hipDeviceProp_t
  47. #define cudaDeviceSynchronize hipDeviceSynchronize
  48. #define cudaError_t hipError_t
  49. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  50. #define cudaEventDisableTiming hipEventDisableTiming
  51. #define cudaEventRecord hipEventRecord
  52. #define cudaEvent_t hipEvent_t
  53. #define cudaEventDestroy hipEventDestroy
  54. #define cudaFree hipFree
  55. #define cudaFreeHost hipHostFree
  56. #define cudaGetDevice hipGetDevice
  57. #define cudaGetDeviceCount hipGetDeviceCount
  58. #define cudaGetDeviceProperties hipGetDeviceProperties
  59. #define cudaGetErrorString hipGetErrorString
  60. #define cudaGetLastError hipGetLastError
  61. #ifdef GGML_HIP_UMA
  62. #define cudaMalloc hipMallocManaged
  63. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  64. #else
  65. #define cudaMalloc hipMalloc
  66. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  67. #endif
  68. #define cudaMemcpy hipMemcpy
  69. #define cudaMemcpyAsync hipMemcpyAsync
  70. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  71. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  72. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  73. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  74. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  75. #define cudaMemcpyKind hipMemcpyKind
  76. #define cudaMemset hipMemset
  77. #define cudaMemsetAsync hipMemsetAsync
  78. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  79. #define cudaSetDevice hipSetDevice
  80. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  81. #define cudaStreamFireAndForget hipStreamFireAndForget
  82. #define cudaStreamNonBlocking hipStreamNonBlocking
  83. #define cudaStreamSynchronize hipStreamSynchronize
  84. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  85. #define cudaStream_t hipStream_t
  86. #define cudaSuccess hipSuccess
  87. #define __trap abort
  88. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  89. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  90. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  91. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  92. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  93. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  94. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  95. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  96. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  97. #else
  98. #include <cuda_runtime.h>
  99. #include <cuda.h>
  100. #include <cublas_v2.h>
  101. #include <cuda_fp16.h>
  102. #if CUDART_VERSION < 11020
  103. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  104. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  105. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  106. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  107. #define cublasComputeType_t cudaDataType_t
  108. #endif // CUDART_VERSION < 11020
  109. #endif // defined(GGML_USE_HIPBLAS)
  110. #include "ggml-cuda.h"
  111. #include "ggml.h"
  112. #include "ggml-backend-impl.h"
  113. #define CC_PASCAL 600
  114. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  115. #define CC_VOLTA 700
  116. #define CC_OFFSET_AMD 1000000
  117. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  118. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  119. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  120. #define GGML_CUDA_MAX_NODES 8192
  121. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  122. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  123. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  124. // - 7B quantum model: +100-200 MB
  125. // - 13B quantum model: +200-400 MB
  126. //
  127. //#define GGML_CUDA_FORCE_MMQ
  128. // TODO: improve this to be correct for more hardware
  129. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  130. #if !defined(GGML_CUDA_FORCE_MMQ)
  131. #define CUDA_USE_TENSOR_CORES
  132. #endif
  133. // max batch size to use MMQ kernels when tensor cores are available
  134. #define MMQ_MAX_BATCH_SIZE 32
  135. #if defined(GGML_USE_HIPBLAS)
  136. #define __CUDA_ARCH__ 1300
  137. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  138. defined(__gfx1150__) || defined(__gfx1151__)
  139. #define RDNA3
  140. #endif
  141. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  142. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  143. #define RDNA2
  144. #endif
  145. #ifndef __has_builtin
  146. #define __has_builtin(x) 0
  147. #endif
  148. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  149. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  150. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  151. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  152. #if __has_builtin(__builtin_elementwise_sub_sat)
  153. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  154. return reinterpret_cast<const int &>(c);
  155. #else
  156. int8x4_t c;
  157. int16_t tmp;
  158. #pragma unroll
  159. for (int i = 0; i < 4; i++) {
  160. tmp = va[i] - vb[i];
  161. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  162. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  163. c[i] = tmp;
  164. }
  165. return reinterpret_cast<int &>(c);
  166. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  167. }
  168. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  169. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  170. c = __builtin_amdgcn_sdot4(a, b, c, false);
  171. #elif defined(RDNA3)
  172. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  173. #elif defined(__gfx1010__) || defined(__gfx900__)
  174. int tmp1;
  175. int tmp2;
  176. asm("\n \
  177. 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 \
  178. 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 \
  179. v_add3_u32 %0, %1, %2, %0 \n \
  180. 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 \
  181. 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 \
  182. v_add3_u32 %0, %1, %2, %0 \n \
  183. "
  184. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  185. : "v"(a), "v"(b)
  186. );
  187. #else
  188. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  189. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  190. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  191. #endif
  192. return c;
  193. }
  194. #endif // defined(GGML_USE_HIPBLAS)
  195. #if defined(_MSC_VER)
  196. #pragma warning(disable: 4244 4267) // possible loss of data
  197. #endif
  198. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  199. [[noreturn]]
  200. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  201. int id = -1; // in case cudaGetDevice fails
  202. cudaGetDevice(&id);
  203. fprintf(stderr, "CUDA error: %s\n", msg);
  204. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  205. fprintf(stderr, " %s\n", stmt);
  206. // abort with GGML_ASSERT to get a stack trace
  207. GGML_ASSERT(!"CUDA error");
  208. }
  209. #define CUDA_CHECK_GEN(err, success, error_fn) \
  210. do { \
  211. auto err_ = (err); \
  212. if (err_ != (success)) { \
  213. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  214. } \
  215. } while (0)
  216. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  217. #if CUDART_VERSION >= 12000
  218. static const char * cublas_get_error_str(const cublasStatus_t err) {
  219. return cublasGetStatusString(err);
  220. }
  221. #else
  222. static const char * cublas_get_error_str(const cublasStatus_t err) {
  223. switch (err) {
  224. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  225. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  226. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  227. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  228. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  229. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  230. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  231. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  232. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  233. default: return "unknown error";
  234. }
  235. }
  236. #endif // CUDART_VERSION >= 12000
  237. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  238. #if !defined(GGML_USE_HIPBLAS)
  239. static const char * cu_get_error_str(CUresult err) {
  240. const char * err_str;
  241. cuGetErrorString(err, &err_str);
  242. return err_str;
  243. }
  244. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  245. #endif
  246. #if CUDART_VERSION >= 11100
  247. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  248. #else
  249. #define GGML_CUDA_ASSUME(x)
  250. #endif // CUDART_VERSION >= 11100
  251. #ifdef GGML_CUDA_F16
  252. typedef half dfloat; // dequantize float
  253. typedef half2 dfloat2;
  254. #else
  255. typedef float dfloat; // dequantize float
  256. typedef float2 dfloat2;
  257. #endif //GGML_CUDA_F16
  258. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  259. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  260. int x32 = 0;
  261. x32 |= x16[0] << 0;
  262. x32 |= x16[1] << 16;
  263. return x32;
  264. }
  265. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  266. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  267. int x32 = 0;
  268. x32 |= x16[0] << 0;
  269. x32 |= x16[1] << 16;
  270. return x32;
  271. }
  272. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  273. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  274. }
  275. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  276. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  277. }
  278. template<typename T>
  279. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  280. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  281. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  282. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  283. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  284. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  285. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  286. typedef void (*ggml_cuda_op_mul_mat_t)(
  287. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  288. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  289. const int64_t src1_padded_row_size, cudaStream_t stream);
  290. typedef void (*ggml_cuda_op_flatten_t)(
  291. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  292. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  293. // QK = number of values after dequantization
  294. // QR = QK / number of values before dequantization
  295. // QI = number of 32 bit integers before dequantization
  296. #define QK4_0 32
  297. #define QR4_0 2
  298. #define QI4_0 (QK4_0 / (4 * QR4_0))
  299. typedef struct {
  300. half d; // delta
  301. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  302. } block_q4_0;
  303. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  304. #define QK4_1 32
  305. #define QR4_1 2
  306. #define QI4_1 (QK4_1 / (4 * QR4_1))
  307. typedef struct {
  308. half2 dm; // dm.x = delta, dm.y = min
  309. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  310. } block_q4_1;
  311. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  312. #define QK5_0 32
  313. #define QR5_0 2
  314. #define QI5_0 (QK5_0 / (4 * QR5_0))
  315. typedef struct {
  316. half d; // delta
  317. uint8_t qh[4]; // 5-th bit of quants
  318. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  319. } block_q5_0;
  320. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  321. #define QK5_1 32
  322. #define QR5_1 2
  323. #define QI5_1 (QK5_1 / (4 * QR5_1))
  324. typedef struct {
  325. half2 dm; // dm.x = delta, dm.y = min
  326. uint8_t qh[4]; // 5-th bit of quants
  327. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  328. } block_q5_1;
  329. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  330. #define QK8_0 32
  331. #define QR8_0 1
  332. #define QI8_0 (QK8_0 / (4 * QR8_0))
  333. typedef struct {
  334. half d; // delta
  335. int8_t qs[QK8_0]; // quants
  336. } block_q8_0;
  337. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  338. #define QK8_1 32
  339. #define QR8_1 1
  340. #define QI8_1 (QK8_1 / (4 * QR8_1))
  341. typedef struct {
  342. half2 ds; // ds.x = delta, ds.y = sum
  343. int8_t qs[QK8_0]; // quants
  344. } block_q8_1;
  345. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  346. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  347. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  348. typedef void (*load_tiles_cuda_t)(
  349. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  350. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  351. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  352. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  353. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  354. //================================= k-quants
  355. #ifdef GGML_QKK_64
  356. #define QK_K 64
  357. #define K_SCALE_SIZE 4
  358. #else
  359. #define QK_K 256
  360. #define K_SCALE_SIZE 12
  361. #endif
  362. #define QR2_K 4
  363. #define QI2_K (QK_K / (4*QR2_K))
  364. typedef struct {
  365. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  366. uint8_t qs[QK_K/4]; // quants
  367. half2 dm; // super-block scale for quantized scales/mins
  368. } block_q2_K;
  369. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  370. #define QR3_K 4
  371. #define QI3_K (QK_K / (4*QR3_K))
  372. typedef struct {
  373. uint8_t hmask[QK_K/8]; // quants - high bit
  374. uint8_t qs[QK_K/4]; // quants - low 2 bits
  375. #ifdef GGML_QKK_64
  376. uint8_t scales[2]; // scales, quantized with 8 bits
  377. #else
  378. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  379. #endif
  380. half d; // super-block scale
  381. } block_q3_K;
  382. //static_assert(sizeof(block_q3_K) == sizeof(ggml_fp16_t) + QK_K / 4 + QK_K / 8 + K_SCALE_SIZE, "wrong q3_K block size/padding");
  383. #define QR4_K 2
  384. #define QI4_K (QK_K / (4*QR4_K))
  385. #ifdef GGML_QKK_64
  386. typedef struct {
  387. half dm[2]; // super-block scales/mins
  388. uint8_t scales[2]; // 4-bit block scales/mins
  389. uint8_t qs[QK_K/2]; // 4--bit quants
  390. } block_q4_K;
  391. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  392. #else
  393. typedef struct {
  394. half2 dm; // super-block scale for quantized scales/mins
  395. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  396. uint8_t qs[QK_K/2]; // 4--bit quants
  397. } block_q4_K;
  398. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  399. #endif
  400. #define QR5_K 2
  401. #define QI5_K (QK_K / (4*QR5_K))
  402. #ifdef GGML_QKK_64
  403. typedef struct {
  404. half d; // super-block scale
  405. int8_t scales[QK_K/16]; // block scales
  406. uint8_t qh[QK_K/8]; // quants, high bit
  407. uint8_t qs[QK_K/2]; // quants, low 4 bits
  408. } block_q5_K;
  409. static_assert(sizeof(block_q5_K) == sizeof(ggml_fp16_t) + QK_K/2 + QK_K/8 + QK_K/16, "wrong q5_K block size/padding");
  410. #else
  411. typedef struct {
  412. half2 dm; // super-block scale for quantized scales/mins
  413. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  414. uint8_t qh[QK_K/8]; // quants, high bit
  415. uint8_t qs[QK_K/2]; // quants, low 4 bits
  416. } block_q5_K;
  417. static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_fp16_t) + K_SCALE_SIZE + QK_K/2 + QK_K/8, "wrong q5_K block size/padding");
  418. #endif
  419. #define QR6_K 2
  420. #define QI6_K (QK_K / (4*QR6_K))
  421. typedef struct {
  422. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  423. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  424. int8_t scales[QK_K/16]; // scales
  425. half d; // delta
  426. } block_q6_K;
  427. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  428. #define QR2_XXS 8
  429. #define QI2_XXS (QK_K / (4*QR2_XXS))
  430. typedef struct {
  431. half d;
  432. uint16_t qs[QK_K/8];
  433. } block_iq2_xxs;
  434. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  435. #define WARP_SIZE 32
  436. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  437. #define CUDA_GELU_BLOCK_SIZE 256
  438. #define CUDA_SILU_BLOCK_SIZE 256
  439. #define CUDA_TANH_BLOCK_SIZE 256
  440. #define CUDA_RELU_BLOCK_SIZE 256
  441. #define CUDA_SQR_BLOCK_SIZE 256
  442. #define CUDA_CPY_BLOCK_SIZE 32
  443. #define CUDA_SCALE_BLOCK_SIZE 256
  444. #define CUDA_CLAMP_BLOCK_SIZE 256
  445. #define CUDA_ROPE_BLOCK_SIZE 256
  446. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  447. #define CUDA_ALIBI_BLOCK_SIZE 32
  448. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  449. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  450. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  451. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  452. #define CUDA_UPSCALE_BLOCK_SIZE 256
  453. #define CUDA_CONCAT_BLOCK_SIZE 256
  454. #define CUDA_PAD_BLOCK_SIZE 256
  455. #define CUDA_ACC_BLOCK_SIZE 256
  456. #define CUDA_IM2COL_BLOCK_SIZE 256
  457. // dmmv = dequantize_mul_mat_vec
  458. #ifndef GGML_CUDA_DMMV_X
  459. #define GGML_CUDA_DMMV_X 32
  460. #endif
  461. #ifndef GGML_CUDA_MMV_Y
  462. #define GGML_CUDA_MMV_Y 1
  463. #endif
  464. #ifndef K_QUANTS_PER_ITERATION
  465. #define K_QUANTS_PER_ITERATION 2
  466. #else
  467. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  468. #endif
  469. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  470. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  471. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  472. #define MUL_MAT_SRC1_COL_STRIDE 128
  473. #define MAX_STREAMS 8
  474. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  475. struct ggml_tensor_extra_gpu {
  476. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  477. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  478. };
  479. // this is faster on Windows
  480. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  481. static void ggml_cuda_set_device(const int device) {
  482. int current_device;
  483. CUDA_CHECK(cudaGetDevice(&current_device));
  484. if (device == current_device) {
  485. return;
  486. }
  487. CUDA_CHECK(cudaSetDevice(device));
  488. }
  489. static int g_device_count = -1;
  490. static int g_main_device = 0;
  491. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  492. struct cuda_device_capabilities {
  493. int cc; // compute capability
  494. size_t smpb; // max. shared memory per block
  495. bool vmm; // virtual memory support
  496. size_t vmm_granularity; // granularity of virtual memory
  497. };
  498. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  499. static void * g_scratch_buffer = nullptr;
  500. static size_t g_scratch_size = 0; // disabled by default
  501. static size_t g_scratch_offset = 0;
  502. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  503. [[noreturn]]
  504. static __device__ void bad_arch() {
  505. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  506. __trap();
  507. (void) bad_arch; // suppress unused function warning
  508. }
  509. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  510. #pragma unroll
  511. for (int mask = 16; mask > 0; mask >>= 1) {
  512. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  513. }
  514. return x;
  515. }
  516. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  517. #pragma unroll
  518. for (int mask = 16; mask > 0; mask >>= 1) {
  519. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  520. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  521. }
  522. return a;
  523. }
  524. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  525. #if __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  526. (void) a;
  527. bad_arch();
  528. #else
  529. #pragma unroll
  530. for (int mask = 16; mask > 0; mask >>= 1) {
  531. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  532. }
  533. return a;
  534. #endif // __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  535. }
  536. static __device__ __forceinline__ float warp_reduce_max(float x) {
  537. #pragma unroll
  538. for (int mask = 16; mask > 0; mask >>= 1) {
  539. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  540. }
  541. return x;
  542. }
  543. static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  544. #if __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  545. (void) x;
  546. bad_arch();
  547. #else
  548. #pragma unroll
  549. for (int mask = 16; mask > 0; mask >>= 1) {
  550. x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  551. }
  552. return x;
  553. #endif // __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  554. }
  555. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  556. return b;
  557. GGML_UNUSED(a);
  558. }
  559. static __device__ __forceinline__ float op_add(const float a, const float b) {
  560. return a + b;
  561. }
  562. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  563. return a * b;
  564. }
  565. static __device__ __forceinline__ float op_div(const float a, const float b) {
  566. return a / b;
  567. }
  568. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  569. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  570. int ne0, int ne1, int ne2, int ne3,
  571. int ne10, int ne11, int ne12, int ne13,
  572. /*int s0, */ int s1, int s2, int s3,
  573. /*int s10,*/ int s11, int s12, int s13) {
  574. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  575. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  576. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  577. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  578. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  579. return;
  580. }
  581. const int i11 = i1 % ne11;
  582. const int i12 = i2 % ne12;
  583. const int i13 = i3 % ne13;
  584. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  585. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  586. const size_t i_dst = i_src0;
  587. const src0_t * src0_row = src0 + i_src0;
  588. const src1_t * src1_row = src1 + i_src1;
  589. dst_t * dst_row = dst + i_dst;
  590. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  591. const int i10 = i0 % ne10;
  592. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  593. }
  594. }
  595. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  596. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  597. int ne0, int ne1, int ne2, int ne3,
  598. int ne10, int ne11, int ne12, int ne13,
  599. /*int s0, */ int s1, int s2, int s3,
  600. /*int s10,*/ int s11, int s12, int s13) {
  601. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  602. const int i3 = i/(ne2*ne1*ne0);
  603. const int i2 = (i/(ne1*ne0)) % ne2;
  604. const int i1 = (i/ne0) % ne1;
  605. const int i0 = i % ne0;
  606. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  607. return;
  608. }
  609. const int i11 = i1 % ne11;
  610. const int i12 = i2 % ne12;
  611. const int i13 = i3 % ne13;
  612. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  613. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  614. const size_t i_dst = i_src0;
  615. const src0_t * src0_row = src0 + i_src0;
  616. const src1_t * src1_row = src1 + i_src1;
  617. dst_t * dst_row = dst + i_dst;
  618. const int i10 = i0 % ne10;
  619. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  620. }
  621. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  622. const int ne10, const int ne11, const int ne12,
  623. const int nb1, const int nb2, int offset) {
  624. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  625. if (i >= ne) {
  626. return;
  627. }
  628. int src1_idx = i - offset;
  629. int oz = src1_idx / nb2;
  630. int oy = (src1_idx - (oz * nb2)) / nb1;
  631. int ox = src1_idx % nb1;
  632. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  633. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  634. } else {
  635. dst[i] = x[i];
  636. }
  637. }
  638. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  639. const float GELU_COEF_A = 0.044715f;
  640. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  641. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  642. if (i >= k) {
  643. return;
  644. }
  645. float xi = x[i];
  646. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  647. }
  648. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  649. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  650. if (i >= k) {
  651. return;
  652. }
  653. dst[i] = x[i] / (1.0f + expf(-x[i]));
  654. }
  655. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  656. const float GELU_QUICK_COEF = -1.702f;
  657. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  658. if (i >= k) {
  659. return;
  660. }
  661. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  662. }
  663. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  664. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  665. if (i >= k) {
  666. return;
  667. }
  668. dst[i] = tanhf(x[i]);
  669. }
  670. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  671. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  672. if (i >= k) {
  673. return;
  674. }
  675. dst[i] = fmaxf(x[i], 0);
  676. }
  677. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  678. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  679. if (i >= k) {
  680. return;
  681. }
  682. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  683. }
  684. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  685. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  686. if (i >= k) {
  687. return;
  688. }
  689. dst[i] = x[i] * x[i];
  690. }
  691. template <int block_size>
  692. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  693. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  694. const int tid = threadIdx.x;
  695. float2 mean_var = make_float2(0.f, 0.f);
  696. for (int col = tid; col < ncols; col += block_size) {
  697. const float xi = x[row*ncols + col];
  698. mean_var.x += xi;
  699. mean_var.y += xi * xi;
  700. }
  701. // sum up partial sums
  702. mean_var = warp_reduce_sum(mean_var);
  703. if (block_size > WARP_SIZE) {
  704. __shared__ float2 s_sum[32];
  705. int warp_id = threadIdx.x / WARP_SIZE;
  706. int lane_id = threadIdx.x % WARP_SIZE;
  707. if (lane_id == 0) {
  708. s_sum[warp_id] = mean_var;
  709. }
  710. __syncthreads();
  711. mean_var = s_sum[lane_id];
  712. mean_var = warp_reduce_sum(mean_var);
  713. }
  714. const float mean = mean_var.x / ncols;
  715. const float var = mean_var.y / ncols - mean * mean;
  716. const float inv_std = rsqrtf(var + eps);
  717. for (int col = tid; col < ncols; col += block_size) {
  718. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  719. }
  720. }
  721. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  722. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  723. if (nidx >= ne0) {
  724. return;
  725. }
  726. // operation
  727. int offset_dst =
  728. nidx +
  729. blockIdx.y * ne0 +
  730. blockIdx.z * ne0 * gridDim.y;
  731. if (blockIdx.z < ne02) { // src0
  732. int offset_src =
  733. nidx +
  734. blockIdx.y * ne0 +
  735. blockIdx.z * ne0 * gridDim.y;
  736. dst[offset_dst] = x[offset_src];
  737. } else {
  738. int offset_src =
  739. nidx +
  740. blockIdx.y * ne0 +
  741. (blockIdx.z - ne02) * ne0 * gridDim.y;
  742. dst[offset_dst] = y[offset_src];
  743. }
  744. }
  745. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  746. int ne0 = ne00 * scale_factor;
  747. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  748. if (nidx >= ne0) {
  749. return;
  750. }
  751. // operation
  752. int i00 = nidx / scale_factor;
  753. int i01 = blockIdx.y / scale_factor;
  754. int offset_src =
  755. i00 +
  756. i01 * ne00 +
  757. blockIdx.z * nb02;
  758. int offset_dst =
  759. nidx +
  760. blockIdx.y * ne0 +
  761. blockIdx.z * ne0 * gridDim.y;
  762. dst[offset_dst] = x[offset_src];
  763. }
  764. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  765. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  766. if (nidx >= ne0) {
  767. return;
  768. }
  769. // operation
  770. int offset_dst =
  771. nidx +
  772. blockIdx.y * ne0 +
  773. blockIdx.z * ne0 * gridDim.y;
  774. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  775. int offset_src =
  776. nidx +
  777. blockIdx.y * ne00 +
  778. blockIdx.z * ne00 * ne01;
  779. dst[offset_dst] = x[offset_src];
  780. } else {
  781. dst[offset_dst] = 0.0f;
  782. }
  783. }
  784. template <int block_size>
  785. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  786. int start = blockIdx.x * group_size;
  787. int end = start + group_size;
  788. start += threadIdx.x;
  789. if (end >= ne_elements) {
  790. end = ne_elements;
  791. }
  792. float tmp = 0.0f; // partial sum for thread in warp
  793. for (int j = start; j < end; j += block_size) {
  794. tmp += x[j];
  795. }
  796. tmp = warp_reduce_sum(tmp);
  797. if (block_size > WARP_SIZE) {
  798. __shared__ float s_sum[32];
  799. int warp_id = threadIdx.x / WARP_SIZE;
  800. int lane_id = threadIdx.x % WARP_SIZE;
  801. if (lane_id == 0) {
  802. s_sum[warp_id] = tmp;
  803. }
  804. __syncthreads();
  805. tmp = s_sum[lane_id];
  806. tmp = warp_reduce_sum(tmp);
  807. }
  808. float mean = tmp / group_size;
  809. tmp = 0.0f;
  810. for (int j = start; j < end; j += block_size) {
  811. float xi = x[j] - mean;
  812. dst[j] = xi;
  813. tmp += xi * xi;
  814. }
  815. tmp = warp_reduce_sum(tmp);
  816. if (block_size > WARP_SIZE) {
  817. __shared__ float s_sum[32];
  818. int warp_id = threadIdx.x / WARP_SIZE;
  819. int lane_id = threadIdx.x % WARP_SIZE;
  820. if (lane_id == 0) {
  821. s_sum[warp_id] = tmp;
  822. }
  823. __syncthreads();
  824. tmp = s_sum[lane_id];
  825. tmp = warp_reduce_sum(tmp);
  826. }
  827. float variance = tmp / group_size;
  828. float scale = rsqrtf(variance + eps);
  829. for (int j = start; j < end; j += block_size) {
  830. dst[j] *= scale;
  831. }
  832. }
  833. template <int block_size>
  834. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  835. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  836. const int tid = threadIdx.x;
  837. float tmp = 0.0f; // partial sum for thread in warp
  838. for (int col = tid; col < ncols; col += block_size) {
  839. const float xi = x[row*ncols + col];
  840. tmp += xi * xi;
  841. }
  842. // sum up partial sums
  843. tmp = warp_reduce_sum(tmp);
  844. if (block_size > WARP_SIZE) {
  845. __shared__ float s_sum[32];
  846. int warp_id = threadIdx.x / WARP_SIZE;
  847. int lane_id = threadIdx.x % WARP_SIZE;
  848. if (lane_id == 0) {
  849. s_sum[warp_id] = tmp;
  850. }
  851. __syncthreads();
  852. tmp = s_sum[lane_id];
  853. tmp = warp_reduce_sum(tmp);
  854. }
  855. const float mean = tmp / ncols;
  856. const float scale = rsqrtf(mean + eps);
  857. for (int col = tid; col < ncols; col += block_size) {
  858. dst[row*ncols + col] = scale * x[row*ncols + col];
  859. }
  860. }
  861. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  862. const block_q4_0 * x = (const block_q4_0 *) vx;
  863. const dfloat d = x[ib].d;
  864. const int vui = x[ib].qs[iqs];
  865. v.x = vui & 0xF;
  866. v.y = vui >> 4;
  867. #ifdef GGML_CUDA_F16
  868. v = __hsub2(v, {8.0f, 8.0f});
  869. v = __hmul2(v, {d, d});
  870. #else
  871. v.x = (v.x - 8.0f) * d;
  872. v.y = (v.y - 8.0f) * d;
  873. #endif // GGML_CUDA_F16
  874. }
  875. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  876. const block_q4_1 * x = (const block_q4_1 *) vx;
  877. const dfloat d = __low2half(x[ib].dm);
  878. const dfloat m = __high2half(x[ib].dm);
  879. const int vui = x[ib].qs[iqs];
  880. v.x = vui & 0xF;
  881. v.y = vui >> 4;
  882. #ifdef GGML_CUDA_F16
  883. v = __hmul2(v, {d, d});
  884. v = __hadd2(v, {m, m});
  885. #else
  886. v.x = (v.x * d) + m;
  887. v.y = (v.y * d) + m;
  888. #endif // GGML_CUDA_F16
  889. }
  890. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  891. const block_q5_0 * x = (const block_q5_0 *) vx;
  892. const dfloat d = x[ib].d;
  893. uint32_t qh;
  894. memcpy(&qh, x[ib].qh, sizeof(qh));
  895. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  896. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  897. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  898. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  899. #ifdef GGML_CUDA_F16
  900. v = __hsub2(v, {16.0f, 16.0f});
  901. v = __hmul2(v, {d, d});
  902. #else
  903. v.x = (v.x - 16.0f) * d;
  904. v.y = (v.y - 16.0f) * d;
  905. #endif // GGML_CUDA_F16
  906. }
  907. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  908. const block_q5_1 * x = (const block_q5_1 *) vx;
  909. const dfloat d = __low2half(x[ib].dm);
  910. const dfloat m = __high2half(x[ib].dm);
  911. uint32_t qh;
  912. memcpy(&qh, x[ib].qh, sizeof(qh));
  913. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  914. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  915. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  916. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  917. #ifdef GGML_CUDA_F16
  918. v = __hmul2(v, {d, d});
  919. v = __hadd2(v, {m, m});
  920. #else
  921. v.x = (v.x * d) + m;
  922. v.y = (v.y * d) + m;
  923. #endif // GGML_CUDA_F16
  924. }
  925. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  926. const block_q8_0 * x = (const block_q8_0 *) vx;
  927. const dfloat d = x[ib].d;
  928. v.x = x[ib].qs[iqs + 0];
  929. v.y = x[ib].qs[iqs + 1];
  930. #ifdef GGML_CUDA_F16
  931. v = __hmul2(v, {d, d});
  932. #else
  933. v.x *= d;
  934. v.y *= d;
  935. #endif // GGML_CUDA_F16
  936. }
  937. //================================== k-quants
  938. template<typename dst_t>
  939. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  940. const int i = blockIdx.x;
  941. const block_q2_K * x = (const block_q2_K *) vx;
  942. const int tid = threadIdx.x;
  943. #if QK_K == 256
  944. const int n = tid/32;
  945. const int l = tid - 32*n;
  946. const int is = 8*n + l/16;
  947. const uint8_t q = x[i].qs[32*n + l];
  948. dst_t * y = yy + i*QK_K + 128*n;
  949. float dall = __low2half(x[i].dm);
  950. float dmin = __high2half(x[i].dm);
  951. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  952. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  953. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  954. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  955. #else
  956. const int is = tid/16; // 0 or 1
  957. const int il = tid%16; // 0...15
  958. const uint8_t q = x[i].qs[il] >> (2*is);
  959. dst_t * y = yy + i*QK_K + 16*is + il;
  960. float dall = __low2half(x[i].dm);
  961. float dmin = __high2half(x[i].dm);
  962. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  963. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  964. #endif
  965. }
  966. template<typename dst_t>
  967. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  968. const int i = blockIdx.x;
  969. const block_q3_K * x = (const block_q3_K *) vx;
  970. #if QK_K == 256
  971. const int r = threadIdx.x/4;
  972. const int tid = r/2;
  973. const int is0 = r%2;
  974. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  975. const int n = tid / 4;
  976. const int j = tid - 4*n;
  977. uint8_t m = 1 << (4*n + j);
  978. int is = 8*n + 2*j + is0;
  979. int shift = 2*j;
  980. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  981. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  982. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  983. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  984. float d_all = x[i].d;
  985. float dl = d_all * (us - 32);
  986. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  987. const uint8_t * q = x[i].qs + 32*n;
  988. const uint8_t * hm = x[i].hmask;
  989. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  990. #else
  991. const int tid = threadIdx.x;
  992. const int is = tid/16; // 0 or 1
  993. const int il = tid%16; // 0...15
  994. const int im = il/8; // 0...1
  995. const int in = il%8; // 0...7
  996. dst_t * y = yy + i*QK_K + 16*is + il;
  997. const uint8_t q = x[i].qs[il] >> (2*is);
  998. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  999. const float d = (float)x[i].d;
  1000. if (is == 0) {
  1001. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1002. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1003. } else {
  1004. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1005. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1006. }
  1007. #endif
  1008. }
  1009. #if QK_K == 256
  1010. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1011. if (j < 4) {
  1012. d = q[j] & 63; m = q[j + 4] & 63;
  1013. } else {
  1014. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1015. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1016. }
  1017. }
  1018. #endif
  1019. template<typename dst_t>
  1020. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1021. const block_q4_K * x = (const block_q4_K *) vx;
  1022. const int i = blockIdx.x;
  1023. #if QK_K == 256
  1024. // assume 32 threads
  1025. const int tid = threadIdx.x;
  1026. const int il = tid/8;
  1027. const int ir = tid%8;
  1028. const int is = 2*il;
  1029. const int n = 4;
  1030. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1031. const float dall = __low2half(x[i].dm);
  1032. const float dmin = __high2half(x[i].dm);
  1033. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1034. uint8_t sc, m;
  1035. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1036. const float d1 = dall * sc; const float m1 = dmin * m;
  1037. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1038. const float d2 = dall * sc; const float m2 = dmin * m;
  1039. for (int l = 0; l < n; ++l) {
  1040. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1041. y[l +32] = d2 * (q[l] >> 4) - m2;
  1042. }
  1043. #else
  1044. const int tid = threadIdx.x;
  1045. const uint8_t * q = x[i].qs;
  1046. dst_t * y = yy + i*QK_K;
  1047. const float d = (float)x[i].dm[0];
  1048. const float m = (float)x[i].dm[1];
  1049. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1050. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1051. #endif
  1052. }
  1053. template<typename dst_t>
  1054. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1055. const block_q5_K * x = (const block_q5_K *) vx;
  1056. const int i = blockIdx.x;
  1057. #if QK_K == 256
  1058. // assume 64 threads - this is very slightly better than the one below
  1059. const int tid = threadIdx.x;
  1060. const int il = tid/16; // il is in 0...3
  1061. const int ir = tid%16; // ir is in 0...15
  1062. const int is = 2*il; // is is in 0...6
  1063. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1064. const float dall = __low2half(x[i].dm);
  1065. const float dmin = __high2half(x[i].dm);
  1066. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1067. const uint8_t * qh = x[i].qh + 2*ir;
  1068. uint8_t sc, m;
  1069. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1070. const float d1 = dall * sc; const float m1 = dmin * m;
  1071. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1072. const float d2 = dall * sc; const float m2 = dmin * m;
  1073. uint8_t hm = 1 << (2*il);
  1074. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1075. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1076. hm <<= 1;
  1077. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1078. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1079. #else
  1080. const int tid = threadIdx.x;
  1081. const uint8_t q = x[i].qs[tid];
  1082. const int im = tid/8; // 0...3
  1083. const int in = tid%8; // 0...7
  1084. const int is = tid/16; // 0 or 1
  1085. const uint8_t h = x[i].qh[in] >> im;
  1086. const float d = x[i].d;
  1087. dst_t * y = yy + i*QK_K + tid;
  1088. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1089. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1090. #endif
  1091. }
  1092. template<typename dst_t>
  1093. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1094. const block_q6_K * x = (const block_q6_K *) vx;
  1095. const int i = blockIdx.x;
  1096. #if QK_K == 256
  1097. // assume 64 threads - this is very slightly better than the one below
  1098. const int tid = threadIdx.x;
  1099. const int ip = tid/32; // ip is 0 or 1
  1100. const int il = tid - 32*ip; // 0...32
  1101. const int is = 8*ip + il/16;
  1102. dst_t * y = yy + i*QK_K + 128*ip + il;
  1103. const float d = x[i].d;
  1104. const uint8_t * ql = x[i].ql + 64*ip + il;
  1105. const uint8_t qh = x[i].qh[32*ip + il];
  1106. const int8_t * sc = x[i].scales + is;
  1107. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1108. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1109. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1110. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1111. #else
  1112. // assume 32 threads
  1113. const int tid = threadIdx.x;
  1114. const int ip = tid/16; // 0 or 1
  1115. const int il = tid - 16*ip; // 0...15
  1116. dst_t * y = yy + i*QK_K + 16*ip + il;
  1117. const float d = x[i].d;
  1118. const uint8_t ql = x[i].ql[16*ip + il];
  1119. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1120. const int8_t * sc = x[i].scales;
  1121. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1122. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1123. #endif
  1124. }
  1125. static const __device__ uint64_t kgrid_iq2xxs[256] = {
  1126. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1127. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1128. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1129. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1130. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1131. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1132. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1133. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1134. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1135. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1136. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1137. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1138. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1139. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1140. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1141. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1142. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1143. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1144. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1145. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1146. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1147. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1148. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1149. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1150. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1151. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1152. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1153. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1154. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1155. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1156. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1157. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1158. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1159. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1160. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1161. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1162. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1163. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1164. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1165. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1166. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1167. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1168. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1169. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1170. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1171. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1172. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1173. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1174. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1175. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1176. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1177. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1178. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1179. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1180. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1181. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1182. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1183. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1184. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1185. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1186. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1187. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1188. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1189. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1190. };
  1191. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1192. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1193. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1194. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1195. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1196. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1197. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1198. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1199. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1200. };
  1201. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  1202. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1203. switch (type) {
  1204. case GGML_TYPE_Q4_0:
  1205. case GGML_TYPE_Q4_1:
  1206. case GGML_TYPE_Q5_0:
  1207. case GGML_TYPE_Q5_1:
  1208. case GGML_TYPE_Q8_0:
  1209. case GGML_TYPE_Q2_K:
  1210. case GGML_TYPE_Q3_K:
  1211. case GGML_TYPE_Q4_K:
  1212. case GGML_TYPE_Q5_K:
  1213. case GGML_TYPE_Q6_K:
  1214. return true;
  1215. default:
  1216. return false;
  1217. }
  1218. }
  1219. template<typename dst_t>
  1220. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1221. const int i = blockIdx.x;
  1222. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1223. const int tid = threadIdx.x;
  1224. #if QK_K == 256
  1225. const int il = tid/8; // 0...3
  1226. const int ib = tid%8; // 0...7
  1227. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1228. const uint16_t * q2 = x[i].qs + 4*ib;
  1229. const uint8_t * aux8 = (const uint8_t *)q2;
  1230. const uint8_t * grid = (const uint8_t *)(kgrid_iq2xxs + aux8[il]);
  1231. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1232. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1233. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1234. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1235. #else
  1236. assert(false);
  1237. #endif
  1238. }
  1239. 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) {
  1240. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1241. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1242. if (row > nrows) return;
  1243. const int num_blocks_per_row = ncols / QK_K;
  1244. const int ib0 = row*num_blocks_per_row;
  1245. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1246. float tmp = 0; // partial sum for thread in warp
  1247. #if QK_K == 256
  1248. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1249. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1250. const int step = 16/K_QUANTS_PER_ITERATION;
  1251. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1252. const int in = tid - step*im; // 0...15 or 0...7
  1253. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1254. const int q_offset = 32*im + l0;
  1255. const int s_offset = 8*im;
  1256. const int y_offset = 128*im + l0;
  1257. uint32_t aux[4];
  1258. const uint8_t * d = (const uint8_t *)aux;
  1259. const uint8_t * m = (const uint8_t *)(aux + 2);
  1260. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1261. const float * y = yy + i * QK_K + y_offset;
  1262. const uint8_t * q = x[i].qs + q_offset;
  1263. const float dall = __low2half(x[i].dm);
  1264. const float dmin = __high2half(x[i].dm);
  1265. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1266. aux[0] = a[0] & 0x0f0f0f0f;
  1267. aux[1] = a[1] & 0x0f0f0f0f;
  1268. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1269. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1270. float sum1 = 0, sum2 = 0;
  1271. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1272. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1273. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1274. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1275. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1276. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1277. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1278. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1279. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1280. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1281. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1282. }
  1283. tmp += dall * sum1 - dmin * sum2;
  1284. }
  1285. #else
  1286. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1287. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1288. const int offset = tid * K_QUANTS_PER_ITERATION;
  1289. uint32_t uaux[2];
  1290. const uint8_t * d = (const uint8_t *)uaux;
  1291. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1292. const float * y = yy + i * QK_K + offset;
  1293. const uint8_t * q = x[i].qs + offset;
  1294. const uint32_t * s = (const uint32_t *)x[i].scales;
  1295. uaux[0] = s[0] & 0x0f0f0f0f;
  1296. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1297. const float2 dall = __half22float2(x[i].dm);
  1298. float sum1 = 0, sum2 = 0;
  1299. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1300. const uint8_t ql = q[l];
  1301. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1302. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1303. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1304. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1305. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1306. }
  1307. tmp += dall.x * sum1 - dall.y * sum2;
  1308. }
  1309. #endif
  1310. // sum up partial sums and write back result
  1311. #pragma unroll
  1312. for (int mask = 16; mask > 0; mask >>= 1) {
  1313. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1314. }
  1315. if (threadIdx.x == 0) {
  1316. dst[row] = tmp;
  1317. }
  1318. }
  1319. 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) {
  1320. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1321. if (row > nrows) return;
  1322. const int num_blocks_per_row = ncols / QK_K;
  1323. const int ib0 = row*num_blocks_per_row;
  1324. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1325. float tmp = 0; // partial sum for thread in warp
  1326. #if QK_K == 256
  1327. const uint16_t kmask1 = 0x0303;
  1328. const uint16_t kmask2 = 0x0f0f;
  1329. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1330. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1331. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1332. const int step = 16/K_QUANTS_PER_ITERATION;
  1333. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1334. const int in = tid - step*im; // 0....15 or 0...7
  1335. const uint8_t m = 1 << (4*im);
  1336. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1337. const int q_offset = 32*im + l0;
  1338. const int y_offset = 128*im + l0;
  1339. uint16_t utmp[4];
  1340. const int8_t * s = (const int8_t *)utmp;
  1341. const uint16_t s_shift = 4*im;
  1342. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1343. const float * y = yy + i * QK_K + y_offset;
  1344. const uint8_t * q = x[i].qs + q_offset;
  1345. const uint8_t * h = x[i].hmask + l0;
  1346. const uint16_t * a = (const uint16_t *)x[i].scales;
  1347. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1348. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1349. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1350. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1351. const float d = x[i].d;
  1352. float sum = 0;
  1353. for (int l = 0; l < n; ++l) {
  1354. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1355. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1356. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1357. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1358. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1359. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1360. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1361. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1362. }
  1363. tmp += d * sum;
  1364. }
  1365. #else
  1366. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1367. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1368. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1369. const int in = offset/8; // 0 or 1
  1370. const int im = offset%8; // 0...7
  1371. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1372. const float * y = yy + i * QK_K + offset;
  1373. const uint8_t * q = x[i].qs + offset;
  1374. const uint8_t * s = x[i].scales;
  1375. const float dall = (float)x[i].d;
  1376. float sum = 0;
  1377. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1378. const uint8_t hl = x[i].hmask[im+l] >> in;
  1379. const uint8_t ql = q[l];
  1380. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1381. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1382. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1383. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1384. }
  1385. tmp += sum;
  1386. }
  1387. #endif
  1388. // sum up partial sums and write back result
  1389. #pragma unroll
  1390. for (int mask = 16; mask > 0; mask >>= 1) {
  1391. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1392. }
  1393. if (threadIdx.x == 0) {
  1394. dst[row] = tmp;
  1395. }
  1396. }
  1397. 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) {
  1398. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1399. if (row > nrows) return;
  1400. const int num_blocks_per_row = ncols / QK_K;
  1401. const int ib0 = row*num_blocks_per_row;
  1402. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1403. #if QK_K == 256
  1404. const uint16_t kmask1 = 0x3f3f;
  1405. const uint16_t kmask2 = 0x0f0f;
  1406. const uint16_t kmask3 = 0xc0c0;
  1407. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1408. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1409. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1410. const int il = tid/step; // 0...3
  1411. const int ir = tid - step*il; // 0...7 or 0...3
  1412. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1413. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1414. const int in = il%2;
  1415. const int l0 = n*(2*ir + in);
  1416. const int q_offset = 32*im + l0;
  1417. const int y_offset = 64*im + l0;
  1418. uint16_t aux[4];
  1419. const uint8_t * sc = (const uint8_t *)aux;
  1420. #if K_QUANTS_PER_ITERATION == 2
  1421. uint32_t q32[4];
  1422. const uint8_t * q4 = (const uint8_t *)q32;
  1423. #else
  1424. uint16_t q16[4];
  1425. const uint8_t * q4 = (const uint8_t *)q16;
  1426. #endif
  1427. float tmp = 0; // partial sum for thread in warp
  1428. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1429. const float * y1 = yy + i*QK_K + y_offset;
  1430. const float * y2 = y1 + 128;
  1431. const float dall = __low2half(x[i].dm);
  1432. const float dmin = __high2half(x[i].dm);
  1433. const uint16_t * a = (const uint16_t *)x[i].scales;
  1434. aux[0] = a[im+0] & kmask1;
  1435. aux[1] = a[im+2] & kmask1;
  1436. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1437. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1438. #if K_QUANTS_PER_ITERATION == 2
  1439. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1440. const uint32_t * q2 = q1 + 16;
  1441. q32[0] = q1[0] & 0x0f0f0f0f;
  1442. q32[1] = q1[0] & 0xf0f0f0f0;
  1443. q32[2] = q2[0] & 0x0f0f0f0f;
  1444. q32[3] = q2[0] & 0xf0f0f0f0;
  1445. float4 s = {0.f, 0.f, 0.f, 0.f};
  1446. float smin = 0;
  1447. for (int l = 0; l < 4; ++l) {
  1448. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1449. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1450. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1451. }
  1452. 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;
  1453. #else
  1454. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1455. const uint16_t * q2 = q1 + 32;
  1456. q16[0] = q1[0] & 0x0f0f;
  1457. q16[1] = q1[0] & 0xf0f0;
  1458. q16[2] = q2[0] & 0x0f0f;
  1459. q16[3] = q2[0] & 0xf0f0;
  1460. float4 s = {0.f, 0.f, 0.f, 0.f};
  1461. float smin = 0;
  1462. for (int l = 0; l < 2; ++l) {
  1463. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1464. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1465. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1466. }
  1467. 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;
  1468. #endif
  1469. }
  1470. #else
  1471. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1472. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1473. const int step = tid * K_QUANTS_PER_ITERATION;
  1474. uint16_t aux16[2];
  1475. const uint8_t * s = (const uint8_t *)aux16;
  1476. float tmp = 0;
  1477. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1478. const uint8_t * q = x[i].qs + step;
  1479. const float * y = yy + i*QK_K + step;
  1480. const uint16_t * a = (const uint16_t *)x[i].scales;
  1481. aux16[0] = a[0] & 0x0f0f;
  1482. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1483. const float d = (float)x[i].dm[0];
  1484. const float m = (float)x[i].dm[1];
  1485. float sum = 0.f;
  1486. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1487. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1488. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1489. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1490. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1491. }
  1492. tmp += sum;
  1493. }
  1494. #endif
  1495. // sum up partial sums and write back result
  1496. #pragma unroll
  1497. for (int mask = 16; mask > 0; mask >>= 1) {
  1498. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1499. }
  1500. if (tid == 0) {
  1501. dst[row] = tmp;
  1502. }
  1503. }
  1504. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1505. const int row = blockIdx.x;
  1506. const int num_blocks_per_row = ncols / QK_K;
  1507. const int ib0 = row*num_blocks_per_row;
  1508. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1509. float tmp = 0; // partial sum for thread in warp
  1510. #if QK_K == 256
  1511. const uint16_t kmask1 = 0x3f3f;
  1512. const uint16_t kmask2 = 0x0f0f;
  1513. const uint16_t kmask3 = 0xc0c0;
  1514. const int tid = threadIdx.x/2; // 0...15
  1515. const int ix = threadIdx.x%2;
  1516. const int il = tid/4; // 0...3
  1517. const int ir = tid - 4*il;// 0...3
  1518. const int n = 2;
  1519. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1520. const int in = il%2;
  1521. const int l0 = n*(2*ir + in);
  1522. const int q_offset = 32*im + l0;
  1523. const int y_offset = 64*im + l0;
  1524. const uint8_t hm1 = 1 << (2*im);
  1525. const uint8_t hm2 = hm1 << 4;
  1526. uint16_t aux[4];
  1527. const uint8_t * sc = (const uint8_t *)aux;
  1528. uint16_t q16[8];
  1529. const uint8_t * q4 = (const uint8_t *)q16;
  1530. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1531. const uint8_t * ql1 = x[i].qs + q_offset;
  1532. const uint8_t * qh = x[i].qh + l0;
  1533. const float * y1 = yy + i*QK_K + y_offset;
  1534. const float * y2 = y1 + 128;
  1535. const float dall = __low2half(x[i].dm);
  1536. const float dmin = __high2half(x[i].dm);
  1537. const uint16_t * a = (const uint16_t *)x[i].scales;
  1538. aux[0] = a[im+0] & kmask1;
  1539. aux[1] = a[im+2] & kmask1;
  1540. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1541. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1542. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1543. float smin = 0;
  1544. const uint16_t * q1 = (const uint16_t *)ql1;
  1545. const uint16_t * q2 = q1 + 32;
  1546. q16[0] = q1[0] & 0x0f0f;
  1547. q16[1] = q1[8] & 0x0f0f;
  1548. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1549. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1550. q16[4] = q2[0] & 0x0f0f;
  1551. q16[5] = q2[8] & 0x0f0f;
  1552. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1553. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1554. for (int l = 0; l < n; ++l) {
  1555. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1556. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1557. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1558. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1559. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1560. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1561. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1562. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1563. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1564. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1565. }
  1566. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1567. }
  1568. #else
  1569. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1570. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1571. const int step = tid * K_QUANTS_PER_ITERATION;
  1572. const int im = step/8;
  1573. const int in = step%8;
  1574. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1575. const uint8_t * q = x[i].qs + step;
  1576. const int8_t * s = x[i].scales;
  1577. const float * y = yy + i*QK_K + step;
  1578. const float d = x[i].d;
  1579. float sum = 0.f;
  1580. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1581. const uint8_t h = x[i].qh[in+j] >> im;
  1582. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1583. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1584. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1585. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1586. }
  1587. tmp += sum;
  1588. }
  1589. #endif
  1590. // sum up partial sums and write back result
  1591. #pragma unroll
  1592. for (int mask = 16; mask > 0; mask >>= 1) {
  1593. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1594. }
  1595. if (threadIdx.x == 0) {
  1596. dst[row] = tmp;
  1597. }
  1598. }
  1599. 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) {
  1600. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1601. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1602. if (row > nrows) return;
  1603. const int num_blocks_per_row = ncols / QK_K;
  1604. const int ib0 = row*num_blocks_per_row;
  1605. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1606. #if QK_K == 256
  1607. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1608. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1609. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1610. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1611. const int in = tid - step*im; // 0...15 or 0...7
  1612. #if K_QUANTS_PER_ITERATION == 1
  1613. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1614. const int is = 0;
  1615. #else
  1616. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1617. const int is = in / 4;
  1618. #endif
  1619. const int ql_offset = 64*im + l0;
  1620. const int qh_offset = 32*im + l0;
  1621. const int s_offset = 8*im + is;
  1622. const int y_offset = 128*im + l0;
  1623. float tmp = 0; // partial sum for thread in warp
  1624. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1625. const float * y = yy + i * QK_K + y_offset;
  1626. const uint8_t * ql = x[i].ql + ql_offset;
  1627. const uint8_t * qh = x[i].qh + qh_offset;
  1628. const int8_t * s = x[i].scales + s_offset;
  1629. const float d = x[i].d;
  1630. #if K_QUANTS_PER_ITERATION == 1
  1631. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1632. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1633. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1634. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1635. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1636. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1637. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1638. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1639. tmp += sum;
  1640. #else
  1641. float sum = 0;
  1642. for (int l = 0; l < 4; ++l) {
  1643. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1644. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1645. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1646. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1647. }
  1648. tmp += sum;
  1649. #endif
  1650. }
  1651. #else
  1652. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1653. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1654. const int step = tid * K_QUANTS_PER_ITERATION;
  1655. float tmp = 0; // partial sum for thread in warp
  1656. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1657. const float * y = yy + i * QK_K + step;
  1658. const uint8_t * ql = x[i].ql + step;
  1659. const uint8_t * qh = x[i].qh + step;
  1660. const int8_t * s = x[i].scales;
  1661. const float d = x[i+0].d;
  1662. float sum = 0;
  1663. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1664. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1665. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1666. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1667. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1668. }
  1669. tmp += sum;
  1670. }
  1671. #endif
  1672. // sum up partial sums and write back result
  1673. #pragma unroll
  1674. for (int mask = 16; mask > 0; mask >>= 1) {
  1675. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1676. }
  1677. if (tid == 0) {
  1678. dst[row] = tmp;
  1679. }
  1680. }
  1681. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1682. const half * x = (const half *) vx;
  1683. // automatic half -> float type cast if dfloat == float
  1684. v.x = x[ib + iqs + 0];
  1685. v.y = x[ib + iqs + 1];
  1686. }
  1687. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1688. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1689. if (ix >= kx_padded) {
  1690. return;
  1691. }
  1692. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1693. const int i_padded = iy*kx_padded + ix;
  1694. block_q8_1 * y = (block_q8_1 *) vy;
  1695. const int ib = i_padded / QK8_1; // block index
  1696. const int iqs = i_padded % QK8_1; // quant index
  1697. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1698. float amax = fabsf(xi);
  1699. float sum = xi;
  1700. #pragma unroll
  1701. for (int mask = 16; mask > 0; mask >>= 1) {
  1702. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1703. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1704. }
  1705. const float d = amax / 127;
  1706. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1707. y[ib].qs[iqs] = q;
  1708. if (iqs > 0) {
  1709. return;
  1710. }
  1711. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1712. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1713. }
  1714. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1715. static __global__ void k_get_rows(
  1716. const void * src0, const int32_t * src1, dst_t * dst,
  1717. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1718. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1719. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1720. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1721. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1722. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1723. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1724. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1725. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1726. if (i00 >= ne00) {
  1727. return;
  1728. }
  1729. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1730. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1731. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1732. const int ib = i00/qk; // block index
  1733. const int iqs = (i00%qk)/qr; // quant index
  1734. const int iybs = i00 - i00%qk; // dst block start index
  1735. const int y_offset = qr == 1 ? 1 : qk/2;
  1736. // dequantize
  1737. dfloat2 v;
  1738. dequantize_kernel(src0_row, ib, iqs, v);
  1739. dst_row[iybs + iqs + 0] = v.x;
  1740. dst_row[iybs + iqs + y_offset] = v.y;
  1741. }
  1742. template<typename src0_t, typename dst_t>
  1743. static __global__ void k_get_rows_float(
  1744. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1745. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1746. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1747. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1748. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1749. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1750. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1751. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1752. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1753. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1754. if (i00 >= ne00) {
  1755. return;
  1756. }
  1757. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1758. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1759. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1760. dst_row[i00] = src0_row[i00];
  1761. }
  1762. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1763. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1764. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  1765. if (i >= k) {
  1766. return;
  1767. }
  1768. const int ib = i/qk; // block index
  1769. const int iqs = (i%qk)/qr; // quant index
  1770. const int iybs = i - i%qk; // y block start index
  1771. const int y_offset = qr == 1 ? 1 : qk/2;
  1772. // dequantize
  1773. dfloat2 v;
  1774. dequantize_kernel(vx, ib, iqs, v);
  1775. y[iybs + iqs + 0] = v.x;
  1776. y[iybs + iqs + y_offset] = v.y;
  1777. }
  1778. template <typename src_t, typename dst_t>
  1779. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1780. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1781. if (i >= k) {
  1782. return;
  1783. }
  1784. const src_t * x = (src_t *) vx;
  1785. y[i] = x[i];
  1786. }
  1787. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1788. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1789. #define VDR_Q4_0_Q8_1_MMVQ 2
  1790. #define VDR_Q4_0_Q8_1_MMQ 4
  1791. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1792. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1793. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1794. int sumi = 0;
  1795. #pragma unroll
  1796. for (int i = 0; i < vdr; ++i) {
  1797. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1798. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1799. // SIMD dot product of quantized values
  1800. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1801. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1802. }
  1803. const float2 ds8f = __half22float2(ds8);
  1804. // second part effectively subtracts 8 from each quant value
  1805. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1806. #else
  1807. bad_arch();
  1808. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1809. }
  1810. #define VDR_Q4_1_Q8_1_MMVQ 2
  1811. #define VDR_Q4_1_Q8_1_MMQ 4
  1812. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1813. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1814. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1815. int sumi = 0;
  1816. #pragma unroll
  1817. for (int i = 0; i < vdr; ++i) {
  1818. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1819. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1820. // SIMD dot product of quantized values
  1821. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1822. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1823. }
  1824. #ifdef GGML_CUDA_F16
  1825. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1826. const float d4d8 = tmp.x;
  1827. const float m4s8 = tmp.y;
  1828. #else
  1829. const float2 dm4f = __half22float2(dm4);
  1830. const float2 ds8f = __half22float2(ds8);
  1831. const float d4d8 = dm4f.x * ds8f.x;
  1832. const float m4s8 = dm4f.y * ds8f.y;
  1833. #endif // GGML_CUDA_F16
  1834. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1835. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1836. #else
  1837. bad_arch();
  1838. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1839. }
  1840. #define VDR_Q5_0_Q8_1_MMVQ 2
  1841. #define VDR_Q5_0_Q8_1_MMQ 4
  1842. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1843. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1844. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1845. int sumi = 0;
  1846. #pragma unroll
  1847. for (int i = 0; i < vdr; ++i) {
  1848. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1849. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1850. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1851. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1852. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1853. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1854. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1855. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1856. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1857. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1858. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1859. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1860. }
  1861. const float2 ds8f = __half22float2(ds8);
  1862. // second part effectively subtracts 16 from each quant value
  1863. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1864. #else
  1865. bad_arch();
  1866. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1867. }
  1868. #define VDR_Q5_1_Q8_1_MMVQ 2
  1869. #define VDR_Q5_1_Q8_1_MMQ 4
  1870. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1871. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1872. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1873. int sumi = 0;
  1874. #pragma unroll
  1875. for (int i = 0; i < vdr; ++i) {
  1876. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1877. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1878. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1879. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1880. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1881. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1882. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1883. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1884. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1885. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1886. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1887. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1888. }
  1889. #ifdef GGML_CUDA_F16
  1890. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1891. const float d5d8 = tmp.x;
  1892. const float m5s8 = tmp.y;
  1893. #else
  1894. const float2 dm5f = __half22float2(dm5);
  1895. const float2 ds8f = __half22float2(ds8);
  1896. const float d5d8 = dm5f.x * ds8f.x;
  1897. const float m5s8 = dm5f.y * ds8f.y;
  1898. #endif // GGML_CUDA_F16
  1899. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1900. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1901. #else
  1902. bad_arch();
  1903. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1904. }
  1905. #define VDR_Q8_0_Q8_1_MMVQ 2
  1906. #define VDR_Q8_0_Q8_1_MMQ 8
  1907. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1908. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1909. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1910. int sumi = 0;
  1911. #pragma unroll
  1912. for (int i = 0; i < vdr; ++i) {
  1913. // SIMD dot product of quantized values
  1914. sumi = __dp4a(v[i], u[i], sumi);
  1915. }
  1916. return d8_0*d8_1 * sumi;
  1917. #else
  1918. bad_arch();
  1919. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1920. }
  1921. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1922. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1923. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1924. int sumi = 0;
  1925. #pragma unroll
  1926. for (int i = 0; i < vdr; ++i) {
  1927. // SIMD dot product of quantized values
  1928. sumi = __dp4a(v[i], u[i], sumi);
  1929. }
  1930. #ifdef GGML_CUDA_F16
  1931. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1932. const float d8d8 = tmp.x;
  1933. const float m8s8 = tmp.y;
  1934. #else
  1935. const float2 dm8f = __half22float2(dm8);
  1936. const float2 ds8f = __half22float2(ds8);
  1937. const float d8d8 = dm8f.x * ds8f.x;
  1938. const float m8s8 = dm8f.y * ds8f.y;
  1939. #endif // GGML_CUDA_F16
  1940. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1941. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1942. #else
  1943. bad_arch();
  1944. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1945. }
  1946. #define VDR_Q2_K_Q8_1_MMVQ 1
  1947. #define VDR_Q2_K_Q8_1_MMQ 2
  1948. // contiguous v/x values
  1949. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1950. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1951. const half2 & dm2, const float * __restrict__ d8) {
  1952. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1953. float sumf_d = 0.0f;
  1954. float sumf_m = 0.0f;
  1955. #pragma unroll
  1956. for (int i = 0; i < QR2_K; ++i) {
  1957. const int sc = scales[2*i];
  1958. const int vi = (v >> (2*i)) & 0x03030303;
  1959. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1960. // fill int with 4x m
  1961. int m = sc >> 4;
  1962. m |= m << 8;
  1963. m |= m << 16;
  1964. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1965. }
  1966. const float2 dm2f = __half22float2(dm2);
  1967. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1968. #else
  1969. bad_arch();
  1970. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1971. }
  1972. // contiguous u/y values
  1973. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1974. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1975. const half2 & dm2, const float & d8) {
  1976. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1977. int sumi_d = 0;
  1978. int sumi_m = 0;
  1979. #pragma unroll
  1980. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1981. int sumi_d_sc = 0;
  1982. const int sc = scales[i0 / (QI8_1/2)];
  1983. // fill int with 4x m
  1984. int m = sc >> 4;
  1985. m |= m << 8;
  1986. m |= m << 16;
  1987. #pragma unroll
  1988. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1989. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1990. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1991. }
  1992. sumi_d += sumi_d_sc * (sc & 0xF);
  1993. }
  1994. const float2 dm2f = __half22float2(dm2);
  1995. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1996. #else
  1997. bad_arch();
  1998. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1999. }
  2000. #define VDR_Q3_K_Q8_1_MMVQ 1
  2001. #define VDR_Q3_K_Q8_1_MMQ 2
  2002. // contiguous v/x values
  2003. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2004. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2005. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2006. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2007. float sumf = 0.0f;
  2008. #pragma unroll
  2009. for (int i = 0; i < QR3_K; ++i) {
  2010. const int isc = scale_offset + 2*i;
  2011. const int isc_low = isc % (QK_K/32);
  2012. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2013. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2014. const int isc_high = isc % (QK_K/64);
  2015. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2016. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2017. const int sc = (sc_low | sc_high) - 32;
  2018. const int vil = (vl >> (2*i)) & 0x03030303;
  2019. const int vih = ((vh >> i) << 2) & 0x04040404;
  2020. const int vi = __vsubss4(vil, vih);
  2021. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2022. }
  2023. return d3 * sumf;
  2024. #else
  2025. bad_arch();
  2026. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2027. }
  2028. // contiguous u/y values
  2029. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2030. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2031. const float & d3, const float & d8) {
  2032. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2033. int sumi = 0;
  2034. #pragma unroll
  2035. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2036. int sumi_sc = 0;
  2037. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2038. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2039. }
  2040. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2041. }
  2042. return d3*d8 * sumi;
  2043. #else
  2044. bad_arch();
  2045. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2046. }
  2047. #define VDR_Q4_K_Q8_1_MMVQ 2
  2048. #define VDR_Q4_K_Q8_1_MMQ 8
  2049. // contiguous v/x values
  2050. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2051. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2052. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2053. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2054. float sumf_d = 0.0f;
  2055. float sumf_m = 0.0f;
  2056. #pragma unroll
  2057. for (int i = 0; i < QR4_K; ++i) {
  2058. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2059. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2060. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2061. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2062. sumf_d += d8[i] * (dot1 * sc[i]);
  2063. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2064. }
  2065. const float2 dm4f = __half22float2(dm4);
  2066. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2067. #else
  2068. bad_arch();
  2069. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2070. }
  2071. // contiguous u/y values
  2072. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2073. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2074. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2075. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2076. float sumf_d = 0.0f;
  2077. float sumf_m = 0.0f;
  2078. #pragma unroll
  2079. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2080. int sumi_d = 0;
  2081. #pragma unroll
  2082. for (int j = 0; j < QI8_1; ++j) {
  2083. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2084. }
  2085. const float2 ds8f = __half22float2(ds8[i]);
  2086. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2087. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2088. }
  2089. const float2 dm4f = __half22float2(dm4);
  2090. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2091. #else
  2092. bad_arch();
  2093. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2094. }
  2095. #define VDR_Q5_K_Q8_1_MMVQ 2
  2096. #define VDR_Q5_K_Q8_1_MMQ 8
  2097. // contiguous v/x values
  2098. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2099. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2100. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2101. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2102. float sumf_d = 0.0f;
  2103. float sumf_m = 0.0f;
  2104. #pragma unroll
  2105. for (int i = 0; i < QR5_K; ++i) {
  2106. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2107. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2108. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2109. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2110. const int v0i = vl0i | vh0i;
  2111. const int v1i = vl1i | vh1i;
  2112. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2113. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2114. sumf_d += d8[i] * (dot1 * sc[i]);
  2115. sumf_m += d8[i] * (dot2 * m[i]);
  2116. }
  2117. const float2 dm5f = __half22float2(dm5);
  2118. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2119. #else
  2120. bad_arch();
  2121. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2122. }
  2123. // contiguous u/y values
  2124. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2125. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2126. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2127. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2128. float sumf_d = 0.0f;
  2129. float sumf_m = 0.0f;
  2130. #pragma unroll
  2131. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2132. int sumi_d = 0;
  2133. #pragma unroll
  2134. for (int j = 0; j < QI8_1; ++j) {
  2135. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2136. }
  2137. const float2 ds8f = __half22float2(ds8[i]);
  2138. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2139. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2140. }
  2141. const float2 dm4f = __half22float2(dm4);
  2142. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2143. #else
  2144. bad_arch();
  2145. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2146. }
  2147. #define VDR_Q6_K_Q8_1_MMVQ 1
  2148. #define VDR_Q6_K_Q8_1_MMQ 8
  2149. // contiguous v/x values
  2150. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2151. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2152. const float & d, const float * __restrict__ d8) {
  2153. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2154. float sumf = 0.0f;
  2155. #pragma unroll
  2156. for (int i = 0; i < QR6_K; ++i) {
  2157. const int sc = scales[4*i];
  2158. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2159. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2160. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2161. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2162. }
  2163. return d*sumf;
  2164. #else
  2165. bad_arch();
  2166. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2167. }
  2168. // contiguous u/y values
  2169. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2170. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2171. const float & d6, const float * __restrict__ d8) {
  2172. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2173. float sumf_d = 0.0f;
  2174. #pragma unroll
  2175. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2176. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2177. #pragma unroll
  2178. for (int i = i0; i < i0 + 2; ++i) {
  2179. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2180. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2181. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2182. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2183. }
  2184. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2185. }
  2186. return d6 * sumf_d;
  2187. #else
  2188. bad_arch();
  2189. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2190. }
  2191. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2192. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2193. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2194. int v[VDR_Q4_0_Q8_1_MMVQ];
  2195. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2196. #pragma unroll
  2197. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2198. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2199. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2200. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2201. }
  2202. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2203. }
  2204. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2205. (void)x_qh; (void)x_sc;
  2206. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2207. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2208. *x_ql = tile_x_qs;
  2209. *x_dm = (half2 *) tile_x_d;
  2210. }
  2211. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2212. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2213. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2214. (void)x_qh; (void)x_sc;
  2215. GGML_CUDA_ASSUME(i_offset >= 0);
  2216. GGML_CUDA_ASSUME(i_offset < nwarps);
  2217. GGML_CUDA_ASSUME(k >= 0);
  2218. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2219. const int kbx = k / QI4_0;
  2220. const int kqsx = k % QI4_0;
  2221. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2222. float * x_dmf = (float *) x_dm;
  2223. #pragma unroll
  2224. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2225. int i = i0 + i_offset;
  2226. if (need_check) {
  2227. i = min(i, i_max);
  2228. }
  2229. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2230. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2231. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2232. }
  2233. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2234. const int kbxd = k % blocks_per_tile_x_row;
  2235. #pragma unroll
  2236. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2237. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2238. if (need_check) {
  2239. i = min(i, i_max);
  2240. }
  2241. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2242. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2243. }
  2244. }
  2245. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2246. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2247. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2248. (void)x_qh; (void)x_sc;
  2249. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2250. const float * x_dmf = (const float *) x_dm;
  2251. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2252. #pragma unroll
  2253. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2254. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2255. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2256. }
  2257. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2258. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2259. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2260. }
  2261. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2262. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2263. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2264. int v[VDR_Q4_1_Q8_1_MMVQ];
  2265. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2266. #pragma unroll
  2267. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2268. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2269. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2270. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2271. }
  2272. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2273. }
  2274. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2275. (void)x_qh; (void)x_sc;
  2276. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2277. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2278. *x_ql = tile_x_qs;
  2279. *x_dm = tile_x_dm;
  2280. }
  2281. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2282. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2283. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2284. (void)x_qh; (void)x_sc;
  2285. GGML_CUDA_ASSUME(i_offset >= 0);
  2286. GGML_CUDA_ASSUME(i_offset < nwarps);
  2287. GGML_CUDA_ASSUME(k >= 0);
  2288. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2289. const int kbx = k / QI4_1;
  2290. const int kqsx = k % QI4_1;
  2291. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2292. #pragma unroll
  2293. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2294. int i = i0 + i_offset;
  2295. if (need_check) {
  2296. i = min(i, i_max);
  2297. }
  2298. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2299. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2300. }
  2301. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2302. const int kbxd = k % blocks_per_tile_x_row;
  2303. #pragma unroll
  2304. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2305. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2306. if (need_check) {
  2307. i = min(i, i_max);
  2308. }
  2309. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2310. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2311. }
  2312. }
  2313. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2314. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2315. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2316. (void)x_qh; (void)x_sc;
  2317. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2318. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2319. #pragma unroll
  2320. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2321. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2322. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2323. }
  2324. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2325. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2326. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2327. }
  2328. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2329. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2330. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2331. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2332. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2333. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2334. #pragma unroll
  2335. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2336. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2337. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2338. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2339. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2340. }
  2341. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2342. }
  2343. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2344. (void)x_qh; (void)x_sc;
  2345. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2346. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2347. *x_ql = tile_x_ql;
  2348. *x_dm = (half2 *) tile_x_d;
  2349. }
  2350. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2351. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2352. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2353. (void)x_qh; (void)x_sc;
  2354. GGML_CUDA_ASSUME(i_offset >= 0);
  2355. GGML_CUDA_ASSUME(i_offset < nwarps);
  2356. GGML_CUDA_ASSUME(k >= 0);
  2357. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2358. const int kbx = k / QI5_0;
  2359. const int kqsx = k % QI5_0;
  2360. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2361. #pragma unroll
  2362. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2363. int i = i0 + i_offset;
  2364. if (need_check) {
  2365. i = min(i, i_max);
  2366. }
  2367. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2368. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2369. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2370. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2371. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2372. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2373. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2374. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2375. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2376. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2377. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2378. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2379. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2380. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2381. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2382. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2383. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2384. }
  2385. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2386. const int kbxd = k % blocks_per_tile_x_row;
  2387. float * x_dmf = (float *) x_dm;
  2388. #pragma unroll
  2389. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2390. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2391. if (need_check) {
  2392. i = min(i, i_max);
  2393. }
  2394. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2395. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2396. }
  2397. }
  2398. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2399. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2400. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2401. (void)x_qh; (void)x_sc;
  2402. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2403. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2404. const float * x_dmf = (const float *) x_dm;
  2405. const float * y_df = (const float *) y_ds;
  2406. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2407. #pragma unroll
  2408. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2409. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2410. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2411. }
  2412. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2413. (&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)]);
  2414. }
  2415. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2416. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2417. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2418. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2419. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2420. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2421. #pragma unroll
  2422. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2423. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2424. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2425. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2426. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2427. }
  2428. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2429. }
  2430. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2431. (void)x_qh; (void)x_sc;
  2432. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2433. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2434. *x_ql = tile_x_ql;
  2435. *x_dm = tile_x_dm;
  2436. }
  2437. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2438. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2439. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2440. (void)x_qh; (void)x_sc;
  2441. GGML_CUDA_ASSUME(i_offset >= 0);
  2442. GGML_CUDA_ASSUME(i_offset < nwarps);
  2443. GGML_CUDA_ASSUME(k >= 0);
  2444. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2445. const int kbx = k / QI5_1;
  2446. const int kqsx = k % QI5_1;
  2447. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2448. #pragma unroll
  2449. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2450. int i = i0 + i_offset;
  2451. if (need_check) {
  2452. i = min(i, i_max);
  2453. }
  2454. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2455. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2456. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2457. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2458. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2459. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2460. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2461. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2462. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2463. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2464. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2465. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2466. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2467. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2468. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2469. }
  2470. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2471. const int kbxd = k % blocks_per_tile_x_row;
  2472. #pragma unroll
  2473. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2474. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2475. if (need_check) {
  2476. i = min(i, i_max);
  2477. }
  2478. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2479. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2480. }
  2481. }
  2482. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2483. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2484. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2485. (void)x_qh; (void)x_sc;
  2486. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2487. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2488. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2489. #pragma unroll
  2490. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2491. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2492. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2493. }
  2494. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2495. (&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)]);
  2496. }
  2497. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2498. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2499. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2500. int v[VDR_Q8_0_Q8_1_MMVQ];
  2501. int u[VDR_Q8_0_Q8_1_MMVQ];
  2502. #pragma unroll
  2503. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2504. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2505. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2506. }
  2507. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2508. }
  2509. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2510. (void)x_qh; (void)x_sc;
  2511. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2512. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2513. *x_ql = tile_x_qs;
  2514. *x_dm = (half2 *) tile_x_d;
  2515. }
  2516. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2517. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2518. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2519. (void)x_qh; (void)x_sc;
  2520. GGML_CUDA_ASSUME(i_offset >= 0);
  2521. GGML_CUDA_ASSUME(i_offset < nwarps);
  2522. GGML_CUDA_ASSUME(k >= 0);
  2523. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2524. const int kbx = k / QI8_0;
  2525. const int kqsx = k % QI8_0;
  2526. float * x_dmf = (float *) x_dm;
  2527. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2528. #pragma unroll
  2529. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2530. int i = i0 + i_offset;
  2531. if (need_check) {
  2532. i = min(i, i_max);
  2533. }
  2534. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2535. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2536. }
  2537. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2538. const int kbxd = k % blocks_per_tile_x_row;
  2539. #pragma unroll
  2540. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2541. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2542. if (need_check) {
  2543. i = min(i, i_max);
  2544. }
  2545. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2546. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2547. }
  2548. }
  2549. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2550. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2551. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2552. (void)x_qh; (void)x_sc;
  2553. const float * x_dmf = (const float *) x_dm;
  2554. const float * y_df = (const float *) y_ds;
  2555. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2556. (&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],
  2557. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2558. }
  2559. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2560. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2561. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2562. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2563. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2564. const uint8_t * scales = bq2_K->scales + scale_offset;
  2565. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2566. int u[QR2_K];
  2567. float d8[QR2_K];
  2568. #pragma unroll
  2569. for (int i = 0; i < QR2_K; ++ i) {
  2570. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2571. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2572. }
  2573. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2574. }
  2575. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2576. (void)x_qh;
  2577. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2578. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2579. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2580. *x_ql = tile_x_ql;
  2581. *x_dm = tile_x_dm;
  2582. *x_sc = tile_x_sc;
  2583. }
  2584. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2585. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2586. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2587. (void)x_qh;
  2588. GGML_CUDA_ASSUME(i_offset >= 0);
  2589. GGML_CUDA_ASSUME(i_offset < nwarps);
  2590. GGML_CUDA_ASSUME(k >= 0);
  2591. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2592. const int kbx = k / QI2_K;
  2593. const int kqsx = k % QI2_K;
  2594. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2595. #pragma unroll
  2596. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2597. int i = i0 + i_offset;
  2598. if (need_check) {
  2599. i = min(i, i_max);
  2600. }
  2601. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2602. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2603. }
  2604. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2605. const int kbxd = k % blocks_per_tile_x_row;
  2606. #pragma unroll
  2607. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2608. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2609. if (need_check) {
  2610. i = min(i, i_max);
  2611. }
  2612. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2613. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2614. }
  2615. #pragma unroll
  2616. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2617. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2618. if (need_check) {
  2619. i = min(i, i_max);
  2620. }
  2621. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2622. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2623. }
  2624. }
  2625. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2626. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2627. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2628. (void)x_qh;
  2629. const int kbx = k / QI2_K;
  2630. const int ky = (k % QI2_K) * QR2_K;
  2631. const float * y_df = (const float *) y_ds;
  2632. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2633. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2634. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2635. #pragma unroll
  2636. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2637. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2638. }
  2639. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2640. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2641. 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]);
  2642. }
  2643. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2644. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2645. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2646. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2647. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2648. const float d = bq3_K->d;
  2649. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2650. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2651. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2652. int u[QR3_K];
  2653. float d8[QR3_K];
  2654. #pragma unroll
  2655. for (int i = 0; i < QR3_K; ++i) {
  2656. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2657. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2658. }
  2659. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2660. }
  2661. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2662. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2663. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2664. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2665. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2666. *x_ql = tile_x_ql;
  2667. *x_dm = tile_x_dm;
  2668. *x_qh = tile_x_qh;
  2669. *x_sc = tile_x_sc;
  2670. }
  2671. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2672. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2673. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2674. GGML_CUDA_ASSUME(i_offset >= 0);
  2675. GGML_CUDA_ASSUME(i_offset < nwarps);
  2676. GGML_CUDA_ASSUME(k >= 0);
  2677. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2678. const int kbx = k / QI3_K;
  2679. const int kqsx = k % QI3_K;
  2680. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2681. #pragma unroll
  2682. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2683. int i = i0 + i_offset;
  2684. if (need_check) {
  2685. i = min(i, i_max);
  2686. }
  2687. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2688. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2689. }
  2690. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2691. const int kbxd = k % blocks_per_tile_x_row;
  2692. float * x_dmf = (float *) x_dm;
  2693. #pragma unroll
  2694. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2695. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2696. if (need_check) {
  2697. i = min(i, i_max);
  2698. }
  2699. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2700. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2701. }
  2702. #pragma unroll
  2703. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2704. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2705. if (need_check) {
  2706. i = min(i, i_max);
  2707. }
  2708. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2709. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2710. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2711. }
  2712. #pragma unroll
  2713. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2714. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2715. if (need_check) {
  2716. i = min(i, i_max);
  2717. }
  2718. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2719. const int ksc = k % (QI3_K/4);
  2720. const int ksc_low = ksc % (QI3_K/8);
  2721. const int shift_low = 4 * (ksc / (QI3_K/8));
  2722. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2723. const int ksc_high = QI3_K/8;
  2724. const int shift_high = 2 * ksc;
  2725. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2726. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2727. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2728. }
  2729. }
  2730. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2731. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2732. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2733. const int kbx = k / QI3_K;
  2734. const int ky = (k % QI3_K) * QR3_K;
  2735. const float * x_dmf = (const float *) x_dm;
  2736. const float * y_df = (const float *) y_ds;
  2737. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2738. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2739. #pragma unroll
  2740. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2741. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2742. const int shift = 2 * ((ky % 32) / 8);
  2743. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2744. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2745. const int vlh = (vh << 2) & 0x04040404;
  2746. v[l] = __vsubss4(vll, vlh);
  2747. }
  2748. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2749. 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]);
  2750. }
  2751. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2752. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2753. #ifndef GGML_QKK_64
  2754. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2755. int v[2];
  2756. int u[2*QR4_K];
  2757. float d8[QR4_K];
  2758. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2759. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2760. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2761. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2762. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2763. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2764. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2765. v[0] = q4[0];
  2766. v[1] = q4[4];
  2767. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2768. uint16_t aux[2];
  2769. const int j = bq8_offset/2;
  2770. if (j < 2) {
  2771. aux[0] = scales[j+0] & 0x3f3f;
  2772. aux[1] = scales[j+2] & 0x3f3f;
  2773. } else {
  2774. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2775. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2776. }
  2777. const uint8_t * sc = (const uint8_t *)aux;
  2778. const uint8_t * m = sc + 2;
  2779. for (int i = 0; i < QR4_K; ++i) {
  2780. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2781. d8[i] = __low2half(bq8i->ds);
  2782. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2783. u[2*i+0] = q8[0];
  2784. u[2*i+1] = q8[4];
  2785. }
  2786. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2787. #else
  2788. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2789. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2790. float sumf_d = 0.0f;
  2791. float sumf_m = 0.0f;
  2792. uint16_t aux16[2];
  2793. const uint8_t * s = (const uint8_t *)aux16;
  2794. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2795. aux16[0] = a[0] & 0x0f0f;
  2796. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2797. const float dall = bq4_K->dm[0];
  2798. const float dmin = bq4_K->dm[1];
  2799. const float d8_1 = __low2float(bq8_1[0].ds);
  2800. const float d8_2 = __low2float(bq8_1[1].ds);
  2801. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2802. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2803. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2804. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2805. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2806. const int v1 = q4[0];
  2807. const int v2 = q4[4];
  2808. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2809. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2810. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2811. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2812. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2813. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2814. return dall * sumf_d - dmin * sumf_m;
  2815. #else
  2816. bad_arch();
  2817. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2818. #endif
  2819. }
  2820. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2821. (void)x_qh;
  2822. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2823. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2824. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2825. *x_ql = tile_x_ql;
  2826. *x_dm = tile_x_dm;
  2827. *x_sc = tile_x_sc;
  2828. }
  2829. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2830. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2831. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2832. (void)x_qh;
  2833. GGML_CUDA_ASSUME(i_offset >= 0);
  2834. GGML_CUDA_ASSUME(i_offset < nwarps);
  2835. GGML_CUDA_ASSUME(k >= 0);
  2836. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2837. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2838. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2839. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2840. #pragma unroll
  2841. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2842. int i = i0 + i_offset;
  2843. if (need_check) {
  2844. i = min(i, i_max);
  2845. }
  2846. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2847. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2848. }
  2849. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2850. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2851. #pragma unroll
  2852. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2853. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2854. if (need_check) {
  2855. i = min(i, i_max);
  2856. }
  2857. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2858. #if QK_K == 256
  2859. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2860. #else
  2861. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2862. #endif
  2863. }
  2864. #pragma unroll
  2865. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2866. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2867. if (need_check) {
  2868. i = min(i, i_max);
  2869. }
  2870. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2871. const int * scales = (const int *) bxi->scales;
  2872. const int ksc = k % (WARP_SIZE/8);
  2873. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2874. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2875. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2876. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2877. }
  2878. }
  2879. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2880. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2881. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2882. (void)x_qh;
  2883. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2884. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2885. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2886. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2887. }
  2888. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2889. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2890. #ifndef GGML_QKK_64
  2891. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2892. int vl[2];
  2893. int vh[2];
  2894. int u[2*QR5_K];
  2895. float d8[QR5_K];
  2896. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2897. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2898. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2899. vl[0] = ql[0];
  2900. vl[1] = ql[4];
  2901. vh[0] = qh[0] >> bq8_offset;
  2902. vh[1] = qh[4] >> bq8_offset;
  2903. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2904. uint16_t aux[2];
  2905. const int j = bq8_offset/2;
  2906. if (j < 2) {
  2907. aux[0] = scales[j+0] & 0x3f3f;
  2908. aux[1] = scales[j+2] & 0x3f3f;
  2909. } else {
  2910. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2911. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2912. }
  2913. const uint8_t * sc = (const uint8_t *)aux;
  2914. const uint8_t * m = sc + 2;
  2915. #pragma unroll
  2916. for (int i = 0; i < QR5_K; ++i) {
  2917. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2918. d8[i] = __low2float(bq8i->ds);
  2919. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2920. u[2*i+0] = q8[0];
  2921. u[2*i+1] = q8[4];
  2922. }
  2923. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2924. #else
  2925. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2926. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2927. const int8_t * s = bq5_K->scales;
  2928. const float d = bq5_K->d;
  2929. const float d8_1 = __low2half(bq8_1[0].ds);
  2930. const float d8_2 = __low2half(bq8_1[1].ds);
  2931. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2932. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2933. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2934. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2935. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2936. const int vl1 = ql[0];
  2937. const int vl2 = ql[4];
  2938. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2939. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2940. const int in = step%8; // 0, 4, 0, 4
  2941. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2942. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2943. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2944. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2945. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2946. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2947. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2948. return d * sumf_d;
  2949. #else
  2950. bad_arch();
  2951. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2952. #endif
  2953. }
  2954. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2955. (void)x_qh;
  2956. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2957. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2958. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2959. *x_ql = tile_x_ql;
  2960. *x_dm = tile_x_dm;
  2961. *x_sc = tile_x_sc;
  2962. }
  2963. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2964. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2965. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2966. (void)x_qh;
  2967. GGML_CUDA_ASSUME(i_offset >= 0);
  2968. GGML_CUDA_ASSUME(i_offset < nwarps);
  2969. GGML_CUDA_ASSUME(k >= 0);
  2970. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2971. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2972. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2973. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2974. #pragma unroll
  2975. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2976. int i = i0 + i_offset;
  2977. if (need_check) {
  2978. i = min(i, i_max);
  2979. }
  2980. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2981. const int ky = QR5_K*kqsx;
  2982. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2983. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2984. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2985. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2986. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2987. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2988. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2989. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2990. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2991. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2992. }
  2993. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2994. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2995. #pragma unroll
  2996. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2997. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2998. if (need_check) {
  2999. i = min(i, i_max);
  3000. }
  3001. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3002. #if QK_K == 256
  3003. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3004. #endif
  3005. }
  3006. #pragma unroll
  3007. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3008. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3009. if (need_check) {
  3010. i = min(i, i_max);
  3011. }
  3012. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3013. const int * scales = (const int *) bxi->scales;
  3014. const int ksc = k % (WARP_SIZE/8);
  3015. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3016. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3017. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3018. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3019. }
  3020. }
  3021. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3022. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3023. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3024. (void)x_qh;
  3025. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3026. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3027. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3028. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3029. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3030. }
  3031. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3032. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3033. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3034. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3035. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3036. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3037. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3038. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3039. const int8_t * scales = bq6_K->scales + scale_offset;
  3040. int u[QR6_K];
  3041. float d8[QR6_K];
  3042. #pragma unroll
  3043. for (int i = 0; i < QR6_K; ++i) {
  3044. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3045. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3046. }
  3047. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3048. }
  3049. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3050. (void)x_qh;
  3051. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3052. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3053. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3054. *x_ql = tile_x_ql;
  3055. *x_dm = tile_x_dm;
  3056. *x_sc = tile_x_sc;
  3057. }
  3058. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3059. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3060. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3061. (void)x_qh;
  3062. GGML_CUDA_ASSUME(i_offset >= 0);
  3063. GGML_CUDA_ASSUME(i_offset < nwarps);
  3064. GGML_CUDA_ASSUME(k >= 0);
  3065. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3066. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3067. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3068. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3069. #pragma unroll
  3070. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3071. int i = i0 + i_offset;
  3072. if (need_check) {
  3073. i = min(i, i_max);
  3074. }
  3075. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3076. const int ky = QR6_K*kqsx;
  3077. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3078. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3079. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3080. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3081. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3082. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3083. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3084. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3085. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3086. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3087. }
  3088. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3089. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3090. float * x_dmf = (float *) x_dm;
  3091. #pragma unroll
  3092. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3093. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3094. if (need_check) {
  3095. i = min(i, i_max);
  3096. }
  3097. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3098. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3099. }
  3100. #pragma unroll
  3101. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3102. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3103. if (need_check) {
  3104. i = min(i, i_max);
  3105. }
  3106. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3107. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3108. }
  3109. }
  3110. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3111. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3112. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3113. (void)x_qh;
  3114. const float * x_dmf = (const float *) x_dm;
  3115. const float * y_df = (const float *) y_ds;
  3116. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3117. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3118. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3119. 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]);
  3120. }
  3121. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3122. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3123. #if QK_K == 256
  3124. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3125. #if QR2_XXS == 8
  3126. const int ib32 = iqs;
  3127. const uint16_t * q2 = bq2->qs + 4*ib32;
  3128. const uint8_t * aux8 = (const uint8_t *)q2;
  3129. const int8_t * q8 = bq8_1[ib32].qs;
  3130. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3131. int sumi = 0;
  3132. for (int l = 0; l < 4; ++l) {
  3133. const uint8_t * grid = (const uint8_t *)(kgrid_iq2xxs + aux8[l]);
  3134. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3135. for (int j = 0; j < 8; ++j) {
  3136. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3137. }
  3138. q8 += 8;
  3139. aux32 >>= 7;
  3140. }
  3141. const float d = (float)bq2->d * (0.5f + aux32) * (float)bq8_1[ib32].ds.x * 0.25f;
  3142. return d * sumi;
  3143. #else
  3144. // iqs is 0...15
  3145. const int ib32 = iqs/2;
  3146. const int il = iqs%2;
  3147. const uint16_t * q2 = bq2->qs + 4*ib32;
  3148. const uint8_t * aux8 = (const uint8_t *)q2;
  3149. const uint8_t * grid1 = (const uint8_t *)(kgrid_iq2xxs + aux8[2*il+0]);
  3150. const uint8_t * grid2 = (const uint8_t *)(kgrid_iq2xxs + aux8[2*il+1]);
  3151. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3152. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * (float)bq8_1[ib32].ds.x * 0.25f;
  3153. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3154. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3155. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3156. int sumi1 = 0, sumi2 = 0;
  3157. for (int j = 0; j < 8; ++j) {
  3158. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3159. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3160. }
  3161. return d * (sumi1 + sumi2);
  3162. #endif
  3163. #else
  3164. assert(false);
  3165. return 0.f;
  3166. #endif
  3167. }
  3168. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3169. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3170. static __device__ __forceinline__ void mul_mat_q(
  3171. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3172. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3173. const block_q_t * x = (const block_q_t *) vx;
  3174. const block_q8_1 * y = (const block_q8_1 *) vy;
  3175. const int blocks_per_row_x = ncols_x / qk;
  3176. const int blocks_per_col_y = nrows_y / QK8_1;
  3177. const int blocks_per_warp = WARP_SIZE / qi;
  3178. const int & ncols_dst = ncols_y;
  3179. const int row_dst_0 = blockIdx.x*mmq_y;
  3180. const int & row_x_0 = row_dst_0;
  3181. const int col_dst_0 = blockIdx.y*mmq_x;
  3182. const int & col_y_0 = col_dst_0;
  3183. int * tile_x_ql = nullptr;
  3184. half2 * tile_x_dm = nullptr;
  3185. int * tile_x_qh = nullptr;
  3186. int * tile_x_sc = nullptr;
  3187. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3188. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3189. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3190. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3191. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3192. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3193. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3194. #pragma unroll
  3195. for (int ir = 0; ir < qr; ++ir) {
  3196. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3197. const int kbxd = kqs / QI8_1;
  3198. #pragma unroll
  3199. for (int i = 0; i < mmq_x; i += nwarps) {
  3200. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3201. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3202. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3203. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3204. }
  3205. #pragma unroll
  3206. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3207. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3208. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3209. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3210. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3211. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3212. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3213. if (need_sum) {
  3214. *dsi_dst = *dsi_src;
  3215. } else {
  3216. float * dfi_dst = (float *) dsi_dst;
  3217. *dfi_dst = __low2half(*dsi_src);
  3218. }
  3219. }
  3220. __syncthreads();
  3221. // #pragma unroll // unrolling this loop causes too much register pressure
  3222. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3223. #pragma unroll
  3224. for (int j = 0; j < mmq_x; j += nwarps) {
  3225. #pragma unroll
  3226. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3227. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3228. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3229. threadIdx.x + i, threadIdx.y + j, k);
  3230. }
  3231. }
  3232. }
  3233. __syncthreads();
  3234. }
  3235. }
  3236. #pragma unroll
  3237. for (int j = 0; j < mmq_x; j += nwarps) {
  3238. const int col_dst = col_dst_0 + j + threadIdx.y;
  3239. if (col_dst >= ncols_dst) {
  3240. return;
  3241. }
  3242. #pragma unroll
  3243. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3244. const int row_dst = row_dst_0 + threadIdx.x + i;
  3245. if (row_dst >= nrows_dst) {
  3246. continue;
  3247. }
  3248. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3249. }
  3250. }
  3251. }
  3252. #define MMQ_X_Q4_0_RDNA2 64
  3253. #define MMQ_Y_Q4_0_RDNA2 128
  3254. #define NWARPS_Q4_0_RDNA2 8
  3255. #define MMQ_X_Q4_0_RDNA1 64
  3256. #define MMQ_Y_Q4_0_RDNA1 64
  3257. #define NWARPS_Q4_0_RDNA1 8
  3258. #if defined(CUDA_USE_TENSOR_CORES)
  3259. #define MMQ_X_Q4_0_AMPERE 4
  3260. #define MMQ_Y_Q4_0_AMPERE 32
  3261. #define NWARPS_Q4_0_AMPERE 4
  3262. #else
  3263. #define MMQ_X_Q4_0_AMPERE 64
  3264. #define MMQ_Y_Q4_0_AMPERE 128
  3265. #define NWARPS_Q4_0_AMPERE 4
  3266. #endif
  3267. #define MMQ_X_Q4_0_PASCAL 64
  3268. #define MMQ_Y_Q4_0_PASCAL 64
  3269. #define NWARPS_Q4_0_PASCAL 8
  3270. template <bool need_check> static __global__ void
  3271. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3272. #if defined(RDNA3) || defined(RDNA2)
  3273. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3274. #endif // defined(RDNA3) || defined(RDNA2)
  3275. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3276. mul_mat_q4_0(
  3277. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3278. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3279. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3280. #if defined(RDNA3) || defined(RDNA2)
  3281. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3282. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3283. const int nwarps = NWARPS_Q4_0_RDNA2;
  3284. #else
  3285. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3286. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3287. const int nwarps = NWARPS_Q4_0_RDNA1;
  3288. #endif // defined(RDNA3) || defined(RDNA2)
  3289. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3290. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3291. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3292. #elif __CUDA_ARCH__ >= CC_VOLTA
  3293. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3294. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3295. const int nwarps = NWARPS_Q4_0_AMPERE;
  3296. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3297. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3298. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3299. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3300. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3301. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3302. const int nwarps = NWARPS_Q4_0_PASCAL;
  3303. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3304. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3305. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3306. #else
  3307. (void) vec_dot_q4_0_q8_1_mul_mat;
  3308. bad_arch();
  3309. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3310. }
  3311. #define MMQ_X_Q4_1_RDNA2 64
  3312. #define MMQ_Y_Q4_1_RDNA2 128
  3313. #define NWARPS_Q4_1_RDNA2 8
  3314. #define MMQ_X_Q4_1_RDNA1 64
  3315. #define MMQ_Y_Q4_1_RDNA1 64
  3316. #define NWARPS_Q4_1_RDNA1 8
  3317. #if defined(CUDA_USE_TENSOR_CORES)
  3318. #define MMQ_X_Q4_1_AMPERE 4
  3319. #define MMQ_Y_Q4_1_AMPERE 32
  3320. #define NWARPS_Q4_1_AMPERE 4
  3321. #else
  3322. #define MMQ_X_Q4_1_AMPERE 64
  3323. #define MMQ_Y_Q4_1_AMPERE 128
  3324. #define NWARPS_Q4_1_AMPERE 4
  3325. #endif
  3326. #define MMQ_X_Q4_1_PASCAL 64
  3327. #define MMQ_Y_Q4_1_PASCAL 64
  3328. #define NWARPS_Q4_1_PASCAL 8
  3329. template <bool need_check> static __global__ void
  3330. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3331. #if defined(RDNA3) || defined(RDNA2)
  3332. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3333. #endif // defined(RDNA3) || defined(RDNA2)
  3334. #elif __CUDA_ARCH__ < CC_VOLTA
  3335. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3336. #endif // __CUDA_ARCH__ < CC_VOLTA
  3337. mul_mat_q4_1(
  3338. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3339. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3340. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3341. #if defined(RDNA3) || defined(RDNA2)
  3342. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3343. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3344. const int nwarps = NWARPS_Q4_1_RDNA2;
  3345. #else
  3346. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3347. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3348. const int nwarps = NWARPS_Q4_1_RDNA1;
  3349. #endif // defined(RDNA3) || defined(RDNA2)
  3350. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3351. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3352. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3353. #elif __CUDA_ARCH__ >= CC_VOLTA
  3354. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3355. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3356. const int nwarps = NWARPS_Q4_1_AMPERE;
  3357. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3358. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3359. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3360. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3361. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3362. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3363. const int nwarps = NWARPS_Q4_1_PASCAL;
  3364. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3365. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3366. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3367. #else
  3368. (void) vec_dot_q4_1_q8_1_mul_mat;
  3369. bad_arch();
  3370. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3371. }
  3372. #define MMQ_X_Q5_0_RDNA2 64
  3373. #define MMQ_Y_Q5_0_RDNA2 128
  3374. #define NWARPS_Q5_0_RDNA2 8
  3375. #define MMQ_X_Q5_0_RDNA1 64
  3376. #define MMQ_Y_Q5_0_RDNA1 64
  3377. #define NWARPS_Q5_0_RDNA1 8
  3378. #if defined(CUDA_USE_TENSOR_CORES)
  3379. #define MMQ_X_Q5_0_AMPERE 4
  3380. #define MMQ_Y_Q5_0_AMPERE 32
  3381. #define NWARPS_Q5_0_AMPERE 4
  3382. #else
  3383. #define MMQ_X_Q5_0_AMPERE 128
  3384. #define MMQ_Y_Q5_0_AMPERE 64
  3385. #define NWARPS_Q5_0_AMPERE 4
  3386. #endif
  3387. #define MMQ_X_Q5_0_PASCAL 64
  3388. #define MMQ_Y_Q5_0_PASCAL 64
  3389. #define NWARPS_Q5_0_PASCAL 8
  3390. template <bool need_check> static __global__ void
  3391. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3392. #if defined(RDNA3) || defined(RDNA2)
  3393. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3394. #endif // defined(RDNA3) || defined(RDNA2)
  3395. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3396. mul_mat_q5_0(
  3397. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3398. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3399. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3400. #if defined(RDNA3) || defined(RDNA2)
  3401. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3402. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3403. const int nwarps = NWARPS_Q5_0_RDNA2;
  3404. #else
  3405. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3406. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3407. const int nwarps = NWARPS_Q5_0_RDNA1;
  3408. #endif // defined(RDNA3) || defined(RDNA2)
  3409. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3410. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3411. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3412. #elif __CUDA_ARCH__ >= CC_VOLTA
  3413. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3414. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3415. const int nwarps = NWARPS_Q5_0_AMPERE;
  3416. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3417. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3418. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3419. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3420. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3421. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3422. const int nwarps = NWARPS_Q5_0_PASCAL;
  3423. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3424. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3425. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3426. #else
  3427. (void) vec_dot_q5_0_q8_1_mul_mat;
  3428. bad_arch();
  3429. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3430. }
  3431. #define MMQ_X_Q5_1_RDNA2 64
  3432. #define MMQ_Y_Q5_1_RDNA2 128
  3433. #define NWARPS_Q5_1_RDNA2 8
  3434. #define MMQ_X_Q5_1_RDNA1 64
  3435. #define MMQ_Y_Q5_1_RDNA1 64
  3436. #define NWARPS_Q5_1_RDNA1 8
  3437. #if defined(CUDA_USE_TENSOR_CORES)
  3438. #define MMQ_X_Q5_1_AMPERE 4
  3439. #define MMQ_Y_Q5_1_AMPERE 32
  3440. #define NWARPS_Q5_1_AMPERE 4
  3441. #else
  3442. #define MMQ_X_Q5_1_AMPERE 128
  3443. #define MMQ_Y_Q5_1_AMPERE 64
  3444. #define NWARPS_Q5_1_AMPERE 4
  3445. #endif
  3446. #define MMQ_X_Q5_1_PASCAL 64
  3447. #define MMQ_Y_Q5_1_PASCAL 64
  3448. #define NWARPS_Q5_1_PASCAL 8
  3449. template <bool need_check> static __global__ void
  3450. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3451. #if defined(RDNA3) || defined(RDNA2)
  3452. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3453. #endif // defined(RDNA3) || defined(RDNA2)
  3454. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3455. mul_mat_q5_1(
  3456. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3457. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3458. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3459. #if defined(RDNA3) || defined(RDNA2)
  3460. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3461. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3462. const int nwarps = NWARPS_Q5_1_RDNA2;
  3463. #else
  3464. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3465. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3466. const int nwarps = NWARPS_Q5_1_RDNA1;
  3467. #endif // defined(RDNA3) || defined(RDNA2)
  3468. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3469. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3470. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3471. #elif __CUDA_ARCH__ >= CC_VOLTA
  3472. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3473. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3474. const int nwarps = NWARPS_Q5_1_AMPERE;
  3475. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3476. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3477. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3478. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3479. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3480. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3481. const int nwarps = NWARPS_Q5_1_PASCAL;
  3482. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3483. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3484. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3485. #else
  3486. (void) vec_dot_q5_1_q8_1_mul_mat;
  3487. bad_arch();
  3488. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3489. }
  3490. #define MMQ_X_Q8_0_RDNA2 64
  3491. #define MMQ_Y_Q8_0_RDNA2 128
  3492. #define NWARPS_Q8_0_RDNA2 8
  3493. #define MMQ_X_Q8_0_RDNA1 64
  3494. #define MMQ_Y_Q8_0_RDNA1 64
  3495. #define NWARPS_Q8_0_RDNA1 8
  3496. #if defined(CUDA_USE_TENSOR_CORES)
  3497. #define MMQ_X_Q8_0_AMPERE 4
  3498. #define MMQ_Y_Q8_0_AMPERE 32
  3499. #define NWARPS_Q8_0_AMPERE 4
  3500. #else
  3501. #define MMQ_X_Q8_0_AMPERE 128
  3502. #define MMQ_Y_Q8_0_AMPERE 64
  3503. #define NWARPS_Q8_0_AMPERE 4
  3504. #endif
  3505. #define MMQ_X_Q8_0_PASCAL 64
  3506. #define MMQ_Y_Q8_0_PASCAL 64
  3507. #define NWARPS_Q8_0_PASCAL 8
  3508. template <bool need_check> static __global__ void
  3509. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3510. #if defined(RDNA3) || defined(RDNA2)
  3511. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3512. #endif // defined(RDNA3) || defined(RDNA2)
  3513. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3514. mul_mat_q8_0(
  3515. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3516. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3517. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3518. #if defined(RDNA3) || defined(RDNA2)
  3519. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3520. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3521. const int nwarps = NWARPS_Q8_0_RDNA2;
  3522. #else
  3523. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3524. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3525. const int nwarps = NWARPS_Q8_0_RDNA1;
  3526. #endif // defined(RDNA3) || defined(RDNA2)
  3527. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3528. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3529. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3530. #elif __CUDA_ARCH__ >= CC_VOLTA
  3531. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3532. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3533. const int nwarps = NWARPS_Q8_0_AMPERE;
  3534. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3535. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3536. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3537. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3538. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3539. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3540. const int nwarps = NWARPS_Q8_0_PASCAL;
  3541. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3542. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3543. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3544. #else
  3545. (void) vec_dot_q8_0_q8_1_mul_mat;
  3546. bad_arch();
  3547. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3548. }
  3549. #define MMQ_X_Q2_K_RDNA2 64
  3550. #define MMQ_Y_Q2_K_RDNA2 128
  3551. #define NWARPS_Q2_K_RDNA2 8
  3552. #define MMQ_X_Q2_K_RDNA1 128
  3553. #define MMQ_Y_Q2_K_RDNA1 32
  3554. #define NWARPS_Q2_K_RDNA1 8
  3555. #if defined(CUDA_USE_TENSOR_CORES)
  3556. #define MMQ_X_Q2_K_AMPERE 4
  3557. #define MMQ_Y_Q2_K_AMPERE 32
  3558. #define NWARPS_Q2_K_AMPERE 4
  3559. #else
  3560. #define MMQ_X_Q2_K_AMPERE 64
  3561. #define MMQ_Y_Q2_K_AMPERE 128
  3562. #define NWARPS_Q2_K_AMPERE 4
  3563. #endif
  3564. #define MMQ_X_Q2_K_PASCAL 64
  3565. #define MMQ_Y_Q2_K_PASCAL 64
  3566. #define NWARPS_Q2_K_PASCAL 8
  3567. template <bool need_check> static __global__ void
  3568. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3569. #if defined(RDNA3) || defined(RDNA2)
  3570. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3571. #endif // defined(RDNA3) || defined(RDNA2)
  3572. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3573. mul_mat_q2_K(
  3574. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3575. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3576. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3577. #if defined(RDNA3) || defined(RDNA2)
  3578. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3579. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3580. const int nwarps = NWARPS_Q2_K_RDNA2;
  3581. #else
  3582. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3583. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3584. const int nwarps = NWARPS_Q2_K_RDNA1;
  3585. #endif // defined(RDNA3) || defined(RDNA2)
  3586. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3587. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3588. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3589. #elif __CUDA_ARCH__ >= CC_VOLTA
  3590. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3591. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3592. const int nwarps = NWARPS_Q2_K_AMPERE;
  3593. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3594. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3595. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3596. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3597. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3598. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3599. const int nwarps = NWARPS_Q2_K_PASCAL;
  3600. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3601. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3602. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3603. #else
  3604. (void) vec_dot_q2_K_q8_1_mul_mat;
  3605. bad_arch();
  3606. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3607. }
  3608. #define MMQ_X_Q3_K_RDNA2 128
  3609. #define MMQ_Y_Q3_K_RDNA2 64
  3610. #define NWARPS_Q3_K_RDNA2 8
  3611. #define MMQ_X_Q3_K_RDNA1 32
  3612. #define MMQ_Y_Q3_K_RDNA1 128
  3613. #define NWARPS_Q3_K_RDNA1 8
  3614. #if defined(CUDA_USE_TENSOR_CORES)
  3615. #define MMQ_X_Q3_K_AMPERE 4
  3616. #define MMQ_Y_Q3_K_AMPERE 32
  3617. #define NWARPS_Q3_K_AMPERE 4
  3618. #else
  3619. #define MMQ_X_Q3_K_AMPERE 128
  3620. #define MMQ_Y_Q3_K_AMPERE 128
  3621. #define NWARPS_Q3_K_AMPERE 4
  3622. #endif
  3623. #define MMQ_X_Q3_K_PASCAL 64
  3624. #define MMQ_Y_Q3_K_PASCAL 64
  3625. #define NWARPS_Q3_K_PASCAL 8
  3626. template <bool need_check> static __global__ void
  3627. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3628. #if defined(RDNA3) || defined(RDNA2)
  3629. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3630. #endif // defined(RDNA3) || defined(RDNA2)
  3631. #elif __CUDA_ARCH__ < CC_VOLTA
  3632. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3633. #endif // __CUDA_ARCH__ < CC_VOLTA
  3634. mul_mat_q3_K(
  3635. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3636. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3637. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3638. #if defined(RDNA3) || defined(RDNA2)
  3639. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3640. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3641. const int nwarps = NWARPS_Q3_K_RDNA2;
  3642. #else
  3643. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3644. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3645. const int nwarps = NWARPS_Q3_K_RDNA1;
  3646. #endif // defined(RDNA3) || defined(RDNA2)
  3647. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3648. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3649. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3650. #elif __CUDA_ARCH__ >= CC_VOLTA
  3651. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3652. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3653. const int nwarps = NWARPS_Q3_K_AMPERE;
  3654. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3655. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3656. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3657. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3658. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3659. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3660. const int nwarps = NWARPS_Q3_K_PASCAL;
  3661. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3662. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3663. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3664. #else
  3665. (void) vec_dot_q3_K_q8_1_mul_mat;
  3666. bad_arch();
  3667. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3668. }
  3669. #define MMQ_X_Q4_K_RDNA2 64
  3670. #define MMQ_Y_Q4_K_RDNA2 128
  3671. #define NWARPS_Q4_K_RDNA2 8
  3672. #define MMQ_X_Q4_K_RDNA1 32
  3673. #define MMQ_Y_Q4_K_RDNA1 64
  3674. #define NWARPS_Q4_K_RDNA1 8
  3675. #if defined(CUDA_USE_TENSOR_CORES)
  3676. #define MMQ_X_Q4_K_AMPERE 4
  3677. #define MMQ_Y_Q4_K_AMPERE 32
  3678. #define NWARPS_Q4_K_AMPERE 4
  3679. #else
  3680. #define MMQ_X_Q4_K_AMPERE 64
  3681. #define MMQ_Y_Q4_K_AMPERE 128
  3682. #define NWARPS_Q4_K_AMPERE 4
  3683. #endif
  3684. #define MMQ_X_Q4_K_PASCAL 64
  3685. #define MMQ_Y_Q4_K_PASCAL 64
  3686. #define NWARPS_Q4_K_PASCAL 8
  3687. template <bool need_check> static __global__ void
  3688. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3689. #if defined(RDNA3) || defined(RDNA2)
  3690. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3691. #endif // defined(RDNA3) || defined(RDNA2)
  3692. #elif __CUDA_ARCH__ < CC_VOLTA
  3693. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3694. #endif // __CUDA_ARCH__ < CC_VOLTA
  3695. mul_mat_q4_K(
  3696. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3697. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3698. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3699. #if defined(RDNA3) || defined(RDNA2)
  3700. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3701. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3702. const int nwarps = NWARPS_Q4_K_RDNA2;
  3703. #else
  3704. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3705. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3706. const int nwarps = NWARPS_Q4_K_RDNA1;
  3707. #endif // defined(RDNA3) || defined(RDNA2)
  3708. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3709. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3710. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3711. #elif __CUDA_ARCH__ >= CC_VOLTA
  3712. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3713. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3714. const int nwarps = NWARPS_Q4_K_AMPERE;
  3715. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3716. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3717. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3718. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3719. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3720. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3721. const int nwarps = NWARPS_Q4_K_PASCAL;
  3722. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3723. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3724. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3725. #else
  3726. (void) vec_dot_q4_K_q8_1_mul_mat;
  3727. bad_arch();
  3728. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3729. }
  3730. #define MMQ_X_Q5_K_RDNA2 64
  3731. #define MMQ_Y_Q5_K_RDNA2 128
  3732. #define NWARPS_Q5_K_RDNA2 8
  3733. #define MMQ_X_Q5_K_RDNA1 32
  3734. #define MMQ_Y_Q5_K_RDNA1 64
  3735. #define NWARPS_Q5_K_RDNA1 8
  3736. #if defined(CUDA_USE_TENSOR_CORES)
  3737. #define MMQ_X_Q5_K_AMPERE 4
  3738. #define MMQ_Y_Q5_K_AMPERE 32
  3739. #define NWARPS_Q5_K_AMPERE 4
  3740. #else
  3741. #define MMQ_X_Q5_K_AMPERE 64
  3742. #define MMQ_Y_Q5_K_AMPERE 128
  3743. #define NWARPS_Q5_K_AMPERE 4
  3744. #endif
  3745. #define MMQ_X_Q5_K_PASCAL 64
  3746. #define MMQ_Y_Q5_K_PASCAL 64
  3747. #define NWARPS_Q5_K_PASCAL 8
  3748. template <bool need_check> static __global__ void
  3749. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3750. #if defined(RDNA3) || defined(RDNA2)
  3751. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3752. #endif // defined(RDNA3) || defined(RDNA2)
  3753. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3754. mul_mat_q5_K(
  3755. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3756. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3757. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3758. #if defined(RDNA3) || defined(RDNA2)
  3759. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3760. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3761. const int nwarps = NWARPS_Q5_K_RDNA2;
  3762. #else
  3763. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3764. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3765. const int nwarps = NWARPS_Q5_K_RDNA1;
  3766. #endif // defined(RDNA3) || defined(RDNA2)
  3767. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3768. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3769. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3770. #elif __CUDA_ARCH__ >= CC_VOLTA
  3771. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3772. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3773. const int nwarps = NWARPS_Q5_K_AMPERE;
  3774. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3775. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3776. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3777. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3778. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3779. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3780. const int nwarps = NWARPS_Q5_K_PASCAL;
  3781. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3782. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3783. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3784. #else
  3785. (void) vec_dot_q5_K_q8_1_mul_mat;
  3786. bad_arch();
  3787. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3788. }
  3789. #define MMQ_X_Q6_K_RDNA2 64
  3790. #define MMQ_Y_Q6_K_RDNA2 128
  3791. #define NWARPS_Q6_K_RDNA2 8
  3792. #define MMQ_X_Q6_K_RDNA1 32
  3793. #define MMQ_Y_Q6_K_RDNA1 64
  3794. #define NWARPS_Q6_K_RDNA1 8
  3795. #if defined(CUDA_USE_TENSOR_CORES)
  3796. #define MMQ_X_Q6_K_AMPERE 4
  3797. #define MMQ_Y_Q6_K_AMPERE 32
  3798. #define NWARPS_Q6_K_AMPERE 4
  3799. #else
  3800. #define MMQ_X_Q6_K_AMPERE 64
  3801. #define MMQ_Y_Q6_K_AMPERE 64
  3802. #define NWARPS_Q6_K_AMPERE 4
  3803. #endif
  3804. #define MMQ_X_Q6_K_PASCAL 64
  3805. #define MMQ_Y_Q6_K_PASCAL 64
  3806. #define NWARPS_Q6_K_PASCAL 8
  3807. template <bool need_check> static __global__ void
  3808. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3809. #if defined(RDNA3) || defined(RDNA2)
  3810. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3811. #endif // defined(RDNA3) || defined(RDNA2)
  3812. #elif __CUDA_ARCH__ < CC_VOLTA
  3813. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3814. #endif // __CUDA_ARCH__ < CC_VOLTA
  3815. mul_mat_q6_K(
  3816. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3817. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3818. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3819. #if defined(RDNA3) || defined(RDNA2)
  3820. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3821. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3822. const int nwarps = NWARPS_Q6_K_RDNA2;
  3823. #else
  3824. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3825. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3826. const int nwarps = NWARPS_Q6_K_RDNA1;
  3827. #endif // defined(RDNA3) || defined(RDNA2)
  3828. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3829. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3830. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3831. #elif __CUDA_ARCH__ >= CC_VOLTA
  3832. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3833. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3834. const int nwarps = NWARPS_Q6_K_AMPERE;
  3835. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3836. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3837. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3838. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3839. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3840. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3841. const int nwarps = NWARPS_Q6_K_PASCAL;
  3842. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3843. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3844. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3845. #else
  3846. (void) vec_dot_q6_K_q8_1_mul_mat;
  3847. bad_arch();
  3848. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3849. }
  3850. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3851. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3852. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3853. if (row >= nrows) {
  3854. return;
  3855. }
  3856. const int blocks_per_row = ncols / qk;
  3857. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3858. // partial sum for each thread
  3859. float tmp = 0.0f;
  3860. const block_q_t * x = (const block_q_t *) vx;
  3861. const block_q8_1 * y = (const block_q8_1 *) vy;
  3862. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3863. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3864. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3865. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3866. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3867. }
  3868. // sum up partial sums and write back result
  3869. #pragma unroll
  3870. for (int mask = 16; mask > 0; mask >>= 1) {
  3871. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3872. }
  3873. if (threadIdx.x == 0) {
  3874. dst[row] = tmp;
  3875. }
  3876. }
  3877. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3878. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3879. // qk = quantized weights per x block
  3880. // qr = number of quantized weights per data value in x block
  3881. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3882. if (row >= nrows) {
  3883. return;
  3884. }
  3885. const int tid = threadIdx.x;
  3886. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3887. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3888. const int y_offset = qr == 1 ? 1 : qk/2;
  3889. // partial sum for each thread
  3890. #ifdef GGML_CUDA_F16
  3891. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3892. #else
  3893. float tmp = 0.0f;
  3894. #endif // GGML_CUDA_F16
  3895. for (int i = 0; i < ncols; i += iter_stride) {
  3896. const int col = i + vals_per_iter*tid;
  3897. const int ib = (row*ncols + col)/qk; // x block index
  3898. const int iqs = (col%qk)/qr; // x quant index
  3899. const int iybs = col - col%qk; // y block start index
  3900. // processing >2 values per i iter is faster for fast GPUs
  3901. #pragma unroll
  3902. for (int j = 0; j < vals_per_iter; j += 2) {
  3903. // process 2 vals per j iter
  3904. // dequantize
  3905. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3906. dfloat2 v;
  3907. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3908. // matrix multiplication
  3909. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3910. #ifdef GGML_CUDA_F16
  3911. tmp += __hmul2(v, {
  3912. y[iybs + iqs + j/qr + 0],
  3913. y[iybs + iqs + j/qr + y_offset]
  3914. });
  3915. #else
  3916. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3917. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3918. #endif // GGML_CUDA_F16
  3919. }
  3920. }
  3921. // sum up partial sums and write back result
  3922. #pragma unroll
  3923. for (int mask = 16; mask > 0; mask >>= 1) {
  3924. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3925. }
  3926. if (tid == 0) {
  3927. #ifdef GGML_CUDA_F16
  3928. dst[row] = tmp.x + tmp.y;
  3929. #else
  3930. dst[row] = tmp;
  3931. #endif // GGML_CUDA_F16
  3932. }
  3933. }
  3934. static __global__ void mul_mat_p021_f16_f32(
  3935. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3936. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3937. const half * x = (const half *) vx;
  3938. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3939. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3940. const int channel_x = channel / (nchannels_y / nchannels_x);
  3941. const int nrows_y = ncols_x;
  3942. const int nrows_dst = nrows_x;
  3943. const int row_dst = row_x;
  3944. float tmp = 0.0f;
  3945. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3946. const int col_x = col_x0 + threadIdx.x;
  3947. if (col_x >= ncols_x) {
  3948. break;
  3949. }
  3950. // x is transposed and permuted
  3951. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3952. const float xi = __half2float(x[ix]);
  3953. const int row_y = col_x;
  3954. // y is not transposed but permuted
  3955. const int iy = channel*nrows_y + row_y;
  3956. tmp += xi * y[iy];
  3957. }
  3958. // dst is not transposed and not permuted
  3959. const int idst = channel*nrows_dst + row_dst;
  3960. // sum up partial sums and write back result
  3961. #pragma unroll
  3962. for (int mask = 16; mask > 0; mask >>= 1) {
  3963. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3964. }
  3965. if (threadIdx.x == 0) {
  3966. dst[idst] = tmp;
  3967. }
  3968. }
  3969. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3970. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3971. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3972. const half * x = (const half *) vx;
  3973. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3974. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3975. const int channel_x = channel / channel_x_divisor;
  3976. const int nrows_y = ncols_x;
  3977. const int nrows_dst = nrows_x;
  3978. const int row_dst = row_x;
  3979. const int idst = channel*nrows_dst + row_dst;
  3980. float tmp = 0.0f;
  3981. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3982. const int col_x = col_x0 + threadIdx.x;
  3983. if (col_x >= ncols_x) {
  3984. break;
  3985. }
  3986. const int row_y = col_x;
  3987. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3988. const int iy = channel*nrows_y + row_y;
  3989. const float xi = __half2float(x[ix]);
  3990. tmp += xi * y[iy];
  3991. }
  3992. // sum up partial sums and write back result
  3993. #pragma unroll
  3994. for (int mask = 16; mask > 0; mask >>= 1) {
  3995. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3996. }
  3997. if (threadIdx.x == 0) {
  3998. dst[idst] = tmp;
  3999. }
  4000. }
  4001. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4002. const float * xi = (const float *) cxi;
  4003. float * dsti = (float *) cdsti;
  4004. *dsti = *xi;
  4005. }
  4006. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4007. const float * xi = (const float *) cxi;
  4008. half * dsti = (half *) cdsti;
  4009. *dsti = __float2half(*xi);
  4010. }
  4011. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4012. const half * xi = (const half *) cxi;
  4013. half * dsti = (half *) cdsti;
  4014. *dsti = *xi;
  4015. }
  4016. template <cpy_kernel_t cpy_1>
  4017. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4018. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4019. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  4020. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4021. if (i >= ne) {
  4022. return;
  4023. }
  4024. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4025. // then combine those indices with the corresponding byte offsets to get the total offsets
  4026. const int i02 = i / (ne00*ne01);
  4027. const int i01 = (i - i02*ne01*ne00) / ne00;
  4028. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  4029. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  4030. const int i12 = i / (ne10*ne11);
  4031. const int i11 = (i - i12*ne10*ne11) / ne10;
  4032. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  4033. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  4034. cpy_1(cx + x_offset, cdst + dst_offset);
  4035. }
  4036. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4037. const float * xi = (const float *) cxi;
  4038. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4039. float amax = 0.0f; // absolute max
  4040. for (int j = 0; j < QK8_0; j++) {
  4041. const float v = xi[j];
  4042. amax = fmaxf(amax, fabsf(v));
  4043. }
  4044. const float d = amax / ((1 << 7) - 1);
  4045. const float id = d ? 1.0f/d : 0.0f;
  4046. dsti->d = d;
  4047. for (int j = 0; j < QK8_0; ++j) {
  4048. const float x0 = xi[j]*id;
  4049. dsti->qs[j] = roundf(x0);
  4050. }
  4051. }
  4052. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4053. const float * xi = (const float *) cxi;
  4054. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4055. float amax = 0.0f;
  4056. float vmax = 0.0f;
  4057. for (int j = 0; j < QK4_0; ++j) {
  4058. const float v = xi[j];
  4059. if (amax < fabsf(v)) {
  4060. amax = fabsf(v);
  4061. vmax = v;
  4062. }
  4063. }
  4064. const float d = vmax / -8;
  4065. const float id = d ? 1.0f/d : 0.0f;
  4066. dsti->d = d;
  4067. for (int j = 0; j < QK4_0/2; ++j) {
  4068. const float x0 = xi[0 + j]*id;
  4069. const float x1 = xi[QK4_0/2 + j]*id;
  4070. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4071. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4072. dsti->qs[j] = xi0;
  4073. dsti->qs[j] |= xi1 << 4;
  4074. }
  4075. }
  4076. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4077. const float * xi = (const float *) cxi;
  4078. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4079. float vmin = FLT_MAX;
  4080. float vmax = -FLT_MAX;
  4081. for (int j = 0; j < QK4_1; ++j) {
  4082. const float v = xi[j];
  4083. if (v < vmin) vmin = v;
  4084. if (v > vmax) vmax = v;
  4085. }
  4086. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4087. const float id = d ? 1.0f/d : 0.0f;
  4088. dsti->dm.x = d;
  4089. dsti->dm.y = vmin;
  4090. for (int j = 0; j < QK4_1/2; ++j) {
  4091. const float x0 = (xi[0 + j] - vmin)*id;
  4092. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4093. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4094. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4095. dsti->qs[j] = xi0;
  4096. dsti->qs[j] |= xi1 << 4;
  4097. }
  4098. }
  4099. template <cpy_kernel_t cpy_blck, int qk>
  4100. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4101. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4102. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  4103. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4104. if (i >= ne) {
  4105. return;
  4106. }
  4107. const int i02 = i / (ne00*ne01);
  4108. const int i01 = (i - i02*ne01*ne00) / ne00;
  4109. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  4110. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  4111. const int i12 = i / (ne10*ne11);
  4112. const int i11 = (i - i12*ne10*ne11) / ne10;
  4113. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  4114. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  4115. cpy_blck(cx + x_offset, cdst + dst_offset);
  4116. }
  4117. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4118. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4119. return 1.0f - min(1.0f, max(0.0f, y));
  4120. }
  4121. struct rope_corr_dims {
  4122. float v[4];
  4123. };
  4124. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4125. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4126. static __device__ void rope_yarn(
  4127. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4128. float * cos_theta, float * sin_theta
  4129. ) {
  4130. // Get n-d rotational scaling corrected for extrapolation
  4131. float theta_interp = freq_scale * theta_extrap;
  4132. float theta = theta_interp;
  4133. if (ext_factor != 0.0f) {
  4134. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4135. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4136. // Get n-d magnitude scaling corrected for interpolation
  4137. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4138. }
  4139. *cos_theta = cosf(theta) * mscale;
  4140. *sin_theta = sinf(theta) * mscale;
  4141. }
  4142. // rope == RoPE == rotary positional embedding
  4143. template<typename T, bool has_pos>
  4144. static __global__ void rope(
  4145. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4146. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4147. ) {
  4148. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4149. if (col >= ncols) {
  4150. return;
  4151. }
  4152. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4153. const int i = row*ncols + col;
  4154. const int i2 = row/p_delta_rows;
  4155. const int p = has_pos ? pos[i2] : 0;
  4156. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4157. float cos_theta, sin_theta;
  4158. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4159. const float x0 = x[i + 0];
  4160. const float x1 = x[i + 1];
  4161. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4162. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4163. }
  4164. template<typename T, bool has_pos>
  4165. static __global__ void rope_neox(
  4166. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4167. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4168. ) {
  4169. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4170. if (col >= ncols) {
  4171. return;
  4172. }
  4173. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4174. const int ib = col / n_dims;
  4175. const int ic = col % n_dims;
  4176. if (ib > 0) {
  4177. const int i = row*ncols + ib*n_dims + ic;
  4178. dst[i + 0] = x[i + 0];
  4179. dst[i + 1] = x[i + 1];
  4180. return;
  4181. }
  4182. const int i = row*ncols + ib*n_dims + ic/2;
  4183. const int i2 = row/p_delta_rows;
  4184. float cur_rot = inv_ndims * ic - ib;
  4185. const int p = has_pos ? pos[i2] : 0;
  4186. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4187. float cos_theta, sin_theta;
  4188. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4189. const float x0 = x[i + 0];
  4190. const float x1 = x[i + n_dims/2];
  4191. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4192. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4193. }
  4194. static __global__ void rope_glm_f32(
  4195. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4196. int n_ctx
  4197. ) {
  4198. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4199. const int half_n_dims = ncols/4;
  4200. if (col >= half_n_dims) {
  4201. return;
  4202. }
  4203. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4204. const int i = row*ncols + col;
  4205. const int i2 = row/p_delta_rows;
  4206. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4207. // FIXME: this is likely wrong
  4208. const int p = pos != nullptr ? pos[i2] : 0;
  4209. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4210. const float sin_theta = sinf(theta);
  4211. const float cos_theta = cosf(theta);
  4212. const float x0 = x[i + 0];
  4213. const float x1 = x[i + half_n_dims];
  4214. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4215. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4216. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4217. const float sin_block_theta = sinf(block_theta);
  4218. const float cos_block_theta = cosf(block_theta);
  4219. const float x2 = x[i + half_n_dims * 2];
  4220. const float x3 = x[i + half_n_dims * 3];
  4221. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4222. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4223. }
  4224. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4225. const int n_heads_log2_floor, const float m0, const float m1) {
  4226. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4227. if (col >= ncols) {
  4228. return;
  4229. }
  4230. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4231. const int i = row*ncols + col;
  4232. const int k = row/k_rows;
  4233. float m_k;
  4234. if (k < n_heads_log2_floor) {
  4235. m_k = powf(m0, k + 1);
  4236. } else {
  4237. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4238. }
  4239. dst[i] = col * m_k + x[i];
  4240. }
  4241. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4242. const int row = blockIdx.y;
  4243. const int col = threadIdx.x;
  4244. float sum = 0.0f;
  4245. for (int i = col; i < ncols; i += blockDim.x) {
  4246. sum += x[row * ncols + i];
  4247. }
  4248. sum = warp_reduce_sum(sum);
  4249. if (col == 0) {
  4250. dst[row] = sum;
  4251. }
  4252. }
  4253. template<typename T>
  4254. static inline __device__ void swap(T & a, T & b) {
  4255. T tmp = a;
  4256. a = b;
  4257. b = tmp;
  4258. }
  4259. template<ggml_sort_order order>
  4260. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4261. // bitonic sort
  4262. int col = threadIdx.x;
  4263. int row = blockIdx.y;
  4264. if (col >= ncols) return;
  4265. const float * x_row = x + row * ncols;
  4266. int * dst_row = dst + row * ncols;
  4267. // initialize indices
  4268. if (col < ncols) {
  4269. dst_row[col] = col;
  4270. }
  4271. __syncthreads();
  4272. for (int k = 2; k <= ncols; k *= 2) {
  4273. for (int j = k / 2; j > 0; j /= 2) {
  4274. int ixj = col ^ j;
  4275. if (ixj > col) {
  4276. if ((col & k) == 0) {
  4277. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  4278. swap(dst_row[col], dst_row[ixj]);
  4279. }
  4280. } else {
  4281. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  4282. swap(dst_row[col], dst_row[ixj]);
  4283. }
  4284. }
  4285. }
  4286. __syncthreads();
  4287. }
  4288. }
  4289. }
  4290. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4291. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4292. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4293. if (col >= ncols) {
  4294. return;
  4295. }
  4296. const int i = row*ncols + col;
  4297. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4298. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4299. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4300. }
  4301. template <bool vals_smem, int ncols_template, int block_size_template, bool need_check>
  4302. static __global__ void soft_max_f16(const float * x, const float * y, float * dst, const int ncols_par, const int nrows_y, const float scale) {
  4303. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  4304. const int ncols_data = ncols_template == 0 ? ncols_par : ncols_template;
  4305. const int ncols_smem = GGML_PAD(ncols_data, 2*WARP_SIZE)/2;
  4306. const int tid = threadIdx.x;
  4307. const int rowx = blockIdx.x;
  4308. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4309. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  4310. const int warp_id = threadIdx.x / WARP_SIZE;
  4311. const int lane_id = threadIdx.x % WARP_SIZE;
  4312. extern __shared__ half data_soft_max_f16[];
  4313. half * buf_iw = data_soft_max_f16 + 0; // shared memory buffer for inter-warp communication
  4314. // (shared memory) buffer to cache values between iterations:
  4315. half2 * vals = vals_smem ? (half2 *) (buf_iw + WARP_SIZE) : (half2 *) (dst + rowx*ncols_data);
  4316. // if the buffer is larger than max. shared memory per block, use dst as temp. buffer instead
  4317. // in that case col_smem == col_data must be enforced to avoid race conditions
  4318. half2 max_val = make_half2(-INFINITY, -INFINITY);
  4319. #pragma unroll
  4320. for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {
  4321. const int col_data = 2*col0 + 2*WARP_SIZE*warp_id + lane_id;
  4322. const int col_smem = vals_smem ? col0 + tid : col_data;
  4323. const int ix = rowx*ncols_data + col_data;
  4324. const int iy = rowy*ncols_data + col_data;
  4325. half2 val;
  4326. if (need_check && col_data + 0 >= ncols_data) {
  4327. val.x = -INFINITY;
  4328. } else {
  4329. val.x = x[ix + 0]*scale + (y ? y[iy + 0] : 0.0f);
  4330. }
  4331. if (need_check && col_data + WARP_SIZE >= ncols_data) {
  4332. val.y = -INFINITY;
  4333. } else {
  4334. val.y = x[ix + WARP_SIZE]*scale + (y ? y[iy + WARP_SIZE] : 0.0f);
  4335. }
  4336. if (!need_check || col_smem < (vals_smem ? ncols_smem : ncols_data)) {
  4337. vals[col_smem] = val;
  4338. }
  4339. max_val = __hmax2(max_val, val);
  4340. }
  4341. // find the max value in the block
  4342. max_val = warp_reduce_max(max_val);
  4343. if (block_size > WARP_SIZE) {
  4344. if (warp_id == 0) {
  4345. buf_iw[lane_id] = -INFINITY;
  4346. }
  4347. __syncthreads();
  4348. if (lane_id == 0) {
  4349. buf_iw[warp_id] = __hmax(max_val.x, max_val.y);
  4350. }
  4351. __syncthreads();
  4352. max_val = __half2half2(buf_iw[lane_id]);
  4353. max_val = warp_reduce_max(max_val);
  4354. } else {
  4355. max_val = __half2half2(__hmax(max_val.x, max_val.y));
  4356. }
  4357. half2 tmp = make_half2(0.0f, 0.0f); // partial sums
  4358. #pragma unroll
  4359. for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {
  4360. const int col_smem = vals_smem ? col0 + tid : 2*col0 + 2*warp_id*WARP_SIZE + lane_id;
  4361. if (ncols_template == 0 && col_smem >= (vals_smem ? ncols_smem : ncols_data)) {
  4362. break;
  4363. }
  4364. const half2 val = h2exp(vals[col_smem] - max_val);
  4365. tmp += val;
  4366. vals[col_smem] = val;
  4367. }
  4368. // find the sum of exps in the block
  4369. tmp = warp_reduce_sum(tmp);
  4370. if (block_size > WARP_SIZE) {
  4371. if (warp_id == 0) {
  4372. buf_iw[lane_id] = 0.0f;
  4373. }
  4374. __syncthreads();
  4375. if (lane_id == 0) {
  4376. buf_iw[warp_id] = tmp.x + tmp.y;
  4377. }
  4378. __syncthreads();
  4379. tmp = __half2half2(buf_iw[lane_id]);
  4380. tmp = warp_reduce_sum(tmp);
  4381. } else {
  4382. tmp = __half2half2(tmp.x + tmp.y);
  4383. }
  4384. const half2 inv_sum = make_half2(1.0f, 1.0f) / tmp;
  4385. #pragma unroll
  4386. for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {
  4387. const int col_data = 2*col0 + 2*WARP_SIZE*warp_id + lane_id;
  4388. const int col_smem = vals_smem ? col0 + tid : col_data;
  4389. const int idst = rowx*ncols_data + col_data;
  4390. const half2 result = vals[col_smem] * inv_sum;
  4391. if (need_check && col_data + 0 >= ncols_data) {
  4392. return;
  4393. }
  4394. dst[idst] = result.x;
  4395. if (need_check && col_data + WARP_SIZE >= ncols_data) {
  4396. return;
  4397. }
  4398. dst[idst + WARP_SIZE] = result.y;
  4399. }
  4400. #else
  4401. (void) x; (void) y; (void) dst; (void) ncols_par; (void) nrows_y; (void) scale;
  4402. bad_arch();
  4403. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  4404. }
  4405. template <bool vals_smem, int ncols_template, int block_size_template>
  4406. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols_par, const int nrows_y, const float scale) {
  4407. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  4408. const int tid = threadIdx.x;
  4409. const int rowx = blockIdx.x;
  4410. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4411. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  4412. const int warp_id = threadIdx.x / WARP_SIZE;
  4413. const int lane_id = threadIdx.x % WARP_SIZE;
  4414. extern __shared__ float data_soft_max_f32[];
  4415. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  4416. // shared memory buffer to cache values between iterations:
  4417. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  4418. float max_val = -INFINITY;
  4419. #pragma unroll
  4420. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4421. const int col = col0 + tid;
  4422. if (ncols_template == 0 && col >= ncols) {
  4423. break;
  4424. }
  4425. const int ix = rowx*ncols + col;
  4426. const int iy = rowy*ncols + col;
  4427. const float val = x[ix]*scale + (y ? y[iy] : 0.0f);
  4428. vals[col] = val;
  4429. max_val = max(max_val, val);
  4430. }
  4431. // find the max value in the block
  4432. max_val = warp_reduce_max(max_val);
  4433. if (block_size > WARP_SIZE) {
  4434. if (warp_id == 0) {
  4435. buf_iw[lane_id] = -INFINITY;
  4436. }
  4437. __syncthreads();
  4438. if (lane_id == 0) {
  4439. buf_iw[warp_id] = max_val;
  4440. }
  4441. __syncthreads();
  4442. max_val = buf_iw[lane_id];
  4443. max_val = warp_reduce_max(max_val);
  4444. }
  4445. float tmp = 0.0f; // partial sum
  4446. #pragma unroll
  4447. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4448. const int col = col0 + tid;
  4449. if (ncols_template == 0 && col >= ncols) {
  4450. break;
  4451. }
  4452. const float val = expf(vals[col] - max_val);
  4453. tmp += val;
  4454. vals[col] = val;
  4455. }
  4456. // find the sum of exps in the block
  4457. tmp = warp_reduce_sum(tmp);
  4458. if (block_size > WARP_SIZE) {
  4459. if (warp_id == 0) {
  4460. buf_iw[lane_id] = 0.0f;
  4461. }
  4462. __syncthreads();
  4463. if (lane_id == 0) {
  4464. buf_iw[warp_id] = tmp;
  4465. }
  4466. __syncthreads();
  4467. tmp = buf_iw[lane_id];
  4468. tmp = warp_reduce_sum(tmp);
  4469. }
  4470. const float inv_sum = 1.0f / tmp;
  4471. #pragma unroll
  4472. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4473. const int col = col0 + tid;
  4474. if (ncols_template == 0 && col >= ncols) {
  4475. return;
  4476. }
  4477. const int idst = rowx*ncols + col;
  4478. dst[idst] = vals[col] * inv_sum;
  4479. }
  4480. }
  4481. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4482. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4483. if (i >= k) {
  4484. return;
  4485. }
  4486. dst[i] = scale * x[i];
  4487. }
  4488. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4489. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4490. if (i >= k) {
  4491. return;
  4492. }
  4493. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4494. }
  4495. static __global__ void im2col_f32_f16(
  4496. const float * x, half * dst,
  4497. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4498. int s0, int s1, int p0, int p1, int d0, int d1) {
  4499. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4500. if (i >= pelements) {
  4501. return;
  4502. }
  4503. const int ksize = OW * (KH > 1 ? KW : 1);
  4504. const int kx = i / ksize;
  4505. const int kd = kx * ksize;
  4506. const int ky = (i - kd) / OW;
  4507. const int ix = i % OW;
  4508. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4509. const int64_t iih = blockIdx.y * s1 + ky * d1 - p1;
  4510. const int64_t offset_dst =
  4511. (blockIdx.y * OW + ix) * CHW +
  4512. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4513. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4514. dst[offset_dst] = __float2half(0.0f);
  4515. } else {
  4516. const int64_t offset_src = blockIdx.z * offset_delta;
  4517. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4518. }
  4519. }
  4520. template<int qk, int qr, dequantize_kernel_t dq>
  4521. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4522. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4523. GGML_TENSOR_BINARY_OP_LOCALS
  4524. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4525. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4526. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4527. // strides in elements
  4528. //const size_t s0 = nb0 / ggml_element_size(dst);
  4529. const size_t s1 = nb1 / ggml_element_size(dst);
  4530. const size_t s2 = nb2 / ggml_element_size(dst);
  4531. const size_t s3 = nb3 / ggml_element_size(dst);
  4532. const size_t s10 = nb10 / ggml_element_size(src1);
  4533. const size_t s11 = nb11 / ggml_element_size(src1);
  4534. const size_t s12 = nb12 / ggml_element_size(src1);
  4535. //const size_t s13 = nb13 / ggml_element_size(src1);
  4536. GGML_ASSERT(ne00 % 2 == 0);
  4537. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4538. src0_dd, src1_dd, dst_dd,
  4539. ne00, /*ne01, ne02, ne03,*/
  4540. /*ne10, ne11,*/ ne12, /*ne13,*/
  4541. /* s0,*/ s1, s2, s3,
  4542. /* nb00,*/ nb01, nb02, nb03,
  4543. s10, s11, s12/*, s13*/);
  4544. (void) dst;
  4545. }
  4546. template<typename src0_t>
  4547. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4548. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4549. GGML_TENSOR_BINARY_OP_LOCALS
  4550. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4551. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4552. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4553. // strides in elements
  4554. //const size_t s0 = nb0 / ggml_element_size(dst);
  4555. const size_t s1 = nb1 / ggml_element_size(dst);
  4556. const size_t s2 = nb2 / ggml_element_size(dst);
  4557. const size_t s3 = nb3 / ggml_element_size(dst);
  4558. const size_t s10 = nb10 / ggml_element_size(src1);
  4559. const size_t s11 = nb11 / ggml_element_size(src1);
  4560. const size_t s12 = nb12 / ggml_element_size(src1);
  4561. //const size_t s13 = nb13 / ggml_element_size(src1);
  4562. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4563. src0_dd, src1_dd, dst_dd,
  4564. ne00, /*ne01, ne02, ne03,*/
  4565. /*ne10, ne11,*/ ne12, /*ne13,*/
  4566. /* s0,*/ s1, s2, s3,
  4567. /* nb00,*/ nb01, nb02, nb03,
  4568. s10, s11, s12/*, s13*/);
  4569. (void) dst;
  4570. }
  4571. template<float (*bin_op)(const float, const float)>
  4572. struct bin_bcast_cuda {
  4573. template<typename src0_t, typename src1_t, typename dst_t>
  4574. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4575. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4576. cudaStream_t stream) {
  4577. GGML_TENSOR_BINARY_OP_LOCALS
  4578. int nr0 = ne10/ne0;
  4579. int nr1 = ne11/ne1;
  4580. int nr2 = ne12/ne2;
  4581. int nr3 = ne13/ne3;
  4582. int nr[4] = { nr0, nr1, nr2, nr3 };
  4583. // collapse dimensions until first broadcast dimension
  4584. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4585. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4586. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4587. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4588. auto collapse = [](int64_t cne[]) {
  4589. cne[0] *= cne[1];
  4590. cne[1] = cne[2];
  4591. cne[2] = cne[3];
  4592. cne[3] = 1;
  4593. };
  4594. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  4595. cnb[1] *= cne[1];
  4596. cnb[2] *= cne[2];
  4597. cnb[3] *= cne[3];
  4598. };
  4599. for (int i = 0; i < 4; i++) {
  4600. if (nr[i] != 1) {
  4601. break;
  4602. }
  4603. if (i > 0) {
  4604. collapse_nb(cnb0, cne0);
  4605. collapse_nb(cnb1, cne1);
  4606. collapse(cne0);
  4607. collapse(cne1);
  4608. }
  4609. }
  4610. {
  4611. int64_t ne0 = cne0[0];
  4612. int64_t ne1 = cne0[1];
  4613. int64_t ne2 = cne0[2];
  4614. int64_t ne3 = cne0[3];
  4615. int64_t ne10 = cne1[0];
  4616. int64_t ne11 = cne1[1];
  4617. int64_t ne12 = cne1[2];
  4618. int64_t ne13 = cne1[3];
  4619. size_t nb0 = cnb0[0];
  4620. size_t nb1 = cnb0[1];
  4621. size_t nb2 = cnb0[2];
  4622. size_t nb3 = cnb0[3];
  4623. size_t nb10 = cnb1[0];
  4624. size_t nb11 = cnb1[1];
  4625. size_t nb12 = cnb1[2];
  4626. size_t nb13 = cnb1[3];
  4627. size_t s0 = nb0 / sizeof(dst_t);
  4628. size_t s1 = nb1 / sizeof(dst_t);
  4629. size_t s2 = nb2 / sizeof(dst_t);
  4630. size_t s3 = nb3 / sizeof(dst_t);
  4631. size_t s10 = nb10 / sizeof(src1_t);
  4632. size_t s11 = nb11 / sizeof(src1_t);
  4633. size_t s12 = nb12 / sizeof(src1_t);
  4634. size_t s13 = nb13 / sizeof(src1_t);
  4635. GGML_ASSERT(s0 == 1);
  4636. GGML_ASSERT(s10 == 1);
  4637. const int block_size = 128;
  4638. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4639. dim3 block_dims;
  4640. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4641. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4642. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4643. dim3 block_nums(
  4644. (hne0 + block_dims.x - 1) / block_dims.x,
  4645. (ne1 + block_dims.y - 1) / block_dims.y,
  4646. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4647. );
  4648. if (block_nums.z > 65535) {
  4649. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4650. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4651. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4652. src0_dd, src1_dd, dst_dd,
  4653. ne0, ne1, ne2, ne3,
  4654. ne10, ne11, ne12, ne13,
  4655. /* s0, */ s1, s2, s3,
  4656. /* s10, */ s11, s12, s13);
  4657. } else {
  4658. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4659. src0_dd, src1_dd, dst_dd,
  4660. ne0, ne1, ne2, ne3,
  4661. ne10, ne11, ne12, ne13,
  4662. /* s0, */ s1, s2, s3,
  4663. /* s10, */ s11, s12, s13);
  4664. }
  4665. }
  4666. }
  4667. };
  4668. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4669. const int ne10, const int ne11, const int ne12,
  4670. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4671. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4672. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4673. }
  4674. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4675. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4676. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4677. }
  4678. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4679. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4680. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4681. }
  4682. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4683. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4684. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4685. }
  4686. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4687. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4688. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4689. }
  4690. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4691. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4692. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4693. }
  4694. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4695. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4696. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4697. }
  4698. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4699. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4700. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4701. }
  4702. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4703. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4704. if (ncols < 1024) {
  4705. const dim3 block_dims(WARP_SIZE, 1, 1);
  4706. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4707. } else {
  4708. const dim3 block_dims(1024, 1, 1);
  4709. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4710. }
  4711. }
  4712. 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) {
  4713. static const float eps = 1e-6f;
  4714. if (group_size < 1024) {
  4715. const dim3 block_dims(WARP_SIZE, 1, 1);
  4716. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4717. } else {
  4718. const dim3 block_dims(1024, 1, 1);
  4719. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4720. }
  4721. }
  4722. 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) {
  4723. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4724. dim3 gridDim(num_blocks, ne1, ne2);
  4725. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4726. }
  4727. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int scale_factor, cudaStream_t stream) {
  4728. int ne0 = (ne00 * scale_factor);
  4729. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4730. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4731. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4732. }
  4733. static void pad_f32_cuda(const float * x, float * dst,
  4734. const int ne00, const int ne01, const int ne02,
  4735. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4736. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4737. dim3 gridDim(num_blocks, ne1, ne2);
  4738. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4739. }
  4740. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4741. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4742. if (ncols < 1024) {
  4743. const dim3 block_dims(WARP_SIZE, 1, 1);
  4744. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4745. } else {
  4746. const dim3 block_dims(1024, 1, 1);
  4747. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4748. }
  4749. }
  4750. 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) {
  4751. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4752. const dim3 num_blocks(block_num_x, ky, 1);
  4753. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4754. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4755. }
  4756. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4757. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4758. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  4759. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4760. }
  4761. template<typename dst_t>
  4762. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4763. const int nb = k / QK_K;
  4764. #if QK_K == 256
  4765. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4766. #else
  4767. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4768. #endif
  4769. }
  4770. template<typename dst_t>
  4771. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4772. const int nb = k / QK_K;
  4773. #if QK_K == 256
  4774. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4775. #else
  4776. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4777. #endif
  4778. }
  4779. template<typename dst_t>
  4780. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4781. const int nb = k / QK_K;
  4782. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4783. }
  4784. template<typename dst_t>
  4785. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4786. const int nb = k / QK_K;
  4787. #if QK_K == 256
  4788. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4789. #else
  4790. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4791. #endif
  4792. }
  4793. template<typename dst_t>
  4794. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4795. const int nb = k / QK_K;
  4796. #if QK_K == 256
  4797. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4798. #else
  4799. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4800. #endif
  4801. }
  4802. template<typename dst_t>
  4803. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4804. const int nb = k / QK_K;
  4805. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  4806. }
  4807. template <typename src_t, typename dst_t>
  4808. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4809. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4810. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4811. }
  4812. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4813. switch (type) {
  4814. case GGML_TYPE_Q4_0:
  4815. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4816. case GGML_TYPE_Q4_1:
  4817. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4818. case GGML_TYPE_Q5_0:
  4819. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4820. case GGML_TYPE_Q5_1:
  4821. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4822. case GGML_TYPE_Q8_0:
  4823. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4824. case GGML_TYPE_Q2_K:
  4825. return dequantize_row_q2_K_cuda;
  4826. case GGML_TYPE_Q3_K:
  4827. return dequantize_row_q3_K_cuda;
  4828. case GGML_TYPE_Q4_K:
  4829. return dequantize_row_q4_K_cuda;
  4830. case GGML_TYPE_Q5_K:
  4831. return dequantize_row_q5_K_cuda;
  4832. case GGML_TYPE_Q6_K:
  4833. return dequantize_row_q6_K_cuda;
  4834. case GGML_TYPE_IQ2_XXS:
  4835. return dequantize_row_iq2_xxs_cuda;
  4836. case GGML_TYPE_F32:
  4837. return convert_unary_cuda<float>;
  4838. default:
  4839. return nullptr;
  4840. }
  4841. }
  4842. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4843. switch (type) {
  4844. case GGML_TYPE_Q4_0:
  4845. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4846. case GGML_TYPE_Q4_1:
  4847. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4848. case GGML_TYPE_Q5_0:
  4849. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4850. case GGML_TYPE_Q5_1:
  4851. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4852. case GGML_TYPE_Q8_0:
  4853. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4854. case GGML_TYPE_Q2_K:
  4855. return dequantize_row_q2_K_cuda;
  4856. case GGML_TYPE_Q3_K:
  4857. return dequantize_row_q3_K_cuda;
  4858. case GGML_TYPE_Q4_K:
  4859. return dequantize_row_q4_K_cuda;
  4860. case GGML_TYPE_Q5_K:
  4861. return dequantize_row_q5_K_cuda;
  4862. case GGML_TYPE_Q6_K:
  4863. return dequantize_row_q6_K_cuda;
  4864. case GGML_TYPE_IQ2_XXS:
  4865. return dequantize_row_iq2_xxs_cuda;
  4866. case GGML_TYPE_F16:
  4867. return convert_unary_cuda<half>;
  4868. default:
  4869. return nullptr;
  4870. }
  4871. }
  4872. 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) {
  4873. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4874. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4875. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4876. const dim3 block_nums(block_num_y, 1, 1);
  4877. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4878. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4879. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4880. }
  4881. 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) {
  4882. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4883. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4884. const dim3 block_nums(block_num_y, 1, 1);
  4885. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4886. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4887. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4888. }
  4889. 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) {
  4890. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4891. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4892. const dim3 block_nums(block_num_y, 1, 1);
  4893. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4894. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4895. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4896. }
  4897. 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) {
  4898. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4899. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4900. const dim3 block_nums(block_num_y, 1, 1);
  4901. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4902. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4903. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4904. }
  4905. 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) {
  4906. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4907. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4908. const dim3 block_nums(block_num_y, 1, 1);
  4909. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4910. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4911. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4912. }
  4913. 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) {
  4914. GGML_ASSERT(ncols % QK_K == 0);
  4915. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4916. const int block_num_y = (nrows + ny - 1) / ny;
  4917. const dim3 block_nums(block_num_y, 1, 1);
  4918. const dim3 block_dims(32, ny, 1);
  4919. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4920. }
  4921. 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) {
  4922. GGML_ASSERT(ncols % QK_K == 0);
  4923. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4924. const int block_num_y = (nrows + ny - 1) / ny;
  4925. const dim3 block_nums(block_num_y, 1, 1);
  4926. const dim3 block_dims(32, ny, 1);
  4927. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4928. }
  4929. 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) {
  4930. GGML_ASSERT(ncols % QK_K == 0);
  4931. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4932. const int block_num_y = (nrows + ny - 1) / ny;
  4933. const dim3 block_nums(block_num_y, 1, 1);
  4934. const dim3 block_dims(32, ny, 1);
  4935. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4936. }
  4937. 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) {
  4938. GGML_ASSERT(ncols % QK_K == 0);
  4939. const dim3 block_dims(32, 1, 1);
  4940. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4941. }
  4942. 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) {
  4943. GGML_ASSERT(ncols % QK_K == 0);
  4944. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4945. const int block_num_y = (nrows + ny - 1) / ny;
  4946. const dim3 block_nums(block_num_y, 1, 1);
  4947. const dim3 block_dims(32, ny, 1);
  4948. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4949. }
  4950. 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) {
  4951. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4952. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4953. const dim3 block_nums(block_num_y, 1, 1);
  4954. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4955. dequantize_mul_mat_vec<1, 1, convert_f16>
  4956. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4957. }
  4958. static void mul_mat_vec_q4_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4959. GGML_ASSERT(ncols % QK4_0 == 0);
  4960. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4961. const dim3 block_nums(block_num_y, 1, 1);
  4962. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4963. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4964. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4965. }
  4966. static void mul_mat_vec_q4_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4967. GGML_ASSERT(ncols % QK4_1 == 0);
  4968. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4969. const dim3 block_nums(block_num_y, 1, 1);
  4970. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4971. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4972. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4973. }
  4974. static void mul_mat_vec_q5_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4975. GGML_ASSERT(ncols % QK5_0 == 0);
  4976. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4977. const dim3 block_nums(block_num_y, 1, 1);
  4978. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4979. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4980. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4981. }
  4982. static void mul_mat_vec_q5_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4983. GGML_ASSERT(ncols % QK5_1 == 0);
  4984. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4985. const dim3 block_nums(block_num_y, 1, 1);
  4986. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4987. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4988. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4989. }
  4990. static void mul_mat_vec_q8_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4991. GGML_ASSERT(ncols % QK8_0 == 0);
  4992. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4993. const dim3 block_nums(block_num_y, 1, 1);
  4994. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4995. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4996. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4997. }
  4998. static void mul_mat_vec_q2_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4999. GGML_ASSERT(ncols % QK_K == 0);
  5000. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5001. const dim3 block_nums(block_num_y, 1, 1);
  5002. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5003. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  5004. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5005. }
  5006. static void mul_mat_vec_q3_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5007. GGML_ASSERT(ncols % QK_K == 0);
  5008. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5009. const dim3 block_nums(block_num_y, 1, 1);
  5010. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5011. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  5012. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5013. }
  5014. static void mul_mat_vec_q4_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5015. GGML_ASSERT(ncols % QK_K == 0);
  5016. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5017. const dim3 block_nums(block_num_y, 1, 1);
  5018. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5019. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  5020. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5021. }
  5022. static void mul_mat_vec_q5_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5023. GGML_ASSERT(ncols % QK_K == 0);
  5024. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5025. const dim3 block_nums(block_num_y, 1, 1);
  5026. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5027. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  5028. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5029. }
  5030. static void mul_mat_vec_q6_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5031. GGML_ASSERT(ncols % QK_K == 0);
  5032. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5033. const dim3 block_nums(block_num_y, 1, 1);
  5034. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5035. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  5036. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5037. }
  5038. static void mul_mat_vec_iq2_xxs_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5039. GGML_ASSERT(ncols % QK_K == 0);
  5040. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5041. const dim3 block_nums(block_num_y, 1, 1);
  5042. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5043. mul_mat_vec_q<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  5044. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5045. }
  5046. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5047. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5048. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5049. int id;
  5050. CUDA_CHECK(cudaGetDevice(&id));
  5051. const int compute_capability = g_device_caps[id].cc;
  5052. int mmq_x, mmq_y, nwarps;
  5053. if (compute_capability >= CC_RDNA2) {
  5054. mmq_x = MMQ_X_Q4_0_RDNA2;
  5055. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5056. nwarps = NWARPS_Q4_0_RDNA2;
  5057. } else if (compute_capability >= CC_OFFSET_AMD) {
  5058. mmq_x = MMQ_X_Q4_0_RDNA1;
  5059. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5060. nwarps = NWARPS_Q4_0_RDNA1;
  5061. } else if (compute_capability >= CC_VOLTA) {
  5062. mmq_x = MMQ_X_Q4_0_AMPERE;
  5063. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5064. nwarps = NWARPS_Q4_0_AMPERE;
  5065. } else if (compute_capability >= MIN_CC_DP4A) {
  5066. mmq_x = MMQ_X_Q4_0_PASCAL;
  5067. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5068. nwarps = NWARPS_Q4_0_PASCAL;
  5069. } else {
  5070. GGML_ASSERT(false);
  5071. }
  5072. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5073. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5074. const dim3 block_nums(block_num_x, block_num_y, 1);
  5075. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5076. if (nrows_x % mmq_y == 0) {
  5077. const bool need_check = false;
  5078. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5079. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5080. } else {
  5081. const bool need_check = true;
  5082. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5083. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5084. }
  5085. }
  5086. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5087. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5088. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5089. int id;
  5090. CUDA_CHECK(cudaGetDevice(&id));
  5091. const int compute_capability = g_device_caps[id].cc;
  5092. int mmq_x, mmq_y, nwarps;
  5093. if (compute_capability >= CC_RDNA2) {
  5094. mmq_x = MMQ_X_Q4_1_RDNA2;
  5095. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5096. nwarps = NWARPS_Q4_1_RDNA2;
  5097. } else if (compute_capability >= CC_OFFSET_AMD) {
  5098. mmq_x = MMQ_X_Q4_1_RDNA1;
  5099. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5100. nwarps = NWARPS_Q4_1_RDNA1;
  5101. } else if (compute_capability >= CC_VOLTA) {
  5102. mmq_x = MMQ_X_Q4_1_AMPERE;
  5103. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5104. nwarps = NWARPS_Q4_1_AMPERE;
  5105. } else if (compute_capability >= MIN_CC_DP4A) {
  5106. mmq_x = MMQ_X_Q4_1_PASCAL;
  5107. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5108. nwarps = NWARPS_Q4_1_PASCAL;
  5109. } else {
  5110. GGML_ASSERT(false);
  5111. }
  5112. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5113. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5114. const dim3 block_nums(block_num_x, block_num_y, 1);
  5115. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5116. if (nrows_x % mmq_y == 0) {
  5117. const bool need_check = false;
  5118. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5119. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5120. } else {
  5121. const bool need_check = true;
  5122. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5123. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5124. }
  5125. }
  5126. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5127. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5128. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5129. int id;
  5130. CUDA_CHECK(cudaGetDevice(&id));
  5131. const int compute_capability = g_device_caps[id].cc;
  5132. int mmq_x, mmq_y, nwarps;
  5133. if (compute_capability >= CC_RDNA2) {
  5134. mmq_x = MMQ_X_Q5_0_RDNA2;
  5135. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5136. nwarps = NWARPS_Q5_0_RDNA2;
  5137. } else if (compute_capability >= CC_OFFSET_AMD) {
  5138. mmq_x = MMQ_X_Q5_0_RDNA1;
  5139. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5140. nwarps = NWARPS_Q5_0_RDNA1;
  5141. } else if (compute_capability >= CC_VOLTA) {
  5142. mmq_x = MMQ_X_Q5_0_AMPERE;
  5143. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5144. nwarps = NWARPS_Q5_0_AMPERE;
  5145. } else if (compute_capability >= MIN_CC_DP4A) {
  5146. mmq_x = MMQ_X_Q5_0_PASCAL;
  5147. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5148. nwarps = NWARPS_Q5_0_PASCAL;
  5149. } else {
  5150. GGML_ASSERT(false);
  5151. }
  5152. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5153. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5154. const dim3 block_nums(block_num_x, block_num_y, 1);
  5155. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5156. if (nrows_x % mmq_y == 0) {
  5157. const bool need_check = false;
  5158. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5159. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5160. } else {
  5161. const bool need_check = true;
  5162. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5163. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5164. }
  5165. }
  5166. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5167. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5168. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5169. int id;
  5170. CUDA_CHECK(cudaGetDevice(&id));
  5171. const int compute_capability = g_device_caps[id].cc;
  5172. int mmq_x, mmq_y, nwarps;
  5173. if (compute_capability >= CC_RDNA2) {
  5174. mmq_x = MMQ_X_Q5_1_RDNA2;
  5175. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5176. nwarps = NWARPS_Q5_1_RDNA2;
  5177. } else if (compute_capability >= CC_OFFSET_AMD) {
  5178. mmq_x = MMQ_X_Q5_1_RDNA1;
  5179. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5180. nwarps = NWARPS_Q5_1_RDNA1;
  5181. } else if (compute_capability >= CC_VOLTA) {
  5182. mmq_x = MMQ_X_Q5_1_AMPERE;
  5183. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5184. nwarps = NWARPS_Q5_1_AMPERE;
  5185. } else if (compute_capability >= MIN_CC_DP4A) {
  5186. mmq_x = MMQ_X_Q5_1_PASCAL;
  5187. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5188. nwarps = NWARPS_Q5_1_PASCAL;
  5189. } else {
  5190. GGML_ASSERT(false);
  5191. }
  5192. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5193. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5194. const dim3 block_nums(block_num_x, block_num_y, 1);
  5195. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5196. if (nrows_x % mmq_y == 0) {
  5197. const bool need_check = false;
  5198. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5199. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5200. } else {
  5201. const bool need_check = true;
  5202. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5203. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5204. }
  5205. }
  5206. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5207. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5208. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5209. int id;
  5210. CUDA_CHECK(cudaGetDevice(&id));
  5211. const int compute_capability = g_device_caps[id].cc;
  5212. int mmq_x, mmq_y, nwarps;
  5213. if (compute_capability >= CC_RDNA2) {
  5214. mmq_x = MMQ_X_Q8_0_RDNA2;
  5215. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5216. nwarps = NWARPS_Q8_0_RDNA2;
  5217. } else if (compute_capability >= CC_OFFSET_AMD) {
  5218. mmq_x = MMQ_X_Q8_0_RDNA1;
  5219. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5220. nwarps = NWARPS_Q8_0_RDNA1;
  5221. } else if (compute_capability >= CC_VOLTA) {
  5222. mmq_x = MMQ_X_Q8_0_AMPERE;
  5223. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5224. nwarps = NWARPS_Q8_0_AMPERE;
  5225. } else if (compute_capability >= MIN_CC_DP4A) {
  5226. mmq_x = MMQ_X_Q8_0_PASCAL;
  5227. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5228. nwarps = NWARPS_Q8_0_PASCAL;
  5229. } else {
  5230. GGML_ASSERT(false);
  5231. }
  5232. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5233. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5234. const dim3 block_nums(block_num_x, block_num_y, 1);
  5235. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5236. if (nrows_x % mmq_y == 0) {
  5237. const bool need_check = false;
  5238. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5239. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5240. } else {
  5241. const bool need_check = true;
  5242. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5243. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5244. }
  5245. }
  5246. static void ggml_mul_mat_q2_K_q8_1_cuda(
  5247. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5248. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5249. int id;
  5250. CUDA_CHECK(cudaGetDevice(&id));
  5251. const int compute_capability = g_device_caps[id].cc;
  5252. int mmq_x, mmq_y, nwarps;
  5253. if (compute_capability >= CC_RDNA2) {
  5254. mmq_x = MMQ_X_Q2_K_RDNA2;
  5255. mmq_y = MMQ_Y_Q2_K_RDNA2;
  5256. nwarps = NWARPS_Q2_K_RDNA2;
  5257. } else if (compute_capability >= CC_OFFSET_AMD) {
  5258. mmq_x = MMQ_X_Q2_K_RDNA1;
  5259. mmq_y = MMQ_Y_Q2_K_RDNA1;
  5260. nwarps = NWARPS_Q2_K_RDNA1;
  5261. } else if (compute_capability >= CC_VOLTA) {
  5262. mmq_x = MMQ_X_Q2_K_AMPERE;
  5263. mmq_y = MMQ_Y_Q2_K_AMPERE;
  5264. nwarps = NWARPS_Q2_K_AMPERE;
  5265. } else if (compute_capability >= MIN_CC_DP4A) {
  5266. mmq_x = MMQ_X_Q2_K_PASCAL;
  5267. mmq_y = MMQ_Y_Q2_K_PASCAL;
  5268. nwarps = NWARPS_Q2_K_PASCAL;
  5269. } else {
  5270. GGML_ASSERT(false);
  5271. }
  5272. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5273. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5274. const dim3 block_nums(block_num_x, block_num_y, 1);
  5275. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5276. if (nrows_x % mmq_y == 0) {
  5277. const bool need_check = false;
  5278. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5279. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5280. } else {
  5281. const bool need_check = true;
  5282. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5283. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5284. }
  5285. }
  5286. static void ggml_mul_mat_q3_K_q8_1_cuda(
  5287. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5288. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5289. #if QK_K == 256
  5290. int id;
  5291. CUDA_CHECK(cudaGetDevice(&id));
  5292. const int compute_capability = g_device_caps[id].cc;
  5293. int mmq_x, mmq_y, nwarps;
  5294. if (compute_capability >= CC_RDNA2) {
  5295. mmq_x = MMQ_X_Q3_K_RDNA2;
  5296. mmq_y = MMQ_Y_Q3_K_RDNA2;
  5297. nwarps = NWARPS_Q3_K_RDNA2;
  5298. } else if (compute_capability >= CC_OFFSET_AMD) {
  5299. mmq_x = MMQ_X_Q3_K_RDNA1;
  5300. mmq_y = MMQ_Y_Q3_K_RDNA1;
  5301. nwarps = NWARPS_Q3_K_RDNA1;
  5302. } else if (compute_capability >= CC_VOLTA) {
  5303. mmq_x = MMQ_X_Q3_K_AMPERE;
  5304. mmq_y = MMQ_Y_Q3_K_AMPERE;
  5305. nwarps = NWARPS_Q3_K_AMPERE;
  5306. } else if (compute_capability >= MIN_CC_DP4A) {
  5307. mmq_x = MMQ_X_Q3_K_PASCAL;
  5308. mmq_y = MMQ_Y_Q3_K_PASCAL;
  5309. nwarps = NWARPS_Q3_K_PASCAL;
  5310. } else {
  5311. GGML_ASSERT(false);
  5312. }
  5313. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5314. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5315. const dim3 block_nums(block_num_x, block_num_y, 1);
  5316. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5317. if (nrows_x % mmq_y == 0) {
  5318. const bool need_check = false;
  5319. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5320. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5321. } else {
  5322. const bool need_check = true;
  5323. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5324. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5325. }
  5326. #endif
  5327. }
  5328. static void ggml_mul_mat_q4_K_q8_1_cuda(
  5329. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5330. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5331. int id;
  5332. CUDA_CHECK(cudaGetDevice(&id));
  5333. const int compute_capability = g_device_caps[id].cc;
  5334. int mmq_x, mmq_y, nwarps;
  5335. if (compute_capability >= CC_RDNA2) {
  5336. mmq_x = MMQ_X_Q4_K_RDNA2;
  5337. mmq_y = MMQ_Y_Q4_K_RDNA2;
  5338. nwarps = NWARPS_Q4_K_RDNA2;
  5339. } else if (compute_capability >= CC_OFFSET_AMD) {
  5340. mmq_x = MMQ_X_Q4_K_RDNA1;
  5341. mmq_y = MMQ_Y_Q4_K_RDNA1;
  5342. nwarps = NWARPS_Q4_K_RDNA1;
  5343. } else if (compute_capability >= CC_VOLTA) {
  5344. mmq_x = MMQ_X_Q4_K_AMPERE;
  5345. mmq_y = MMQ_Y_Q4_K_AMPERE;
  5346. nwarps = NWARPS_Q4_K_AMPERE;
  5347. } else if (compute_capability >= MIN_CC_DP4A) {
  5348. mmq_x = MMQ_X_Q4_K_PASCAL;
  5349. mmq_y = MMQ_Y_Q4_K_PASCAL;
  5350. nwarps = NWARPS_Q4_K_PASCAL;
  5351. } else {
  5352. GGML_ASSERT(false);
  5353. }
  5354. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5355. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5356. const dim3 block_nums(block_num_x, block_num_y, 1);
  5357. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5358. if (nrows_x % mmq_y == 0) {
  5359. const bool need_check = false;
  5360. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5361. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5362. } else {
  5363. const bool need_check = true;
  5364. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5365. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5366. }
  5367. }
  5368. static void ggml_mul_mat_q5_K_q8_1_cuda(
  5369. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5370. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5371. int id;
  5372. CUDA_CHECK(cudaGetDevice(&id));
  5373. const int compute_capability = g_device_caps[id].cc;
  5374. int mmq_x, mmq_y, nwarps;
  5375. if (compute_capability >= CC_RDNA2) {
  5376. mmq_x = MMQ_X_Q5_K_RDNA2;
  5377. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5378. nwarps = NWARPS_Q5_K_RDNA2;
  5379. } else if (compute_capability >= CC_OFFSET_AMD) {
  5380. mmq_x = MMQ_X_Q5_K_RDNA1;
  5381. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5382. nwarps = NWARPS_Q5_K_RDNA1;
  5383. } else if (compute_capability >= CC_VOLTA) {
  5384. mmq_x = MMQ_X_Q5_K_AMPERE;
  5385. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5386. nwarps = NWARPS_Q5_K_AMPERE;
  5387. } else if (compute_capability >= MIN_CC_DP4A) {
  5388. mmq_x = MMQ_X_Q5_K_PASCAL;
  5389. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5390. nwarps = NWARPS_Q5_K_PASCAL;
  5391. } else {
  5392. GGML_ASSERT(false);
  5393. }
  5394. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5395. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5396. const dim3 block_nums(block_num_x, block_num_y, 1);
  5397. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5398. if (nrows_x % mmq_y == 0) {
  5399. const bool need_check = false;
  5400. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5401. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5402. } else {
  5403. const bool need_check = true;
  5404. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5405. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5406. }
  5407. }
  5408. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5409. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5410. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5411. int id;
  5412. CUDA_CHECK(cudaGetDevice(&id));
  5413. const int compute_capability = g_device_caps[id].cc;
  5414. int mmq_x, mmq_y, nwarps;
  5415. if (compute_capability >= CC_RDNA2) {
  5416. mmq_x = MMQ_X_Q6_K_RDNA2;
  5417. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5418. nwarps = NWARPS_Q6_K_RDNA2;
  5419. } else if (compute_capability >= CC_OFFSET_AMD) {
  5420. mmq_x = MMQ_X_Q6_K_RDNA1;
  5421. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5422. nwarps = NWARPS_Q6_K_RDNA1;
  5423. } else if (compute_capability >= CC_VOLTA) {
  5424. mmq_x = MMQ_X_Q6_K_AMPERE;
  5425. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5426. nwarps = NWARPS_Q6_K_AMPERE;
  5427. } else if (compute_capability >= MIN_CC_DP4A) {
  5428. mmq_x = MMQ_X_Q6_K_PASCAL;
  5429. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5430. nwarps = NWARPS_Q6_K_PASCAL;
  5431. } else {
  5432. GGML_ASSERT(false);
  5433. }
  5434. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5435. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5436. const dim3 block_nums(block_num_x, block_num_y, 1);
  5437. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5438. if (nrows_x % mmq_y == 0) {
  5439. const bool need_check = false;
  5440. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5441. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5442. } else {
  5443. const bool need_check = true;
  5444. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5445. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5446. }
  5447. }
  5448. static void ggml_mul_mat_p021_f16_f32_cuda(
  5449. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5450. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5451. const dim3 block_nums(1, nrows_x, nchannels_y);
  5452. const dim3 block_dims(WARP_SIZE, 1, 1);
  5453. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5454. }
  5455. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5456. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5457. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5458. const dim3 block_nums(1, nrows_x, nchannels_y);
  5459. const dim3 block_dims(WARP_SIZE, 1, 1);
  5460. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5461. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5462. }
  5463. static void ggml_cpy_f32_f32_cuda(
  5464. const char * cx, char * cdst, const int ne,
  5465. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5466. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5467. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5468. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5469. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5470. }
  5471. static void ggml_cpy_f32_f16_cuda(
  5472. const char * cx, char * cdst, const int ne,
  5473. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5474. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5475. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5476. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5477. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5478. }
  5479. static void ggml_cpy_f32_q8_0_cuda(
  5480. const char * cx, char * cdst, const int ne,
  5481. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5482. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5483. GGML_ASSERT(ne % QK8_0 == 0);
  5484. const int num_blocks = ne / QK8_0;
  5485. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5486. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5487. }
  5488. static void ggml_cpy_f32_q4_0_cuda(
  5489. const char * cx, char * cdst, const int ne,
  5490. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5491. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5492. GGML_ASSERT(ne % QK4_0 == 0);
  5493. const int num_blocks = ne / QK4_0;
  5494. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5495. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5496. }
  5497. static void ggml_cpy_f32_q4_1_cuda(
  5498. const char * cx, char * cdst, const int ne,
  5499. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5500. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5501. GGML_ASSERT(ne % QK4_1 == 0);
  5502. const int num_blocks = ne / QK4_1;
  5503. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5504. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5505. }
  5506. static void ggml_cpy_f16_f16_cuda(
  5507. const char * cx, char * cdst, const int ne,
  5508. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5509. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5510. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5511. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5512. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5513. }
  5514. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5515. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5516. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5517. }
  5518. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5519. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5520. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5521. }
  5522. template<typename T>
  5523. static void rope_cuda(
  5524. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5525. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5526. ) {
  5527. GGML_ASSERT(ncols % 2 == 0);
  5528. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5529. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5530. const dim3 block_nums(nrows, num_blocks_x, 1);
  5531. if (pos == nullptr) {
  5532. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5533. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5534. );
  5535. } else {
  5536. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5537. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5538. );
  5539. }
  5540. }
  5541. template<typename T>
  5542. static void rope_neox_cuda(
  5543. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5544. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5545. ) {
  5546. GGML_ASSERT(ncols % 2 == 0);
  5547. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5548. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5549. const dim3 block_nums(nrows, num_blocks_x, 1);
  5550. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5551. const float inv_ndims = -1.0f / n_dims;
  5552. if (pos == nullptr) {
  5553. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5554. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5555. theta_scale, inv_ndims
  5556. );
  5557. } else {
  5558. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5559. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5560. theta_scale, inv_ndims
  5561. );
  5562. }
  5563. }
  5564. static void rope_glm_f32_cuda(
  5565. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5566. float freq_base, int n_ctx, cudaStream_t stream
  5567. ) {
  5568. GGML_ASSERT(ncols % 4 == 0);
  5569. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5570. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5571. const dim3 block_nums(num_blocks_x, nrows, 1);
  5572. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5573. }
  5574. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5575. const int k_rows, const int n_heads_log2_floor, const float m0,
  5576. const float m1, cudaStream_t stream) {
  5577. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5578. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5579. const dim3 block_nums(num_blocks_x, nrows, 1);
  5580. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5581. }
  5582. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5583. const dim3 block_dims(WARP_SIZE, 1, 1);
  5584. const dim3 block_nums(1, nrows, 1);
  5585. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5586. }
  5587. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5588. // bitonic sort requires ncols to be power of 2
  5589. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5590. const dim3 block_dims(ncols, 1, 1);
  5591. const dim3 block_nums(1, nrows, 1);
  5592. if (order == GGML_SORT_ASC) {
  5593. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5594. } else if (order == GGML_SORT_DESC) {
  5595. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5596. } else {
  5597. GGML_ASSERT(false);
  5598. }
  5599. }
  5600. 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) {
  5601. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5602. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5603. const dim3 block_nums(nrows_x, block_num_x, 1);
  5604. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5605. }
  5606. static void soft_max_f16_cuda(const float * x, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, cudaStream_t stream) {
  5607. int nth = WARP_SIZE;
  5608. while (nth < ncols_x/2 && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5609. const dim3 block_dims(nth, 1, 1);
  5610. const dim3 block_nums(nrows_x, 1, 1);
  5611. const size_t shmem = (GGML_PAD(ncols_x, 2*WARP_SIZE) + WARP_SIZE)*sizeof(half);
  5612. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  5613. if (shmem <= g_device_caps[g_main_device].smpb) {
  5614. switch (ncols_x) {
  5615. case 32:
  5616. soft_max_f16<true, 32, 32, true><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5617. break;
  5618. case 64:
  5619. soft_max_f16<true, 64, 32, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5620. break;
  5621. case 128:
  5622. soft_max_f16<true, 128, 64, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5623. break;
  5624. case 256:
  5625. soft_max_f16<true, 256, 128, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5626. break;
  5627. case 512:
  5628. soft_max_f16<true, 512, 256, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5629. break;
  5630. case 1024:
  5631. soft_max_f16<true, 1024, 512, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5632. break;
  5633. case 2048:
  5634. soft_max_f16<true, 2048, 1024, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5635. break;
  5636. case 4096:
  5637. soft_max_f16<true, 4096, 1024, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5638. break;
  5639. default:
  5640. soft_max_f16<true, 0, 0, true><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5641. break;
  5642. }
  5643. } else {
  5644. const size_t shmem_low = WARP_SIZE*sizeof(half);
  5645. soft_max_f16<false, 0, 0, true><<<block_nums, block_dims, shmem_low, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5646. }
  5647. }
  5648. static void soft_max_f32_cuda(const float * x, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, cudaStream_t stream) {
  5649. int nth = WARP_SIZE;
  5650. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5651. const dim3 block_dims(nth, 1, 1);
  5652. const dim3 block_nums(nrows_x, 1, 1);
  5653. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  5654. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  5655. if (shmem < g_device_caps[g_main_device].smpb) {
  5656. switch (ncols_x) {
  5657. case 32:
  5658. soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5659. break;
  5660. case 64:
  5661. soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5662. break;
  5663. case 128:
  5664. soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5665. break;
  5666. case 256:
  5667. soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5668. break;
  5669. case 512:
  5670. soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5671. break;
  5672. case 1024:
  5673. soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5674. break;
  5675. case 2048:
  5676. soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5677. break;
  5678. case 4096:
  5679. soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5680. break;
  5681. default:
  5682. soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5683. break;
  5684. }
  5685. } else {
  5686. const size_t shmem_low = WARP_SIZE*sizeof(float);
  5687. soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5688. }
  5689. }
  5690. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5691. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5692. int offset_delta,
  5693. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5694. const int parallel_elements = OW * KW * KH;
  5695. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5696. dim3 block_nums(num_blocks, OH, IC);
  5697. im2col_f32_f16<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, offset_delta, IW, IH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  5698. }
  5699. // buffer pool for cuda
  5700. #define MAX_CUDA_BUFFERS 256
  5701. struct scoped_spin_lock {
  5702. std::atomic_flag& lock;
  5703. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5704. while (lock.test_and_set(std::memory_order_acquire)) {
  5705. ; // spin
  5706. }
  5707. }
  5708. ~scoped_spin_lock() {
  5709. lock.clear(std::memory_order_release);
  5710. }
  5711. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5712. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5713. };
  5714. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5715. // #define DEBUG_CUDA_MALLOC
  5716. struct ggml_cuda_buffer {
  5717. void * ptr = nullptr;
  5718. size_t size = 0;
  5719. };
  5720. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5721. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  5722. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  5723. scoped_spin_lock lock(g_cuda_pool_lock);
  5724. #ifdef DEBUG_CUDA_MALLOC
  5725. int nnz = 0;
  5726. size_t max_size = 0;
  5727. #endif
  5728. size_t best_diff = 1ull << 36;
  5729. int ibest = -1;
  5730. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5731. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5732. if (b.ptr != nullptr) {
  5733. #ifdef DEBUG_CUDA_MALLOC
  5734. ++nnz;
  5735. if (b.size > max_size) max_size = b.size;
  5736. #endif
  5737. if (b.size >= size) {
  5738. size_t diff = b.size - size;
  5739. if (diff < best_diff) {
  5740. best_diff = diff;
  5741. ibest = i;
  5742. if (!best_diff) {
  5743. void * ptr = b.ptr;
  5744. *actual_size = b.size;
  5745. b.ptr = nullptr;
  5746. b.size = 0;
  5747. return ptr;
  5748. }
  5749. }
  5750. }
  5751. }
  5752. }
  5753. if (ibest >= 0) {
  5754. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  5755. void * ptr = b.ptr;
  5756. *actual_size = b.size;
  5757. b.ptr = nullptr;
  5758. b.size = 0;
  5759. return ptr;
  5760. }
  5761. void * ptr;
  5762. size_t look_ahead_size = (size_t) (1.05 * size);
  5763. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5764. ggml_cuda_set_device(device);
  5765. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5766. *actual_size = look_ahead_size;
  5767. g_cuda_pool_size[device] += look_ahead_size;
  5768. #ifdef DEBUG_CUDA_MALLOC
  5769. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  5770. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  5771. #endif
  5772. return ptr;
  5773. }
  5774. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  5775. scoped_spin_lock lock(g_cuda_pool_lock);
  5776. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5777. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5778. if (b.ptr == nullptr) {
  5779. b.ptr = ptr;
  5780. b.size = size;
  5781. return;
  5782. }
  5783. }
  5784. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5785. ggml_cuda_set_device(device);
  5786. CUDA_CHECK(cudaFree(ptr));
  5787. g_cuda_pool_size[device] -= size;
  5788. }
  5789. #if !defined(GGML_USE_HIPBLAS)
  5790. // pool with virtual memory
  5791. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  5792. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  5793. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  5794. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  5795. scoped_spin_lock lock(g_cuda_pool_lock);
  5796. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  5797. const size_t alignment = 128;
  5798. size = alignment * ((size + alignment - 1) / alignment);
  5799. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  5800. if (size > avail) {
  5801. // round up to the next multiple of the granularity
  5802. size_t reserve_size = size - avail;
  5803. const size_t granularity = g_device_caps[device].vmm_granularity;
  5804. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  5805. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  5806. // allocate more physical memory
  5807. CUmemAllocationProp prop = {};
  5808. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5809. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5810. prop.location.id = device;
  5811. CUmemGenericAllocationHandle handle;
  5812. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  5813. // reserve virtual address space (if not already reserved)
  5814. if (g_cuda_pool_addr[device] == 0) {
  5815. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  5816. }
  5817. // map at the end of the pool
  5818. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  5819. // the memory allocation handle is no longer needed after mapping
  5820. CU_CHECK(cuMemRelease(handle));
  5821. // set access
  5822. CUmemAccessDesc access = {};
  5823. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5824. access.location.id = device;
  5825. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  5826. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  5827. // add to the pool
  5828. g_cuda_pool_size[device] += reserve_size;
  5829. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  5830. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  5831. // (unsigned long long) (reserve_size/1024/1024));
  5832. }
  5833. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  5834. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  5835. *actual_size = size;
  5836. g_cuda_pool_used[device] += size;
  5837. #ifdef DEBUG_CUDA_MALLOC
  5838. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  5839. #endif
  5840. return ptr;
  5841. }
  5842. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  5843. scoped_spin_lock lock(g_cuda_pool_lock);
  5844. #ifdef DEBUG_CUDA_MALLOC
  5845. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  5846. #endif
  5847. g_cuda_pool_used[device] -= size;
  5848. // all deallocations must be in reverse order of the allocations
  5849. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  5850. }
  5851. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  5852. if (g_device_caps[device].vmm) {
  5853. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  5854. } else {
  5855. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  5856. }
  5857. }
  5858. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  5859. if (g_device_caps[device].vmm) {
  5860. ggml_cuda_pool_free_vmm(device, ptr, size);
  5861. } else {
  5862. ggml_cuda_pool_free_leg(device, ptr, size);
  5863. }
  5864. }
  5865. #else
  5866. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  5867. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  5868. #endif // !defined(GGML_USE_HIPBLAS)
  5869. template<typename T>
  5870. struct cuda_pool_alloc {
  5871. int device = -1;
  5872. T * ptr = nullptr;
  5873. size_t actual_size = 0;
  5874. // size is in number of elements
  5875. T * alloc(size_t size) {
  5876. GGML_ASSERT(ptr == nullptr);
  5877. CUDA_CHECK(cudaGetDevice(&device));
  5878. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  5879. return ptr;
  5880. }
  5881. cuda_pool_alloc(size_t size) {
  5882. alloc(size);
  5883. }
  5884. ~cuda_pool_alloc() {
  5885. if (ptr != nullptr) {
  5886. ggml_cuda_pool_free(device, ptr, actual_size);
  5887. }
  5888. }
  5889. T * get() {
  5890. return ptr;
  5891. }
  5892. cuda_pool_alloc() = default;
  5893. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  5894. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  5895. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  5896. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  5897. };
  5898. static bool g_cublas_loaded = false;
  5899. bool ggml_cublas_loaded(void) {
  5900. return g_cublas_loaded;
  5901. }
  5902. void ggml_init_cublas() {
  5903. static bool initialized = false;
  5904. if (!initialized) {
  5905. #ifdef __HIP_PLATFORM_AMD__
  5906. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5907. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5908. rocblas_initialize();
  5909. CUDA_CHECK(cudaDeviceSynchronize());
  5910. #endif
  5911. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5912. initialized = true;
  5913. g_cublas_loaded = false;
  5914. return;
  5915. }
  5916. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5917. int64_t total_vram = 0;
  5918. #if defined(GGML_CUDA_FORCE_MMQ)
  5919. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5920. #else
  5921. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5922. #endif
  5923. #if defined(CUDA_USE_TENSOR_CORES)
  5924. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5925. #else
  5926. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5927. #endif
  5928. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5929. for (int id = 0; id < g_device_count; ++id) {
  5930. int device_vmm = 0;
  5931. #if !defined(GGML_USE_HIPBLAS)
  5932. CUdevice device;
  5933. CU_CHECK(cuDeviceGet(&device, id));
  5934. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  5935. if (device_vmm) {
  5936. CUmemAllocationProp alloc_prop = {};
  5937. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5938. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5939. alloc_prop.location.id = id;
  5940. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  5941. }
  5942. #endif // !defined(GGML_USE_HIPBLAS)
  5943. g_device_caps[id].vmm = !!device_vmm;
  5944. cudaDeviceProp prop;
  5945. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5946. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  5947. g_tensor_split[id] = total_vram;
  5948. total_vram += prop.totalGlobalMem;
  5949. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5950. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5951. #else
  5952. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  5953. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5954. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  5955. }
  5956. for (int id = 0; id < g_device_count; ++id) {
  5957. g_tensor_split[id] /= total_vram;
  5958. }
  5959. for (int id = 0; id < g_device_count; ++id) {
  5960. ggml_cuda_set_device(id);
  5961. // create cuda streams
  5962. for (int is = 0; is < MAX_STREAMS; ++is) {
  5963. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5964. }
  5965. // create cublas handle
  5966. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5967. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5968. }
  5969. // configure logging to stdout
  5970. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5971. initialized = true;
  5972. g_cublas_loaded = true;
  5973. }
  5974. }
  5975. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5976. if (tensor_split == nullptr) {
  5977. return;
  5978. }
  5979. bool all_zero = true;
  5980. for (int i = 0; i < g_device_count; ++i) {
  5981. if (tensor_split[i] != 0.0f) {
  5982. all_zero = false;
  5983. break;
  5984. }
  5985. }
  5986. if (all_zero) {
  5987. return;
  5988. }
  5989. float split_sum = 0.0f;
  5990. for (int i = 0; i < g_device_count; ++i) {
  5991. g_tensor_split[i] = split_sum;
  5992. split_sum += tensor_split[i];
  5993. }
  5994. for (int i = 0; i < g_device_count; ++i) {
  5995. g_tensor_split[i] /= split_sum;
  5996. }
  5997. }
  5998. void * ggml_cuda_host_malloc(size_t size) {
  5999. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6000. return nullptr;
  6001. }
  6002. void * ptr = nullptr;
  6003. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6004. if (err != cudaSuccess) {
  6005. // clear the error
  6006. cudaGetLastError();
  6007. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6008. size/1024.0/1024.0, cudaGetErrorString(err));
  6009. return nullptr;
  6010. }
  6011. return ptr;
  6012. }
  6013. void ggml_cuda_host_free(void * ptr) {
  6014. CUDA_CHECK(cudaFreeHost(ptr));
  6015. }
  6016. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6017. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6018. cudaMemcpyKind kind;
  6019. char * src_ptr;
  6020. if (src->backend == GGML_BACKEND_CPU) {
  6021. kind = cudaMemcpyHostToDevice;
  6022. src_ptr = (char *) src->data;
  6023. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  6024. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6025. kind = cudaMemcpyDeviceToDevice;
  6026. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6027. int id;
  6028. CUDA_CHECK(cudaGetDevice(&id));
  6029. src_ptr = (char *) extra->data_device[id];
  6030. } else {
  6031. GGML_ASSERT(false);
  6032. }
  6033. char * dst_ptr = (char *) dst;
  6034. const int64_t ne0 = src->ne[0];
  6035. const int64_t nb0 = src->nb[0];
  6036. const int64_t nb1 = src->nb[1];
  6037. const int64_t nb2 = src->nb[2];
  6038. const int64_t nb3 = src->nb[3];
  6039. const enum ggml_type type = src->type;
  6040. const int64_t ts = ggml_type_size(type);
  6041. const int64_t bs = ggml_blck_size(type);
  6042. int64_t i1_diff = i1_high - i1_low;
  6043. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6044. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6045. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6046. } else if (nb0 == ts) {
  6047. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6048. } else {
  6049. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6050. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6051. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6052. // pretend the row is a matrix with cols=1
  6053. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6054. if (r != cudaSuccess) return r;
  6055. }
  6056. return cudaSuccess;
  6057. }
  6058. }
  6059. static void ggml_cuda_op_get_rows(
  6060. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6061. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6062. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6063. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6064. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6065. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6066. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6067. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6068. switch (src0->type) {
  6069. case GGML_TYPE_F16:
  6070. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6071. break;
  6072. case GGML_TYPE_F32:
  6073. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6074. break;
  6075. case GGML_TYPE_Q4_0:
  6076. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6077. break;
  6078. case GGML_TYPE_Q4_1:
  6079. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6080. break;
  6081. case GGML_TYPE_Q5_0:
  6082. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6083. break;
  6084. case GGML_TYPE_Q5_1:
  6085. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6086. break;
  6087. case GGML_TYPE_Q8_0:
  6088. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6089. break;
  6090. default:
  6091. // TODO: k-quants
  6092. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6093. GGML_ASSERT(false);
  6094. break;
  6095. }
  6096. }
  6097. template<class op>
  6098. static void ggml_cuda_op_bin_bcast(
  6099. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6100. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6101. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6102. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6103. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6104. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6105. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6106. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6107. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6108. } else {
  6109. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6110. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6111. GGML_ASSERT(false);
  6112. }
  6113. }
  6114. static void ggml_cuda_op_repeat(
  6115. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6116. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6117. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6118. (void) src1;
  6119. (void) src1_d;
  6120. }
  6121. static void ggml_cuda_op_add(
  6122. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6123. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6124. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6125. }
  6126. static void ggml_cuda_op_acc(
  6127. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6128. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6129. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6130. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6131. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6132. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6133. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6134. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6135. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6136. int offset = dst->op_params[3] / 4; // offset in bytes
  6137. 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);
  6138. (void) dst;
  6139. }
  6140. static void ggml_cuda_op_mul(
  6141. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6142. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6143. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6144. }
  6145. static void ggml_cuda_op_div(
  6146. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6147. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6148. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6149. }
  6150. static void ggml_cuda_op_gelu(
  6151. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6152. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6153. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6154. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6155. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6156. (void) src1;
  6157. (void) dst;
  6158. (void) src1_dd;
  6159. }
  6160. static void ggml_cuda_op_silu(
  6161. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6162. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6163. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6164. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6165. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6166. (void) src1;
  6167. (void) dst;
  6168. (void) src1_dd;
  6169. }
  6170. static void ggml_cuda_op_gelu_quick(
  6171. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6172. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6173. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6174. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6175. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6176. (void) src1;
  6177. (void) dst;
  6178. (void) src1_dd;
  6179. }
  6180. static void ggml_cuda_op_tanh(
  6181. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6182. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6183. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6184. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6185. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6186. (void) src1;
  6187. (void) dst;
  6188. (void) src1_dd;
  6189. }
  6190. static void ggml_cuda_op_relu(
  6191. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6192. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6193. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6194. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6195. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6196. (void) src1;
  6197. (void) dst;
  6198. (void) src1_dd;
  6199. }
  6200. static void ggml_cuda_op_leaky_relu(
  6201. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6202. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6203. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6204. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6205. float negative_slope;
  6206. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6207. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6208. (void) src1;
  6209. (void) dst;
  6210. (void) src1_dd;
  6211. }
  6212. static void ggml_cuda_op_sqr(
  6213. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6214. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6215. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6216. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6217. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6218. (void) src1;
  6219. (void) dst;
  6220. (void) src1_dd;
  6221. }
  6222. static void ggml_cuda_op_norm(
  6223. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6224. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6225. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6226. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6227. const int64_t ne00 = src0->ne[0];
  6228. const int64_t nrows = ggml_nrows(src0);
  6229. float eps;
  6230. memcpy(&eps, dst->op_params, sizeof(float));
  6231. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6232. (void) src1;
  6233. (void) dst;
  6234. (void) src1_dd;
  6235. }
  6236. static void ggml_cuda_op_group_norm(
  6237. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6238. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6239. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6240. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6241. int num_groups = dst->op_params[0];
  6242. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6243. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  6244. (void) src1;
  6245. (void) dst;
  6246. (void) src1_dd;
  6247. }
  6248. static void ggml_cuda_op_concat(
  6249. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6250. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6251. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6252. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6253. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6254. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6255. 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);
  6256. }
  6257. (void) src1;
  6258. (void) dst;
  6259. }
  6260. static void ggml_cuda_op_upscale(
  6261. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6262. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6263. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6264. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6265. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6266. const int scale_factor = dst->op_params[0];
  6267. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  6268. (void) src1;
  6269. (void) dst;
  6270. (void) src1_dd;
  6271. }
  6272. static void ggml_cuda_op_pad(
  6273. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6274. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6275. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6276. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6277. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6278. pad_f32_cuda(src0_dd, dst_dd,
  6279. src0->ne[0], src0->ne[1], src0->ne[2],
  6280. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  6281. (void) src1;
  6282. (void) dst;
  6283. (void) src1_dd;
  6284. }
  6285. static void ggml_cuda_op_rms_norm(
  6286. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6287. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6288. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6289. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6290. const int64_t ne00 = src0->ne[0];
  6291. const int64_t nrows = ggml_nrows(src0);
  6292. float eps;
  6293. memcpy(&eps, dst->op_params, sizeof(float));
  6294. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6295. (void) src1;
  6296. (void) dst;
  6297. (void) src1_dd;
  6298. }
  6299. static void ggml_cuda_op_mul_mat_q(
  6300. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6301. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6302. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6303. const int64_t ne00 = src0->ne[0];
  6304. const int64_t ne10 = src1->ne[0];
  6305. GGML_ASSERT(ne10 % QK8_1 == 0);
  6306. const int64_t ne0 = dst->ne[0];
  6307. const int64_t row_diff = row_high - row_low;
  6308. int id;
  6309. CUDA_CHECK(cudaGetDevice(&id));
  6310. // the main device has a larger memory buffer to hold the results from all GPUs
  6311. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  6312. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6313. switch (src0->type) {
  6314. case GGML_TYPE_Q4_0:
  6315. 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);
  6316. break;
  6317. case GGML_TYPE_Q4_1:
  6318. 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);
  6319. break;
  6320. case GGML_TYPE_Q5_0:
  6321. 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);
  6322. break;
  6323. case GGML_TYPE_Q5_1:
  6324. 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);
  6325. break;
  6326. case GGML_TYPE_Q8_0:
  6327. 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);
  6328. break;
  6329. case GGML_TYPE_Q2_K:
  6330. 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);
  6331. break;
  6332. case GGML_TYPE_Q3_K:
  6333. 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);
  6334. break;
  6335. case GGML_TYPE_Q4_K:
  6336. 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);
  6337. break;
  6338. case GGML_TYPE_Q5_K:
  6339. 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);
  6340. break;
  6341. case GGML_TYPE_Q6_K:
  6342. 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);
  6343. break;
  6344. default:
  6345. GGML_ASSERT(false);
  6346. break;
  6347. }
  6348. (void) src1;
  6349. (void) dst;
  6350. (void) src1_ddf_i;
  6351. }
  6352. static int64_t get_row_rounding(ggml_type type) {
  6353. int64_t min_compute_capability = INT_MAX;
  6354. int64_t max_compute_capability = INT_MIN;
  6355. for (int id = 0; id < g_device_count; ++id) {
  6356. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6357. if (min_compute_capability > g_device_caps[id].cc) {
  6358. min_compute_capability = g_device_caps[id].cc;
  6359. }
  6360. if (max_compute_capability < g_device_caps[id].cc) {
  6361. max_compute_capability = g_device_caps[id].cc;
  6362. }
  6363. }
  6364. }
  6365. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6366. switch(type) {
  6367. case GGML_TYPE_Q4_0:
  6368. case GGML_TYPE_Q4_1:
  6369. case GGML_TYPE_Q5_0:
  6370. case GGML_TYPE_Q5_1:
  6371. case GGML_TYPE_Q8_0:
  6372. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  6373. case GGML_TYPE_F16:
  6374. case GGML_TYPE_F32:
  6375. return 1;
  6376. case GGML_TYPE_Q2_K:
  6377. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  6378. case GGML_TYPE_Q3_K:
  6379. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  6380. case GGML_TYPE_Q4_K:
  6381. case GGML_TYPE_Q5_K:
  6382. case GGML_TYPE_Q6_K:
  6383. case GGML_TYPE_IQ2_XXS:
  6384. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  6385. default:
  6386. GGML_ASSERT(false);
  6387. }
  6388. #else
  6389. switch(type) {
  6390. case GGML_TYPE_Q4_0:
  6391. case GGML_TYPE_Q4_1:
  6392. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  6393. case GGML_TYPE_Q5_0:
  6394. case GGML_TYPE_Q5_1:
  6395. case GGML_TYPE_Q8_0:
  6396. return 64;
  6397. case GGML_TYPE_F16:
  6398. case GGML_TYPE_F32:
  6399. return 1;
  6400. case GGML_TYPE_Q2_K:
  6401. case GGML_TYPE_Q3_K:
  6402. case GGML_TYPE_Q4_K:
  6403. case GGML_TYPE_Q5_K:
  6404. case GGML_TYPE_IQ2_XXS:
  6405. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  6406. case GGML_TYPE_Q6_K:
  6407. return 64;
  6408. default:
  6409. GGML_ASSERT(false);
  6410. }
  6411. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6412. }
  6413. static void ggml_cuda_op_mul_mat_vec_q(
  6414. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6415. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6416. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6417. GGML_ASSERT(ggml_nrows(src1) == 1);
  6418. const int64_t ne00 = src0->ne[0];
  6419. const int64_t row_diff = row_high - row_low;
  6420. switch (src0->type) {
  6421. case GGML_TYPE_Q4_0:
  6422. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6423. break;
  6424. case GGML_TYPE_Q4_1:
  6425. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6426. break;
  6427. case GGML_TYPE_Q5_0:
  6428. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6429. break;
  6430. case GGML_TYPE_Q5_1:
  6431. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6432. break;
  6433. case GGML_TYPE_Q8_0:
  6434. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6435. break;
  6436. case GGML_TYPE_Q2_K:
  6437. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6438. break;
  6439. case GGML_TYPE_Q3_K:
  6440. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6441. break;
  6442. case GGML_TYPE_Q4_K:
  6443. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6444. break;
  6445. case GGML_TYPE_Q5_K:
  6446. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6447. break;
  6448. case GGML_TYPE_Q6_K:
  6449. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6450. break;
  6451. case GGML_TYPE_IQ2_XXS:
  6452. mul_mat_vec_iq2_xxs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6453. break;
  6454. default:
  6455. GGML_ASSERT(false);
  6456. break;
  6457. }
  6458. (void) src1;
  6459. (void) dst;
  6460. (void) src1_ddf_i;
  6461. (void) src1_ncols;
  6462. (void) src1_padded_row_size;
  6463. }
  6464. static void ggml_cuda_op_dequantize_mul_mat_vec(
  6465. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6466. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6467. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6468. const int64_t ne00 = src0->ne[0];
  6469. const int64_t row_diff = row_high - row_low;
  6470. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6471. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  6472. #ifdef GGML_CUDA_F16
  6473. cuda_pool_alloc<half> src1_dfloat_a;
  6474. half * src1_dfloat = nullptr; // dfloat == half
  6475. bool src1_convert_f16 =
  6476. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  6477. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  6478. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  6479. if (src1_convert_f16) {
  6480. src1_dfloat = src1_dfloat_a.alloc(ne00);
  6481. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  6482. ne00, 1, sizeof(float), 0, 0,
  6483. ne00, 1, sizeof(half), 0, 0, stream);
  6484. }
  6485. #else
  6486. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  6487. #endif // GGML_CUDA_F16
  6488. switch (src0->type) {
  6489. case GGML_TYPE_Q4_0:
  6490. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6491. break;
  6492. case GGML_TYPE_Q4_1:
  6493. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6494. break;
  6495. case GGML_TYPE_Q5_0:
  6496. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6497. break;
  6498. case GGML_TYPE_Q5_1:
  6499. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6500. break;
  6501. case GGML_TYPE_Q8_0:
  6502. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6503. break;
  6504. case GGML_TYPE_Q2_K:
  6505. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6506. break;
  6507. case GGML_TYPE_Q3_K:
  6508. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6509. break;
  6510. case GGML_TYPE_Q4_K:
  6511. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6512. break;
  6513. case GGML_TYPE_Q5_K:
  6514. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6515. break;
  6516. case GGML_TYPE_Q6_K:
  6517. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6518. break;
  6519. case GGML_TYPE_F16:
  6520. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6521. break;
  6522. default:
  6523. GGML_ASSERT(false);
  6524. break;
  6525. }
  6526. (void) src1;
  6527. (void) dst;
  6528. (void) src1_ddq_i;
  6529. (void) src1_ncols;
  6530. (void) src1_padded_row_size;
  6531. }
  6532. static void ggml_cuda_op_mul_mat_cublas(
  6533. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6534. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6535. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6536. GGML_ASSERT(src0_dd_i != nullptr);
  6537. GGML_ASSERT(src1_ddf_i != nullptr);
  6538. GGML_ASSERT(dst_dd_i != nullptr);
  6539. const int64_t ne00 = src0->ne[0];
  6540. const int64_t ne10 = src1->ne[0];
  6541. const int64_t ne0 = dst->ne[0];
  6542. const int64_t row_diff = row_high - row_low;
  6543. int id;
  6544. CUDA_CHECK(cudaGetDevice(&id));
  6545. // the main device has a larger memory buffer to hold the results from all GPUs
  6546. // ldc == nrows of the matrix that cuBLAS writes into
  6547. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6548. const int compute_capability = g_device_caps[id].cc;
  6549. 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) {
  6550. //printf("this branch\n");
  6551. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  6552. cuda_pool_alloc<half> src0_as_f16;
  6553. if (src0->type != GGML_TYPE_F16) {
  6554. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  6555. GGML_ASSERT(to_fp16_cuda != nullptr);
  6556. size_t ne = row_diff*ne00;
  6557. src0_as_f16.alloc(ne);
  6558. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  6559. }
  6560. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  6561. cuda_pool_alloc<half> src1_as_f16;
  6562. if (src1->type != GGML_TYPE_F16) {
  6563. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6564. GGML_ASSERT(to_fp16_cuda != nullptr);
  6565. size_t ne = src1_ncols*ne10;
  6566. src1_as_f16.alloc(ne);
  6567. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  6568. }
  6569. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  6570. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  6571. const half alpha_f16 = 1.0f;
  6572. const half beta_f16 = 0.0f;
  6573. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6574. CUBLAS_CHECK(
  6575. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6576. row_diff, src1_ncols, ne10,
  6577. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6578. src1_ptr, CUDA_R_16F, ne10,
  6579. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  6580. CUBLAS_COMPUTE_16F,
  6581. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6582. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6583. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  6584. } else {
  6585. cuda_pool_alloc<float> src0_ddq_as_f32;
  6586. cuda_pool_alloc<float> src1_ddq_as_f32;
  6587. if (src0->type != GGML_TYPE_F32) {
  6588. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6589. GGML_ASSERT(to_fp32_cuda != nullptr);
  6590. src0_ddq_as_f32.alloc(row_diff*ne00);
  6591. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  6592. }
  6593. if (src1->type != GGML_TYPE_F32) {
  6594. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  6595. GGML_ASSERT(to_fp32_cuda != nullptr);
  6596. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  6597. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  6598. }
  6599. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  6600. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  6601. const float alpha = 1.0f;
  6602. const float beta = 0.0f;
  6603. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6604. CUBLAS_CHECK(
  6605. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6606. row_diff, src1_ncols, ne10,
  6607. &alpha, src0_ddf_i, ne00,
  6608. src1_ddf1_i, ne10,
  6609. &beta, dst_dd_i, ldc));
  6610. }
  6611. (void) dst;
  6612. (void) src1_ddq_i;
  6613. (void) src1_padded_row_size;
  6614. }
  6615. static void ggml_cuda_op_rope(
  6616. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6617. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6618. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6619. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6620. GGML_ASSERT(src0->type == dst->type);
  6621. const int64_t ne00 = src0->ne[0];
  6622. const int64_t ne01 = src0->ne[1];
  6623. const int64_t ne2 = dst->ne[2];
  6624. const int64_t nrows = ggml_nrows(src0);
  6625. //const int n_past = ((int32_t *) dst->op_params)[0];
  6626. const int n_dims = ((int32_t *) dst->op_params)[1];
  6627. const int mode = ((int32_t *) dst->op_params)[2];
  6628. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6629. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6630. // RoPE alteration for extended context
  6631. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6632. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6633. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6634. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6635. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6636. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6637. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6638. const int32_t * pos = nullptr;
  6639. if ((mode & 1) == 0) {
  6640. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6641. GGML_ASSERT(src1->ne[0] == ne2);
  6642. pos = (const int32_t *) src1_dd;
  6643. }
  6644. const bool is_neox = mode & 2;
  6645. const bool is_glm = mode & 4;
  6646. rope_corr_dims corr_dims;
  6647. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6648. // compute
  6649. if (is_glm) {
  6650. GGML_ASSERT(false);
  6651. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6652. } else if (is_neox) {
  6653. if (src0->type == GGML_TYPE_F32) {
  6654. rope_neox_cuda(
  6655. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6656. attn_factor, corr_dims, main_stream
  6657. );
  6658. } else if (src0->type == GGML_TYPE_F16) {
  6659. rope_neox_cuda(
  6660. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6661. attn_factor, corr_dims, main_stream
  6662. );
  6663. } else {
  6664. GGML_ASSERT(false);
  6665. }
  6666. } else {
  6667. if (src0->type == GGML_TYPE_F32) {
  6668. rope_cuda(
  6669. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6670. attn_factor, corr_dims, main_stream
  6671. );
  6672. } else if (src0->type == GGML_TYPE_F16) {
  6673. rope_cuda(
  6674. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6675. attn_factor, corr_dims, main_stream
  6676. );
  6677. } else {
  6678. GGML_ASSERT(false);
  6679. }
  6680. }
  6681. (void) src1;
  6682. (void) dst;
  6683. (void) src1_dd;
  6684. }
  6685. static void ggml_cuda_op_alibi(
  6686. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6687. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6688. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6689. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6690. const int64_t ne00 = src0->ne[0];
  6691. const int64_t ne01 = src0->ne[1];
  6692. const int64_t ne02 = src0->ne[2];
  6693. const int64_t nrows = ggml_nrows(src0);
  6694. //const int n_past = ((int32_t *) dst->op_params)[0];
  6695. const int n_head = ((int32_t *) dst->op_params)[1];
  6696. float max_bias;
  6697. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6698. //GGML_ASSERT(ne01 + n_past == ne00);
  6699. GGML_ASSERT(n_head == ne02);
  6700. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6701. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6702. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6703. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6704. (void) src1;
  6705. (void) src1_dd;
  6706. }
  6707. static void ggml_cuda_op_im2col(
  6708. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6709. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6710. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6711. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6712. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6713. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6714. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6715. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6716. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6717. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6718. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6719. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6720. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6721. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6722. const int64_t IW = src1->ne[0];
  6723. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6724. const int64_t KW = src0->ne[0];
  6725. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6726. const int64_t OW = dst->ne[1];
  6727. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6728. im2col_f32_f16_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  6729. (void) src0;
  6730. (void) src0_dd;
  6731. }
  6732. static void ggml_cuda_op_sum_rows(
  6733. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6734. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6735. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6736. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6737. const int64_t ncols = src0->ne[0];
  6738. const int64_t nrows = ggml_nrows(src0);
  6739. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6740. (void) src1;
  6741. (void) dst;
  6742. (void) src1_dd;
  6743. }
  6744. static void ggml_cuda_op_argsort(
  6745. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6746. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6747. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6748. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6749. const int64_t ncols = src0->ne[0];
  6750. const int64_t nrows = ggml_nrows(src0);
  6751. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6752. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6753. (void) src1;
  6754. (void) dst;
  6755. (void) src1_dd;
  6756. }
  6757. static void ggml_cuda_op_diag_mask_inf(
  6758. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6759. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6760. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6761. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6762. const int64_t ne00 = src0->ne[0];
  6763. const int64_t ne01 = src0->ne[1];
  6764. const int nrows0 = ggml_nrows(src0);
  6765. const int n_past = ((int32_t *) dst->op_params)[0];
  6766. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6767. (void) src1;
  6768. (void) dst;
  6769. (void) src1_dd;
  6770. }
  6771. static void ggml_cuda_op_soft_max(
  6772. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6773. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6774. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6775. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6776. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6777. const int64_t ne00 = src0->ne[0];
  6778. const int64_t nrows_x = ggml_nrows(src0);
  6779. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6780. float scale = 1.0f;
  6781. memcpy(&scale, dst->op_params, sizeof(float));
  6782. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6783. const bool use_f16_soft_max = false;
  6784. #else
  6785. #ifdef GGML_CUDA_F16
  6786. const bool use_f16_soft_max = true;
  6787. #else
  6788. const bool use_f16_soft_max = false;
  6789. #endif // GGML_CUDA_F16
  6790. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6791. if (use_f16_soft_max) {
  6792. soft_max_f16_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6793. } else {
  6794. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6795. }
  6796. (void) dst;
  6797. }
  6798. static void ggml_cuda_op_scale(
  6799. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6800. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6801. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6802. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6803. float scale;
  6804. memcpy(&scale, dst->op_params, sizeof(float));
  6805. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6806. CUDA_CHECK(cudaGetLastError());
  6807. (void) src1;
  6808. (void) dst;
  6809. (void) src1_dd;
  6810. }
  6811. static void ggml_cuda_op_clamp(
  6812. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6813. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6814. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6815. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6816. float min;
  6817. float max;
  6818. memcpy(&min, dst->op_params, sizeof(float));
  6819. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6820. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6821. CUDA_CHECK(cudaGetLastError());
  6822. (void) src1;
  6823. (void) dst;
  6824. (void) src1_dd;
  6825. }
  6826. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6827. const int64_t nrows0 = ggml_nrows(src0);
  6828. const bool use_src1 = src1 != nullptr;
  6829. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6830. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6831. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6832. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6833. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6834. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6835. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6836. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6837. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6838. // dd = data device
  6839. float * src0_ddf = nullptr;
  6840. float * src1_ddf = nullptr;
  6841. float * dst_ddf = nullptr;
  6842. cuda_pool_alloc<float> src0_f;
  6843. cuda_pool_alloc<float> src1_f;
  6844. cuda_pool_alloc<float> dst_f;
  6845. ggml_cuda_set_device(g_main_device);
  6846. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6847. if (src0_on_device) {
  6848. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6849. } else {
  6850. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  6851. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6852. }
  6853. if (use_src1) {
  6854. if (src1_on_device) {
  6855. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6856. } else {
  6857. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  6858. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6859. }
  6860. }
  6861. if (dst_on_device) {
  6862. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6863. } else {
  6864. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  6865. }
  6866. // do the computation
  6867. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6868. CUDA_CHECK(cudaGetLastError());
  6869. // copy dst to host if necessary
  6870. if (!dst_on_device) {
  6871. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6872. }
  6873. if (dst->backend == GGML_BACKEND_CPU) {
  6874. CUDA_CHECK(cudaDeviceSynchronize());
  6875. }
  6876. }
  6877. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6878. static bool peer_access_enabled = false;
  6879. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6880. if (peer_access_enabled == enable_peer_access) {
  6881. return;
  6882. }
  6883. #ifdef NDEBUG
  6884. for (int id = 0; id < g_device_count; ++id) {
  6885. ggml_cuda_set_device(id);
  6886. CUDA_CHECK(cudaDeviceSynchronize());
  6887. }
  6888. for (int id = 0; id < g_device_count; ++id) {
  6889. ggml_cuda_set_device(id);
  6890. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6891. if (id == id_other) {
  6892. continue;
  6893. }
  6894. if (id != g_main_device && id_other != g_main_device) {
  6895. continue;
  6896. }
  6897. int can_access_peer;
  6898. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6899. if (can_access_peer) {
  6900. if (enable_peer_access) {
  6901. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6902. } else {
  6903. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6904. }
  6905. }
  6906. }
  6907. }
  6908. #endif // NDEBUG
  6909. peer_access_enabled = enable_peer_access;
  6910. }
  6911. static void ggml_cuda_op_mul_mat(
  6912. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6913. const bool convert_src1_to_q8_1) {
  6914. const int64_t ne00 = src0->ne[0];
  6915. const int64_t ne01 = src0->ne[1];
  6916. const int64_t ne02 = src0->ne[2];
  6917. const int64_t ne03 = src0->ne[3];
  6918. const int64_t ne10 = src1->ne[0];
  6919. const int64_t ne11 = src1->ne[1];
  6920. const int64_t ne12 = src1->ne[2];
  6921. const int64_t ne13 = src1->ne[3];
  6922. const int64_t nrows1 = ggml_nrows(src1);
  6923. GGML_ASSERT(ne03 == ne13);
  6924. const int64_t ne0 = dst->ne[0];
  6925. const int64_t ne1 = dst->ne[1];
  6926. const int nb2 = dst->nb[2];
  6927. const int nb3 = dst->nb[3];
  6928. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6929. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6930. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  6931. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6932. const int64_t i02_divisor = ne12 / ne02;
  6933. const size_t src0_ts = ggml_type_size(src0->type);
  6934. const size_t src0_bs = ggml_blck_size(src0->type);
  6935. const size_t q8_1_ts = sizeof(block_q8_1);
  6936. const size_t q8_1_bs = QK8_1;
  6937. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6938. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6939. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6940. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6941. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6942. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6943. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6944. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6945. GGML_ASSERT(!(split && ne02 > 1));
  6946. GGML_ASSERT(!(split && ne03 > 1));
  6947. GGML_ASSERT(!(split && ne02 < ne12));
  6948. struct dev_data {
  6949. cuda_pool_alloc<char> src0_dd_alloc;
  6950. cuda_pool_alloc<float> src1_ddf_alloc;
  6951. cuda_pool_alloc<char> src1_ddq_alloc;
  6952. cuda_pool_alloc<float> dst_dd_alloc;
  6953. char * src0_dd = nullptr;
  6954. float * src1_ddf = nullptr; // float
  6955. char * src1_ddq = nullptr; // q8_1
  6956. float * dst_dd = nullptr;
  6957. int64_t row_low;
  6958. int64_t row_high;
  6959. };
  6960. dev_data dev[GGML_CUDA_MAX_DEVICES];
  6961. int used_devices = 0;
  6962. for (int id = 0; id < g_device_count; ++id) {
  6963. // by default, use all rows
  6964. dev[id].row_low = 0;
  6965. dev[id].row_high = ne01;
  6966. // for multi GPU, get the row boundaries from tensor split
  6967. // and round to mul_mat_q tile sizes
  6968. if (split) {
  6969. const int64_t rounding = get_row_rounding(src0->type);
  6970. if (id != 0) {
  6971. dev[id].row_low = ne01*g_tensor_split[id];
  6972. if (dev[id].row_low < ne01) {
  6973. dev[id].row_low -= dev[id].row_low % rounding;
  6974. }
  6975. }
  6976. if (id != g_device_count - 1) {
  6977. dev[id].row_high = ne01*g_tensor_split[id + 1];
  6978. if (dev[id].row_high < ne01) {
  6979. dev[id].row_high -= dev[id].row_high % rounding;
  6980. }
  6981. }
  6982. }
  6983. }
  6984. for (int id = 0; id < g_device_count; ++id) {
  6985. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  6986. continue;
  6987. }
  6988. used_devices++;
  6989. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6990. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6991. ggml_cuda_set_device(id);
  6992. cudaStream_t stream = g_cudaStreams[id][0];
  6993. if (src0_on_device && src0_is_contiguous) {
  6994. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  6995. } else {
  6996. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  6997. }
  6998. if (src1_on_device && src1_is_contiguous) {
  6999. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7000. } else {
  7001. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7002. }
  7003. if (convert_src1_to_q8_1) {
  7004. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7005. if (src1_on_device && src1_is_contiguous) {
  7006. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7007. CUDA_CHECK(cudaGetLastError());
  7008. }
  7009. }
  7010. if (dst_on_device) {
  7011. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7012. } else {
  7013. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7014. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7015. }
  7016. }
  7017. // if multiple devices are used they need to wait for the main device
  7018. // here an event is recorded that signals that the main device has finished calculating the input data
  7019. if (split && used_devices > 1) {
  7020. ggml_cuda_set_device(g_main_device);
  7021. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7022. }
  7023. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7024. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7025. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7026. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7027. for (int id = 0; id < g_device_count; ++id) {
  7028. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7029. continue;
  7030. }
  7031. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7032. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7033. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7034. ggml_cuda_set_device(id);
  7035. cudaStream_t stream = g_cudaStreams[id][is];
  7036. // wait for main GPU data if necessary
  7037. if (split && (id != g_main_device || is != 0)) {
  7038. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7039. }
  7040. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7041. const int64_t i03 = i0 / ne12;
  7042. const int64_t i02 = i0 % ne12;
  7043. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7044. // for split tensors the data begins at i0 == i0_offset_low
  7045. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7046. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7047. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7048. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7049. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7050. // in that case an offset on dst_ddf_i is needed
  7051. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  7052. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7053. }
  7054. // copy src0, src1 to device if necessary
  7055. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  7056. if (id != g_main_device) {
  7057. if (convert_src1_to_q8_1) {
  7058. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7059. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7060. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7061. } else {
  7062. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7063. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7064. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7065. src1_ncols*ne10*sizeof(float), stream));
  7066. }
  7067. }
  7068. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  7069. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7070. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7071. } else {
  7072. GGML_ASSERT(false);
  7073. }
  7074. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  7075. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7076. CUDA_CHECK(cudaGetLastError());
  7077. }
  7078. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7079. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7080. }
  7081. // do the computation
  7082. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7083. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7084. CUDA_CHECK(cudaGetLastError());
  7085. // copy dst to host or other device if necessary
  7086. if (!dst_on_device) {
  7087. void * dst_off_device;
  7088. cudaMemcpyKind kind;
  7089. if (dst->backend == GGML_BACKEND_CPU) {
  7090. dst_off_device = dst->data;
  7091. kind = cudaMemcpyDeviceToHost;
  7092. } else if (dst->backend == GGML_BACKEND_GPU) {
  7093. dst_off_device = dst_extra->data_device[g_main_device];
  7094. kind = cudaMemcpyDeviceToDevice;
  7095. } else {
  7096. GGML_ASSERT(false);
  7097. }
  7098. if (split) {
  7099. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7100. // dst is NOT transposed.
  7101. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7102. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7103. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7104. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7105. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7106. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7107. #if !defined(GGML_USE_HIPBLAS)
  7108. if (kind == cudaMemcpyDeviceToDevice) {
  7109. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7110. cudaMemcpy3DPeerParms p = {};
  7111. p.dstDevice = g_main_device;
  7112. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7113. p.srcDevice = id;
  7114. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7115. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7116. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7117. } else
  7118. #endif
  7119. {
  7120. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7121. dst_dd_i, row_diff*sizeof(float),
  7122. row_diff*sizeof(float), src1_ncols,
  7123. kind, stream));
  7124. }
  7125. } else {
  7126. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7127. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7128. dhf_dst_i += src1_col_0*ne0;
  7129. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7130. }
  7131. }
  7132. // add event for the main device to wait on until other device is done
  7133. if (split && (id != g_main_device || is != 0)) {
  7134. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7135. }
  7136. }
  7137. }
  7138. }
  7139. // main device waits for all other devices to be finished
  7140. if (split && g_device_count > 1) {
  7141. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7142. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7143. ggml_cuda_set_device(g_main_device);
  7144. for (int id = 0; id < g_device_count; ++id) {
  7145. if (dev[id].row_low == dev[id].row_high) {
  7146. continue;
  7147. }
  7148. for (int64_t is = 0; is < is_max; ++is) {
  7149. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7150. }
  7151. }
  7152. }
  7153. if (dst->backend == GGML_BACKEND_CPU) {
  7154. ggml_cuda_set_device(g_main_device);
  7155. CUDA_CHECK(cudaDeviceSynchronize());
  7156. }
  7157. }
  7158. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7159. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  7160. }
  7161. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7162. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  7163. }
  7164. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7165. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  7166. }
  7167. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7168. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  7169. }
  7170. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7171. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  7172. }
  7173. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7174. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  7175. }
  7176. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7177. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  7178. }
  7179. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7180. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  7181. }
  7182. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7183. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  7184. }
  7185. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7186. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  7187. }
  7188. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7189. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  7190. }
  7191. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7192. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  7193. }
  7194. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7195. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  7196. }
  7197. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7198. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  7199. }
  7200. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7201. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  7202. }
  7203. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7204. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  7205. }
  7206. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7207. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  7208. }
  7209. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7210. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  7211. }
  7212. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7213. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  7214. }
  7215. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  7216. if (!g_cublas_loaded) return false;
  7217. const int64_t ne10 = src1->ne[0];
  7218. const int64_t ne0 = dst->ne[0];
  7219. const int64_t ne1 = dst->ne[1];
  7220. // TODO: find the optimal values for these
  7221. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  7222. src1->type == GGML_TYPE_F32 &&
  7223. dst->type == GGML_TYPE_F32 &&
  7224. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  7225. }
  7226. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7227. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  7228. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  7229. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  7230. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  7231. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7232. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7233. const int64_t ne00 = src0->ne[0];
  7234. const int64_t ne01 = src0->ne[1];
  7235. const int64_t ne02 = src0->ne[2];
  7236. const int64_t ne12 = src1->ne[2];
  7237. ggml_cuda_set_device(g_main_device);
  7238. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7239. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7240. void * src0_ddq = src0_extra->data_device[g_main_device];
  7241. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7242. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7243. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7244. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7245. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  7246. }
  7247. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7248. GGML_ASSERT(!ggml_is_transposed(src0));
  7249. GGML_ASSERT(!ggml_is_transposed(src1));
  7250. GGML_ASSERT(!ggml_is_permuted(src0));
  7251. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  7252. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7253. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7254. const int64_t ne00 = src0->ne[0];
  7255. const int64_t ne01 = src0->ne[1];
  7256. const int64_t ne02 = src0->ne[2];
  7257. const int64_t nb01 = src0->nb[1];
  7258. const int64_t nb02 = src0->nb[2];
  7259. const int64_t ne12 = src1->ne[2];
  7260. ggml_cuda_set_device(g_main_device);
  7261. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7262. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7263. void * src0_ddq = src0_extra->data_device[g_main_device];
  7264. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7265. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7266. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7267. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7268. const int64_t row_stride_x = nb01 / sizeof(half);
  7269. const int64_t channel_stride_x = nb02 / sizeof(half);
  7270. 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);
  7271. }
  7272. static __global__ void k_compute_batched_ptrs(
  7273. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  7274. const void ** ptrs_src, void ** ptrs_dst,
  7275. int64_t ne12, int64_t ne13,
  7276. int64_t ne23,
  7277. size_t nb02, size_t nb03,
  7278. size_t nb12, size_t nb13,
  7279. size_t nbd2, size_t nbd3,
  7280. int64_t r2, int64_t r3) {
  7281. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7282. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7283. if (i13 >= ne13 || i12 >= ne12) {
  7284. return;
  7285. }
  7286. int64_t i03 = i13 / r3;
  7287. int64_t i02 = i12 / r2;
  7288. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  7289. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  7290. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  7291. }
  7292. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7293. GGML_ASSERT(!ggml_is_transposed(src0));
  7294. GGML_ASSERT(!ggml_is_transposed(src1));
  7295. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  7296. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7297. GGML_TENSOR_BINARY_OP_LOCALS
  7298. const int64_t ne_dst = ggml_nelements(dst);
  7299. ggml_cuda_set_device(g_main_device);
  7300. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7301. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7302. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7303. void * src0_ddq = src0_extra->data_device[g_main_device];
  7304. half * src0_f16 = (half *) src0_ddq;
  7305. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7306. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7307. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7308. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7309. // convert src1 to fp16
  7310. cuda_pool_alloc<half> src1_f16_alloc;
  7311. if (src1->type != GGML_TYPE_F16) {
  7312. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7313. const int64_t ne_src1 = ggml_nelements(src1);
  7314. src1_f16_alloc.alloc(ne_src1);
  7315. GGML_ASSERT(to_fp16_cuda != nullptr);
  7316. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  7317. }
  7318. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  7319. cuda_pool_alloc<half> dst_f16;
  7320. char * dst_t;
  7321. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  7322. cudaDataType_t cu_data_type = CUDA_R_16F;
  7323. // dst strides
  7324. size_t nbd2 = dst->nb[2];
  7325. size_t nbd3 = dst->nb[3];
  7326. const half alpha_f16 = 1.0f;
  7327. const half beta_f16 = 0.0f;
  7328. const float alpha_f32 = 1.0f;
  7329. const float beta_f32 = 0.0f;
  7330. const void * alpha = &alpha_f16;
  7331. const void * beta = &beta_f16;
  7332. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  7333. dst_t = (char *) dst_f16.alloc(ne_dst);
  7334. nbd2 /= sizeof(float) / sizeof(half);
  7335. nbd3 /= sizeof(float) / sizeof(half);
  7336. } else {
  7337. dst_t = (char *) dst_ddf;
  7338. cu_compute_type = CUBLAS_COMPUTE_32F;
  7339. cu_data_type = CUDA_R_32F;
  7340. alpha = &alpha_f32;
  7341. beta = &beta_f32;
  7342. }
  7343. GGML_ASSERT(ne12 % ne02 == 0);
  7344. GGML_ASSERT(ne13 % ne03 == 0);
  7345. // broadcast factors
  7346. const int64_t r2 = ne12/ne02;
  7347. const int64_t r3 = ne13/ne03;
  7348. #if 0
  7349. // use cublasGemmEx
  7350. {
  7351. for (int i13 = 0; i13 < ne13; ++i13) {
  7352. for (int i12 = 0; i12 < ne12; ++i12) {
  7353. int i03 = i13 / r3;
  7354. int i02 = i12 / r2;
  7355. CUBLAS_CHECK(
  7356. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7357. ne01, ne11, ne10,
  7358. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  7359. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  7360. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  7361. cu_compute_type,
  7362. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7363. }
  7364. }
  7365. }
  7366. #else
  7367. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  7368. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  7369. // use cublasGemmStridedBatchedEx
  7370. CUBLAS_CHECK(
  7371. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7372. ne01, ne11, ne10,
  7373. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  7374. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  7375. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  7376. ne12*ne13,
  7377. cu_compute_type,
  7378. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7379. } else {
  7380. // use cublasGemmBatchedEx
  7381. const int ne23 = ne12*ne13;
  7382. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  7383. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  7384. dim3 block_dims(ne13, ne12);
  7385. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  7386. src0_f16, src1_f16, dst_t,
  7387. ptrs_src.get(), ptrs_dst.get(),
  7388. ne12, ne13,
  7389. ne23,
  7390. nb02, nb03,
  7391. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  7392. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  7393. nbd2, nbd3,
  7394. r2, r3);
  7395. CUDA_CHECK(cudaGetLastError());
  7396. CUBLAS_CHECK(
  7397. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7398. ne01, ne11, ne10,
  7399. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  7400. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  7401. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  7402. ne23,
  7403. cu_compute_type,
  7404. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7405. }
  7406. #endif
  7407. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  7408. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7409. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  7410. }
  7411. }
  7412. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7413. const bool all_on_device =
  7414. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  7415. (src1->backend == GGML_BACKEND_GPU) &&
  7416. ( dst->backend == GGML_BACKEND_GPU);
  7417. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  7418. int64_t min_compute_capability = INT_MAX;
  7419. for (int id = 0; id < g_device_count; ++id) {
  7420. if (min_compute_capability > g_device_caps[id].cc && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  7421. min_compute_capability = g_device_caps[id].cc;
  7422. }
  7423. }
  7424. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7425. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  7426. bool use_mul_mat_q = ggml_is_quantized(src0->type);
  7427. #ifdef CUDA_USE_TENSOR_CORES
  7428. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  7429. #endif // CUDA_USE_TENSOR_CORES
  7430. #else
  7431. const bool fp16_performance_good = min_compute_capability >= CC_VOLTA;
  7432. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  7433. #ifdef CUDA_USE_TENSOR_CORES
  7434. // when tensor cores are available, use them for large batch size
  7435. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  7436. use_mul_mat_q = use_mul_mat_q && !(fp16_performance_good && src1->ne[1] > MMQ_MAX_BATCH_SIZE);
  7437. #endif // CUDA_USE_TENSOR_CORES
  7438. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7439. use_mul_mat_q = use_mul_mat_q && ggml_cuda_supports_mmq(src0->type);
  7440. // debug helpers
  7441. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  7442. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  7443. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  7444. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  7445. //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);
  7446. //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);
  7447. if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  7448. // KQ single-batch
  7449. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  7450. } else if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  7451. // KQV single-batch
  7452. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  7453. } else if (!split && all_on_device && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  7454. // KQ + KQV multi-batch
  7455. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  7456. } else if (src0->type == GGML_TYPE_F32) {
  7457. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7458. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  7459. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->type == GGML_TYPE_F32) {
  7460. #ifdef GGML_CUDA_FORCE_DMMV
  7461. const bool use_mul_mat_vec_q = false;
  7462. #else
  7463. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  7464. #endif // GGML_CUDA_FORCE_DMMV
  7465. if (use_mul_mat_vec_q) {
  7466. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  7467. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  7468. } else {
  7469. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  7470. }
  7471. } else {
  7472. if (use_mul_mat_q) {
  7473. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  7474. } else {
  7475. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7476. }
  7477. }
  7478. } else {
  7479. GGML_ASSERT(false);
  7480. }
  7481. }
  7482. #if 0
  7483. template<typename ... Srcs>
  7484. static __global__ void k_compute_batched_ptrs_id(
  7485. const void ** ptrs_src, void ** ptrs_dst,
  7486. int ne12, int ne13,
  7487. int ne23,
  7488. int nb02, int nb03,
  7489. int nb12, int nb13,
  7490. int nb2, int nb3,
  7491. int r2, int r3,
  7492. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  7493. const half * src1_f16, half * dst_f16,
  7494. const int32_t * ids, const int id,
  7495. Srcs... src0s) {
  7496. int i = ids[id];
  7497. half * src0_f16;
  7498. const void * srcs_ar[] = { (const half *) src0s... };
  7499. if (src0_type == GGML_TYPE_F16) {
  7500. src0_f16 = (half *) srcs_ar[i];
  7501. } else {
  7502. src0_f16 = src0_as_f16;
  7503. if (threadIdx.x == 0 && threadIdx.y == 0) {
  7504. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  7505. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  7506. }
  7507. }
  7508. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7509. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7510. if (i13 >= ne13 || i12 >= ne12) {
  7511. return;
  7512. }
  7513. int i03 = i13 / r3;
  7514. int i02 = i12 / r2;
  7515. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  7516. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  7517. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  7518. }
  7519. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  7520. const struct ggml_tensor * ids = dst->src[0];
  7521. const struct ggml_tensor * src1 = dst->src[1];
  7522. const struct ggml_tensor * src00 = dst->src[2];
  7523. const int id = dst->op_params[0];
  7524. GGML_ASSERT(!ggml_is_transposed(src00));
  7525. GGML_ASSERT(!ggml_is_transposed(src1));
  7526. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  7527. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7528. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  7529. const int64_t ne01 = src00->ne[1];
  7530. const int64_t ne02 = src00->ne[2];
  7531. const int64_t ne03 = src00->ne[3];
  7532. //const int64_t nb01 = src00->nb[1];
  7533. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  7534. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  7535. const int64_t ne10 = src1->ne[0];
  7536. const int64_t ne11 = src1->ne[1];
  7537. const int64_t ne12 = src1->ne[2];
  7538. const int64_t ne13 = src1->ne[3];
  7539. //const int64_t nb11 = src1->nb[1];
  7540. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  7541. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  7542. const int64_t ne1 = ggml_nelements(src1);
  7543. const int64_t ne = ggml_nelements(dst);
  7544. ggml_cuda_set_device(g_main_device);
  7545. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7546. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7547. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7548. //void * src0_ddq = src0_extra->data_device[g_main_device];
  7549. //half * src0_as_f16 = (half *) src0_ddq;
  7550. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7551. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7552. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7553. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7554. // convert src1 to fp16
  7555. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7556. GGML_ASSERT(to_fp16_cuda != nullptr);
  7557. size_t src1_as = 0;
  7558. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7559. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7560. size_t dst_as = 0;
  7561. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7562. GGML_ASSERT(ne12 % ne02 == 0);
  7563. GGML_ASSERT(ne13 % ne03 == 0);
  7564. // broadcast factors
  7565. const int64_t r2 = ne12/ne02;
  7566. const int64_t r3 = ne13/ne03;
  7567. const half alpha_f16 = 1.0f;
  7568. const half beta_f16 = 0.0f;
  7569. // use cublasGemmBatchedEx
  7570. const int ne23 = ne12*ne13;
  7571. const void ** ptrs_src = nullptr;
  7572. void ** ptrs_dst = nullptr;
  7573. size_t ptrs_src_s = 0;
  7574. size_t ptrs_dst_s = 0;
  7575. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7576. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7577. int64_t src0_ne = ggml_nelements(src00);
  7578. half * src0_as_f16 = nullptr;
  7579. size_t src0_as = 0;
  7580. if (src00->type != GGML_TYPE_F16) {
  7581. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7582. }
  7583. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7584. dim3 block_dims(ne13, ne12);
  7585. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7586. ptrs_src, ptrs_dst,
  7587. ne12, ne13,
  7588. ne23,
  7589. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7590. nb12, nb13,
  7591. dst->nb[2], dst->nb[3],
  7592. r2, r3,
  7593. src00->type, src0_as_f16, src0_ne,
  7594. src1_as_f16, dst_f16,
  7595. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7596. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7597. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7598. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7599. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7600. );
  7601. CUDA_CHECK(cudaGetLastError());
  7602. CUBLAS_CHECK(
  7603. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7604. ne01, ne11, ne10,
  7605. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7606. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7607. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7608. ne23,
  7609. CUBLAS_COMPUTE_16F,
  7610. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7611. if (src0_as != 0) {
  7612. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7613. }
  7614. if (ptrs_src_s != 0) {
  7615. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7616. }
  7617. if (ptrs_dst_s != 0) {
  7618. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7619. }
  7620. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7621. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7622. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7623. ggml_cuda_pool_free(dst_f16, dst_as);
  7624. }
  7625. #endif
  7626. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7627. #if 0
  7628. ggml_cuda_mul_mat_id_cublas(dst);
  7629. // TODO: mmq/mmv support
  7630. #endif
  7631. const int64_t nb11 = src1->nb[1];
  7632. const int64_t nb1 = dst->nb[1];
  7633. const struct ggml_tensor * ids = src0;
  7634. const int32_t id = ((int32_t *) dst->op_params)[0];
  7635. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7636. std::vector<char> ids_host(ggml_nbytes(ids));
  7637. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7638. if (ids->backend == GGML_BACKEND_GPU) {
  7639. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7640. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7641. CUDA_CHECK(cudaStreamSynchronize(stream));
  7642. } else {
  7643. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7644. }
  7645. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7646. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7647. ggml_tensor_extra_gpu src1_row_extra;
  7648. ggml_tensor_extra_gpu dst_row_extra;
  7649. ggml_tensor src1_row = *src1;
  7650. ggml_tensor dst_row = *dst;
  7651. src1_row.backend = GGML_BACKEND_GPU;
  7652. dst_row.backend = GGML_BACKEND_GPU;
  7653. src1_row.extra = &src1_row_extra;
  7654. dst_row.extra = &dst_row_extra;
  7655. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7656. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7657. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7658. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7659. if (src1->ne[1] == 1) {
  7660. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7661. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7662. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7663. //int32_t row_id;
  7664. //CUDA_CHECK(cudaMemcpyAsync(&row_id, ids_dev + i01*ids->nb[1] + id*ids->nb[0], sizeof(int32_t), cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  7665. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7666. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7667. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7668. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7669. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7670. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7671. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7672. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7673. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7674. }
  7675. } else {
  7676. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  7677. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  7678. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  7679. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  7680. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7681. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7682. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7683. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  7684. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7685. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7686. int64_t num_src1_rows = 0;
  7687. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7688. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7689. if (row_id_i != row_id) {
  7690. continue;
  7691. }
  7692. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7693. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  7694. nb11, src1_kind, stream));
  7695. num_src1_rows++;
  7696. }
  7697. if (num_src1_rows == 0) {
  7698. continue;
  7699. }
  7700. src1_row.ne[1] = num_src1_rows;
  7701. dst_row.ne[1] = num_src1_rows;
  7702. src1_row.nb[1] = nb11;
  7703. src1_row.nb[2] = num_src1_rows*nb11;
  7704. src1_row.nb[3] = num_src1_rows*nb11;
  7705. dst_row.nb[1] = nb1;
  7706. dst_row.nb[2] = num_src1_rows*nb1;
  7707. dst_row.nb[3] = num_src1_rows*nb1;
  7708. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7709. num_src1_rows = 0;
  7710. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7711. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7712. if (row_id_i != row_id) {
  7713. continue;
  7714. }
  7715. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7716. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  7717. nb1, dst_kind, stream));
  7718. num_src1_rows++;
  7719. }
  7720. }
  7721. }
  7722. if (dst->backend == GGML_BACKEND_CPU) {
  7723. CUDA_CHECK(cudaStreamSynchronize(stream));
  7724. }
  7725. }
  7726. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7727. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7728. }
  7729. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7730. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7731. }
  7732. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7733. const int64_t ne = ggml_nelements(src0);
  7734. GGML_ASSERT(ne == ggml_nelements(src1));
  7735. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7736. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7737. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7738. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7739. const int64_t ne00 = src0->ne[0];
  7740. const int64_t ne01 = src0->ne[1];
  7741. GGML_ASSERT(src0->ne[3] == 1);
  7742. const int64_t nb00 = src0->nb[0];
  7743. const int64_t nb01 = src0->nb[1];
  7744. const int64_t nb02 = src0->nb[2];
  7745. const int64_t ne10 = src1->ne[0];
  7746. const int64_t ne11 = src1->ne[1];
  7747. GGML_ASSERT(src1->ne[3] == 1);
  7748. const int64_t nb10 = src1->nb[0];
  7749. const int64_t nb11 = src1->nb[1];
  7750. const int64_t nb12 = src1->nb[2];
  7751. ggml_cuda_set_device(g_main_device);
  7752. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7753. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7754. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7755. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7756. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7757. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7758. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7759. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7760. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7761. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7762. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7763. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7764. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7765. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7766. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7767. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7768. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7769. } else {
  7770. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7771. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7772. GGML_ASSERT(false);
  7773. }
  7774. (void) dst;
  7775. }
  7776. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7777. // TODO: why do we pass dst as src1 here?
  7778. ggml_cuda_cpy(src0, dst, nullptr);
  7779. (void) src1;
  7780. }
  7781. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7782. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7783. }
  7784. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7785. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7786. }
  7787. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7788. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7789. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7790. }
  7791. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7792. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7793. }
  7794. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7795. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7796. }
  7797. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7798. GGML_ASSERT(ggml_is_contiguous(src0));
  7799. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7800. }
  7801. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7802. GGML_ASSERT(ggml_is_contiguous(src0));
  7803. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7804. }
  7805. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7806. (void) src0;
  7807. (void) src1;
  7808. (void) dst;
  7809. }
  7810. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7811. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7812. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7813. }
  7814. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7815. const int64_t nrows = ggml_nrows(tensor);
  7816. const int64_t ne0 = tensor->ne[0];
  7817. const size_t nb1 = tensor->nb[1];
  7818. ggml_backend_type backend = tensor->backend;
  7819. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7820. memset(extra, 0, sizeof(*extra));
  7821. for (int id = 0; id < g_device_count; ++id) {
  7822. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7823. continue;
  7824. }
  7825. ggml_cuda_set_device(id);
  7826. int64_t row_low, row_high;
  7827. if (backend == GGML_BACKEND_GPU) {
  7828. row_low = 0;
  7829. row_high = nrows;
  7830. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7831. const int64_t rounding = get_row_rounding(tensor->type);
  7832. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7833. row_low -= row_low % rounding;
  7834. if (id == g_device_count - 1) {
  7835. row_high = nrows;
  7836. } else {
  7837. row_high = nrows*g_tensor_split[id + 1];
  7838. row_high -= row_high % rounding;
  7839. }
  7840. } else {
  7841. GGML_ASSERT(false);
  7842. }
  7843. if (row_low == row_high) {
  7844. continue;
  7845. }
  7846. int64_t nrows_split = row_high - row_low;
  7847. const size_t offset_split = row_low*nb1;
  7848. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7849. const size_t original_size = size;
  7850. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7851. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7852. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7853. }
  7854. char * buf;
  7855. CUDA_CHECK(cudaMalloc(&buf, size));
  7856. char * buf_host = (char *)data + offset_split;
  7857. // set padding to 0 to avoid possible NaN values
  7858. if (size > original_size) {
  7859. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7860. }
  7861. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7862. extra->data_device[id] = buf;
  7863. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7864. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7865. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7866. }
  7867. }
  7868. }
  7869. tensor->extra = extra;
  7870. }
  7871. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7872. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7873. return;
  7874. }
  7875. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7876. for (int id = 0; id < g_device_count; ++id) {
  7877. ggml_cuda_set_device(id);
  7878. if (extra->data_device[id] != nullptr) {
  7879. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7880. }
  7881. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7882. if (extra->events[id][is] != nullptr) {
  7883. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7884. }
  7885. }
  7886. }
  7887. delete extra;
  7888. }
  7889. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7890. static size_t g_temp_tensor_extra_index = 0;
  7891. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7892. if (g_temp_tensor_extras == nullptr) {
  7893. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7894. }
  7895. size_t alloc_index = g_temp_tensor_extra_index;
  7896. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7897. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7898. memset(extra, 0, sizeof(*extra));
  7899. return extra;
  7900. }
  7901. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7902. if (scratch && g_scratch_size == 0) {
  7903. return;
  7904. }
  7905. tensor->backend = GGML_BACKEND_GPU;
  7906. // recursively assign CUDA buffers until a compute tensor is found
  7907. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7908. const ggml_op src0_op = tensor->src[0]->op;
  7909. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7910. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7911. }
  7912. }
  7913. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7914. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7915. }
  7916. if (scratch && no_alloc) {
  7917. return;
  7918. }
  7919. ggml_tensor_extra_gpu * extra;
  7920. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7921. tensor->op == GGML_OP_VIEW ||
  7922. force_inplace;
  7923. const size_t size = ggml_nbytes(tensor);
  7924. ggml_cuda_set_device(g_main_device);
  7925. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7926. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7927. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7928. size_t offset = 0;
  7929. if (tensor->op == GGML_OP_VIEW) {
  7930. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7931. }
  7932. extra = ggml_cuda_alloc_temp_tensor_extra();
  7933. extra->data_device[g_main_device] = src0_ddc + offset;
  7934. } else if (tensor->op == GGML_OP_CPY) {
  7935. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7936. void * src1_ddv = src1_extra->data_device[g_main_device];
  7937. extra = ggml_cuda_alloc_temp_tensor_extra();
  7938. extra->data_device[g_main_device] = src1_ddv;
  7939. } else if (scratch) {
  7940. GGML_ASSERT(size <= g_scratch_size);
  7941. if (g_scratch_offset + size > g_scratch_size) {
  7942. g_scratch_offset = 0;
  7943. }
  7944. char * data = (char *) g_scratch_buffer;
  7945. if (data == nullptr) {
  7946. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7947. g_scratch_buffer = data;
  7948. }
  7949. extra = ggml_cuda_alloc_temp_tensor_extra();
  7950. extra->data_device[g_main_device] = data + g_scratch_offset;
  7951. g_scratch_offset += size;
  7952. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7953. } else { // allocate new buffers outside of scratch
  7954. void * data;
  7955. CUDA_CHECK(cudaMalloc(&data, size));
  7956. CUDA_CHECK(cudaMemset(data, 0, size));
  7957. extra = new ggml_tensor_extra_gpu;
  7958. memset(extra, 0, sizeof(*extra));
  7959. extra->data_device[g_main_device] = data;
  7960. }
  7961. tensor->extra = extra;
  7962. }
  7963. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7964. if (g_scratch_size == 0) {
  7965. return;
  7966. }
  7967. if (g_scratch_buffer == nullptr) {
  7968. ggml_cuda_set_device(g_main_device);
  7969. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7970. }
  7971. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7972. const bool inplace = tensor->view_src != nullptr;
  7973. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  7974. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  7975. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7976. size_t view_offset = 0;
  7977. if (tensor->op == GGML_OP_VIEW) {
  7978. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7979. }
  7980. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7981. } else {
  7982. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7983. }
  7984. tensor->extra = extra;
  7985. }
  7986. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7987. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7988. GGML_ASSERT(ggml_is_contiguous(tensor));
  7989. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7990. ggml_cuda_set_device(g_main_device);
  7991. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7992. }
  7993. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7994. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7995. }
  7996. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7997. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7998. }
  7999. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  8000. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  8001. }
  8002. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  8003. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  8004. }
  8005. void ggml_cuda_set_main_device(const int main_device) {
  8006. if (main_device >= g_device_count) {
  8007. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8008. main_device, g_device_count, g_main_device);
  8009. return;
  8010. }
  8011. if (g_main_device != main_device && g_device_count > 1) {
  8012. g_main_device = main_device;
  8013. cudaDeviceProp prop;
  8014. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8015. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8016. }
  8017. }
  8018. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  8019. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  8020. // it still won't always work as expected, but it's better than nothing
  8021. if (scratch_size > g_scratch_size) {
  8022. ggml_cuda_free_scratch();
  8023. }
  8024. g_scratch_size = std::max(g_scratch_size, scratch_size);
  8025. }
  8026. void ggml_cuda_free_scratch() {
  8027. if (g_scratch_buffer == nullptr) {
  8028. return;
  8029. }
  8030. CUDA_CHECK(cudaFree(g_scratch_buffer));
  8031. g_scratch_buffer = nullptr;
  8032. }
  8033. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8034. if (!g_cublas_loaded) return false;
  8035. ggml_cuda_func_t func;
  8036. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  8037. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  8038. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  8039. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8040. return false;
  8041. }
  8042. if (tensor->op == GGML_OP_MUL_MAT) {
  8043. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8044. #ifndef NDEBUG
  8045. 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]);
  8046. #endif
  8047. return false;
  8048. }
  8049. }
  8050. switch (tensor->op) {
  8051. case GGML_OP_REPEAT:
  8052. func = ggml_cuda_repeat;
  8053. break;
  8054. case GGML_OP_GET_ROWS:
  8055. func = ggml_cuda_get_rows;
  8056. break;
  8057. case GGML_OP_DUP:
  8058. func = ggml_cuda_dup;
  8059. break;
  8060. case GGML_OP_ADD:
  8061. func = ggml_cuda_add;
  8062. break;
  8063. case GGML_OP_ACC:
  8064. func = ggml_cuda_acc;
  8065. break;
  8066. case GGML_OP_MUL:
  8067. func = ggml_cuda_mul;
  8068. break;
  8069. case GGML_OP_DIV:
  8070. func = ggml_cuda_div;
  8071. break;
  8072. case GGML_OP_UNARY:
  8073. switch (ggml_get_unary_op(tensor)) {
  8074. case GGML_UNARY_OP_GELU:
  8075. func = ggml_cuda_gelu;
  8076. break;
  8077. case GGML_UNARY_OP_SILU:
  8078. func = ggml_cuda_silu;
  8079. break;
  8080. case GGML_UNARY_OP_GELU_QUICK:
  8081. func = ggml_cuda_gelu_quick;
  8082. break;
  8083. case GGML_UNARY_OP_TANH:
  8084. func = ggml_cuda_tanh;
  8085. break;
  8086. case GGML_UNARY_OP_RELU:
  8087. func = ggml_cuda_relu;
  8088. break;
  8089. default:
  8090. return false;
  8091. }
  8092. break;
  8093. case GGML_OP_NORM:
  8094. func = ggml_cuda_norm;
  8095. break;
  8096. case GGML_OP_GROUP_NORM:
  8097. func = ggml_cuda_group_norm;
  8098. break;
  8099. case GGML_OP_CONCAT:
  8100. func = ggml_cuda_concat;
  8101. break;
  8102. case GGML_OP_UPSCALE:
  8103. func = ggml_cuda_upscale;
  8104. break;
  8105. case GGML_OP_PAD:
  8106. func = ggml_cuda_pad;
  8107. break;
  8108. case GGML_OP_LEAKY_RELU:
  8109. func = ggml_cuda_leaky_relu;
  8110. break;
  8111. case GGML_OP_RMS_NORM:
  8112. func = ggml_cuda_rms_norm;
  8113. break;
  8114. case GGML_OP_MUL_MAT:
  8115. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8116. return false;
  8117. }
  8118. func = ggml_cuda_mul_mat;
  8119. break;
  8120. case GGML_OP_MUL_MAT_ID:
  8121. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8122. return false;
  8123. }
  8124. func = ggml_cuda_mul_mat_id;
  8125. break;
  8126. case GGML_OP_SCALE:
  8127. func = ggml_cuda_scale;
  8128. break;
  8129. case GGML_OP_SQR:
  8130. func = ggml_cuda_sqr;
  8131. break;
  8132. case GGML_OP_CLAMP:
  8133. func = ggml_cuda_clamp;
  8134. break;
  8135. case GGML_OP_CPY:
  8136. func = ggml_cuda_cpy;
  8137. break;
  8138. case GGML_OP_CONT:
  8139. func = ggml_cuda_dup;
  8140. break;
  8141. case GGML_OP_NONE:
  8142. case GGML_OP_RESHAPE:
  8143. case GGML_OP_VIEW:
  8144. case GGML_OP_PERMUTE:
  8145. case GGML_OP_TRANSPOSE:
  8146. func = ggml_cuda_nop;
  8147. break;
  8148. case GGML_OP_DIAG_MASK_INF:
  8149. func = ggml_cuda_diag_mask_inf;
  8150. break;
  8151. case GGML_OP_SOFT_MAX:
  8152. func = ggml_cuda_soft_max;
  8153. break;
  8154. case GGML_OP_ROPE:
  8155. func = ggml_cuda_rope;
  8156. break;
  8157. case GGML_OP_ALIBI:
  8158. func = ggml_cuda_alibi;
  8159. break;
  8160. case GGML_OP_IM2COL:
  8161. func = ggml_cuda_im2col;
  8162. break;
  8163. case GGML_OP_SUM_ROWS:
  8164. func = ggml_cuda_sum_rows;
  8165. break;
  8166. case GGML_OP_ARGSORT:
  8167. func = ggml_cuda_argsort;
  8168. break;
  8169. default:
  8170. return false;
  8171. }
  8172. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  8173. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8174. }
  8175. if (params->ith != 0) {
  8176. return true;
  8177. }
  8178. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  8179. return true;
  8180. }
  8181. func(tensor->src[0], tensor->src[1], tensor);
  8182. return true;
  8183. }
  8184. int ggml_cuda_get_device_count() {
  8185. int device_count;
  8186. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8187. return 0;
  8188. }
  8189. return device_count;
  8190. }
  8191. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8192. cudaDeviceProp prop;
  8193. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8194. snprintf(description, description_size, "%s", prop.name);
  8195. }
  8196. ////////////////////////////////////////////////////////////////////////////////
  8197. // backend interface
  8198. #define UNUSED GGML_UNUSED
  8199. // cuda buffer
  8200. struct ggml_backend_buffer_context_cuda {
  8201. int device;
  8202. void * dev_ptr = nullptr;
  8203. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8204. size_t temp_tensor_extra_index = 0;
  8205. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  8206. ~ggml_backend_buffer_context_cuda() {
  8207. delete[] temp_tensor_extras;
  8208. }
  8209. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8210. if (temp_tensor_extras == nullptr) {
  8211. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8212. }
  8213. size_t alloc_index = temp_tensor_extra_index;
  8214. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8215. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8216. memset(extra, 0, sizeof(*extra));
  8217. return extra;
  8218. }
  8219. };
  8220. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8221. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8222. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8223. delete ctx;
  8224. }
  8225. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8226. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8227. return ctx->dev_ptr;
  8228. }
  8229. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8230. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8231. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8232. assert(tensor->view_src->buffer->buft == buffer->buft);
  8233. tensor->backend = tensor->view_src->backend;
  8234. tensor->extra = tensor->view_src->extra;
  8235. return;
  8236. }
  8237. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8238. extra->data_device[ctx->device] = tensor->data;
  8239. tensor->backend = GGML_BACKEND_GPU;
  8240. tensor->extra = extra;
  8241. if (ggml_is_quantized(tensor->type)) {
  8242. // initialize padding to 0 to avoid possible NaN values
  8243. int64_t row_low = 0;
  8244. int64_t row_high = ggml_nrows(tensor);
  8245. int64_t nrows_split = row_high - row_low;
  8246. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  8247. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8248. if (padded_size > original_size && tensor->view_src == nullptr) {
  8249. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  8250. }
  8251. }
  8252. UNUSED(buffer);
  8253. }
  8254. 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) {
  8255. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8256. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8257. ggml_cuda_set_device(ctx->device);
  8258. CUDA_CHECK(cudaDeviceSynchronize());
  8259. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8260. }
  8261. 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) {
  8262. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8263. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8264. ggml_cuda_set_device(ctx->device);
  8265. CUDA_CHECK(cudaDeviceSynchronize());
  8266. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8267. }
  8268. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8269. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8270. ggml_cuda_set_device(ctx->device);
  8271. CUDA_CHECK(cudaDeviceSynchronize());
  8272. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8273. }
  8274. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  8275. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8276. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8277. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8278. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8279. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8280. /* .cpy_tensor_from = */ NULL,
  8281. /* .cpy_tensor_to = */ NULL,
  8282. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8283. };
  8284. // cuda buffer type
  8285. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8286. int device = (int) (intptr_t) buft->context;
  8287. ggml_cuda_set_device(device);
  8288. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  8289. void * dev_ptr;
  8290. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  8291. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  8292. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  8293. }
  8294. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8295. return 128;
  8296. UNUSED(buft);
  8297. }
  8298. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  8299. int64_t row_low = 0;
  8300. int64_t row_high = ggml_nrows(tensor);
  8301. int64_t nrows_split = row_high - row_low;
  8302. size_t size = ggml_nbytes_split(tensor, nrows_split);
  8303. int64_t ne0 = tensor->ne[0];
  8304. if (ggml_is_quantized(tensor->type)) {
  8305. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8306. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8307. }
  8308. }
  8309. return size;
  8310. UNUSED(buft);
  8311. }
  8312. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  8313. return ggml_backend_is_cuda(backend);
  8314. UNUSED(buft);
  8315. }
  8316. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  8317. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  8318. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  8319. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  8320. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  8321. /* .is_host = */ nullptr,
  8322. };
  8323. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  8324. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  8325. static bool ggml_backend_cuda_buffer_type_initialized = false;
  8326. if (!ggml_backend_cuda_buffer_type_initialized) {
  8327. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  8328. ggml_backend_cuda_buffer_types[i] = {
  8329. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  8330. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  8331. };
  8332. }
  8333. ggml_backend_cuda_buffer_type_initialized = true;
  8334. }
  8335. return &ggml_backend_cuda_buffer_types[device];
  8336. }
  8337. // host buffer type
  8338. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8339. ggml_cuda_host_free(buffer->context);
  8340. }
  8341. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8342. void * ptr = ggml_cuda_host_malloc(size);
  8343. if (ptr == nullptr) {
  8344. // fallback to cpu buffer
  8345. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  8346. }
  8347. // FIXME: this is a hack to avoid having to implement a new buffer type
  8348. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  8349. buffer->buft = buft;
  8350. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  8351. return buffer;
  8352. }
  8353. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  8354. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  8355. /* .iface = */ {
  8356. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  8357. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  8358. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  8359. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  8360. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  8361. },
  8362. /* .context = */ nullptr,
  8363. };
  8364. return &ggml_backend_cuda_buffer_type_host;
  8365. }
  8366. // backend
  8367. struct ggml_backend_context_cuda {
  8368. int device;
  8369. };
  8370. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  8371. return GGML_CUDA_NAME;
  8372. UNUSED(backend);
  8373. }
  8374. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  8375. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8376. delete cuda_ctx;
  8377. delete backend;
  8378. }
  8379. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  8380. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8381. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  8382. }
  8383. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8384. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8385. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  8386. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8387. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  8388. }
  8389. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8390. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8391. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  8392. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8393. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  8394. }
  8395. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  8396. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8397. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  8398. UNUSED(backend);
  8399. }
  8400. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8401. GGML_ASSERT(!"not implemented");
  8402. return nullptr;
  8403. UNUSED(backend);
  8404. UNUSED(cgraph);
  8405. }
  8406. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  8407. GGML_ASSERT(!"not implemented");
  8408. UNUSED(backend);
  8409. UNUSED(plan);
  8410. }
  8411. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  8412. GGML_ASSERT(!"not implemented");
  8413. UNUSED(backend);
  8414. UNUSED(plan);
  8415. }
  8416. static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8417. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8418. ggml_cuda_set_main_device(cuda_ctx->device);
  8419. ggml_compute_params params = {};
  8420. params.type = GGML_TASK_COMPUTE;
  8421. params.ith = 0;
  8422. for (int i = 0; i < cgraph->n_nodes; i++) {
  8423. ggml_tensor * node = cgraph->nodes[i];
  8424. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  8425. continue;
  8426. assert(node->backend == GGML_BACKEND_GPU);
  8427. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  8428. assert(node->extra != nullptr);
  8429. for (int j = 0; j < GGML_MAX_SRC; j++) {
  8430. if (node->src[j] != nullptr) {
  8431. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  8432. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  8433. assert(node->src[j]->extra != nullptr);
  8434. }
  8435. }
  8436. bool ok = ggml_cuda_compute_forward(&params, node);
  8437. if (!ok) {
  8438. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  8439. }
  8440. GGML_ASSERT(ok);
  8441. #if 0
  8442. if (node->type == GGML_TYPE_F32) {
  8443. cudaDeviceSynchronize();
  8444. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  8445. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  8446. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  8447. ggml_type_name(node->src[0]->type),
  8448. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  8449. node->src[0]->name,
  8450. node->src[1] ? node->src[1]->name : "none");
  8451. double sum = 0.0;
  8452. double sq_sum = 0.0;
  8453. for (int i = 0; i < ggml_nelements(node); i++) {
  8454. printf("%f ", tmp[i]);
  8455. sum += tmp[i];
  8456. sq_sum += tmp[i]*tmp[i];
  8457. }
  8458. printf("\n");
  8459. printf("sum: %f, ", sum);
  8460. printf("sq_sum: %f\n", sq_sum);
  8461. }
  8462. #endif
  8463. }
  8464. UNUSED(backend);
  8465. return true;
  8466. }
  8467. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  8468. switch (op->op) {
  8469. case GGML_OP_UNARY:
  8470. switch (ggml_get_unary_op(op)) {
  8471. case GGML_UNARY_OP_GELU:
  8472. case GGML_UNARY_OP_SILU:
  8473. case GGML_UNARY_OP_RELU:
  8474. case GGML_UNARY_OP_GELU_QUICK:
  8475. case GGML_UNARY_OP_TANH:
  8476. return true;
  8477. default:
  8478. return false;
  8479. }
  8480. break;
  8481. case GGML_OP_MUL_MAT:
  8482. case GGML_OP_MUL_MAT_ID:
  8483. {
  8484. struct ggml_tensor * a;
  8485. struct ggml_tensor * b;
  8486. if (op->op == GGML_OP_MUL_MAT) {
  8487. a = op->src[0];
  8488. b = op->src[1];
  8489. } else {
  8490. a = op->src[2];
  8491. b = op->src[1];
  8492. }
  8493. if (a->ne[3] != b->ne[3]) {
  8494. return false;
  8495. }
  8496. return true;
  8497. } break;
  8498. case GGML_OP_GET_ROWS:
  8499. {
  8500. switch (op->src[0]->type) {
  8501. case GGML_TYPE_F16:
  8502. case GGML_TYPE_F32:
  8503. case GGML_TYPE_Q4_0:
  8504. case GGML_TYPE_Q4_1:
  8505. case GGML_TYPE_Q5_0:
  8506. case GGML_TYPE_Q5_1:
  8507. case GGML_TYPE_Q8_0:
  8508. return true;
  8509. default:
  8510. return false;
  8511. }
  8512. } break;
  8513. case GGML_OP_CPY:
  8514. {
  8515. ggml_type src0_type = op->src[0]->type;
  8516. ggml_type src1_type = op->src[1]->type;
  8517. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  8518. return true;
  8519. }
  8520. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  8521. return true;
  8522. }
  8523. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  8524. return true;
  8525. }
  8526. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  8527. return true;
  8528. }
  8529. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  8530. return true;
  8531. }
  8532. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8533. return true;
  8534. }
  8535. return false;
  8536. } break;
  8537. case GGML_OP_DUP:
  8538. case GGML_OP_REPEAT:
  8539. case GGML_OP_CONCAT:
  8540. {
  8541. ggml_type src0_type = op->src[0]->type;
  8542. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  8543. } break;
  8544. case GGML_OP_NONE:
  8545. case GGML_OP_RESHAPE:
  8546. case GGML_OP_VIEW:
  8547. case GGML_OP_PERMUTE:
  8548. case GGML_OP_TRANSPOSE:
  8549. case GGML_OP_NORM:
  8550. case GGML_OP_ADD:
  8551. case GGML_OP_MUL:
  8552. case GGML_OP_DIV:
  8553. case GGML_OP_RMS_NORM:
  8554. case GGML_OP_SCALE:
  8555. case GGML_OP_SQR:
  8556. case GGML_OP_CLAMP:
  8557. case GGML_OP_CONT:
  8558. case GGML_OP_DIAG_MASK_INF:
  8559. case GGML_OP_SOFT_MAX:
  8560. case GGML_OP_ROPE:
  8561. case GGML_OP_ALIBI:
  8562. case GGML_OP_IM2COL:
  8563. case GGML_OP_SUM_ROWS:
  8564. case GGML_OP_ARGSORT:
  8565. case GGML_OP_ACC:
  8566. case GGML_OP_GROUP_NORM:
  8567. case GGML_OP_UPSCALE:
  8568. case GGML_OP_PAD:
  8569. case GGML_OP_LEAKY_RELU:
  8570. return true;
  8571. default:
  8572. return false;
  8573. }
  8574. UNUSED(backend);
  8575. }
  8576. static ggml_backend_i cuda_backend_i = {
  8577. /* .get_name = */ ggml_backend_cuda_name,
  8578. /* .free = */ ggml_backend_cuda_free,
  8579. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8580. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8581. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8582. /* .cpy_tensor_from_async = */ NULL,
  8583. /* .cpy_tensor_to_async = */ NULL,
  8584. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8585. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8586. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8587. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8588. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8589. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8590. };
  8591. ggml_backend_t ggml_backend_cuda_init(int device) {
  8592. ggml_init_cublas(); // TODO: remove from ggml.c
  8593. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8594. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8595. return nullptr;
  8596. }
  8597. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8598. ggml_cuda_set_main_device(device);
  8599. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8600. /* .device = */ device
  8601. };
  8602. ggml_backend_t cuda_backend = new ggml_backend {
  8603. /* .interface = */ cuda_backend_i,
  8604. /* .context = */ ctx
  8605. };
  8606. return cuda_backend;
  8607. }
  8608. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8609. return backend->iface.get_name == ggml_backend_cuda_name;
  8610. }
  8611. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8612. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8613. return cuda_backend;
  8614. UNUSED(params);
  8615. }
  8616. extern "C" int ggml_backend_cuda_reg_devices();
  8617. int ggml_backend_cuda_reg_devices() {
  8618. int device_count = ggml_cuda_get_device_count();
  8619. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8620. for (int i = 0; i < device_count; i++) {
  8621. char name[128];
  8622. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8623. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8624. }
  8625. return device_count;
  8626. }