ggml-cuda.cu 346 KB

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