ggml-cuda.cu 311 KB

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