ggml-cuda.cu 303 KB

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