ggml-cuda.cu 235 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231
  1. #include <cstddef>
  2. #include <cstdint>
  3. #include <limits>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <atomic>
  7. #include <assert.h>
  8. #include <cuda_runtime.h>
  9. #include <cublas_v2.h>
  10. #include <cuda_fp16.h>
  11. #include "ggml-cuda.h"
  12. #include "ggml.h"
  13. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  14. #define CC_TURING 700
  15. #if defined(_MSC_VER)
  16. #pragma warning(disable: 4244 4267) // possible loss of data
  17. #endif
  18. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  19. #define CUDA_CHECK(err) \
  20. do { \
  21. cudaError_t err_ = (err); \
  22. if (err_ != cudaSuccess) { \
  23. fprintf(stderr, "CUDA error %d at %s:%d: %s\n", err_, __FILE__, __LINE__, \
  24. cudaGetErrorString(err_)); \
  25. exit(1); \
  26. } \
  27. } while (0)
  28. #if CUDART_VERSION >= 12000
  29. #define CUBLAS_CHECK(err) \
  30. do { \
  31. cublasStatus_t err_ = (err); \
  32. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  33. fprintf(stderr, "\ncuBLAS error %d at %s:%d: %s\n", \
  34. err_, __FILE__, __LINE__, cublasGetStatusString(err_)); \
  35. exit(1); \
  36. } \
  37. } while (0)
  38. #else
  39. #define CUBLAS_CHECK(err) \
  40. do { \
  41. cublasStatus_t err_ = (err); \
  42. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  43. fprintf(stderr, "\ncuBLAS error %d at %s:%d\n", err_, __FILE__, __LINE__); \
  44. exit(1); \
  45. } \
  46. } while (0)
  47. #endif // CUDART_VERSION >= 11
  48. #ifdef GGML_CUDA_F16
  49. typedef half dfloat; // dequantize float
  50. typedef half2 dfloat2;
  51. #else
  52. typedef float dfloat; // dequantize float
  53. typedef float2 dfloat2;
  54. #endif //GGML_CUDA_F16
  55. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  56. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  57. int x32 = 0;
  58. x32 |= x16[0] << 0;
  59. x32 |= x16[1] << 16;
  60. return x32;
  61. }
  62. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  63. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  64. int x32 = 0;
  65. x32 |= x16[0] << 0;
  66. x32 |= x16[1] << 16;
  67. return x32;
  68. }
  69. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  70. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  71. }
  72. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  73. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  74. }
  75. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  76. typedef void (*to_fp32_cuda_t)(const void * __restrict__ x, float * __restrict__ y, int k, cudaStream_t stream);
  77. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  78. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  79. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  80. typedef void (*ggml_cuda_op_t)(
  81. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i, float * src0_ddf_i,
  82. float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  83. cudaStream_t & cudaStream_main);
  84. // QK = number of values after dequantization
  85. // QR = QK / number of values before dequantization
  86. // QI = number of 32 bit integers before dequantization
  87. #define QK4_0 32
  88. #define QR4_0 2
  89. #define QI4_0 (QK4_0 / (4 * QR4_0))
  90. typedef struct {
  91. half d; // delta
  92. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  93. } block_q4_0;
  94. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  95. #define QK4_1 32
  96. #define QR4_1 2
  97. #define QI4_1 (QK4_1 / (4 * QR4_1))
  98. typedef struct {
  99. half2 dm; // dm.x = delta, dm.y = min
  100. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  101. } block_q4_1;
  102. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  103. #define QK5_0 32
  104. #define QR5_0 2
  105. #define QI5_0 (QK5_0 / (4 * QR5_0))
  106. typedef struct {
  107. half d; // delta
  108. uint8_t qh[4]; // 5-th bit of quants
  109. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  110. } block_q5_0;
  111. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  112. #define QK5_1 32
  113. #define QR5_1 2
  114. #define QI5_1 (QK5_1 / (4 * QR5_1))
  115. typedef struct {
  116. half2 dm; // dm.x = delta, dm.y = min
  117. uint8_t qh[4]; // 5-th bit of quants
  118. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  119. } block_q5_1;
  120. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  121. #define QK8_0 32
  122. #define QR8_0 1
  123. #define QI8_0 (QK8_0 / (4 * QR8_0))
  124. typedef struct {
  125. half d; // delta
  126. int8_t qs[QK8_0]; // quants
  127. } block_q8_0;
  128. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  129. #define QK8_1 32
  130. #define QR8_1 1
  131. #define QI8_1 (QK8_1 / (4 * QR8_1))
  132. typedef struct {
  133. half2 ds; // ds.x = delta, ds.y = sum
  134. int8_t qs[QK8_0]; // quants
  135. } block_q8_1;
  136. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  137. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  138. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  139. typedef void (*load_tiles_cuda_t)(
  140. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  141. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  142. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  143. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  144. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  145. //================================= k-quants
  146. #ifdef GGML_QKK_64
  147. #define QK_K 64
  148. #define K_SCALE_SIZE 4
  149. #else
  150. #define QK_K 256
  151. #define K_SCALE_SIZE 12
  152. #endif
  153. #define QR2_K 4
  154. #define QI2_K (QK_K / (4*QR2_K))
  155. typedef struct {
  156. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  157. uint8_t qs[QK_K/4]; // quants
  158. half2 dm; // super-block scale for quantized scales/mins
  159. } block_q2_K;
  160. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  161. #define QR3_K 4
  162. #define QI3_K (QK_K / (4*QR3_K))
  163. typedef struct {
  164. uint8_t hmask[QK_K/8]; // quants - high bit
  165. uint8_t qs[QK_K/4]; // quants - low 2 bits
  166. #ifdef GGML_QKK_64
  167. uint8_t scales[2]; // scales, quantized with 8 bits
  168. #else
  169. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  170. #endif
  171. half d; // super-block scale
  172. } block_q3_K;
  173. //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");
  174. #define QR4_K 2
  175. #define QI4_K (QK_K / (4*QR4_K))
  176. #ifdef GGML_QKK_64
  177. typedef struct {
  178. half d[2]; // super-block scales/mins
  179. uint8_t scales[2]; // 4-bit block scales/mins
  180. uint8_t qs[QK_K/2]; // 4--bit quants
  181. } block_q4_K;
  182. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + QK_K/2 + 2, "wrong q4_K block size/padding");
  183. #else
  184. typedef struct {
  185. half2 dm; // super-block scale for quantized scales/mins
  186. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  187. uint8_t qs[QK_K/2]; // 4--bit quants
  188. } block_q4_K;
  189. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  190. #endif
  191. #define QR5_K 2
  192. #define QI5_K (QK_K / (4*QR5_K))
  193. #ifdef GGML_QKK_64
  194. typedef struct {
  195. half d; // super-block scale
  196. int8_t scales[QK_K/16]; // block scales
  197. uint8_t qh[QK_K/8]; // quants, high bit
  198. uint8_t qs[QK_K/2]; // quants, low 4 bits
  199. } block_q5_K;
  200. 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");
  201. #else
  202. typedef struct {
  203. half2 dm; // super-block scale for quantized scales/mins
  204. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  205. uint8_t qh[QK_K/8]; // quants, high bit
  206. uint8_t qs[QK_K/2]; // quants, low 4 bits
  207. } block_q5_K;
  208. 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");
  209. #endif
  210. #define QR6_K 2
  211. #define QI6_K (QK_K / (4*QR6_K))
  212. typedef struct {
  213. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  214. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  215. int8_t scales[QK_K/16]; // scales
  216. half d; // delta
  217. } block_q6_K;
  218. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  219. #define WARP_SIZE 32
  220. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  221. #define CUDA_ADD_BLOCK_SIZE 256
  222. #define CUDA_MUL_BLOCK_SIZE 256
  223. #define CUDA_GELU_BLOCK_SIZE 256
  224. #define CUDA_SILU_BLOCK_SIZE 256
  225. #define CUDA_CPY_BLOCK_SIZE 32
  226. #define CUDA_SCALE_BLOCK_SIZE 256
  227. #define CUDA_ROPE_BLOCK_SIZE 256
  228. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  229. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  230. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  231. // dmmv = dequantize_mul_mat_vec
  232. #ifndef GGML_CUDA_DMMV_X
  233. #define GGML_CUDA_DMMV_X 32
  234. #endif
  235. #ifndef GGML_CUDA_MMV_Y
  236. #define GGML_CUDA_MMV_Y 1
  237. #endif
  238. #ifndef K_QUANTS_PER_ITERATION
  239. #define K_QUANTS_PER_ITERATION 2
  240. #else
  241. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  242. #endif
  243. struct ggml_tensor_extra_gpu {
  244. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  245. cudaEvent_t events[GGML_CUDA_MAX_DEVICES]; // events for synchronizing multiple GPUs
  246. };
  247. static int g_device_count = -1;
  248. static int g_main_device = 0;
  249. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  250. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  251. static bool g_mul_mat_q = false;
  252. static void * g_scratch_buffer = nullptr;
  253. static size_t g_scratch_size = 1024*1024*1024; // 1 GB by default
  254. static size_t g_scratch_offset = 0;
  255. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  256. static cudaStream_t g_cudaStreams_main[GGML_CUDA_MAX_DEVICES] = { nullptr };
  257. static __global__ void add_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  258. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  259. if (i >= kx) {
  260. return;
  261. }
  262. dst[i] = x[i] + y[i%ky];
  263. }
  264. static __global__ void add_f16_f32_f16(const half * x, const float * y, half * dst, const int k) {
  265. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  266. if (i >= k) {
  267. return;
  268. }
  269. dst[i] = __hadd(x[i], __float2half(y[i]));
  270. }
  271. static __global__ void mul_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  272. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  273. if (i >= kx) {
  274. return;
  275. }
  276. dst[i] = x[i] * y[i%ky];
  277. }
  278. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  279. const float GELU_COEF_A = 0.044715f;
  280. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  281. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  282. if (i >= k) {
  283. return;
  284. }
  285. float xi = x[i];
  286. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  287. }
  288. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  289. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  290. if (i >= k) {
  291. return;
  292. }
  293. dst[i] = x[i] / (1.0f + expf(-x[i]));
  294. }
  295. static __global__ void norm_f32(const float * x, float * dst, const int ncols) {
  296. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  297. const int tid = threadIdx.x;
  298. const float eps = 1e-5f;
  299. float mean = 0.0f;
  300. float var = 0.0f;
  301. for (int col = tid; col < ncols; col += WARP_SIZE) {
  302. const float xi = x[row*ncols + col];
  303. mean += xi;
  304. var += xi * xi;
  305. }
  306. // sum up partial sums
  307. #pragma unroll
  308. for (int mask = 16; mask > 0; mask >>= 1) {
  309. mean += __shfl_xor_sync(0xffffffff, mean, mask, 32);
  310. var += __shfl_xor_sync(0xffffffff, var, mask, 32);
  311. }
  312. mean /= ncols;
  313. var = var / ncols - mean * mean;
  314. const float inv_var = rsqrtf(var + eps);
  315. for (int col = tid; col < ncols; col += WARP_SIZE) {
  316. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_var;
  317. }
  318. }
  319. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  320. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  321. const int tid = threadIdx.x;
  322. float tmp = 0.0f; // partial sum for thread in warp
  323. for (int col = tid; col < ncols; col += WARP_SIZE) {
  324. const float xi = x[row*ncols + col];
  325. tmp += xi * xi;
  326. }
  327. // sum up partial sums
  328. #pragma unroll
  329. for (int mask = 16; mask > 0; mask >>= 1) {
  330. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  331. }
  332. const float mean = tmp / ncols;
  333. const float scale = rsqrtf(mean + eps);
  334. for (int col = tid; col < ncols; col += WARP_SIZE) {
  335. dst[row*ncols + col] = scale * x[row*ncols + col];
  336. }
  337. }
  338. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  339. const block_q4_0 * x = (const block_q4_0 *) vx;
  340. const dfloat d = x[ib].d;
  341. const int vui = x[ib].qs[iqs];
  342. v.x = vui & 0xF;
  343. v.y = vui >> 4;
  344. #ifdef GGML_CUDA_F16
  345. v = __hsub2(v, {8.0f, 8.0f});
  346. v = __hmul2(v, {d, d});
  347. #else
  348. v.x = (v.x - 8.0f) * d;
  349. v.y = (v.y - 8.0f) * d;
  350. #endif // GGML_CUDA_F16
  351. }
  352. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  353. const block_q4_1 * x = (const block_q4_1 *) vx;
  354. const dfloat d = x[ib].dm.x;
  355. const dfloat m = x[ib].dm.y;
  356. const int vui = x[ib].qs[iqs];
  357. v.x = vui & 0xF;
  358. v.y = vui >> 4;
  359. #ifdef GGML_CUDA_F16
  360. v = __hmul2(v, {d, d});
  361. v = __hadd2(v, {m, m});
  362. #else
  363. v.x = (v.x * d) + m;
  364. v.y = (v.y * d) + m;
  365. #endif // GGML_CUDA_F16
  366. }
  367. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  368. const block_q5_0 * x = (const block_q5_0 *) vx;
  369. const dfloat d = x[ib].d;
  370. uint32_t qh;
  371. memcpy(&qh, x[ib].qh, sizeof(qh));
  372. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  373. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  374. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  375. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  376. #ifdef GGML_CUDA_F16
  377. v = __hsub2(v, {16.0f, 16.0f});
  378. v = __hmul2(v, {d, d});
  379. #else
  380. v.x = (v.x - 16.0f) * d;
  381. v.y = (v.y - 16.0f) * d;
  382. #endif // GGML_CUDA_F16
  383. }
  384. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  385. const block_q5_1 * x = (const block_q5_1 *) vx;
  386. const dfloat d = x[ib].dm.x;
  387. const dfloat m = x[ib].dm.y;
  388. uint32_t qh;
  389. memcpy(&qh, x[ib].qh, sizeof(qh));
  390. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  391. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  392. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  393. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  394. #ifdef GGML_CUDA_F16
  395. v = __hmul2(v, {d, d});
  396. v = __hadd2(v, {m, m});
  397. #else
  398. v.x = (v.x * d) + m;
  399. v.y = (v.y * d) + m;
  400. #endif // GGML_CUDA_F16
  401. }
  402. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  403. const block_q8_0 * x = (const block_q8_0 *) vx;
  404. const dfloat d = x[ib].d;
  405. v.x = x[ib].qs[iqs + 0];
  406. v.y = x[ib].qs[iqs + 1];
  407. #ifdef GGML_CUDA_F16
  408. v = __hmul2(v, {d, d});
  409. #else
  410. v.x *= d;
  411. v.y *= d;
  412. #endif // GGML_CUDA_F16
  413. }
  414. //================================== k-quants
  415. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, float * __restrict__ yy) {
  416. const int i = blockIdx.x;
  417. const block_q2_K * x = (const block_q2_K *) vx;
  418. const int tid = threadIdx.x;
  419. #if QK_K == 256
  420. const int n = tid/32;
  421. const int l = tid - 32*n;
  422. const int is = 8*n + l/16;
  423. const uint8_t q = x[i].qs[32*n + l];
  424. float * y = yy + i*QK_K + 128*n;
  425. float dall = x[i].dm.x;
  426. float dmin = x[i].dm.y;
  427. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  428. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  429. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  430. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  431. #else
  432. const int is = tid/16; // 0 or 1
  433. const int il = tid%16; // 0...15
  434. const uint8_t q = x[i].qs[il] >> (2*is);
  435. float * y = yy + i*QK_K + 16*is + il;
  436. float dall = x[i].dm.x;
  437. float dmin = x[i].dm.y;
  438. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  439. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  440. #endif
  441. }
  442. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, float * __restrict__ yy) {
  443. const int i = blockIdx.x;
  444. const block_q3_K * x = (const block_q3_K *) vx;
  445. #if QK_K == 256
  446. const int r = threadIdx.x/4;
  447. const int tid = r/2;
  448. const int is0 = r%2;
  449. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  450. const int n = tid / 4;
  451. const int j = tid - 4*n;
  452. uint8_t m = 1 << (4*n + j);
  453. int is = 8*n + 2*j + is0;
  454. int shift = 2*j;
  455. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  456. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  457. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  458. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  459. float d_all = x[i].d;
  460. float dl = d_all * (us - 32);
  461. float * y = yy + i*QK_K + 128*n + 32*j;
  462. const uint8_t * q = x[i].qs + 32*n;
  463. const uint8_t * hm = x[i].hmask;
  464. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  465. #else
  466. const int tid = threadIdx.x;
  467. const int is = tid/16; // 0 or 1
  468. const int il = tid%16; // 0...15
  469. const int im = il/8; // 0...1
  470. const int in = il%8; // 0...7
  471. float * y = yy + i*QK_K + 16*is + il;
  472. const uint8_t q = x[i].qs[il] >> (2*is);
  473. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  474. const float d = (float)x[i].d;
  475. if (is == 0) {
  476. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  477. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  478. } else {
  479. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  480. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  481. }
  482. #endif
  483. }
  484. #if QK_K == 256
  485. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  486. if (j < 4) {
  487. d = q[j] & 63; m = q[j + 4] & 63;
  488. } else {
  489. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  490. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  491. }
  492. }
  493. #endif
  494. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, float * __restrict__ yy) {
  495. const block_q4_K * x = (const block_q4_K *) vx;
  496. const int i = blockIdx.x;
  497. #if QK_K == 256
  498. // assume 32 threads
  499. const int tid = threadIdx.x;
  500. const int il = tid/8;
  501. const int ir = tid%8;
  502. const int is = 2*il;
  503. const int n = 4;
  504. float * y = yy + i*QK_K + 64*il + n*ir;
  505. const float dall = x[i].dm.x;
  506. const float dmin = x[i].dm.y;
  507. const uint8_t * q = x[i].qs + 32*il + n*ir;
  508. uint8_t sc, m;
  509. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  510. const float d1 = dall * sc; const float m1 = dmin * m;
  511. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  512. const float d2 = dall * sc; const float m2 = dmin * m;
  513. for (int l = 0; l < n; ++l) {
  514. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  515. y[l +32] = d2 * (q[l] >> 4) - m2;
  516. }
  517. #else
  518. const int tid = threadIdx.x;
  519. const uint8_t * q = x[i].qs;
  520. float * y = yy + i*QK_K;
  521. const float d = (float)x[i].d[0];
  522. const float m = (float)x[i].d[1];
  523. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  524. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  525. #endif
  526. }
  527. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, float * __restrict__ yy) {
  528. const block_q5_K * x = (const block_q5_K *) vx;
  529. const int i = blockIdx.x;
  530. #if QK_K == 256
  531. // assume 64 threads - this is very slightly better than the one below
  532. const int tid = threadIdx.x;
  533. const int il = tid/16; // il is in 0...3
  534. const int ir = tid%16; // ir is in 0...15
  535. const int is = 2*il; // is is in 0...6
  536. float * y = yy + i*QK_K + 64*il + 2*ir;
  537. const float dall = x[i].dm.x;
  538. const float dmin = x[i].dm.y;
  539. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  540. const uint8_t * qh = x[i].qh + 2*ir;
  541. uint8_t sc, m;
  542. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  543. const float d1 = dall * sc; const float m1 = dmin * m;
  544. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  545. const float d2 = dall * sc; const float m2 = dmin * m;
  546. uint8_t hm = 1 << (2*il);
  547. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  548. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  549. hm <<= 1;
  550. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  551. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  552. #else
  553. const int tid = threadIdx.x;
  554. const uint8_t q = x[i].qs[tid];
  555. const int im = tid/8; // 0...3
  556. const int in = tid%8; // 0...7
  557. const int is = tid/16; // 0 or 1
  558. const uint8_t h = x[i].qh[in] >> im;
  559. const float d = x[i].d;
  560. float * y = yy + i*QK_K + tid;
  561. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  562. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  563. #endif
  564. }
  565. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, float * __restrict__ yy) {
  566. const block_q6_K * x = (const block_q6_K *) vx;
  567. const int i = blockIdx.x;
  568. #if QK_K == 256
  569. // assume 64 threads - this is very slightly better than the one below
  570. const int tid = threadIdx.x;
  571. const int ip = tid/32; // ip is 0 or 1
  572. const int il = tid - 32*ip; // 0...32
  573. const int is = 8*ip + il/16;
  574. float * y = yy + i*QK_K + 128*ip + il;
  575. const float d = x[i].d;
  576. const uint8_t * ql = x[i].ql + 64*ip + il;
  577. const uint8_t qh = x[i].qh[32*ip + il];
  578. const int8_t * sc = x[i].scales + is;
  579. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  580. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  581. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  582. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  583. #else
  584. // assume 32 threads
  585. const int tid = threadIdx.x;
  586. const int ip = tid/16; // 0 or 1
  587. const int il = tid - 16*ip; // 0...15
  588. float * y = yy + i*QK_K + 16*ip + il;
  589. const float d = x[i].d;
  590. const uint8_t ql = x[i].ql[16*ip + il];
  591. const uint8_t qh = x[i].qh[il] >> (2*ip);
  592. const int8_t * sc = x[i].scales;
  593. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  594. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  595. #endif
  596. }
  597. 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) {
  598. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  599. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  600. if (row > nrows) return;
  601. const int num_blocks_per_row = ncols / QK_K;
  602. const int ib0 = row*num_blocks_per_row;
  603. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  604. float tmp = 0; // partial sum for thread in warp
  605. #if QK_K == 256
  606. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  607. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  608. const int step = 16/K_QUANTS_PER_ITERATION;
  609. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  610. const int in = tid - step*im; // 0...15 or 0...7
  611. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  612. const int q_offset = 32*im + l0;
  613. const int s_offset = 8*im;
  614. const int y_offset = 128*im + l0;
  615. uint32_t aux[4];
  616. const uint8_t * d = (const uint8_t *)aux;
  617. const uint8_t * m = (const uint8_t *)(aux + 2);
  618. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  619. const float * y = yy + i * QK_K + y_offset;
  620. const uint8_t * q = x[i].qs + q_offset;
  621. const float dall = x[i].dm.x;
  622. const float dmin = x[i].dm.y;
  623. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  624. aux[0] = a[0] & 0x0f0f0f0f;
  625. aux[1] = a[1] & 0x0f0f0f0f;
  626. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  627. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  628. float sum1 = 0, sum2 = 0;
  629. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  630. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  631. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  632. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  633. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  634. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  635. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  636. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  637. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  638. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  639. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  640. }
  641. tmp += dall * sum1 - dmin * sum2;
  642. }
  643. #else
  644. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  645. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  646. const int offset = tid * K_QUANTS_PER_ITERATION;
  647. uint32_t uaux[2];
  648. const uint8_t * d = (const uint8_t *)uaux;
  649. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  650. const float * y = yy + i * QK_K + offset;
  651. const uint8_t * q = x[i].qs + offset;
  652. const uint32_t * s = (const uint32_t *)x[i].scales;
  653. uaux[0] = s[0] & 0x0f0f0f0f;
  654. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  655. const float2 dall = __half22float2(x[i].dm);
  656. float sum1 = 0, sum2 = 0;
  657. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  658. const uint8_t ql = q[l];
  659. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  660. + y[l+16] * d[1] * ((ql >> 2) & 3)
  661. + y[l+32] * d[2] * ((ql >> 4) & 3)
  662. + y[l+48] * d[3] * ((ql >> 6) & 3);
  663. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  664. }
  665. tmp += dall.x * sum1 - dall.y * sum2;
  666. }
  667. #endif
  668. // sum up partial sums and write back result
  669. #pragma unroll
  670. for (int mask = 16; mask > 0; mask >>= 1) {
  671. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  672. }
  673. if (threadIdx.x == 0) {
  674. dst[row] = tmp;
  675. }
  676. }
  677. 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) {
  678. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  679. if (row > nrows) return;
  680. const int num_blocks_per_row = ncols / QK_K;
  681. const int ib0 = row*num_blocks_per_row;
  682. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  683. float tmp = 0; // partial sum for thread in warp
  684. #if QK_K == 256
  685. const uint16_t kmask1 = 0x0303;
  686. const uint16_t kmask2 = 0x0f0f;
  687. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  688. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  689. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  690. const int step = 16/K_QUANTS_PER_ITERATION;
  691. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  692. const int in = tid - step*im; // 0....15 or 0...7
  693. const uint8_t m = 1 << (4*im);
  694. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  695. const int q_offset = 32*im + l0;
  696. const int y_offset = 128*im + l0;
  697. uint16_t utmp[4];
  698. const int8_t * s = (const int8_t *)utmp;
  699. const uint16_t s_shift = 4*im;
  700. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  701. const float * y = yy + i * QK_K + y_offset;
  702. const uint8_t * q = x[i].qs + q_offset;
  703. const uint8_t * h = x[i].hmask + l0;
  704. const uint16_t * a = (const uint16_t *)x[i].scales;
  705. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  706. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  707. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  708. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  709. const float d = x[i].d;
  710. float sum = 0;
  711. for (int l = 0; l < n; ++l) {
  712. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  713. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  714. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  715. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  716. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  717. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  718. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  719. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  720. }
  721. tmp += d * sum;
  722. }
  723. #else
  724. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  725. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  726. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  727. const int in = offset/8; // 0 or 1
  728. const int im = offset%8; // 0...7
  729. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  730. const float * y = yy + i * QK_K + offset;
  731. const uint8_t * q = x[i].qs + offset;
  732. const uint8_t * s = x[i].scales;
  733. const float dall = (float)x[i].d;
  734. float sum = 0;
  735. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  736. const uint8_t hl = x[i].hmask[im+l] >> in;
  737. const uint8_t ql = q[l];
  738. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  739. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  740. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  741. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  742. }
  743. tmp += sum;
  744. }
  745. #endif
  746. // sum up partial sums and write back result
  747. #pragma unroll
  748. for (int mask = 16; mask > 0; mask >>= 1) {
  749. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  750. }
  751. if (threadIdx.x == 0) {
  752. dst[row] = tmp;
  753. }
  754. }
  755. 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) {
  756. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  757. if (row > nrows) return;
  758. const int num_blocks_per_row = ncols / QK_K;
  759. const int ib0 = row*num_blocks_per_row;
  760. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  761. #if QK_K == 256
  762. const uint16_t kmask1 = 0x3f3f;
  763. const uint16_t kmask2 = 0x0f0f;
  764. const uint16_t kmask3 = 0xc0c0;
  765. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  766. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  767. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  768. const int il = tid/step; // 0...3
  769. const int ir = tid - step*il; // 0...7 or 0...3
  770. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  771. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  772. const int in = il%2;
  773. const int l0 = n*(2*ir + in);
  774. const int q_offset = 32*im + l0;
  775. const int y_offset = 64*im + l0;
  776. uint16_t aux[4];
  777. const uint8_t * sc = (const uint8_t *)aux;
  778. #if K_QUANTS_PER_ITERATION == 2
  779. uint32_t q32[4];
  780. const uint8_t * q4 = (const uint8_t *)q32;
  781. #else
  782. uint16_t q16[4];
  783. const uint8_t * q4 = (const uint8_t *)q16;
  784. #endif
  785. float tmp = 0; // partial sum for thread in warp
  786. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  787. const float * y1 = yy + i*QK_K + y_offset;
  788. const float * y2 = y1 + 128;
  789. const float dall = x[i].dm.x;
  790. const float dmin = x[i].dm.y;
  791. const uint16_t * a = (const uint16_t *)x[i].scales;
  792. aux[0] = a[im+0] & kmask1;
  793. aux[1] = a[im+2] & kmask1;
  794. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  795. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  796. #if K_QUANTS_PER_ITERATION == 2
  797. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  798. const uint32_t * q2 = q1 + 16;
  799. q32[0] = q1[0] & 0x0f0f0f0f;
  800. q32[1] = q1[0] & 0xf0f0f0f0;
  801. q32[2] = q2[0] & 0x0f0f0f0f;
  802. q32[3] = q2[0] & 0xf0f0f0f0;
  803. float4 s = {0.f, 0.f, 0.f, 0.f};
  804. float smin = 0;
  805. for (int l = 0; l < 4; ++l) {
  806. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  807. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  808. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  809. }
  810. 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;
  811. #else
  812. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  813. const uint16_t * q2 = q1 + 32;
  814. q16[0] = q1[0] & 0x0f0f;
  815. q16[1] = q1[0] & 0xf0f0;
  816. q16[2] = q2[0] & 0x0f0f;
  817. q16[3] = q2[0] & 0xf0f0;
  818. float4 s = {0.f, 0.f, 0.f, 0.f};
  819. float smin = 0;
  820. for (int l = 0; l < 2; ++l) {
  821. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  822. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  823. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  824. }
  825. 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;
  826. #endif
  827. }
  828. #else
  829. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  830. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  831. const int step = tid * K_QUANTS_PER_ITERATION;
  832. uint16_t aux16[2];
  833. const uint8_t * s = (const uint8_t *)aux16;
  834. float tmp = 0;
  835. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  836. const uint8_t * q = x[i].qs + step;
  837. const float * y = yy + i*QK_K + step;
  838. const uint16_t * a = (const uint16_t *)x[i].scales;
  839. aux16[0] = a[0] & 0x0f0f;
  840. aux16[1] = (a[0] >> 4) & 0x0f0f;
  841. const float d = (float)x[i].d[0];
  842. const float m = (float)x[i].d[1];
  843. float sum = 0.f;
  844. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  845. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  846. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  847. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  848. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  849. }
  850. tmp += sum;
  851. }
  852. #endif
  853. // sum up partial sums and write back result
  854. #pragma unroll
  855. for (int mask = 16; mask > 0; mask >>= 1) {
  856. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  857. }
  858. if (tid == 0) {
  859. dst[row] = tmp;
  860. }
  861. }
  862. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  863. const int row = blockIdx.x;
  864. const int num_blocks_per_row = ncols / QK_K;
  865. const int ib0 = row*num_blocks_per_row;
  866. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  867. float tmp = 0; // partial sum for thread in warp
  868. #if QK_K == 256
  869. const uint16_t kmask1 = 0x3f3f;
  870. const uint16_t kmask2 = 0x0f0f;
  871. const uint16_t kmask3 = 0xc0c0;
  872. const int tid = threadIdx.x/2; // 0...15
  873. const int ix = threadIdx.x%2;
  874. const int il = tid/4; // 0...3
  875. const int ir = tid - 4*il;// 0...3
  876. const int n = 2;
  877. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  878. const int in = il%2;
  879. const int l0 = n*(2*ir + in);
  880. const int q_offset = 32*im + l0;
  881. const int y_offset = 64*im + l0;
  882. const uint8_t hm1 = 1 << (2*im);
  883. const uint8_t hm2 = hm1 << 4;
  884. uint16_t aux[4];
  885. const uint8_t * sc = (const uint8_t *)aux;
  886. uint16_t q16[8];
  887. const uint8_t * q4 = (const uint8_t *)q16;
  888. for (int i = ix; i < num_blocks_per_row; i += 2) {
  889. const uint8_t * ql1 = x[i].qs + q_offset;
  890. const uint8_t * qh = x[i].qh + l0;
  891. const float * y1 = yy + i*QK_K + y_offset;
  892. const float * y2 = y1 + 128;
  893. const float dall = x[i].dm.x;
  894. const float dmin = x[i].dm.y;
  895. const uint16_t * a = (const uint16_t *)x[i].scales;
  896. aux[0] = a[im+0] & kmask1;
  897. aux[1] = a[im+2] & kmask1;
  898. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  899. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  900. float4 sum = {0.f, 0.f, 0.f, 0.f};
  901. float smin = 0;
  902. const uint16_t * q1 = (const uint16_t *)ql1;
  903. const uint16_t * q2 = q1 + 32;
  904. q16[0] = q1[0] & 0x0f0f;
  905. q16[1] = q1[8] & 0x0f0f;
  906. q16[2] = (q1[0] >> 4) & 0x0f0f;
  907. q16[3] = (q1[8] >> 4) & 0x0f0f;
  908. q16[4] = q2[0] & 0x0f0f;
  909. q16[5] = q2[8] & 0x0f0f;
  910. q16[6] = (q2[0] >> 4) & 0x0f0f;
  911. q16[7] = (q2[8] >> 4) & 0x0f0f;
  912. for (int l = 0; l < n; ++l) {
  913. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  914. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  915. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  916. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  917. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  918. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  919. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  920. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  921. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  922. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  923. }
  924. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  925. }
  926. #else
  927. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  928. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  929. const int step = tid * K_QUANTS_PER_ITERATION;
  930. const int im = step/8;
  931. const int in = step%8;
  932. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  933. const uint8_t * q = x[i].qs + step;
  934. const int8_t * s = x[i].scales;
  935. const float * y = yy + i*QK_K + step;
  936. const float d = x[i].d;
  937. float sum = 0.f;
  938. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  939. const uint8_t h = x[i].qh[in+j] >> im;
  940. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  941. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  942. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  943. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  944. }
  945. tmp += sum;
  946. }
  947. #endif
  948. // sum up partial sums and write back result
  949. #pragma unroll
  950. for (int mask = 16; mask > 0; mask >>= 1) {
  951. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  952. }
  953. if (threadIdx.x == 0) {
  954. dst[row] = tmp;
  955. }
  956. }
  957. 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) {
  958. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  959. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  960. if (row > nrows) return;
  961. const int num_blocks_per_row = ncols / QK_K;
  962. const int ib0 = row*num_blocks_per_row;
  963. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  964. #if QK_K == 256
  965. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  966. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  967. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  968. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  969. const int in = tid - step*im; // 0...15 or 0...7
  970. #if K_QUANTS_PER_ITERATION == 1
  971. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  972. const int is = 0;
  973. #else
  974. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  975. const int is = in / 4;
  976. #endif
  977. const int ql_offset = 64*im + l0;
  978. const int qh_offset = 32*im + l0;
  979. const int s_offset = 8*im + is;
  980. const int y_offset = 128*im + l0;
  981. float tmp = 0; // partial sum for thread in warp
  982. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  983. const float * y = yy + i * QK_K + y_offset;
  984. const uint8_t * ql = x[i].ql + ql_offset;
  985. const uint8_t * qh = x[i].qh + qh_offset;
  986. const int8_t * s = x[i].scales + s_offset;
  987. const float d = x[i].d;
  988. #if K_QUANTS_PER_ITERATION == 1
  989. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  990. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  991. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  992. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  993. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  994. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  995. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  996. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  997. tmp += sum;
  998. #else
  999. float sum = 0;
  1000. for (int l = 0; l < 4; ++l) {
  1001. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1002. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1003. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1004. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1005. }
  1006. tmp += sum;
  1007. #endif
  1008. }
  1009. #else
  1010. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1011. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1012. const int step = tid * K_QUANTS_PER_ITERATION;
  1013. float tmp = 0; // partial sum for thread in warp
  1014. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1015. const float * y = yy + i * QK_K + step;
  1016. const uint8_t * ql = x[i].ql + step;
  1017. const uint8_t * qh = x[i].qh + step;
  1018. const int8_t * s = x[i].scales;
  1019. const float d = x[i+0].d;
  1020. float sum = 0;
  1021. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1022. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1023. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1024. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1025. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1026. }
  1027. tmp += sum;
  1028. }
  1029. #endif
  1030. // sum up partial sums and write back result
  1031. #pragma unroll
  1032. for (int mask = 16; mask > 0; mask >>= 1) {
  1033. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1034. }
  1035. if (tid == 0) {
  1036. dst[row] = tmp;
  1037. }
  1038. }
  1039. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1040. const half * x = (const half *) vx;
  1041. // automatic half -> float type cast if dfloat == float
  1042. v.x = x[ib + iqs + 0];
  1043. v.y = x[ib + iqs + 1];
  1044. }
  1045. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1046. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1047. if (ix >= kx_padded) {
  1048. return;
  1049. }
  1050. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1051. const int i_padded = iy*kx_padded + ix;
  1052. block_q8_1 * y = (block_q8_1 *) vy;
  1053. const int ib = i_padded / QK8_1; // block index
  1054. const int iqs = i_padded % QK8_1; // quant index
  1055. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1056. float amax = fabsf(xi);
  1057. float sum = xi;
  1058. #pragma unroll
  1059. for (int mask = 16; mask > 0; mask >>= 1) {
  1060. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1061. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1062. }
  1063. const float d = amax / 127;
  1064. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1065. y[ib].qs[iqs] = q;
  1066. if (iqs > 0) {
  1067. return;
  1068. }
  1069. y[ib].ds.x = d;
  1070. y[ib].ds.y = sum;
  1071. }
  1072. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  1073. static __global__ void dequantize_block(const void * __restrict__ vx, float * __restrict__ y, const int k) {
  1074. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1075. if (i >= k) {
  1076. return;
  1077. }
  1078. const int ib = i/qk; // block index
  1079. const int iqs = (i%qk)/qr; // quant index
  1080. const int iybs = i - i%qk; // y block start index
  1081. const int y_offset = qr == 1 ? 1 : qk/2;
  1082. // dequantize
  1083. dfloat2 v;
  1084. dequantize_kernel(vx, ib, iqs, v);
  1085. y[iybs + iqs + 0] = v.x;
  1086. y[iybs + iqs + y_offset] = v.y;
  1087. }
  1088. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1089. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1090. #define VDR_Q4_0_Q8_1_MMVQ 2
  1091. #define VDR_Q4_0_Q8_1_MMQ 4
  1092. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1093. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1094. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1095. int sumi = 0;
  1096. #pragma unroll
  1097. for (int i = 0; i < vdr; ++i) {
  1098. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1099. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1100. // SIMD dot product of quantized values
  1101. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1102. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1103. }
  1104. const float2 ds8f = __half22float2(ds8);
  1105. // second part effectively subtracts 8 from each quant value
  1106. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1107. #else
  1108. return 0.0f; // only to satisfy the compiler
  1109. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1110. }
  1111. #define VDR_Q4_1_Q8_1_MMVQ 2
  1112. #define VDR_Q4_1_Q8_1_MMQ 4
  1113. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1114. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1115. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1116. int sumi = 0;
  1117. #pragma unroll
  1118. for (int i = 0; i < vdr; ++i) {
  1119. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1120. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1121. // SIMD dot product of quantized values
  1122. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1123. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1124. }
  1125. #ifdef GGML_CUDA_F16
  1126. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1127. const float d4d8 = tmp.x;
  1128. const float m4s8 = tmp.y;
  1129. #else
  1130. const float2 dm4f = __half22float2(dm4);
  1131. const float2 ds8f = __half22float2(ds8);
  1132. const float d4d8 = dm4f.x * ds8f.x;
  1133. const float m4s8 = dm4f.y * ds8f.y;
  1134. #endif // GGML_CUDA_F16
  1135. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1136. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1137. #else
  1138. return 0.0f; // only to satisfy the compiler
  1139. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1140. }
  1141. #define VDR_Q5_0_Q8_1_MMVQ 2
  1142. #define VDR_Q5_0_Q8_1_MMQ 4
  1143. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1144. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1145. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1146. int sumi = 0;
  1147. #pragma unroll
  1148. for (int i = 0; i < vdr; ++i) {
  1149. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1150. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1151. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1152. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1153. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1154. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1155. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1156. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1157. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1158. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1159. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1160. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1161. }
  1162. const float2 ds8f = __half22float2(ds8);
  1163. // second part effectively subtracts 16 from each quant value
  1164. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1165. #else
  1166. return 0.0f; // only to satisfy the compiler
  1167. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1168. }
  1169. #define VDR_Q5_1_Q8_1_MMVQ 2
  1170. #define VDR_Q5_1_Q8_1_MMQ 4
  1171. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1172. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1173. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1174. int sumi = 0;
  1175. #pragma unroll
  1176. for (int i = 0; i < vdr; ++i) {
  1177. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1178. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1179. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1180. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1181. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1182. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1183. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1184. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1185. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1186. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1187. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1188. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1189. }
  1190. #ifdef GGML_CUDA_F16
  1191. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1192. const float d5d8 = tmp.x;
  1193. const float m5s8 = tmp.y;
  1194. #else
  1195. const float2 dm5f = __half22float2(dm5);
  1196. const float2 ds8f = __half22float2(ds8);
  1197. const float d5d8 = dm5f.x * ds8f.x;
  1198. const float m5s8 = dm5f.y * ds8f.y;
  1199. #endif // GGML_CUDA_F16
  1200. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1201. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1202. #else
  1203. return 0.0f; // only to satisfy the compiler
  1204. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1205. }
  1206. #define VDR_Q8_0_Q8_1_MMVQ 2
  1207. #define VDR_Q8_0_Q8_1_MMQ 8
  1208. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1209. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1210. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1211. int sumi = 0;
  1212. #pragma unroll
  1213. for (int i = 0; i < vdr; ++i) {
  1214. // SIMD dot product of quantized values
  1215. sumi = __dp4a(v[i], u[i], sumi);
  1216. }
  1217. return d8_0*d8_1 * sumi;
  1218. #else
  1219. return 0.0f; // only to satisfy the compiler
  1220. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1221. }
  1222. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1223. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1224. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1225. int sumi = 0;
  1226. #pragma unroll
  1227. for (int i = 0; i < vdr; ++i) {
  1228. // SIMD dot product of quantized values
  1229. sumi = __dp4a(v[i], u[i], sumi);
  1230. }
  1231. #ifdef GGML_CUDA_F16
  1232. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1233. const float d8d8 = tmp.x;
  1234. const float m8s8 = tmp.y;
  1235. #else
  1236. const float2 dm8f = __half22float2(dm8);
  1237. const float2 ds8f = __half22float2(ds8);
  1238. const float d8d8 = dm8f.x * ds8f.x;
  1239. const float m8s8 = dm8f.y * ds8f.y;
  1240. #endif // GGML_CUDA_F16
  1241. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1242. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1243. #else
  1244. return 0.0f; // only to satisfy the compiler
  1245. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1246. }
  1247. #define VDR_Q2_K_Q8_1_MMVQ 1
  1248. #define VDR_Q2_K_Q8_1_MMQ 2
  1249. // contiguous v/x values
  1250. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1251. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1252. const half2 & dm2, const float * __restrict__ d8) {
  1253. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1254. float sumf_d = 0.0f;
  1255. float sumf_m = 0.0f;
  1256. #pragma unroll
  1257. for (int i = 0; i < QR2_K; ++i) {
  1258. const int sc = scales[2*i];
  1259. const int vi = (v >> (2*i)) & 0x03030303;
  1260. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1261. // fill int with 4x m
  1262. int m = sc >> 4;
  1263. m |= m << 8;
  1264. m |= m << 16;
  1265. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1266. }
  1267. const float2 dm2f = __half22float2(dm2);
  1268. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1269. #else
  1270. return 0.0f; // only to satisfy the compiler
  1271. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1272. }
  1273. // contiguous u/y values
  1274. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1275. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1276. const half2 & dm2, const float & d8) {
  1277. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1278. int sumi_d = 0;
  1279. int sumi_m = 0;
  1280. #pragma unroll
  1281. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1282. int sumi_d_sc = 0;
  1283. const int sc = scales[i0 / (QI8_1/2)];
  1284. // fill int with 4x m
  1285. int m = sc >> 4;
  1286. m |= m << 8;
  1287. m |= m << 16;
  1288. #pragma unroll
  1289. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1290. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1291. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1292. }
  1293. sumi_d += sumi_d_sc * (sc & 0xF);
  1294. }
  1295. const float2 dm2f = __half22float2(dm2);
  1296. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1297. #else
  1298. return 0.0f; // only to satisfy the compiler
  1299. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1300. }
  1301. #define VDR_Q3_K_Q8_1_MMVQ 1
  1302. #define VDR_Q3_K_Q8_1_MMQ 2
  1303. // contiguous v/x values
  1304. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1305. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1306. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1307. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1308. float sumf = 0.0f;
  1309. #pragma unroll
  1310. for (int i = 0; i < QR3_K; ++i) {
  1311. const int isc = scale_offset + 2*i;
  1312. const int isc_low = isc % (QK_K/32);
  1313. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1314. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1315. const int isc_high = isc % (QK_K/64);
  1316. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1317. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1318. const int sc = (sc_low | sc_high) - 32;
  1319. const int vil = (vl >> (2*i)) & 0x03030303;
  1320. const int vih = ((vh >> i) << 2) & 0x04040404;
  1321. const int vi = __vsubss4(vil, vih);
  1322. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1323. }
  1324. return d3 * sumf;
  1325. #else
  1326. return 0.0f; // only to satisfy the compiler
  1327. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1328. }
  1329. // contiguous u/y values
  1330. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1331. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1332. const float & d3, const float & d8) {
  1333. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1334. int sumi = 0;
  1335. #pragma unroll
  1336. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1337. int sumi_sc = 0;
  1338. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1339. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1340. }
  1341. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1342. }
  1343. return d3*d8 * sumi;
  1344. #else
  1345. return 0.0f; // only to satisfy the compiler
  1346. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1347. }
  1348. #define VDR_Q4_K_Q8_1_MMVQ 2
  1349. #define VDR_Q4_K_Q8_1_MMQ 8
  1350. // contiguous v/x values
  1351. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1352. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1353. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1354. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1355. float sumf_d = 0.0f;
  1356. float sumf_m = 0.0f;
  1357. #pragma unroll
  1358. for (int i = 0; i < QR4_K; ++i) {
  1359. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1360. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1361. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1362. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1363. sumf_d += d8[i] * (dot1 * sc[i]);
  1364. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1365. }
  1366. const float2 dm4f = __half22float2(dm4);
  1367. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1368. #else
  1369. return 0.0f; // only to satisfy the compiler
  1370. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1371. }
  1372. // contiguous u/y values
  1373. // also used for q5_K
  1374. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1375. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1376. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1377. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1378. float sumf_d = 0.0f;
  1379. float sumf_m = 0.0f;
  1380. #pragma unroll
  1381. for (int i0 = 0; i0 < VDR_Q4_K_Q8_1_MMQ; i0 += (QI8_1/QR4_K)) {
  1382. int sumi_d = 0;
  1383. #pragma unroll
  1384. for (int i = i0; i < i0 + (QI8_1/QR4_K); ++i) {
  1385. sumi_d = __dp4a(v[2*i+0], u[2*i+0], sumi_d); // SIMD dot product
  1386. sumi_d = __dp4a(v[2*i+1], u[2*i+1], sumi_d); // SIMD dot product
  1387. }
  1388. const float2 ds8f = __half22float2(ds8[i0 / 4]);
  1389. sumf_d += ds8f.x * (sc[i0/4] * sumi_d);
  1390. sumf_m += ds8f.y * m[i0/4]; // sum of q8_1 block * q4_K min val
  1391. }
  1392. const float2 dm4f = __half22float2(dm4);
  1393. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1394. #else
  1395. return 0.0f; // only to satisfy the compiler
  1396. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1397. }
  1398. #define VDR_Q5_K_Q8_1_MMVQ 2
  1399. #define VDR_Q5_K_Q8_1_MMQ 8
  1400. // contiguous v/x values
  1401. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl(
  1402. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1403. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1404. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1405. float sumf_d = 0.0f;
  1406. float sumf_m = 0.0f;
  1407. #pragma unroll
  1408. for (int i = 0; i < QR5_K; ++i) {
  1409. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1410. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1411. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1412. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1413. const int v0i = vl0i | vh0i;
  1414. const int v1i = vl1i | vh1i;
  1415. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1416. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1417. sumf_d += d8[i] * (dot1 * sc[i]);
  1418. sumf_m += d8[i] * (dot2 * m[i]);
  1419. }
  1420. const float2 dm5f = __half22float2(dm5);
  1421. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1422. #else
  1423. return 0.0f; // only to satisfy the compiler
  1424. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1425. }
  1426. #define VDR_Q6_K_Q8_1_MMVQ 1
  1427. #define VDR_Q6_K_Q8_1_MMQ 8
  1428. // contiguous v/x values
  1429. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1430. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1431. const float & d, const float * __restrict__ d8) {
  1432. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1433. float sumf = 0.0f;
  1434. #pragma unroll
  1435. for (int i = 0; i < QR6_K; ++i) {
  1436. const int sc = scales[4*i];
  1437. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1438. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1439. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1440. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1441. }
  1442. return d*sumf;
  1443. #else
  1444. return 0.0f; // only to satisfy the compiler
  1445. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1446. }
  1447. // contiguous u/y values
  1448. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  1449. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  1450. const float & d6, const float * __restrict__ d8) {
  1451. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1452. float sumf_d = 0.0f;
  1453. #pragma unroll
  1454. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  1455. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  1456. #pragma unroll
  1457. for (int i = i0; i < i0 + 2; ++i) {
  1458. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  1459. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  1460. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  1461. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  1462. }
  1463. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  1464. }
  1465. return d6 * sumf_d;
  1466. #else
  1467. return 0.0f; // only to satisfy the compiler
  1468. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1469. }
  1470. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  1471. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1472. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  1473. int v[VDR_Q4_0_Q8_1_MMVQ];
  1474. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  1475. #pragma unroll
  1476. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  1477. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  1478. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1479. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  1480. }
  1481. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  1482. }
  1483. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1484. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  1485. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  1486. *x_ql = tile_x_qs;
  1487. *x_dm = (half2 *) tile_x_d;
  1488. }
  1489. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  1490. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1491. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1492. __builtin_assume(i_offset >= 0);
  1493. __builtin_assume(i_offset < nwarps);
  1494. __builtin_assume(k >= 0);
  1495. __builtin_assume(k < WARP_SIZE);
  1496. const int kbx = k / QI4_0;
  1497. const int kqsx = k % QI4_0;
  1498. const block_q4_0 * bx0 = (block_q4_0 *) vx;
  1499. float * x_dmf = (float *) x_dm;
  1500. #pragma unroll
  1501. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1502. int i = i0 + i_offset;
  1503. if (need_check) {
  1504. i = min(i, i_max);
  1505. }
  1506. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1507. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  1508. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  1509. }
  1510. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  1511. const int kbxd = k % blocks_per_tile_x_row;
  1512. #pragma unroll
  1513. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  1514. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  1515. if (need_check) {
  1516. i = min(i, i_max);
  1517. }
  1518. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1519. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  1520. }
  1521. }
  1522. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  1523. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1524. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1525. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1526. const float * x_dmf = (float *) x_dm;
  1527. int u[2*VDR_Q4_0_Q8_1_MMQ];
  1528. #pragma unroll
  1529. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  1530. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1531. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  1532. }
  1533. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  1534. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  1535. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1536. }
  1537. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  1538. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1539. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  1540. int v[VDR_Q4_1_Q8_1_MMVQ];
  1541. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  1542. #pragma unroll
  1543. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  1544. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  1545. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1546. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  1547. }
  1548. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  1549. }
  1550. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1551. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  1552. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  1553. *x_ql = tile_x_qs;
  1554. *x_dm = tile_x_dm;
  1555. }
  1556. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  1557. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1558. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1559. __builtin_assume(i_offset >= 0);
  1560. __builtin_assume(i_offset < nwarps);
  1561. __builtin_assume(k >= 0);
  1562. __builtin_assume(k < WARP_SIZE);
  1563. const int kbx = k / QI4_1;
  1564. const int kqsx = k % QI4_1;
  1565. const block_q4_1 * bx0 = (block_q4_1 *) vx;
  1566. #pragma unroll
  1567. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1568. int i = i0 + i_offset;
  1569. if (need_check) {
  1570. i = min(i, i_max);
  1571. }
  1572. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  1573. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1574. }
  1575. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  1576. const int kbxd = k % blocks_per_tile_x_row;
  1577. #pragma unroll
  1578. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  1579. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  1580. if (need_check) {
  1581. i = min(i, i_max);
  1582. }
  1583. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  1584. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  1585. }
  1586. }
  1587. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  1588. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1589. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1590. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1591. int u[2*VDR_Q4_1_Q8_1_MMQ];
  1592. #pragma unroll
  1593. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  1594. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1595. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  1596. }
  1597. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  1598. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  1599. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1600. }
  1601. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  1602. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1603. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  1604. int vl[VDR_Q5_0_Q8_1_MMVQ];
  1605. int vh[VDR_Q5_0_Q8_1_MMVQ];
  1606. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  1607. #pragma unroll
  1608. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  1609. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  1610. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  1611. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1612. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  1613. }
  1614. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  1615. }
  1616. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1617. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  1618. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  1619. *x_ql = tile_x_ql;
  1620. *x_dm = (half2 *) tile_x_d;
  1621. }
  1622. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  1623. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1624. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1625. __builtin_assume(i_offset >= 0);
  1626. __builtin_assume(i_offset < nwarps);
  1627. __builtin_assume(k >= 0);
  1628. __builtin_assume(k < WARP_SIZE);
  1629. const int kbx = k / QI5_0;
  1630. const int kqsx = k % QI5_0;
  1631. const block_q5_0 * bx0 = (block_q5_0 *) vx;
  1632. #pragma unroll
  1633. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1634. int i = i0 + i_offset;
  1635. if (need_check) {
  1636. i = min(i, i_max);
  1637. }
  1638. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1639. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  1640. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  1641. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  1642. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  1643. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  1644. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  1645. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  1646. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  1647. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  1648. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  1649. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  1650. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  1651. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  1652. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  1653. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  1654. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  1655. }
  1656. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  1657. const int kbxd = k % blocks_per_tile_x_row;
  1658. float * x_dmf = (float *) x_dm;
  1659. #pragma unroll
  1660. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  1661. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  1662. if (need_check) {
  1663. i = min(i, i_max);
  1664. }
  1665. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1666. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  1667. }
  1668. }
  1669. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  1670. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1671. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1672. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1673. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  1674. const float * x_dmf = (const float *) x_dm;
  1675. const float * y_df = (const float *) y_ds;
  1676. int u[2*VDR_Q5_0_Q8_1_MMQ];
  1677. #pragma unroll
  1678. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  1679. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1680. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  1681. }
  1682. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  1683. (&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)]);
  1684. }
  1685. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  1686. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1687. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  1688. int vl[VDR_Q5_1_Q8_1_MMVQ];
  1689. int vh[VDR_Q5_1_Q8_1_MMVQ];
  1690. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  1691. #pragma unroll
  1692. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  1693. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  1694. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  1695. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1696. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  1697. }
  1698. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  1699. }
  1700. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1701. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  1702. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  1703. *x_ql = tile_x_ql;
  1704. *x_dm = tile_x_dm;
  1705. }
  1706. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  1707. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1708. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1709. __builtin_assume(i_offset >= 0);
  1710. __builtin_assume(i_offset < nwarps);
  1711. __builtin_assume(k >= 0);
  1712. __builtin_assume(k < WARP_SIZE);
  1713. const int kbx = k / QI5_1;
  1714. const int kqsx = k % QI5_1;
  1715. const block_q5_1 * bx0 = (block_q5_1 *) vx;
  1716. #pragma unroll
  1717. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1718. int i = i0 + i_offset;
  1719. if (need_check) {
  1720. i = min(i, i_max);
  1721. }
  1722. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  1723. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1724. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  1725. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  1726. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  1727. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  1728. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  1729. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  1730. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  1731. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  1732. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  1733. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  1734. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  1735. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  1736. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  1737. }
  1738. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  1739. const int kbxd = k % blocks_per_tile_x_row;
  1740. #pragma unroll
  1741. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  1742. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  1743. if (need_check) {
  1744. i = min(i, i_max);
  1745. }
  1746. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  1747. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  1748. }
  1749. }
  1750. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  1751. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1752. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1753. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1754. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  1755. int u[2*VDR_Q5_1_Q8_1_MMQ];
  1756. #pragma unroll
  1757. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  1758. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1759. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  1760. }
  1761. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  1762. (&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)]);
  1763. }
  1764. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  1765. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1766. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  1767. int v[VDR_Q8_0_Q8_1_MMVQ];
  1768. int u[VDR_Q8_0_Q8_1_MMVQ];
  1769. #pragma unroll
  1770. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  1771. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  1772. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1773. }
  1774. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, bq8_1->ds.x);
  1775. }
  1776. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1777. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  1778. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  1779. *x_ql = tile_x_qs;
  1780. *x_dm = (half2 *) tile_x_d;
  1781. }
  1782. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  1783. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1784. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1785. __builtin_assume(i_offset >= 0);
  1786. __builtin_assume(i_offset < nwarps);
  1787. __builtin_assume(k >= 0);
  1788. __builtin_assume(k < WARP_SIZE);
  1789. const int kbx = k / QI8_0;
  1790. const int kqsx = k % QI8_0;
  1791. float * x_dmf = (float *) x_dm;
  1792. const block_q8_0 * bx0 = (block_q8_0 *) vx;
  1793. #pragma unroll
  1794. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1795. int i = i0 + i_offset;
  1796. if (need_check) {
  1797. i = min(i, i_max);
  1798. }
  1799. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1800. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  1801. }
  1802. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  1803. const int kbxd = k % blocks_per_tile_x_row;
  1804. #pragma unroll
  1805. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  1806. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  1807. if (need_check) {
  1808. i = min(i, i_max);
  1809. }
  1810. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1811. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  1812. }
  1813. }
  1814. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  1815. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1816. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1817. const float * x_dmf = (const float *) x_dm;
  1818. const float * y_df = (const float *) y_ds;
  1819. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  1820. (&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],
  1821. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  1822. }
  1823. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  1824. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1825. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  1826. const int bq8_offset = QR2_K * (iqs / QI8_1);
  1827. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  1828. const uint8_t * scales = bq2_K->scales + scale_offset;
  1829. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  1830. int u[QR2_K];
  1831. float d8[QR2_K];
  1832. #pragma unroll
  1833. for (int i = 0; i < QR2_K; ++ i) {
  1834. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  1835. d8[i] = bq8_1[bq8_offset + i].ds.x;
  1836. }
  1837. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  1838. }
  1839. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1840. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  1841. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  1842. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  1843. *x_ql = tile_x_ql;
  1844. *x_dm = tile_x_dm;
  1845. *x_sc = tile_x_sc;
  1846. }
  1847. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  1848. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1849. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1850. __builtin_assume(i_offset >= 0);
  1851. __builtin_assume(i_offset < nwarps);
  1852. __builtin_assume(k >= 0);
  1853. __builtin_assume(k < WARP_SIZE);
  1854. const int kbx = k / QI2_K;
  1855. const int kqsx = k % QI2_K;
  1856. const block_q2_K * bx0 = (block_q2_K *) vx;
  1857. #pragma unroll
  1858. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1859. int i = i0 + i_offset;
  1860. if (need_check) {
  1861. i = min(i, i_max);
  1862. }
  1863. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  1864. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1865. }
  1866. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  1867. const int kbxd = k % blocks_per_tile_x_row;
  1868. #pragma unroll
  1869. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  1870. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  1871. if (need_check) {
  1872. i = min(i, i_max);
  1873. }
  1874. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  1875. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  1876. }
  1877. #pragma unroll
  1878. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  1879. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  1880. if (need_check) {
  1881. i = min(i, i_max);
  1882. }
  1883. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  1884. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  1885. }
  1886. }
  1887. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  1888. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1889. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1890. const int kbx = k / QI2_K;
  1891. const int ky = (k % QI2_K) * QR2_K;
  1892. const float * y_df = (const float *) y_ds;
  1893. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  1894. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  1895. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  1896. #pragma unroll
  1897. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  1898. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  1899. }
  1900. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  1901. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  1902. 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]);
  1903. }
  1904. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  1905. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1906. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  1907. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  1908. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  1909. const float d = bq3_K->d;
  1910. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  1911. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  1912. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  1913. int u[QR3_K];
  1914. float d8[QR3_K];
  1915. #pragma unroll
  1916. for (int i = 0; i < QR3_K; ++i) {
  1917. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  1918. d8[i] = bq8_1[bq8_offset + i].ds.x;
  1919. }
  1920. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  1921. }
  1922. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1923. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  1924. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  1925. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  1926. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  1927. *x_ql = tile_x_ql;
  1928. *x_dm = tile_x_dm;
  1929. *x_qh = tile_x_qh;
  1930. *x_sc = tile_x_sc;
  1931. }
  1932. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  1933. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1934. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1935. __builtin_assume(i_offset >= 0);
  1936. __builtin_assume(i_offset < nwarps);
  1937. __builtin_assume(k >= 0);
  1938. __builtin_assume(k < WARP_SIZE);
  1939. const int kbx = k / QI3_K;
  1940. const int kqsx = k % QI3_K;
  1941. const block_q3_K * bx0 = (block_q3_K *) vx;
  1942. #pragma unroll
  1943. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1944. int i = i0 + i_offset;
  1945. if (need_check) {
  1946. i = min(i, i_max);
  1947. }
  1948. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  1949. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  1950. }
  1951. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  1952. const int kbxd = k % blocks_per_tile_x_row;
  1953. float * x_dmf = (float *) x_dm;
  1954. #pragma unroll
  1955. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  1956. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  1957. if (need_check) {
  1958. i = min(i, i_max);
  1959. }
  1960. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  1961. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  1962. }
  1963. #pragma unroll
  1964. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  1965. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  1966. if (need_check) {
  1967. i = min(i, i_max);
  1968. }
  1969. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  1970. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  1971. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  1972. }
  1973. #pragma unroll
  1974. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  1975. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  1976. if (need_check) {
  1977. i = min(i, i_max);
  1978. }
  1979. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  1980. const int ksc = k % (QI3_K/4);
  1981. const int ksc_low = ksc % (QI3_K/8);
  1982. const int shift_low = 4 * (ksc / (QI3_K/8));
  1983. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  1984. const int ksc_high = QI3_K/8;
  1985. const int shift_high = 2 * ksc;
  1986. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  1987. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  1988. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  1989. }
  1990. }
  1991. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  1992. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1993. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1994. const int kbx = k / QI3_K;
  1995. const int ky = (k % QI3_K) * QR3_K;
  1996. const float * x_dmf = (const float *) x_dm;
  1997. const float * y_df = (const float *) y_ds;
  1998. const int8_t * scales = ((int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  1999. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2000. #pragma unroll
  2001. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2002. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2003. const int shift = 2 * ((ky % 32) / 8);
  2004. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2005. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2006. const int vlh = (vh << 2) & 0x04040404;
  2007. v[l] = __vsubss4(vll, vlh);
  2008. }
  2009. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2010. 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]);
  2011. }
  2012. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2013. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2014. #ifndef GGML_QKK_64
  2015. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2016. int v[2];
  2017. int u[2*QR4_K];
  2018. float d8[QR4_K];
  2019. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2020. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2021. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2022. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2023. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2024. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2025. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2026. v[0] = q4[0];
  2027. v[1] = q4[4];
  2028. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2029. uint16_t aux[2];
  2030. const int j = bq8_offset/2;
  2031. if (j < 2) {
  2032. aux[0] = scales[j+0] & 0x3f3f;
  2033. aux[1] = scales[j+2] & 0x3f3f;
  2034. } else {
  2035. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2036. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2037. }
  2038. const uint8_t * sc = (const uint8_t *)aux;
  2039. const uint8_t * m = sc + 2;
  2040. for (int i = 0; i < QR4_K; ++i) {
  2041. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2042. d8[i] = bq8i->ds.x;
  2043. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2044. u[2*i+0] = q8[0];
  2045. u[2*i+1] = q8[4];
  2046. }
  2047. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2048. #else
  2049. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2050. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2051. float sumf_d = 0.0f;
  2052. float sumf_m = 0.0f;
  2053. uint16_t aux16[2];
  2054. const uint8_t * s = (const uint8_t *)aux16;
  2055. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2056. aux16[0] = a[0] & 0x0f0f;
  2057. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2058. const float dall = bq4_K->d[0];
  2059. const float dmin = bq4_K->d[1];
  2060. const float d8_1 = bq8_1[0].ds.x;
  2061. const float d8_2 = bq8_1[1].ds.x;
  2062. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2063. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2064. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2065. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2066. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2067. const int v1 = q4[0];
  2068. const int v2 = q4[4];
  2069. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2070. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2071. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2072. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2073. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2074. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2075. return dall * sumf_d - dmin * sumf_m;
  2076. #else
  2077. return 0.0f; // only to satisfy the compiler
  2078. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2079. #endif
  2080. }
  2081. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2082. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2083. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2084. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2085. *x_ql = tile_x_ql;
  2086. *x_dm = tile_x_dm;
  2087. *x_sc = tile_x_sc;
  2088. }
  2089. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2090. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2091. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2092. __builtin_assume(i_offset >= 0);
  2093. __builtin_assume(i_offset < nwarps);
  2094. __builtin_assume(k >= 0);
  2095. __builtin_assume(k < WARP_SIZE);
  2096. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2097. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2098. const block_q4_K * bx0 = (block_q4_K *) vx;
  2099. #pragma unroll
  2100. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2101. int i = i0 + i_offset;
  2102. if (need_check) {
  2103. i = min(i, i_max);
  2104. }
  2105. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2106. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2107. }
  2108. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2109. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2110. #pragma unroll
  2111. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2112. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2113. if (need_check) {
  2114. i = min(i, i_max);
  2115. }
  2116. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2117. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2118. }
  2119. #pragma unroll
  2120. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2121. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2122. if (need_check) {
  2123. i = min(i, i_max);
  2124. }
  2125. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2126. const int * scales = (int *) bxi->scales;
  2127. const int ksc = k % (WARP_SIZE/8);
  2128. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2129. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2130. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2131. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2132. }
  2133. }
  2134. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2135. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2136. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2137. int v[QR4_K*VDR_Q4_K_Q8_1_MMQ];
  2138. #pragma unroll
  2139. for (int l = 0; l < VDR_Q4_K_Q8_1_MMQ; ++l) {
  2140. v[l + 0] = (x_ql[i * (WARP_SIZE + 1) + k + l] >> 0) & 0x0F0F0F0F;
  2141. v[l + (QI4_K/4)] = (x_ql[i * (WARP_SIZE + 1) + k + l] >> 4) & 0x0F0F0F0F;
  2142. }
  2143. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2144. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2145. return vec_dot_q4_K_q8_1_impl_mmq(v, &y_qs[index_y], sc, sc+8, x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2146. }
  2147. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2148. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2149. #ifndef GGML_QKK_64
  2150. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2151. int vl[2];
  2152. int vh[2];
  2153. int u[2*QR5_K];
  2154. float d8[QR5_K];
  2155. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2156. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2157. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2158. vl[0] = ql[0];
  2159. vl[1] = ql[4];
  2160. vh[0] = qh[0] >> bq8_offset;
  2161. vh[1] = qh[4] >> bq8_offset;
  2162. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2163. uint16_t aux[2];
  2164. const int j = bq8_offset/2;
  2165. if (j < 2) {
  2166. aux[0] = scales[j+0] & 0x3f3f;
  2167. aux[1] = scales[j+2] & 0x3f3f;
  2168. } else {
  2169. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2170. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2171. }
  2172. const uint8_t * sc = (const uint8_t *)aux;
  2173. const uint8_t * m = sc + 2;
  2174. #pragma unroll
  2175. for (int i = 0; i < QR5_K; ++i) {
  2176. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2177. d8[i] = bq8i->ds.x;
  2178. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2179. u[2*i+0] = q8[0];
  2180. u[2*i+1] = q8[4];
  2181. }
  2182. return vec_dot_q5_K_q8_1_impl(vl, vh, u, sc, m, bq5_K->dm, d8);
  2183. #else
  2184. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2185. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2186. const int8_t * s = bq5_K->scales;
  2187. const float d = bq5_K->d;
  2188. const float d8_1 = bq8_1[0].ds.x;
  2189. const float d8_2 = bq8_1[1].ds.x;
  2190. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2191. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2192. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2193. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2194. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2195. const int vl1 = ql[0];
  2196. const int vl2 = ql[4];
  2197. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2198. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2199. const int in = step%8; // 0, 4, 0, 4
  2200. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2201. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2202. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2203. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2204. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2205. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2206. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2207. return d * sumf_d;
  2208. #else
  2209. return 0.0f; // only to satisfy the compiler
  2210. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2211. #endif
  2212. }
  2213. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2214. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2215. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2216. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2217. *x_ql = tile_x_ql;
  2218. *x_dm = tile_x_dm;
  2219. *x_sc = tile_x_sc;
  2220. }
  2221. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2222. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2223. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2224. __builtin_assume(i_offset >= 0);
  2225. __builtin_assume(i_offset < nwarps);
  2226. __builtin_assume(k >= 0);
  2227. __builtin_assume(k < WARP_SIZE);
  2228. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2229. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2230. const block_q5_K * bx0 = (block_q5_K *) vx;
  2231. #pragma unroll
  2232. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2233. int i = i0 + i_offset;
  2234. if (need_check) {
  2235. i = min(i, i_max);
  2236. }
  2237. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2238. const int ky = QR5_K*kqsx;
  2239. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2240. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2241. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2242. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2243. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2244. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2245. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2246. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2247. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2248. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2249. }
  2250. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2251. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2252. #pragma unroll
  2253. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2254. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2255. if (need_check) {
  2256. i = min(i, i_max);
  2257. }
  2258. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2259. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2260. }
  2261. #pragma unroll
  2262. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2263. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2264. if (need_check) {
  2265. i = min(i, i_max);
  2266. }
  2267. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2268. const int * scales = (int *) bxi->scales;
  2269. const int ksc = k % (WARP_SIZE/8);
  2270. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2271. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2272. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2273. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2274. }
  2275. }
  2276. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2277. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2278. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2279. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2280. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2281. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2282. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8, x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2283. }
  2284. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2285. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2286. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2287. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2288. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2289. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2290. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2291. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2292. const int8_t * scales = bq6_K->scales + scale_offset;
  2293. int u[QR6_K];
  2294. float d8[QR6_K];
  2295. #pragma unroll
  2296. for (int i = 0; i < QR6_K; ++i) {
  2297. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2298. d8[i] = bq8_1[bq8_offset + 2*i].ds.x;
  2299. }
  2300. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2301. }
  2302. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2303. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2304. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2305. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2306. *x_ql = tile_x_ql;
  2307. *x_dm = tile_x_dm;
  2308. *x_sc = tile_x_sc;
  2309. }
  2310. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2311. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2312. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2313. __builtin_assume(i_offset >= 0);
  2314. __builtin_assume(i_offset < nwarps);
  2315. __builtin_assume(k >= 0);
  2316. __builtin_assume(k < WARP_SIZE);
  2317. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2318. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2319. const block_q6_K * bx0 = (block_q6_K *) vx;
  2320. #pragma unroll
  2321. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2322. int i = i0 + i_offset;
  2323. if (need_check) {
  2324. i = min(i, i_max);
  2325. }
  2326. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2327. const int ky = QR6_K*kqsx;
  2328. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2329. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2330. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2331. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2332. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2333. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2334. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2335. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2336. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2337. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2338. }
  2339. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2340. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2341. float * x_dmf = (float *) x_dm;
  2342. #pragma unroll
  2343. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2344. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2345. if (need_check) {
  2346. i = min(i, i_max);
  2347. }
  2348. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2349. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2350. }
  2351. #pragma unroll
  2352. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2353. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2354. if (need_check) {
  2355. i = min(i, i_max);
  2356. }
  2357. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2358. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2359. }
  2360. }
  2361. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2362. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2363. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2364. const float * x_dmf = (const float *) x_dm;
  2365. const float * y_df = (const float *) y_ds;
  2366. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2367. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2368. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2369. 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]);
  2370. }
  2371. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2372. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2373. static __global__ void mul_mat_q(
  2374. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2375. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2376. const block_q_t * x = (const block_q_t *) vx;
  2377. const block_q8_1 * y = (const block_q8_1 *) vy;
  2378. const int blocks_per_row_x = ncols_x / qk;
  2379. const int blocks_per_col_y = nrows_y / QK8_1;
  2380. const int blocks_per_warp = WARP_SIZE / qi;
  2381. const int & ncols_dst = ncols_y;
  2382. const int row_dst_0 = blockIdx.x*mmq_y;
  2383. const int & row_x_0 = row_dst_0;
  2384. const int row_dst = row_dst_0 + threadIdx.x;
  2385. const int col_dst_0 = blockIdx.y*mmq_x;
  2386. const int & col_y_0 = col_dst_0;
  2387. int * tile_x_ql = nullptr;
  2388. half2 * tile_x_dm = nullptr;
  2389. int * tile_x_qh = nullptr;
  2390. int * tile_x_sc = nullptr;
  2391. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2392. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2393. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2394. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {0.0f};
  2395. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2396. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2397. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2398. #pragma unroll
  2399. for (int ir = 0; ir < qr; ++ir) {
  2400. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2401. const int kbxd = kqs / QI8_1;
  2402. #pragma unroll
  2403. for (int i = 0; i < mmq_x; i += nwarps) {
  2404. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2405. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2406. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2407. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2408. }
  2409. #pragma unroll
  2410. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2411. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2412. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2413. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2414. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2415. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  2416. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  2417. if (need_sum) {
  2418. *dsi_dst = *dsi_src;
  2419. } else {
  2420. float * dfi_dst = (float *) dsi_dst;
  2421. *dfi_dst = (*dsi_src).x;
  2422. }
  2423. }
  2424. __syncthreads();
  2425. // #pragma unroll // unrolling this loop causes too much register pressure
  2426. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  2427. #pragma unroll
  2428. for (int j = 0; j < mmq_x; j += nwarps) {
  2429. #pragma unroll
  2430. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2431. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  2432. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  2433. threadIdx.x + i, threadIdx.y + j, k);
  2434. }
  2435. }
  2436. }
  2437. __syncthreads();
  2438. }
  2439. }
  2440. if (row_dst >= nrows_dst) {
  2441. return;
  2442. }
  2443. for (int j = 0; j < mmq_x; j += nwarps) {
  2444. const int col_dst = col_dst_0 + j + threadIdx.y;
  2445. if (col_dst >= ncols_dst) {
  2446. return;
  2447. }
  2448. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2449. dst[col_dst*nrows_dst + row_dst + i] = sum[i/WARP_SIZE][j/nwarps];
  2450. }
  2451. }
  2452. }
  2453. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  2454. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  2455. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  2456. if (row >= nrows) {
  2457. return;
  2458. }
  2459. const int blocks_per_row = ncols / qk;
  2460. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  2461. // partial sum for each thread
  2462. float tmp = 0.0f;
  2463. const block_q_t * x = (const block_q_t *) vx;
  2464. const block_q8_1 * y = (const block_q8_1 *) vy;
  2465. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  2466. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  2467. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  2468. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  2469. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  2470. }
  2471. // sum up partial sums and write back result
  2472. #pragma unroll
  2473. for (int mask = 16; mask > 0; mask >>= 1) {
  2474. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2475. }
  2476. if (threadIdx.x == 0) {
  2477. dst[row] = tmp;
  2478. }
  2479. }
  2480. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  2481. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  2482. // qk = quantized weights per x block
  2483. // qr = number of quantized weights per data value in x block
  2484. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  2485. if (row >= nrows) {
  2486. return;
  2487. }
  2488. const int tid = threadIdx.x;
  2489. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  2490. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  2491. const int y_offset = qr == 1 ? 1 : qk/2;
  2492. // partial sum for each thread
  2493. #ifdef GGML_CUDA_F16
  2494. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  2495. #else
  2496. float tmp = 0.0f;
  2497. #endif // GGML_CUDA_F16
  2498. for (int i = 0; i < ncols; i += iter_stride) {
  2499. const int col = i + vals_per_iter*tid;
  2500. const int ib = (row*ncols + col)/qk; // x block index
  2501. const int iqs = (col%qk)/qr; // x quant index
  2502. const int iybs = col - col%qk; // y block start index
  2503. // processing >2 values per i iter is faster for fast GPUs
  2504. #pragma unroll
  2505. for (int j = 0; j < vals_per_iter; j += 2) {
  2506. // process 2 vals per j iter
  2507. // dequantize
  2508. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  2509. dfloat2 v;
  2510. dequantize_kernel(vx, ib, iqs + j/qr, v);
  2511. // matrix multiplication
  2512. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  2513. #ifdef GGML_CUDA_F16
  2514. tmp += __hmul2(v, {
  2515. y[iybs + iqs + j/qr + 0],
  2516. y[iybs + iqs + j/qr + y_offset]
  2517. });
  2518. #else
  2519. tmp += v.x * y[iybs + iqs + j/qr + 0];
  2520. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  2521. #endif // GGML_CUDA_F16
  2522. }
  2523. }
  2524. // sum up partial sums and write back result
  2525. #pragma unroll
  2526. for (int mask = 16; mask > 0; mask >>= 1) {
  2527. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2528. }
  2529. if (tid == 0) {
  2530. #ifdef GGML_CUDA_F16
  2531. dst[row] = tmp.x + tmp.y;
  2532. #else
  2533. dst[row] = tmp;
  2534. #endif // GGML_CUDA_F16
  2535. }
  2536. }
  2537. static __global__ void mul_mat_p021_f16_f32(
  2538. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  2539. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  2540. const half * x = (const half *) vx;
  2541. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  2542. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  2543. const int channel_x = channel / (nchannels_y / nchannels_x);
  2544. const int nrows_y = ncols_x;
  2545. const int nrows_dst = nrows_x;
  2546. const int row_dst = row_x;
  2547. float tmp = 0.0f;
  2548. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  2549. const int col_x = col_x0 + threadIdx.x;
  2550. if (col_x >= ncols_x) {
  2551. break;
  2552. }
  2553. // x is transposed and permuted
  2554. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  2555. const float xi = __half2float(x[ix]);
  2556. const int row_y = col_x;
  2557. // y is not transposed but permuted
  2558. const int iy = channel*nrows_y + row_y;
  2559. tmp += xi * y[iy];
  2560. }
  2561. // dst is not transposed and not permuted
  2562. const int idst = channel*nrows_dst + row_dst;
  2563. // sum up partial sums and write back result
  2564. #pragma unroll
  2565. for (int mask = 16; mask > 0; mask >>= 1) {
  2566. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2567. }
  2568. if (threadIdx.x == 0) {
  2569. dst[idst] = tmp;
  2570. }
  2571. }
  2572. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  2573. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  2574. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  2575. const half * x = (const half *) vx;
  2576. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  2577. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  2578. const int channel_x = channel / channel_x_divisor;
  2579. const int nrows_y = ncols_x;
  2580. const int nrows_dst = nrows_x;
  2581. const int row_dst = row_x;
  2582. const int idst = channel*nrows_dst + row_dst;
  2583. float tmp = 0.0f;
  2584. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  2585. const int col_x = col_x0 + threadIdx.x;
  2586. if (col_x >= ncols_x) {
  2587. break;
  2588. }
  2589. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  2590. const float xi = __half2float(x[ix]);
  2591. const int row_y = col_x;
  2592. const int iy = channel*nrows_y + row_y;
  2593. tmp += xi * y[iy];
  2594. }
  2595. // sum up partial sums and write back result
  2596. #pragma unroll
  2597. for (int mask = 16; mask > 0; mask >>= 1) {
  2598. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2599. }
  2600. if (threadIdx.x == 0) {
  2601. dst[idst] = tmp;
  2602. }
  2603. }
  2604. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  2605. const float * xi = (const float *) cxi;
  2606. float * dsti = (float *) cdsti;
  2607. *dsti = *xi;
  2608. }
  2609. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  2610. const float * xi = (const float *) cxi;
  2611. half * dsti = (half *) cdsti;
  2612. *dsti = __float2half(*xi);
  2613. }
  2614. template <cpy_kernel_t cpy_1>
  2615. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  2616. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  2617. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  2618. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2619. if (i >= ne) {
  2620. return;
  2621. }
  2622. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  2623. // then combine those indices with the corresponding byte offsets to get the total offsets
  2624. const int i02 = i / (ne00*ne01);
  2625. const int i01 = (i - i02*ne01*ne00) / ne00;
  2626. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  2627. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  2628. const int i12 = i / (ne10*ne11);
  2629. const int i11 = (i - i12*ne10*ne11) / ne10;
  2630. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  2631. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  2632. cpy_1(cx + x_offset, cdst + dst_offset);
  2633. }
  2634. // rope == RoPE == rotary positional embedding
  2635. static __global__ void rope_f32(const float * x, float * dst, const int ncols, const float p0,
  2636. const float p_delta, const int p_delta_rows, const float theta_scale) {
  2637. const int col = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2638. if (col >= ncols) {
  2639. return;
  2640. }
  2641. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  2642. const int i = row*ncols + col;
  2643. const float theta = (p0 + p_delta * (row/p_delta_rows))*powf(theta_scale, col/2);
  2644. const float sin_theta = sinf(theta);
  2645. const float cos_theta = cosf(theta);
  2646. const float x0 = x[i + 0];
  2647. const float x1 = x[i + 1];
  2648. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  2649. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  2650. }
  2651. static __global__ void rope_glm_f32(const float * x, float * dst, const int ncols, const float p, const float block_p, const float theta_scale) {
  2652. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  2653. const int half_n_dims = ncols/4;
  2654. if (col >= half_n_dims) {
  2655. return;
  2656. }
  2657. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  2658. const int i = row*ncols + col;
  2659. const float col_theta_scale = powf(theta_scale, col);
  2660. const float theta = p*col_theta_scale;
  2661. const float sin_theta = sinf(theta);
  2662. const float cos_theta = cosf(theta);
  2663. const float x0 = x[i + 0];
  2664. const float x1 = x[i + half_n_dims];
  2665. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  2666. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  2667. const float block_theta = block_p*col_theta_scale;
  2668. const float sin_block_theta = sinf(block_theta);
  2669. const float cos_block_theta = cosf(block_theta);
  2670. const float x2 = x[i + half_n_dims * 2];
  2671. const float x3 = x[i + half_n_dims * 3];
  2672. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  2673. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  2674. }
  2675. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  2676. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  2677. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  2678. if (col >= ncols) {
  2679. return;
  2680. }
  2681. const int i = row*ncols + col;
  2682. // dst[i] = col > n_past + row ? -INFINITY : x[i];
  2683. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  2684. }
  2685. // the CUDA soft max implementation differs from the CPU implementation
  2686. // instead of doubles floats are used
  2687. // values are also not normalized to the maximum value by subtracting it in the exponential function
  2688. // theoretically these changes could cause problems with rounding error and arithmetic overflow but for LLaMa it seems to be fine
  2689. static __global__ void soft_max_f32(const float * x, float * dst, const int ncols) {
  2690. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  2691. const int block_size = blockDim.x;
  2692. const int tid = threadIdx.x;
  2693. float tmp = 0.0;
  2694. for (int block_start = 0; block_start < ncols; block_start += block_size) {
  2695. const int col = block_start + tid;
  2696. if (col >= ncols) {
  2697. break;
  2698. }
  2699. const int i = row*ncols + col;
  2700. const float val = expf(x[i]);
  2701. tmp += val;
  2702. dst[i] = val;
  2703. }
  2704. // sum up partial sums
  2705. #pragma unroll
  2706. for (int mask = 16; mask > 0; mask >>= 1) {
  2707. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2708. }
  2709. for (int block_start = 0; block_start < ncols; block_start += block_size) {
  2710. const int col = block_start + tid;
  2711. if (col >= ncols) {
  2712. break;
  2713. }
  2714. const int i = row*ncols + col;
  2715. dst[i] /= tmp;
  2716. }
  2717. }
  2718. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  2719. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2720. if (i >= k) {
  2721. return;
  2722. }
  2723. dst[i] = scale * x[i];
  2724. }
  2725. static void add_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  2726. const int num_blocks = (kx + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  2727. add_f32<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  2728. }
  2729. static void add_f16_f32_f16_cuda(const half * x, const float * y, half * dst, const int k, cudaStream_t stream) {
  2730. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  2731. add_f16_f32_f16<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  2732. }
  2733. static void mul_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  2734. const int num_blocks = (kx + CUDA_MUL_BLOCK_SIZE - 1) / CUDA_MUL_BLOCK_SIZE;
  2735. mul_f32<<<num_blocks, CUDA_MUL_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  2736. }
  2737. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  2738. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  2739. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  2740. }
  2741. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  2742. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  2743. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  2744. }
  2745. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  2746. GGML_ASSERT(ncols % WARP_SIZE == 0);
  2747. const dim3 block_dims(WARP_SIZE, 1, 1);
  2748. norm_f32<<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  2749. }
  2750. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  2751. GGML_ASSERT(ncols % WARP_SIZE == 0);
  2752. const dim3 block_dims(WARP_SIZE, 1, 1);
  2753. rms_norm_f32<<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  2754. }
  2755. 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) {
  2756. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  2757. const dim3 num_blocks(block_num_x, ky, 1);
  2758. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  2759. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  2760. }
  2761. static void dequantize_row_q4_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2762. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  2763. dequantize_block<QK4_0, QR4_0, dequantize_q4_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  2764. }
  2765. static void dequantize_row_q4_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2766. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  2767. dequantize_block<QK4_1, QR4_1, dequantize_q4_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  2768. }
  2769. static void dequantize_row_q5_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2770. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  2771. dequantize_block<QK5_0, QR5_0, dequantize_q5_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  2772. }
  2773. static void dequantize_row_q5_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2774. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  2775. dequantize_block<QK5_1, QR5_1, dequantize_q5_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  2776. }
  2777. static void dequantize_row_q8_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2778. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  2779. dequantize_block<QK8_0, QR8_0, dequantize_q8_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  2780. }
  2781. static void dequantize_row_q2_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2782. const int nb = k / QK_K;
  2783. #if QK_K == 256
  2784. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  2785. #else
  2786. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  2787. #endif
  2788. }
  2789. static void dequantize_row_q3_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2790. const int nb = k / QK_K;
  2791. #if QK_K == 256
  2792. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  2793. #else
  2794. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  2795. #endif
  2796. }
  2797. static void dequantize_row_q4_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2798. const int nb = k / QK_K;
  2799. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  2800. }
  2801. static void dequantize_row_q5_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2802. const int nb = k / QK_K;
  2803. #if QK_K == 256
  2804. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  2805. #else
  2806. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  2807. #endif
  2808. }
  2809. static void dequantize_row_q6_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2810. const int nb = k / QK_K;
  2811. #if QK_K == 256
  2812. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  2813. #else
  2814. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  2815. #endif
  2816. }
  2817. 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) {
  2818. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  2819. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2820. const dim3 block_nums(1, block_num_y, 1);
  2821. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2822. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  2823. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2824. }
  2825. 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) {
  2826. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  2827. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2828. const dim3 block_nums(1, block_num_y, 1);
  2829. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2830. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  2831. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2832. }
  2833. 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) {
  2834. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  2835. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2836. const dim3 block_nums(1, block_num_y, 1);
  2837. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2838. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  2839. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2840. }
  2841. 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) {
  2842. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  2843. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2844. const dim3 block_nums(1, block_num_y, 1);
  2845. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2846. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  2847. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2848. }
  2849. 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) {
  2850. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  2851. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2852. const dim3 block_nums(1, block_num_y, 1);
  2853. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2854. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  2855. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2856. }
  2857. 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) {
  2858. GGML_ASSERT(ncols % QK_K == 0);
  2859. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  2860. const int block_num_y = (nrows + ny - 1) / ny;
  2861. const dim3 block_nums(1, block_num_y, 1);
  2862. const dim3 block_dims(32, ny, 1);
  2863. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2864. }
  2865. 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) {
  2866. GGML_ASSERT(ncols % QK_K == 0);
  2867. const int ny = 2 / K_QUANTS_PER_ITERATION;
  2868. const int block_num_y = (nrows + ny - 1) / ny;
  2869. const dim3 block_nums(1, block_num_y, 1);
  2870. const dim3 block_dims(32, ny, 1);
  2871. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2872. }
  2873. 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) {
  2874. GGML_ASSERT(ncols % QK_K == 0);
  2875. const int ny = 2 / K_QUANTS_PER_ITERATION;
  2876. const int block_num_y = (nrows + ny - 1) / ny;
  2877. const dim3 block_nums(1, block_num_y, 1);
  2878. const dim3 block_dims(32, ny, 1);
  2879. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2880. }
  2881. 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) {
  2882. GGML_ASSERT(ncols % QK_K == 0);
  2883. const dim3 block_dims(32, 1, 1);
  2884. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  2885. }
  2886. 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) {
  2887. GGML_ASSERT(ncols % QK_K == 0);
  2888. const int ny = 2 / K_QUANTS_PER_ITERATION;
  2889. const int block_num_y = (nrows + ny - 1) / ny;
  2890. const dim3 block_nums(1, block_num_y, 1);
  2891. const dim3 block_dims(32, ny, 1);
  2892. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2893. }
  2894. 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) {
  2895. GGML_ASSERT(ncols % QK4_0 == 0);
  2896. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2897. const dim3 block_nums(1, block_num_y, 1);
  2898. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2899. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  2900. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2901. }
  2902. 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) {
  2903. GGML_ASSERT(ncols % QK4_1 == 0);
  2904. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2905. const dim3 block_nums(1, block_num_y, 1);
  2906. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2907. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  2908. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2909. }
  2910. 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) {
  2911. GGML_ASSERT(ncols % QK5_0 == 0);
  2912. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2913. const dim3 block_nums(1, block_num_y, 1);
  2914. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2915. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  2916. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2917. }
  2918. 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) {
  2919. GGML_ASSERT(ncols % QK5_1 == 0);
  2920. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2921. const dim3 block_nums(1, block_num_y, 1);
  2922. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2923. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  2924. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2925. }
  2926. 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) {
  2927. GGML_ASSERT(ncols % QK8_0 == 0);
  2928. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2929. const dim3 block_nums(1, block_num_y, 1);
  2930. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2931. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  2932. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2933. }
  2934. 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) {
  2935. GGML_ASSERT(ncols % QK_K == 0);
  2936. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2937. const dim3 block_nums(1, block_num_y, 1);
  2938. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2939. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  2940. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2941. }
  2942. 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) {
  2943. GGML_ASSERT(ncols % QK_K == 0);
  2944. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2945. const dim3 block_nums(1, block_num_y, 1);
  2946. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2947. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  2948. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2949. }
  2950. 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) {
  2951. GGML_ASSERT(ncols % QK_K == 0);
  2952. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2953. const dim3 block_nums(1, block_num_y, 1);
  2954. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2955. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  2956. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2957. }
  2958. 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) {
  2959. GGML_ASSERT(ncols % QK_K == 0);
  2960. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2961. const dim3 block_nums(1, block_num_y, 1);
  2962. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2963. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  2964. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2965. }
  2966. 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) {
  2967. GGML_ASSERT(ncols % QK_K == 0);
  2968. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2969. const dim3 block_nums(1, block_num_y, 1);
  2970. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2971. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  2972. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  2973. }
  2974. static void convert_fp16_to_fp32_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  2975. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  2976. dequantize_block<1, 1, convert_f16><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  2977. }
  2978. 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) {
  2979. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  2980. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  2981. const dim3 block_nums(1, block_num_y, 1);
  2982. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  2983. dequantize_mul_mat_vec<1, 1, convert_f16>
  2984. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  2985. }
  2986. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  2987. switch (type) {
  2988. case GGML_TYPE_Q4_0:
  2989. return dequantize_row_q4_0_cuda;
  2990. case GGML_TYPE_Q4_1:
  2991. return dequantize_row_q4_1_cuda;
  2992. case GGML_TYPE_Q5_0:
  2993. return dequantize_row_q5_0_cuda;
  2994. case GGML_TYPE_Q5_1:
  2995. return dequantize_row_q5_1_cuda;
  2996. case GGML_TYPE_Q8_0:
  2997. return dequantize_row_q8_0_cuda;
  2998. case GGML_TYPE_Q2_K:
  2999. return dequantize_row_q2_K_cuda;
  3000. case GGML_TYPE_Q3_K:
  3001. return dequantize_row_q3_K_cuda;
  3002. case GGML_TYPE_Q4_K:
  3003. return dequantize_row_q4_K_cuda;
  3004. case GGML_TYPE_Q5_K:
  3005. return dequantize_row_q5_K_cuda;
  3006. case GGML_TYPE_Q6_K:
  3007. return dequantize_row_q6_K_cuda;
  3008. case GGML_TYPE_F16:
  3009. return convert_fp16_to_fp32_cuda;
  3010. default:
  3011. return nullptr;
  3012. }
  3013. }
  3014. static void ggml_mul_mat_q4_0_q8_1_cuda(
  3015. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3016. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3017. int id;
  3018. CUDA_CHECK(cudaGetDevice(&id));
  3019. const int compute_capability = g_compute_capabilities[id];
  3020. if (compute_capability >= CC_TURING) {
  3021. const int mmq_x = 64;
  3022. const int mmq_y = 128;
  3023. const int nwarps = 4;
  3024. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3025. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3026. const dim3 block_nums(block_num_x, block_num_y, 1);
  3027. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3028. if (nrows_x % mmq_y == 0) {
  3029. const bool need_check = false;
  3030. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3031. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3032. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3033. } else {
  3034. const bool need_check = true;
  3035. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3036. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3037. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3038. }
  3039. } else {
  3040. const int mmq_x = 64;
  3041. const int mmq_y = 64;
  3042. const int nwarps = 4;
  3043. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3044. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3045. const dim3 block_nums(block_num_x, block_num_y, 1);
  3046. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3047. if (nrows_x % mmq_y == 0) {
  3048. const bool need_check = false;
  3049. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3050. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3051. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3052. } else {
  3053. const bool need_check = true;
  3054. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3055. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3056. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3057. }
  3058. }
  3059. }
  3060. static void ggml_mul_mat_q4_1_q8_1_cuda(
  3061. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3062. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3063. int id;
  3064. CUDA_CHECK(cudaGetDevice(&id));
  3065. const int compute_capability = g_compute_capabilities[id];
  3066. if (compute_capability >= CC_TURING) {
  3067. const int mmq_x = 64;
  3068. const int mmq_y = 128;
  3069. const int nwarps = 4;
  3070. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3071. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3072. const dim3 block_nums(block_num_x, block_num_y, 1);
  3073. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3074. if (nrows_x % mmq_y == 0) {
  3075. const bool need_check = false;
  3076. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3077. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3078. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3079. } else {
  3080. const bool need_check = true;
  3081. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3082. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3083. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3084. }
  3085. } else {
  3086. const int mmq_x = 64;
  3087. const int mmq_y = 64;
  3088. const int nwarps = 8;
  3089. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3090. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3091. const dim3 block_nums(block_num_x, block_num_y, 1);
  3092. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3093. if (nrows_x % mmq_y == 0) {
  3094. const bool need_check = false;
  3095. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3096. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3097. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3098. } else {
  3099. const bool need_check = true;
  3100. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3101. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3102. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3103. }
  3104. }
  3105. }
  3106. static void ggml_mul_mat_q5_0_q8_1_cuda(
  3107. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3108. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3109. int id;
  3110. CUDA_CHECK(cudaGetDevice(&id));
  3111. const int compute_capability = g_compute_capabilities[id];
  3112. if (compute_capability >= CC_TURING) {
  3113. const int mmq_x = 128;
  3114. const int mmq_y = 64;
  3115. const int nwarps = 4;
  3116. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3117. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3118. const dim3 block_nums(block_num_x, block_num_y, 1);
  3119. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3120. if (nrows_x % mmq_y == 0) {
  3121. const bool need_check = false;
  3122. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3123. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3124. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3125. } else {
  3126. const bool need_check = true;
  3127. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3128. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3129. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3130. }
  3131. } else {
  3132. const int mmq_x = 64;
  3133. const int mmq_y = 64;
  3134. const int nwarps = 8;
  3135. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3136. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3137. const dim3 block_nums(block_num_x, block_num_y, 1);
  3138. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3139. if (nrows_x % mmq_y == 0) {
  3140. const bool need_check = false;
  3141. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3142. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3143. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3144. } else {
  3145. const bool need_check = true;
  3146. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3147. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3148. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3149. }
  3150. }
  3151. }
  3152. static void ggml_mul_mat_q5_1_q8_1_cuda(
  3153. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3154. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3155. int id;
  3156. CUDA_CHECK(cudaGetDevice(&id));
  3157. const int compute_capability = g_compute_capabilities[id];
  3158. if (compute_capability >= CC_TURING) {
  3159. const int mmq_x = 128;
  3160. const int mmq_y = 64;
  3161. const int nwarps = 8;
  3162. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3163. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3164. const dim3 block_nums(block_num_x, block_num_y, 1);
  3165. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3166. if (nrows_x % mmq_y == 0) {
  3167. const bool need_check = false;
  3168. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3169. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3170. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3171. } else {
  3172. const bool need_check = true;
  3173. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3174. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3175. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3176. }
  3177. } else {
  3178. const int mmq_x = 64;
  3179. const int mmq_y = 64;
  3180. const int nwarps = 8;
  3181. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3182. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3183. const dim3 block_nums(block_num_x, block_num_y, 1);
  3184. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3185. if (nrows_x % mmq_y == 0) {
  3186. const bool need_check = false;
  3187. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3188. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3189. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3190. } else {
  3191. const bool need_check = true;
  3192. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3193. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3194. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3195. }
  3196. }
  3197. }
  3198. static void ggml_mul_mat_q8_0_q8_1_cuda(
  3199. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3200. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3201. int id;
  3202. CUDA_CHECK(cudaGetDevice(&id));
  3203. const int compute_capability = g_compute_capabilities[id];
  3204. if (compute_capability >= CC_TURING) {
  3205. const int mmq_x = 128;
  3206. const int mmq_y = 64;
  3207. const int nwarps = 4;
  3208. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3209. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3210. const dim3 block_nums(block_num_x, block_num_y, 1);
  3211. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3212. if (nrows_x % mmq_y == 0) {
  3213. const bool need_check = false;
  3214. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3215. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3216. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3217. } else {
  3218. const bool need_check = true;
  3219. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3220. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3221. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3222. }
  3223. } else {
  3224. const int mmq_x = 64;
  3225. const int mmq_y = 64;
  3226. const int nwarps = 8;
  3227. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3228. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3229. const dim3 block_nums(block_num_x, block_num_y, 1);
  3230. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3231. if (nrows_x % mmq_y == 0) {
  3232. const bool need_check = false;
  3233. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3234. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3235. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3236. } else {
  3237. const bool need_check = true;
  3238. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3239. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3240. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3241. }
  3242. }
  3243. }
  3244. static void ggml_mul_mat_q2_K_q8_1_cuda(
  3245. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3246. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3247. int id;
  3248. CUDA_CHECK(cudaGetDevice(&id));
  3249. const int compute_capability = g_compute_capabilities[id];
  3250. if (compute_capability >= CC_TURING) {
  3251. const int mmq_x = 64;
  3252. const int mmq_y = 128;
  3253. const int nwarps = 4;
  3254. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3255. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3256. const dim3 block_nums(block_num_x, block_num_y, 1);
  3257. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3258. if (nrows_x % mmq_y == 0) {
  3259. const bool need_check = false;
  3260. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3261. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3262. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3263. } else {
  3264. const bool need_check = true;
  3265. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3266. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3267. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3268. }
  3269. } else {
  3270. const int mmq_x = 64;
  3271. const int mmq_y = 64;
  3272. const int nwarps = 8;
  3273. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3274. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3275. const dim3 block_nums(block_num_x, block_num_y, 1);
  3276. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3277. if (nrows_x % mmq_y == 0) {
  3278. const bool need_check = false;
  3279. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3280. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3281. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3282. } else {
  3283. const bool need_check = true;
  3284. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3285. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3286. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3287. }
  3288. }
  3289. }
  3290. static void ggml_mul_mat_q3_K_q8_1_cuda(
  3291. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3292. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3293. int id;
  3294. CUDA_CHECK(cudaGetDevice(&id));
  3295. const int compute_capability = g_compute_capabilities[id];
  3296. if (compute_capability >= CC_TURING) {
  3297. const int mmq_x = 128;
  3298. const int mmq_y = 128;
  3299. const int nwarps = 4;
  3300. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3301. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3302. const dim3 block_nums(block_num_x, block_num_y, 1);
  3303. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3304. if (nrows_x % mmq_y == 0) {
  3305. const bool need_check = false;
  3306. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3307. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3308. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3309. } else {
  3310. const bool need_check = true;
  3311. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3312. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3313. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3314. }
  3315. } else {
  3316. const int mmq_x = 64;
  3317. const int mmq_y = 64;
  3318. const int nwarps = 8;
  3319. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3320. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3321. const dim3 block_nums(block_num_x, block_num_y, 1);
  3322. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3323. if (nrows_x % mmq_y == 0) {
  3324. const bool need_check = false;
  3325. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3326. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3327. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3328. } else {
  3329. const bool need_check = true;
  3330. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3331. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3332. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3333. }
  3334. }
  3335. }
  3336. static void ggml_mul_mat_q4_K_q8_1_cuda(
  3337. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3338. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3339. int id;
  3340. CUDA_CHECK(cudaGetDevice(&id));
  3341. const int compute_capability = g_compute_capabilities[id];
  3342. if (compute_capability >= CC_TURING) {
  3343. const int mmq_x = 64;
  3344. const int mmq_y = 128;
  3345. const int nwarps = 4;
  3346. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3347. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3348. const dim3 block_nums(block_num_x, block_num_y, 1);
  3349. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3350. if (nrows_x % mmq_y == 0) {
  3351. const bool need_check = false;
  3352. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3353. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3354. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3355. } else {
  3356. const bool need_check = true;
  3357. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3358. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3359. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3360. }
  3361. } else {
  3362. const int mmq_x = 32;
  3363. const int mmq_y = 64;
  3364. const int nwarps = 8;
  3365. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3366. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3367. const dim3 block_nums(block_num_x, block_num_y, 1);
  3368. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3369. if (nrows_x % mmq_y == 0) {
  3370. const bool need_check = false;
  3371. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3372. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3373. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3374. } else {
  3375. const bool need_check = true;
  3376. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3377. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3378. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3379. }
  3380. }
  3381. }
  3382. static void ggml_mul_mat_q5_K_q8_1_cuda(
  3383. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3384. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3385. int id;
  3386. CUDA_CHECK(cudaGetDevice(&id));
  3387. const int compute_capability = g_compute_capabilities[id];
  3388. if (compute_capability >= CC_TURING) {
  3389. const int mmq_x = 64;
  3390. const int mmq_y = 128;
  3391. const int nwarps = 4;
  3392. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3393. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3394. const dim3 block_nums(block_num_x, block_num_y, 1);
  3395. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3396. if (nrows_x % mmq_y == 0) {
  3397. const bool need_check = false;
  3398. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3399. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3400. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3401. } else {
  3402. const bool need_check = true;
  3403. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3404. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3405. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3406. }
  3407. } else {
  3408. const int mmq_x = 64;
  3409. const int mmq_y = 64;
  3410. const int nwarps = 8;
  3411. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3412. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3413. const dim3 block_nums(block_num_x, block_num_y, 1);
  3414. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3415. if (nrows_x % mmq_y == 0) {
  3416. const bool need_check = false;
  3417. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3418. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3419. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3420. } else {
  3421. const bool need_check = true;
  3422. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3423. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3424. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3425. }
  3426. }
  3427. }
  3428. static void ggml_mul_mat_q6_K_q8_1_cuda(
  3429. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3430. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3431. int id;
  3432. CUDA_CHECK(cudaGetDevice(&id));
  3433. const int compute_capability = g_compute_capabilities[id];
  3434. if (compute_capability >= CC_TURING) {
  3435. const int mmq_x = 64;
  3436. const int mmq_y = 64;
  3437. const int nwarps = 4;
  3438. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3439. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3440. const dim3 block_nums(block_num_x, block_num_y, 1);
  3441. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3442. if (nrows_x % mmq_y == 0) {
  3443. const bool need_check = false;
  3444. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3445. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3446. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3447. } else {
  3448. const bool need_check = true;
  3449. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3450. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3451. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3452. }
  3453. } else {
  3454. const int mmq_x = 32;
  3455. const int mmq_y = 64;
  3456. const int nwarps = 8;
  3457. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3458. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3459. const dim3 block_nums(block_num_x, block_num_y, 1);
  3460. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3461. if (nrows_x % mmq_y == 0) {
  3462. const bool need_check = false;
  3463. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3464. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3465. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3466. } else {
  3467. const bool need_check = true;
  3468. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3469. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3470. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3471. }
  3472. }
  3473. }
  3474. static void ggml_mul_mat_p021_f16_f32_cuda(
  3475. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  3476. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  3477. const dim3 block_nums(1, nrows_x, nchannels_y);
  3478. const dim3 block_dims(WARP_SIZE, 1, 1);
  3479. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  3480. }
  3481. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  3482. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  3483. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  3484. const dim3 block_nums(1, nrows_x, nchannels_y);
  3485. const dim3 block_dims(WARP_SIZE, 1, 1);
  3486. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  3487. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  3488. }
  3489. static void ggml_cpy_f32_f32_cuda(
  3490. const char * cx, char * cdst, const int ne,
  3491. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3492. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  3493. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  3494. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  3495. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  3496. }
  3497. static void ggml_cpy_f32_f16_cuda(
  3498. const char * cx, char * cdst, const int ne,
  3499. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3500. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  3501. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  3502. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  3503. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  3504. }
  3505. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  3506. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  3507. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  3508. }
  3509. static void rope_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p0,
  3510. const float p_delta, const int p_delta_rows, const float theta_scale, cudaStream_t stream) {
  3511. GGML_ASSERT(nrows % 2 == 0);
  3512. const dim3 block_dims(2*CUDA_ROPE_BLOCK_SIZE, 1, 1);
  3513. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  3514. const dim3 block_nums(num_blocks_x, nrows, 1);
  3515. rope_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p0, p_delta, p_delta_rows, theta_scale);
  3516. }
  3517. static void rope_glm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p, const float block_p, const float theta_scale, cudaStream_t stream) {
  3518. GGML_ASSERT(nrows % 4 == 0);
  3519. const dim3 block_dims(4*CUDA_ROPE_BLOCK_SIZE, 1, 1);
  3520. const int num_blocks_x = (ncols + 4*CUDA_ROPE_BLOCK_SIZE - 1) / (4*CUDA_ROPE_BLOCK_SIZE);
  3521. const dim3 block_nums(num_blocks_x, nrows, 1);
  3522. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p, block_p, theta_scale);
  3523. }
  3524. 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) {
  3525. const dim3 block_dims(CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1, 1);
  3526. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  3527. const dim3 block_nums(block_num_x, nrows_x, 1);
  3528. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  3529. }
  3530. static void soft_max_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, cudaStream_t stream) {
  3531. const dim3 block_dims(WARP_SIZE, 1, 1);
  3532. const dim3 block_nums(1, nrows_x, 1);
  3533. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x);
  3534. }
  3535. // buffer pool for cuda
  3536. #define MAX_CUDA_BUFFERS 256
  3537. struct scoped_spin_lock {
  3538. std::atomic_flag& lock;
  3539. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  3540. while (lock.test_and_set(std::memory_order_acquire)) {
  3541. ; // spin
  3542. }
  3543. }
  3544. ~scoped_spin_lock() {
  3545. lock.clear(std::memory_order_release);
  3546. }
  3547. scoped_spin_lock(const scoped_spin_lock&) = delete;
  3548. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  3549. };
  3550. struct cuda_buffer {
  3551. void * ptr = nullptr;
  3552. size_t size = 0;
  3553. };
  3554. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  3555. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  3556. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  3557. scoped_spin_lock lock(g_cuda_pool_lock);
  3558. int id;
  3559. CUDA_CHECK(cudaGetDevice(&id));
  3560. #ifdef DEBUG_CUDA_MALLOC
  3561. int nnz = 0;
  3562. size_t max_size = 0, tot_size = 0;
  3563. #endif
  3564. size_t best_diff = 1ull << 36;
  3565. int ibest = -1;
  3566. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  3567. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  3568. if (b.ptr != nullptr) {
  3569. #ifdef DEBUG_CUDA_MALLOC
  3570. ++nnz;
  3571. tot_size += b.size;
  3572. if (b.size > max_size) max_size = b.size;
  3573. #endif
  3574. if (b.size >= size) {
  3575. size_t diff = b.size - size;
  3576. if (diff < best_diff) {
  3577. best_diff = diff;
  3578. ibest = i;
  3579. if (!best_diff) {
  3580. void * ptr = b.ptr;
  3581. *actual_size = b.size;
  3582. b.ptr = nullptr;
  3583. b.size = 0;
  3584. return ptr;
  3585. }
  3586. }
  3587. }
  3588. }
  3589. }
  3590. if (ibest >= 0) {
  3591. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  3592. void * ptr = b.ptr;
  3593. *actual_size = b.size;
  3594. b.ptr = nullptr;
  3595. b.size = 0;
  3596. return ptr;
  3597. }
  3598. #ifdef DEBUG_CUDA_MALLOC
  3599. fprintf(stderr, "%s: %d buffers, max_size = %u MB, tot_size = %u MB, requested %u MB\n", __func__, nnz,
  3600. (uint32_t)(max_size/1024/1024), (uint32_t)(tot_size/1024/1024), (uint32_t)(size/1024/1024));
  3601. #endif
  3602. void * ptr;
  3603. size_t look_ahead_size = (size_t) (1.05 * size);
  3604. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  3605. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  3606. *actual_size = look_ahead_size;
  3607. return ptr;
  3608. }
  3609. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  3610. scoped_spin_lock lock(g_cuda_pool_lock);
  3611. int id;
  3612. CUDA_CHECK(cudaGetDevice(&id));
  3613. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  3614. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  3615. if (b.ptr == nullptr) {
  3616. b.ptr = ptr;
  3617. b.size = size;
  3618. return;
  3619. }
  3620. }
  3621. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  3622. CUDA_CHECK(cudaFree(ptr));
  3623. }
  3624. void ggml_init_cublas() {
  3625. static bool initialized = false;
  3626. if (!initialized) {
  3627. CUDA_CHECK(cudaGetDeviceCount(&g_device_count));
  3628. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  3629. int64_t total_vram = 0;
  3630. fprintf(stderr, "%s: found %d CUDA devices:\n", __func__, g_device_count);
  3631. for (int id = 0; id < g_device_count; ++id) {
  3632. cudaDeviceProp prop;
  3633. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  3634. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  3635. g_tensor_split[id] = total_vram;
  3636. total_vram += prop.totalGlobalMem;
  3637. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  3638. }
  3639. for (int id = 0; id < g_device_count; ++id) {
  3640. g_tensor_split[id] /= total_vram;
  3641. }
  3642. for (int id = 0; id < g_device_count; ++id) {
  3643. CUDA_CHECK(cudaSetDevice(id));
  3644. // create main stream
  3645. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams_main[id], cudaStreamNonBlocking));
  3646. // create cublas handle
  3647. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  3648. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  3649. }
  3650. // configure logging to stdout
  3651. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  3652. initialized = true;
  3653. }
  3654. }
  3655. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  3656. if (tensor_split == nullptr) {
  3657. return;
  3658. }
  3659. bool all_zero = true;
  3660. for (int i = 0; i < g_device_count; ++i) {
  3661. if (tensor_split[i] != 0.0f) {
  3662. all_zero = false;
  3663. break;
  3664. }
  3665. }
  3666. if (all_zero) {
  3667. return;
  3668. }
  3669. float split_sum = 0.0f;
  3670. for (int i = 0; i < g_device_count; ++i) {
  3671. g_tensor_split[i] = split_sum;
  3672. split_sum += tensor_split[i];
  3673. }
  3674. for (int i = 0; i < g_device_count; ++i) {
  3675. g_tensor_split[i] /= split_sum;
  3676. }
  3677. }
  3678. void * ggml_cuda_host_malloc(size_t size) {
  3679. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  3680. return nullptr;
  3681. }
  3682. void * ptr = nullptr;
  3683. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  3684. if (err != cudaSuccess) {
  3685. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  3686. // This can fixed the OOM error in WSL.
  3687. cudaGetLastError();
  3688. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  3689. size/1024.0/1024.0, cudaGetErrorString(err));
  3690. return nullptr;
  3691. }
  3692. return ptr;
  3693. }
  3694. void ggml_cuda_host_free(void * ptr) {
  3695. CUDA_CHECK(cudaFreeHost(ptr));
  3696. }
  3697. static cudaError_t ggml_cuda_cpy_tensor_2d(
  3698. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  3699. cudaMemcpyKind kind;
  3700. char * src_ptr;
  3701. if (src->backend == GGML_BACKEND_CPU) {
  3702. kind = cudaMemcpyHostToDevice;
  3703. src_ptr = (char *) src->data;
  3704. } else if (src->backend == GGML_BACKEND_GPU) {
  3705. kind = cudaMemcpyDeviceToDevice;
  3706. struct ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  3707. int id;
  3708. CUDA_CHECK(cudaGetDevice(&id));
  3709. src_ptr = (char *) extra->data_device[id];
  3710. } else {
  3711. GGML_ASSERT(false);
  3712. }
  3713. char * dst_ptr = (char *) dst;
  3714. const int64_t ne0 = src->ne[0];
  3715. const int64_t nb0 = src->nb[0];
  3716. const int64_t nb1 = src->nb[1];
  3717. const int64_t nb2 = src->nb[2];
  3718. const int64_t nb3 = src->nb[3];
  3719. const enum ggml_type type = src->type;
  3720. const int64_t ts = ggml_type_size(type);
  3721. const int64_t bs = ggml_blck_size(type);
  3722. int64_t i1_diff = i1_high - i1_low;
  3723. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  3724. if (nb0 == ts && nb1 == ts*ne0/bs) {
  3725. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  3726. } else if (nb0 == ts) {
  3727. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  3728. } else {
  3729. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  3730. const void * rx = (const void *) ((const char *) x + i1*nb1);
  3731. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  3732. // pretend the row is a matrix with cols=1
  3733. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  3734. if (r != cudaSuccess) return r;
  3735. }
  3736. return cudaSuccess;
  3737. }
  3738. }
  3739. inline void ggml_cuda_op_add(
  3740. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3741. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3742. cudaStream_t & cudaStream_main){
  3743. GGML_ASSERT(src0_ddq_i != nullptr || src0_ddf_i != nullptr);
  3744. GGML_ASSERT(src1_ddf_i != nullptr);
  3745. GGML_ASSERT(dst_ddf_i != nullptr);
  3746. const int64_t ne00 = src0->ne[0];
  3747. const int64_t i01_diff = i01_high - i01_low;
  3748. const int64_t ne10 = src1->ne[0];
  3749. const int64_t ne11 = src1->ne[1];
  3750. // compute
  3751. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3752. add_f32_cuda(src0_ddf_i, src1_ddf_i, dst_ddf_i, ne00*i01_diff, ne10*ne11, cudaStream_main);
  3753. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  3754. add_f16_f32_f16_cuda((half *) src0_ddq_i, src1_ddf_i, (half *) dst_ddf_i, ne00*i01_diff, cudaStream_main);
  3755. } else {
  3756. GGML_ASSERT(false);
  3757. }
  3758. (void) src1;
  3759. (void) dst;
  3760. (void) src0_ddq_i;
  3761. (void) i02;
  3762. (void) i1;
  3763. }
  3764. inline void ggml_cuda_op_mul(
  3765. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3766. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3767. cudaStream_t & cudaStream_main){
  3768. GGML_ASSERT(src0_ddf_i != nullptr);
  3769. GGML_ASSERT(src1_ddf_i != nullptr);
  3770. GGML_ASSERT(dst_ddf_i != nullptr);
  3771. const int64_t ne00 = src0->ne[0];
  3772. const int64_t i01_diff = i01_high - i01_low;
  3773. const int64_t ne10 = src1->ne[0];
  3774. const int64_t ne11 = src1->ne[1];
  3775. mul_f32_cuda(src0_ddf_i, src1_ddf_i, dst_ddf_i, ne00*i01_diff, ne10*ne11, cudaStream_main);
  3776. (void) dst;
  3777. (void) src0_ddq_i;
  3778. (void) i02;
  3779. (void) i1;
  3780. }
  3781. inline void ggml_cuda_op_gelu(
  3782. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3783. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3784. cudaStream_t & cudaStream_main){
  3785. GGML_ASSERT(src0_ddf_i != nullptr);
  3786. GGML_ASSERT(dst_ddf_i != nullptr);
  3787. const int64_t ne00 = src0->ne[0];
  3788. const int64_t i01_diff = i01_high - i01_low;
  3789. // compute
  3790. gelu_f32_cuda(src0_ddf_i, dst_ddf_i, ne00*i01_diff, cudaStream_main);
  3791. (void) src1;
  3792. (void) dst;
  3793. (void) src0_ddq_i;
  3794. (void) src1_ddf_i;
  3795. (void) i02;
  3796. (void) i1;
  3797. }
  3798. inline void ggml_cuda_op_silu(
  3799. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3800. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3801. cudaStream_t & cudaStream_main){
  3802. GGML_ASSERT(src0_ddf_i != nullptr);
  3803. GGML_ASSERT(dst_ddf_i != nullptr);
  3804. const int64_t ne00 = src0->ne[0];
  3805. const int64_t i01_diff = i01_high - i01_low;
  3806. // compute
  3807. silu_f32_cuda(src0_ddf_i, dst_ddf_i, ne00*i01_diff, cudaStream_main);
  3808. (void) src1;
  3809. (void) dst;
  3810. (void) src0_ddq_i;
  3811. (void) src1_ddf_i;
  3812. (void) i02;
  3813. (void) i1;
  3814. }
  3815. inline void ggml_cuda_op_norm(
  3816. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3817. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3818. cudaStream_t & cudaStream_main){
  3819. GGML_ASSERT(src0_ddf_i != nullptr);
  3820. GGML_ASSERT(dst_ddf_i != nullptr);
  3821. const int64_t ne00 = src0->ne[0];
  3822. const int64_t i01_diff = i01_high - i01_low;
  3823. // compute
  3824. norm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  3825. (void) src1;
  3826. (void) dst;
  3827. (void) src0_ddq_i;
  3828. (void) src1_ddf_i;
  3829. (void) i02;
  3830. (void) i1;
  3831. }
  3832. inline void ggml_cuda_op_rms_norm(
  3833. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3834. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3835. cudaStream_t & cudaStream_main){
  3836. GGML_ASSERT(src0_ddf_i != nullptr);
  3837. GGML_ASSERT(dst_ddf_i != nullptr);
  3838. const int64_t ne00 = src0->ne[0];
  3839. const int64_t i01_diff = i01_high - i01_low;
  3840. float eps;
  3841. memcpy(&eps, dst->op_params, sizeof(float));
  3842. // compute
  3843. rms_norm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, eps, cudaStream_main);
  3844. (void) src1;
  3845. (void) dst;
  3846. (void) src0_ddq_i;
  3847. (void) src1_ddf_i;
  3848. (void) i02;
  3849. (void) i1;
  3850. }
  3851. inline void ggml_cuda_op_mul_mat_q(
  3852. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3853. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3854. cudaStream_t & cudaStream_main){
  3855. GGML_ASSERT(src0_ddq_i != nullptr);
  3856. GGML_ASSERT(src1_ddf_i != nullptr);
  3857. GGML_ASSERT(dst_ddf_i != nullptr);
  3858. const int64_t ne00 = src0->ne[0];
  3859. const int64_t ne10 = src1->ne[0];
  3860. const int64_t ne11 = src1->ne[1];
  3861. GGML_ASSERT(ne10 % QK8_1 == 0);
  3862. const int64_t ne0 = dst->ne[0];
  3863. const int64_t i01_diff = i01_high - i01_low;
  3864. int id;
  3865. CUDA_CHECK(cudaGetDevice(&id));
  3866. // the main device has a larger memory buffer to hold the results from all GPUs
  3867. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  3868. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : i01_diff;
  3869. const int64_t padded_row_size = ne10 % MATRIX_ROW_PADDING == 0 ?
  3870. ne10 : ne10 - ne10 % MATRIX_ROW_PADDING + MATRIX_ROW_PADDING;
  3871. size_t as;
  3872. void * src1_q8_1 = ggml_cuda_pool_malloc(padded_row_size*ne11*sizeof(block_q8_1)/QK8_1, &as);
  3873. quantize_row_q8_1_cuda(src1_ddf_i, src1_q8_1, ne10, ne11, padded_row_size, cudaStream_main);
  3874. switch (src0->type) {
  3875. case GGML_TYPE_Q4_0:
  3876. ggml_mul_mat_q4_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3877. break;
  3878. case GGML_TYPE_Q4_1:
  3879. ggml_mul_mat_q4_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3880. break;
  3881. case GGML_TYPE_Q5_0:
  3882. ggml_mul_mat_q5_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3883. break;
  3884. case GGML_TYPE_Q5_1:
  3885. ggml_mul_mat_q5_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3886. break;
  3887. case GGML_TYPE_Q8_0:
  3888. ggml_mul_mat_q8_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3889. break;
  3890. case GGML_TYPE_Q2_K:
  3891. ggml_mul_mat_q2_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3892. break;
  3893. case GGML_TYPE_Q3_K:
  3894. ggml_mul_mat_q3_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3895. break;
  3896. case GGML_TYPE_Q4_K:
  3897. ggml_mul_mat_q4_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3898. break;
  3899. case GGML_TYPE_Q5_K:
  3900. ggml_mul_mat_q5_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3901. break;
  3902. case GGML_TYPE_Q6_K:
  3903. ggml_mul_mat_q6_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  3904. break;
  3905. default:
  3906. GGML_ASSERT(false);
  3907. break;
  3908. }
  3909. ggml_cuda_pool_free(src1_q8_1, as);
  3910. (void) src1;
  3911. (void) dst;
  3912. (void) src0_ddf_i;
  3913. (void) i02;
  3914. (void) i1;
  3915. }
  3916. static int64_t get_row_rounding(ggml_type type) {
  3917. int max_compute_capability = INT_MIN;
  3918. for (int id = 0; id < g_device_count; ++id) {
  3919. if (max_compute_capability < g_compute_capabilities[id]
  3920. && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  3921. max_compute_capability = g_compute_capabilities[id];
  3922. }
  3923. }
  3924. switch(type) {
  3925. case GGML_TYPE_Q4_0:
  3926. case GGML_TYPE_Q4_1:
  3927. return max_compute_capability >= CC_TURING ? 128 : 64;
  3928. case GGML_TYPE_Q5_0:
  3929. case GGML_TYPE_Q5_1:
  3930. case GGML_TYPE_Q8_0:
  3931. return 64;
  3932. case GGML_TYPE_F16:
  3933. return 1;
  3934. case GGML_TYPE_Q2_K:
  3935. case GGML_TYPE_Q3_K:
  3936. case GGML_TYPE_Q4_K:
  3937. case GGML_TYPE_Q5_K:
  3938. return max_compute_capability >= CC_TURING ? 128 : 64;
  3939. case GGML_TYPE_Q6_K:
  3940. return 64;
  3941. default:
  3942. GGML_ASSERT(false);
  3943. }
  3944. }
  3945. inline void ggml_cuda_op_mul_mat_vec(
  3946. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  3947. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  3948. cudaStream_t & cudaStream_main){
  3949. GGML_ASSERT(src0_ddq_i != nullptr);
  3950. GGML_ASSERT(src1_ddf_i != nullptr);
  3951. GGML_ASSERT(dst_ddf_i != nullptr);
  3952. const int64_t ne00 = src0->ne[0];
  3953. const int64_t nrows = i01_high - i01_low;
  3954. #ifdef GGML_CUDA_FORCE_DMMV
  3955. const bool use_mul_mat_vec_q = false;
  3956. (void) g_compute_capabilities[0];
  3957. #else
  3958. int id;
  3959. CUDA_CHECK(cudaGetDevice(&id));
  3960. bool mul_mat_vec_q_implemented =
  3961. src0->type == GGML_TYPE_Q4_0 ||
  3962. src0->type == GGML_TYPE_Q4_1 ||
  3963. src0->type == GGML_TYPE_Q5_0 ||
  3964. src0->type == GGML_TYPE_Q5_1 ||
  3965. src0->type == GGML_TYPE_Q8_0;
  3966. #if QK_K == 256
  3967. mul_mat_vec_q_implemented = mul_mat_vec_q_implemented ||
  3968. src0->type == GGML_TYPE_Q2_K ||
  3969. src0->type == GGML_TYPE_Q3_K ||
  3970. src0->type == GGML_TYPE_Q4_K ||
  3971. src0->type == GGML_TYPE_Q5_K ||
  3972. src0->type == GGML_TYPE_Q6_K;
  3973. #endif // QK_K == 256
  3974. const bool use_mul_mat_vec_q = g_compute_capabilities[id] >= MIN_CC_DP4A && mul_mat_vec_q_implemented;
  3975. #endif
  3976. if (use_mul_mat_vec_q) {
  3977. const int64_t padded_row_size = ne00 % MATRIX_ROW_PADDING == 0 ?
  3978. ne00 : ne00 - ne00 % MATRIX_ROW_PADDING + MATRIX_ROW_PADDING;
  3979. size_t as;
  3980. void * src1_q8_1 = ggml_cuda_pool_malloc(padded_row_size*sizeof(block_q8_1)/QK8_1, &as);
  3981. quantize_row_q8_1_cuda(src1_ddf_i, src1_q8_1, ne00, 1, padded_row_size, cudaStream_main);
  3982. switch (src0->type) {
  3983. case GGML_TYPE_Q4_0:
  3984. mul_mat_vec_q4_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  3985. break;
  3986. case GGML_TYPE_Q4_1:
  3987. mul_mat_vec_q4_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  3988. break;
  3989. case GGML_TYPE_Q5_0:
  3990. mul_mat_vec_q5_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  3991. break;
  3992. case GGML_TYPE_Q5_1:
  3993. mul_mat_vec_q5_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  3994. break;
  3995. case GGML_TYPE_Q8_0:
  3996. mul_mat_vec_q8_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  3997. break;
  3998. case GGML_TYPE_Q2_K:
  3999. mul_mat_vec_q2_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4000. break;
  4001. case GGML_TYPE_Q3_K:
  4002. mul_mat_vec_q3_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4003. break;
  4004. case GGML_TYPE_Q4_K:
  4005. mul_mat_vec_q4_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4006. break;
  4007. case GGML_TYPE_Q5_K:
  4008. mul_mat_vec_q5_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4009. break;
  4010. case GGML_TYPE_Q6_K:
  4011. mul_mat_vec_q6_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4012. break;
  4013. default:
  4014. GGML_ASSERT(false);
  4015. break;
  4016. }
  4017. ggml_cuda_pool_free(src1_q8_1, as);
  4018. } else {
  4019. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  4020. #ifdef GGML_CUDA_F16
  4021. size_t ash;
  4022. dfloat * src1_dfloat = nullptr; // dfloat == half
  4023. bool src1_convert_f16 = src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  4024. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  4025. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  4026. if (src1_convert_f16) {
  4027. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  4028. ggml_cpy_f32_f16_cuda((char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  4029. ne00, 1, sizeof(float), 0, 0,
  4030. ne00, 1, sizeof(half), 0, 0, cudaStream_main);
  4031. }
  4032. #else
  4033. dfloat * src1_dfloat = src1_ddf_i; // dfloat == float, no conversion
  4034. #endif // GGML_CUDA_F16
  4035. switch (src0->type) {
  4036. case GGML_TYPE_Q4_0:
  4037. dequantize_mul_mat_vec_q4_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4038. break;
  4039. case GGML_TYPE_Q4_1:
  4040. dequantize_mul_mat_vec_q4_1_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4041. break;
  4042. case GGML_TYPE_Q5_0:
  4043. dequantize_mul_mat_vec_q5_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4044. break;
  4045. case GGML_TYPE_Q5_1:
  4046. dequantize_mul_mat_vec_q5_1_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4047. break;
  4048. case GGML_TYPE_Q8_0:
  4049. dequantize_mul_mat_vec_q8_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4050. break;
  4051. case GGML_TYPE_Q2_K:
  4052. dequantize_mul_mat_vec_q2_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4053. break;
  4054. case GGML_TYPE_Q3_K:
  4055. dequantize_mul_mat_vec_q3_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4056. break;
  4057. case GGML_TYPE_Q4_K:
  4058. dequantize_mul_mat_vec_q4_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4059. break;
  4060. case GGML_TYPE_Q5_K:
  4061. dequantize_mul_mat_vec_q5_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4062. break;
  4063. case GGML_TYPE_Q6_K:
  4064. dequantize_mul_mat_vec_q6_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4065. break;
  4066. case GGML_TYPE_F16:
  4067. convert_mul_mat_vec_f16_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4068. break;
  4069. default:
  4070. GGML_ASSERT(false);
  4071. break;
  4072. }
  4073. #ifdef GGML_CUDA_F16
  4074. if (src1_convert_f16) {
  4075. ggml_cuda_pool_free(src1_dfloat, ash);
  4076. }
  4077. #endif // GGML_CUDA_F16
  4078. }
  4079. (void) src1;
  4080. (void) dst;
  4081. (void) src0_ddf_i;
  4082. (void) i02;
  4083. (void) i1;
  4084. }
  4085. inline void ggml_cuda_op_mul_mat_cublas(
  4086. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4087. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4088. cudaStream_t & cudaStream_main){
  4089. GGML_ASSERT(src0_ddf_i != nullptr);
  4090. GGML_ASSERT(src1_ddf_i != nullptr);
  4091. GGML_ASSERT(dst_ddf_i != nullptr);
  4092. const float alpha = 1.0f;
  4093. const float beta = 0.0f;
  4094. const int64_t ne00 = src0->ne[0];
  4095. const int64_t ne10 = src1->ne[0];
  4096. const int64_t ne11 = src1->ne[1];
  4097. const int64_t ne0 = dst->ne[0];
  4098. const int64_t i01_diff = i01_high - i01_low;
  4099. int id;
  4100. CUDA_CHECK(cudaGetDevice(&id));
  4101. // the main device has a larger memory buffer to hold the results from all GPUs
  4102. // ldc == nrows of the matrix that cuBLAS writes into
  4103. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : i01_diff;
  4104. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], cudaStream_main));
  4105. CUBLAS_CHECK(
  4106. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  4107. i01_diff, ne11, ne10,
  4108. &alpha, src0_ddf_i, ne00,
  4109. src1_ddf_i, ne10,
  4110. &beta, dst_ddf_i, ldc));
  4111. (void) dst;
  4112. (void) src0_ddq_i;
  4113. (void) i02;
  4114. (void) i1;
  4115. }
  4116. inline void ggml_cuda_op_rope(
  4117. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4118. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4119. cudaStream_t & cudaStream_main){
  4120. GGML_ASSERT(src0_ddf_i != nullptr);
  4121. GGML_ASSERT(dst_ddf_i != nullptr);
  4122. const int64_t ne00 = src0->ne[0];
  4123. const int64_t ne01 = src0->ne[1];
  4124. const int64_t i01_diff = i01_high - i01_low;
  4125. const int n_past = ((int32_t *) dst->op_params)[0];
  4126. const int n_dims = ((int32_t *) dst->op_params)[1];
  4127. const int mode = ((int32_t *) dst->op_params)[2];
  4128. const int n_ctx = ((int32_t *) dst->op_params)[3];
  4129. // RoPE alteration for extended context
  4130. float freq_base, freq_scale;
  4131. memcpy(&freq_base, (int32_t *) dst->op_params + 4, sizeof(float));
  4132. memcpy(&freq_scale, (int32_t *) dst->op_params + 5, sizeof(float));
  4133. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  4134. const bool is_glm = mode & 4;
  4135. // compute
  4136. if (is_glm) {
  4137. const float p = (((mode & 1) == 0 ? n_past + i02 : i02)) * freq_scale;
  4138. const float id_p = min(p, n_ctx - 2.f);
  4139. const float block_p = max(p - (n_ctx - 2.f), 0.f);
  4140. rope_glm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, id_p, block_p, theta_scale, cudaStream_main);
  4141. } else {
  4142. const float p0 = (((mode & 1) == 0 ? n_past : 0)) * freq_scale;
  4143. rope_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, p0, freq_scale, ne01, theta_scale, cudaStream_main);
  4144. }
  4145. (void) src1;
  4146. (void) dst;
  4147. (void) src0_ddq_i;
  4148. (void) src1_ddf_i;
  4149. (void) i1;
  4150. }
  4151. inline void ggml_cuda_op_diag_mask_inf(
  4152. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4153. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4154. cudaStream_t & cudaStream_main){
  4155. GGML_ASSERT(src0_ddf_i != nullptr);
  4156. GGML_ASSERT(dst_ddf_i != nullptr);
  4157. const int64_t ne00 = src0->ne[0];
  4158. const int64_t ne01 = src0->ne[1];
  4159. const int64_t i01_diff = i01_high - i01_low;
  4160. const int n_past = ((int32_t *) dst->op_params)[0];
  4161. // compute
  4162. diag_mask_inf_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, ne01, n_past, cudaStream_main);
  4163. (void) src1;
  4164. (void) dst;
  4165. (void) src0_ddq_i;
  4166. (void) src1_ddf_i;
  4167. (void) i02;
  4168. (void) i1;
  4169. }
  4170. inline void ggml_cuda_op_soft_max(
  4171. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4172. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4173. cudaStream_t & cudaStream_main){
  4174. GGML_ASSERT(src0_ddf_i != nullptr);
  4175. GGML_ASSERT(dst_ddf_i != nullptr);
  4176. const int64_t ne00 = src0->ne[0];
  4177. const int64_t i01_diff = i01_high - i01_low;
  4178. // compute
  4179. soft_max_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  4180. (void) src1;
  4181. (void) dst;
  4182. (void) src0_ddq_i;
  4183. (void) src1_ddf_i;
  4184. (void) i02;
  4185. (void) i1;
  4186. }
  4187. inline void ggml_cuda_op_scale(
  4188. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4189. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4190. cudaStream_t & cudaStream_main){
  4191. GGML_ASSERT(src0_ddf_i != nullptr);
  4192. GGML_ASSERT(dst_ddf_i != nullptr);
  4193. const float scale = ((float *) src1->data)[0];
  4194. const int64_t ne00 = src0->ne[0];
  4195. const int64_t i01_diff = i01_high - i01_low;
  4196. // compute
  4197. scale_f32_cuda(src0_ddf_i, dst_ddf_i, scale, ne00*i01_diff, cudaStream_main);
  4198. CUDA_CHECK(cudaGetLastError());
  4199. (void) src1;
  4200. (void) dst;
  4201. (void) src0_ddq_i;
  4202. (void) src1_ddf_i;
  4203. (void) i02;
  4204. (void) i1;
  4205. }
  4206. static void ggml_cuda_op(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4207. ggml_cuda_op_t op, bool src0_needs_f32, bool flatten_rows) {
  4208. const int64_t ne00 = src0->ne[0];
  4209. const int64_t ne01 = src0->ne[1];
  4210. const int64_t ne02 = src0->ne[2];
  4211. const int64_t ne03 = src0->ne[3];
  4212. const int64_t nrows0 = ggml_nrows(src0);
  4213. const bool use_src1 = src1 != nullptr;
  4214. const int64_t ne10 = use_src1 ? src1->ne[0] : 1;
  4215. const int64_t ne11 = use_src1 ? src1->ne[1] : 1;
  4216. const int64_t ne12 = use_src1 ? src1->ne[2] : 1;
  4217. const int64_t ne13 = use_src1 ? src1->ne[3] : 1;
  4218. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  4219. GGML_ASSERT(ne03 == ne13);
  4220. const int64_t ne0 = dst->ne[0];
  4221. const int64_t ne1 = dst->ne[1];
  4222. const int nb2 = dst->nb[2];
  4223. const int nb3 = dst->nb[3];
  4224. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  4225. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  4226. // strides for iteration over dims 3 and 2
  4227. const int64_t num_iters_0 = ne02 >= ne12 ? ne02*ne03 : ne12*ne13;
  4228. const int64_t num_iters = flatten_rows ? 1 : num_iters_0;
  4229. const int64_t stride_mod = flatten_rows ? num_iters_0 : 1;
  4230. const int64_t src0_stride = ne00 * ne01 * stride_mod;
  4231. const int64_t src1_stride = ne10 * ne11 * stride_mod;
  4232. const int64_t dst_stride = ne0 * ne1 * stride_mod;
  4233. const int64_t rows_per_iter = flatten_rows ? nrows0 : ne01;
  4234. const int64_t i03_max = flatten_rows ? 1 : ne03;
  4235. const int64_t i02_max = flatten_rows ? 1 : (ne02 >= ne12 ? ne02 : ne12);
  4236. const int64_t i02_divisor = ne02 >= ne12 ? 1 : ne12 / ne02;
  4237. GGML_ASSERT(!(flatten_rows && ne02 < ne12));
  4238. const size_t src0_ts = ggml_type_size(src0->type);
  4239. const size_t src0_bs = ggml_blck_size(src0->type);
  4240. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  4241. struct ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  4242. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  4243. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  4244. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  4245. const bool src0_is_f32 = src0->type == GGML_TYPE_F32;
  4246. const bool src1_is_contiguous = use_src1 && ggml_is_contiguous(src1);
  4247. const bool src1_stays_on_host = use_src1 && (
  4248. dst->op == GGML_OP_SCALE || dst->op == GGML_OP_DIAG_MASK_INF || dst->op == GGML_OP_ROPE);
  4249. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  4250. GGML_ASSERT(!(split && ne02 < ne12));
  4251. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  4252. // dd = data device
  4253. char * src0_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // quantized
  4254. float * src0_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  4255. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr};
  4256. float * dst_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr};
  4257. // asq = actual size quantized, asf = actual size float
  4258. size_t src0_asq[GGML_CUDA_MAX_DEVICES] = {0};
  4259. size_t src0_asf[GGML_CUDA_MAX_DEVICES] = {0};
  4260. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  4261. size_t dst_asf[GGML_CUDA_MAX_DEVICES] = {0};
  4262. // if multiple devices are used they need to wait for the main device
  4263. // here an event is recorded that signifies that the main device has finished calculating the input data
  4264. if (split && g_device_count > 1) {
  4265. CUDA_CHECK(cudaSetDevice(g_main_device));
  4266. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device], g_cudaStreams_main[g_main_device]));
  4267. }
  4268. for (int id = 0; id < g_device_count; ++id) {
  4269. if (!split && id != g_main_device) {
  4270. continue;
  4271. }
  4272. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  4273. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  4274. int64_t row_low, row_high;
  4275. if (split) {
  4276. const int64_t rounding = get_row_rounding(src0->type);
  4277. row_low = id == 0 ? 0 : nrows0*g_tensor_split[id];
  4278. row_low -= row_low % rounding;
  4279. if (id == g_device_count - 1) {
  4280. row_high = nrows0;
  4281. } else {
  4282. row_high = nrows0*g_tensor_split[id + 1];
  4283. row_high -= row_high % rounding;
  4284. }
  4285. } else {
  4286. row_low = 0;
  4287. row_high = nrows0*i02_divisor;
  4288. }
  4289. if (row_low == row_high) {
  4290. continue;
  4291. }
  4292. int64_t row_diff = row_high - row_low;
  4293. cudaSetDevice(id);
  4294. cudaStream_t cudaStream_main = g_cudaStreams_main[id];
  4295. // wait for main GPU data if necessary
  4296. if (split && id != g_main_device) {
  4297. CUDA_CHECK(cudaStreamWaitEvent(cudaStream_main, src0_extra->events[g_main_device]));
  4298. }
  4299. if (src0_on_device && src0_is_contiguous) {
  4300. if (src0_is_f32) {
  4301. src0_ddf[id] = (float *) src0_extra->data_device[id];
  4302. } else {
  4303. src0_ddq[id] = (char *) src0_extra->data_device[id];
  4304. }
  4305. } else {
  4306. if (src0_is_f32) {
  4307. src0_ddf[id] = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_asf[id]);
  4308. } else {
  4309. src0_ddq[id] = (char *) ggml_cuda_pool_malloc(row_diff*ne00 * src0_ts/src0_bs, &src0_asq[id]);
  4310. }
  4311. }
  4312. if (src0_needs_f32 && !src0_is_f32) {
  4313. src0_ddf[id] = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_asf[id]);
  4314. }
  4315. if (use_src1 && !src1_stays_on_host) {
  4316. if (src1_on_device && src1_is_contiguous) {
  4317. src1_ddf[id] = (float *) src1_extra->data_device[id];
  4318. } else {
  4319. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(num_iters*src1_stride * sizeof(float), &src1_asf[id]);
  4320. }
  4321. }
  4322. if (dst_on_device) {
  4323. dst_ddf[id] = (float *) dst_extra->data_device[id];
  4324. } else {
  4325. size_t size_dst_ddf = split ? row_diff*ne1 * sizeof(float) : num_iters*dst_stride * sizeof(float);
  4326. dst_ddf[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_asf[id]);
  4327. }
  4328. for (int64_t i03 = 0; i03 < i03_max; i03++) {
  4329. const int64_t i13 = i03 % ne13;
  4330. for (int64_t i02 = 0; i02 < i02_max; i02++) {
  4331. const int64_t i12 = i02 % ne12;
  4332. const int64_t i0 = i03*i02_max + i02;
  4333. // i0 values that contain the lower/upper rows for a split tensor when using multiple GPUs
  4334. const int64_t i0_offset_low = row_low/rows_per_iter;
  4335. const int64_t i0_offset_high = row_high/rows_per_iter;
  4336. int64_t i01_low = 0;
  4337. int64_t i01_high = rows_per_iter;
  4338. if (split) {
  4339. if (i0 < i0_offset_low || i0 > i0_offset_high) {
  4340. continue;
  4341. }
  4342. if (i0 == i0_offset_low) {
  4343. i01_low = row_low % rows_per_iter;
  4344. }
  4345. if (i0 == i0_offset_high) {
  4346. i01_high = row_high % rows_per_iter;
  4347. }
  4348. }
  4349. // There is possibly a bug in the Windows nvcc compiler regarding instruction reordering or optimizing out local variables.
  4350. // Removing the first assert or changing the order of the arguments causes the second assert to fail.
  4351. // Removing both asserts results in i01_high becoming 0 which in turn results in garbage output.
  4352. // The root cause seems to be a problem with i0_offset_high becoming 0 when it should always be >0 (for single GPU).
  4353. GGML_ASSERT(i01_low == 0 || g_device_count > 1);
  4354. GGML_ASSERT(i01_high == rows_per_iter || g_device_count > 1);
  4355. const int64_t i01_diff = i01_high - i01_low;
  4356. if (i01_diff == 0) {
  4357. continue;
  4358. }
  4359. const int64_t i11 = i13*ne12 + i12;
  4360. // for split tensors the data begins at i0 == i0_offset_low
  4361. char * src0_ddq_i = src0_ddq[id] + (i0/i02_divisor - i0_offset_low)*src0_stride*src0_ts/src0_bs;
  4362. float * src0_ddf_i = src0_ddf[id] + (i0/i02_divisor - i0_offset_low)*src0_stride;
  4363. float * src1_ddf_i = src1_ddf[id] + i11*src1_stride;
  4364. float * dst_ddf_i = dst_ddf[id] + (i0 - i0_offset_low)*dst_stride;
  4365. // for split tensors the data pointer needs to be rounded down
  4366. // to the bin edge for i03, i02 bins beyond the first
  4367. if (i0 - i0_offset_low > 0) {
  4368. GGML_ASSERT(!flatten_rows);
  4369. src0_ddq_i -= (row_low % ne01)*ne00 * src0_ts/src0_bs;
  4370. src0_ddf_i -= (row_low % ne01)*ne00;
  4371. dst_ddf_i -= (row_low % ne0)*ne1;
  4372. }
  4373. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  4374. // in that case an offset on dst_ddf_i is needed
  4375. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  4376. dst_ddf_i += i01_low; // offset is 0 if no tensor split
  4377. }
  4378. // copy src0, src1 to device if necessary
  4379. if (use_src1 && !src1_stays_on_host) {
  4380. if (src1->backend == GGML_BACKEND_CPU) {
  4381. GGML_ASSERT(!flatten_rows || nrows0 == ggml_nrows(src1));
  4382. int64_t nrows1 = flatten_rows ? nrows0 : ne11;
  4383. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf_i, src1, i03, i02, 0, nrows1, cudaStream_main));
  4384. } else if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  4385. if (id != g_main_device) {
  4386. GGML_ASSERT(!flatten_rows);
  4387. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  4388. src1_ddf_i_source += i11*src1_stride;
  4389. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_stride*sizeof(float),
  4390. cudaMemcpyDeviceToDevice, cudaStream_main));
  4391. }
  4392. } else if (src1_on_device && !src1_is_contiguous) {
  4393. GGML_ASSERT(!split);
  4394. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf_i, src1, i03, i02, 0, ne11, cudaStream_main));
  4395. } else {
  4396. GGML_ASSERT(false);
  4397. }
  4398. }
  4399. if ((!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  4400. if (src0_is_f32) {
  4401. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf_i, src0, i03, i02/i02_divisor, i01_low, i01_high, cudaStream_main));
  4402. } else {
  4403. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddq_i, src0, i03, i02/i02_divisor, i01_low, i01_high, cudaStream_main));
  4404. }
  4405. }
  4406. // convert src0 to f32 if it is necessary for the ggml_cuda_op
  4407. if (src0_needs_f32 && !src0_is_f32) {
  4408. to_fp32_cuda(src0_ddq_i, src0_ddf_i, i01_diff*ne00, cudaStream_main);
  4409. CUDA_CHECK(cudaGetLastError());
  4410. }
  4411. // do the computation
  4412. op(src0, src1, dst, src0_ddq_i, src0_ddf_i, src1_ddf_i, dst_ddf_i, i02, i01_low, i01_high, i11, cudaStream_main);
  4413. CUDA_CHECK(cudaGetLastError());
  4414. // copy dst to host or other device if necessary
  4415. if (!dst_on_device) {
  4416. void * dst_off_device;
  4417. cudaMemcpyKind kind;
  4418. if (dst->backend == GGML_BACKEND_CPU) {
  4419. dst_off_device = dst->data;
  4420. kind = cudaMemcpyDeviceToHost;
  4421. } else if (dst->backend == GGML_BACKEND_GPU) {
  4422. dst_off_device = dst_extra->data_device[g_main_device];
  4423. kind = cudaMemcpyDeviceToDevice;
  4424. } else {
  4425. GGML_ASSERT(false);
  4426. }
  4427. if (split) {
  4428. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  4429. // dst is NOT transposed.
  4430. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  4431. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  4432. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  4433. float * dhf_dst_i = (float *) ((char *) dst_off_device + i01_low*sizeof(float) + i02*nb2 + i03*nb3);
  4434. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_ddf_i, i01_diff*sizeof(float),
  4435. i01_diff*sizeof(float), ne1, kind, cudaStream_main));
  4436. } else {
  4437. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  4438. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_ddf_i, dst_stride*sizeof(float), kind, cudaStream_main));
  4439. }
  4440. }
  4441. // signify to main device that other device is done
  4442. if (split && g_device_count > 1 && id != g_main_device) {
  4443. CUDA_CHECK(cudaEventRecord(src0_extra->events[id], cudaStream_main));
  4444. }
  4445. }
  4446. }
  4447. }
  4448. // wait until each device is finished, then free their buffers
  4449. for (int id = 0; id < g_device_count; ++id) {
  4450. if (src0_asq[id] == 0 && src0_asf[id] == 0 && src1_asf[id] == 0 && dst_asf[id] == 0) {
  4451. continue;
  4452. }
  4453. CUDA_CHECK(cudaSetDevice(id));
  4454. if (src0_asq[id] > 0) {
  4455. ggml_cuda_pool_free(src0_ddq[id], src0_asq[id]);
  4456. }
  4457. if (src0_asf[id] > 0) {
  4458. ggml_cuda_pool_free(src0_ddf[id], src0_asf[id]);
  4459. }
  4460. if (src1_asf[id] > 0) {
  4461. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  4462. }
  4463. if (dst_asf[id] > 0) {
  4464. ggml_cuda_pool_free(dst_ddf[id], dst_asf[id]);
  4465. }
  4466. }
  4467. // main device waits for all other devices to be finished
  4468. if (split && g_device_count > 1) {
  4469. CUDA_CHECK(cudaSetDevice(g_main_device));
  4470. for (int id = 0; id < g_device_count; ++id) {
  4471. if (id != g_main_device && src0_extra->events[id]) {
  4472. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams_main[g_main_device], src0_extra->events[id]));
  4473. }
  4474. }
  4475. }
  4476. if (dst->backend == GGML_BACKEND_CPU) {
  4477. CUDA_CHECK(cudaSetDevice(g_main_device));
  4478. CUDA_CHECK(cudaDeviceSynchronize());
  4479. }
  4480. }
  4481. void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4482. // ggml_cuda_add permits f16 dst even though this could in theory cause problems with the pointer arithmetic in ggml_cuda_op.
  4483. // Due to flatten_rows == true this does in practice not make a difference however.
  4484. // Better solution would be nice but right now that would require disproportionate changes.
  4485. GGML_ASSERT(
  4486. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16) &&
  4487. src1->type == GGML_TYPE_F32 &&
  4488. (dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16));
  4489. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_add, false, true);
  4490. }
  4491. void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4492. GGML_ASSERT(src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4493. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul, true, false); // TODO ggml_cuda_op needs modification for flatten
  4494. }
  4495. void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4496. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4497. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_gelu, true, true);
  4498. }
  4499. void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4500. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4501. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_silu, true, true);
  4502. }
  4503. void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4504. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4505. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_norm, true, true);
  4506. }
  4507. void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4508. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4509. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_rms_norm, true, true);
  4510. }
  4511. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  4512. const int64_t ne10 = src1->ne[0];
  4513. const int64_t ne0 = dst->ne[0];
  4514. const int64_t ne1 = dst->ne[1];
  4515. // TODO: find the optimal values for these
  4516. if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  4517. src1->type == GGML_TYPE_F32 &&
  4518. dst->type == GGML_TYPE_F32 &&
  4519. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32)) {
  4520. return true;
  4521. }
  4522. return false;
  4523. }
  4524. void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  4525. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4526. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  4527. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  4528. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  4529. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4530. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4531. const int64_t ne00 = src0->ne[0];
  4532. const int64_t ne01 = src0->ne[1];
  4533. const int64_t ne02 = src0->ne[2];
  4534. const int64_t ne12 = src1->ne[2];
  4535. CUDA_CHECK(cudaSetDevice(g_main_device));
  4536. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  4537. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  4538. void * src0_ddq = src0_extra->data_device[g_main_device];
  4539. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  4540. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  4541. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  4542. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  4543. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, cudaStream_main);
  4544. }
  4545. void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  4546. GGML_ASSERT(!ggml_is_contiguous(src0) && ggml_is_contiguous(src1));
  4547. GGML_ASSERT(!ggml_is_permuted(src0));
  4548. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  4549. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4550. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4551. const int64_t ne00 = src0->ne[0];
  4552. const int64_t ne01 = src0->ne[1];
  4553. const int64_t ne02 = src0->ne[2];
  4554. const int64_t ne12 = src1->ne[2];
  4555. const int64_t nb01 = src0->nb[1];
  4556. const int64_t nb02 = src0->nb[2];
  4557. CUDA_CHECK(cudaSetDevice(g_main_device));
  4558. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  4559. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  4560. void * src0_ddq = src0_extra->data_device[g_main_device];
  4561. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  4562. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  4563. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  4564. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  4565. const int row_stride_x = nb01 / sizeof(half);
  4566. const int channel_stride_x = nb02 / sizeof(half);
  4567. ggml_mul_mat_vec_nc_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, row_stride_x, ne02, ne12, channel_stride_x, cudaStream_main);
  4568. }
  4569. void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4570. bool all_on_device = (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  4571. src1->backend == GGML_BACKEND_GPU && dst->backend == GGML_BACKEND_GPU;
  4572. if (all_on_device && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  4573. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  4574. } else if (all_on_device && !ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && src1->ne[1] == 1) {
  4575. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  4576. }else if (src0->type == GGML_TYPE_F32) {
  4577. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, true, false);
  4578. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  4579. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  4580. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_vec, false, false);
  4581. } else {
  4582. int min_compute_capability = INT_MAX;
  4583. for (int id = 0; id < g_device_count; ++id) {
  4584. if (min_compute_capability > g_compute_capabilities[id]
  4585. && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  4586. min_compute_capability = g_compute_capabilities[id];
  4587. }
  4588. }
  4589. if (g_mul_mat_q && ggml_is_quantized(src0->type) && min_compute_capability >= MIN_CC_DP4A) {
  4590. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_q, false, false);
  4591. } else {
  4592. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, true, false);
  4593. }
  4594. }
  4595. } else {
  4596. GGML_ASSERT(false);
  4597. }
  4598. }
  4599. void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4600. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4601. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_scale, true, true);
  4602. }
  4603. void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4604. const int64_t ne = ggml_nelements(src0);
  4605. GGML_ASSERT(ne == ggml_nelements(src1));
  4606. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  4607. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  4608. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  4609. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  4610. const int64_t ne00 = src0->ne[0];
  4611. const int64_t ne01 = src0->ne[1];
  4612. GGML_ASSERT(src0->ne[3] == 1);
  4613. const int64_t nb00 = src0->nb[0];
  4614. const int64_t nb01 = src0->nb[1];
  4615. const int64_t nb02 = src0->nb[2];
  4616. const int64_t ne10 = src1->ne[0];
  4617. const int64_t ne11 = src1->ne[1];
  4618. GGML_ASSERT(src1->ne[3] == 1);
  4619. const int64_t nb10 = src1->nb[0];
  4620. const int64_t nb11 = src1->nb[1];
  4621. const int64_t nb12 = src1->nb[2];
  4622. CUDA_CHECK(cudaSetDevice(g_main_device));
  4623. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  4624. const struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  4625. const struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  4626. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  4627. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  4628. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  4629. ggml_cpy_f32_f32_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  4630. ne10, ne11, nb10, nb11, nb12, cudaStream_main);
  4631. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  4632. ggml_cpy_f32_f16_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  4633. ne10, ne11, nb10, nb11, nb12, cudaStream_main);
  4634. } else {
  4635. GGML_ASSERT(false);
  4636. }
  4637. (void) dst;
  4638. }
  4639. void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4640. ggml_cuda_cpy(src0, dst, nullptr);
  4641. (void) src1;
  4642. }
  4643. void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4644. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4645. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_diag_mask_inf, true, true);
  4646. }
  4647. void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4648. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4649. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_soft_max, true, true);
  4650. }
  4651. void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4652. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4653. const int mode = ((int32_t *) dst->op_params)[2];
  4654. const bool is_glm = mode & 4;
  4655. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_rope, true, !is_glm); // flatten support not implemented for glm
  4656. }
  4657. void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4658. (void) src0;
  4659. (void) src1;
  4660. (void) dst;
  4661. }
  4662. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  4663. int nrows = ggml_nrows(tensor);
  4664. const int64_t ne0 = tensor->ne[0];
  4665. const size_t nb1 = tensor->nb[1];
  4666. ggml_backend backend = tensor->backend;
  4667. struct ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  4668. memset(extra, 0, sizeof(*extra));
  4669. for (int id = 0; id < g_device_count; ++id) {
  4670. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  4671. continue;
  4672. }
  4673. cudaSetDevice(id);
  4674. int row_low, row_high;
  4675. if (backend == GGML_BACKEND_GPU) {
  4676. row_low = 0;
  4677. row_high = nrows;
  4678. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  4679. const int64_t rounding = get_row_rounding(tensor->type);
  4680. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  4681. row_low -= row_low % rounding;
  4682. if (id == g_device_count - 1) {
  4683. row_high = nrows;
  4684. } else {
  4685. row_high = nrows*g_tensor_split[id + 1];
  4686. row_high -= row_high % rounding;
  4687. }
  4688. } else {
  4689. GGML_ASSERT(false);
  4690. }
  4691. if (row_low == row_high) {
  4692. continue;
  4693. }
  4694. int64_t nrows_split = row_high - row_low;
  4695. const size_t offset_split = row_low*nb1;
  4696. size_t size = ggml_nbytes_split(tensor, nrows_split);
  4697. const size_t original_size = size;
  4698. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  4699. if (ne0 % MATRIX_ROW_PADDING != 0) {
  4700. size += (MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING)
  4701. * ggml_type_size(tensor->type)/ggml_blck_size(tensor->type);
  4702. }
  4703. char * buf;
  4704. CUDA_CHECK(cudaMalloc(&buf, size));
  4705. char * buf_host = (char*)data + offset_split;
  4706. // set padding to 0 to avoid possible NaN values
  4707. if (size > original_size) {
  4708. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  4709. }
  4710. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  4711. extra->data_device[id] = buf;
  4712. if (backend == GGML_BACKEND_GPU_SPLIT) {
  4713. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id], cudaEventDisableTiming));
  4714. }
  4715. }
  4716. tensor->extra = extra;
  4717. }
  4718. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  4719. if (!tensor || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  4720. return;
  4721. }
  4722. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4723. for (int id = 0; id < g_device_count; ++id) {
  4724. if (extra->data_device[id] != nullptr) {
  4725. CUDA_CHECK(cudaSetDevice(id));
  4726. CUDA_CHECK(cudaFree(extra->data_device[id]));
  4727. }
  4728. if (extra->events[id] != nullptr) {
  4729. CUDA_CHECK(cudaSetDevice(id));
  4730. CUDA_CHECK(cudaEventDestroy(extra->events[id]));
  4731. }
  4732. }
  4733. delete extra;
  4734. }
  4735. static struct ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  4736. static size_t g_temp_tensor_extra_index = 0;
  4737. static struct ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  4738. if (g_temp_tensor_extras == nullptr) {
  4739. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_MAX_NODES];
  4740. }
  4741. size_t alloc_index = g_temp_tensor_extra_index;
  4742. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_MAX_NODES;
  4743. struct ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  4744. memset(extra, 0, sizeof(*extra));
  4745. return extra;
  4746. }
  4747. void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace) {
  4748. if (scratch && g_scratch_size == 0) {
  4749. return;
  4750. }
  4751. // recursively assign CUDA buffers until a compute tensor is found
  4752. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  4753. const ggml_op src0_op = tensor->src[0]->op;
  4754. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  4755. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace);
  4756. }
  4757. }
  4758. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  4759. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace);
  4760. }
  4761. tensor->backend = GGML_BACKEND_GPU;
  4762. struct ggml_tensor_extra_gpu * extra;
  4763. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  4764. tensor->op == GGML_OP_VIEW ||
  4765. force_inplace;
  4766. const size_t size = ggml_nbytes(tensor);
  4767. CUDA_CHECK(cudaSetDevice(g_main_device));
  4768. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  4769. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  4770. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  4771. size_t offset = 0;
  4772. if (tensor->op == GGML_OP_VIEW) {
  4773. memcpy(&offset, tensor->op_params, sizeof(size_t));
  4774. }
  4775. extra = ggml_cuda_alloc_temp_tensor_extra();
  4776. extra->data_device[g_main_device] = src0_ddc + offset;
  4777. } else if (tensor->op == GGML_OP_CPY) {
  4778. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  4779. void * src1_ddv = src1_extra->data_device[g_main_device];
  4780. extra = ggml_cuda_alloc_temp_tensor_extra();
  4781. extra->data_device[g_main_device] = src1_ddv;
  4782. } else if (scratch) {
  4783. GGML_ASSERT(size <= g_scratch_size);
  4784. if (g_scratch_offset + size > g_scratch_size) {
  4785. g_scratch_offset = 0;
  4786. }
  4787. char * data = (char *) g_scratch_buffer;
  4788. if (data == nullptr) {
  4789. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  4790. g_scratch_buffer = data;
  4791. }
  4792. extra = ggml_cuda_alloc_temp_tensor_extra();
  4793. extra->data_device[g_main_device] = data + g_scratch_offset;
  4794. g_scratch_offset += size;
  4795. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  4796. } else { // allocate new buffers outside of scratch
  4797. void * data;
  4798. CUDA_CHECK(cudaMalloc(&data, size));
  4799. CUDA_CHECK(cudaMemset(data, 0, size));
  4800. extra = new ggml_tensor_extra_gpu;
  4801. memset(extra, 0, sizeof(*extra));
  4802. extra->data_device[g_main_device] = data;
  4803. }
  4804. tensor->extra = extra;
  4805. }
  4806. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  4807. ggml_cuda_assign_buffers_impl(tensor, true, false);
  4808. }
  4809. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  4810. ggml_cuda_assign_buffers_impl(tensor, false, false);
  4811. }
  4812. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  4813. ggml_cuda_assign_buffers_impl(tensor, false, true);
  4814. }
  4815. void ggml_cuda_set_main_device(int main_device) {
  4816. if (main_device >= g_device_count) {
  4817. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  4818. main_device, g_device_count, g_main_device);
  4819. return;
  4820. }
  4821. g_main_device = main_device;
  4822. if (g_device_count > 1) {
  4823. cudaDeviceProp prop;
  4824. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  4825. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  4826. }
  4827. }
  4828. void ggml_cuda_set_mul_mat_q(bool mul_mat_q) {
  4829. g_mul_mat_q = mul_mat_q;
  4830. }
  4831. void ggml_cuda_set_scratch_size(size_t scratch_size) {
  4832. g_scratch_size = scratch_size;
  4833. }
  4834. void ggml_cuda_free_scratch() {
  4835. if (g_scratch_buffer == nullptr) {
  4836. return;
  4837. }
  4838. CUDA_CHECK(cudaFree(g_scratch_buffer));
  4839. g_scratch_buffer = nullptr;
  4840. }
  4841. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor){
  4842. ggml_cuda_func_t func;
  4843. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  4844. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  4845. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  4846. switch (tensor->op) {
  4847. case GGML_OP_DUP:
  4848. if (!any_on_device) {
  4849. return false;
  4850. }
  4851. func = ggml_cuda_dup;
  4852. break;
  4853. case GGML_OP_ADD:
  4854. if (!any_on_device) {
  4855. return false;
  4856. }
  4857. func = ggml_cuda_add;
  4858. break;
  4859. case GGML_OP_MUL:
  4860. if (!any_on_device) {
  4861. return false;
  4862. }
  4863. func = ggml_cuda_mul;
  4864. break;
  4865. case GGML_OP_UNARY:
  4866. switch (ggml_get_unary_op(tensor)) {
  4867. case GGML_UNARY_OP_GELU:
  4868. if (!any_on_device) {
  4869. return false;
  4870. }
  4871. func = ggml_cuda_gelu;
  4872. break;
  4873. case GGML_UNARY_OP_SILU:
  4874. if (!any_on_device) {
  4875. return false;
  4876. }
  4877. func = ggml_cuda_silu;
  4878. break;
  4879. default:
  4880. return false;
  4881. } break;
  4882. case GGML_OP_NORM:
  4883. if (!any_on_device) {
  4884. return false;
  4885. }
  4886. func = ggml_cuda_norm;
  4887. break;
  4888. case GGML_OP_RMS_NORM:
  4889. if (!any_on_device) {
  4890. return false;
  4891. }
  4892. func = ggml_cuda_rms_norm;
  4893. break;
  4894. case GGML_OP_MUL_MAT:
  4895. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  4896. return false;
  4897. }
  4898. func = ggml_cuda_mul_mat;
  4899. break;
  4900. case GGML_OP_SCALE:
  4901. if (!any_on_device) {
  4902. return false;
  4903. }
  4904. func = ggml_cuda_scale;
  4905. break;
  4906. case GGML_OP_CPY:
  4907. if (!any_on_device) {
  4908. return false;
  4909. }
  4910. func = ggml_cuda_cpy;
  4911. break;
  4912. case GGML_OP_CONT:
  4913. if (!any_on_device) {
  4914. return false;
  4915. }
  4916. func = ggml_cuda_dup;
  4917. break;
  4918. case GGML_OP_RESHAPE:
  4919. case GGML_OP_VIEW:
  4920. case GGML_OP_PERMUTE:
  4921. case GGML_OP_TRANSPOSE:
  4922. if (!any_on_device) {
  4923. return false;
  4924. }
  4925. func = ggml_cuda_nop;
  4926. break;
  4927. case GGML_OP_DIAG_MASK_INF:
  4928. if (!any_on_device) {
  4929. return false;
  4930. }
  4931. func = ggml_cuda_diag_mask_inf;
  4932. break;
  4933. case GGML_OP_SOFT_MAX:
  4934. if (!any_on_device) {
  4935. return false;
  4936. }
  4937. func = ggml_cuda_soft_max;
  4938. break;
  4939. case GGML_OP_ROPE:
  4940. if (!any_on_device) {
  4941. return false;
  4942. }
  4943. func = ggml_cuda_rope;
  4944. break;
  4945. default:
  4946. return false;
  4947. }
  4948. if (params->ith != 0) {
  4949. return true;
  4950. }
  4951. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  4952. return true;
  4953. }
  4954. func(tensor->src[0], tensor->src[1], tensor);
  4955. return true;
  4956. }