ggml-cuda.cu 311 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412
  1. #include <algorithm>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <limits>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <atomic>
  8. #include <assert.h>
  9. #if defined(GGML_USE_HIPBLAS)
  10. #include <hip/hip_runtime.h>
  11. #include <hipblas/hipblas.h>
  12. #include <hip/hip_fp16.h>
  13. #ifdef __HIP_PLATFORM_AMD__
  14. // for rocblas_initialize()
  15. #include "rocblas/rocblas.h"
  16. #endif // __HIP_PLATFORM_AMD__
  17. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  18. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  19. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  20. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  21. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  22. #define CUBLAS_OP_N HIPBLAS_OP_N
  23. #define CUBLAS_OP_T HIPBLAS_OP_T
  24. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  25. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  26. #define CUDA_R_16F HIPBLAS_R_16F
  27. #define CUDA_R_32F HIPBLAS_R_32F
  28. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  29. #define cublasCreate hipblasCreate
  30. #define cublasGemmEx hipblasGemmEx
  31. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  32. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  33. #define cublasHandle_t hipblasHandle_t
  34. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  35. #define cublasSetStream hipblasSetStream
  36. #define cublasSgemm hipblasSgemm
  37. #define cublasStatus_t hipblasStatus_t
  38. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  39. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  40. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  41. #define cudaDeviceProp hipDeviceProp_t
  42. #define cudaDeviceSynchronize hipDeviceSynchronize
  43. #define cudaError_t hipError_t
  44. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  45. #define cudaEventDisableTiming hipEventDisableTiming
  46. #define cudaEventRecord hipEventRecord
  47. #define cudaEvent_t hipEvent_t
  48. #define cudaEventDestroy hipEventDestroy
  49. #define cudaFree hipFree
  50. #define cudaFreeHost hipHostFree
  51. #define cudaGetDevice hipGetDevice
  52. #define cudaGetDeviceCount hipGetDeviceCount
  53. #define cudaGetDeviceProperties hipGetDeviceProperties
  54. #define cudaGetErrorString hipGetErrorString
  55. #define cudaGetLastError hipGetLastError
  56. #define cudaMalloc hipMalloc
  57. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  58. #define cudaMemcpy hipMemcpy
  59. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  60. #define cudaMemcpyAsync hipMemcpyAsync
  61. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  62. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  63. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  64. #define cudaMemcpyKind hipMemcpyKind
  65. #define cudaMemset hipMemset
  66. #define cudaMemsetAsync hipMemsetAsync
  67. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  68. #define cudaSetDevice hipSetDevice
  69. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  70. #define cudaStreamNonBlocking hipStreamNonBlocking
  71. #define cudaStreamSynchronize hipStreamSynchronize
  72. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  73. #define cudaStream_t hipStream_t
  74. #define cudaSuccess hipSuccess
  75. #else
  76. #include <cuda_runtime.h>
  77. #include <cublas_v2.h>
  78. #include <cuda_fp16.h>
  79. #endif // defined(GGML_USE_HIPBLAS)
  80. #include "ggml-cuda.h"
  81. #include "ggml.h"
  82. #include "ggml-backend-impl.h"
  83. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  84. #define CC_VOLTA 700
  85. #define CC_OFFSET_AMD 1000000
  86. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  87. #define GGML_CUDA_MAX_NODES 8192
  88. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  89. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  90. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  91. // - 7B quantum model: +100-200 MB
  92. // - 13B quantum model: +200-400 MB
  93. //
  94. //#define GGML_CUDA_FORCE_MMQ
  95. // TODO: improve this to be correct for more hardware
  96. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  97. // probably other such cases, and not sure what happens on AMD hardware
  98. #if !defined(GGML_CUDA_FORCE_MMQ)
  99. #define CUDA_USE_TENSOR_CORES
  100. #endif
  101. // max batch size to use MMQ kernels when tensor cores are available
  102. #define MMQ_MAX_BATCH_SIZE 32
  103. #if defined(GGML_USE_HIPBLAS)
  104. #define __CUDA_ARCH__ 1300
  105. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  106. defined(__gfx1150__) || defined(__gfx1151__)
  107. #define RDNA3
  108. #endif
  109. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  110. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  111. #define RDNA2
  112. #endif
  113. #ifndef __has_builtin
  114. #define __has_builtin(x) 0
  115. #endif
  116. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  117. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  118. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  119. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  120. #if __has_builtin(__builtin_elementwise_sub_sat)
  121. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  122. return reinterpret_cast<const int&>(c);
  123. #else
  124. int8x4_t c;
  125. int16_t tmp;
  126. #pragma unroll
  127. for (int i = 0; i < 4; i++) {
  128. tmp = va[i] - vb[i];
  129. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  130. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  131. c[i] = tmp;
  132. }
  133. return reinterpret_cast<int&>(c);
  134. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  135. }
  136. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  137. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  138. c = __builtin_amdgcn_sdot4(a, b, c, false);
  139. #elif defined(__gfx1100__)
  140. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  141. #elif defined(__gfx1010__) || defined(__gfx900__)
  142. int tmp1;
  143. int tmp2;
  144. asm("\n \
  145. 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 \
  146. 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 \
  147. v_add3_u32 %0, %1, %2, %0 \n \
  148. 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 \
  149. 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 \
  150. v_add3_u32 %0, %1, %2, %0 \n \
  151. "
  152. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  153. : "v"(a), "v"(b)
  154. );
  155. #else
  156. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  157. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  158. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  159. #endif
  160. return c;
  161. }
  162. #endif // defined(GGML_USE_HIPBLAS)
  163. #if defined(_MSC_VER)
  164. #pragma warning(disable: 4244 4267) // possible loss of data
  165. #endif
  166. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  167. #define CUDA_CHECK(err) \
  168. do { \
  169. cudaError_t err_ = (err); \
  170. if (err_ != cudaSuccess) { \
  171. int id; \
  172. cudaGetDevice(&id); \
  173. fprintf(stderr, "\nCUDA error %d at %s:%d: %s\n", err_, __FILE__, __LINE__, \
  174. cudaGetErrorString(err_)); \
  175. fprintf(stderr, "current device: %d\n", id); \
  176. exit(1); \
  177. } \
  178. } while (0)
  179. #if CUDART_VERSION >= 12000
  180. #define CUBLAS_CHECK(err) \
  181. do { \
  182. cublasStatus_t err_ = (err); \
  183. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  184. int id; \
  185. cudaGetDevice(&id); \
  186. fprintf(stderr, "\ncuBLAS error %d at %s:%d: %s\n", \
  187. err_, __FILE__, __LINE__, cublasGetStatusString(err_)); \
  188. fprintf(stderr, "current device: %d\n", id); \
  189. exit(1); \
  190. } \
  191. } while (0)
  192. #else
  193. #define CUBLAS_CHECK(err) \
  194. do { \
  195. cublasStatus_t err_ = (err); \
  196. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  197. int id; \
  198. cudaGetDevice(&id); \
  199. fprintf(stderr, "\ncuBLAS error %d at %s:%d\n", err_, __FILE__, __LINE__); \
  200. fprintf(stderr, "current device: %d\n", id); \
  201. exit(1); \
  202. } \
  203. } while (0)
  204. #endif // CUDART_VERSION >= 11
  205. #if CUDART_VERSION >= 11100
  206. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  207. #else
  208. #define GGML_CUDA_ASSUME(x)
  209. #endif // CUDART_VERSION >= 11100
  210. #ifdef GGML_CUDA_F16
  211. typedef half dfloat; // dequantize float
  212. typedef half2 dfloat2;
  213. #else
  214. typedef float dfloat; // dequantize float
  215. typedef float2 dfloat2;
  216. #endif //GGML_CUDA_F16
  217. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  218. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  219. int x32 = 0;
  220. x32 |= x16[0] << 0;
  221. x32 |= x16[1] << 16;
  222. return x32;
  223. }
  224. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  225. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  226. int x32 = 0;
  227. x32 |= x16[0] << 0;
  228. x32 |= x16[1] << 16;
  229. return x32;
  230. }
  231. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  232. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  233. }
  234. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  235. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  236. }
  237. template<typename T>
  238. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  239. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  240. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  241. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  242. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  243. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  244. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  245. typedef void (*ggml_cuda_op_mul_mat_t)(
  246. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  247. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  248. const int64_t src1_padded_row_size, const cudaStream_t & stream);
  249. typedef void (*ggml_cuda_op_flatten_t)(
  250. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  251. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream);
  252. // QK = number of values after dequantization
  253. // QR = QK / number of values before dequantization
  254. // QI = number of 32 bit integers before dequantization
  255. #define QK4_0 32
  256. #define QR4_0 2
  257. #define QI4_0 (QK4_0 / (4 * QR4_0))
  258. typedef struct {
  259. half d; // delta
  260. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  261. } block_q4_0;
  262. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  263. #define QK4_1 32
  264. #define QR4_1 2
  265. #define QI4_1 (QK4_1 / (4 * QR4_1))
  266. typedef struct {
  267. half2 dm; // dm.x = delta, dm.y = min
  268. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  269. } block_q4_1;
  270. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  271. #define QK5_0 32
  272. #define QR5_0 2
  273. #define QI5_0 (QK5_0 / (4 * QR5_0))
  274. typedef struct {
  275. half d; // delta
  276. uint8_t qh[4]; // 5-th bit of quants
  277. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  278. } block_q5_0;
  279. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  280. #define QK5_1 32
  281. #define QR5_1 2
  282. #define QI5_1 (QK5_1 / (4 * QR5_1))
  283. typedef struct {
  284. half2 dm; // dm.x = delta, dm.y = min
  285. uint8_t qh[4]; // 5-th bit of quants
  286. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  287. } block_q5_1;
  288. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  289. #define QK8_0 32
  290. #define QR8_0 1
  291. #define QI8_0 (QK8_0 / (4 * QR8_0))
  292. typedef struct {
  293. half d; // delta
  294. int8_t qs[QK8_0]; // quants
  295. } block_q8_0;
  296. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  297. #define QK8_1 32
  298. #define QR8_1 1
  299. #define QI8_1 (QK8_1 / (4 * QR8_1))
  300. typedef struct {
  301. half2 ds; // ds.x = delta, ds.y = sum
  302. int8_t qs[QK8_0]; // quants
  303. } block_q8_1;
  304. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  305. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  306. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  307. typedef void (*load_tiles_cuda_t)(
  308. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  309. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  310. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  311. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  312. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  313. //================================= k-quants
  314. #ifdef GGML_QKK_64
  315. #define QK_K 64
  316. #define K_SCALE_SIZE 4
  317. #else
  318. #define QK_K 256
  319. #define K_SCALE_SIZE 12
  320. #endif
  321. #define QR2_K 4
  322. #define QI2_K (QK_K / (4*QR2_K))
  323. typedef struct {
  324. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  325. uint8_t qs[QK_K/4]; // quants
  326. half2 dm; // super-block scale for quantized scales/mins
  327. } block_q2_K;
  328. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  329. #define QR3_K 4
  330. #define QI3_K (QK_K / (4*QR3_K))
  331. typedef struct {
  332. uint8_t hmask[QK_K/8]; // quants - high bit
  333. uint8_t qs[QK_K/4]; // quants - low 2 bits
  334. #ifdef GGML_QKK_64
  335. uint8_t scales[2]; // scales, quantized with 8 bits
  336. #else
  337. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  338. #endif
  339. half d; // super-block scale
  340. } block_q3_K;
  341. //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");
  342. #define QR4_K 2
  343. #define QI4_K (QK_K / (4*QR4_K))
  344. #ifdef GGML_QKK_64
  345. typedef struct {
  346. half dm[2]; // super-block scales/mins
  347. uint8_t scales[2]; // 4-bit block scales/mins
  348. uint8_t qs[QK_K/2]; // 4--bit quants
  349. } block_q4_K;
  350. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  351. #else
  352. typedef struct {
  353. half2 dm; // super-block scale for quantized scales/mins
  354. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  355. uint8_t qs[QK_K/2]; // 4--bit quants
  356. } block_q4_K;
  357. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  358. #endif
  359. #define QR5_K 2
  360. #define QI5_K (QK_K / (4*QR5_K))
  361. #ifdef GGML_QKK_64
  362. typedef struct {
  363. half d; // super-block scale
  364. int8_t scales[QK_K/16]; // block scales
  365. uint8_t qh[QK_K/8]; // quants, high bit
  366. uint8_t qs[QK_K/2]; // quants, low 4 bits
  367. } block_q5_K;
  368. 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");
  369. #else
  370. typedef struct {
  371. half2 dm; // super-block scale for quantized scales/mins
  372. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  373. uint8_t qh[QK_K/8]; // quants, high bit
  374. uint8_t qs[QK_K/2]; // quants, low 4 bits
  375. } block_q5_K;
  376. 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");
  377. #endif
  378. #define QR6_K 2
  379. #define QI6_K (QK_K / (4*QR6_K))
  380. typedef struct {
  381. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  382. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  383. int8_t scales[QK_K/16]; // scales
  384. half d; // delta
  385. } block_q6_K;
  386. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  387. #define WARP_SIZE 32
  388. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  389. #define CUDA_ADD_BLOCK_SIZE 256
  390. #define CUDA_MUL_BLOCK_SIZE 256
  391. #define CUDA_GELU_BLOCK_SIZE 256
  392. #define CUDA_SILU_BLOCK_SIZE 256
  393. #define CUDA_RELU_BLOCK_SIZE 256
  394. #define CUDA_SQR_BLOCK_SIZE 256
  395. #define CUDA_CPY_BLOCK_SIZE 32
  396. #define CUDA_SCALE_BLOCK_SIZE 256
  397. #define CUDA_CLAMP_BLOCK_SIZE 256
  398. #define CUDA_ROPE_BLOCK_SIZE 256
  399. #define CUDA_ALIBI_BLOCK_SIZE 32
  400. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  401. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  402. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  403. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  404. // dmmv = dequantize_mul_mat_vec
  405. #ifndef GGML_CUDA_DMMV_X
  406. #define GGML_CUDA_DMMV_X 32
  407. #endif
  408. #ifndef GGML_CUDA_MMV_Y
  409. #define GGML_CUDA_MMV_Y 1
  410. #endif
  411. #ifndef K_QUANTS_PER_ITERATION
  412. #define K_QUANTS_PER_ITERATION 2
  413. #else
  414. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  415. #endif
  416. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  417. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  418. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  419. #define MUL_MAT_SRC1_COL_STRIDE 128
  420. #define MAX_STREAMS 8
  421. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { nullptr };
  422. struct ggml_tensor_extra_gpu {
  423. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  424. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  425. };
  426. // this is faster on Windows
  427. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  428. inline cudaError_t ggml_cuda_set_device(const int device) {
  429. int current_device;
  430. CUDA_CHECK(cudaGetDevice(&current_device));
  431. if (device == current_device) {
  432. return cudaSuccess;
  433. }
  434. return cudaSetDevice(device);
  435. }
  436. static int g_device_count = -1;
  437. static int g_main_device = 0;
  438. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  439. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  440. static void * g_scratch_buffer = nullptr;
  441. static size_t g_scratch_size = 0; // disabled by default
  442. static size_t g_scratch_offset = 0;
  443. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  444. static __global__ void add_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  445. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  446. if (i >= kx) {
  447. return;
  448. }
  449. dst[i] = x[i] + y[i%ky];
  450. }
  451. static __global__ void add_f16_f32_f16(const half * x, const float * y, half * dst, const int k) {
  452. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  453. if (i >= k) {
  454. return;
  455. }
  456. dst[i] = __hadd(x[i], __float2half(y[i]));
  457. }
  458. static __global__ void add_f16_f32_f32(const half * x, const float * y, float * dst, const int k) {
  459. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  460. if (i >= k) {
  461. return;
  462. }
  463. dst[i] = __half2float(x[i]) + y[i];
  464. }
  465. static __global__ void mul_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  466. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  467. if (i >= kx) {
  468. return;
  469. }
  470. dst[i] = x[i] * y[i%ky];
  471. }
  472. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  473. const float GELU_COEF_A = 0.044715f;
  474. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  475. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  476. if (i >= k) {
  477. return;
  478. }
  479. float xi = x[i];
  480. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  481. }
  482. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  483. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  484. if (i >= k) {
  485. return;
  486. }
  487. dst[i] = x[i] / (1.0f + expf(-x[i]));
  488. }
  489. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  490. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  491. if (i >= k) {
  492. return;
  493. }
  494. dst[i] = fmaxf(x[i], 0);
  495. }
  496. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  497. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  498. if (i >= k) {
  499. return;
  500. }
  501. dst[i] = x[i] * x[i];
  502. }
  503. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  504. #pragma unroll
  505. for (int mask = 16; mask > 0; mask >>= 1) {
  506. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  507. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  508. }
  509. return a;
  510. }
  511. template <int block_size>
  512. static __global__ void norm_f32(const float * x, float * dst, const int ncols) {
  513. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  514. const int tid = threadIdx.x;
  515. const float eps = 1e-5f;
  516. float2 mean_var = make_float2(0.f, 0.f);
  517. for (int col = tid; col < ncols; col += block_size) {
  518. const float xi = x[row*ncols + col];
  519. mean_var.x += xi;
  520. mean_var.y += xi * xi;
  521. }
  522. // sum up partial sums
  523. mean_var = warp_reduce_sum(mean_var);
  524. if (block_size > WARP_SIZE) {
  525. __shared__ float2 s_sum[32];
  526. int warp_id = threadIdx.x / WARP_SIZE;
  527. int lane_id = threadIdx.x % WARP_SIZE;
  528. if (lane_id == 0) {
  529. s_sum[warp_id] = mean_var;
  530. }
  531. __syncthreads();
  532. mean_var = s_sum[lane_id];
  533. mean_var = warp_reduce_sum(mean_var);
  534. }
  535. const float mean = mean_var.x / ncols;
  536. const float var = mean_var.y / ncols - mean * mean;
  537. const float inv_std = rsqrtf(var + eps);
  538. for (int col = tid; col < ncols; col += block_size) {
  539. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  540. }
  541. }
  542. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  543. #pragma unroll
  544. for (int mask = 16; mask > 0; mask >>= 1) {
  545. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  546. }
  547. return x;
  548. }
  549. template <int block_size>
  550. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  551. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  552. const int tid = threadIdx.x;
  553. float tmp = 0.0f; // partial sum for thread in warp
  554. for (int col = tid; col < ncols; col += block_size) {
  555. const float xi = x[row*ncols + col];
  556. tmp += xi * xi;
  557. }
  558. // sum up partial sums
  559. tmp = warp_reduce_sum(tmp);
  560. if (block_size > WARP_SIZE) {
  561. __shared__ float s_sum[32];
  562. int warp_id = threadIdx.x / WARP_SIZE;
  563. int lane_id = threadIdx.x % WARP_SIZE;
  564. if (lane_id == 0) {
  565. s_sum[warp_id] = tmp;
  566. }
  567. __syncthreads();
  568. tmp = s_sum[lane_id];
  569. tmp = warp_reduce_sum(tmp);
  570. }
  571. const float mean = tmp / ncols;
  572. const float scale = rsqrtf(mean + eps);
  573. for (int col = tid; col < ncols; col += block_size) {
  574. dst[row*ncols + col] = scale * x[row*ncols + col];
  575. }
  576. }
  577. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  578. const block_q4_0 * x = (const block_q4_0 *) vx;
  579. const dfloat d = x[ib].d;
  580. const int vui = x[ib].qs[iqs];
  581. v.x = vui & 0xF;
  582. v.y = vui >> 4;
  583. #ifdef GGML_CUDA_F16
  584. v = __hsub2(v, {8.0f, 8.0f});
  585. v = __hmul2(v, {d, d});
  586. #else
  587. v.x = (v.x - 8.0f) * d;
  588. v.y = (v.y - 8.0f) * d;
  589. #endif // GGML_CUDA_F16
  590. }
  591. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  592. const block_q4_1 * x = (const block_q4_1 *) vx;
  593. const dfloat d = __low2half(x[ib].dm);
  594. const dfloat m = __high2half(x[ib].dm);
  595. const int vui = x[ib].qs[iqs];
  596. v.x = vui & 0xF;
  597. v.y = vui >> 4;
  598. #ifdef GGML_CUDA_F16
  599. v = __hmul2(v, {d, d});
  600. v = __hadd2(v, {m, m});
  601. #else
  602. v.x = (v.x * d) + m;
  603. v.y = (v.y * d) + m;
  604. #endif // GGML_CUDA_F16
  605. }
  606. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  607. const block_q5_0 * x = (const block_q5_0 *) vx;
  608. const dfloat d = x[ib].d;
  609. uint32_t qh;
  610. memcpy(&qh, x[ib].qh, sizeof(qh));
  611. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  612. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  613. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  614. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  615. #ifdef GGML_CUDA_F16
  616. v = __hsub2(v, {16.0f, 16.0f});
  617. v = __hmul2(v, {d, d});
  618. #else
  619. v.x = (v.x - 16.0f) * d;
  620. v.y = (v.y - 16.0f) * d;
  621. #endif // GGML_CUDA_F16
  622. }
  623. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  624. const block_q5_1 * x = (const block_q5_1 *) vx;
  625. const dfloat d = __low2half(x[ib].dm);
  626. const dfloat m = __high2half(x[ib].dm);
  627. uint32_t qh;
  628. memcpy(&qh, x[ib].qh, sizeof(qh));
  629. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  630. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  631. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  632. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  633. #ifdef GGML_CUDA_F16
  634. v = __hmul2(v, {d, d});
  635. v = __hadd2(v, {m, m});
  636. #else
  637. v.x = (v.x * d) + m;
  638. v.y = (v.y * d) + m;
  639. #endif // GGML_CUDA_F16
  640. }
  641. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  642. const block_q8_0 * x = (const block_q8_0 *) vx;
  643. const dfloat d = x[ib].d;
  644. v.x = x[ib].qs[iqs + 0];
  645. v.y = x[ib].qs[iqs + 1];
  646. #ifdef GGML_CUDA_F16
  647. v = __hmul2(v, {d, d});
  648. #else
  649. v.x *= d;
  650. v.y *= d;
  651. #endif // GGML_CUDA_F16
  652. }
  653. //================================== k-quants
  654. template<typename dst_t>
  655. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  656. const int i = blockIdx.x;
  657. const block_q2_K * x = (const block_q2_K *) vx;
  658. const int tid = threadIdx.x;
  659. #if QK_K == 256
  660. const int n = tid/32;
  661. const int l = tid - 32*n;
  662. const int is = 8*n + l/16;
  663. const uint8_t q = x[i].qs[32*n + l];
  664. dst_t * y = yy + i*QK_K + 128*n;
  665. float dall = __low2half(x[i].dm);
  666. float dmin = __high2half(x[i].dm);
  667. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  668. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  669. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  670. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  671. #else
  672. const int is = tid/16; // 0 or 1
  673. const int il = tid%16; // 0...15
  674. const uint8_t q = x[i].qs[il] >> (2*is);
  675. dst_t * y = yy + i*QK_K + 16*is + il;
  676. float dall = __low2half(x[i].dm);
  677. float dmin = __high2half(x[i].dm);
  678. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  679. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  680. #endif
  681. }
  682. template<typename dst_t>
  683. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  684. const int i = blockIdx.x;
  685. const block_q3_K * x = (const block_q3_K *) vx;
  686. #if QK_K == 256
  687. const int r = threadIdx.x/4;
  688. const int tid = r/2;
  689. const int is0 = r%2;
  690. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  691. const int n = tid / 4;
  692. const int j = tid - 4*n;
  693. uint8_t m = 1 << (4*n + j);
  694. int is = 8*n + 2*j + is0;
  695. int shift = 2*j;
  696. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  697. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  698. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  699. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  700. float d_all = x[i].d;
  701. float dl = d_all * (us - 32);
  702. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  703. const uint8_t * q = x[i].qs + 32*n;
  704. const uint8_t * hm = x[i].hmask;
  705. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  706. #else
  707. const int tid = threadIdx.x;
  708. const int is = tid/16; // 0 or 1
  709. const int il = tid%16; // 0...15
  710. const int im = il/8; // 0...1
  711. const int in = il%8; // 0...7
  712. dst_t * y = yy + i*QK_K + 16*is + il;
  713. const uint8_t q = x[i].qs[il] >> (2*is);
  714. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  715. const float d = (float)x[i].d;
  716. if (is == 0) {
  717. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  718. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  719. } else {
  720. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  721. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  722. }
  723. #endif
  724. }
  725. #if QK_K == 256
  726. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  727. if (j < 4) {
  728. d = q[j] & 63; m = q[j + 4] & 63;
  729. } else {
  730. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  731. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  732. }
  733. }
  734. #endif
  735. template<typename dst_t>
  736. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  737. const block_q4_K * x = (const block_q4_K *) vx;
  738. const int i = blockIdx.x;
  739. #if QK_K == 256
  740. // assume 32 threads
  741. const int tid = threadIdx.x;
  742. const int il = tid/8;
  743. const int ir = tid%8;
  744. const int is = 2*il;
  745. const int n = 4;
  746. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  747. const float dall = __low2half(x[i].dm);
  748. const float dmin = __high2half(x[i].dm);
  749. const uint8_t * q = x[i].qs + 32*il + n*ir;
  750. uint8_t sc, m;
  751. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  752. const float d1 = dall * sc; const float m1 = dmin * m;
  753. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  754. const float d2 = dall * sc; const float m2 = dmin * m;
  755. for (int l = 0; l < n; ++l) {
  756. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  757. y[l +32] = d2 * (q[l] >> 4) - m2;
  758. }
  759. #else
  760. const int tid = threadIdx.x;
  761. const uint8_t * q = x[i].qs;
  762. dst_t * y = yy + i*QK_K;
  763. const float d = (float)x[i].dm[0];
  764. const float m = (float)x[i].dm[1];
  765. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  766. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  767. #endif
  768. }
  769. template<typename dst_t>
  770. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  771. const block_q5_K * x = (const block_q5_K *) vx;
  772. const int i = blockIdx.x;
  773. #if QK_K == 256
  774. // assume 64 threads - this is very slightly better than the one below
  775. const int tid = threadIdx.x;
  776. const int il = tid/16; // il is in 0...3
  777. const int ir = tid%16; // ir is in 0...15
  778. const int is = 2*il; // is is in 0...6
  779. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  780. const float dall = __low2half(x[i].dm);
  781. const float dmin = __high2half(x[i].dm);
  782. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  783. const uint8_t * qh = x[i].qh + 2*ir;
  784. uint8_t sc, m;
  785. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  786. const float d1 = dall * sc; const float m1 = dmin * m;
  787. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  788. const float d2 = dall * sc; const float m2 = dmin * m;
  789. uint8_t hm = 1 << (2*il);
  790. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  791. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  792. hm <<= 1;
  793. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  794. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  795. #else
  796. const int tid = threadIdx.x;
  797. const uint8_t q = x[i].qs[tid];
  798. const int im = tid/8; // 0...3
  799. const int in = tid%8; // 0...7
  800. const int is = tid/16; // 0 or 1
  801. const uint8_t h = x[i].qh[in] >> im;
  802. const float d = x[i].d;
  803. dst_t * y = yy + i*QK_K + tid;
  804. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  805. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  806. #endif
  807. }
  808. template<typename dst_t>
  809. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  810. const block_q6_K * x = (const block_q6_K *) vx;
  811. const int i = blockIdx.x;
  812. #if QK_K == 256
  813. // assume 64 threads - this is very slightly better than the one below
  814. const int tid = threadIdx.x;
  815. const int ip = tid/32; // ip is 0 or 1
  816. const int il = tid - 32*ip; // 0...32
  817. const int is = 8*ip + il/16;
  818. dst_t * y = yy + i*QK_K + 128*ip + il;
  819. const float d = x[i].d;
  820. const uint8_t * ql = x[i].ql + 64*ip + il;
  821. const uint8_t qh = x[i].qh[32*ip + il];
  822. const int8_t * sc = x[i].scales + is;
  823. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  824. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  825. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  826. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  827. #else
  828. // assume 32 threads
  829. const int tid = threadIdx.x;
  830. const int ip = tid/16; // 0 or 1
  831. const int il = tid - 16*ip; // 0...15
  832. dst_t * y = yy + i*QK_K + 16*ip + il;
  833. const float d = x[i].d;
  834. const uint8_t ql = x[i].ql[16*ip + il];
  835. const uint8_t qh = x[i].qh[il] >> (2*ip);
  836. const int8_t * sc = x[i].scales;
  837. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  838. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  839. #endif
  840. }
  841. 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) {
  842. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  843. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  844. if (row > nrows) return;
  845. const int num_blocks_per_row = ncols / QK_K;
  846. const int ib0 = row*num_blocks_per_row;
  847. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  848. float tmp = 0; // partial sum for thread in warp
  849. #if QK_K == 256
  850. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  851. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  852. const int step = 16/K_QUANTS_PER_ITERATION;
  853. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  854. const int in = tid - step*im; // 0...15 or 0...7
  855. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  856. const int q_offset = 32*im + l0;
  857. const int s_offset = 8*im;
  858. const int y_offset = 128*im + l0;
  859. uint32_t aux[4];
  860. const uint8_t * d = (const uint8_t *)aux;
  861. const uint8_t * m = (const uint8_t *)(aux + 2);
  862. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  863. const float * y = yy + i * QK_K + y_offset;
  864. const uint8_t * q = x[i].qs + q_offset;
  865. const float dall = __low2half(x[i].dm);
  866. const float dmin = __high2half(x[i].dm);
  867. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  868. aux[0] = a[0] & 0x0f0f0f0f;
  869. aux[1] = a[1] & 0x0f0f0f0f;
  870. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  871. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  872. float sum1 = 0, sum2 = 0;
  873. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  874. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  875. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  876. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  877. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  878. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  879. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  880. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  881. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  882. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  883. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  884. }
  885. tmp += dall * sum1 - dmin * sum2;
  886. }
  887. #else
  888. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  889. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  890. const int offset = tid * K_QUANTS_PER_ITERATION;
  891. uint32_t uaux[2];
  892. const uint8_t * d = (const uint8_t *)uaux;
  893. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  894. const float * y = yy + i * QK_K + offset;
  895. const uint8_t * q = x[i].qs + offset;
  896. const uint32_t * s = (const uint32_t *)x[i].scales;
  897. uaux[0] = s[0] & 0x0f0f0f0f;
  898. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  899. const float2 dall = __half22float2(x[i].dm);
  900. float sum1 = 0, sum2 = 0;
  901. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  902. const uint8_t ql = q[l];
  903. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  904. + y[l+16] * d[1] * ((ql >> 2) & 3)
  905. + y[l+32] * d[2] * ((ql >> 4) & 3)
  906. + y[l+48] * d[3] * ((ql >> 6) & 3);
  907. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  908. }
  909. tmp += dall.x * sum1 - dall.y * sum2;
  910. }
  911. #endif
  912. // sum up partial sums and write back result
  913. #pragma unroll
  914. for (int mask = 16; mask > 0; mask >>= 1) {
  915. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  916. }
  917. if (threadIdx.x == 0) {
  918. dst[row] = tmp;
  919. }
  920. }
  921. 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) {
  922. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  923. if (row > nrows) return;
  924. const int num_blocks_per_row = ncols / QK_K;
  925. const int ib0 = row*num_blocks_per_row;
  926. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  927. float tmp = 0; // partial sum for thread in warp
  928. #if QK_K == 256
  929. const uint16_t kmask1 = 0x0303;
  930. const uint16_t kmask2 = 0x0f0f;
  931. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  932. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  933. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  934. const int step = 16/K_QUANTS_PER_ITERATION;
  935. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  936. const int in = tid - step*im; // 0....15 or 0...7
  937. const uint8_t m = 1 << (4*im);
  938. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  939. const int q_offset = 32*im + l0;
  940. const int y_offset = 128*im + l0;
  941. uint16_t utmp[4];
  942. const int8_t * s = (const int8_t *)utmp;
  943. const uint16_t s_shift = 4*im;
  944. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  945. const float * y = yy + i * QK_K + y_offset;
  946. const uint8_t * q = x[i].qs + q_offset;
  947. const uint8_t * h = x[i].hmask + l0;
  948. const uint16_t * a = (const uint16_t *)x[i].scales;
  949. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  950. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  951. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  952. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  953. const float d = x[i].d;
  954. float sum = 0;
  955. for (int l = 0; l < n; ++l) {
  956. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  957. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  958. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  959. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  960. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  961. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  962. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  963. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  964. }
  965. tmp += d * sum;
  966. }
  967. #else
  968. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  969. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  970. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  971. const int in = offset/8; // 0 or 1
  972. const int im = offset%8; // 0...7
  973. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  974. const float * y = yy + i * QK_K + offset;
  975. const uint8_t * q = x[i].qs + offset;
  976. const uint8_t * s = x[i].scales;
  977. const float dall = (float)x[i].d;
  978. float sum = 0;
  979. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  980. const uint8_t hl = x[i].hmask[im+l] >> in;
  981. const uint8_t ql = q[l];
  982. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  983. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  984. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  985. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  986. }
  987. tmp += sum;
  988. }
  989. #endif
  990. // sum up partial sums and write back result
  991. #pragma unroll
  992. for (int mask = 16; mask > 0; mask >>= 1) {
  993. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  994. }
  995. if (threadIdx.x == 0) {
  996. dst[row] = tmp;
  997. }
  998. }
  999. 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) {
  1000. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1001. if (row > nrows) return;
  1002. const int num_blocks_per_row = ncols / QK_K;
  1003. const int ib0 = row*num_blocks_per_row;
  1004. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1005. #if QK_K == 256
  1006. const uint16_t kmask1 = 0x3f3f;
  1007. const uint16_t kmask2 = 0x0f0f;
  1008. const uint16_t kmask3 = 0xc0c0;
  1009. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1010. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1011. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1012. const int il = tid/step; // 0...3
  1013. const int ir = tid - step*il; // 0...7 or 0...3
  1014. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1015. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1016. const int in = il%2;
  1017. const int l0 = n*(2*ir + in);
  1018. const int q_offset = 32*im + l0;
  1019. const int y_offset = 64*im + l0;
  1020. uint16_t aux[4];
  1021. const uint8_t * sc = (const uint8_t *)aux;
  1022. #if K_QUANTS_PER_ITERATION == 2
  1023. uint32_t q32[4];
  1024. const uint8_t * q4 = (const uint8_t *)q32;
  1025. #else
  1026. uint16_t q16[4];
  1027. const uint8_t * q4 = (const uint8_t *)q16;
  1028. #endif
  1029. float tmp = 0; // partial sum for thread in warp
  1030. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1031. const float * y1 = yy + i*QK_K + y_offset;
  1032. const float * y2 = y1 + 128;
  1033. const float dall = __low2half(x[i].dm);
  1034. const float dmin = __high2half(x[i].dm);
  1035. const uint16_t * a = (const uint16_t *)x[i].scales;
  1036. aux[0] = a[im+0] & kmask1;
  1037. aux[1] = a[im+2] & kmask1;
  1038. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1039. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1040. #if K_QUANTS_PER_ITERATION == 2
  1041. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1042. const uint32_t * q2 = q1 + 16;
  1043. q32[0] = q1[0] & 0x0f0f0f0f;
  1044. q32[1] = q1[0] & 0xf0f0f0f0;
  1045. q32[2] = q2[0] & 0x0f0f0f0f;
  1046. q32[3] = q2[0] & 0xf0f0f0f0;
  1047. float4 s = {0.f, 0.f, 0.f, 0.f};
  1048. float smin = 0;
  1049. for (int l = 0; l < 4; ++l) {
  1050. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1051. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1052. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1053. }
  1054. 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;
  1055. #else
  1056. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1057. const uint16_t * q2 = q1 + 32;
  1058. q16[0] = q1[0] & 0x0f0f;
  1059. q16[1] = q1[0] & 0xf0f0;
  1060. q16[2] = q2[0] & 0x0f0f;
  1061. q16[3] = q2[0] & 0xf0f0;
  1062. float4 s = {0.f, 0.f, 0.f, 0.f};
  1063. float smin = 0;
  1064. for (int l = 0; l < 2; ++l) {
  1065. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1066. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1067. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1068. }
  1069. 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;
  1070. #endif
  1071. }
  1072. #else
  1073. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1074. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1075. const int step = tid * K_QUANTS_PER_ITERATION;
  1076. uint16_t aux16[2];
  1077. const uint8_t * s = (const uint8_t *)aux16;
  1078. float tmp = 0;
  1079. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1080. const uint8_t * q = x[i].qs + step;
  1081. const float * y = yy + i*QK_K + step;
  1082. const uint16_t * a = (const uint16_t *)x[i].scales;
  1083. aux16[0] = a[0] & 0x0f0f;
  1084. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1085. const float d = (float)x[i].dm[0];
  1086. const float m = (float)x[i].dm[1];
  1087. float sum = 0.f;
  1088. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1089. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1090. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1091. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1092. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1093. }
  1094. tmp += sum;
  1095. }
  1096. #endif
  1097. // sum up partial sums and write back result
  1098. #pragma unroll
  1099. for (int mask = 16; mask > 0; mask >>= 1) {
  1100. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1101. }
  1102. if (tid == 0) {
  1103. dst[row] = tmp;
  1104. }
  1105. }
  1106. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1107. const int row = blockIdx.x;
  1108. const int num_blocks_per_row = ncols / QK_K;
  1109. const int ib0 = row*num_blocks_per_row;
  1110. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1111. float tmp = 0; // partial sum for thread in warp
  1112. #if QK_K == 256
  1113. const uint16_t kmask1 = 0x3f3f;
  1114. const uint16_t kmask2 = 0x0f0f;
  1115. const uint16_t kmask3 = 0xc0c0;
  1116. const int tid = threadIdx.x/2; // 0...15
  1117. const int ix = threadIdx.x%2;
  1118. const int il = tid/4; // 0...3
  1119. const int ir = tid - 4*il;// 0...3
  1120. const int n = 2;
  1121. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1122. const int in = il%2;
  1123. const int l0 = n*(2*ir + in);
  1124. const int q_offset = 32*im + l0;
  1125. const int y_offset = 64*im + l0;
  1126. const uint8_t hm1 = 1 << (2*im);
  1127. const uint8_t hm2 = hm1 << 4;
  1128. uint16_t aux[4];
  1129. const uint8_t * sc = (const uint8_t *)aux;
  1130. uint16_t q16[8];
  1131. const uint8_t * q4 = (const uint8_t *)q16;
  1132. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1133. const uint8_t * ql1 = x[i].qs + q_offset;
  1134. const uint8_t * qh = x[i].qh + l0;
  1135. const float * y1 = yy + i*QK_K + y_offset;
  1136. const float * y2 = y1 + 128;
  1137. const float dall = __low2half(x[i].dm);
  1138. const float dmin = __high2half(x[i].dm);
  1139. const uint16_t * a = (const uint16_t *)x[i].scales;
  1140. aux[0] = a[im+0] & kmask1;
  1141. aux[1] = a[im+2] & kmask1;
  1142. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1143. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1144. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1145. float smin = 0;
  1146. const uint16_t * q1 = (const uint16_t *)ql1;
  1147. const uint16_t * q2 = q1 + 32;
  1148. q16[0] = q1[0] & 0x0f0f;
  1149. q16[1] = q1[8] & 0x0f0f;
  1150. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1151. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1152. q16[4] = q2[0] & 0x0f0f;
  1153. q16[5] = q2[8] & 0x0f0f;
  1154. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1155. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1156. for (int l = 0; l < n; ++l) {
  1157. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1158. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1159. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1160. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1161. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1162. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1163. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1164. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1165. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1166. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1167. }
  1168. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1169. }
  1170. #else
  1171. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1172. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1173. const int step = tid * K_QUANTS_PER_ITERATION;
  1174. const int im = step/8;
  1175. const int in = step%8;
  1176. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1177. const uint8_t * q = x[i].qs + step;
  1178. const int8_t * s = x[i].scales;
  1179. const float * y = yy + i*QK_K + step;
  1180. const float d = x[i].d;
  1181. float sum = 0.f;
  1182. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1183. const uint8_t h = x[i].qh[in+j] >> im;
  1184. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1185. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1186. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1187. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1188. }
  1189. tmp += sum;
  1190. }
  1191. #endif
  1192. // sum up partial sums and write back result
  1193. #pragma unroll
  1194. for (int mask = 16; mask > 0; mask >>= 1) {
  1195. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1196. }
  1197. if (threadIdx.x == 0) {
  1198. dst[row] = tmp;
  1199. }
  1200. }
  1201. 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) {
  1202. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1203. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1204. if (row > nrows) return;
  1205. const int num_blocks_per_row = ncols / QK_K;
  1206. const int ib0 = row*num_blocks_per_row;
  1207. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1208. #if QK_K == 256
  1209. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1210. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1211. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1212. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1213. const int in = tid - step*im; // 0...15 or 0...7
  1214. #if K_QUANTS_PER_ITERATION == 1
  1215. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1216. const int is = 0;
  1217. #else
  1218. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1219. const int is = in / 4;
  1220. #endif
  1221. const int ql_offset = 64*im + l0;
  1222. const int qh_offset = 32*im + l0;
  1223. const int s_offset = 8*im + is;
  1224. const int y_offset = 128*im + l0;
  1225. float tmp = 0; // partial sum for thread in warp
  1226. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1227. const float * y = yy + i * QK_K + y_offset;
  1228. const uint8_t * ql = x[i].ql + ql_offset;
  1229. const uint8_t * qh = x[i].qh + qh_offset;
  1230. const int8_t * s = x[i].scales + s_offset;
  1231. const float d = x[i].d;
  1232. #if K_QUANTS_PER_ITERATION == 1
  1233. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1234. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1235. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1236. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1237. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1238. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1239. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1240. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1241. tmp += sum;
  1242. #else
  1243. float sum = 0;
  1244. for (int l = 0; l < 4; ++l) {
  1245. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1246. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1247. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1248. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1249. }
  1250. tmp += sum;
  1251. #endif
  1252. }
  1253. #else
  1254. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1255. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1256. const int step = tid * K_QUANTS_PER_ITERATION;
  1257. float tmp = 0; // partial sum for thread in warp
  1258. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1259. const float * y = yy + i * QK_K + step;
  1260. const uint8_t * ql = x[i].ql + step;
  1261. const uint8_t * qh = x[i].qh + step;
  1262. const int8_t * s = x[i].scales;
  1263. const float d = x[i+0].d;
  1264. float sum = 0;
  1265. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1266. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1267. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1268. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1269. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1270. }
  1271. tmp += sum;
  1272. }
  1273. #endif
  1274. // sum up partial sums and write back result
  1275. #pragma unroll
  1276. for (int mask = 16; mask > 0; mask >>= 1) {
  1277. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1278. }
  1279. if (tid == 0) {
  1280. dst[row] = tmp;
  1281. }
  1282. }
  1283. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1284. const half * x = (const half *) vx;
  1285. // automatic half -> float type cast if dfloat == float
  1286. v.x = x[ib + iqs + 0];
  1287. v.y = x[ib + iqs + 1];
  1288. }
  1289. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1290. const float * x = (const float *) vx;
  1291. // automatic half -> float type cast if dfloat == float
  1292. v.x = x[ib + iqs + 0];
  1293. v.y = x[ib + iqs + 1];
  1294. }
  1295. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1296. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1297. if (ix >= kx_padded) {
  1298. return;
  1299. }
  1300. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1301. const int i_padded = iy*kx_padded + ix;
  1302. block_q8_1 * y = (block_q8_1 *) vy;
  1303. const int ib = i_padded / QK8_1; // block index
  1304. const int iqs = i_padded % QK8_1; // quant index
  1305. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1306. float amax = fabsf(xi);
  1307. float sum = xi;
  1308. #pragma unroll
  1309. for (int mask = 16; mask > 0; mask >>= 1) {
  1310. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1311. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1312. }
  1313. const float d = amax / 127;
  1314. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1315. y[ib].qs[iqs] = q;
  1316. if (iqs > 0) {
  1317. return;
  1318. }
  1319. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1320. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1321. }
  1322. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1323. static __global__ void k_get_rows(const void * x, const int32_t * y, dst_t * dst, const int ncols) {
  1324. const int col = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1325. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  1326. if (col >= ncols) {
  1327. return;
  1328. }
  1329. const int r = y[row];
  1330. // copy x[r*ncols + col] to dst[row*ncols + col]
  1331. const int xi = r*ncols + col;
  1332. const int di = row*ncols + col;
  1333. const int ib = xi/qk; // block index
  1334. const int iqs = (xi%qk)/qr; // quant index
  1335. const int iybs = di - di%qk; // y block start index
  1336. const int y_offset = qr == 1 ? 1 : qk/2;
  1337. // dequantize
  1338. dfloat2 v;
  1339. dequantize_kernel(x, ib, iqs, v);
  1340. dst[iybs + iqs + 0] = v.x;
  1341. dst[iybs + iqs + y_offset] = v.y;
  1342. }
  1343. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1344. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1345. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1346. if (i >= k) {
  1347. return;
  1348. }
  1349. const int ib = i/qk; // block index
  1350. const int iqs = (i%qk)/qr; // quant index
  1351. const int iybs = i - i%qk; // y block start index
  1352. const int y_offset = qr == 1 ? 1 : qk/2;
  1353. // dequantize
  1354. dfloat2 v;
  1355. dequantize_kernel(vx, ib, iqs, v);
  1356. y[iybs + iqs + 0] = v.x;
  1357. y[iybs + iqs + y_offset] = v.y;
  1358. }
  1359. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1360. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1361. #define VDR_Q4_0_Q8_1_MMVQ 2
  1362. #define VDR_Q4_0_Q8_1_MMQ 4
  1363. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1364. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1365. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1366. int sumi = 0;
  1367. #pragma unroll
  1368. for (int i = 0; i < vdr; ++i) {
  1369. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1370. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1371. // SIMD dot product of quantized values
  1372. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1373. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1374. }
  1375. const float2 ds8f = __half22float2(ds8);
  1376. // second part effectively subtracts 8 from each quant value
  1377. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1378. #else
  1379. assert(false);
  1380. return 0.0f; // only to satisfy the compiler
  1381. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1382. }
  1383. #define VDR_Q4_1_Q8_1_MMVQ 2
  1384. #define VDR_Q4_1_Q8_1_MMQ 4
  1385. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1386. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1387. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1388. int sumi = 0;
  1389. #pragma unroll
  1390. for (int i = 0; i < vdr; ++i) {
  1391. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1392. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1393. // SIMD dot product of quantized values
  1394. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1395. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1396. }
  1397. #ifdef GGML_CUDA_F16
  1398. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1399. const float d4d8 = tmp.x;
  1400. const float m4s8 = tmp.y;
  1401. #else
  1402. const float2 dm4f = __half22float2(dm4);
  1403. const float2 ds8f = __half22float2(ds8);
  1404. const float d4d8 = dm4f.x * ds8f.x;
  1405. const float m4s8 = dm4f.y * ds8f.y;
  1406. #endif // GGML_CUDA_F16
  1407. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1408. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1409. #else
  1410. assert(false);
  1411. return 0.0f; // only to satisfy the compiler
  1412. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1413. }
  1414. #define VDR_Q5_0_Q8_1_MMVQ 2
  1415. #define VDR_Q5_0_Q8_1_MMQ 4
  1416. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1417. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1418. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1419. int sumi = 0;
  1420. #pragma unroll
  1421. for (int i = 0; i < vdr; ++i) {
  1422. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1423. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1424. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1425. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1426. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1427. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1428. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1429. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1430. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1431. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1432. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1433. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1434. }
  1435. const float2 ds8f = __half22float2(ds8);
  1436. // second part effectively subtracts 16 from each quant value
  1437. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1438. #else
  1439. assert(false);
  1440. return 0.0f; // only to satisfy the compiler
  1441. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1442. }
  1443. #define VDR_Q5_1_Q8_1_MMVQ 2
  1444. #define VDR_Q5_1_Q8_1_MMQ 4
  1445. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1446. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1447. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1448. int sumi = 0;
  1449. #pragma unroll
  1450. for (int i = 0; i < vdr; ++i) {
  1451. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1452. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1453. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1454. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1455. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1456. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1457. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1458. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1459. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1460. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1461. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1462. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1463. }
  1464. #ifdef GGML_CUDA_F16
  1465. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1466. const float d5d8 = tmp.x;
  1467. const float m5s8 = tmp.y;
  1468. #else
  1469. const float2 dm5f = __half22float2(dm5);
  1470. const float2 ds8f = __half22float2(ds8);
  1471. const float d5d8 = dm5f.x * ds8f.x;
  1472. const float m5s8 = dm5f.y * ds8f.y;
  1473. #endif // GGML_CUDA_F16
  1474. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1475. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1476. #else
  1477. assert(false);
  1478. return 0.0f; // only to satisfy the compiler
  1479. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1480. }
  1481. #define VDR_Q8_0_Q8_1_MMVQ 2
  1482. #define VDR_Q8_0_Q8_1_MMQ 8
  1483. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1484. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1485. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1486. int sumi = 0;
  1487. #pragma unroll
  1488. for (int i = 0; i < vdr; ++i) {
  1489. // SIMD dot product of quantized values
  1490. sumi = __dp4a(v[i], u[i], sumi);
  1491. }
  1492. return d8_0*d8_1 * sumi;
  1493. #else
  1494. assert(false);
  1495. return 0.0f; // only to satisfy the compiler
  1496. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1497. }
  1498. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1499. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1500. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1501. int sumi = 0;
  1502. #pragma unroll
  1503. for (int i = 0; i < vdr; ++i) {
  1504. // SIMD dot product of quantized values
  1505. sumi = __dp4a(v[i], u[i], sumi);
  1506. }
  1507. #ifdef GGML_CUDA_F16
  1508. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1509. const float d8d8 = tmp.x;
  1510. const float m8s8 = tmp.y;
  1511. #else
  1512. const float2 dm8f = __half22float2(dm8);
  1513. const float2 ds8f = __half22float2(ds8);
  1514. const float d8d8 = dm8f.x * ds8f.x;
  1515. const float m8s8 = dm8f.y * ds8f.y;
  1516. #endif // GGML_CUDA_F16
  1517. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1518. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1519. #else
  1520. assert(false);
  1521. return 0.0f; // only to satisfy the compiler
  1522. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1523. }
  1524. #define VDR_Q2_K_Q8_1_MMVQ 1
  1525. #define VDR_Q2_K_Q8_1_MMQ 2
  1526. // contiguous v/x values
  1527. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1528. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1529. const half2 & dm2, const float * __restrict__ d8) {
  1530. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1531. float sumf_d = 0.0f;
  1532. float sumf_m = 0.0f;
  1533. #pragma unroll
  1534. for (int i = 0; i < QR2_K; ++i) {
  1535. const int sc = scales[2*i];
  1536. const int vi = (v >> (2*i)) & 0x03030303;
  1537. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1538. // fill int with 4x m
  1539. int m = sc >> 4;
  1540. m |= m << 8;
  1541. m |= m << 16;
  1542. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1543. }
  1544. const float2 dm2f = __half22float2(dm2);
  1545. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1546. #else
  1547. assert(false);
  1548. return 0.0f; // only to satisfy the compiler
  1549. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1550. }
  1551. // contiguous u/y values
  1552. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1553. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1554. const half2 & dm2, const float & d8) {
  1555. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1556. int sumi_d = 0;
  1557. int sumi_m = 0;
  1558. #pragma unroll
  1559. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1560. int sumi_d_sc = 0;
  1561. const int sc = scales[i0 / (QI8_1/2)];
  1562. // fill int with 4x m
  1563. int m = sc >> 4;
  1564. m |= m << 8;
  1565. m |= m << 16;
  1566. #pragma unroll
  1567. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1568. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1569. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1570. }
  1571. sumi_d += sumi_d_sc * (sc & 0xF);
  1572. }
  1573. const float2 dm2f = __half22float2(dm2);
  1574. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1575. #else
  1576. assert(false);
  1577. return 0.0f; // only to satisfy the compiler
  1578. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1579. }
  1580. #define VDR_Q3_K_Q8_1_MMVQ 1
  1581. #define VDR_Q3_K_Q8_1_MMQ 2
  1582. // contiguous v/x values
  1583. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1584. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1585. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1586. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1587. float sumf = 0.0f;
  1588. #pragma unroll
  1589. for (int i = 0; i < QR3_K; ++i) {
  1590. const int isc = scale_offset + 2*i;
  1591. const int isc_low = isc % (QK_K/32);
  1592. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1593. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1594. const int isc_high = isc % (QK_K/64);
  1595. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1596. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1597. const int sc = (sc_low | sc_high) - 32;
  1598. const int vil = (vl >> (2*i)) & 0x03030303;
  1599. const int vih = ((vh >> i) << 2) & 0x04040404;
  1600. const int vi = __vsubss4(vil, vih);
  1601. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1602. }
  1603. return d3 * sumf;
  1604. #else
  1605. assert(false);
  1606. return 0.0f; // only to satisfy the compiler
  1607. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1608. }
  1609. // contiguous u/y values
  1610. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1611. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1612. const float & d3, const float & d8) {
  1613. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1614. int sumi = 0;
  1615. #pragma unroll
  1616. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1617. int sumi_sc = 0;
  1618. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1619. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1620. }
  1621. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1622. }
  1623. return d3*d8 * sumi;
  1624. #else
  1625. assert(false);
  1626. return 0.0f; // only to satisfy the compiler
  1627. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1628. }
  1629. #define VDR_Q4_K_Q8_1_MMVQ 2
  1630. #define VDR_Q4_K_Q8_1_MMQ 8
  1631. // contiguous v/x values
  1632. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1633. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1634. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1635. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1636. float sumf_d = 0.0f;
  1637. float sumf_m = 0.0f;
  1638. #pragma unroll
  1639. for (int i = 0; i < QR4_K; ++i) {
  1640. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1641. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1642. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1643. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1644. sumf_d += d8[i] * (dot1 * sc[i]);
  1645. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1646. }
  1647. const float2 dm4f = __half22float2(dm4);
  1648. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1649. #else
  1650. assert(false);
  1651. return 0.0f; // only to satisfy the compiler
  1652. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1653. }
  1654. // contiguous u/y values
  1655. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1656. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1657. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1658. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1659. float sumf_d = 0.0f;
  1660. float sumf_m = 0.0f;
  1661. #pragma unroll
  1662. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1663. int sumi_d = 0;
  1664. #pragma unroll
  1665. for (int j = 0; j < QI8_1; ++j) {
  1666. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1667. }
  1668. const float2 ds8f = __half22float2(ds8[i]);
  1669. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1670. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1671. }
  1672. const float2 dm4f = __half22float2(dm4);
  1673. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1674. #else
  1675. assert(false);
  1676. return 0.0f; // only to satisfy the compiler
  1677. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1678. }
  1679. #define VDR_Q5_K_Q8_1_MMVQ 2
  1680. #define VDR_Q5_K_Q8_1_MMQ 8
  1681. // contiguous v/x values
  1682. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1683. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1684. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1685. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1686. float sumf_d = 0.0f;
  1687. float sumf_m = 0.0f;
  1688. #pragma unroll
  1689. for (int i = 0; i < QR5_K; ++i) {
  1690. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1691. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1692. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1693. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1694. const int v0i = vl0i | vh0i;
  1695. const int v1i = vl1i | vh1i;
  1696. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1697. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1698. sumf_d += d8[i] * (dot1 * sc[i]);
  1699. sumf_m += d8[i] * (dot2 * m[i]);
  1700. }
  1701. const float2 dm5f = __half22float2(dm5);
  1702. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1703. #else
  1704. assert(false);
  1705. return 0.0f; // only to satisfy the compiler
  1706. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1707. }
  1708. // contiguous u/y values
  1709. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1710. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1711. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1712. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1713. float sumf_d = 0.0f;
  1714. float sumf_m = 0.0f;
  1715. #pragma unroll
  1716. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1717. int sumi_d = 0;
  1718. #pragma unroll
  1719. for (int j = 0; j < QI8_1; ++j) {
  1720. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1721. }
  1722. const float2 ds8f = __half22float2(ds8[i]);
  1723. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1724. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1725. }
  1726. const float2 dm4f = __half22float2(dm4);
  1727. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1728. #else
  1729. assert(false);
  1730. return 0.0f; // only to satisfy the compiler
  1731. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1732. }
  1733. #define VDR_Q6_K_Q8_1_MMVQ 1
  1734. #define VDR_Q6_K_Q8_1_MMQ 8
  1735. // contiguous v/x values
  1736. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1737. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1738. const float & d, const float * __restrict__ d8) {
  1739. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1740. float sumf = 0.0f;
  1741. #pragma unroll
  1742. for (int i = 0; i < QR6_K; ++i) {
  1743. const int sc = scales[4*i];
  1744. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1745. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1746. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1747. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1748. }
  1749. return d*sumf;
  1750. #else
  1751. assert(false);
  1752. return 0.0f; // only to satisfy the compiler
  1753. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1754. }
  1755. // contiguous u/y values
  1756. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  1757. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  1758. const float & d6, const float * __restrict__ d8) {
  1759. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1760. float sumf_d = 0.0f;
  1761. #pragma unroll
  1762. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  1763. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  1764. #pragma unroll
  1765. for (int i = i0; i < i0 + 2; ++i) {
  1766. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  1767. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  1768. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  1769. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  1770. }
  1771. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  1772. }
  1773. return d6 * sumf_d;
  1774. #else
  1775. assert(false);
  1776. return 0.0f; // only to satisfy the compiler
  1777. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1778. }
  1779. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  1780. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1781. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  1782. int v[VDR_Q4_0_Q8_1_MMVQ];
  1783. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  1784. #pragma unroll
  1785. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  1786. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  1787. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1788. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  1789. }
  1790. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  1791. }
  1792. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1793. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  1794. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  1795. *x_ql = tile_x_qs;
  1796. *x_dm = (half2 *) tile_x_d;
  1797. }
  1798. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  1799. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1800. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1801. GGML_CUDA_ASSUME(i_offset >= 0);
  1802. GGML_CUDA_ASSUME(i_offset < nwarps);
  1803. GGML_CUDA_ASSUME(k >= 0);
  1804. GGML_CUDA_ASSUME(k < WARP_SIZE);
  1805. const int kbx = k / QI4_0;
  1806. const int kqsx = k % QI4_0;
  1807. const block_q4_0 * bx0 = (block_q4_0 *) vx;
  1808. float * x_dmf = (float *) x_dm;
  1809. #pragma unroll
  1810. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1811. int i = i0 + i_offset;
  1812. if (need_check) {
  1813. i = min(i, i_max);
  1814. }
  1815. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1816. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  1817. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  1818. }
  1819. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  1820. const int kbxd = k % blocks_per_tile_x_row;
  1821. #pragma unroll
  1822. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  1823. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  1824. if (need_check) {
  1825. i = min(i, i_max);
  1826. }
  1827. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1828. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  1829. }
  1830. }
  1831. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  1832. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1833. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1834. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1835. const float * x_dmf = (float *) x_dm;
  1836. int u[2*VDR_Q4_0_Q8_1_MMQ];
  1837. #pragma unroll
  1838. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  1839. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1840. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  1841. }
  1842. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  1843. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  1844. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1845. }
  1846. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  1847. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1848. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  1849. int v[VDR_Q4_1_Q8_1_MMVQ];
  1850. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  1851. #pragma unroll
  1852. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  1853. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  1854. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1855. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  1856. }
  1857. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  1858. }
  1859. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1860. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  1861. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  1862. *x_ql = tile_x_qs;
  1863. *x_dm = tile_x_dm;
  1864. }
  1865. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  1866. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1867. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1868. GGML_CUDA_ASSUME(i_offset >= 0);
  1869. GGML_CUDA_ASSUME(i_offset < nwarps);
  1870. GGML_CUDA_ASSUME(k >= 0);
  1871. GGML_CUDA_ASSUME(k < WARP_SIZE);
  1872. const int kbx = k / QI4_1;
  1873. const int kqsx = k % QI4_1;
  1874. const block_q4_1 * bx0 = (block_q4_1 *) vx;
  1875. #pragma unroll
  1876. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1877. int i = i0 + i_offset;
  1878. if (need_check) {
  1879. i = min(i, i_max);
  1880. }
  1881. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  1882. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1883. }
  1884. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  1885. const int kbxd = k % blocks_per_tile_x_row;
  1886. #pragma unroll
  1887. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  1888. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  1889. if (need_check) {
  1890. i = min(i, i_max);
  1891. }
  1892. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  1893. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  1894. }
  1895. }
  1896. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  1897. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1898. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1899. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1900. int u[2*VDR_Q4_1_Q8_1_MMQ];
  1901. #pragma unroll
  1902. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  1903. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1904. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  1905. }
  1906. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  1907. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  1908. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1909. }
  1910. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  1911. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1912. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  1913. int vl[VDR_Q5_0_Q8_1_MMVQ];
  1914. int vh[VDR_Q5_0_Q8_1_MMVQ];
  1915. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  1916. #pragma unroll
  1917. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  1918. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  1919. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  1920. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1921. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  1922. }
  1923. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  1924. }
  1925. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1926. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  1927. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  1928. *x_ql = tile_x_ql;
  1929. *x_dm = (half2 *) tile_x_d;
  1930. }
  1931. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  1932. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1933. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1934. GGML_CUDA_ASSUME(i_offset >= 0);
  1935. GGML_CUDA_ASSUME(i_offset < nwarps);
  1936. GGML_CUDA_ASSUME(k >= 0);
  1937. GGML_CUDA_ASSUME(k < WARP_SIZE);
  1938. const int kbx = k / QI5_0;
  1939. const int kqsx = k % QI5_0;
  1940. const block_q5_0 * bx0 = (block_q5_0 *) vx;
  1941. #pragma unroll
  1942. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1943. int i = i0 + i_offset;
  1944. if (need_check) {
  1945. i = min(i, i_max);
  1946. }
  1947. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1948. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  1949. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  1950. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  1951. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  1952. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  1953. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  1954. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  1955. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  1956. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  1957. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  1958. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  1959. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  1960. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  1961. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  1962. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  1963. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  1964. }
  1965. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  1966. const int kbxd = k % blocks_per_tile_x_row;
  1967. float * x_dmf = (float *) x_dm;
  1968. #pragma unroll
  1969. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  1970. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  1971. if (need_check) {
  1972. i = min(i, i_max);
  1973. }
  1974. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1975. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  1976. }
  1977. }
  1978. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  1979. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1980. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1981. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1982. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  1983. const float * x_dmf = (const float *) x_dm;
  1984. const float * y_df = (const float *) y_ds;
  1985. int u[2*VDR_Q5_0_Q8_1_MMQ];
  1986. #pragma unroll
  1987. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  1988. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1989. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  1990. }
  1991. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  1992. (&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)]);
  1993. }
  1994. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  1995. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1996. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  1997. int vl[VDR_Q5_1_Q8_1_MMVQ];
  1998. int vh[VDR_Q5_1_Q8_1_MMVQ];
  1999. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2000. #pragma unroll
  2001. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2002. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2003. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2004. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2005. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2006. }
  2007. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2008. }
  2009. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2010. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2011. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2012. *x_ql = tile_x_ql;
  2013. *x_dm = tile_x_dm;
  2014. }
  2015. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2016. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2017. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2018. GGML_CUDA_ASSUME(i_offset >= 0);
  2019. GGML_CUDA_ASSUME(i_offset < nwarps);
  2020. GGML_CUDA_ASSUME(k >= 0);
  2021. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2022. const int kbx = k / QI5_1;
  2023. const int kqsx = k % QI5_1;
  2024. const block_q5_1 * bx0 = (block_q5_1 *) vx;
  2025. #pragma unroll
  2026. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2027. int i = i0 + i_offset;
  2028. if (need_check) {
  2029. i = min(i, i_max);
  2030. }
  2031. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2032. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2033. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2034. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2035. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2036. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2037. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2038. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2039. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2040. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2041. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2042. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2043. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2044. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2045. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2046. }
  2047. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2048. const int kbxd = k % blocks_per_tile_x_row;
  2049. #pragma unroll
  2050. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2051. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2052. if (need_check) {
  2053. i = min(i, i_max);
  2054. }
  2055. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2056. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2057. }
  2058. }
  2059. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2060. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2061. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2062. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2063. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2064. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2065. #pragma unroll
  2066. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2067. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2068. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2069. }
  2070. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2071. (&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)]);
  2072. }
  2073. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2074. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2075. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2076. int v[VDR_Q8_0_Q8_1_MMVQ];
  2077. int u[VDR_Q8_0_Q8_1_MMVQ];
  2078. #pragma unroll
  2079. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2080. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2081. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2082. }
  2083. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2084. }
  2085. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2086. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2087. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2088. *x_ql = tile_x_qs;
  2089. *x_dm = (half2 *) tile_x_d;
  2090. }
  2091. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2092. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2093. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2094. GGML_CUDA_ASSUME(i_offset >= 0);
  2095. GGML_CUDA_ASSUME(i_offset < nwarps);
  2096. GGML_CUDA_ASSUME(k >= 0);
  2097. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2098. const int kbx = k / QI8_0;
  2099. const int kqsx = k % QI8_0;
  2100. float * x_dmf = (float *) x_dm;
  2101. const block_q8_0 * bx0 = (block_q8_0 *) vx;
  2102. #pragma unroll
  2103. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2104. int i = i0 + i_offset;
  2105. if (need_check) {
  2106. i = min(i, i_max);
  2107. }
  2108. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2109. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2110. }
  2111. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2112. const int kbxd = k % blocks_per_tile_x_row;
  2113. #pragma unroll
  2114. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2115. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2116. if (need_check) {
  2117. i = min(i, i_max);
  2118. }
  2119. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2120. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2121. }
  2122. }
  2123. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2124. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2125. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2126. const float * x_dmf = (const float *) x_dm;
  2127. const float * y_df = (const float *) y_ds;
  2128. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2129. (&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],
  2130. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2131. }
  2132. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2133. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2134. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2135. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2136. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2137. const uint8_t * scales = bq2_K->scales + scale_offset;
  2138. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2139. int u[QR2_K];
  2140. float d8[QR2_K];
  2141. #pragma unroll
  2142. for (int i = 0; i < QR2_K; ++ i) {
  2143. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2144. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2145. }
  2146. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2147. }
  2148. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2149. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2150. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2151. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2152. *x_ql = tile_x_ql;
  2153. *x_dm = tile_x_dm;
  2154. *x_sc = tile_x_sc;
  2155. }
  2156. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2157. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2158. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2159. GGML_CUDA_ASSUME(i_offset >= 0);
  2160. GGML_CUDA_ASSUME(i_offset < nwarps);
  2161. GGML_CUDA_ASSUME(k >= 0);
  2162. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2163. const int kbx = k / QI2_K;
  2164. const int kqsx = k % QI2_K;
  2165. const block_q2_K * bx0 = (block_q2_K *) vx;
  2166. #pragma unroll
  2167. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2168. int i = i0 + i_offset;
  2169. if (need_check) {
  2170. i = min(i, i_max);
  2171. }
  2172. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2173. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2174. }
  2175. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2176. const int kbxd = k % blocks_per_tile_x_row;
  2177. #pragma unroll
  2178. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2179. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2180. if (need_check) {
  2181. i = min(i, i_max);
  2182. }
  2183. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2184. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2185. }
  2186. #pragma unroll
  2187. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2188. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2189. if (need_check) {
  2190. i = min(i, i_max);
  2191. }
  2192. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2193. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2194. }
  2195. }
  2196. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2197. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2198. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2199. const int kbx = k / QI2_K;
  2200. const int ky = (k % QI2_K) * QR2_K;
  2201. const float * y_df = (const float *) y_ds;
  2202. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2203. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2204. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2205. #pragma unroll
  2206. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2207. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2208. }
  2209. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2210. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2211. 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]);
  2212. }
  2213. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2214. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2215. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2216. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2217. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2218. const float d = bq3_K->d;
  2219. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2220. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2221. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2222. int u[QR3_K];
  2223. float d8[QR3_K];
  2224. #pragma unroll
  2225. for (int i = 0; i < QR3_K; ++i) {
  2226. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2227. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2228. }
  2229. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2230. }
  2231. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2232. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2233. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2234. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2235. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2236. *x_ql = tile_x_ql;
  2237. *x_dm = tile_x_dm;
  2238. *x_qh = tile_x_qh;
  2239. *x_sc = tile_x_sc;
  2240. }
  2241. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2242. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2243. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2244. GGML_CUDA_ASSUME(i_offset >= 0);
  2245. GGML_CUDA_ASSUME(i_offset < nwarps);
  2246. GGML_CUDA_ASSUME(k >= 0);
  2247. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2248. const int kbx = k / QI3_K;
  2249. const int kqsx = k % QI3_K;
  2250. const block_q3_K * bx0 = (block_q3_K *) vx;
  2251. #pragma unroll
  2252. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2253. int i = i0 + i_offset;
  2254. if (need_check) {
  2255. i = min(i, i_max);
  2256. }
  2257. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2258. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2259. }
  2260. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2261. const int kbxd = k % blocks_per_tile_x_row;
  2262. float * x_dmf = (float *) x_dm;
  2263. #pragma unroll
  2264. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2265. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2266. if (need_check) {
  2267. i = min(i, i_max);
  2268. }
  2269. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2270. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2271. }
  2272. #pragma unroll
  2273. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2274. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2275. if (need_check) {
  2276. i = min(i, i_max);
  2277. }
  2278. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2279. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2280. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2281. }
  2282. #pragma unroll
  2283. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2284. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2285. if (need_check) {
  2286. i = min(i, i_max);
  2287. }
  2288. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2289. const int ksc = k % (QI3_K/4);
  2290. const int ksc_low = ksc % (QI3_K/8);
  2291. const int shift_low = 4 * (ksc / (QI3_K/8));
  2292. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2293. const int ksc_high = QI3_K/8;
  2294. const int shift_high = 2 * ksc;
  2295. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2296. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2297. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2298. }
  2299. }
  2300. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2301. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2302. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2303. const int kbx = k / QI3_K;
  2304. const int ky = (k % QI3_K) * QR3_K;
  2305. const float * x_dmf = (const float *) x_dm;
  2306. const float * y_df = (const float *) y_ds;
  2307. const int8_t * scales = ((int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2308. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2309. #pragma unroll
  2310. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2311. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2312. const int shift = 2 * ((ky % 32) / 8);
  2313. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2314. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2315. const int vlh = (vh << 2) & 0x04040404;
  2316. v[l] = __vsubss4(vll, vlh);
  2317. }
  2318. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2319. 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]);
  2320. }
  2321. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2322. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2323. #ifndef GGML_QKK_64
  2324. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2325. int v[2];
  2326. int u[2*QR4_K];
  2327. float d8[QR4_K];
  2328. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2329. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2330. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2331. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2332. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2333. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2334. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2335. v[0] = q4[0];
  2336. v[1] = q4[4];
  2337. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2338. uint16_t aux[2];
  2339. const int j = bq8_offset/2;
  2340. if (j < 2) {
  2341. aux[0] = scales[j+0] & 0x3f3f;
  2342. aux[1] = scales[j+2] & 0x3f3f;
  2343. } else {
  2344. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2345. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2346. }
  2347. const uint8_t * sc = (const uint8_t *)aux;
  2348. const uint8_t * m = sc + 2;
  2349. for (int i = 0; i < QR4_K; ++i) {
  2350. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2351. d8[i] = __low2half(bq8i->ds);
  2352. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2353. u[2*i+0] = q8[0];
  2354. u[2*i+1] = q8[4];
  2355. }
  2356. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2357. #else
  2358. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2359. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2360. float sumf_d = 0.0f;
  2361. float sumf_m = 0.0f;
  2362. uint16_t aux16[2];
  2363. const uint8_t * s = (const uint8_t *)aux16;
  2364. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2365. aux16[0] = a[0] & 0x0f0f;
  2366. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2367. const float dall = bq4_K->dm[0];
  2368. const float dmin = bq4_K->dm[1];
  2369. const float d8_1 = __low2float(bq8_1[0].ds);
  2370. const float d8_2 = __low2float(bq8_1[1].ds);
  2371. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2372. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2373. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2374. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2375. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2376. const int v1 = q4[0];
  2377. const int v2 = q4[4];
  2378. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2379. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2380. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2381. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2382. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2383. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2384. return dall * sumf_d - dmin * sumf_m;
  2385. #else
  2386. assert(false);
  2387. return 0.0f; // only to satisfy the compiler
  2388. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2389. #endif
  2390. }
  2391. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2392. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2393. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2394. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2395. *x_ql = tile_x_ql;
  2396. *x_dm = tile_x_dm;
  2397. *x_sc = tile_x_sc;
  2398. }
  2399. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2400. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2401. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2402. GGML_CUDA_ASSUME(i_offset >= 0);
  2403. GGML_CUDA_ASSUME(i_offset < nwarps);
  2404. GGML_CUDA_ASSUME(k >= 0);
  2405. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2406. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2407. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2408. const block_q4_K * bx0 = (block_q4_K *) vx;
  2409. #pragma unroll
  2410. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2411. int i = i0 + i_offset;
  2412. if (need_check) {
  2413. i = min(i, i_max);
  2414. }
  2415. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2416. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2417. }
  2418. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2419. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2420. #pragma unroll
  2421. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2422. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2423. if (need_check) {
  2424. i = min(i, i_max);
  2425. }
  2426. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2427. #if QK_K == 256
  2428. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2429. #else
  2430. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2431. #endif
  2432. }
  2433. #pragma unroll
  2434. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2435. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2436. if (need_check) {
  2437. i = min(i, i_max);
  2438. }
  2439. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2440. const int * scales = (int *) bxi->scales;
  2441. const int ksc = k % (WARP_SIZE/8);
  2442. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2443. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2444. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2445. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2446. }
  2447. }
  2448. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2449. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2450. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2451. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2452. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2453. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2454. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2455. }
  2456. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2457. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2458. #ifndef GGML_QKK_64
  2459. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2460. int vl[2];
  2461. int vh[2];
  2462. int u[2*QR5_K];
  2463. float d8[QR5_K];
  2464. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2465. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2466. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2467. vl[0] = ql[0];
  2468. vl[1] = ql[4];
  2469. vh[0] = qh[0] >> bq8_offset;
  2470. vh[1] = qh[4] >> bq8_offset;
  2471. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2472. uint16_t aux[2];
  2473. const int j = bq8_offset/2;
  2474. if (j < 2) {
  2475. aux[0] = scales[j+0] & 0x3f3f;
  2476. aux[1] = scales[j+2] & 0x3f3f;
  2477. } else {
  2478. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2479. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2480. }
  2481. const uint8_t * sc = (const uint8_t *)aux;
  2482. const uint8_t * m = sc + 2;
  2483. #pragma unroll
  2484. for (int i = 0; i < QR5_K; ++i) {
  2485. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2486. d8[i] = __low2float(bq8i->ds);
  2487. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2488. u[2*i+0] = q8[0];
  2489. u[2*i+1] = q8[4];
  2490. }
  2491. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2492. #else
  2493. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2494. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2495. const int8_t * s = bq5_K->scales;
  2496. const float d = bq5_K->d;
  2497. const float d8_1 = __low2half(bq8_1[0].ds);
  2498. const float d8_2 = __low2half(bq8_1[1].ds);
  2499. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2500. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2501. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2502. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2503. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2504. const int vl1 = ql[0];
  2505. const int vl2 = ql[4];
  2506. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2507. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2508. const int in = step%8; // 0, 4, 0, 4
  2509. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2510. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2511. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2512. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2513. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2514. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2515. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2516. return d * sumf_d;
  2517. #else
  2518. assert(false);
  2519. return 0.0f; // only to satisfy the compiler
  2520. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2521. #endif
  2522. }
  2523. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2524. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2525. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2526. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2527. *x_ql = tile_x_ql;
  2528. *x_dm = tile_x_dm;
  2529. *x_sc = tile_x_sc;
  2530. }
  2531. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2532. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2533. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2534. GGML_CUDA_ASSUME(i_offset >= 0);
  2535. GGML_CUDA_ASSUME(i_offset < nwarps);
  2536. GGML_CUDA_ASSUME(k >= 0);
  2537. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2538. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2539. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2540. const block_q5_K * bx0 = (block_q5_K *) vx;
  2541. #pragma unroll
  2542. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2543. int i = i0 + i_offset;
  2544. if (need_check) {
  2545. i = min(i, i_max);
  2546. }
  2547. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2548. const int ky = QR5_K*kqsx;
  2549. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2550. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2551. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2552. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2553. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2554. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2555. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2556. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2557. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2558. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2559. }
  2560. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2561. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2562. #pragma unroll
  2563. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2564. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2565. if (need_check) {
  2566. i = min(i, i_max);
  2567. }
  2568. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2569. #if QK_K == 256
  2570. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2571. #endif
  2572. }
  2573. #pragma unroll
  2574. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2575. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2576. if (need_check) {
  2577. i = min(i, i_max);
  2578. }
  2579. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2580. const int * scales = (int *) bxi->scales;
  2581. const int ksc = k % (WARP_SIZE/8);
  2582. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2583. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2584. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2585. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2586. }
  2587. }
  2588. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2589. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2590. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2591. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2592. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2593. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2594. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2595. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2596. }
  2597. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2598. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2599. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2600. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2601. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2602. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2603. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2604. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2605. const int8_t * scales = bq6_K->scales + scale_offset;
  2606. int u[QR6_K];
  2607. float d8[QR6_K];
  2608. #pragma unroll
  2609. for (int i = 0; i < QR6_K; ++i) {
  2610. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2611. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2612. }
  2613. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2614. }
  2615. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2616. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2617. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2618. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2619. *x_ql = tile_x_ql;
  2620. *x_dm = tile_x_dm;
  2621. *x_sc = tile_x_sc;
  2622. }
  2623. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2624. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2625. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2626. GGML_CUDA_ASSUME(i_offset >= 0);
  2627. GGML_CUDA_ASSUME(i_offset < nwarps);
  2628. GGML_CUDA_ASSUME(k >= 0);
  2629. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2630. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2631. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2632. const block_q6_K * bx0 = (block_q6_K *) vx;
  2633. #pragma unroll
  2634. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2635. int i = i0 + i_offset;
  2636. if (need_check) {
  2637. i = min(i, i_max);
  2638. }
  2639. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2640. const int ky = QR6_K*kqsx;
  2641. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2642. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2643. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2644. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2645. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2646. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2647. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2648. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2649. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2650. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2651. }
  2652. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2653. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2654. float * x_dmf = (float *) x_dm;
  2655. #pragma unroll
  2656. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2657. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2658. if (need_check) {
  2659. i = min(i, i_max);
  2660. }
  2661. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2662. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2663. }
  2664. #pragma unroll
  2665. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2666. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2667. if (need_check) {
  2668. i = min(i, i_max);
  2669. }
  2670. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2671. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2672. }
  2673. }
  2674. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2675. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2676. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2677. const float * x_dmf = (const float *) x_dm;
  2678. const float * y_df = (const float *) y_ds;
  2679. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2680. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2681. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2682. 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]);
  2683. }
  2684. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2685. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2686. static __device__ __forceinline__ void mul_mat_q(
  2687. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2688. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2689. const block_q_t * x = (const block_q_t *) vx;
  2690. const block_q8_1 * y = (const block_q8_1 *) vy;
  2691. const int blocks_per_row_x = ncols_x / qk;
  2692. const int blocks_per_col_y = nrows_y / QK8_1;
  2693. const int blocks_per_warp = WARP_SIZE / qi;
  2694. const int & ncols_dst = ncols_y;
  2695. const int row_dst_0 = blockIdx.x*mmq_y;
  2696. const int & row_x_0 = row_dst_0;
  2697. const int col_dst_0 = blockIdx.y*mmq_x;
  2698. const int & col_y_0 = col_dst_0;
  2699. int * tile_x_ql = nullptr;
  2700. half2 * tile_x_dm = nullptr;
  2701. int * tile_x_qh = nullptr;
  2702. int * tile_x_sc = nullptr;
  2703. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2704. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2705. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2706. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {0.0f};
  2707. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2708. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2709. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2710. #pragma unroll
  2711. for (int ir = 0; ir < qr; ++ir) {
  2712. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2713. const int kbxd = kqs / QI8_1;
  2714. #pragma unroll
  2715. for (int i = 0; i < mmq_x; i += nwarps) {
  2716. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2717. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2718. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2719. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2720. }
  2721. #pragma unroll
  2722. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2723. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2724. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2725. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2726. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2727. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  2728. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  2729. if (need_sum) {
  2730. *dsi_dst = *dsi_src;
  2731. } else {
  2732. float * dfi_dst = (float *) dsi_dst;
  2733. *dfi_dst = __low2half(*dsi_src);
  2734. }
  2735. }
  2736. __syncthreads();
  2737. // #pragma unroll // unrolling this loop causes too much register pressure
  2738. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  2739. #pragma unroll
  2740. for (int j = 0; j < mmq_x; j += nwarps) {
  2741. #pragma unroll
  2742. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2743. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  2744. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  2745. threadIdx.x + i, threadIdx.y + j, k);
  2746. }
  2747. }
  2748. }
  2749. __syncthreads();
  2750. }
  2751. }
  2752. #pragma unroll
  2753. for (int j = 0; j < mmq_x; j += nwarps) {
  2754. const int col_dst = col_dst_0 + j + threadIdx.y;
  2755. if (col_dst >= ncols_dst) {
  2756. return;
  2757. }
  2758. #pragma unroll
  2759. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2760. const int row_dst = row_dst_0 + threadIdx.x + i;
  2761. if (row_dst >= nrows_dst) {
  2762. continue;
  2763. }
  2764. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  2765. }
  2766. }
  2767. }
  2768. #define MMQ_X_Q4_0_RDNA2 64
  2769. #define MMQ_Y_Q4_0_RDNA2 128
  2770. #define NWARPS_Q4_0_RDNA2 8
  2771. #define MMQ_X_Q4_0_RDNA1 64
  2772. #define MMQ_Y_Q4_0_RDNA1 64
  2773. #define NWARPS_Q4_0_RDNA1 8
  2774. #if defined(CUDA_USE_TENSOR_CORES)
  2775. #define MMQ_X_Q4_0_AMPERE 4
  2776. #define MMQ_Y_Q4_0_AMPERE 32
  2777. #define NWARPS_Q4_0_AMPERE 4
  2778. #else
  2779. #define MMQ_X_Q4_0_AMPERE 64
  2780. #define MMQ_Y_Q4_0_AMPERE 128
  2781. #define NWARPS_Q4_0_AMPERE 4
  2782. #endif
  2783. #define MMQ_X_Q4_0_PASCAL 64
  2784. #define MMQ_Y_Q4_0_PASCAL 64
  2785. #define NWARPS_Q4_0_PASCAL 8
  2786. template <bool need_check> static __global__ void
  2787. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2788. #if defined(RDNA3) || defined(RDNA2)
  2789. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  2790. #endif // defined(RDNA3) || defined(RDNA2)
  2791. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2792. mul_mat_q4_0(
  2793. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2794. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2795. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2796. #if defined(RDNA3) || defined(RDNA2)
  2797. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  2798. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  2799. const int nwarps = NWARPS_Q4_0_RDNA2;
  2800. #else
  2801. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  2802. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  2803. const int nwarps = NWARPS_Q4_0_RDNA1;
  2804. #endif // defined(RDNA3) || defined(RDNA2)
  2805. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2806. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2807. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2808. #elif __CUDA_ARCH__ >= CC_VOLTA
  2809. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  2810. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  2811. const int nwarps = NWARPS_Q4_0_AMPERE;
  2812. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2813. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2814. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2815. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2816. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  2817. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  2818. const int nwarps = NWARPS_Q4_0_PASCAL;
  2819. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2820. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2821. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2822. #else
  2823. (void) vec_dot_q4_0_q8_1_mul_mat;
  2824. assert(false);
  2825. #endif // __CUDA_ARCH__ >= CC_VOLTA
  2826. }
  2827. #define MMQ_X_Q4_1_RDNA2 64
  2828. #define MMQ_Y_Q4_1_RDNA2 128
  2829. #define NWARPS_Q4_1_RDNA2 8
  2830. #define MMQ_X_Q4_1_RDNA1 64
  2831. #define MMQ_Y_Q4_1_RDNA1 64
  2832. #define NWARPS_Q4_1_RDNA1 8
  2833. #if defined(CUDA_USE_TENSOR_CORES)
  2834. #define MMQ_X_Q4_1_AMPERE 4
  2835. #define MMQ_Y_Q4_1_AMPERE 32
  2836. #define NWARPS_Q4_1_AMPERE 4
  2837. #else
  2838. #define MMQ_X_Q4_1_AMPERE 64
  2839. #define MMQ_Y_Q4_1_AMPERE 128
  2840. #define NWARPS_Q4_1_AMPERE 4
  2841. #endif
  2842. #define MMQ_X_Q4_1_PASCAL 64
  2843. #define MMQ_Y_Q4_1_PASCAL 64
  2844. #define NWARPS_Q4_1_PASCAL 8
  2845. template <bool need_check> static __global__ void
  2846. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2847. #if defined(RDNA3) || defined(RDNA2)
  2848. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  2849. #endif // defined(RDNA3) || defined(RDNA2)
  2850. #elif __CUDA_ARCH__ < CC_VOLTA
  2851. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  2852. #endif // __CUDA_ARCH__ < CC_VOLTA
  2853. mul_mat_q4_1(
  2854. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2855. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2856. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2857. #if defined(RDNA3) || defined(RDNA2)
  2858. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  2859. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  2860. const int nwarps = NWARPS_Q4_1_RDNA2;
  2861. #else
  2862. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  2863. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  2864. const int nwarps = NWARPS_Q4_1_RDNA1;
  2865. #endif // defined(RDNA3) || defined(RDNA2)
  2866. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2867. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2868. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2869. #elif __CUDA_ARCH__ >= CC_VOLTA
  2870. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  2871. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  2872. const int nwarps = NWARPS_Q4_1_AMPERE;
  2873. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2874. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2875. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2876. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2877. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  2878. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  2879. const int nwarps = NWARPS_Q4_1_PASCAL;
  2880. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2881. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2882. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2883. #else
  2884. (void) vec_dot_q4_1_q8_1_mul_mat;
  2885. assert(false);
  2886. #endif // __CUDA_ARCH__ >= CC_VOLTA
  2887. }
  2888. #define MMQ_X_Q5_0_RDNA2 64
  2889. #define MMQ_Y_Q5_0_RDNA2 128
  2890. #define NWARPS_Q5_0_RDNA2 8
  2891. #define MMQ_X_Q5_0_RDNA1 64
  2892. #define MMQ_Y_Q5_0_RDNA1 64
  2893. #define NWARPS_Q5_0_RDNA1 8
  2894. #if defined(CUDA_USE_TENSOR_CORES)
  2895. #define MMQ_X_Q5_0_AMPERE 4
  2896. #define MMQ_Y_Q5_0_AMPERE 32
  2897. #define NWARPS_Q5_0_AMPERE 4
  2898. #else
  2899. #define MMQ_X_Q5_0_AMPERE 128
  2900. #define MMQ_Y_Q5_0_AMPERE 64
  2901. #define NWARPS_Q5_0_AMPERE 4
  2902. #endif
  2903. #define MMQ_X_Q5_0_PASCAL 64
  2904. #define MMQ_Y_Q5_0_PASCAL 64
  2905. #define NWARPS_Q5_0_PASCAL 8
  2906. template <bool need_check> static __global__ void
  2907. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2908. #if defined(RDNA3) || defined(RDNA2)
  2909. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  2910. #endif // defined(RDNA3) || defined(RDNA2)
  2911. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2912. mul_mat_q5_0(
  2913. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2914. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2915. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2916. #if defined(RDNA3) || defined(RDNA2)
  2917. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  2918. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  2919. const int nwarps = NWARPS_Q5_0_RDNA2;
  2920. #else
  2921. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  2922. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  2923. const int nwarps = NWARPS_Q5_0_RDNA1;
  2924. #endif // defined(RDNA3) || defined(RDNA2)
  2925. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2926. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2927. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2928. #elif __CUDA_ARCH__ >= CC_VOLTA
  2929. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  2930. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  2931. const int nwarps = NWARPS_Q5_0_AMPERE;
  2932. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2933. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2934. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2935. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2936. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  2937. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  2938. const int nwarps = NWARPS_Q5_0_PASCAL;
  2939. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2940. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2941. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2942. #else
  2943. (void) vec_dot_q5_0_q8_1_mul_mat;
  2944. assert(false);
  2945. #endif // __CUDA_ARCH__ >= CC_VOLTA
  2946. }
  2947. #define MMQ_X_Q5_1_RDNA2 64
  2948. #define MMQ_Y_Q5_1_RDNA2 128
  2949. #define NWARPS_Q5_1_RDNA2 8
  2950. #define MMQ_X_Q5_1_RDNA1 64
  2951. #define MMQ_Y_Q5_1_RDNA1 64
  2952. #define NWARPS_Q5_1_RDNA1 8
  2953. #if defined(CUDA_USE_TENSOR_CORES)
  2954. #define MMQ_X_Q5_1_AMPERE 4
  2955. #define MMQ_Y_Q5_1_AMPERE 32
  2956. #define NWARPS_Q5_1_AMPERE 4
  2957. #else
  2958. #define MMQ_X_Q5_1_AMPERE 128
  2959. #define MMQ_Y_Q5_1_AMPERE 64
  2960. #define NWARPS_Q5_1_AMPERE 4
  2961. #endif
  2962. #define MMQ_X_Q5_1_PASCAL 64
  2963. #define MMQ_Y_Q5_1_PASCAL 64
  2964. #define NWARPS_Q5_1_PASCAL 8
  2965. template <bool need_check> static __global__ void
  2966. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2967. #if defined(RDNA3) || defined(RDNA2)
  2968. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  2969. #endif // defined(RDNA3) || defined(RDNA2)
  2970. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2971. mul_mat_q5_1(
  2972. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2973. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2974. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2975. #if defined(RDNA3) || defined(RDNA2)
  2976. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  2977. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  2978. const int nwarps = NWARPS_Q5_1_RDNA2;
  2979. #else
  2980. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  2981. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  2982. const int nwarps = NWARPS_Q5_1_RDNA1;
  2983. #endif // defined(RDNA3) || defined(RDNA2)
  2984. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2985. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  2986. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2987. #elif __CUDA_ARCH__ >= CC_VOLTA
  2988. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  2989. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  2990. const int nwarps = NWARPS_Q5_1_AMPERE;
  2991. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2992. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  2993. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2994. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2995. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  2996. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  2997. const int nwarps = NWARPS_Q5_1_PASCAL;
  2998. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2999. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3000. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3001. #else
  3002. (void) vec_dot_q5_1_q8_1_mul_mat;
  3003. assert(false);
  3004. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3005. }
  3006. #define MMQ_X_Q8_0_RDNA2 64
  3007. #define MMQ_Y_Q8_0_RDNA2 128
  3008. #define NWARPS_Q8_0_RDNA2 8
  3009. #define MMQ_X_Q8_0_RDNA1 64
  3010. #define MMQ_Y_Q8_0_RDNA1 64
  3011. #define NWARPS_Q8_0_RDNA1 8
  3012. #if defined(CUDA_USE_TENSOR_CORES)
  3013. #define MMQ_X_Q8_0_AMPERE 4
  3014. #define MMQ_Y_Q8_0_AMPERE 32
  3015. #define NWARPS_Q8_0_AMPERE 4
  3016. #else
  3017. #define MMQ_X_Q8_0_AMPERE 128
  3018. #define MMQ_Y_Q8_0_AMPERE 64
  3019. #define NWARPS_Q8_0_AMPERE 4
  3020. #endif
  3021. #define MMQ_X_Q8_0_PASCAL 64
  3022. #define MMQ_Y_Q8_0_PASCAL 64
  3023. #define NWARPS_Q8_0_PASCAL 8
  3024. template <bool need_check> static __global__ void
  3025. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3026. #if defined(RDNA3) || defined(RDNA2)
  3027. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3028. #endif // defined(RDNA3) || defined(RDNA2)
  3029. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3030. mul_mat_q8_0(
  3031. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3032. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3033. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3034. #if defined(RDNA3) || defined(RDNA2)
  3035. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3036. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3037. const int nwarps = NWARPS_Q8_0_RDNA2;
  3038. #else
  3039. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3040. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3041. const int nwarps = NWARPS_Q8_0_RDNA1;
  3042. #endif // defined(RDNA3) || defined(RDNA2)
  3043. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3044. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3045. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3046. #elif __CUDA_ARCH__ >= CC_VOLTA
  3047. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3048. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3049. const int nwarps = NWARPS_Q8_0_AMPERE;
  3050. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3051. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3052. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3053. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3054. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3055. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3056. const int nwarps = NWARPS_Q8_0_PASCAL;
  3057. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3058. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3059. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3060. #else
  3061. (void) vec_dot_q8_0_q8_1_mul_mat;
  3062. assert(false);
  3063. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3064. }
  3065. #define MMQ_X_Q2_K_RDNA2 64
  3066. #define MMQ_Y_Q2_K_RDNA2 128
  3067. #define NWARPS_Q2_K_RDNA2 8
  3068. #define MMQ_X_Q2_K_RDNA1 128
  3069. #define MMQ_Y_Q2_K_RDNA1 32
  3070. #define NWARPS_Q2_K_RDNA1 8
  3071. #if defined(CUDA_USE_TENSOR_CORES)
  3072. #define MMQ_X_Q2_K_AMPERE 4
  3073. #define MMQ_Y_Q2_K_AMPERE 32
  3074. #define NWARPS_Q2_K_AMPERE 4
  3075. #else
  3076. #define MMQ_X_Q2_K_AMPERE 64
  3077. #define MMQ_Y_Q2_K_AMPERE 128
  3078. #define NWARPS_Q2_K_AMPERE 4
  3079. #endif
  3080. #define MMQ_X_Q2_K_PASCAL 64
  3081. #define MMQ_Y_Q2_K_PASCAL 64
  3082. #define NWARPS_Q2_K_PASCAL 8
  3083. template <bool need_check> static __global__ void
  3084. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3085. #if defined(RDNA3) || defined(RDNA2)
  3086. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3087. #endif // defined(RDNA3) || defined(RDNA2)
  3088. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3089. mul_mat_q2_K(
  3090. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3091. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3092. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3093. #if defined(RDNA3) || defined(RDNA2)
  3094. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3095. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3096. const int nwarps = NWARPS_Q2_K_RDNA2;
  3097. #else
  3098. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3099. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3100. const int nwarps = NWARPS_Q2_K_RDNA1;
  3101. #endif // defined(RDNA3) || defined(RDNA2)
  3102. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3103. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3104. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3105. #elif __CUDA_ARCH__ >= CC_VOLTA
  3106. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3107. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3108. const int nwarps = NWARPS_Q2_K_AMPERE;
  3109. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3110. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3111. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3112. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3113. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3114. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3115. const int nwarps = NWARPS_Q2_K_PASCAL;
  3116. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3117. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3118. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3119. #else
  3120. (void) vec_dot_q2_K_q8_1_mul_mat;
  3121. assert(false);
  3122. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3123. }
  3124. #define MMQ_X_Q3_K_RDNA2 128
  3125. #define MMQ_Y_Q3_K_RDNA2 64
  3126. #define NWARPS_Q3_K_RDNA2 8
  3127. #define MMQ_X_Q3_K_RDNA1 32
  3128. #define MMQ_Y_Q3_K_RDNA1 128
  3129. #define NWARPS_Q3_K_RDNA1 8
  3130. #if defined(CUDA_USE_TENSOR_CORES)
  3131. #define MMQ_X_Q3_K_AMPERE 4
  3132. #define MMQ_Y_Q3_K_AMPERE 32
  3133. #define NWARPS_Q3_K_AMPERE 4
  3134. #else
  3135. #define MMQ_X_Q3_K_AMPERE 128
  3136. #define MMQ_Y_Q3_K_AMPERE 128
  3137. #define NWARPS_Q3_K_AMPERE 4
  3138. #endif
  3139. #define MMQ_X_Q3_K_PASCAL 64
  3140. #define MMQ_Y_Q3_K_PASCAL 64
  3141. #define NWARPS_Q3_K_PASCAL 8
  3142. template <bool need_check> static __global__ void
  3143. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3144. #if defined(RDNA3) || defined(RDNA2)
  3145. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3146. #endif // defined(RDNA3) || defined(RDNA2)
  3147. #elif __CUDA_ARCH__ < CC_VOLTA
  3148. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3149. #endif // __CUDA_ARCH__ < CC_VOLTA
  3150. mul_mat_q3_K(
  3151. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3152. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3153. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3154. #if defined(RDNA3) || defined(RDNA2)
  3155. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3156. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3157. const int nwarps = NWARPS_Q3_K_RDNA2;
  3158. #else
  3159. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3160. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3161. const int nwarps = NWARPS_Q3_K_RDNA1;
  3162. #endif // defined(RDNA3) || defined(RDNA2)
  3163. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3164. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3165. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3166. #elif __CUDA_ARCH__ >= CC_VOLTA
  3167. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3168. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3169. const int nwarps = NWARPS_Q3_K_AMPERE;
  3170. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3171. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3172. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3173. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3174. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3175. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3176. const int nwarps = NWARPS_Q3_K_PASCAL;
  3177. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3178. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3179. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3180. #else
  3181. (void) vec_dot_q3_K_q8_1_mul_mat;
  3182. assert(false);
  3183. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3184. }
  3185. #define MMQ_X_Q4_K_RDNA2 64
  3186. #define MMQ_Y_Q4_K_RDNA2 128
  3187. #define NWARPS_Q4_K_RDNA2 8
  3188. #define MMQ_X_Q4_K_RDNA1 32
  3189. #define MMQ_Y_Q4_K_RDNA1 64
  3190. #define NWARPS_Q4_K_RDNA1 8
  3191. #if defined(CUDA_USE_TENSOR_CORES)
  3192. #define MMQ_X_Q4_K_AMPERE 4
  3193. #define MMQ_Y_Q4_K_AMPERE 32
  3194. #define NWARPS_Q4_K_AMPERE 4
  3195. #else
  3196. #define MMQ_X_Q4_K_AMPERE 64
  3197. #define MMQ_Y_Q4_K_AMPERE 128
  3198. #define NWARPS_Q4_K_AMPERE 4
  3199. #endif
  3200. #define MMQ_X_Q4_K_PASCAL 64
  3201. #define MMQ_Y_Q4_K_PASCAL 64
  3202. #define NWARPS_Q4_K_PASCAL 8
  3203. template <bool need_check> static __global__ void
  3204. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3205. #if defined(RDNA3) || defined(RDNA2)
  3206. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3207. #endif // defined(RDNA3) || defined(RDNA2)
  3208. #elif __CUDA_ARCH__ < CC_VOLTA
  3209. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3210. #endif // __CUDA_ARCH__ < CC_VOLTA
  3211. mul_mat_q4_K(
  3212. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3213. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3214. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3215. #if defined(RDNA3) || defined(RDNA2)
  3216. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3217. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3218. const int nwarps = NWARPS_Q4_K_RDNA2;
  3219. #else
  3220. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3221. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3222. const int nwarps = NWARPS_Q4_K_RDNA1;
  3223. #endif // defined(RDNA3) || defined(RDNA2)
  3224. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3225. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3226. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3227. #elif __CUDA_ARCH__ >= CC_VOLTA
  3228. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3229. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3230. const int nwarps = NWARPS_Q4_K_AMPERE;
  3231. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3232. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3233. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3234. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3235. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3236. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3237. const int nwarps = NWARPS_Q4_K_PASCAL;
  3238. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3239. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3240. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3241. #else
  3242. (void) vec_dot_q4_K_q8_1_mul_mat;
  3243. assert(false);
  3244. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3245. }
  3246. #define MMQ_X_Q5_K_RDNA2 64
  3247. #define MMQ_Y_Q5_K_RDNA2 128
  3248. #define NWARPS_Q5_K_RDNA2 8
  3249. #define MMQ_X_Q5_K_RDNA1 32
  3250. #define MMQ_Y_Q5_K_RDNA1 64
  3251. #define NWARPS_Q5_K_RDNA1 8
  3252. #if defined(CUDA_USE_TENSOR_CORES)
  3253. #define MMQ_X_Q5_K_AMPERE 4
  3254. #define MMQ_Y_Q5_K_AMPERE 32
  3255. #define NWARPS_Q5_K_AMPERE 4
  3256. #else
  3257. #define MMQ_X_Q5_K_AMPERE 64
  3258. #define MMQ_Y_Q5_K_AMPERE 128
  3259. #define NWARPS_Q5_K_AMPERE 4
  3260. #endif
  3261. #define MMQ_X_Q5_K_PASCAL 64
  3262. #define MMQ_Y_Q5_K_PASCAL 64
  3263. #define NWARPS_Q5_K_PASCAL 8
  3264. template <bool need_check> static __global__ void
  3265. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3266. #if defined(RDNA3) || defined(RDNA2)
  3267. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3268. #endif // defined(RDNA3) || defined(RDNA2)
  3269. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3270. mul_mat_q5_K(
  3271. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3272. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3273. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3274. #if defined(RDNA3) || defined(RDNA2)
  3275. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3276. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3277. const int nwarps = NWARPS_Q5_K_RDNA2;
  3278. #else
  3279. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3280. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3281. const int nwarps = NWARPS_Q5_K_RDNA1;
  3282. #endif // defined(RDNA3) || defined(RDNA2)
  3283. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3284. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3285. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3286. #elif __CUDA_ARCH__ >= CC_VOLTA
  3287. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3288. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3289. const int nwarps = NWARPS_Q5_K_AMPERE;
  3290. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3291. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3292. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3293. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3294. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3295. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3296. const int nwarps = NWARPS_Q5_K_PASCAL;
  3297. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3298. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3299. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3300. #else
  3301. (void) vec_dot_q5_K_q8_1_mul_mat;
  3302. assert(false);
  3303. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3304. }
  3305. #define MMQ_X_Q6_K_RDNA2 64
  3306. #define MMQ_Y_Q6_K_RDNA2 128
  3307. #define NWARPS_Q6_K_RDNA2 8
  3308. #define MMQ_X_Q6_K_RDNA1 32
  3309. #define MMQ_Y_Q6_K_RDNA1 64
  3310. #define NWARPS_Q6_K_RDNA1 8
  3311. #if defined(CUDA_USE_TENSOR_CORES)
  3312. #define MMQ_X_Q6_K_AMPERE 4
  3313. #define MMQ_Y_Q6_K_AMPERE 32
  3314. #define NWARPS_Q6_K_AMPERE 4
  3315. #else
  3316. #define MMQ_X_Q6_K_AMPERE 64
  3317. #define MMQ_Y_Q6_K_AMPERE 64
  3318. #define NWARPS_Q6_K_AMPERE 4
  3319. #endif
  3320. #define MMQ_X_Q6_K_PASCAL 64
  3321. #define MMQ_Y_Q6_K_PASCAL 64
  3322. #define NWARPS_Q6_K_PASCAL 8
  3323. template <bool need_check> static __global__ void
  3324. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3325. #if defined(RDNA3) || defined(RDNA2)
  3326. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3327. #endif // defined(RDNA3) || defined(RDNA2)
  3328. #elif __CUDA_ARCH__ < CC_VOLTA
  3329. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3330. #endif // __CUDA_ARCH__ < CC_VOLTA
  3331. mul_mat_q6_K(
  3332. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3333. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3334. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3335. #if defined(RDNA3) || defined(RDNA2)
  3336. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3337. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3338. const int nwarps = NWARPS_Q6_K_RDNA2;
  3339. #else
  3340. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3341. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3342. const int nwarps = NWARPS_Q6_K_RDNA1;
  3343. #endif // defined(RDNA3) || defined(RDNA2)
  3344. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3345. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3346. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3347. #elif __CUDA_ARCH__ >= CC_VOLTA
  3348. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3349. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3350. const int nwarps = NWARPS_Q6_K_AMPERE;
  3351. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3352. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3353. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3354. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3355. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3356. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3357. const int nwarps = NWARPS_Q6_K_PASCAL;
  3358. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3359. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3360. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3361. #else
  3362. (void) vec_dot_q6_K_q8_1_mul_mat;
  3363. assert(false);
  3364. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3365. }
  3366. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3367. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3368. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3369. if (row >= nrows) {
  3370. return;
  3371. }
  3372. const int blocks_per_row = ncols / qk;
  3373. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3374. // partial sum for each thread
  3375. float tmp = 0.0f;
  3376. const block_q_t * x = (const block_q_t *) vx;
  3377. const block_q8_1 * y = (const block_q8_1 *) vy;
  3378. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3379. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3380. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3381. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3382. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3383. }
  3384. // sum up partial sums and write back result
  3385. #pragma unroll
  3386. for (int mask = 16; mask > 0; mask >>= 1) {
  3387. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3388. }
  3389. if (threadIdx.x == 0) {
  3390. dst[row] = tmp;
  3391. }
  3392. }
  3393. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3394. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3395. // qk = quantized weights per x block
  3396. // qr = number of quantized weights per data value in x block
  3397. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3398. if (row >= nrows) {
  3399. return;
  3400. }
  3401. const int tid = threadIdx.x;
  3402. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3403. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3404. const int y_offset = qr == 1 ? 1 : qk/2;
  3405. // partial sum for each thread
  3406. #ifdef GGML_CUDA_F16
  3407. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3408. #else
  3409. float tmp = 0.0f;
  3410. #endif // GGML_CUDA_F16
  3411. for (int i = 0; i < ncols; i += iter_stride) {
  3412. const int col = i + vals_per_iter*tid;
  3413. const int ib = (row*ncols + col)/qk; // x block index
  3414. const int iqs = (col%qk)/qr; // x quant index
  3415. const int iybs = col - col%qk; // y block start index
  3416. // processing >2 values per i iter is faster for fast GPUs
  3417. #pragma unroll
  3418. for (int j = 0; j < vals_per_iter; j += 2) {
  3419. // process 2 vals per j iter
  3420. // dequantize
  3421. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3422. dfloat2 v;
  3423. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3424. // matrix multiplication
  3425. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3426. #ifdef GGML_CUDA_F16
  3427. tmp += __hmul2(v, {
  3428. y[iybs + iqs + j/qr + 0],
  3429. y[iybs + iqs + j/qr + y_offset]
  3430. });
  3431. #else
  3432. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3433. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3434. #endif // GGML_CUDA_F16
  3435. }
  3436. }
  3437. // sum up partial sums and write back result
  3438. #pragma unroll
  3439. for (int mask = 16; mask > 0; mask >>= 1) {
  3440. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3441. }
  3442. if (tid == 0) {
  3443. #ifdef GGML_CUDA_F16
  3444. dst[row] = tmp.x + tmp.y;
  3445. #else
  3446. dst[row] = tmp;
  3447. #endif // GGML_CUDA_F16
  3448. }
  3449. }
  3450. static __global__ void mul_mat_p021_f16_f32(
  3451. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3452. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3453. const half * x = (const half *) vx;
  3454. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3455. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3456. const int channel_x = channel / (nchannels_y / nchannels_x);
  3457. const int nrows_y = ncols_x;
  3458. const int nrows_dst = nrows_x;
  3459. const int row_dst = row_x;
  3460. float tmp = 0.0f;
  3461. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3462. const int col_x = col_x0 + threadIdx.x;
  3463. if (col_x >= ncols_x) {
  3464. break;
  3465. }
  3466. // x is transposed and permuted
  3467. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3468. const float xi = __half2float(x[ix]);
  3469. const int row_y = col_x;
  3470. // y is not transposed but permuted
  3471. const int iy = channel*nrows_y + row_y;
  3472. tmp += xi * y[iy];
  3473. }
  3474. // dst is not transposed and not permuted
  3475. const int idst = channel*nrows_dst + row_dst;
  3476. // sum up partial sums and write back result
  3477. #pragma unroll
  3478. for (int mask = 16; mask > 0; mask >>= 1) {
  3479. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3480. }
  3481. if (threadIdx.x == 0) {
  3482. dst[idst] = tmp;
  3483. }
  3484. }
  3485. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3486. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3487. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3488. const half * x = (const half *) vx;
  3489. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3490. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3491. const int channel_x = channel / channel_x_divisor;
  3492. const int nrows_y = ncols_x;
  3493. const int nrows_dst = nrows_x;
  3494. const int row_dst = row_x;
  3495. const int idst = channel*nrows_dst + row_dst;
  3496. float tmp = 0.0f;
  3497. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3498. const int col_x = col_x0 + threadIdx.x;
  3499. if (col_x >= ncols_x) {
  3500. break;
  3501. }
  3502. const int row_y = col_x;
  3503. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3504. const int iy = channel*nrows_y + row_y;
  3505. const float xi = __half2float(x[ix]);
  3506. tmp += xi * y[iy];
  3507. }
  3508. // sum up partial sums and write back result
  3509. #pragma unroll
  3510. for (int mask = 16; mask > 0; mask >>= 1) {
  3511. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3512. }
  3513. if (threadIdx.x == 0) {
  3514. dst[idst] = tmp;
  3515. }
  3516. }
  3517. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3518. const float * xi = (const float *) cxi;
  3519. float * dsti = (float *) cdsti;
  3520. *dsti = *xi;
  3521. }
  3522. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3523. const float * xi = (const float *) cxi;
  3524. half * dsti = (half *) cdsti;
  3525. *dsti = __float2half(*xi);
  3526. }
  3527. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3528. const half * xi = (const half *) cxi;
  3529. half * dsti = (half *) cdsti;
  3530. *dsti = *xi;
  3531. }
  3532. template <cpy_kernel_t cpy_1>
  3533. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3534. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3535. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3536. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3537. if (i >= ne) {
  3538. return;
  3539. }
  3540. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3541. // then combine those indices with the corresponding byte offsets to get the total offsets
  3542. const int i02 = i / (ne00*ne01);
  3543. const int i01 = (i - i02*ne01*ne00) / ne00;
  3544. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3545. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3546. const int i12 = i / (ne10*ne11);
  3547. const int i11 = (i - i12*ne10*ne11) / ne10;
  3548. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3549. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3550. cpy_1(cx + x_offset, cdst + dst_offset);
  3551. }
  3552. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3553. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3554. return 1.0f - min(1.0f, max(0.0f, y));
  3555. }
  3556. struct rope_corr_dims {
  3557. float v[4];
  3558. };
  3559. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3560. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3561. static __device__ void rope_yarn(
  3562. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3563. float * cos_theta, float * sin_theta
  3564. ) {
  3565. // Get n-d rotational scaling corrected for extrapolation
  3566. float theta_interp = freq_scale * theta_extrap;
  3567. float theta = theta_interp;
  3568. if (ext_factor != 0.0f) {
  3569. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3570. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3571. // Get n-d magnitude scaling corrected for interpolation
  3572. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3573. }
  3574. *cos_theta = cosf(theta) * mscale;
  3575. *sin_theta = sinf(theta) * mscale;
  3576. }
  3577. // rope == RoPE == rotary positional embedding
  3578. template<typename T, bool has_pos>
  3579. static __global__ void rope(
  3580. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3581. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3582. ) {
  3583. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3584. if (col >= ncols) {
  3585. return;
  3586. }
  3587. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3588. const int i = row*ncols + col;
  3589. const int i2 = row/p_delta_rows;
  3590. const int p = has_pos ? pos[i2] : 0;
  3591. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3592. float cos_theta, sin_theta;
  3593. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3594. const float x0 = x[i + 0];
  3595. const float x1 = x[i + 1];
  3596. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3597. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3598. }
  3599. template<typename T, bool has_pos>
  3600. static __global__ void rope_neox(
  3601. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3602. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3603. ) {
  3604. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3605. if (col >= ncols) {
  3606. return;
  3607. }
  3608. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3609. const int i = row*ncols + col/2;
  3610. const int i2 = row/p_delta_rows;
  3611. // simplified from `(ib * ncols + col) * (-1 / ncols)`, where ib is assumed to be zero
  3612. const float cur_rot = -float(col)/ncols;
  3613. const int p = has_pos ? pos[i2] : 0;
  3614. const float theta_base = p*powf(freq_base, cur_rot);
  3615. float cos_theta, sin_theta;
  3616. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3617. const float x0 = x[i + 0];
  3618. const float x1 = x[i + ncols/2];
  3619. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3620. dst[i + ncols/2] = x0*sin_theta + x1*cos_theta;
  3621. }
  3622. static __global__ void rope_glm_f32(
  3623. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3624. int n_ctx
  3625. ) {
  3626. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3627. const int half_n_dims = ncols/4;
  3628. if (col >= half_n_dims) {
  3629. return;
  3630. }
  3631. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3632. const int i = row*ncols + col;
  3633. const int i2 = row/p_delta_rows;
  3634. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  3635. // FIXME: this is likely wrong
  3636. const int p = pos != nullptr ? pos[i2] : 0;
  3637. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  3638. const float sin_theta = sinf(theta);
  3639. const float cos_theta = cosf(theta);
  3640. const float x0 = x[i + 0];
  3641. const float x1 = x[i + half_n_dims];
  3642. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3643. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  3644. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  3645. const float sin_block_theta = sinf(block_theta);
  3646. const float cos_block_theta = cosf(block_theta);
  3647. const float x2 = x[i + half_n_dims * 2];
  3648. const float x3 = x[i + half_n_dims * 3];
  3649. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  3650. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  3651. }
  3652. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  3653. const int n_heads_log2_floor, const float m0, const float m1) {
  3654. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3655. if (col >= ncols) {
  3656. return;
  3657. }
  3658. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3659. const int i = row*ncols + col;
  3660. const int k = row/k_rows;
  3661. float m_k;
  3662. if (k < n_heads_log2_floor) {
  3663. m_k = powf(m0, k + 1);
  3664. } else {
  3665. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  3666. }
  3667. dst[i] = col * m_k + x[i];
  3668. }
  3669. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  3670. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  3671. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3672. if (col >= ncols) {
  3673. return;
  3674. }
  3675. const int i = row*ncols + col;
  3676. // dst[i] = col > n_past + row ? -INFINITY : x[i];
  3677. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  3678. }
  3679. // the CUDA soft max implementation differs from the CPU implementation
  3680. // instead of doubles floats are used
  3681. static __global__ void soft_max_f32(const float * x, float * dst, const int ncols) {
  3682. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3683. const int block_size = blockDim.y;
  3684. const int tid = threadIdx.y;
  3685. float max_val = -INFINITY;
  3686. for (int col = tid; col < ncols; col += block_size) {
  3687. const int i = row*ncols + col;
  3688. max_val = max(max_val, x[i]);
  3689. }
  3690. // find the max value in the block
  3691. #pragma unroll
  3692. for (int mask = 16; mask > 0; mask >>= 1) {
  3693. max_val = max(max_val, __shfl_xor_sync(0xffffffff, max_val, mask, 32));
  3694. }
  3695. float tmp = 0.f;
  3696. for (int col = tid; col < ncols; col += block_size) {
  3697. const int i = row*ncols + col;
  3698. const float val = expf(x[i] - max_val);
  3699. tmp += val;
  3700. dst[i] = val;
  3701. }
  3702. // sum up partial sums
  3703. #pragma unroll
  3704. for (int mask = 16; mask > 0; mask >>= 1) {
  3705. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3706. }
  3707. const float inv_tmp = 1.f / tmp;
  3708. for (int col = tid; col < ncols; col += block_size) {
  3709. const int i = row*ncols + col;
  3710. dst[i] *= inv_tmp;
  3711. }
  3712. }
  3713. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  3714. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3715. if (i >= k) {
  3716. return;
  3717. }
  3718. dst[i] = scale * x[i];
  3719. }
  3720. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  3721. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3722. if (i >= k) {
  3723. return;
  3724. }
  3725. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  3726. }
  3727. static __global__ void im2col_f32_f16(
  3728. const float * x, half * dst,
  3729. int ofs0, int ofs1, int IW, int IH, int CHW,
  3730. int s0, int s1, int p0, int p1, int d0, int d1) {
  3731. const int iiw = blockIdx.z * s0 + threadIdx.z * d0 - p0;
  3732. const int iih = blockIdx.y * s1 + threadIdx.y * d1 - p1;
  3733. const int offset_dst =
  3734. (threadIdx.x * gridDim.y * gridDim.z + blockIdx.y * gridDim.z + blockIdx.z) * CHW +
  3735. (blockIdx.x * (blockDim.y * blockDim.z) + threadIdx.y * blockDim.z + threadIdx.z);
  3736. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  3737. dst[offset_dst] = __float2half(0.0f);
  3738. } else {
  3739. const int offset_src = threadIdx.x * ofs0 + blockIdx.x * ofs1;
  3740. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  3741. }
  3742. }
  3743. template<int qk, int qr, dequantize_kernel_t dq>
  3744. static void get_rows_cuda(const void * x, const int32_t * y, float * dst, const int nrows, const int ncols, cudaStream_t stream) {
  3745. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  3746. const int block_num_x = (ncols + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  3747. const dim3 block_nums(block_num_x, nrows, 1);
  3748. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols);
  3749. }
  3750. static void add_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  3751. const int num_blocks = (kx + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  3752. add_f32<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  3753. }
  3754. static void add_f16_f32_f16_cuda(const half * x, const float * y, half * dst, const int k, cudaStream_t stream) {
  3755. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  3756. add_f16_f32_f16<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  3757. }
  3758. static void add_f16_f32_f32_cuda(const half * x, const float * y, float * dst, const int k, cudaStream_t stream) {
  3759. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  3760. add_f16_f32_f32<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  3761. }
  3762. static void mul_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  3763. const int num_blocks = (kx + CUDA_MUL_BLOCK_SIZE - 1) / CUDA_MUL_BLOCK_SIZE;
  3764. mul_f32<<<num_blocks, CUDA_MUL_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  3765. }
  3766. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3767. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  3768. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3769. }
  3770. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3771. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  3772. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3773. }
  3774. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3775. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  3776. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3777. }
  3778. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3779. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  3780. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3781. }
  3782. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3783. GGML_ASSERT(ncols % WARP_SIZE == 0);
  3784. if (ncols < 1024) {
  3785. const dim3 block_dims(WARP_SIZE, 1, 1);
  3786. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  3787. } else {
  3788. const dim3 block_dims(1024, 1, 1);
  3789. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  3790. }
  3791. }
  3792. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  3793. GGML_ASSERT(ncols % WARP_SIZE == 0);
  3794. if (ncols < 1024) {
  3795. const dim3 block_dims(WARP_SIZE, 1, 1);
  3796. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  3797. } else {
  3798. const dim3 block_dims(1024, 1, 1);
  3799. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  3800. }
  3801. }
  3802. 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) {
  3803. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  3804. const dim3 num_blocks(block_num_x, ky, 1);
  3805. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  3806. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  3807. }
  3808. template<typename dst_t>
  3809. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3810. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3811. dequantize_block<QK4_0, QR4_0, dequantize_q4_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3812. }
  3813. template<typename dst_t>
  3814. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3815. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3816. dequantize_block<QK4_1, QR4_1, dequantize_q4_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3817. }
  3818. template<typename dst_t>
  3819. static void dequantize_row_q5_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3820. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3821. dequantize_block<QK5_0, QR5_0, dequantize_q5_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3822. }
  3823. template<typename dst_t>
  3824. static void dequantize_row_q5_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3825. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3826. dequantize_block<QK5_1, QR5_1, dequantize_q5_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3827. }
  3828. template<typename dst_t>
  3829. static void dequantize_row_q8_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3830. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3831. dequantize_block<QK8_0, QR8_0, dequantize_q8_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3832. }
  3833. template<typename dst_t>
  3834. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3835. const int nb = k / QK_K;
  3836. #if QK_K == 256
  3837. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  3838. #else
  3839. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  3840. #endif
  3841. }
  3842. template<typename dst_t>
  3843. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3844. const int nb = k / QK_K;
  3845. #if QK_K == 256
  3846. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  3847. #else
  3848. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  3849. #endif
  3850. }
  3851. template<typename dst_t>
  3852. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3853. const int nb = k / QK_K;
  3854. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  3855. }
  3856. template<typename dst_t>
  3857. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3858. const int nb = k / QK_K;
  3859. #if QK_K == 256
  3860. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  3861. #else
  3862. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  3863. #endif
  3864. }
  3865. template<typename dst_t>
  3866. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  3867. const int nb = k / QK_K;
  3868. #if QK_K == 256
  3869. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  3870. #else
  3871. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  3872. #endif
  3873. }
  3874. 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) {
  3875. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3876. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3877. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  3878. const dim3 block_nums(block_num_y, 1, 1);
  3879. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3880. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  3881. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3882. }
  3883. 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) {
  3884. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3885. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3886. const dim3 block_nums(block_num_y, 1, 1);
  3887. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3888. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  3889. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3890. }
  3891. 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) {
  3892. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3893. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3894. const dim3 block_nums(block_num_y, 1, 1);
  3895. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3896. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  3897. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3898. }
  3899. 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) {
  3900. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3901. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3902. const dim3 block_nums(block_num_y, 1, 1);
  3903. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3904. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  3905. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3906. }
  3907. 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) {
  3908. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3909. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3910. const dim3 block_nums(block_num_y, 1, 1);
  3911. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3912. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  3913. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3914. }
  3915. 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) {
  3916. GGML_ASSERT(ncols % QK_K == 0);
  3917. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  3918. const int block_num_y = (nrows + ny - 1) / ny;
  3919. const dim3 block_nums(block_num_y, 1, 1);
  3920. const dim3 block_dims(32, ny, 1);
  3921. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3922. }
  3923. 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) {
  3924. GGML_ASSERT(ncols % QK_K == 0);
  3925. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3926. const int block_num_y = (nrows + ny - 1) / ny;
  3927. const dim3 block_nums(block_num_y, 1, 1);
  3928. const dim3 block_dims(32, ny, 1);
  3929. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3930. }
  3931. 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) {
  3932. GGML_ASSERT(ncols % QK_K == 0);
  3933. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3934. const int block_num_y = (nrows + ny - 1) / ny;
  3935. const dim3 block_nums(block_num_y, 1, 1);
  3936. const dim3 block_dims(32, ny, 1);
  3937. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3938. }
  3939. 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) {
  3940. GGML_ASSERT(ncols % QK_K == 0);
  3941. const dim3 block_dims(32, 1, 1);
  3942. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  3943. }
  3944. 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) {
  3945. GGML_ASSERT(ncols % QK_K == 0);
  3946. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3947. const int block_num_y = (nrows + ny - 1) / ny;
  3948. const dim3 block_nums(block_num_y, 1, 1);
  3949. const dim3 block_dims(32, ny, 1);
  3950. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3951. }
  3952. 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) {
  3953. GGML_ASSERT(ncols % QK4_0 == 0);
  3954. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3955. const dim3 block_nums(block_num_y, 1, 1);
  3956. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3957. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  3958. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3959. }
  3960. 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) {
  3961. GGML_ASSERT(ncols % QK4_1 == 0);
  3962. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3963. const dim3 block_nums(block_num_y, 1, 1);
  3964. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3965. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  3966. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3967. }
  3968. 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) {
  3969. GGML_ASSERT(ncols % QK5_0 == 0);
  3970. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3971. const dim3 block_nums(block_num_y, 1, 1);
  3972. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3973. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  3974. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3975. }
  3976. 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) {
  3977. GGML_ASSERT(ncols % QK5_1 == 0);
  3978. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3979. const dim3 block_nums(block_num_y, 1, 1);
  3980. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3981. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  3982. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3983. }
  3984. 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) {
  3985. GGML_ASSERT(ncols % QK8_0 == 0);
  3986. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3987. const dim3 block_nums(block_num_y, 1, 1);
  3988. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3989. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  3990. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3991. }
  3992. 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) {
  3993. GGML_ASSERT(ncols % QK_K == 0);
  3994. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3995. const dim3 block_nums(block_num_y, 1, 1);
  3996. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3997. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  3998. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3999. }
  4000. 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) {
  4001. GGML_ASSERT(ncols % QK_K == 0);
  4002. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4003. const dim3 block_nums(block_num_y, 1, 1);
  4004. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4005. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4006. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4007. }
  4008. 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) {
  4009. GGML_ASSERT(ncols % QK_K == 0);
  4010. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4011. const dim3 block_nums(block_num_y, 1, 1);
  4012. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4013. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4014. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4015. }
  4016. 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) {
  4017. GGML_ASSERT(ncols % QK_K == 0);
  4018. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4019. const dim3 block_nums(block_num_y, 1, 1);
  4020. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4021. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4022. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4023. }
  4024. 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) {
  4025. GGML_ASSERT(ncols % QK_K == 0);
  4026. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4027. const dim3 block_nums(block_num_y, 1, 1);
  4028. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4029. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4030. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4031. }
  4032. static void convert_fp16_to_fp32_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  4033. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4034. dequantize_block<1, 1, convert_f16><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4035. }
  4036. static void convert_fp32_to_fp16_cuda(const void * vx, half * y, const int k, cudaStream_t stream) {
  4037. const int num_blocks = (k + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4038. dequantize_block<1, 1, convert_f32><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4039. }
  4040. 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) {
  4041. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4042. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4043. const dim3 block_nums(block_num_y, 1, 1);
  4044. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4045. dequantize_mul_mat_vec<1, 1, convert_f16>
  4046. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4047. }
  4048. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4049. switch (type) {
  4050. case GGML_TYPE_Q4_0:
  4051. return dequantize_row_q4_0_cuda;
  4052. case GGML_TYPE_Q4_1:
  4053. return dequantize_row_q4_1_cuda;
  4054. case GGML_TYPE_Q5_0:
  4055. return dequantize_row_q5_0_cuda;
  4056. case GGML_TYPE_Q5_1:
  4057. return dequantize_row_q5_1_cuda;
  4058. case GGML_TYPE_Q8_0:
  4059. return dequantize_row_q8_0_cuda;
  4060. case GGML_TYPE_Q2_K:
  4061. return dequantize_row_q2_K_cuda;
  4062. case GGML_TYPE_Q3_K:
  4063. return dequantize_row_q3_K_cuda;
  4064. case GGML_TYPE_Q4_K:
  4065. return dequantize_row_q4_K_cuda;
  4066. case GGML_TYPE_Q5_K:
  4067. return dequantize_row_q5_K_cuda;
  4068. case GGML_TYPE_Q6_K:
  4069. return dequantize_row_q6_K_cuda;
  4070. case GGML_TYPE_F32:
  4071. return convert_fp32_to_fp16_cuda;
  4072. default:
  4073. return nullptr;
  4074. }
  4075. }
  4076. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4077. switch (type) {
  4078. case GGML_TYPE_Q4_0:
  4079. return dequantize_row_q4_0_cuda;
  4080. case GGML_TYPE_Q4_1:
  4081. return dequantize_row_q4_1_cuda;
  4082. case GGML_TYPE_Q5_0:
  4083. return dequantize_row_q5_0_cuda;
  4084. case GGML_TYPE_Q5_1:
  4085. return dequantize_row_q5_1_cuda;
  4086. case GGML_TYPE_Q8_0:
  4087. return dequantize_row_q8_0_cuda;
  4088. case GGML_TYPE_Q2_K:
  4089. return dequantize_row_q2_K_cuda;
  4090. case GGML_TYPE_Q3_K:
  4091. return dequantize_row_q3_K_cuda;
  4092. case GGML_TYPE_Q4_K:
  4093. return dequantize_row_q4_K_cuda;
  4094. case GGML_TYPE_Q5_K:
  4095. return dequantize_row_q5_K_cuda;
  4096. case GGML_TYPE_Q6_K:
  4097. return dequantize_row_q6_K_cuda;
  4098. case GGML_TYPE_F16:
  4099. return convert_fp16_to_fp32_cuda;
  4100. default:
  4101. return nullptr;
  4102. }
  4103. }
  4104. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4105. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4106. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4107. int id;
  4108. CUDA_CHECK(cudaGetDevice(&id));
  4109. const int compute_capability = g_compute_capabilities[id];
  4110. int mmq_x, mmq_y, nwarps;
  4111. if (compute_capability >= CC_RDNA2) {
  4112. mmq_x = MMQ_X_Q4_0_RDNA2;
  4113. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4114. nwarps = NWARPS_Q4_0_RDNA2;
  4115. } else if (compute_capability >= CC_OFFSET_AMD) {
  4116. mmq_x = MMQ_X_Q4_0_RDNA1;
  4117. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4118. nwarps = NWARPS_Q4_0_RDNA1;
  4119. } else if (compute_capability >= CC_VOLTA) {
  4120. mmq_x = MMQ_X_Q4_0_AMPERE;
  4121. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4122. nwarps = NWARPS_Q4_0_AMPERE;
  4123. } else if (compute_capability >= MIN_CC_DP4A) {
  4124. mmq_x = MMQ_X_Q4_0_PASCAL;
  4125. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4126. nwarps = NWARPS_Q4_0_PASCAL;
  4127. } else {
  4128. GGML_ASSERT(false);
  4129. }
  4130. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4131. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4132. const dim3 block_nums(block_num_x, block_num_y, 1);
  4133. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4134. if (nrows_x % mmq_y == 0) {
  4135. const bool need_check = false;
  4136. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4137. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4138. } else {
  4139. const bool need_check = true;
  4140. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4141. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4142. }
  4143. }
  4144. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4145. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4146. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4147. int id;
  4148. CUDA_CHECK(cudaGetDevice(&id));
  4149. const int compute_capability = g_compute_capabilities[id];
  4150. int mmq_x, mmq_y, nwarps;
  4151. if (compute_capability >= CC_RDNA2) {
  4152. mmq_x = MMQ_X_Q4_1_RDNA2;
  4153. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4154. nwarps = NWARPS_Q4_1_RDNA2;
  4155. } else if (compute_capability >= CC_OFFSET_AMD) {
  4156. mmq_x = MMQ_X_Q4_1_RDNA1;
  4157. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4158. nwarps = NWARPS_Q4_1_RDNA1;
  4159. } else if (compute_capability >= CC_VOLTA) {
  4160. mmq_x = MMQ_X_Q4_1_AMPERE;
  4161. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4162. nwarps = NWARPS_Q4_1_AMPERE;
  4163. } else if (compute_capability >= MIN_CC_DP4A) {
  4164. mmq_x = MMQ_X_Q4_1_PASCAL;
  4165. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4166. nwarps = NWARPS_Q4_1_PASCAL;
  4167. } else {
  4168. GGML_ASSERT(false);
  4169. }
  4170. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4171. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4172. const dim3 block_nums(block_num_x, block_num_y, 1);
  4173. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4174. if (nrows_x % mmq_y == 0) {
  4175. const bool need_check = false;
  4176. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4177. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4178. } else {
  4179. const bool need_check = true;
  4180. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4181. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4182. }
  4183. }
  4184. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4185. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4186. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4187. int id;
  4188. CUDA_CHECK(cudaGetDevice(&id));
  4189. const int compute_capability = g_compute_capabilities[id];
  4190. int mmq_x, mmq_y, nwarps;
  4191. if (compute_capability >= CC_RDNA2) {
  4192. mmq_x = MMQ_X_Q5_0_RDNA2;
  4193. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4194. nwarps = NWARPS_Q5_0_RDNA2;
  4195. } else if (compute_capability >= CC_OFFSET_AMD) {
  4196. mmq_x = MMQ_X_Q5_0_RDNA1;
  4197. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4198. nwarps = NWARPS_Q5_0_RDNA1;
  4199. } else if (compute_capability >= CC_VOLTA) {
  4200. mmq_x = MMQ_X_Q5_0_AMPERE;
  4201. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4202. nwarps = NWARPS_Q5_0_AMPERE;
  4203. } else if (compute_capability >= MIN_CC_DP4A) {
  4204. mmq_x = MMQ_X_Q5_0_PASCAL;
  4205. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4206. nwarps = NWARPS_Q5_0_PASCAL;
  4207. } else {
  4208. GGML_ASSERT(false);
  4209. }
  4210. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4211. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4212. const dim3 block_nums(block_num_x, block_num_y, 1);
  4213. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4214. if (nrows_x % mmq_y == 0) {
  4215. const bool need_check = false;
  4216. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4217. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4218. } else {
  4219. const bool need_check = true;
  4220. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4221. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4222. }
  4223. }
  4224. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4225. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4226. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4227. int id;
  4228. CUDA_CHECK(cudaGetDevice(&id));
  4229. const int compute_capability = g_compute_capabilities[id];
  4230. int mmq_x, mmq_y, nwarps;
  4231. if (compute_capability >= CC_RDNA2) {
  4232. mmq_x = MMQ_X_Q5_1_RDNA2;
  4233. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4234. nwarps = NWARPS_Q5_1_RDNA2;
  4235. } else if (compute_capability >= CC_OFFSET_AMD) {
  4236. mmq_x = MMQ_X_Q5_1_RDNA1;
  4237. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4238. nwarps = NWARPS_Q5_1_RDNA1;
  4239. } else if (compute_capability >= CC_VOLTA) {
  4240. mmq_x = MMQ_X_Q5_1_AMPERE;
  4241. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4242. nwarps = NWARPS_Q5_1_AMPERE;
  4243. } else if (compute_capability >= MIN_CC_DP4A) {
  4244. mmq_x = MMQ_X_Q5_1_PASCAL;
  4245. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4246. nwarps = NWARPS_Q5_1_PASCAL;
  4247. } else {
  4248. GGML_ASSERT(false);
  4249. }
  4250. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4251. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4252. const dim3 block_nums(block_num_x, block_num_y, 1);
  4253. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4254. if (nrows_x % mmq_y == 0) {
  4255. const bool need_check = false;
  4256. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4257. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4258. } else {
  4259. const bool need_check = true;
  4260. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4261. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4262. }
  4263. }
  4264. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4265. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4266. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4267. int id;
  4268. CUDA_CHECK(cudaGetDevice(&id));
  4269. const int compute_capability = g_compute_capabilities[id];
  4270. int mmq_x, mmq_y, nwarps;
  4271. if (compute_capability >= CC_RDNA2) {
  4272. mmq_x = MMQ_X_Q8_0_RDNA2;
  4273. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4274. nwarps = NWARPS_Q8_0_RDNA2;
  4275. } else if (compute_capability >= CC_OFFSET_AMD) {
  4276. mmq_x = MMQ_X_Q8_0_RDNA1;
  4277. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4278. nwarps = NWARPS_Q8_0_RDNA1;
  4279. } else if (compute_capability >= CC_VOLTA) {
  4280. mmq_x = MMQ_X_Q8_0_AMPERE;
  4281. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4282. nwarps = NWARPS_Q8_0_AMPERE;
  4283. } else if (compute_capability >= MIN_CC_DP4A) {
  4284. mmq_x = MMQ_X_Q8_0_PASCAL;
  4285. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4286. nwarps = NWARPS_Q8_0_PASCAL;
  4287. } else {
  4288. GGML_ASSERT(false);
  4289. }
  4290. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4291. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4292. const dim3 block_nums(block_num_x, block_num_y, 1);
  4293. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4294. if (nrows_x % mmq_y == 0) {
  4295. const bool need_check = false;
  4296. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4297. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4298. } else {
  4299. const bool need_check = true;
  4300. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4301. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4302. }
  4303. }
  4304. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4305. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4306. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4307. int id;
  4308. CUDA_CHECK(cudaGetDevice(&id));
  4309. const int compute_capability = g_compute_capabilities[id];
  4310. int mmq_x, mmq_y, nwarps;
  4311. if (compute_capability >= CC_RDNA2) {
  4312. mmq_x = MMQ_X_Q2_K_RDNA2;
  4313. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4314. nwarps = NWARPS_Q2_K_RDNA2;
  4315. } else if (compute_capability >= CC_OFFSET_AMD) {
  4316. mmq_x = MMQ_X_Q2_K_RDNA1;
  4317. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4318. nwarps = NWARPS_Q2_K_RDNA1;
  4319. } else if (compute_capability >= CC_VOLTA) {
  4320. mmq_x = MMQ_X_Q2_K_AMPERE;
  4321. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4322. nwarps = NWARPS_Q2_K_AMPERE;
  4323. } else if (compute_capability >= MIN_CC_DP4A) {
  4324. mmq_x = MMQ_X_Q2_K_PASCAL;
  4325. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4326. nwarps = NWARPS_Q2_K_PASCAL;
  4327. } else {
  4328. GGML_ASSERT(false);
  4329. }
  4330. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4331. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4332. const dim3 block_nums(block_num_x, block_num_y, 1);
  4333. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4334. if (nrows_x % mmq_y == 0) {
  4335. const bool need_check = false;
  4336. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4337. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4338. } else {
  4339. const bool need_check = true;
  4340. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4341. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4342. }
  4343. }
  4344. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4345. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4346. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4347. #if QK_K == 256
  4348. int id;
  4349. CUDA_CHECK(cudaGetDevice(&id));
  4350. const int compute_capability = g_compute_capabilities[id];
  4351. int mmq_x, mmq_y, nwarps;
  4352. if (compute_capability >= CC_RDNA2) {
  4353. mmq_x = MMQ_X_Q3_K_RDNA2;
  4354. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4355. nwarps = NWARPS_Q3_K_RDNA2;
  4356. } else if (compute_capability >= CC_OFFSET_AMD) {
  4357. mmq_x = MMQ_X_Q3_K_RDNA1;
  4358. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4359. nwarps = NWARPS_Q3_K_RDNA1;
  4360. } else if (compute_capability >= CC_VOLTA) {
  4361. mmq_x = MMQ_X_Q3_K_AMPERE;
  4362. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4363. nwarps = NWARPS_Q3_K_AMPERE;
  4364. } else if (compute_capability >= MIN_CC_DP4A) {
  4365. mmq_x = MMQ_X_Q3_K_PASCAL;
  4366. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4367. nwarps = NWARPS_Q3_K_PASCAL;
  4368. } else {
  4369. GGML_ASSERT(false);
  4370. }
  4371. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4372. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4373. const dim3 block_nums(block_num_x, block_num_y, 1);
  4374. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4375. if (nrows_x % mmq_y == 0) {
  4376. const bool need_check = false;
  4377. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4378. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4379. } else {
  4380. const bool need_check = true;
  4381. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4382. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4383. }
  4384. #endif
  4385. }
  4386. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4387. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4388. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4389. int id;
  4390. CUDA_CHECK(cudaGetDevice(&id));
  4391. const int compute_capability = g_compute_capabilities[id];
  4392. int mmq_x, mmq_y, nwarps;
  4393. if (compute_capability >= CC_RDNA2) {
  4394. mmq_x = MMQ_X_Q4_K_RDNA2;
  4395. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4396. nwarps = NWARPS_Q4_K_RDNA2;
  4397. } else if (compute_capability >= CC_OFFSET_AMD) {
  4398. mmq_x = MMQ_X_Q4_K_RDNA1;
  4399. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4400. nwarps = NWARPS_Q4_K_RDNA1;
  4401. } else if (compute_capability >= CC_VOLTA) {
  4402. mmq_x = MMQ_X_Q4_K_AMPERE;
  4403. mmq_y = MMQ_Y_Q4_K_AMPERE;
  4404. nwarps = NWARPS_Q4_K_AMPERE;
  4405. } else if (compute_capability >= MIN_CC_DP4A) {
  4406. mmq_x = MMQ_X_Q4_K_PASCAL;
  4407. mmq_y = MMQ_Y_Q4_K_PASCAL;
  4408. nwarps = NWARPS_Q4_K_PASCAL;
  4409. } else {
  4410. GGML_ASSERT(false);
  4411. }
  4412. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4413. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4414. const dim3 block_nums(block_num_x, block_num_y, 1);
  4415. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4416. if (nrows_x % mmq_y == 0) {
  4417. const bool need_check = false;
  4418. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4419. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4420. } else {
  4421. const bool need_check = true;
  4422. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4423. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4424. }
  4425. }
  4426. static void ggml_mul_mat_q5_K_q8_1_cuda(
  4427. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4428. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4429. int id;
  4430. CUDA_CHECK(cudaGetDevice(&id));
  4431. const int compute_capability = g_compute_capabilities[id];
  4432. int mmq_x, mmq_y, nwarps;
  4433. if (compute_capability >= CC_RDNA2) {
  4434. mmq_x = MMQ_X_Q5_K_RDNA2;
  4435. mmq_y = MMQ_Y_Q5_K_RDNA2;
  4436. nwarps = NWARPS_Q5_K_RDNA2;
  4437. } else if (compute_capability >= CC_OFFSET_AMD) {
  4438. mmq_x = MMQ_X_Q5_K_RDNA1;
  4439. mmq_y = MMQ_Y_Q5_K_RDNA1;
  4440. nwarps = NWARPS_Q5_K_RDNA1;
  4441. } else if (compute_capability >= CC_VOLTA) {
  4442. mmq_x = MMQ_X_Q5_K_AMPERE;
  4443. mmq_y = MMQ_Y_Q5_K_AMPERE;
  4444. nwarps = NWARPS_Q5_K_AMPERE;
  4445. } else if (compute_capability >= MIN_CC_DP4A) {
  4446. mmq_x = MMQ_X_Q5_K_PASCAL;
  4447. mmq_y = MMQ_Y_Q5_K_PASCAL;
  4448. nwarps = NWARPS_Q5_K_PASCAL;
  4449. } else {
  4450. GGML_ASSERT(false);
  4451. }
  4452. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4453. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4454. const dim3 block_nums(block_num_x, block_num_y, 1);
  4455. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4456. if (nrows_x % mmq_y == 0) {
  4457. const bool need_check = false;
  4458. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4459. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4460. } else {
  4461. const bool need_check = true;
  4462. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4463. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4464. }
  4465. }
  4466. static void ggml_mul_mat_q6_K_q8_1_cuda(
  4467. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4468. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4469. int id;
  4470. CUDA_CHECK(cudaGetDevice(&id));
  4471. const int compute_capability = g_compute_capabilities[id];
  4472. int mmq_x, mmq_y, nwarps;
  4473. if (compute_capability >= CC_RDNA2) {
  4474. mmq_x = MMQ_X_Q6_K_RDNA2;
  4475. mmq_y = MMQ_Y_Q6_K_RDNA2;
  4476. nwarps = NWARPS_Q6_K_RDNA2;
  4477. } else if (compute_capability >= CC_OFFSET_AMD) {
  4478. mmq_x = MMQ_X_Q6_K_RDNA1;
  4479. mmq_y = MMQ_Y_Q6_K_RDNA1;
  4480. nwarps = NWARPS_Q6_K_RDNA1;
  4481. } else if (compute_capability >= CC_VOLTA) {
  4482. mmq_x = MMQ_X_Q6_K_AMPERE;
  4483. mmq_y = MMQ_Y_Q6_K_AMPERE;
  4484. nwarps = NWARPS_Q6_K_AMPERE;
  4485. } else if (compute_capability >= MIN_CC_DP4A) {
  4486. mmq_x = MMQ_X_Q6_K_PASCAL;
  4487. mmq_y = MMQ_Y_Q6_K_PASCAL;
  4488. nwarps = NWARPS_Q6_K_PASCAL;
  4489. } else {
  4490. GGML_ASSERT(false);
  4491. }
  4492. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4493. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4494. const dim3 block_nums(block_num_x, block_num_y, 1);
  4495. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4496. if (nrows_x % mmq_y == 0) {
  4497. const bool need_check = false;
  4498. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4499. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4500. } else {
  4501. const bool need_check = true;
  4502. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4503. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4504. }
  4505. }
  4506. static void ggml_mul_mat_p021_f16_f32_cuda(
  4507. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  4508. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  4509. const dim3 block_nums(1, nrows_x, nchannels_y);
  4510. const dim3 block_dims(WARP_SIZE, 1, 1);
  4511. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  4512. }
  4513. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  4514. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  4515. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  4516. const dim3 block_nums(1, nrows_x, nchannels_y);
  4517. const dim3 block_dims(WARP_SIZE, 1, 1);
  4518. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  4519. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  4520. }
  4521. static void ggml_cpy_f32_f32_cuda(
  4522. const char * cx, char * cdst, const int ne,
  4523. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4524. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  4525. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  4526. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  4527. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  4528. }
  4529. static void ggml_cpy_f32_f16_cuda(
  4530. const char * cx, char * cdst, const int ne,
  4531. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4532. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  4533. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  4534. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  4535. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  4536. }
  4537. static void ggml_cpy_f16_f16_cuda(
  4538. const char * cx, char * cdst, const int ne,
  4539. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4540. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  4541. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  4542. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  4543. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  4544. }
  4545. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  4546. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  4547. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  4548. }
  4549. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  4550. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  4551. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  4552. }
  4553. template<typename T>
  4554. static void rope_cuda(
  4555. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  4556. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  4557. ) {
  4558. GGML_ASSERT(ncols % 2 == 0);
  4559. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  4560. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  4561. const dim3 block_nums(nrows, num_blocks_x, 1);
  4562. if (pos == nullptr) {
  4563. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  4564. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  4565. );
  4566. } else {
  4567. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  4568. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  4569. );
  4570. }
  4571. }
  4572. template<typename T>
  4573. static void rope_neox_cuda(
  4574. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  4575. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  4576. ) {
  4577. GGML_ASSERT(ncols % 2 == 0);
  4578. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  4579. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  4580. const dim3 block_nums(nrows, num_blocks_x, 1);
  4581. if (pos == nullptr) {
  4582. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  4583. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  4584. );
  4585. } else {
  4586. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  4587. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  4588. );
  4589. }
  4590. }
  4591. static void rope_glm_f32_cuda(
  4592. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  4593. float freq_base, int n_ctx, cudaStream_t stream
  4594. ) {
  4595. GGML_ASSERT(ncols % 4 == 0);
  4596. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  4597. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  4598. const dim3 block_nums(num_blocks_x, nrows, 1);
  4599. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  4600. }
  4601. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  4602. const int k_rows, const int n_heads_log2_floor, const float m0,
  4603. const float m1, cudaStream_t stream) {
  4604. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  4605. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  4606. const dim3 block_nums(num_blocks_x, nrows, 1);
  4607. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  4608. }
  4609. 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) {
  4610. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  4611. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  4612. const dim3 block_nums(nrows_x, block_num_x, 1);
  4613. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  4614. }
  4615. static void soft_max_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, cudaStream_t stream) {
  4616. const dim3 block_dims(1, WARP_SIZE, 1);
  4617. const dim3 block_nums(nrows_x, 1, 1);
  4618. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x);
  4619. }
  4620. static void im2col_f32_f16_cuda(const float * x, half * dst,
  4621. int OH, int IW, int IH, int OW, int IC,
  4622. int KH, int KW, int N, int ofs0, int ofs1,
  4623. int s0, int s1, int p0, int p1, int d0, int d1, cudaStream_t stream) {
  4624. dim3 block_nums(IC, OH, OW);
  4625. dim3 block_dims(N, KH, KW);
  4626. im2col_f32_f16<<<block_nums, block_dims, 0, stream>>>(x, dst, ofs0, ofs1, IW, IH, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  4627. }
  4628. // buffer pool for cuda
  4629. #define MAX_CUDA_BUFFERS 256
  4630. struct scoped_spin_lock {
  4631. std::atomic_flag& lock;
  4632. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  4633. while (lock.test_and_set(std::memory_order_acquire)) {
  4634. ; // spin
  4635. }
  4636. }
  4637. ~scoped_spin_lock() {
  4638. lock.clear(std::memory_order_release);
  4639. }
  4640. scoped_spin_lock(const scoped_spin_lock&) = delete;
  4641. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  4642. };
  4643. struct cuda_buffer {
  4644. void * ptr = nullptr;
  4645. size_t size = 0;
  4646. };
  4647. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  4648. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  4649. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  4650. scoped_spin_lock lock(g_cuda_pool_lock);
  4651. int id;
  4652. CUDA_CHECK(cudaGetDevice(&id));
  4653. #ifdef DEBUG_CUDA_MALLOC
  4654. int nnz = 0;
  4655. size_t max_size = 0, tot_size = 0;
  4656. #endif
  4657. size_t best_diff = 1ull << 36;
  4658. int ibest = -1;
  4659. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  4660. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  4661. if (b.ptr != nullptr) {
  4662. #ifdef DEBUG_CUDA_MALLOC
  4663. ++nnz;
  4664. tot_size += b.size;
  4665. if (b.size > max_size) max_size = b.size;
  4666. #endif
  4667. if (b.size >= size) {
  4668. size_t diff = b.size - size;
  4669. if (diff < best_diff) {
  4670. best_diff = diff;
  4671. ibest = i;
  4672. if (!best_diff) {
  4673. void * ptr = b.ptr;
  4674. *actual_size = b.size;
  4675. b.ptr = nullptr;
  4676. b.size = 0;
  4677. return ptr;
  4678. }
  4679. }
  4680. }
  4681. }
  4682. }
  4683. if (ibest >= 0) {
  4684. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  4685. void * ptr = b.ptr;
  4686. *actual_size = b.size;
  4687. b.ptr = nullptr;
  4688. b.size = 0;
  4689. return ptr;
  4690. }
  4691. #ifdef DEBUG_CUDA_MALLOC
  4692. fprintf(stderr, "%s: %d buffers, max_size = %u MiB, tot_size = %u MiB, requested %u MiB\n", __func__, nnz,
  4693. (uint32_t)(max_size/1024/1024), (uint32_t)(tot_size/1024/1024), (uint32_t)(size/1024/1024));
  4694. #endif
  4695. void * ptr;
  4696. size_t look_ahead_size = (size_t) (1.05 * size);
  4697. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  4698. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  4699. *actual_size = look_ahead_size;
  4700. return ptr;
  4701. }
  4702. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  4703. scoped_spin_lock lock(g_cuda_pool_lock);
  4704. int id;
  4705. CUDA_CHECK(cudaGetDevice(&id));
  4706. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  4707. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  4708. if (b.ptr == nullptr) {
  4709. b.ptr = ptr;
  4710. b.size = size;
  4711. return;
  4712. }
  4713. }
  4714. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  4715. CUDA_CHECK(cudaFree(ptr));
  4716. }
  4717. static bool g_cublas_loaded = false;
  4718. bool ggml_cublas_loaded(void) {
  4719. return g_cublas_loaded;
  4720. }
  4721. void ggml_init_cublas() {
  4722. static bool initialized = false;
  4723. if (!initialized) {
  4724. #ifdef __HIP_PLATFORM_AMD__
  4725. // Workaround for a rocBLAS bug when using multiple graphics cards:
  4726. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  4727. rocblas_initialize();
  4728. CUDA_CHECK(cudaDeviceSynchronize());
  4729. #endif
  4730. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  4731. initialized = true;
  4732. g_cublas_loaded = false;
  4733. return;
  4734. }
  4735. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  4736. int64_t total_vram = 0;
  4737. #if defined(GGML_CUDA_FORCE_MMQ)
  4738. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  4739. #else
  4740. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  4741. #endif
  4742. #if defined(CUDA_USE_TENSOR_CORES)
  4743. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  4744. #else
  4745. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  4746. #endif
  4747. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  4748. for (int id = 0; id < g_device_count; ++id) {
  4749. cudaDeviceProp prop;
  4750. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  4751. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  4752. g_tensor_split[id] = total_vram;
  4753. total_vram += prop.totalGlobalMem;
  4754. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4755. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  4756. #else
  4757. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  4758. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4759. }
  4760. for (int id = 0; id < g_device_count; ++id) {
  4761. g_tensor_split[id] /= total_vram;
  4762. }
  4763. for (int id = 0; id < g_device_count; ++id) {
  4764. CUDA_CHECK(ggml_cuda_set_device(id));
  4765. // create cuda streams
  4766. for (int is = 0; is < MAX_STREAMS; ++is) {
  4767. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  4768. }
  4769. // create cublas handle
  4770. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  4771. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  4772. }
  4773. // configure logging to stdout
  4774. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  4775. initialized = true;
  4776. g_cublas_loaded = true;
  4777. }
  4778. }
  4779. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  4780. if (tensor_split == nullptr) {
  4781. return;
  4782. }
  4783. bool all_zero = true;
  4784. for (int i = 0; i < g_device_count; ++i) {
  4785. if (tensor_split[i] != 0.0f) {
  4786. all_zero = false;
  4787. break;
  4788. }
  4789. }
  4790. if (all_zero) {
  4791. return;
  4792. }
  4793. float split_sum = 0.0f;
  4794. for (int i = 0; i < g_device_count; ++i) {
  4795. g_tensor_split[i] = split_sum;
  4796. split_sum += tensor_split[i];
  4797. }
  4798. for (int i = 0; i < g_device_count; ++i) {
  4799. g_tensor_split[i] /= split_sum;
  4800. }
  4801. }
  4802. void * ggml_cuda_host_malloc(size_t size) {
  4803. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  4804. return nullptr;
  4805. }
  4806. void * ptr = nullptr;
  4807. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  4808. if (err != cudaSuccess) {
  4809. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  4810. // This can fixed the OOM error in WSL.
  4811. cudaGetLastError();
  4812. fprintf(stderr, "WARNING: failed to allocate %.2f MiB of pinned memory: %s\n",
  4813. size/1024.0/1024.0, cudaGetErrorString(err));
  4814. return nullptr;
  4815. }
  4816. return ptr;
  4817. }
  4818. void ggml_cuda_host_free(void * ptr) {
  4819. CUDA_CHECK(cudaFreeHost(ptr));
  4820. }
  4821. static cudaError_t ggml_cuda_cpy_tensor_2d(
  4822. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  4823. cudaMemcpyKind kind;
  4824. char * src_ptr;
  4825. if (src->backend == GGML_BACKEND_CPU) {
  4826. kind = cudaMemcpyHostToDevice;
  4827. src_ptr = (char *) src->data;
  4828. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  4829. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  4830. kind = cudaMemcpyDeviceToDevice;
  4831. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  4832. int id;
  4833. CUDA_CHECK(cudaGetDevice(&id));
  4834. src_ptr = (char *) extra->data_device[id];
  4835. } else {
  4836. GGML_ASSERT(false);
  4837. }
  4838. char * dst_ptr = (char *) dst;
  4839. const int64_t ne0 = src->ne[0];
  4840. const int64_t nb0 = src->nb[0];
  4841. const int64_t nb1 = src->nb[1];
  4842. const int64_t nb2 = src->nb[2];
  4843. const int64_t nb3 = src->nb[3];
  4844. const enum ggml_type type = src->type;
  4845. const int64_t ts = ggml_type_size(type);
  4846. const int64_t bs = ggml_blck_size(type);
  4847. int64_t i1_diff = i1_high - i1_low;
  4848. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  4849. if (nb0 == ts && nb1 == ts*ne0/bs) {
  4850. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  4851. } else if (nb0 == ts) {
  4852. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  4853. } else {
  4854. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  4855. const void * rx = (const void *) ((const char *) x + i1*nb1);
  4856. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  4857. // pretend the row is a matrix with cols=1
  4858. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  4859. if (r != cudaSuccess) return r;
  4860. }
  4861. return cudaSuccess;
  4862. }
  4863. }
  4864. static void ggml_cuda_op_repeat(
  4865. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4866. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & stream) {
  4867. // guaranteed to be an integer due to the check in ggml_can_repeat
  4868. const int64_t ne0 = dst->ne[0];
  4869. const int64_t ne1 = dst->ne[1];
  4870. const int64_t ne2 = dst->ne[2];
  4871. const int64_t ne3 = dst->ne[3];
  4872. const int64_t ne00 = src0->ne[0];
  4873. const int64_t ne01 = src0->ne[1];
  4874. const int64_t ne02 = src0->ne[2];
  4875. const int64_t ne03 = src0->ne[3];
  4876. const size_t nb0 = dst->nb[0];
  4877. const size_t nb1 = dst->nb[1];
  4878. const size_t nb2 = dst->nb[2];
  4879. const size_t nb3 = dst->nb[3];
  4880. const size_t nb00 = src0->nb[0];
  4881. const size_t nb01 = src0->nb[1];
  4882. const size_t nb02 = src0->nb[2];
  4883. const size_t nb03 = src0->nb[3];
  4884. const int nr0 = (int)(ne0/ne00);
  4885. const int nr1 = (int)(ne1/ne01);
  4886. const int nr2 = (int)(ne2/ne02);
  4887. const int nr3 = (int)(ne3/ne03);
  4888. // TODO: support for transposed / permuted tensors
  4889. GGML_ASSERT(nb0 == sizeof(float));
  4890. GGML_ASSERT(nb00 == sizeof(float));
  4891. // TODO: very inefficient, implement in a kernel, or fewer cudaMemcpyAsync calls for contiguous tensors
  4892. for (int i3 = 0; i3 < nr3; i3++) {
  4893. for (int k3 = 0; k3 < ne03; k3++) {
  4894. for (int i2 = 0; i2 < nr2; i2++) {
  4895. for (int k2 = 0; k2 < ne02; k2++) {
  4896. for (int i1 = 0; i1 < nr1; i1++) {
  4897. for (int k1 = 0; k1 < ne01; k1++) {
  4898. for (int i0 = 0; i0 < nr0; i0++) {
  4899. CUDA_CHECK(cudaMemcpyAsync(
  4900. (char *) dst_d + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0,
  4901. (const char *) src0_d + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01,
  4902. ne00*nb0, cudaMemcpyDeviceToDevice, stream));
  4903. }
  4904. }
  4905. }
  4906. }
  4907. }
  4908. }
  4909. }
  4910. (void) src1;
  4911. (void) src1_d;
  4912. }
  4913. static void ggml_cuda_op_get_rows(
  4914. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4915. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & stream) {
  4916. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4917. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  4918. GGML_ASSERT(ggml_is_contiguous(src0));
  4919. GGML_ASSERT(ggml_is_contiguous(src1));
  4920. GGML_ASSERT(ggml_is_contiguous(dst));
  4921. const int ncols = src0->ne[0];
  4922. const int nrows = ggml_nelements(src1);
  4923. const int32_t * src1_i32 = (const int32_t *) src1_d;
  4924. switch (src0->type) {
  4925. case GGML_TYPE_F16:
  4926. get_rows_cuda<1, 1, convert_f16>(src0_d, src1_i32, dst_d, nrows, ncols, stream);
  4927. break;
  4928. case GGML_TYPE_F32:
  4929. get_rows_cuda<1, 1, convert_f32>(src0_d, src1_i32, dst_d, nrows, ncols, stream);
  4930. break;
  4931. case GGML_TYPE_Q4_0:
  4932. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0_d, src1_i32, dst_d, nrows, ncols, stream);
  4933. break;
  4934. case GGML_TYPE_Q4_1:
  4935. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0_d, src1_i32, dst_d, nrows, ncols, stream);
  4936. break;
  4937. case GGML_TYPE_Q5_0:
  4938. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0_d, src1_i32, dst_d, nrows, ncols, stream);
  4939. break;
  4940. case GGML_TYPE_Q5_1:
  4941. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0_d, src1_i32, dst_d, nrows, ncols, stream);
  4942. break;
  4943. case GGML_TYPE_Q8_0:
  4944. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0_d, src1_i32, dst_d, nrows, ncols, stream);
  4945. break;
  4946. default:
  4947. // TODO: k-quants
  4948. GGML_ASSERT(false);
  4949. break;
  4950. }
  4951. }
  4952. inline void ggml_cuda_op_add(
  4953. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4954. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4955. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4956. const int64_t ne10 = src1->ne[0];
  4957. const int64_t ne11 = src1->ne[1];
  4958. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4959. add_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(src0), ne10*ne11, main_stream);
  4960. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4961. add_f16_f32_f16_cuda((const half *) src0_dd, src1_dd, (half *) dst_dd, ggml_nelements(src0), main_stream);
  4962. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  4963. add_f16_f32_f32_cuda((const half *) src0_dd, src1_dd, dst_dd, ggml_nelements(src0), main_stream);
  4964. } else {
  4965. fprintf(stderr, "src0->type: %d dst->type: %d\n", src0->type, dst->type);
  4966. GGML_ASSERT(false);
  4967. }
  4968. (void) src1;
  4969. (void) dst;
  4970. }
  4971. inline void ggml_cuda_op_mul(
  4972. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4973. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4974. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4975. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4976. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4977. const int64_t ne10 = src1->ne[0];
  4978. const int64_t ne11 = src1->ne[1];
  4979. mul_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(src0), ne10*ne11, main_stream);
  4980. (void) dst;
  4981. }
  4982. inline void ggml_cuda_op_gelu(
  4983. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4984. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4985. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4986. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4987. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  4988. (void) src1;
  4989. (void) dst;
  4990. (void) src1_dd;
  4991. }
  4992. inline void ggml_cuda_op_silu(
  4993. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4994. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4995. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4996. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4997. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  4998. (void) src1;
  4999. (void) dst;
  5000. (void) src1_dd;
  5001. }
  5002. inline void ggml_cuda_op_relu(
  5003. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5004. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5005. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5006. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5007. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5008. (void) src1;
  5009. (void) dst;
  5010. (void) src1_dd;
  5011. }
  5012. inline void ggml_cuda_op_sqr(
  5013. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5014. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5015. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5016. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5017. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5018. (void) src1;
  5019. (void) dst;
  5020. (void) src1_dd;
  5021. }
  5022. inline void ggml_cuda_op_norm(
  5023. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5024. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5025. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5026. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5027. const int64_t ne00 = src0->ne[0];
  5028. const int64_t nrows = ggml_nrows(src0);
  5029. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, main_stream);
  5030. (void) src1;
  5031. (void) dst;
  5032. (void) src1_dd;
  5033. }
  5034. inline void ggml_cuda_op_rms_norm(
  5035. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5036. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5037. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5038. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5039. const int64_t ne00 = src0->ne[0];
  5040. const int64_t nrows = ggml_nrows(src0);
  5041. float eps;
  5042. memcpy(&eps, dst->op_params, sizeof(float));
  5043. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5044. (void) src1;
  5045. (void) dst;
  5046. (void) src1_dd;
  5047. }
  5048. inline void ggml_cuda_op_mul_mat_q(
  5049. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5050. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5051. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5052. const int64_t ne00 = src0->ne[0];
  5053. const int64_t ne10 = src1->ne[0];
  5054. GGML_ASSERT(ne10 % QK8_1 == 0);
  5055. const int64_t ne0 = dst->ne[0];
  5056. const int64_t row_diff = row_high - row_low;
  5057. int id;
  5058. CUDA_CHECK(cudaGetDevice(&id));
  5059. // the main device has a larger memory buffer to hold the results from all GPUs
  5060. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5061. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5062. switch (src0->type) {
  5063. case GGML_TYPE_Q4_0:
  5064. 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);
  5065. break;
  5066. case GGML_TYPE_Q4_1:
  5067. 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);
  5068. break;
  5069. case GGML_TYPE_Q5_0:
  5070. 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);
  5071. break;
  5072. case GGML_TYPE_Q5_1:
  5073. 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);
  5074. break;
  5075. case GGML_TYPE_Q8_0:
  5076. 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);
  5077. break;
  5078. case GGML_TYPE_Q2_K:
  5079. 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);
  5080. break;
  5081. case GGML_TYPE_Q3_K:
  5082. 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);
  5083. break;
  5084. case GGML_TYPE_Q4_K:
  5085. 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);
  5086. break;
  5087. case GGML_TYPE_Q5_K:
  5088. 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);
  5089. break;
  5090. case GGML_TYPE_Q6_K:
  5091. 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);
  5092. break;
  5093. default:
  5094. GGML_ASSERT(false);
  5095. break;
  5096. }
  5097. (void) src1;
  5098. (void) dst;
  5099. (void) src1_ddf_i;
  5100. }
  5101. static int64_t get_row_rounding(ggml_type type) {
  5102. int64_t min_compute_capability = INT_MAX;
  5103. int64_t max_compute_capability = INT_MIN;
  5104. for (int64_t id = 0; id < g_device_count; ++id) {
  5105. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5106. if (min_compute_capability > g_compute_capabilities[id]) {
  5107. min_compute_capability = g_compute_capabilities[id];
  5108. }
  5109. if (max_compute_capability < g_compute_capabilities[id]) {
  5110. max_compute_capability = g_compute_capabilities[id];
  5111. }
  5112. }
  5113. }
  5114. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5115. switch(type) {
  5116. case GGML_TYPE_Q4_0:
  5117. case GGML_TYPE_Q4_1:
  5118. case GGML_TYPE_Q5_0:
  5119. case GGML_TYPE_Q5_1:
  5120. case GGML_TYPE_Q8_0:
  5121. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5122. case GGML_TYPE_F16:
  5123. case GGML_TYPE_F32:
  5124. return 1;
  5125. case GGML_TYPE_Q2_K:
  5126. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5127. case GGML_TYPE_Q3_K:
  5128. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5129. case GGML_TYPE_Q4_K:
  5130. case GGML_TYPE_Q5_K:
  5131. case GGML_TYPE_Q6_K:
  5132. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5133. default:
  5134. GGML_ASSERT(false);
  5135. }
  5136. #else
  5137. switch(type) {
  5138. case GGML_TYPE_Q4_0:
  5139. case GGML_TYPE_Q4_1:
  5140. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5141. case GGML_TYPE_Q5_0:
  5142. case GGML_TYPE_Q5_1:
  5143. case GGML_TYPE_Q8_0:
  5144. return 64;
  5145. case GGML_TYPE_F16:
  5146. case GGML_TYPE_F32:
  5147. return 1;
  5148. case GGML_TYPE_Q2_K:
  5149. case GGML_TYPE_Q3_K:
  5150. case GGML_TYPE_Q4_K:
  5151. case GGML_TYPE_Q5_K:
  5152. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5153. case GGML_TYPE_Q6_K:
  5154. return 64;
  5155. default:
  5156. GGML_ASSERT(false);
  5157. }
  5158. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5159. }
  5160. inline void ggml_cuda_op_mul_mat_vec_q(
  5161. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5162. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5163. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5164. const int64_t ne00 = src0->ne[0];
  5165. const int64_t row_diff = row_high - row_low;
  5166. switch (src0->type) {
  5167. case GGML_TYPE_Q4_0:
  5168. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5169. break;
  5170. case GGML_TYPE_Q4_1:
  5171. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5172. break;
  5173. case GGML_TYPE_Q5_0:
  5174. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5175. break;
  5176. case GGML_TYPE_Q5_1:
  5177. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5178. break;
  5179. case GGML_TYPE_Q8_0:
  5180. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5181. break;
  5182. case GGML_TYPE_Q2_K:
  5183. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5184. break;
  5185. case GGML_TYPE_Q3_K:
  5186. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5187. break;
  5188. case GGML_TYPE_Q4_K:
  5189. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5190. break;
  5191. case GGML_TYPE_Q5_K:
  5192. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5193. break;
  5194. case GGML_TYPE_Q6_K:
  5195. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5196. break;
  5197. default:
  5198. GGML_ASSERT(false);
  5199. break;
  5200. }
  5201. (void) src1;
  5202. (void) dst;
  5203. (void) src1_ddf_i;
  5204. (void) src1_ncols;
  5205. (void) src1_padded_row_size;
  5206. }
  5207. inline void ggml_cuda_op_dequantize_mul_mat_vec(
  5208. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5209. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5210. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5211. const int64_t ne00 = src0->ne[0];
  5212. const int64_t row_diff = row_high - row_low;
  5213. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  5214. #ifdef GGML_CUDA_F16
  5215. size_t ash;
  5216. dfloat * src1_dfloat = nullptr; // dfloat == half
  5217. bool src1_convert_f16 = src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  5218. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  5219. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  5220. if (src1_convert_f16) {
  5221. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  5222. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  5223. ne00, 1, sizeof(float), 0, 0,
  5224. ne00, 1, sizeof(half), 0, 0, stream);
  5225. }
  5226. #else
  5227. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  5228. #endif // GGML_CUDA_F16
  5229. switch (src0->type) {
  5230. case GGML_TYPE_Q4_0:
  5231. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5232. break;
  5233. case GGML_TYPE_Q4_1:
  5234. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5235. break;
  5236. case GGML_TYPE_Q5_0:
  5237. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5238. break;
  5239. case GGML_TYPE_Q5_1:
  5240. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5241. break;
  5242. case GGML_TYPE_Q8_0:
  5243. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5244. break;
  5245. case GGML_TYPE_Q2_K:
  5246. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5247. break;
  5248. case GGML_TYPE_Q3_K:
  5249. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5250. break;
  5251. case GGML_TYPE_Q4_K:
  5252. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5253. break;
  5254. case GGML_TYPE_Q5_K:
  5255. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5256. break;
  5257. case GGML_TYPE_Q6_K:
  5258. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5259. break;
  5260. case GGML_TYPE_F16:
  5261. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5262. break;
  5263. default:
  5264. GGML_ASSERT(false);
  5265. break;
  5266. }
  5267. #ifdef GGML_CUDA_F16
  5268. if (src1_convert_f16) {
  5269. ggml_cuda_pool_free(src1_dfloat, ash);
  5270. }
  5271. #endif // GGML_CUDA_F16
  5272. (void) src1;
  5273. (void) dst;
  5274. (void) src1_ddq_i;
  5275. (void) src1_ncols;
  5276. (void) src1_padded_row_size;
  5277. }
  5278. inline void ggml_cuda_op_mul_mat_cublas(
  5279. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5280. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5281. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5282. GGML_ASSERT(src0_dd_i != nullptr);
  5283. GGML_ASSERT(src1_ddf_i != nullptr);
  5284. GGML_ASSERT(dst_dd_i != nullptr);
  5285. const int64_t ne00 = src0->ne[0];
  5286. const int64_t ne10 = src1->ne[0];
  5287. const int64_t ne0 = dst->ne[0];
  5288. const int64_t row_diff = row_high - row_low;
  5289. int id;
  5290. CUDA_CHECK(cudaGetDevice(&id));
  5291. // the main device has a larger memory buffer to hold the results from all GPUs
  5292. // ldc == nrows of the matrix that cuBLAS writes into
  5293. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5294. const int compute_capability = g_compute_capabilities[id];
  5295. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1]) {
  5296. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  5297. half * src0_as_f16 = nullptr;
  5298. size_t src0_as = 0;
  5299. if (src0->type != GGML_TYPE_F16) {
  5300. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  5301. GGML_ASSERT(to_fp16_cuda != nullptr);
  5302. size_t ne = row_diff*ne00;
  5303. src0_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src0_as);
  5304. to_fp16_cuda(src0_dd_i, src0_as_f16, ne, stream);
  5305. }
  5306. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16;
  5307. half * src1_as_f16 = nullptr;
  5308. size_t src1_as = 0;
  5309. if (src1->type != GGML_TYPE_F16) {
  5310. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  5311. GGML_ASSERT(to_fp16_cuda != nullptr);
  5312. size_t ne = src1_ncols*ne10;
  5313. src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src1_as);
  5314. to_fp16_cuda(src1_ddf_i, src1_as_f16, ne, stream);
  5315. }
  5316. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16;
  5317. size_t dst_as = 0;
  5318. half * dst_f16 = (half *) ggml_cuda_pool_malloc(row_diff*src1_ncols * sizeof(half), &dst_as);
  5319. const half alpha_f16 = 1.0f;
  5320. const half beta_f16 = 0.0f;
  5321. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  5322. CUBLAS_CHECK(
  5323. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  5324. row_diff, src1_ncols, ne10,
  5325. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  5326. src1_ptr, CUDA_R_16F, ne10,
  5327. &beta_f16, dst_f16, CUDA_R_16F, ldc,
  5328. CUBLAS_COMPUTE_16F,
  5329. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  5330. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  5331. to_fp32_cuda(dst_f16, dst_dd_i, row_diff*src1_ncols, stream);
  5332. ggml_cuda_pool_free(dst_f16, dst_as);
  5333. if (src0_as != 0) {
  5334. ggml_cuda_pool_free(src0_as_f16, src0_as);
  5335. }
  5336. if (src1_as != 0) {
  5337. ggml_cuda_pool_free(src1_as_f16, src1_as);
  5338. }
  5339. }
  5340. else {
  5341. float * src0_ddq_as_f32 = nullptr;
  5342. size_t src0_as = 0;
  5343. if (src0->type != GGML_TYPE_F32) {
  5344. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  5345. GGML_ASSERT(to_fp32_cuda != nullptr);
  5346. src0_ddq_as_f32 = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_as); // NOLINT
  5347. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32, row_diff*ne00, stream);
  5348. }
  5349. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32;
  5350. const float alpha = 1.0f;
  5351. const float beta = 0.0f;
  5352. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  5353. CUBLAS_CHECK(
  5354. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  5355. row_diff, src1_ncols, ne10,
  5356. &alpha, src0_ddf_i, ne00,
  5357. src1_ddf_i, ne10,
  5358. &beta, dst_dd_i, ldc));
  5359. if (src0_as != 0) {
  5360. ggml_cuda_pool_free(src0_ddq_as_f32, src0_as);
  5361. }
  5362. }
  5363. (void) dst;
  5364. (void) src1_ddq_i;
  5365. (void) src1_padded_row_size;
  5366. }
  5367. inline void ggml_cuda_op_rope(
  5368. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5369. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5370. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  5371. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  5372. GGML_ASSERT(src0->type == dst->type);
  5373. const int64_t ne00 = src0->ne[0];
  5374. const int64_t ne01 = src0->ne[1];
  5375. const int64_t ne2 = dst->ne[2];
  5376. const int64_t nrows = ggml_nrows(src0);
  5377. //const int n_past = ((int32_t *) dst->op_params)[0];
  5378. const int n_dims = ((int32_t *) dst->op_params)[1];
  5379. const int mode = ((int32_t *) dst->op_params)[2];
  5380. const int n_ctx = ((int32_t *) dst->op_params)[3];
  5381. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  5382. // RoPE alteration for extended context
  5383. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  5384. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  5385. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  5386. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  5387. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  5388. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  5389. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  5390. const int32_t * pos = nullptr;
  5391. if ((mode & 1) == 0) {
  5392. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5393. GGML_ASSERT(src1->ne[0] == ne2);
  5394. pos = (const int32_t *) src1_dd;
  5395. }
  5396. const bool is_neox = mode & 2;
  5397. const bool is_glm = mode & 4;
  5398. rope_corr_dims corr_dims;
  5399. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  5400. // compute
  5401. if (is_glm) {
  5402. GGML_ASSERT(false);
  5403. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  5404. } else if (is_neox) {
  5405. GGML_ASSERT(ne00 == n_dims && "ne00 != n_dims is not implemented for CUDA yet");
  5406. if (src0->type == GGML_TYPE_F32) {
  5407. rope_neox_cuda(
  5408. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  5409. attn_factor, corr_dims, main_stream
  5410. );
  5411. } else if (src0->type == GGML_TYPE_F16) {
  5412. rope_neox_cuda(
  5413. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  5414. attn_factor, corr_dims, main_stream
  5415. );
  5416. } else {
  5417. GGML_ASSERT(false);
  5418. }
  5419. } else {
  5420. if (src0->type == GGML_TYPE_F32) {
  5421. rope_cuda(
  5422. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  5423. attn_factor, corr_dims, main_stream
  5424. );
  5425. } else if (src0->type == GGML_TYPE_F16) {
  5426. rope_cuda(
  5427. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  5428. attn_factor, corr_dims, main_stream
  5429. );
  5430. } else {
  5431. GGML_ASSERT(false);
  5432. }
  5433. }
  5434. (void) src1;
  5435. (void) dst;
  5436. (void) src1_dd;
  5437. }
  5438. inline void ggml_cuda_op_alibi(
  5439. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5440. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5441. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5442. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5443. const int64_t ne00 = src0->ne[0];
  5444. const int64_t ne01 = src0->ne[1];
  5445. const int64_t ne02 = src0->ne[2];
  5446. const int64_t nrows = ggml_nrows(src0);
  5447. //const int n_past = ((int32_t *) dst->op_params)[0];
  5448. const int n_head = ((int32_t *) dst->op_params)[1];
  5449. float max_bias;
  5450. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  5451. //GGML_ASSERT(ne01 + n_past == ne00);
  5452. GGML_ASSERT(n_head == ne02);
  5453. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  5454. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  5455. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  5456. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  5457. (void) src1;
  5458. (void) src1_dd;
  5459. }
  5460. inline void ggml_cuda_op_im2col(
  5461. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5462. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5463. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5464. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5465. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  5466. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  5467. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  5468. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  5469. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  5470. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  5471. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  5472. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  5473. const int64_t N = src1->ne[is_2D ? 3 : 2];
  5474. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  5475. const int64_t IH = is_2D ? src1->ne[1] : 1;
  5476. const int64_t IW = src1->ne[0];
  5477. const int64_t KH = is_2D ? src0->ne[1] : 1;
  5478. const int64_t KW = src0->ne[0];
  5479. const int64_t OH = is_2D ? dst->ne[2] : 1;
  5480. const int64_t OW = dst->ne[1];
  5481. const size_t ofs0 = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  5482. const size_t ofs1 = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  5483. im2col_f32_f16_cuda(src1_dd, (half*) dst_dd,
  5484. OH, IW, IH, OW, IC, KH, KW, N,
  5485. ofs0, ofs1, s0, s1, p0, p1, d0, d1, main_stream);
  5486. (void) src0;
  5487. (void) src0_dd;
  5488. }
  5489. inline void ggml_cuda_op_diag_mask_inf(
  5490. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5491. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5492. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5493. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5494. const int64_t ne00 = src0->ne[0];
  5495. const int64_t ne01 = src0->ne[1];
  5496. const int nrows0 = ggml_nrows(src0);
  5497. const int n_past = ((int32_t *) dst->op_params)[0];
  5498. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  5499. (void) src1;
  5500. (void) dst;
  5501. (void) src1_dd;
  5502. }
  5503. inline void ggml_cuda_op_soft_max(
  5504. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5505. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5506. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5507. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5508. const int64_t ne00 = src0->ne[0];
  5509. const int64_t nrows = ggml_nrows(src0);
  5510. soft_max_f32_cuda(src0_dd, dst_dd, ne00, nrows, main_stream);
  5511. (void) src1;
  5512. (void) dst;
  5513. (void) src1_dd;
  5514. }
  5515. inline void ggml_cuda_op_scale(
  5516. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5517. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5518. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5519. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5520. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5521. float scale;
  5522. // HACK: support for ggml backend interface
  5523. if (src1->backend == GGML_BACKEND_CPU) {
  5524. scale = ((float *) src1->data)[0];
  5525. } else {
  5526. // TODO: pass pointer to kernel instead of copying to host
  5527. CUDA_CHECK(cudaMemcpy(&scale, src1->data, sizeof(float), cudaMemcpyDeviceToHost));
  5528. }
  5529. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  5530. CUDA_CHECK(cudaGetLastError());
  5531. (void) src1;
  5532. (void) dst;
  5533. (void) src1_dd;
  5534. }
  5535. inline void ggml_cuda_op_clamp(
  5536. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5537. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5538. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5539. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5540. float min;
  5541. float max;
  5542. memcpy(&min, dst->op_params, sizeof(float));
  5543. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  5544. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  5545. CUDA_CHECK(cudaGetLastError());
  5546. (void) src1;
  5547. (void) dst;
  5548. (void) src1_dd;
  5549. }
  5550. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  5551. const int64_t nrows0 = ggml_nrows(src0);
  5552. const bool use_src1 = src1 != nullptr;
  5553. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  5554. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  5555. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  5556. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5557. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  5558. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5559. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  5560. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  5561. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  5562. const bool src1_stays_on_host = use_src1 && dst->op == GGML_OP_SCALE;
  5563. // dd = data device
  5564. float * src0_ddf = nullptr;
  5565. float * src1_ddf = nullptr;
  5566. float * dst_ddf = nullptr;
  5567. // as = actual size
  5568. size_t src0_asf = 0;
  5569. size_t src1_asf = 0;
  5570. size_t dst_asf = 0;
  5571. ggml_cuda_set_device(g_main_device);
  5572. const cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  5573. if (src0_on_device) {
  5574. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  5575. } else {
  5576. src0_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_asf);
  5577. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  5578. }
  5579. if (use_src1 && !src1_stays_on_host) {
  5580. if (src1_on_device) {
  5581. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  5582. } else {
  5583. src1_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf);
  5584. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  5585. }
  5586. }
  5587. if (dst_on_device) {
  5588. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  5589. } else {
  5590. dst_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(dst), &dst_asf);
  5591. }
  5592. // do the computation
  5593. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  5594. CUDA_CHECK(cudaGetLastError());
  5595. // copy dst to host if necessary
  5596. if (!dst_on_device) {
  5597. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  5598. }
  5599. if (src0_asf > 0) {
  5600. ggml_cuda_pool_free(src0_ddf, src0_asf);
  5601. }
  5602. if (src1_asf > 0) {
  5603. ggml_cuda_pool_free(src1_ddf, src1_asf);
  5604. }
  5605. if (dst_asf > 0) {
  5606. ggml_cuda_pool_free(dst_ddf, dst_asf);
  5607. }
  5608. if (dst->backend == GGML_BACKEND_CPU) {
  5609. CUDA_CHECK(cudaDeviceSynchronize());
  5610. }
  5611. }
  5612. static void ggml_cuda_set_peer_access(const int n_tokens) {
  5613. static bool peer_access_enabled = false;
  5614. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  5615. if (peer_access_enabled == enable_peer_access) {
  5616. return;
  5617. }
  5618. #ifdef NDEBUG
  5619. for (int id = 0; id < g_device_count; ++id) {
  5620. CUDA_CHECK(ggml_cuda_set_device(id));
  5621. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  5622. if (id == id_other) {
  5623. continue;
  5624. }
  5625. if (id != g_main_device && id_other != g_main_device) {
  5626. continue;
  5627. }
  5628. int can_access_peer;
  5629. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  5630. if (can_access_peer) {
  5631. if (enable_peer_access) {
  5632. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  5633. } else {
  5634. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  5635. }
  5636. }
  5637. }
  5638. }
  5639. #endif // NDEBUG
  5640. peer_access_enabled = enable_peer_access;
  5641. }
  5642. static void ggml_cuda_op_mul_mat(
  5643. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  5644. const bool convert_src1_to_q8_1) {
  5645. const int64_t ne00 = src0->ne[0];
  5646. const int64_t ne01 = src0->ne[1];
  5647. const int64_t ne02 = src0->ne[2];
  5648. const int64_t ne03 = src0->ne[3];
  5649. const int64_t nrows0 = ggml_nrows(src0);
  5650. const int64_t ne10 = src1->ne[0];
  5651. const int64_t ne11 = src1->ne[1];
  5652. const int64_t ne12 = src1->ne[2];
  5653. const int64_t ne13 = src1->ne[3];
  5654. const int64_t nrows1 = ggml_nrows(src1);
  5655. GGML_ASSERT(ne03 == ne13);
  5656. const int64_t ne0 = dst->ne[0];
  5657. const int64_t ne1 = dst->ne[1];
  5658. const int nb2 = dst->nb[2];
  5659. const int nb3 = dst->nb[3];
  5660. ggml_cuda_set_peer_access(ne11);
  5661. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  5662. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  5663. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  5664. const int64_t i02_divisor = ne12 / ne02;
  5665. const size_t src0_ts = ggml_type_size(src0->type);
  5666. const size_t src0_bs = ggml_blck_size(src0->type);
  5667. const size_t q8_1_ts = sizeof(block_q8_1);
  5668. const size_t q8_1_bs = QK8_1;
  5669. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5670. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5671. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5672. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  5673. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  5674. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  5675. const int64_t src1_padded_col_size = ne10 % MATRIX_ROW_PADDING == 0 ?
  5676. ne10 : ne10 - ne10 % MATRIX_ROW_PADDING + MATRIX_ROW_PADDING;
  5677. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  5678. GGML_ASSERT(!(split && ne02 > 1));
  5679. GGML_ASSERT(!(split && ne03 > 1));
  5680. GGML_ASSERT(!(split && ne02 < ne12));
  5681. // dd = data device
  5682. char * src0_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  5683. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  5684. char * src1_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // q8_1
  5685. float * dst_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  5686. // as = actual size
  5687. size_t src0_as[GGML_CUDA_MAX_DEVICES] = {0};
  5688. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  5689. size_t src1_asq[GGML_CUDA_MAX_DEVICES] = {0};
  5690. size_t dst_as[GGML_CUDA_MAX_DEVICES] = {0};
  5691. int64_t row_low[GGML_CUDA_MAX_DEVICES];
  5692. int64_t row_high[GGML_CUDA_MAX_DEVICES];
  5693. int used_devices = 0;
  5694. for (int64_t id = 0; id < g_device_count; ++id) {
  5695. // by default, use all rows
  5696. row_low[id] = 0;
  5697. row_high[id] = ne01;
  5698. // for multi GPU, get the row boundaries from tensor split
  5699. // and round to mul_mat_q tile sizes
  5700. if (split) {
  5701. const int64_t rounding = get_row_rounding(src0->type);
  5702. if (id != 0) {
  5703. row_low[id] = ne01*g_tensor_split[id];
  5704. row_low[id] -= row_low[id] % rounding;
  5705. }
  5706. if (id != g_device_count - 1) {
  5707. row_high[id] = ne01*g_tensor_split[id + 1];
  5708. row_high[id] -= row_high[id] % rounding;
  5709. }
  5710. }
  5711. }
  5712. for (int64_t id = 0; id < g_device_count; ++id) {
  5713. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  5714. continue;
  5715. }
  5716. used_devices++;
  5717. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  5718. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  5719. ggml_cuda_set_device(id);
  5720. const cudaStream_t stream = g_cudaStreams[id][0];
  5721. if (src0_on_device && src0_is_contiguous) {
  5722. src0_dd[id] = (char *) src0_extra->data_device[id];
  5723. } else {
  5724. const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  5725. src0_dd[id] = (char *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  5726. }
  5727. if (src1_on_device && src1_is_contiguous) {
  5728. src1_ddf[id] = (float *) src1_extra->data_device[id];
  5729. } else {
  5730. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  5731. }
  5732. if (convert_src1_to_q8_1) {
  5733. src1_ddq[id] = (char *) ggml_cuda_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  5734. if (src1_on_device && src1_is_contiguous) {
  5735. quantize_row_q8_1_cuda(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  5736. CUDA_CHECK(cudaGetLastError());
  5737. }
  5738. }
  5739. if (dst_on_device) {
  5740. dst_dd[id] = (float *) dst_extra->data_device[id];
  5741. } else {
  5742. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  5743. dst_dd[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_as[id]);
  5744. }
  5745. }
  5746. // if multiple devices are used they need to wait for the main device
  5747. // here an event is recorded that signals that the main device has finished calculating the input data
  5748. if (split && used_devices > 1) {
  5749. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5750. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  5751. }
  5752. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  5753. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  5754. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  5755. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  5756. for (int64_t id = 0; id < g_device_count; ++id) {
  5757. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  5758. continue;
  5759. }
  5760. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  5761. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  5762. const int64_t row_diff = row_high[id] - row_low[id];
  5763. ggml_cuda_set_device(id);
  5764. const cudaStream_t stream = g_cudaStreams[id][is];
  5765. // wait for main GPU data if necessary
  5766. if (split && (id != g_main_device || is != 0)) {
  5767. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  5768. }
  5769. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  5770. const int64_t i03 = i0 / ne12;
  5771. const int64_t i02 = i0 % ne12;
  5772. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  5773. // for split tensors the data begins at i0 == i0_offset_low
  5774. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * ne01*ne00*src0_ts/src0_bs;
  5775. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  5776. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  5777. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  5778. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  5779. // in that case an offset on dst_ddf_i is needed
  5780. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  5781. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  5782. }
  5783. // copy src0, src1 to device if necessary
  5784. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  5785. if (id != g_main_device) {
  5786. if (convert_src1_to_q8_1) {
  5787. char * src1_ddq_i_source = src1_ddq[g_main_device] + src1_ddq_i_offset;
  5788. CUDA_CHECK(cudaMemcpyAsync(src1_ddq_i, src1_ddq_i_source, src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs,
  5789. cudaMemcpyDeviceToDevice, stream));
  5790. } else {
  5791. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  5792. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  5793. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_ncols*ne10*sizeof(float),
  5794. cudaMemcpyDeviceToDevice, stream));
  5795. }
  5796. }
  5797. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  5798. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  5799. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  5800. } else {
  5801. GGML_ASSERT(false);
  5802. }
  5803. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  5804. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  5805. CUDA_CHECK(cudaGetLastError());
  5806. }
  5807. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  5808. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  5809. }
  5810. // do the computation
  5811. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  5812. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  5813. CUDA_CHECK(cudaGetLastError());
  5814. // copy dst to host or other device if necessary
  5815. if (!dst_on_device) {
  5816. void * dst_off_device;
  5817. cudaMemcpyKind kind;
  5818. if (dst->backend == GGML_BACKEND_CPU) {
  5819. dst_off_device = dst->data;
  5820. kind = cudaMemcpyDeviceToHost;
  5821. } else if (dst->backend == GGML_BACKEND_GPU) {
  5822. dst_off_device = dst_extra->data_device[g_main_device];
  5823. kind = cudaMemcpyDeviceToDevice;
  5824. } else {
  5825. GGML_ASSERT(false);
  5826. }
  5827. if (split) {
  5828. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  5829. // dst is NOT transposed.
  5830. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  5831. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  5832. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  5833. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  5834. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  5835. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  5836. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_dd_i, row_diff*sizeof(float),
  5837. row_diff*sizeof(float), src1_ncols, kind, stream));
  5838. } else {
  5839. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  5840. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  5841. dhf_dst_i += src1_col_0*ne0;
  5842. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  5843. }
  5844. }
  5845. // add event for the main device to wait on until other device is done
  5846. if (split && (id != g_main_device || is != 0)) {
  5847. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  5848. }
  5849. }
  5850. }
  5851. }
  5852. for (int64_t id = 0; id < g_device_count; ++id) {
  5853. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  5854. continue;
  5855. }
  5856. CUDA_CHECK(ggml_cuda_set_device(id));
  5857. // free buffers again when done
  5858. if (src0_as[id] > 0) {
  5859. ggml_cuda_pool_free(src0_dd[id], src0_as[id]);
  5860. }
  5861. if (src1_asf[id] > 0) {
  5862. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  5863. }
  5864. if (src1_asq[id] > 0) {
  5865. ggml_cuda_pool_free(src1_ddq[id], src1_asq[id]);
  5866. }
  5867. if (dst_as[id] > 0) {
  5868. ggml_cuda_pool_free(dst_dd[id], dst_as[id]);
  5869. }
  5870. }
  5871. // main device waits for all other devices to be finished
  5872. if (split && g_device_count > 1) {
  5873. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  5874. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  5875. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5876. for (int64_t id = 0; id < g_device_count; ++id) {
  5877. if (row_low[id] == row_high[id]) {
  5878. continue;
  5879. }
  5880. for (int64_t is = 0; is < is_max; ++is) {
  5881. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  5882. }
  5883. }
  5884. }
  5885. if (dst->backend == GGML_BACKEND_CPU) {
  5886. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5887. CUDA_CHECK(cudaDeviceSynchronize());
  5888. }
  5889. }
  5890. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5891. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  5892. }
  5893. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5894. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  5895. }
  5896. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5897. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  5898. }
  5899. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5900. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  5901. }
  5902. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5903. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  5904. }
  5905. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5906. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  5907. }
  5908. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5909. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  5910. }
  5911. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5912. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  5913. }
  5914. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5915. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  5916. }
  5917. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5918. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  5919. }
  5920. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  5921. if (!g_cublas_loaded) return false;
  5922. const int64_t ne10 = src1->ne[0];
  5923. const int64_t ne0 = dst->ne[0];
  5924. const int64_t ne1 = dst->ne[1];
  5925. // TODO: find the optimal values for these
  5926. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  5927. src1->type == GGML_TYPE_F32 &&
  5928. dst->type == GGML_TYPE_F32 &&
  5929. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  5930. }
  5931. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  5932. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  5933. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  5934. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  5935. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  5936. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5937. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5938. const int64_t ne00 = src0->ne[0];
  5939. const int64_t ne01 = src0->ne[1];
  5940. const int64_t ne02 = src0->ne[2];
  5941. const int64_t ne12 = src1->ne[2];
  5942. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5943. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  5944. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5945. void * src0_ddq = src0_extra->data_device[g_main_device];
  5946. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5947. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  5948. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5949. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  5950. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  5951. }
  5952. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  5953. GGML_ASSERT(!ggml_is_transposed(src0));
  5954. GGML_ASSERT(!ggml_is_transposed(src1));
  5955. GGML_ASSERT(!ggml_is_permuted(src0));
  5956. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  5957. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5958. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5959. const int64_t ne00 = src0->ne[0];
  5960. const int64_t ne01 = src0->ne[1];
  5961. const int64_t ne02 = src0->ne[2];
  5962. const int64_t nb01 = src0->nb[1];
  5963. const int64_t nb02 = src0->nb[2];
  5964. const int64_t ne12 = src1->ne[2];
  5965. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5966. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  5967. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5968. void * src0_ddq = src0_extra->data_device[g_main_device];
  5969. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5970. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  5971. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5972. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  5973. const int64_t row_stride_x = nb01 / sizeof(half);
  5974. const int64_t channel_stride_x = nb02 / sizeof(half);
  5975. 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);
  5976. }
  5977. __global__ void k_compute_batched_ptrs(
  5978. const half * src0_as_f16, const half * src1_as_f16, half * dst_f16,
  5979. const void ** ptrs_src, void ** ptrs_dst,
  5980. int ne12, int ne13,
  5981. int ne23,
  5982. int nb02, int nb03,
  5983. int nb12, int nb13,
  5984. int nb2, int nb3,
  5985. int r2, int r3) {
  5986. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  5987. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  5988. if (i13 >= ne13 || i12 >= ne12) {
  5989. return;
  5990. }
  5991. int i03 = i13 / r3;
  5992. int i02 = i12 / r2;
  5993. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  5994. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  5995. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  5996. }
  5997. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5998. GGML_ASSERT(!ggml_is_transposed(src0));
  5999. GGML_ASSERT(!ggml_is_transposed(src1));
  6000. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6001. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6002. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6003. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  6004. const int64_t ne01 = src0->ne[1];
  6005. const int64_t ne02 = src0->ne[2];
  6006. const int64_t ne03 = src0->ne[3];
  6007. const int64_t nb01 = src0->nb[1];
  6008. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  6009. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  6010. const int64_t ne10 = src1->ne[0];
  6011. const int64_t ne11 = src1->ne[1];
  6012. const int64_t ne12 = src1->ne[2];
  6013. const int64_t ne13 = src1->ne[3];
  6014. const int64_t nb11 = src1->nb[1];
  6015. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6016. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6017. const int64_t ne1 = ggml_nelements(src1);
  6018. const int64_t ne = ggml_nelements(dst);
  6019. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6020. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6021. int id;
  6022. CUDA_CHECK(cudaGetDevice(&id));
  6023. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], main_stream));
  6024. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6025. void * src0_ddq = src0_extra->data_device[g_main_device];
  6026. half * src0_as_f16 = (half *) src0_ddq;
  6027. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6028. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6029. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6030. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6031. // convert src1 to fp16
  6032. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6033. GGML_ASSERT(to_fp16_cuda != nullptr);
  6034. size_t src1_as = 0;
  6035. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  6036. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  6037. size_t dst_as = 0;
  6038. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  6039. GGML_ASSERT(ne12 % ne02 == 0);
  6040. GGML_ASSERT(ne13 % ne03 == 0);
  6041. // broadcast factors
  6042. const int64_t r2 = ne12/ne02;
  6043. const int64_t r3 = ne13/ne03;
  6044. const half alpha_f16 = 1.0f;
  6045. const half beta_f16 = 0.0f;
  6046. #if 0
  6047. // use cublasGemmEx
  6048. {
  6049. for (int i13 = 0; i13 < ne13; ++i13) {
  6050. for (int i12 = 0; i12 < ne12; ++i12) {
  6051. int i03 = i13 / r3;
  6052. int i02 = i12 / r2;
  6053. CUBLAS_CHECK(
  6054. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6055. ne01, ne11, ne10,
  6056. &alpha_f16, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6057. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6058. &beta_f16, ( char *) dst_f16 + i12* dst->nb[2]/2 + i13* dst->nb[3]/2, CUDA_R_16F, ne01,
  6059. CUBLAS_COMPUTE_16F,
  6060. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6061. }
  6062. }
  6063. }
  6064. #else
  6065. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6066. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6067. // use cublasGemmStridedBatchedEx
  6068. CUBLAS_CHECK(
  6069. cublasGemmStridedBatchedEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6070. ne01, ne11, ne10,
  6071. &alpha_f16, (const char *) src0_as_f16, CUDA_R_16F, nb01/sizeof(half), src0->nb[2]/sizeof(half), // strideA
  6072. (const char *) src1_as_f16, CUDA_R_16F, nb11/sizeof(float), src1->nb[2]/sizeof(float), // strideB
  6073. &beta_f16, ( char *) dst_f16, CUDA_R_16F, ne01, dst->nb[2]/sizeof(float), // strideC
  6074. ne12*ne13,
  6075. CUBLAS_COMPUTE_16F,
  6076. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6077. } else {
  6078. // use cublasGemmBatchedEx
  6079. const int ne23 = ne12*ne13;
  6080. const void ** ptrs_src = nullptr;
  6081. void ** ptrs_dst = nullptr;
  6082. size_t ptrs_src_s = 0;
  6083. size_t ptrs_dst_s = 0;
  6084. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  6085. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  6086. dim3 block_dims(ne13, ne12);
  6087. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6088. src0_as_f16, src1_as_f16, dst_f16,
  6089. ptrs_src, ptrs_dst,
  6090. ne12, ne13,
  6091. ne23,
  6092. nb02, nb03,
  6093. nb12, nb13,
  6094. dst->nb[2], dst->nb[3],
  6095. r2, r3);
  6096. CUDA_CHECK(cudaGetLastError());
  6097. CUBLAS_CHECK(
  6098. cublasGemmBatchedEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6099. ne01, ne11, ne10,
  6100. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, nb01/sizeof(half),
  6101. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, nb11/sizeof(float),
  6102. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  6103. ne23,
  6104. CUBLAS_COMPUTE_16F,
  6105. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6106. if (ptrs_src_s != 0) {
  6107. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  6108. }
  6109. if (ptrs_dst_s != 0) {
  6110. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  6111. }
  6112. }
  6113. #endif
  6114. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6115. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  6116. ggml_cuda_pool_free(src1_as_f16, src1_as);
  6117. ggml_cuda_pool_free(dst_f16, dst_as);
  6118. }
  6119. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6120. const bool all_on_device =
  6121. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6122. (src1->backend == GGML_BACKEND_GPU) &&
  6123. ( dst->backend == GGML_BACKEND_GPU);
  6124. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6125. int64_t min_compute_capability = INT_MAX;
  6126. for (int64_t id = 0; id < g_device_count; ++id) {
  6127. if (min_compute_capability > g_compute_capabilities[id] && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6128. min_compute_capability = g_compute_capabilities[id];
  6129. }
  6130. }
  6131. #ifdef CUDA_USE_TENSOR_CORES
  6132. const bool use_tensor_cores = true;
  6133. #else
  6134. const bool use_tensor_cores = false;
  6135. #endif
  6136. // debug helpers
  6137. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  6138. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  6139. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  6140. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  6141. //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);
  6142. //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);
  6143. if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  6144. // KQ single-batch
  6145. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  6146. } else if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  6147. // KQV single-batch
  6148. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  6149. } else if (!split && all_on_device && use_tensor_cores && src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  6150. // KQ + KQV multi-batch
  6151. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  6152. } else if (src0->type == GGML_TYPE_F32) {
  6153. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6154. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  6155. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  6156. #ifdef GGML_CUDA_FORCE_DMMV
  6157. const bool use_mul_mat_vec_q = false;
  6158. #else
  6159. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  6160. #endif // GGML_CUDA_FORCE_DMMV
  6161. if (use_mul_mat_vec_q) {
  6162. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  6163. } else {
  6164. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  6165. }
  6166. } else {
  6167. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  6168. // when tensor cores are available, use them for large batch size
  6169. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  6170. if (use_tensor_cores && min_compute_capability >= CC_VOLTA && src1->ne[1] > MMQ_MAX_BATCH_SIZE) {
  6171. use_mul_mat_q = false;
  6172. }
  6173. if (use_mul_mat_q) {
  6174. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  6175. } else {
  6176. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6177. }
  6178. }
  6179. } else {
  6180. GGML_ASSERT(false);
  6181. }
  6182. }
  6183. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6184. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  6185. }
  6186. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6187. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  6188. }
  6189. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6190. const int64_t ne = ggml_nelements(src0);
  6191. GGML_ASSERT(ne == ggml_nelements(src1));
  6192. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  6193. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  6194. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  6195. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  6196. const int64_t ne00 = src0->ne[0];
  6197. const int64_t ne01 = src0->ne[1];
  6198. GGML_ASSERT(src0->ne[3] == 1);
  6199. const int64_t nb00 = src0->nb[0];
  6200. const int64_t nb01 = src0->nb[1];
  6201. const int64_t nb02 = src0->nb[2];
  6202. const int64_t ne10 = src1->ne[0];
  6203. const int64_t ne11 = src1->ne[1];
  6204. GGML_ASSERT(src1->ne[3] == 1);
  6205. const int64_t nb10 = src1->nb[0];
  6206. const int64_t nb11 = src1->nb[1];
  6207. const int64_t nb12 = src1->nb[2];
  6208. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6209. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6210. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6211. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6212. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  6213. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  6214. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  6215. ggml_cpy_f32_f32_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  6216. ne10, ne11, nb10, nb11, nb12, main_stream);
  6217. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  6218. ggml_cpy_f32_f16_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  6219. ne10, ne11, nb10, nb11, nb12, main_stream);
  6220. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  6221. ggml_cpy_f16_f16_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  6222. ne10, ne11, nb10, nb11, nb12, main_stream);
  6223. } else {
  6224. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  6225. ggml_type_name(src0->type), ggml_type_name(src1->type));
  6226. GGML_ASSERT(false);
  6227. }
  6228. (void) dst;
  6229. }
  6230. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6231. ggml_cuda_cpy(src0, dst, nullptr);
  6232. (void) src1;
  6233. }
  6234. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6235. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  6236. }
  6237. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6238. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  6239. }
  6240. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6241. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  6242. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  6243. }
  6244. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6245. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  6246. }
  6247. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6248. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  6249. }
  6250. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6251. (void) src0;
  6252. (void) src1;
  6253. (void) dst;
  6254. }
  6255. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  6256. const int64_t nrows = ggml_nrows(tensor);
  6257. const int64_t ne0 = tensor->ne[0];
  6258. const size_t nb1 = tensor->nb[1];
  6259. ggml_backend_type backend = tensor->backend;
  6260. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  6261. memset(extra, 0, sizeof(*extra));
  6262. for (int64_t id = 0; id < g_device_count; ++id) {
  6263. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  6264. continue;
  6265. }
  6266. ggml_cuda_set_device(id);
  6267. int64_t row_low, row_high;
  6268. if (backend == GGML_BACKEND_GPU) {
  6269. row_low = 0;
  6270. row_high = nrows;
  6271. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  6272. const int64_t rounding = get_row_rounding(tensor->type);
  6273. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  6274. row_low -= row_low % rounding;
  6275. if (id == g_device_count - 1) {
  6276. row_high = nrows;
  6277. } else {
  6278. row_high = nrows*g_tensor_split[id + 1];
  6279. row_high -= row_high % rounding;
  6280. }
  6281. } else {
  6282. GGML_ASSERT(false);
  6283. }
  6284. if (row_low == row_high) {
  6285. continue;
  6286. }
  6287. int64_t nrows_split = row_high - row_low;
  6288. const size_t offset_split = row_low*nb1;
  6289. size_t size = ggml_nbytes_split(tensor, nrows_split);
  6290. const size_t original_size = size;
  6291. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  6292. if (ne0 % MATRIX_ROW_PADDING != 0) {
  6293. size += (MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING)
  6294. * ggml_type_size(tensor->type)/ggml_blck_size(tensor->type);
  6295. }
  6296. char * buf;
  6297. CUDA_CHECK(cudaMalloc(&buf, size));
  6298. char * buf_host = (char*)data + offset_split;
  6299. // set padding to 0 to avoid possible NaN values
  6300. if (size > original_size) {
  6301. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  6302. }
  6303. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  6304. extra->data_device[id] = buf;
  6305. if (backend == GGML_BACKEND_GPU_SPLIT) {
  6306. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  6307. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  6308. }
  6309. }
  6310. }
  6311. tensor->extra = extra;
  6312. }
  6313. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  6314. if (!tensor || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  6315. return;
  6316. }
  6317. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  6318. for (int64_t id = 0; id < g_device_count; ++id) {
  6319. if (extra->data_device[id] != nullptr) {
  6320. CUDA_CHECK(ggml_cuda_set_device(id));
  6321. CUDA_CHECK(cudaFree(extra->data_device[id]));
  6322. }
  6323. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  6324. if (extra->events[id][is] != nullptr) {
  6325. CUDA_CHECK(ggml_cuda_set_device(id));
  6326. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  6327. }
  6328. }
  6329. }
  6330. delete extra;
  6331. }
  6332. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  6333. static size_t g_temp_tensor_extra_index = 0;
  6334. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  6335. if (g_temp_tensor_extras == nullptr) {
  6336. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  6337. }
  6338. size_t alloc_index = g_temp_tensor_extra_index;
  6339. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  6340. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  6341. memset(extra, 0, sizeof(*extra));
  6342. return extra;
  6343. }
  6344. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  6345. if (scratch && g_scratch_size == 0) {
  6346. return;
  6347. }
  6348. tensor->backend = GGML_BACKEND_GPU;
  6349. // recursively assign CUDA buffers until a compute tensor is found
  6350. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  6351. const ggml_op src0_op = tensor->src[0]->op;
  6352. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  6353. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  6354. }
  6355. }
  6356. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  6357. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  6358. }
  6359. if (scratch && no_alloc) {
  6360. return;
  6361. }
  6362. ggml_tensor_extra_gpu * extra;
  6363. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  6364. tensor->op == GGML_OP_VIEW ||
  6365. force_inplace;
  6366. const size_t size = ggml_nbytes(tensor);
  6367. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6368. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  6369. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  6370. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  6371. size_t offset = 0;
  6372. if (tensor->op == GGML_OP_VIEW) {
  6373. memcpy(&offset, tensor->op_params, sizeof(size_t));
  6374. }
  6375. extra = ggml_cuda_alloc_temp_tensor_extra();
  6376. extra->data_device[g_main_device] = src0_ddc + offset;
  6377. } else if (tensor->op == GGML_OP_CPY) {
  6378. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  6379. void * src1_ddv = src1_extra->data_device[g_main_device];
  6380. extra = ggml_cuda_alloc_temp_tensor_extra();
  6381. extra->data_device[g_main_device] = src1_ddv;
  6382. } else if (scratch) {
  6383. GGML_ASSERT(size <= g_scratch_size);
  6384. if (g_scratch_offset + size > g_scratch_size) {
  6385. g_scratch_offset = 0;
  6386. }
  6387. char * data = (char *) g_scratch_buffer;
  6388. if (data == nullptr) {
  6389. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  6390. g_scratch_buffer = data;
  6391. }
  6392. extra = ggml_cuda_alloc_temp_tensor_extra();
  6393. extra->data_device[g_main_device] = data + g_scratch_offset;
  6394. g_scratch_offset += size;
  6395. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  6396. } else { // allocate new buffers outside of scratch
  6397. void * data;
  6398. CUDA_CHECK(cudaMalloc(&data, size));
  6399. CUDA_CHECK(cudaMemset(data, 0, size));
  6400. extra = new ggml_tensor_extra_gpu;
  6401. memset(extra, 0, sizeof(*extra));
  6402. extra->data_device[g_main_device] = data;
  6403. }
  6404. tensor->extra = extra;
  6405. }
  6406. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  6407. if (g_scratch_size == 0) {
  6408. return;
  6409. }
  6410. if (g_scratch_buffer == nullptr) {
  6411. ggml_cuda_set_device(g_main_device);
  6412. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  6413. }
  6414. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  6415. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  6416. tensor->op == GGML_OP_VIEW;
  6417. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  6418. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  6419. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  6420. size_t view_offset = 0;
  6421. if (tensor->op == GGML_OP_VIEW) {
  6422. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  6423. }
  6424. extra->data_device[g_main_device] = src0_ddc + view_offset;
  6425. } else {
  6426. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  6427. }
  6428. tensor->extra = extra;
  6429. }
  6430. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  6431. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  6432. GGML_ASSERT(ggml_is_contiguous(tensor));
  6433. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  6434. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6435. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  6436. }
  6437. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  6438. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  6439. }
  6440. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  6441. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  6442. }
  6443. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  6444. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  6445. }
  6446. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  6447. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  6448. }
  6449. void ggml_cuda_set_main_device(const int main_device) {
  6450. if (main_device >= g_device_count) {
  6451. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  6452. main_device, g_device_count, g_main_device);
  6453. return;
  6454. }
  6455. g_main_device = main_device;
  6456. if (g_device_count > 1) {
  6457. cudaDeviceProp prop;
  6458. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  6459. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  6460. }
  6461. }
  6462. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  6463. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  6464. // it still won't always work as expected, but it's better than nothing
  6465. if (scratch_size > g_scratch_size) {
  6466. ggml_cuda_free_scratch();
  6467. }
  6468. g_scratch_size = std::max(g_scratch_size, scratch_size);
  6469. }
  6470. void ggml_cuda_free_scratch() {
  6471. if (g_scratch_buffer == nullptr) {
  6472. return;
  6473. }
  6474. CUDA_CHECK(cudaFree(g_scratch_buffer));
  6475. g_scratch_buffer = nullptr;
  6476. }
  6477. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  6478. if (!g_cublas_loaded) return false;
  6479. ggml_cuda_func_t func;
  6480. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  6481. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  6482. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  6483. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT) {
  6484. return false;
  6485. }
  6486. if (tensor->op == GGML_OP_MUL_MAT) {
  6487. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  6488. #ifndef NDEBUG
  6489. fprintf(stderr, "%s: cannot compute %s: src0->ne[3] = %d, src1->ne[3] = %d - fallback to CPU\n", __func__, tensor->name, tensor->src[0]->ne[3], tensor->src[1]->ne[3]);
  6490. #endif
  6491. return false;
  6492. }
  6493. }
  6494. switch (tensor->op) {
  6495. case GGML_OP_REPEAT:
  6496. func = ggml_cuda_repeat;
  6497. break;
  6498. case GGML_OP_GET_ROWS:
  6499. func = ggml_cuda_get_rows;
  6500. break;
  6501. case GGML_OP_DUP:
  6502. func = ggml_cuda_dup;
  6503. break;
  6504. case GGML_OP_ADD:
  6505. func = ggml_cuda_add;
  6506. break;
  6507. case GGML_OP_MUL:
  6508. func = ggml_cuda_mul;
  6509. break;
  6510. case GGML_OP_UNARY:
  6511. switch (ggml_get_unary_op(tensor)) {
  6512. case GGML_UNARY_OP_GELU:
  6513. func = ggml_cuda_gelu;
  6514. break;
  6515. case GGML_UNARY_OP_SILU:
  6516. func = ggml_cuda_silu;
  6517. break;
  6518. case GGML_UNARY_OP_RELU:
  6519. func = ggml_cuda_relu;
  6520. break;
  6521. default:
  6522. return false;
  6523. } break;
  6524. case GGML_OP_NORM:
  6525. func = ggml_cuda_norm;
  6526. break;
  6527. case GGML_OP_RMS_NORM:
  6528. func = ggml_cuda_rms_norm;
  6529. break;
  6530. case GGML_OP_MUL_MAT:
  6531. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  6532. return false;
  6533. }
  6534. func = ggml_cuda_mul_mat;
  6535. break;
  6536. case GGML_OP_SCALE:
  6537. func = ggml_cuda_scale;
  6538. break;
  6539. case GGML_OP_SQR:
  6540. func = ggml_cuda_sqr;
  6541. break;
  6542. case GGML_OP_CLAMP:
  6543. if (!any_on_device) {
  6544. return false;
  6545. }
  6546. func = ggml_cuda_clamp;
  6547. break;
  6548. case GGML_OP_CPY:
  6549. func = ggml_cuda_cpy;
  6550. break;
  6551. case GGML_OP_CONT:
  6552. func = ggml_cuda_dup;
  6553. break;
  6554. case GGML_OP_RESHAPE:
  6555. case GGML_OP_VIEW:
  6556. case GGML_OP_PERMUTE:
  6557. case GGML_OP_TRANSPOSE:
  6558. func = ggml_cuda_nop;
  6559. break;
  6560. case GGML_OP_DIAG_MASK_INF:
  6561. func = ggml_cuda_diag_mask_inf;
  6562. break;
  6563. case GGML_OP_SOFT_MAX:
  6564. func = ggml_cuda_soft_max;
  6565. break;
  6566. case GGML_OP_ROPE:
  6567. func = ggml_cuda_rope;
  6568. break;
  6569. case GGML_OP_ALIBI:
  6570. func = ggml_cuda_alibi;
  6571. break;
  6572. case GGML_OP_IM2COL:
  6573. func = ggml_cuda_im2col;
  6574. break;
  6575. default:
  6576. return false;
  6577. }
  6578. if (params->ith != 0) {
  6579. return true;
  6580. }
  6581. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  6582. return true;
  6583. }
  6584. func(tensor->src[0], tensor->src[1], tensor);
  6585. return true;
  6586. }
  6587. int ggml_cuda_get_device_count() {
  6588. int device_count;
  6589. CUDA_CHECK(cudaGetDeviceCount(&device_count));
  6590. return device_count;
  6591. }
  6592. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  6593. cudaDeviceProp prop;
  6594. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  6595. snprintf(description, description_size, "%s", prop.name);
  6596. }
  6597. ////////////////////////////////////////////////////////////////////////////////
  6598. // backend interface
  6599. #define UNUSED GGML_UNUSED
  6600. struct ggml_backend_context_cuda {
  6601. };
  6602. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  6603. return GGML_CUDA_NAME;
  6604. UNUSED(backend);
  6605. }
  6606. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  6607. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  6608. delete cuda_ctx;
  6609. delete backend;
  6610. }
  6611. struct ggml_backend_buffer_context_cuda {
  6612. void * device;
  6613. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  6614. size_t temp_tensor_extra_index = 0;
  6615. ~ggml_backend_buffer_context_cuda() {
  6616. delete[] temp_tensor_extras;
  6617. }
  6618. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  6619. if (temp_tensor_extras == nullptr) {
  6620. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  6621. }
  6622. size_t alloc_index = temp_tensor_extra_index;
  6623. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  6624. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  6625. memset(extra, 0, sizeof(*extra));
  6626. return extra;
  6627. }
  6628. };
  6629. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  6630. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  6631. CUDA_CHECK(cudaFree(ctx->device));
  6632. delete ctx;
  6633. }
  6634. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  6635. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  6636. return ctx->device;
  6637. }
  6638. static size_t ggml_backend_cuda_buffer_get_alloc_size(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  6639. int64_t row_low = 0;
  6640. int64_t row_high = ggml_nrows(tensor);
  6641. int64_t nrows_split = row_high - row_low;
  6642. size_t size = ggml_nbytes_split(tensor, nrows_split);
  6643. int64_t ne0 = tensor->ne[0];
  6644. if (ggml_is_quantized(tensor->type)) {
  6645. if (ne0 % MATRIX_ROW_PADDING != 0) {
  6646. size += (MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING)
  6647. * ggml_type_size(tensor->type)/ggml_blck_size(tensor->type);
  6648. }
  6649. }
  6650. return size;
  6651. UNUSED(buffer);
  6652. }
  6653. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  6654. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  6655. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  6656. assert(tensor->view_src->buffer->backend == buffer->backend);
  6657. tensor->backend = tensor->view_src->backend;
  6658. tensor->extra = tensor->view_src->extra;
  6659. return;
  6660. }
  6661. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  6662. extra->data_device[g_main_device] = tensor->data;
  6663. tensor->backend = GGML_BACKEND_GPU;
  6664. tensor->extra = extra;
  6665. if (ggml_is_quantized(tensor->type)) {
  6666. // initialize padding to 0 to avoid possible NaN values
  6667. int64_t row_low = 0;
  6668. int64_t row_high = ggml_nrows(tensor);
  6669. int64_t nrows_split = row_high - row_low;
  6670. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  6671. size_t padded_size = ggml_backend_cuda_buffer_get_alloc_size(tensor->buffer, tensor);
  6672. if (padded_size > original_size && tensor->view_src == nullptr) {
  6673. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[g_main_device][0]));
  6674. }
  6675. }
  6676. UNUSED(buffer);
  6677. }
  6678. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  6679. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  6680. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  6681. /* .get_alloc_size = */ ggml_backend_cuda_buffer_get_alloc_size,
  6682. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  6683. /* .free_tensor = */ NULL,
  6684. };
  6685. static ggml_backend_buffer_t ggml_backend_cuda_alloc_buffer(ggml_backend_t backend, size_t size) {
  6686. ggml_cuda_set_device(g_main_device);
  6687. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda;
  6688. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  6689. ggml_cuda_set_device(g_main_device);
  6690. CUDA_CHECK(cudaMalloc(&ctx->device, size));
  6691. return ggml_backend_buffer_init(backend, cuda_backend_buffer_interface, ctx, size);
  6692. }
  6693. static size_t ggml_backend_cuda_get_alignment(ggml_backend_t backend) {
  6694. return 128;
  6695. UNUSED(backend);
  6696. }
  6697. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  6698. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds");
  6699. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  6700. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  6701. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[g_main_device][0]));
  6702. UNUSED(backend);
  6703. }
  6704. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  6705. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor read out of bounds");
  6706. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  6707. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  6708. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  6709. UNUSED(backend);
  6710. }
  6711. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  6712. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  6713. UNUSED(backend);
  6714. }
  6715. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  6716. GGML_ASSERT(!"not implemented");
  6717. return nullptr;
  6718. UNUSED(backend);
  6719. UNUSED(cgraph);
  6720. }
  6721. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  6722. GGML_ASSERT(!"not implemented");
  6723. UNUSED(backend);
  6724. UNUSED(plan);
  6725. }
  6726. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  6727. GGML_ASSERT(!"not implemented");
  6728. UNUSED(backend);
  6729. UNUSED(plan);
  6730. }
  6731. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  6732. ggml_cuda_set_device(g_main_device);
  6733. ggml_compute_params params = {};
  6734. params.type = GGML_TASK_COMPUTE;
  6735. params.ith = 0;
  6736. for (int i = 0; i < cgraph->n_nodes; i++) {
  6737. ggml_tensor * node = cgraph->nodes[i];
  6738. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  6739. continue;
  6740. assert(node->backend == GGML_BACKEND_GPU);
  6741. for (int j = 0; j < GGML_MAX_SRC; j++) {
  6742. if (node->src[j] != nullptr) {
  6743. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  6744. }
  6745. }
  6746. bool ok = ggml_cuda_compute_forward(&params, node);
  6747. if (!ok) {
  6748. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  6749. }
  6750. GGML_ASSERT(ok);
  6751. #if 0
  6752. if (node->type == GGML_TYPE_F32) {
  6753. cudaDeviceSynchronize();
  6754. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  6755. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  6756. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  6757. ggml_type_name(node->src[0]->type),
  6758. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  6759. node->src[0]->name,
  6760. node->src[1] ? node->src[1]->name : "none");
  6761. double sum = 0.0;
  6762. double sq_sum = 0.0;
  6763. for (int i = 0; i < ggml_nelements(node); i++) {
  6764. printf("%f ", tmp[i]);
  6765. sum += tmp[i];
  6766. sq_sum += tmp[i]*tmp[i];
  6767. }
  6768. printf("\n");
  6769. printf("sum: %f, ", sum);
  6770. printf("sq_sum: %f\n", sq_sum);
  6771. }
  6772. #endif
  6773. }
  6774. UNUSED(backend);
  6775. }
  6776. static ggml_backend_i cuda_backend_i = {
  6777. /* .get_name = */ ggml_backend_cuda_name,
  6778. /* .free = */ ggml_backend_cuda_free,
  6779. /* .alloc_buffer = */ ggml_backend_cuda_alloc_buffer,
  6780. /* .get_alignment = */ ggml_backend_cuda_get_alignment,
  6781. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  6782. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  6783. /* .synchronize = */ ggml_backend_cuda_synchronize,
  6784. /* .cpy_tensor_from = */ nullptr,
  6785. /* .cpy_tensor_to = */ nullptr,
  6786. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  6787. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  6788. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  6789. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  6790. /* .supports_op = */ nullptr,
  6791. };
  6792. ggml_backend_t ggml_backend_cuda_init() {
  6793. ggml_init_cublas(); // TODO: remove from ggml.c
  6794. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda;
  6795. ggml_backend_t cuda_backend = new ggml_backend {
  6796. /* .interface = */ cuda_backend_i,
  6797. /* .context = */ ctx
  6798. };
  6799. return cuda_backend;
  6800. }