1
0

ggml-cuda.cu 265 KB

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