ggml-cuda.cu 296 KB

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