ggml-cuda.cu 306 KB

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