ggml-cuda.cu 369 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134
  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 cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  33. #define cublasCreate hipblasCreate
  34. #define cublasGemmEx hipblasGemmEx
  35. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  36. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  37. #define cublasHandle_t hipblasHandle_t
  38. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  39. #define cublasSetStream hipblasSetStream
  40. #define cublasSgemm hipblasSgemm
  41. #define cublasStatus_t hipblasStatus_t
  42. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  43. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  44. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  45. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  46. #define cudaDeviceProp hipDeviceProp_t
  47. #define cudaDeviceSynchronize hipDeviceSynchronize
  48. #define cudaError_t hipError_t
  49. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  50. #define cudaEventDisableTiming hipEventDisableTiming
  51. #define cudaEventRecord hipEventRecord
  52. #define cudaEvent_t hipEvent_t
  53. #define cudaEventDestroy hipEventDestroy
  54. #define cudaFree hipFree
  55. #define cudaFreeHost hipHostFree
  56. #define cudaGetDevice hipGetDevice
  57. #define cudaGetDeviceCount hipGetDeviceCount
  58. #define cudaGetDeviceProperties hipGetDeviceProperties
  59. #define cudaGetErrorString hipGetErrorString
  60. #define cudaGetLastError hipGetLastError
  61. #ifdef GGML_HIP_UMA
  62. #define cudaMalloc hipMallocManaged
  63. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  64. #else
  65. #define cudaMalloc hipMalloc
  66. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  67. #endif
  68. #define cudaMemcpy hipMemcpy
  69. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  70. #define cudaMemcpyAsync hipMemcpyAsync
  71. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  72. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  73. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  74. #define cudaMemcpyKind hipMemcpyKind
  75. #define cudaMemset hipMemset
  76. #define cudaMemsetAsync hipMemsetAsync
  77. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  78. #define cudaSetDevice hipSetDevice
  79. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  80. #define cudaStreamFireAndForget hipStreamFireAndForget
  81. #define cudaStreamNonBlocking hipStreamNonBlocking
  82. #define cudaStreamSynchronize hipStreamSynchronize
  83. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  84. #define cudaStream_t hipStream_t
  85. #define cudaSuccess hipSuccess
  86. #define __trap abort
  87. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  88. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  89. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  90. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  91. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  92. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  93. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  94. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  95. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  96. #else
  97. #include <cuda_runtime.h>
  98. #include <cuda.h>
  99. #include <cublas_v2.h>
  100. #include <cuda_fp16.h>
  101. #if CUDART_VERSION < 11020
  102. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  103. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  104. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  105. #define cublasComputeType_t cudaDataType_t
  106. #endif // CUDART_VERSION < 11020
  107. #endif // defined(GGML_USE_HIPBLAS)
  108. #include "ggml-cuda.h"
  109. #include "ggml.h"
  110. #include "ggml-backend-impl.h"
  111. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  112. #define CC_VOLTA 700
  113. #define CC_OFFSET_AMD 1000000
  114. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  115. #define GGML_CUDA_MAX_NODES 8192
  116. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  117. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  118. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  119. // - 7B quantum model: +100-200 MB
  120. // - 13B quantum model: +200-400 MB
  121. //
  122. //#define GGML_CUDA_FORCE_MMQ
  123. // TODO: improve this to be correct for more hardware
  124. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  125. // probably other such cases, and not sure what happens on AMD hardware
  126. #if !defined(GGML_CUDA_FORCE_MMQ)
  127. #define CUDA_USE_TENSOR_CORES
  128. #endif
  129. // max batch size to use MMQ kernels when tensor cores are available
  130. #define MMQ_MAX_BATCH_SIZE 32
  131. #if defined(GGML_USE_HIPBLAS)
  132. #define __CUDA_ARCH__ 1300
  133. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  134. defined(__gfx1150__) || defined(__gfx1151__)
  135. #define RDNA3
  136. #endif
  137. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  138. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  139. #define RDNA2
  140. #endif
  141. #ifndef __has_builtin
  142. #define __has_builtin(x) 0
  143. #endif
  144. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  145. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  146. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  147. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  148. #if __has_builtin(__builtin_elementwise_sub_sat)
  149. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  150. return reinterpret_cast<const int&>(c);
  151. #else
  152. int8x4_t c;
  153. int16_t tmp;
  154. #pragma unroll
  155. for (int i = 0; i < 4; i++) {
  156. tmp = va[i] - vb[i];
  157. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  158. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  159. c[i] = tmp;
  160. }
  161. return reinterpret_cast<int&>(c);
  162. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  163. }
  164. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  165. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  166. c = __builtin_amdgcn_sdot4(a, b, c, false);
  167. #elif defined(__gfx1100__)
  168. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  169. #elif defined(__gfx1010__) || defined(__gfx900__)
  170. int tmp1;
  171. int tmp2;
  172. asm("\n \
  173. 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 \
  174. 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 \
  175. v_add3_u32 %0, %1, %2, %0 \n \
  176. 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 \
  177. 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 \
  178. v_add3_u32 %0, %1, %2, %0 \n \
  179. "
  180. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  181. : "v"(a), "v"(b)
  182. );
  183. #else
  184. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  185. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  186. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  187. #endif
  188. return c;
  189. }
  190. #endif // defined(GGML_USE_HIPBLAS)
  191. #if defined(_MSC_VER)
  192. #pragma warning(disable: 4244 4267) // possible loss of data
  193. #endif
  194. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  195. #if CUDART_VERSION >= 12000
  196. static const char * cublas_get_error_str(const cublasStatus_t err) {
  197. return cublasGetStatusString(err);
  198. }
  199. #else
  200. static const char * cublas_get_error_str(const cublasStatus_t err) {
  201. switch (err) {
  202. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  203. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  204. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  205. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  206. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  207. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  208. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  209. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  210. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  211. default: return "unknown error";
  212. }
  213. }
  214. #endif // CUDART_VERSION >= 12000
  215. [[noreturn]]
  216. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  217. fprintf(stderr, "CUDA error: %s: %s\n", stmt, msg);
  218. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  219. GGML_ASSERT(!"CUDA error");
  220. }
  221. #define CUDA_CHECK(err) do { auto err_ = (err); if (err_ != cudaSuccess) ggml_cuda_error(#err, __func__, __FILE__, __LINE__, cudaGetErrorString(err_)); } while (0)
  222. #define CUBLAS_CHECK(err) do { auto err_ = (err); if (err_ != CUBLAS_STATUS_SUCCESS) ggml_cuda_error(#err, __func__, __FILE__, __LINE__, cublas_get_error_str(err_)); } while (0)
  223. #if !defined(GGML_USE_HIPBLAS)
  224. static const char * cu_get_error_str(CUresult err) {
  225. const char * err_str;
  226. cuGetErrorString(err, &err_str);
  227. return err_str;
  228. }
  229. #define CU_CHECK(err) do { auto err_ = (err); if (err_ != CUDA_SUCCESS) ggml_cuda_error(#err, __func__, __FILE__, __LINE__, cu_get_error_str(err_)); } while (0)
  230. #endif
  231. #if CUDART_VERSION >= 11100
  232. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  233. #else
  234. #define GGML_CUDA_ASSUME(x)
  235. #endif // CUDART_VERSION >= 11100
  236. #ifdef GGML_CUDA_F16
  237. typedef half dfloat; // dequantize float
  238. typedef half2 dfloat2;
  239. #else
  240. typedef float dfloat; // dequantize float
  241. typedef float2 dfloat2;
  242. #endif //GGML_CUDA_F16
  243. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  244. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  245. int x32 = 0;
  246. x32 |= x16[0] << 0;
  247. x32 |= x16[1] << 16;
  248. return x32;
  249. }
  250. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  251. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  252. int x32 = 0;
  253. x32 |= x16[0] << 0;
  254. x32 |= x16[1] << 16;
  255. return x32;
  256. }
  257. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  258. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  259. }
  260. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  261. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  262. }
  263. template<typename T>
  264. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  265. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  266. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  267. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  268. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  269. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  270. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  271. typedef void (*ggml_cuda_op_mul_mat_t)(
  272. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  273. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  274. const int64_t src1_padded_row_size, const cudaStream_t & stream);
  275. typedef void (*ggml_cuda_op_flatten_t)(
  276. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  277. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream);
  278. // QK = number of values after dequantization
  279. // QR = QK / number of values before dequantization
  280. // QI = number of 32 bit integers before dequantization
  281. #define QK4_0 32
  282. #define QR4_0 2
  283. #define QI4_0 (QK4_0 / (4 * QR4_0))
  284. typedef struct {
  285. half d; // delta
  286. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  287. } block_q4_0;
  288. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  289. #define QK4_1 32
  290. #define QR4_1 2
  291. #define QI4_1 (QK4_1 / (4 * QR4_1))
  292. typedef struct {
  293. half2 dm; // dm.x = delta, dm.y = min
  294. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  295. } block_q4_1;
  296. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  297. #define QK5_0 32
  298. #define QR5_0 2
  299. #define QI5_0 (QK5_0 / (4 * QR5_0))
  300. typedef struct {
  301. half d; // delta
  302. uint8_t qh[4]; // 5-th bit of quants
  303. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  304. } block_q5_0;
  305. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  306. #define QK5_1 32
  307. #define QR5_1 2
  308. #define QI5_1 (QK5_1 / (4 * QR5_1))
  309. typedef struct {
  310. half2 dm; // dm.x = delta, dm.y = min
  311. uint8_t qh[4]; // 5-th bit of quants
  312. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  313. } block_q5_1;
  314. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  315. #define QK8_0 32
  316. #define QR8_0 1
  317. #define QI8_0 (QK8_0 / (4 * QR8_0))
  318. typedef struct {
  319. half d; // delta
  320. int8_t qs[QK8_0]; // quants
  321. } block_q8_0;
  322. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  323. #define QK8_1 32
  324. #define QR8_1 1
  325. #define QI8_1 (QK8_1 / (4 * QR8_1))
  326. typedef struct {
  327. half2 ds; // ds.x = delta, ds.y = sum
  328. int8_t qs[QK8_0]; // quants
  329. } block_q8_1;
  330. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  331. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  332. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  333. typedef void (*load_tiles_cuda_t)(
  334. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  335. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  336. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  337. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  338. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  339. //================================= k-quants
  340. #ifdef GGML_QKK_64
  341. #define QK_K 64
  342. #define K_SCALE_SIZE 4
  343. #else
  344. #define QK_K 256
  345. #define K_SCALE_SIZE 12
  346. #endif
  347. #define QR2_K 4
  348. #define QI2_K (QK_K / (4*QR2_K))
  349. typedef struct {
  350. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  351. uint8_t qs[QK_K/4]; // quants
  352. half2 dm; // super-block scale for quantized scales/mins
  353. } block_q2_K;
  354. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  355. #define QR3_K 4
  356. #define QI3_K (QK_K / (4*QR3_K))
  357. typedef struct {
  358. uint8_t hmask[QK_K/8]; // quants - high bit
  359. uint8_t qs[QK_K/4]; // quants - low 2 bits
  360. #ifdef GGML_QKK_64
  361. uint8_t scales[2]; // scales, quantized with 8 bits
  362. #else
  363. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  364. #endif
  365. half d; // super-block scale
  366. } block_q3_K;
  367. //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");
  368. #define QR4_K 2
  369. #define QI4_K (QK_K / (4*QR4_K))
  370. #ifdef GGML_QKK_64
  371. typedef struct {
  372. half dm[2]; // super-block scales/mins
  373. uint8_t scales[2]; // 4-bit block scales/mins
  374. uint8_t qs[QK_K/2]; // 4--bit quants
  375. } block_q4_K;
  376. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  377. #else
  378. typedef struct {
  379. half2 dm; // super-block scale for quantized scales/mins
  380. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  381. uint8_t qs[QK_K/2]; // 4--bit quants
  382. } block_q4_K;
  383. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  384. #endif
  385. #define QR5_K 2
  386. #define QI5_K (QK_K / (4*QR5_K))
  387. #ifdef GGML_QKK_64
  388. typedef struct {
  389. half d; // super-block scale
  390. int8_t scales[QK_K/16]; // block scales
  391. uint8_t qh[QK_K/8]; // quants, high bit
  392. uint8_t qs[QK_K/2]; // quants, low 4 bits
  393. } block_q5_K;
  394. 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");
  395. #else
  396. typedef struct {
  397. half2 dm; // super-block scale for quantized scales/mins
  398. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  399. uint8_t qh[QK_K/8]; // quants, high bit
  400. uint8_t qs[QK_K/2]; // quants, low 4 bits
  401. } block_q5_K;
  402. 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");
  403. #endif
  404. #define QR6_K 2
  405. #define QI6_K (QK_K / (4*QR6_K))
  406. typedef struct {
  407. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  408. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  409. int8_t scales[QK_K/16]; // scales
  410. half d; // delta
  411. } block_q6_K;
  412. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  413. #define WARP_SIZE 32
  414. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  415. #define CUDA_GELU_BLOCK_SIZE 256
  416. #define CUDA_SILU_BLOCK_SIZE 256
  417. #define CUDA_TANH_BLOCK_SIZE 256
  418. #define CUDA_RELU_BLOCK_SIZE 256
  419. #define CUDA_SQR_BLOCK_SIZE 256
  420. #define CUDA_CPY_BLOCK_SIZE 32
  421. #define CUDA_SCALE_BLOCK_SIZE 256
  422. #define CUDA_CLAMP_BLOCK_SIZE 256
  423. #define CUDA_ROPE_BLOCK_SIZE 256
  424. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  425. #define CUDA_ALIBI_BLOCK_SIZE 32
  426. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  427. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  428. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  429. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  430. #define CUDA_UPSCALE_BLOCK_SIZE 256
  431. #define CUDA_CONCAT_BLOCK_SIZE 256
  432. #define CUDA_PAD_BLOCK_SIZE 256
  433. #define CUDA_ACC_BLOCK_SIZE 256
  434. #define CUDA_IM2COL_BLOCK_SIZE 256
  435. // dmmv = dequantize_mul_mat_vec
  436. #ifndef GGML_CUDA_DMMV_X
  437. #define GGML_CUDA_DMMV_X 32
  438. #endif
  439. #ifndef GGML_CUDA_MMV_Y
  440. #define GGML_CUDA_MMV_Y 1
  441. #endif
  442. #ifndef K_QUANTS_PER_ITERATION
  443. #define K_QUANTS_PER_ITERATION 2
  444. #else
  445. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  446. #endif
  447. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  448. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  449. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  450. #define MUL_MAT_SRC1_COL_STRIDE 128
  451. #define MAX_STREAMS 8
  452. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  453. struct ggml_tensor_extra_gpu {
  454. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  455. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  456. };
  457. // this is faster on Windows
  458. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  459. inline cudaError_t ggml_cuda_set_device(const int device) {
  460. int current_device;
  461. CUDA_CHECK(cudaGetDevice(&current_device));
  462. if (device == current_device) {
  463. return cudaSuccess;
  464. }
  465. return cudaSetDevice(device);
  466. }
  467. static int g_device_count = -1;
  468. static int g_main_device = 0;
  469. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  470. struct cuda_device_capabilities {
  471. int cc; // compute capability
  472. bool vmm; // virtual memory support
  473. size_t vmm_granularity; // granularity of virtual memory
  474. };
  475. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, false, 0} };
  476. static void * g_scratch_buffer = nullptr;
  477. static size_t g_scratch_size = 0; // disabled by default
  478. static size_t g_scratch_offset = 0;
  479. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  480. [[noreturn]]
  481. static __device__ void bad_arch() {
  482. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  483. __trap();
  484. (void) bad_arch; // suppress unused function warning
  485. }
  486. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  487. #pragma unroll
  488. for (int mask = 16; mask > 0; mask >>= 1) {
  489. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  490. }
  491. return x;
  492. }
  493. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  494. #pragma unroll
  495. for (int mask = 16; mask > 0; mask >>= 1) {
  496. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  497. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  498. }
  499. return a;
  500. }
  501. static __device__ __forceinline__ float warp_reduce_max(float x) {
  502. #pragma unroll
  503. for (int mask = 16; mask > 0; mask >>= 1) {
  504. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  505. }
  506. return x;
  507. }
  508. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  509. return b;
  510. }
  511. static __device__ __forceinline__ float op_add(const float a, const float b) {
  512. return a + b;
  513. }
  514. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  515. return a * b;
  516. }
  517. static __device__ __forceinline__ float op_div(const float a, const float b) {
  518. return a / b;
  519. }
  520. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  521. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  522. int ne0, int ne1, int ne2, int ne3,
  523. int ne10, int ne11, int ne12, int ne13,
  524. /*int s0, */ int s1, int s2, int s3,
  525. /*int s10,*/ int s11, int s12, int s13) {
  526. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  527. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  528. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  529. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  530. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  531. return;
  532. }
  533. const int i11 = i1 % ne11;
  534. const int i12 = i2 % ne12;
  535. const int i13 = i3 % ne13;
  536. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  537. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  538. const size_t i_dst = i_src0;
  539. const src0_t * src0_row = src0 + i_src0;
  540. const src1_t * src1_row = src1 + i_src1;
  541. dst_t * dst_row = dst + i_dst;
  542. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  543. const int i10 = i0 % ne10;
  544. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  545. }
  546. }
  547. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  548. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  549. int ne0, int ne1, int ne2, int ne3,
  550. int ne10, int ne11, int ne12, int ne13,
  551. /*int s0, */ int s1, int s2, int s3,
  552. /*int s10,*/ int s11, int s12, int s13) {
  553. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  554. const int i3 = i/(ne2*ne1*ne0);
  555. const int i2 = (i/(ne1*ne0)) % ne2;
  556. const int i1 = (i/ne0) % ne1;
  557. const int i0 = i % ne0;
  558. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  559. return;
  560. }
  561. const int i11 = i1 % ne11;
  562. const int i12 = i2 % ne12;
  563. const int i13 = i3 % ne13;
  564. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  565. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  566. const size_t i_dst = i_src0;
  567. const src0_t * src0_row = src0 + i_src0;
  568. const src1_t * src1_row = src1 + i_src1;
  569. dst_t * dst_row = dst + i_dst;
  570. const int i10 = i0 % ne10;
  571. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  572. }
  573. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  574. const int ne10, const int ne11, const int ne12,
  575. const int nb1, const int nb2, int offset) {
  576. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  577. if (i >= ne) {
  578. return;
  579. }
  580. int src1_idx = i - offset;
  581. int oz = src1_idx / nb2;
  582. int oy = (src1_idx - (oz * nb2)) / nb1;
  583. int ox = src1_idx % nb1;
  584. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  585. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  586. } else {
  587. dst[i] = x[i];
  588. }
  589. }
  590. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  591. const float GELU_COEF_A = 0.044715f;
  592. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  593. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  594. if (i >= k) {
  595. return;
  596. }
  597. float xi = x[i];
  598. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  599. }
  600. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  601. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  602. if (i >= k) {
  603. return;
  604. }
  605. dst[i] = x[i] / (1.0f + expf(-x[i]));
  606. }
  607. static __global__ void gelu_quick_f32(const float *x, float *dst, int k) {
  608. const float GELU_QUICK_COEF = -1.702f;
  609. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  610. if (i >= k) {
  611. return;
  612. }
  613. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  614. }
  615. static __global__ void tanh_f32(const float *x, float *dst, int k) {
  616. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  617. if (i >= k) {
  618. return;
  619. }
  620. dst[i] = tanhf(x[i]);
  621. }
  622. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  623. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  624. if (i >= k) {
  625. return;
  626. }
  627. dst[i] = fmaxf(x[i], 0);
  628. }
  629. static __global__ void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope) {
  630. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  631. if (i >= k) {
  632. return;
  633. }
  634. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  635. }
  636. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  637. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  638. if (i >= k) {
  639. return;
  640. }
  641. dst[i] = x[i] * x[i];
  642. }
  643. template <int block_size>
  644. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  645. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  646. const int tid = threadIdx.x;
  647. float2 mean_var = make_float2(0.f, 0.f);
  648. for (int col = tid; col < ncols; col += block_size) {
  649. const float xi = x[row*ncols + col];
  650. mean_var.x += xi;
  651. mean_var.y += xi * xi;
  652. }
  653. // sum up partial sums
  654. mean_var = warp_reduce_sum(mean_var);
  655. if (block_size > WARP_SIZE) {
  656. __shared__ float2 s_sum[32];
  657. int warp_id = threadIdx.x / WARP_SIZE;
  658. int lane_id = threadIdx.x % WARP_SIZE;
  659. if (lane_id == 0) {
  660. s_sum[warp_id] = mean_var;
  661. }
  662. __syncthreads();
  663. mean_var = s_sum[lane_id];
  664. mean_var = warp_reduce_sum(mean_var);
  665. }
  666. const float mean = mean_var.x / ncols;
  667. const float var = mean_var.y / ncols - mean * mean;
  668. const float inv_std = rsqrtf(var + eps);
  669. for (int col = tid; col < ncols; col += block_size) {
  670. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  671. }
  672. }
  673. static __global__ void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02) {
  674. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  675. if (nidx >= ne0) {
  676. return;
  677. }
  678. // operation
  679. int offset_dst =
  680. nidx +
  681. blockIdx.y * ne0 +
  682. blockIdx.z * ne0 * gridDim.y;
  683. if (blockIdx.z < ne02) { // src0
  684. int offset_src =
  685. nidx +
  686. blockIdx.y * ne0 +
  687. blockIdx.z * ne0 * gridDim.y;
  688. dst[offset_dst] = x[offset_src];
  689. } else {
  690. int offset_src =
  691. nidx +
  692. blockIdx.y * ne0 +
  693. (blockIdx.z - ne02) * ne0 * gridDim.y;
  694. dst[offset_dst] = y[offset_src];
  695. }
  696. }
  697. static __global__ void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor) {
  698. int ne0 = ne00 * scale_factor;
  699. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  700. if (nidx >= ne0) {
  701. return;
  702. }
  703. // operation
  704. int i00 = nidx / scale_factor;
  705. int i01 = blockIdx.y / scale_factor;
  706. int offset_src =
  707. i00 +
  708. i01 * ne00 +
  709. blockIdx.z * nb02;
  710. int offset_dst =
  711. nidx +
  712. blockIdx.y * ne0 +
  713. blockIdx.z * ne0 * gridDim.y;
  714. dst[offset_dst] = x[offset_src];
  715. }
  716. static __global__ void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  717. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  718. if (nidx >= ne0) {
  719. return;
  720. }
  721. // operation
  722. int offset_dst =
  723. nidx +
  724. blockIdx.y * ne0 +
  725. blockIdx.z * ne0 * gridDim.y;
  726. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  727. int offset_src =
  728. nidx +
  729. blockIdx.y * ne00 +
  730. blockIdx.z * ne00 * ne01;
  731. dst[offset_dst] = x[offset_src];
  732. } else {
  733. dst[offset_dst] = 0.0f;
  734. }
  735. }
  736. template <int block_size>
  737. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  738. int start = blockIdx.x * group_size;
  739. int end = start + group_size;
  740. start += threadIdx.x;
  741. if (end >= ne_elements) {
  742. end = ne_elements;
  743. }
  744. float tmp = 0.0f; // partial sum for thread in warp
  745. for (int j = start; j < end; j += block_size) {
  746. tmp += x[j];
  747. }
  748. tmp = warp_reduce_sum(tmp);
  749. if (block_size > WARP_SIZE) {
  750. __shared__ float s_sum[32];
  751. int warp_id = threadIdx.x / WARP_SIZE;
  752. int lane_id = threadIdx.x % WARP_SIZE;
  753. if (lane_id == 0) {
  754. s_sum[warp_id] = tmp;
  755. }
  756. __syncthreads();
  757. tmp = s_sum[lane_id];
  758. tmp = warp_reduce_sum(tmp);
  759. }
  760. float mean = tmp / group_size;
  761. tmp = 0.0f;
  762. for (int j = start; j < end; j += block_size) {
  763. float xi = x[j] - mean;
  764. dst[j] = xi;
  765. tmp += xi * xi;
  766. }
  767. tmp = warp_reduce_sum(tmp);
  768. if (block_size > WARP_SIZE) {
  769. __shared__ float s_sum[32];
  770. int warp_id = threadIdx.x / WARP_SIZE;
  771. int lane_id = threadIdx.x % WARP_SIZE;
  772. if (lane_id == 0) {
  773. s_sum[warp_id] = tmp;
  774. }
  775. __syncthreads();
  776. tmp = s_sum[lane_id];
  777. tmp = warp_reduce_sum(tmp);
  778. }
  779. float variance = tmp / group_size;
  780. float scale = rsqrtf(variance + eps);
  781. for (int j = start; j < end; j += block_size) {
  782. dst[j] *= scale;
  783. }
  784. }
  785. template <int block_size>
  786. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  787. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  788. const int tid = threadIdx.x;
  789. float tmp = 0.0f; // partial sum for thread in warp
  790. for (int col = tid; col < ncols; col += block_size) {
  791. const float xi = x[row*ncols + col];
  792. tmp += xi * xi;
  793. }
  794. // sum up partial sums
  795. tmp = warp_reduce_sum(tmp);
  796. if (block_size > WARP_SIZE) {
  797. __shared__ float s_sum[32];
  798. int warp_id = threadIdx.x / WARP_SIZE;
  799. int lane_id = threadIdx.x % WARP_SIZE;
  800. if (lane_id == 0) {
  801. s_sum[warp_id] = tmp;
  802. }
  803. __syncthreads();
  804. tmp = s_sum[lane_id];
  805. tmp = warp_reduce_sum(tmp);
  806. }
  807. const float mean = tmp / ncols;
  808. const float scale = rsqrtf(mean + eps);
  809. for (int col = tid; col < ncols; col += block_size) {
  810. dst[row*ncols + col] = scale * x[row*ncols + col];
  811. }
  812. }
  813. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  814. const block_q4_0 * x = (const block_q4_0 *) vx;
  815. const dfloat d = x[ib].d;
  816. const int vui = x[ib].qs[iqs];
  817. v.x = vui & 0xF;
  818. v.y = vui >> 4;
  819. #ifdef GGML_CUDA_F16
  820. v = __hsub2(v, {8.0f, 8.0f});
  821. v = __hmul2(v, {d, d});
  822. #else
  823. v.x = (v.x - 8.0f) * d;
  824. v.y = (v.y - 8.0f) * d;
  825. #endif // GGML_CUDA_F16
  826. }
  827. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  828. const block_q4_1 * x = (const block_q4_1 *) vx;
  829. const dfloat d = __low2half(x[ib].dm);
  830. const dfloat m = __high2half(x[ib].dm);
  831. const int vui = x[ib].qs[iqs];
  832. v.x = vui & 0xF;
  833. v.y = vui >> 4;
  834. #ifdef GGML_CUDA_F16
  835. v = __hmul2(v, {d, d});
  836. v = __hadd2(v, {m, m});
  837. #else
  838. v.x = (v.x * d) + m;
  839. v.y = (v.y * d) + m;
  840. #endif // GGML_CUDA_F16
  841. }
  842. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  843. const block_q5_0 * x = (const block_q5_0 *) vx;
  844. const dfloat d = x[ib].d;
  845. uint32_t qh;
  846. memcpy(&qh, x[ib].qh, sizeof(qh));
  847. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  848. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  849. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  850. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  851. #ifdef GGML_CUDA_F16
  852. v = __hsub2(v, {16.0f, 16.0f});
  853. v = __hmul2(v, {d, d});
  854. #else
  855. v.x = (v.x - 16.0f) * d;
  856. v.y = (v.y - 16.0f) * d;
  857. #endif // GGML_CUDA_F16
  858. }
  859. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  860. const block_q5_1 * x = (const block_q5_1 *) vx;
  861. const dfloat d = __low2half(x[ib].dm);
  862. const dfloat m = __high2half(x[ib].dm);
  863. uint32_t qh;
  864. memcpy(&qh, x[ib].qh, sizeof(qh));
  865. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  866. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  867. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  868. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  869. #ifdef GGML_CUDA_F16
  870. v = __hmul2(v, {d, d});
  871. v = __hadd2(v, {m, m});
  872. #else
  873. v.x = (v.x * d) + m;
  874. v.y = (v.y * d) + m;
  875. #endif // GGML_CUDA_F16
  876. }
  877. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  878. const block_q8_0 * x = (const block_q8_0 *) vx;
  879. const dfloat d = x[ib].d;
  880. v.x = x[ib].qs[iqs + 0];
  881. v.y = x[ib].qs[iqs + 1];
  882. #ifdef GGML_CUDA_F16
  883. v = __hmul2(v, {d, d});
  884. #else
  885. v.x *= d;
  886. v.y *= d;
  887. #endif // GGML_CUDA_F16
  888. }
  889. //================================== k-quants
  890. template<typename dst_t>
  891. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  892. const int i = blockIdx.x;
  893. const block_q2_K * x = (const block_q2_K *) vx;
  894. const int tid = threadIdx.x;
  895. #if QK_K == 256
  896. const int n = tid/32;
  897. const int l = tid - 32*n;
  898. const int is = 8*n + l/16;
  899. const uint8_t q = x[i].qs[32*n + l];
  900. dst_t * y = yy + i*QK_K + 128*n;
  901. float dall = __low2half(x[i].dm);
  902. float dmin = __high2half(x[i].dm);
  903. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  904. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  905. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  906. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  907. #else
  908. const int is = tid/16; // 0 or 1
  909. const int il = tid%16; // 0...15
  910. const uint8_t q = x[i].qs[il] >> (2*is);
  911. dst_t * y = yy + i*QK_K + 16*is + il;
  912. float dall = __low2half(x[i].dm);
  913. float dmin = __high2half(x[i].dm);
  914. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  915. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  916. #endif
  917. }
  918. template<typename dst_t>
  919. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  920. const int i = blockIdx.x;
  921. const block_q3_K * x = (const block_q3_K *) vx;
  922. #if QK_K == 256
  923. const int r = threadIdx.x/4;
  924. const int tid = r/2;
  925. const int is0 = r%2;
  926. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  927. const int n = tid / 4;
  928. const int j = tid - 4*n;
  929. uint8_t m = 1 << (4*n + j);
  930. int is = 8*n + 2*j + is0;
  931. int shift = 2*j;
  932. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  933. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  934. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  935. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  936. float d_all = x[i].d;
  937. float dl = d_all * (us - 32);
  938. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  939. const uint8_t * q = x[i].qs + 32*n;
  940. const uint8_t * hm = x[i].hmask;
  941. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  942. #else
  943. const int tid = threadIdx.x;
  944. const int is = tid/16; // 0 or 1
  945. const int il = tid%16; // 0...15
  946. const int im = il/8; // 0...1
  947. const int in = il%8; // 0...7
  948. dst_t * y = yy + i*QK_K + 16*is + il;
  949. const uint8_t q = x[i].qs[il] >> (2*is);
  950. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  951. const float d = (float)x[i].d;
  952. if (is == 0) {
  953. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  954. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  955. } else {
  956. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  957. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  958. }
  959. #endif
  960. }
  961. #if QK_K == 256
  962. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  963. if (j < 4) {
  964. d = q[j] & 63; m = q[j + 4] & 63;
  965. } else {
  966. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  967. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  968. }
  969. }
  970. #endif
  971. template<typename dst_t>
  972. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  973. const block_q4_K * x = (const block_q4_K *) vx;
  974. const int i = blockIdx.x;
  975. #if QK_K == 256
  976. // assume 32 threads
  977. const int tid = threadIdx.x;
  978. const int il = tid/8;
  979. const int ir = tid%8;
  980. const int is = 2*il;
  981. const int n = 4;
  982. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  983. const float dall = __low2half(x[i].dm);
  984. const float dmin = __high2half(x[i].dm);
  985. const uint8_t * q = x[i].qs + 32*il + n*ir;
  986. uint8_t sc, m;
  987. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  988. const float d1 = dall * sc; const float m1 = dmin * m;
  989. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  990. const float d2 = dall * sc; const float m2 = dmin * m;
  991. for (int l = 0; l < n; ++l) {
  992. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  993. y[l +32] = d2 * (q[l] >> 4) - m2;
  994. }
  995. #else
  996. const int tid = threadIdx.x;
  997. const uint8_t * q = x[i].qs;
  998. dst_t * y = yy + i*QK_K;
  999. const float d = (float)x[i].dm[0];
  1000. const float m = (float)x[i].dm[1];
  1001. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1002. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1003. #endif
  1004. }
  1005. template<typename dst_t>
  1006. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1007. const block_q5_K * x = (const block_q5_K *) vx;
  1008. const int i = blockIdx.x;
  1009. #if QK_K == 256
  1010. // assume 64 threads - this is very slightly better than the one below
  1011. const int tid = threadIdx.x;
  1012. const int il = tid/16; // il is in 0...3
  1013. const int ir = tid%16; // ir is in 0...15
  1014. const int is = 2*il; // is is in 0...6
  1015. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1016. const float dall = __low2half(x[i].dm);
  1017. const float dmin = __high2half(x[i].dm);
  1018. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1019. const uint8_t * qh = x[i].qh + 2*ir;
  1020. uint8_t sc, m;
  1021. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1022. const float d1 = dall * sc; const float m1 = dmin * m;
  1023. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1024. const float d2 = dall * sc; const float m2 = dmin * m;
  1025. uint8_t hm = 1 << (2*il);
  1026. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1027. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1028. hm <<= 1;
  1029. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1030. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1031. #else
  1032. const int tid = threadIdx.x;
  1033. const uint8_t q = x[i].qs[tid];
  1034. const int im = tid/8; // 0...3
  1035. const int in = tid%8; // 0...7
  1036. const int is = tid/16; // 0 or 1
  1037. const uint8_t h = x[i].qh[in] >> im;
  1038. const float d = x[i].d;
  1039. dst_t * y = yy + i*QK_K + tid;
  1040. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1041. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1042. #endif
  1043. }
  1044. template<typename dst_t>
  1045. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1046. const block_q6_K * x = (const block_q6_K *) vx;
  1047. const int i = blockIdx.x;
  1048. #if QK_K == 256
  1049. // assume 64 threads - this is very slightly better than the one below
  1050. const int tid = threadIdx.x;
  1051. const int ip = tid/32; // ip is 0 or 1
  1052. const int il = tid - 32*ip; // 0...32
  1053. const int is = 8*ip + il/16;
  1054. dst_t * y = yy + i*QK_K + 128*ip + il;
  1055. const float d = x[i].d;
  1056. const uint8_t * ql = x[i].ql + 64*ip + il;
  1057. const uint8_t qh = x[i].qh[32*ip + il];
  1058. const int8_t * sc = x[i].scales + is;
  1059. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1060. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1061. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1062. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1063. #else
  1064. // assume 32 threads
  1065. const int tid = threadIdx.x;
  1066. const int ip = tid/16; // 0 or 1
  1067. const int il = tid - 16*ip; // 0...15
  1068. dst_t * y = yy + i*QK_K + 16*ip + il;
  1069. const float d = x[i].d;
  1070. const uint8_t ql = x[i].ql[16*ip + il];
  1071. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1072. const int8_t * sc = x[i].scales;
  1073. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1074. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1075. #endif
  1076. }
  1077. 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) {
  1078. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1079. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1080. if (row > nrows) return;
  1081. const int num_blocks_per_row = ncols / QK_K;
  1082. const int ib0 = row*num_blocks_per_row;
  1083. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1084. float tmp = 0; // partial sum for thread in warp
  1085. #if QK_K == 256
  1086. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1087. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1088. const int step = 16/K_QUANTS_PER_ITERATION;
  1089. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1090. const int in = tid - step*im; // 0...15 or 0...7
  1091. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1092. const int q_offset = 32*im + l0;
  1093. const int s_offset = 8*im;
  1094. const int y_offset = 128*im + l0;
  1095. uint32_t aux[4];
  1096. const uint8_t * d = (const uint8_t *)aux;
  1097. const uint8_t * m = (const uint8_t *)(aux + 2);
  1098. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1099. const float * y = yy + i * QK_K + y_offset;
  1100. const uint8_t * q = x[i].qs + q_offset;
  1101. const float dall = __low2half(x[i].dm);
  1102. const float dmin = __high2half(x[i].dm);
  1103. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1104. aux[0] = a[0] & 0x0f0f0f0f;
  1105. aux[1] = a[1] & 0x0f0f0f0f;
  1106. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1107. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1108. float sum1 = 0, sum2 = 0;
  1109. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1110. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1111. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1112. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1113. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1114. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1115. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1116. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1117. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1118. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1119. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1120. }
  1121. tmp += dall * sum1 - dmin * sum2;
  1122. }
  1123. #else
  1124. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1125. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1126. const int offset = tid * K_QUANTS_PER_ITERATION;
  1127. uint32_t uaux[2];
  1128. const uint8_t * d = (const uint8_t *)uaux;
  1129. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1130. const float * y = yy + i * QK_K + offset;
  1131. const uint8_t * q = x[i].qs + offset;
  1132. const uint32_t * s = (const uint32_t *)x[i].scales;
  1133. uaux[0] = s[0] & 0x0f0f0f0f;
  1134. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1135. const float2 dall = __half22float2(x[i].dm);
  1136. float sum1 = 0, sum2 = 0;
  1137. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1138. const uint8_t ql = q[l];
  1139. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1140. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1141. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1142. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1143. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1144. }
  1145. tmp += dall.x * sum1 - dall.y * sum2;
  1146. }
  1147. #endif
  1148. // sum up partial sums and write back result
  1149. #pragma unroll
  1150. for (int mask = 16; mask > 0; mask >>= 1) {
  1151. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1152. }
  1153. if (threadIdx.x == 0) {
  1154. dst[row] = tmp;
  1155. }
  1156. }
  1157. 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) {
  1158. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1159. if (row > nrows) return;
  1160. const int num_blocks_per_row = ncols / QK_K;
  1161. const int ib0 = row*num_blocks_per_row;
  1162. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1163. float tmp = 0; // partial sum for thread in warp
  1164. #if QK_K == 256
  1165. const uint16_t kmask1 = 0x0303;
  1166. const uint16_t kmask2 = 0x0f0f;
  1167. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1168. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1169. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1170. const int step = 16/K_QUANTS_PER_ITERATION;
  1171. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1172. const int in = tid - step*im; // 0....15 or 0...7
  1173. const uint8_t m = 1 << (4*im);
  1174. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1175. const int q_offset = 32*im + l0;
  1176. const int y_offset = 128*im + l0;
  1177. uint16_t utmp[4];
  1178. const int8_t * s = (const int8_t *)utmp;
  1179. const uint16_t s_shift = 4*im;
  1180. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1181. const float * y = yy + i * QK_K + y_offset;
  1182. const uint8_t * q = x[i].qs + q_offset;
  1183. const uint8_t * h = x[i].hmask + l0;
  1184. const uint16_t * a = (const uint16_t *)x[i].scales;
  1185. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1186. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1187. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1188. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1189. const float d = x[i].d;
  1190. float sum = 0;
  1191. for (int l = 0; l < n; ++l) {
  1192. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1193. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1194. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1195. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1196. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1197. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1198. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1199. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1200. }
  1201. tmp += d * sum;
  1202. }
  1203. #else
  1204. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1205. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1206. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1207. const int in = offset/8; // 0 or 1
  1208. const int im = offset%8; // 0...7
  1209. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1210. const float * y = yy + i * QK_K + offset;
  1211. const uint8_t * q = x[i].qs + offset;
  1212. const uint8_t * s = x[i].scales;
  1213. const float dall = (float)x[i].d;
  1214. float sum = 0;
  1215. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1216. const uint8_t hl = x[i].hmask[im+l] >> in;
  1217. const uint8_t ql = q[l];
  1218. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1219. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1220. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1221. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1222. }
  1223. tmp += sum;
  1224. }
  1225. #endif
  1226. // sum up partial sums and write back result
  1227. #pragma unroll
  1228. for (int mask = 16; mask > 0; mask >>= 1) {
  1229. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1230. }
  1231. if (threadIdx.x == 0) {
  1232. dst[row] = tmp;
  1233. }
  1234. }
  1235. 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) {
  1236. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1237. if (row > nrows) return;
  1238. const int num_blocks_per_row = ncols / QK_K;
  1239. const int ib0 = row*num_blocks_per_row;
  1240. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1241. #if QK_K == 256
  1242. const uint16_t kmask1 = 0x3f3f;
  1243. const uint16_t kmask2 = 0x0f0f;
  1244. const uint16_t kmask3 = 0xc0c0;
  1245. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1246. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1247. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1248. const int il = tid/step; // 0...3
  1249. const int ir = tid - step*il; // 0...7 or 0...3
  1250. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1251. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1252. const int in = il%2;
  1253. const int l0 = n*(2*ir + in);
  1254. const int q_offset = 32*im + l0;
  1255. const int y_offset = 64*im + l0;
  1256. uint16_t aux[4];
  1257. const uint8_t * sc = (const uint8_t *)aux;
  1258. #if K_QUANTS_PER_ITERATION == 2
  1259. uint32_t q32[4];
  1260. const uint8_t * q4 = (const uint8_t *)q32;
  1261. #else
  1262. uint16_t q16[4];
  1263. const uint8_t * q4 = (const uint8_t *)q16;
  1264. #endif
  1265. float tmp = 0; // partial sum for thread in warp
  1266. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1267. const float * y1 = yy + i*QK_K + y_offset;
  1268. const float * y2 = y1 + 128;
  1269. const float dall = __low2half(x[i].dm);
  1270. const float dmin = __high2half(x[i].dm);
  1271. const uint16_t * a = (const uint16_t *)x[i].scales;
  1272. aux[0] = a[im+0] & kmask1;
  1273. aux[1] = a[im+2] & kmask1;
  1274. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1275. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1276. #if K_QUANTS_PER_ITERATION == 2
  1277. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1278. const uint32_t * q2 = q1 + 16;
  1279. q32[0] = q1[0] & 0x0f0f0f0f;
  1280. q32[1] = q1[0] & 0xf0f0f0f0;
  1281. q32[2] = q2[0] & 0x0f0f0f0f;
  1282. q32[3] = q2[0] & 0xf0f0f0f0;
  1283. float4 s = {0.f, 0.f, 0.f, 0.f};
  1284. float smin = 0;
  1285. for (int l = 0; l < 4; ++l) {
  1286. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1287. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1288. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1289. }
  1290. 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;
  1291. #else
  1292. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1293. const uint16_t * q2 = q1 + 32;
  1294. q16[0] = q1[0] & 0x0f0f;
  1295. q16[1] = q1[0] & 0xf0f0;
  1296. q16[2] = q2[0] & 0x0f0f;
  1297. q16[3] = q2[0] & 0xf0f0;
  1298. float4 s = {0.f, 0.f, 0.f, 0.f};
  1299. float smin = 0;
  1300. for (int l = 0; l < 2; ++l) {
  1301. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1302. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1303. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1304. }
  1305. 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;
  1306. #endif
  1307. }
  1308. #else
  1309. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1310. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1311. const int step = tid * K_QUANTS_PER_ITERATION;
  1312. uint16_t aux16[2];
  1313. const uint8_t * s = (const uint8_t *)aux16;
  1314. float tmp = 0;
  1315. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1316. const uint8_t * q = x[i].qs + step;
  1317. const float * y = yy + i*QK_K + step;
  1318. const uint16_t * a = (const uint16_t *)x[i].scales;
  1319. aux16[0] = a[0] & 0x0f0f;
  1320. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1321. const float d = (float)x[i].dm[0];
  1322. const float m = (float)x[i].dm[1];
  1323. float sum = 0.f;
  1324. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1325. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1326. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1327. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1328. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1329. }
  1330. tmp += sum;
  1331. }
  1332. #endif
  1333. // sum up partial sums and write back result
  1334. #pragma unroll
  1335. for (int mask = 16; mask > 0; mask >>= 1) {
  1336. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1337. }
  1338. if (tid == 0) {
  1339. dst[row] = tmp;
  1340. }
  1341. }
  1342. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1343. const int row = blockIdx.x;
  1344. const int num_blocks_per_row = ncols / QK_K;
  1345. const int ib0 = row*num_blocks_per_row;
  1346. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1347. float tmp = 0; // partial sum for thread in warp
  1348. #if QK_K == 256
  1349. const uint16_t kmask1 = 0x3f3f;
  1350. const uint16_t kmask2 = 0x0f0f;
  1351. const uint16_t kmask3 = 0xc0c0;
  1352. const int tid = threadIdx.x/2; // 0...15
  1353. const int ix = threadIdx.x%2;
  1354. const int il = tid/4; // 0...3
  1355. const int ir = tid - 4*il;// 0...3
  1356. const int n = 2;
  1357. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1358. const int in = il%2;
  1359. const int l0 = n*(2*ir + in);
  1360. const int q_offset = 32*im + l0;
  1361. const int y_offset = 64*im + l0;
  1362. const uint8_t hm1 = 1 << (2*im);
  1363. const uint8_t hm2 = hm1 << 4;
  1364. uint16_t aux[4];
  1365. const uint8_t * sc = (const uint8_t *)aux;
  1366. uint16_t q16[8];
  1367. const uint8_t * q4 = (const uint8_t *)q16;
  1368. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1369. const uint8_t * ql1 = x[i].qs + q_offset;
  1370. const uint8_t * qh = x[i].qh + l0;
  1371. const float * y1 = yy + i*QK_K + y_offset;
  1372. const float * y2 = y1 + 128;
  1373. const float dall = __low2half(x[i].dm);
  1374. const float dmin = __high2half(x[i].dm);
  1375. const uint16_t * a = (const uint16_t *)x[i].scales;
  1376. aux[0] = a[im+0] & kmask1;
  1377. aux[1] = a[im+2] & kmask1;
  1378. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1379. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1380. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1381. float smin = 0;
  1382. const uint16_t * q1 = (const uint16_t *)ql1;
  1383. const uint16_t * q2 = q1 + 32;
  1384. q16[0] = q1[0] & 0x0f0f;
  1385. q16[1] = q1[8] & 0x0f0f;
  1386. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1387. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1388. q16[4] = q2[0] & 0x0f0f;
  1389. q16[5] = q2[8] & 0x0f0f;
  1390. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1391. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1392. for (int l = 0; l < n; ++l) {
  1393. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1394. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1395. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1396. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1397. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1398. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1399. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1400. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1401. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1402. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1403. }
  1404. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1405. }
  1406. #else
  1407. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1408. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1409. const int step = tid * K_QUANTS_PER_ITERATION;
  1410. const int im = step/8;
  1411. const int in = step%8;
  1412. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1413. const uint8_t * q = x[i].qs + step;
  1414. const int8_t * s = x[i].scales;
  1415. const float * y = yy + i*QK_K + step;
  1416. const float d = x[i].d;
  1417. float sum = 0.f;
  1418. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1419. const uint8_t h = x[i].qh[in+j] >> im;
  1420. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1421. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1422. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1423. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1424. }
  1425. tmp += sum;
  1426. }
  1427. #endif
  1428. // sum up partial sums and write back result
  1429. #pragma unroll
  1430. for (int mask = 16; mask > 0; mask >>= 1) {
  1431. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1432. }
  1433. if (threadIdx.x == 0) {
  1434. dst[row] = tmp;
  1435. }
  1436. }
  1437. 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) {
  1438. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1439. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1440. if (row > nrows) return;
  1441. const int num_blocks_per_row = ncols / QK_K;
  1442. const int ib0 = row*num_blocks_per_row;
  1443. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1444. #if QK_K == 256
  1445. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1446. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1447. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1448. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1449. const int in = tid - step*im; // 0...15 or 0...7
  1450. #if K_QUANTS_PER_ITERATION == 1
  1451. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1452. const int is = 0;
  1453. #else
  1454. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1455. const int is = in / 4;
  1456. #endif
  1457. const int ql_offset = 64*im + l0;
  1458. const int qh_offset = 32*im + l0;
  1459. const int s_offset = 8*im + is;
  1460. const int y_offset = 128*im + l0;
  1461. float tmp = 0; // partial sum for thread in warp
  1462. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1463. const float * y = yy + i * QK_K + y_offset;
  1464. const uint8_t * ql = x[i].ql + ql_offset;
  1465. const uint8_t * qh = x[i].qh + qh_offset;
  1466. const int8_t * s = x[i].scales + s_offset;
  1467. const float d = x[i].d;
  1468. #if K_QUANTS_PER_ITERATION == 1
  1469. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1470. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1471. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1472. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1473. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1474. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1475. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1476. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1477. tmp += sum;
  1478. #else
  1479. float sum = 0;
  1480. for (int l = 0; l < 4; ++l) {
  1481. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1482. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1483. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1484. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1485. }
  1486. tmp += sum;
  1487. #endif
  1488. }
  1489. #else
  1490. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1491. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1492. const int step = tid * K_QUANTS_PER_ITERATION;
  1493. float tmp = 0; // partial sum for thread in warp
  1494. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1495. const float * y = yy + i * QK_K + step;
  1496. const uint8_t * ql = x[i].ql + step;
  1497. const uint8_t * qh = x[i].qh + step;
  1498. const int8_t * s = x[i].scales;
  1499. const float d = x[i+0].d;
  1500. float sum = 0;
  1501. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1502. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1503. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1504. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1505. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1506. }
  1507. tmp += sum;
  1508. }
  1509. #endif
  1510. // sum up partial sums and write back result
  1511. #pragma unroll
  1512. for (int mask = 16; mask > 0; mask >>= 1) {
  1513. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1514. }
  1515. if (tid == 0) {
  1516. dst[row] = tmp;
  1517. }
  1518. }
  1519. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1520. const half * x = (const half *) vx;
  1521. // automatic half -> float type cast if dfloat == float
  1522. v.x = x[ib + iqs + 0];
  1523. v.y = x[ib + iqs + 1];
  1524. }
  1525. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1526. const float * x = (const float *) vx;
  1527. // automatic half -> float type cast if dfloat == float
  1528. v.x = x[ib + iqs + 0];
  1529. v.y = x[ib + iqs + 1];
  1530. }
  1531. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1532. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1533. if (ix >= kx_padded) {
  1534. return;
  1535. }
  1536. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1537. const int i_padded = iy*kx_padded + ix;
  1538. block_q8_1 * y = (block_q8_1 *) vy;
  1539. const int ib = i_padded / QK8_1; // block index
  1540. const int iqs = i_padded % QK8_1; // quant index
  1541. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1542. float amax = fabsf(xi);
  1543. float sum = xi;
  1544. #pragma unroll
  1545. for (int mask = 16; mask > 0; mask >>= 1) {
  1546. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1547. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1548. }
  1549. const float d = amax / 127;
  1550. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1551. y[ib].qs[iqs] = q;
  1552. if (iqs > 0) {
  1553. return;
  1554. }
  1555. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1556. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1557. }
  1558. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1559. static __global__ void k_get_rows(
  1560. const void * src0, const int32_t * src1, dst_t * dst,
  1561. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1562. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1563. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1564. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1565. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1566. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1567. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1568. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1569. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1570. if (i00 >= ne00) {
  1571. return;
  1572. }
  1573. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1574. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1575. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1576. const int ib = i00/qk; // block index
  1577. const int iqs = (i00%qk)/qr; // quant index
  1578. const int iybs = i00 - i00%qk; // dst block start index
  1579. const int y_offset = qr == 1 ? 1 : qk/2;
  1580. // dequantize
  1581. dfloat2 v;
  1582. dequantize_kernel(src0_row, ib, iqs, v);
  1583. dst_row[iybs + iqs + 0] = v.x;
  1584. dst_row[iybs + iqs + y_offset] = v.y;
  1585. }
  1586. template<typename src0_t, typename dst_t>
  1587. static __global__ void k_get_rows_float(
  1588. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1589. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1590. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1591. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1592. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1593. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1594. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1595. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1596. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1597. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1598. if (i00 >= ne00) {
  1599. return;
  1600. }
  1601. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1602. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1603. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1604. dst_row[i00] = src0_row[i00];
  1605. }
  1606. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1607. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1608. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1609. if (i >= k) {
  1610. return;
  1611. }
  1612. const int ib = i/qk; // block index
  1613. const int iqs = (i%qk)/qr; // quant index
  1614. const int iybs = i - i%qk; // y block start index
  1615. const int y_offset = qr == 1 ? 1 : qk/2;
  1616. // dequantize
  1617. dfloat2 v;
  1618. dequantize_kernel(vx, ib, iqs, v);
  1619. y[iybs + iqs + 0] = v.x;
  1620. y[iybs + iqs + y_offset] = v.y;
  1621. }
  1622. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1623. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1624. #define VDR_Q4_0_Q8_1_MMVQ 2
  1625. #define VDR_Q4_0_Q8_1_MMQ 4
  1626. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1627. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1628. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1629. int sumi = 0;
  1630. #pragma unroll
  1631. for (int i = 0; i < vdr; ++i) {
  1632. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1633. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1634. // SIMD dot product of quantized values
  1635. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1636. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1637. }
  1638. const float2 ds8f = __half22float2(ds8);
  1639. // second part effectively subtracts 8 from each quant value
  1640. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1641. #else
  1642. bad_arch();
  1643. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1644. }
  1645. #define VDR_Q4_1_Q8_1_MMVQ 2
  1646. #define VDR_Q4_1_Q8_1_MMQ 4
  1647. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1648. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1649. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1650. int sumi = 0;
  1651. #pragma unroll
  1652. for (int i = 0; i < vdr; ++i) {
  1653. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1654. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1655. // SIMD dot product of quantized values
  1656. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1657. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1658. }
  1659. #ifdef GGML_CUDA_F16
  1660. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1661. const float d4d8 = tmp.x;
  1662. const float m4s8 = tmp.y;
  1663. #else
  1664. const float2 dm4f = __half22float2(dm4);
  1665. const float2 ds8f = __half22float2(ds8);
  1666. const float d4d8 = dm4f.x * ds8f.x;
  1667. const float m4s8 = dm4f.y * ds8f.y;
  1668. #endif // GGML_CUDA_F16
  1669. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1670. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1671. #else
  1672. bad_arch();
  1673. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1674. }
  1675. #define VDR_Q5_0_Q8_1_MMVQ 2
  1676. #define VDR_Q5_0_Q8_1_MMQ 4
  1677. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1678. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1679. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1680. int sumi = 0;
  1681. #pragma unroll
  1682. for (int i = 0; i < vdr; ++i) {
  1683. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1684. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1685. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1686. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1687. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1688. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1689. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1690. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1691. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1692. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1693. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1694. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1695. }
  1696. const float2 ds8f = __half22float2(ds8);
  1697. // second part effectively subtracts 16 from each quant value
  1698. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1699. #else
  1700. bad_arch();
  1701. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1702. }
  1703. #define VDR_Q5_1_Q8_1_MMVQ 2
  1704. #define VDR_Q5_1_Q8_1_MMQ 4
  1705. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1706. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1707. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1708. int sumi = 0;
  1709. #pragma unroll
  1710. for (int i = 0; i < vdr; ++i) {
  1711. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1712. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1713. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1714. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1715. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1716. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1717. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1718. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1719. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1720. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1721. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1722. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1723. }
  1724. #ifdef GGML_CUDA_F16
  1725. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1726. const float d5d8 = tmp.x;
  1727. const float m5s8 = tmp.y;
  1728. #else
  1729. const float2 dm5f = __half22float2(dm5);
  1730. const float2 ds8f = __half22float2(ds8);
  1731. const float d5d8 = dm5f.x * ds8f.x;
  1732. const float m5s8 = dm5f.y * ds8f.y;
  1733. #endif // GGML_CUDA_F16
  1734. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1735. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1736. #else
  1737. bad_arch();
  1738. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1739. }
  1740. #define VDR_Q8_0_Q8_1_MMVQ 2
  1741. #define VDR_Q8_0_Q8_1_MMQ 8
  1742. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1743. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1744. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1745. int sumi = 0;
  1746. #pragma unroll
  1747. for (int i = 0; i < vdr; ++i) {
  1748. // SIMD dot product of quantized values
  1749. sumi = __dp4a(v[i], u[i], sumi);
  1750. }
  1751. return d8_0*d8_1 * sumi;
  1752. #else
  1753. bad_arch();
  1754. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1755. }
  1756. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1757. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1758. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1759. int sumi = 0;
  1760. #pragma unroll
  1761. for (int i = 0; i < vdr; ++i) {
  1762. // SIMD dot product of quantized values
  1763. sumi = __dp4a(v[i], u[i], sumi);
  1764. }
  1765. #ifdef GGML_CUDA_F16
  1766. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1767. const float d8d8 = tmp.x;
  1768. const float m8s8 = tmp.y;
  1769. #else
  1770. const float2 dm8f = __half22float2(dm8);
  1771. const float2 ds8f = __half22float2(ds8);
  1772. const float d8d8 = dm8f.x * ds8f.x;
  1773. const float m8s8 = dm8f.y * ds8f.y;
  1774. #endif // GGML_CUDA_F16
  1775. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1776. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1777. #else
  1778. bad_arch();
  1779. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1780. }
  1781. #define VDR_Q2_K_Q8_1_MMVQ 1
  1782. #define VDR_Q2_K_Q8_1_MMQ 2
  1783. // contiguous v/x values
  1784. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1785. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1786. const half2 & dm2, const float * __restrict__ d8) {
  1787. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1788. float sumf_d = 0.0f;
  1789. float sumf_m = 0.0f;
  1790. #pragma unroll
  1791. for (int i = 0; i < QR2_K; ++i) {
  1792. const int sc = scales[2*i];
  1793. const int vi = (v >> (2*i)) & 0x03030303;
  1794. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1795. // fill int with 4x m
  1796. int m = sc >> 4;
  1797. m |= m << 8;
  1798. m |= m << 16;
  1799. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1800. }
  1801. const float2 dm2f = __half22float2(dm2);
  1802. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1803. #else
  1804. bad_arch();
  1805. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1806. }
  1807. // contiguous u/y values
  1808. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1809. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1810. const half2 & dm2, const float & d8) {
  1811. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1812. int sumi_d = 0;
  1813. int sumi_m = 0;
  1814. #pragma unroll
  1815. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1816. int sumi_d_sc = 0;
  1817. const int sc = scales[i0 / (QI8_1/2)];
  1818. // fill int with 4x m
  1819. int m = sc >> 4;
  1820. m |= m << 8;
  1821. m |= m << 16;
  1822. #pragma unroll
  1823. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1824. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1825. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1826. }
  1827. sumi_d += sumi_d_sc * (sc & 0xF);
  1828. }
  1829. const float2 dm2f = __half22float2(dm2);
  1830. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1831. #else
  1832. bad_arch();
  1833. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1834. }
  1835. #define VDR_Q3_K_Q8_1_MMVQ 1
  1836. #define VDR_Q3_K_Q8_1_MMQ 2
  1837. // contiguous v/x values
  1838. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1839. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1840. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1841. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1842. float sumf = 0.0f;
  1843. #pragma unroll
  1844. for (int i = 0; i < QR3_K; ++i) {
  1845. const int isc = scale_offset + 2*i;
  1846. const int isc_low = isc % (QK_K/32);
  1847. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1848. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1849. const int isc_high = isc % (QK_K/64);
  1850. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1851. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1852. const int sc = (sc_low | sc_high) - 32;
  1853. const int vil = (vl >> (2*i)) & 0x03030303;
  1854. const int vih = ((vh >> i) << 2) & 0x04040404;
  1855. const int vi = __vsubss4(vil, vih);
  1856. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1857. }
  1858. return d3 * sumf;
  1859. #else
  1860. bad_arch();
  1861. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1862. }
  1863. // contiguous u/y values
  1864. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1865. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1866. const float & d3, const float & d8) {
  1867. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1868. int sumi = 0;
  1869. #pragma unroll
  1870. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1871. int sumi_sc = 0;
  1872. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1873. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1874. }
  1875. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1876. }
  1877. return d3*d8 * sumi;
  1878. #else
  1879. bad_arch();
  1880. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1881. }
  1882. #define VDR_Q4_K_Q8_1_MMVQ 2
  1883. #define VDR_Q4_K_Q8_1_MMQ 8
  1884. // contiguous v/x values
  1885. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1886. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1887. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1888. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1889. float sumf_d = 0.0f;
  1890. float sumf_m = 0.0f;
  1891. #pragma unroll
  1892. for (int i = 0; i < QR4_K; ++i) {
  1893. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1894. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1895. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1896. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1897. sumf_d += d8[i] * (dot1 * sc[i]);
  1898. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1899. }
  1900. const float2 dm4f = __half22float2(dm4);
  1901. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1902. #else
  1903. bad_arch();
  1904. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1905. }
  1906. // contiguous u/y values
  1907. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1908. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1909. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1910. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1911. float sumf_d = 0.0f;
  1912. float sumf_m = 0.0f;
  1913. #pragma unroll
  1914. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1915. int sumi_d = 0;
  1916. #pragma unroll
  1917. for (int j = 0; j < QI8_1; ++j) {
  1918. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1919. }
  1920. const float2 ds8f = __half22float2(ds8[i]);
  1921. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1922. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1923. }
  1924. const float2 dm4f = __half22float2(dm4);
  1925. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1926. #else
  1927. bad_arch();
  1928. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1929. }
  1930. #define VDR_Q5_K_Q8_1_MMVQ 2
  1931. #define VDR_Q5_K_Q8_1_MMQ 8
  1932. // contiguous v/x values
  1933. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1934. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1935. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1936. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1937. float sumf_d = 0.0f;
  1938. float sumf_m = 0.0f;
  1939. #pragma unroll
  1940. for (int i = 0; i < QR5_K; ++i) {
  1941. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1942. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1943. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1944. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1945. const int v0i = vl0i | vh0i;
  1946. const int v1i = vl1i | vh1i;
  1947. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1948. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1949. sumf_d += d8[i] * (dot1 * sc[i]);
  1950. sumf_m += d8[i] * (dot2 * m[i]);
  1951. }
  1952. const float2 dm5f = __half22float2(dm5);
  1953. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1954. #else
  1955. bad_arch();
  1956. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1957. }
  1958. // contiguous u/y values
  1959. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1960. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1961. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1962. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1963. float sumf_d = 0.0f;
  1964. float sumf_m = 0.0f;
  1965. #pragma unroll
  1966. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1967. int sumi_d = 0;
  1968. #pragma unroll
  1969. for (int j = 0; j < QI8_1; ++j) {
  1970. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1971. }
  1972. const float2 ds8f = __half22float2(ds8[i]);
  1973. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1974. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1975. }
  1976. const float2 dm4f = __half22float2(dm4);
  1977. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1978. #else
  1979. bad_arch();
  1980. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1981. }
  1982. #define VDR_Q6_K_Q8_1_MMVQ 1
  1983. #define VDR_Q6_K_Q8_1_MMQ 8
  1984. // contiguous v/x values
  1985. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1986. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1987. const float & d, const float * __restrict__ d8) {
  1988. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1989. float sumf = 0.0f;
  1990. #pragma unroll
  1991. for (int i = 0; i < QR6_K; ++i) {
  1992. const int sc = scales[4*i];
  1993. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1994. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1995. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1996. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1997. }
  1998. return d*sumf;
  1999. #else
  2000. bad_arch();
  2001. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2002. }
  2003. // contiguous u/y values
  2004. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2005. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2006. const float & d6, const float * __restrict__ d8) {
  2007. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2008. float sumf_d = 0.0f;
  2009. #pragma unroll
  2010. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2011. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2012. #pragma unroll
  2013. for (int i = i0; i < i0 + 2; ++i) {
  2014. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2015. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2016. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2017. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2018. }
  2019. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2020. }
  2021. return d6 * sumf_d;
  2022. #else
  2023. bad_arch();
  2024. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2025. }
  2026. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2027. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2028. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2029. int v[VDR_Q4_0_Q8_1_MMVQ];
  2030. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2031. #pragma unroll
  2032. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2033. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2034. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2035. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2036. }
  2037. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2038. }
  2039. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2040. (void)x_qh; (void)x_sc;
  2041. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2042. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2043. *x_ql = tile_x_qs;
  2044. *x_dm = (half2 *) tile_x_d;
  2045. }
  2046. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2047. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2048. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2049. (void)x_qh; (void)x_sc;
  2050. GGML_CUDA_ASSUME(i_offset >= 0);
  2051. GGML_CUDA_ASSUME(i_offset < nwarps);
  2052. GGML_CUDA_ASSUME(k >= 0);
  2053. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2054. const int kbx = k / QI4_0;
  2055. const int kqsx = k % QI4_0;
  2056. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2057. float * x_dmf = (float *) x_dm;
  2058. #pragma unroll
  2059. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2060. int i = i0 + i_offset;
  2061. if (need_check) {
  2062. i = min(i, i_max);
  2063. }
  2064. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2065. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2066. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2067. }
  2068. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2069. const int kbxd = k % blocks_per_tile_x_row;
  2070. #pragma unroll
  2071. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2072. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2073. if (need_check) {
  2074. i = min(i, i_max);
  2075. }
  2076. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2077. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2078. }
  2079. }
  2080. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2081. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2082. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2083. (void)x_qh; (void)x_sc;
  2084. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2085. const float * x_dmf = (const float *) x_dm;
  2086. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2087. #pragma unroll
  2088. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2089. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2090. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2091. }
  2092. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2093. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2094. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2095. }
  2096. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2097. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2098. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2099. int v[VDR_Q4_1_Q8_1_MMVQ];
  2100. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2101. #pragma unroll
  2102. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2103. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2104. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2105. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2106. }
  2107. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2108. }
  2109. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2110. (void)x_qh; (void)x_sc;
  2111. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2112. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2113. *x_ql = tile_x_qs;
  2114. *x_dm = tile_x_dm;
  2115. }
  2116. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2117. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2118. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2119. (void)x_qh; (void)x_sc;
  2120. GGML_CUDA_ASSUME(i_offset >= 0);
  2121. GGML_CUDA_ASSUME(i_offset < nwarps);
  2122. GGML_CUDA_ASSUME(k >= 0);
  2123. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2124. const int kbx = k / QI4_1;
  2125. const int kqsx = k % QI4_1;
  2126. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2127. #pragma unroll
  2128. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2129. int i = i0 + i_offset;
  2130. if (need_check) {
  2131. i = min(i, i_max);
  2132. }
  2133. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2134. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2135. }
  2136. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2137. const int kbxd = k % blocks_per_tile_x_row;
  2138. #pragma unroll
  2139. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2140. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2141. if (need_check) {
  2142. i = min(i, i_max);
  2143. }
  2144. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2145. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2146. }
  2147. }
  2148. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2149. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2150. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2151. (void)x_qh; (void)x_sc;
  2152. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2153. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2154. #pragma unroll
  2155. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2156. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2157. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2158. }
  2159. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2160. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2161. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2162. }
  2163. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2164. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2165. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2166. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2167. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2168. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2169. #pragma unroll
  2170. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2171. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2172. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2173. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2174. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2175. }
  2176. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2177. }
  2178. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2179. (void)x_qh; (void)x_sc;
  2180. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2181. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2182. *x_ql = tile_x_ql;
  2183. *x_dm = (half2 *) tile_x_d;
  2184. }
  2185. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2186. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2187. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2188. (void)x_qh; (void)x_sc;
  2189. GGML_CUDA_ASSUME(i_offset >= 0);
  2190. GGML_CUDA_ASSUME(i_offset < nwarps);
  2191. GGML_CUDA_ASSUME(k >= 0);
  2192. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2193. const int kbx = k / QI5_0;
  2194. const int kqsx = k % QI5_0;
  2195. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2196. #pragma unroll
  2197. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2198. int i = i0 + i_offset;
  2199. if (need_check) {
  2200. i = min(i, i_max);
  2201. }
  2202. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2203. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2204. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2205. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2206. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2207. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2208. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2209. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2210. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2211. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2212. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2213. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2214. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2215. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2216. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2217. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2218. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2219. }
  2220. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2221. const int kbxd = k % blocks_per_tile_x_row;
  2222. float * x_dmf = (float *) x_dm;
  2223. #pragma unroll
  2224. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2225. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2226. if (need_check) {
  2227. i = min(i, i_max);
  2228. }
  2229. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2230. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2231. }
  2232. }
  2233. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2234. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2235. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2236. (void)x_qh; (void)x_sc;
  2237. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2238. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2239. const float * x_dmf = (const float *) x_dm;
  2240. const float * y_df = (const float *) y_ds;
  2241. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2242. #pragma unroll
  2243. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2244. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2245. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2246. }
  2247. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2248. (&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)]);
  2249. }
  2250. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2251. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2252. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2253. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2254. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2255. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2256. #pragma unroll
  2257. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2258. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2259. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2260. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2261. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2262. }
  2263. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2264. }
  2265. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2266. (void)x_qh; (void)x_sc;
  2267. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2268. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2269. *x_ql = tile_x_ql;
  2270. *x_dm = tile_x_dm;
  2271. }
  2272. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2273. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2274. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2275. (void)x_qh; (void)x_sc;
  2276. GGML_CUDA_ASSUME(i_offset >= 0);
  2277. GGML_CUDA_ASSUME(i_offset < nwarps);
  2278. GGML_CUDA_ASSUME(k >= 0);
  2279. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2280. const int kbx = k / QI5_1;
  2281. const int kqsx = k % QI5_1;
  2282. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2283. #pragma unroll
  2284. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2285. int i = i0 + i_offset;
  2286. if (need_check) {
  2287. i = min(i, i_max);
  2288. }
  2289. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2290. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2291. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2292. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2293. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2294. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2295. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2296. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2297. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2298. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2299. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2300. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2301. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2302. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2303. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2304. }
  2305. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2306. const int kbxd = k % blocks_per_tile_x_row;
  2307. #pragma unroll
  2308. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2309. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2310. if (need_check) {
  2311. i = min(i, i_max);
  2312. }
  2313. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2314. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2315. }
  2316. }
  2317. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2318. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2319. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2320. (void)x_qh; (void)x_sc;
  2321. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2322. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2323. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2324. #pragma unroll
  2325. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2326. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2327. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2328. }
  2329. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2330. (&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)]);
  2331. }
  2332. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2333. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2334. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2335. int v[VDR_Q8_0_Q8_1_MMVQ];
  2336. int u[VDR_Q8_0_Q8_1_MMVQ];
  2337. #pragma unroll
  2338. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2339. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2340. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2341. }
  2342. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2343. }
  2344. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2345. (void)x_qh; (void)x_sc;
  2346. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2347. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2348. *x_ql = tile_x_qs;
  2349. *x_dm = (half2 *) tile_x_d;
  2350. }
  2351. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2352. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2353. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2354. (void)x_qh; (void)x_sc;
  2355. GGML_CUDA_ASSUME(i_offset >= 0);
  2356. GGML_CUDA_ASSUME(i_offset < nwarps);
  2357. GGML_CUDA_ASSUME(k >= 0);
  2358. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2359. const int kbx = k / QI8_0;
  2360. const int kqsx = k % QI8_0;
  2361. float * x_dmf = (float *) x_dm;
  2362. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2363. #pragma unroll
  2364. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2365. int i = i0 + i_offset;
  2366. if (need_check) {
  2367. i = min(i, i_max);
  2368. }
  2369. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2370. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2371. }
  2372. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2373. const int kbxd = k % blocks_per_tile_x_row;
  2374. #pragma unroll
  2375. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2376. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2377. if (need_check) {
  2378. i = min(i, i_max);
  2379. }
  2380. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2381. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2382. }
  2383. }
  2384. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2385. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2386. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2387. (void)x_qh; (void)x_sc;
  2388. const float * x_dmf = (const float *) x_dm;
  2389. const float * y_df = (const float *) y_ds;
  2390. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2391. (&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],
  2392. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2393. }
  2394. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2395. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2396. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2397. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2398. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2399. const uint8_t * scales = bq2_K->scales + scale_offset;
  2400. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2401. int u[QR2_K];
  2402. float d8[QR2_K];
  2403. #pragma unroll
  2404. for (int i = 0; i < QR2_K; ++ i) {
  2405. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2406. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2407. }
  2408. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2409. }
  2410. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2411. (void)x_qh;
  2412. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2413. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2414. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2415. *x_ql = tile_x_ql;
  2416. *x_dm = tile_x_dm;
  2417. *x_sc = tile_x_sc;
  2418. }
  2419. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2420. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2421. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2422. (void)x_qh;
  2423. GGML_CUDA_ASSUME(i_offset >= 0);
  2424. GGML_CUDA_ASSUME(i_offset < nwarps);
  2425. GGML_CUDA_ASSUME(k >= 0);
  2426. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2427. const int kbx = k / QI2_K;
  2428. const int kqsx = k % QI2_K;
  2429. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2430. #pragma unroll
  2431. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2432. int i = i0 + i_offset;
  2433. if (need_check) {
  2434. i = min(i, i_max);
  2435. }
  2436. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2437. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2438. }
  2439. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2440. const int kbxd = k % blocks_per_tile_x_row;
  2441. #pragma unroll
  2442. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2443. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2444. if (need_check) {
  2445. i = min(i, i_max);
  2446. }
  2447. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2448. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2449. }
  2450. #pragma unroll
  2451. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2452. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2453. if (need_check) {
  2454. i = min(i, i_max);
  2455. }
  2456. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2457. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2458. }
  2459. }
  2460. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2461. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2462. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2463. (void)x_qh;
  2464. const int kbx = k / QI2_K;
  2465. const int ky = (k % QI2_K) * QR2_K;
  2466. const float * y_df = (const float *) y_ds;
  2467. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2468. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2469. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2470. #pragma unroll
  2471. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2472. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2473. }
  2474. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2475. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2476. 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]);
  2477. }
  2478. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2479. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2480. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2481. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2482. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2483. const float d = bq3_K->d;
  2484. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2485. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2486. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2487. int u[QR3_K];
  2488. float d8[QR3_K];
  2489. #pragma unroll
  2490. for (int i = 0; i < QR3_K; ++i) {
  2491. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2492. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2493. }
  2494. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2495. }
  2496. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2497. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2498. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2499. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2500. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2501. *x_ql = tile_x_ql;
  2502. *x_dm = tile_x_dm;
  2503. *x_qh = tile_x_qh;
  2504. *x_sc = tile_x_sc;
  2505. }
  2506. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2507. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2508. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2509. GGML_CUDA_ASSUME(i_offset >= 0);
  2510. GGML_CUDA_ASSUME(i_offset < nwarps);
  2511. GGML_CUDA_ASSUME(k >= 0);
  2512. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2513. const int kbx = k / QI3_K;
  2514. const int kqsx = k % QI3_K;
  2515. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2516. #pragma unroll
  2517. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2518. int i = i0 + i_offset;
  2519. if (need_check) {
  2520. i = min(i, i_max);
  2521. }
  2522. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2523. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2524. }
  2525. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2526. const int kbxd = k % blocks_per_tile_x_row;
  2527. float * x_dmf = (float *) x_dm;
  2528. #pragma unroll
  2529. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2530. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2531. if (need_check) {
  2532. i = min(i, i_max);
  2533. }
  2534. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2535. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2536. }
  2537. #pragma unroll
  2538. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2539. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2540. if (need_check) {
  2541. i = min(i, i_max);
  2542. }
  2543. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2544. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2545. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2546. }
  2547. #pragma unroll
  2548. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2549. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2550. if (need_check) {
  2551. i = min(i, i_max);
  2552. }
  2553. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2554. const int ksc = k % (QI3_K/4);
  2555. const int ksc_low = ksc % (QI3_K/8);
  2556. const int shift_low = 4 * (ksc / (QI3_K/8));
  2557. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2558. const int ksc_high = QI3_K/8;
  2559. const int shift_high = 2 * ksc;
  2560. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2561. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2562. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2563. }
  2564. }
  2565. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2566. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2567. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2568. const int kbx = k / QI3_K;
  2569. const int ky = (k % QI3_K) * QR3_K;
  2570. const float * x_dmf = (const float *) x_dm;
  2571. const float * y_df = (const float *) y_ds;
  2572. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2573. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2574. #pragma unroll
  2575. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2576. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2577. const int shift = 2 * ((ky % 32) / 8);
  2578. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2579. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2580. const int vlh = (vh << 2) & 0x04040404;
  2581. v[l] = __vsubss4(vll, vlh);
  2582. }
  2583. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2584. 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]);
  2585. }
  2586. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2587. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2588. #ifndef GGML_QKK_64
  2589. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2590. int v[2];
  2591. int u[2*QR4_K];
  2592. float d8[QR4_K];
  2593. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2594. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2595. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2596. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2597. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2598. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2599. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2600. v[0] = q4[0];
  2601. v[1] = q4[4];
  2602. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2603. uint16_t aux[2];
  2604. const int j = bq8_offset/2;
  2605. if (j < 2) {
  2606. aux[0] = scales[j+0] & 0x3f3f;
  2607. aux[1] = scales[j+2] & 0x3f3f;
  2608. } else {
  2609. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2610. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2611. }
  2612. const uint8_t * sc = (const uint8_t *)aux;
  2613. const uint8_t * m = sc + 2;
  2614. for (int i = 0; i < QR4_K; ++i) {
  2615. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2616. d8[i] = __low2half(bq8i->ds);
  2617. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2618. u[2*i+0] = q8[0];
  2619. u[2*i+1] = q8[4];
  2620. }
  2621. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2622. #else
  2623. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2624. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2625. float sumf_d = 0.0f;
  2626. float sumf_m = 0.0f;
  2627. uint16_t aux16[2];
  2628. const uint8_t * s = (const uint8_t *)aux16;
  2629. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2630. aux16[0] = a[0] & 0x0f0f;
  2631. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2632. const float dall = bq4_K->dm[0];
  2633. const float dmin = bq4_K->dm[1];
  2634. const float d8_1 = __low2float(bq8_1[0].ds);
  2635. const float d8_2 = __low2float(bq8_1[1].ds);
  2636. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2637. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2638. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2639. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2640. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2641. const int v1 = q4[0];
  2642. const int v2 = q4[4];
  2643. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2644. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2645. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2646. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2647. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2648. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2649. return dall * sumf_d - dmin * sumf_m;
  2650. #else
  2651. bad_arch();
  2652. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2653. #endif
  2654. }
  2655. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2656. (void)x_qh;
  2657. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2658. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2659. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2660. *x_ql = tile_x_ql;
  2661. *x_dm = tile_x_dm;
  2662. *x_sc = tile_x_sc;
  2663. }
  2664. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2665. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2666. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2667. (void)x_qh;
  2668. GGML_CUDA_ASSUME(i_offset >= 0);
  2669. GGML_CUDA_ASSUME(i_offset < nwarps);
  2670. GGML_CUDA_ASSUME(k >= 0);
  2671. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2672. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2673. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2674. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2675. #pragma unroll
  2676. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2677. int i = i0 + i_offset;
  2678. if (need_check) {
  2679. i = min(i, i_max);
  2680. }
  2681. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2682. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2683. }
  2684. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2685. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2686. #pragma unroll
  2687. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2688. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2689. if (need_check) {
  2690. i = min(i, i_max);
  2691. }
  2692. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2693. #if QK_K == 256
  2694. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2695. #else
  2696. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2697. #endif
  2698. }
  2699. #pragma unroll
  2700. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2701. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2702. if (need_check) {
  2703. i = min(i, i_max);
  2704. }
  2705. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2706. const int * scales = (const int *) bxi->scales;
  2707. const int ksc = k % (WARP_SIZE/8);
  2708. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2709. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2710. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2711. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2712. }
  2713. }
  2714. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2715. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2716. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2717. (void)x_qh;
  2718. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2719. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2720. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2721. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2722. }
  2723. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2724. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2725. #ifndef GGML_QKK_64
  2726. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2727. int vl[2];
  2728. int vh[2];
  2729. int u[2*QR5_K];
  2730. float d8[QR5_K];
  2731. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2732. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2733. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2734. vl[0] = ql[0];
  2735. vl[1] = ql[4];
  2736. vh[0] = qh[0] >> bq8_offset;
  2737. vh[1] = qh[4] >> bq8_offset;
  2738. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2739. uint16_t aux[2];
  2740. const int j = bq8_offset/2;
  2741. if (j < 2) {
  2742. aux[0] = scales[j+0] & 0x3f3f;
  2743. aux[1] = scales[j+2] & 0x3f3f;
  2744. } else {
  2745. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2746. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2747. }
  2748. const uint8_t * sc = (const uint8_t *)aux;
  2749. const uint8_t * m = sc + 2;
  2750. #pragma unroll
  2751. for (int i = 0; i < QR5_K; ++i) {
  2752. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2753. d8[i] = __low2float(bq8i->ds);
  2754. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2755. u[2*i+0] = q8[0];
  2756. u[2*i+1] = q8[4];
  2757. }
  2758. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2759. #else
  2760. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2761. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2762. const int8_t * s = bq5_K->scales;
  2763. const float d = bq5_K->d;
  2764. const float d8_1 = __low2half(bq8_1[0].ds);
  2765. const float d8_2 = __low2half(bq8_1[1].ds);
  2766. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2767. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2768. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2769. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2770. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2771. const int vl1 = ql[0];
  2772. const int vl2 = ql[4];
  2773. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2774. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2775. const int in = step%8; // 0, 4, 0, 4
  2776. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2777. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2778. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2779. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2780. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2781. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2782. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2783. return d * sumf_d;
  2784. #else
  2785. bad_arch();
  2786. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2787. #endif
  2788. }
  2789. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2790. (void)x_qh;
  2791. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2792. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2793. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2794. *x_ql = tile_x_ql;
  2795. *x_dm = tile_x_dm;
  2796. *x_sc = tile_x_sc;
  2797. }
  2798. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2799. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2800. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2801. (void)x_qh;
  2802. GGML_CUDA_ASSUME(i_offset >= 0);
  2803. GGML_CUDA_ASSUME(i_offset < nwarps);
  2804. GGML_CUDA_ASSUME(k >= 0);
  2805. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2806. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2807. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2808. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2809. #pragma unroll
  2810. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2811. int i = i0 + i_offset;
  2812. if (need_check) {
  2813. i = min(i, i_max);
  2814. }
  2815. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2816. const int ky = QR5_K*kqsx;
  2817. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2818. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2819. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2820. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2821. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2822. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2823. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2824. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2825. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2826. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2827. }
  2828. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2829. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2830. #pragma unroll
  2831. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2832. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2833. if (need_check) {
  2834. i = min(i, i_max);
  2835. }
  2836. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2837. #if QK_K == 256
  2838. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2839. #endif
  2840. }
  2841. #pragma unroll
  2842. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2843. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2844. if (need_check) {
  2845. i = min(i, i_max);
  2846. }
  2847. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2848. const int * scales = (const int *) bxi->scales;
  2849. const int ksc = k % (WARP_SIZE/8);
  2850. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2851. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2852. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2853. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2854. }
  2855. }
  2856. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2857. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2858. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2859. (void)x_qh;
  2860. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2861. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2862. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2863. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2864. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2865. }
  2866. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2867. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2868. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2869. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2870. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2871. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2872. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2873. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2874. const int8_t * scales = bq6_K->scales + scale_offset;
  2875. int u[QR6_K];
  2876. float d8[QR6_K];
  2877. #pragma unroll
  2878. for (int i = 0; i < QR6_K; ++i) {
  2879. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2880. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2881. }
  2882. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2883. }
  2884. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2885. (void)x_qh;
  2886. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2887. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2888. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2889. *x_ql = tile_x_ql;
  2890. *x_dm = tile_x_dm;
  2891. *x_sc = tile_x_sc;
  2892. }
  2893. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2894. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2895. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2896. (void)x_qh;
  2897. GGML_CUDA_ASSUME(i_offset >= 0);
  2898. GGML_CUDA_ASSUME(i_offset < nwarps);
  2899. GGML_CUDA_ASSUME(k >= 0);
  2900. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2901. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2902. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2903. const block_q6_K * bx0 = (const block_q6_K *) vx;
  2904. #pragma unroll
  2905. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2906. int i = i0 + i_offset;
  2907. if (need_check) {
  2908. i = min(i, i_max);
  2909. }
  2910. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2911. const int ky = QR6_K*kqsx;
  2912. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2913. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2914. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2915. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2916. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2917. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2918. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2919. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2920. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2921. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2922. }
  2923. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2924. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2925. float * x_dmf = (float *) x_dm;
  2926. #pragma unroll
  2927. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2928. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2929. if (need_check) {
  2930. i = min(i, i_max);
  2931. }
  2932. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2933. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2934. }
  2935. #pragma unroll
  2936. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2937. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2938. if (need_check) {
  2939. i = min(i, i_max);
  2940. }
  2941. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2942. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2943. }
  2944. }
  2945. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2946. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2947. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2948. (void)x_qh;
  2949. const float * x_dmf = (const float *) x_dm;
  2950. const float * y_df = (const float *) y_ds;
  2951. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2952. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2953. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2954. 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]);
  2955. }
  2956. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2957. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2958. static __device__ __forceinline__ void mul_mat_q(
  2959. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2960. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2961. const block_q_t * x = (const block_q_t *) vx;
  2962. const block_q8_1 * y = (const block_q8_1 *) vy;
  2963. const int blocks_per_row_x = ncols_x / qk;
  2964. const int blocks_per_col_y = nrows_y / QK8_1;
  2965. const int blocks_per_warp = WARP_SIZE / qi;
  2966. const int & ncols_dst = ncols_y;
  2967. const int row_dst_0 = blockIdx.x*mmq_y;
  2968. const int & row_x_0 = row_dst_0;
  2969. const int col_dst_0 = blockIdx.y*mmq_x;
  2970. const int & col_y_0 = col_dst_0;
  2971. int * tile_x_ql = nullptr;
  2972. half2 * tile_x_dm = nullptr;
  2973. int * tile_x_qh = nullptr;
  2974. int * tile_x_sc = nullptr;
  2975. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2976. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2977. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2978. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  2979. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2980. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2981. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2982. #pragma unroll
  2983. for (int ir = 0; ir < qr; ++ir) {
  2984. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2985. const int kbxd = kqs / QI8_1;
  2986. #pragma unroll
  2987. for (int i = 0; i < mmq_x; i += nwarps) {
  2988. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2989. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2990. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2991. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2992. }
  2993. #pragma unroll
  2994. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2995. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2996. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2997. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2998. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2999. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3000. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3001. if (need_sum) {
  3002. *dsi_dst = *dsi_src;
  3003. } else {
  3004. float * dfi_dst = (float *) dsi_dst;
  3005. *dfi_dst = __low2half(*dsi_src);
  3006. }
  3007. }
  3008. __syncthreads();
  3009. // #pragma unroll // unrolling this loop causes too much register pressure
  3010. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3011. #pragma unroll
  3012. for (int j = 0; j < mmq_x; j += nwarps) {
  3013. #pragma unroll
  3014. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3015. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3016. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3017. threadIdx.x + i, threadIdx.y + j, k);
  3018. }
  3019. }
  3020. }
  3021. __syncthreads();
  3022. }
  3023. }
  3024. #pragma unroll
  3025. for (int j = 0; j < mmq_x; j += nwarps) {
  3026. const int col_dst = col_dst_0 + j + threadIdx.y;
  3027. if (col_dst >= ncols_dst) {
  3028. return;
  3029. }
  3030. #pragma unroll
  3031. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3032. const int row_dst = row_dst_0 + threadIdx.x + i;
  3033. if (row_dst >= nrows_dst) {
  3034. continue;
  3035. }
  3036. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3037. }
  3038. }
  3039. }
  3040. #define MMQ_X_Q4_0_RDNA2 64
  3041. #define MMQ_Y_Q4_0_RDNA2 128
  3042. #define NWARPS_Q4_0_RDNA2 8
  3043. #define MMQ_X_Q4_0_RDNA1 64
  3044. #define MMQ_Y_Q4_0_RDNA1 64
  3045. #define NWARPS_Q4_0_RDNA1 8
  3046. #if defined(CUDA_USE_TENSOR_CORES)
  3047. #define MMQ_X_Q4_0_AMPERE 4
  3048. #define MMQ_Y_Q4_0_AMPERE 32
  3049. #define NWARPS_Q4_0_AMPERE 4
  3050. #else
  3051. #define MMQ_X_Q4_0_AMPERE 64
  3052. #define MMQ_Y_Q4_0_AMPERE 128
  3053. #define NWARPS_Q4_0_AMPERE 4
  3054. #endif
  3055. #define MMQ_X_Q4_0_PASCAL 64
  3056. #define MMQ_Y_Q4_0_PASCAL 64
  3057. #define NWARPS_Q4_0_PASCAL 8
  3058. template <bool need_check> static __global__ void
  3059. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3060. #if defined(RDNA3) || defined(RDNA2)
  3061. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3062. #endif // defined(RDNA3) || defined(RDNA2)
  3063. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3064. mul_mat_q4_0(
  3065. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3066. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3067. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3068. #if defined(RDNA3) || defined(RDNA2)
  3069. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3070. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3071. const int nwarps = NWARPS_Q4_0_RDNA2;
  3072. #else
  3073. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3074. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3075. const int nwarps = NWARPS_Q4_0_RDNA1;
  3076. #endif // defined(RDNA3) || defined(RDNA2)
  3077. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3078. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3079. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3080. #elif __CUDA_ARCH__ >= CC_VOLTA
  3081. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3082. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3083. const int nwarps = NWARPS_Q4_0_AMPERE;
  3084. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3085. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3086. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3087. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3088. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3089. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3090. const int nwarps = NWARPS_Q4_0_PASCAL;
  3091. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3092. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3093. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3094. #else
  3095. (void) vec_dot_q4_0_q8_1_mul_mat;
  3096. bad_arch();
  3097. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3098. }
  3099. #define MMQ_X_Q4_1_RDNA2 64
  3100. #define MMQ_Y_Q4_1_RDNA2 128
  3101. #define NWARPS_Q4_1_RDNA2 8
  3102. #define MMQ_X_Q4_1_RDNA1 64
  3103. #define MMQ_Y_Q4_1_RDNA1 64
  3104. #define NWARPS_Q4_1_RDNA1 8
  3105. #if defined(CUDA_USE_TENSOR_CORES)
  3106. #define MMQ_X_Q4_1_AMPERE 4
  3107. #define MMQ_Y_Q4_1_AMPERE 32
  3108. #define NWARPS_Q4_1_AMPERE 4
  3109. #else
  3110. #define MMQ_X_Q4_1_AMPERE 64
  3111. #define MMQ_Y_Q4_1_AMPERE 128
  3112. #define NWARPS_Q4_1_AMPERE 4
  3113. #endif
  3114. #define MMQ_X_Q4_1_PASCAL 64
  3115. #define MMQ_Y_Q4_1_PASCAL 64
  3116. #define NWARPS_Q4_1_PASCAL 8
  3117. template <bool need_check> static __global__ void
  3118. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3119. #if defined(RDNA3) || defined(RDNA2)
  3120. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3121. #endif // defined(RDNA3) || defined(RDNA2)
  3122. #elif __CUDA_ARCH__ < CC_VOLTA
  3123. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3124. #endif // __CUDA_ARCH__ < CC_VOLTA
  3125. mul_mat_q4_1(
  3126. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3127. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3128. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3129. #if defined(RDNA3) || defined(RDNA2)
  3130. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3131. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3132. const int nwarps = NWARPS_Q4_1_RDNA2;
  3133. #else
  3134. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3135. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3136. const int nwarps = NWARPS_Q4_1_RDNA1;
  3137. #endif // defined(RDNA3) || defined(RDNA2)
  3138. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3139. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3140. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3141. #elif __CUDA_ARCH__ >= CC_VOLTA
  3142. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3143. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3144. const int nwarps = NWARPS_Q4_1_AMPERE;
  3145. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3146. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3147. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3148. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3149. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3150. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3151. const int nwarps = NWARPS_Q4_1_PASCAL;
  3152. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3153. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3154. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3155. #else
  3156. (void) vec_dot_q4_1_q8_1_mul_mat;
  3157. bad_arch();
  3158. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3159. }
  3160. #define MMQ_X_Q5_0_RDNA2 64
  3161. #define MMQ_Y_Q5_0_RDNA2 128
  3162. #define NWARPS_Q5_0_RDNA2 8
  3163. #define MMQ_X_Q5_0_RDNA1 64
  3164. #define MMQ_Y_Q5_0_RDNA1 64
  3165. #define NWARPS_Q5_0_RDNA1 8
  3166. #if defined(CUDA_USE_TENSOR_CORES)
  3167. #define MMQ_X_Q5_0_AMPERE 4
  3168. #define MMQ_Y_Q5_0_AMPERE 32
  3169. #define NWARPS_Q5_0_AMPERE 4
  3170. #else
  3171. #define MMQ_X_Q5_0_AMPERE 128
  3172. #define MMQ_Y_Q5_0_AMPERE 64
  3173. #define NWARPS_Q5_0_AMPERE 4
  3174. #endif
  3175. #define MMQ_X_Q5_0_PASCAL 64
  3176. #define MMQ_Y_Q5_0_PASCAL 64
  3177. #define NWARPS_Q5_0_PASCAL 8
  3178. template <bool need_check> static __global__ void
  3179. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3180. #if defined(RDNA3) || defined(RDNA2)
  3181. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3182. #endif // defined(RDNA3) || defined(RDNA2)
  3183. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3184. mul_mat_q5_0(
  3185. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3186. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3187. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3188. #if defined(RDNA3) || defined(RDNA2)
  3189. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3190. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3191. const int nwarps = NWARPS_Q5_0_RDNA2;
  3192. #else
  3193. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3194. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3195. const int nwarps = NWARPS_Q5_0_RDNA1;
  3196. #endif // defined(RDNA3) || defined(RDNA2)
  3197. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3198. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3199. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3200. #elif __CUDA_ARCH__ >= CC_VOLTA
  3201. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3202. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3203. const int nwarps = NWARPS_Q5_0_AMPERE;
  3204. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3205. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3206. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3207. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3208. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3209. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3210. const int nwarps = NWARPS_Q5_0_PASCAL;
  3211. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3212. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3213. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3214. #else
  3215. (void) vec_dot_q5_0_q8_1_mul_mat;
  3216. bad_arch();
  3217. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3218. }
  3219. #define MMQ_X_Q5_1_RDNA2 64
  3220. #define MMQ_Y_Q5_1_RDNA2 128
  3221. #define NWARPS_Q5_1_RDNA2 8
  3222. #define MMQ_X_Q5_1_RDNA1 64
  3223. #define MMQ_Y_Q5_1_RDNA1 64
  3224. #define NWARPS_Q5_1_RDNA1 8
  3225. #if defined(CUDA_USE_TENSOR_CORES)
  3226. #define MMQ_X_Q5_1_AMPERE 4
  3227. #define MMQ_Y_Q5_1_AMPERE 32
  3228. #define NWARPS_Q5_1_AMPERE 4
  3229. #else
  3230. #define MMQ_X_Q5_1_AMPERE 128
  3231. #define MMQ_Y_Q5_1_AMPERE 64
  3232. #define NWARPS_Q5_1_AMPERE 4
  3233. #endif
  3234. #define MMQ_X_Q5_1_PASCAL 64
  3235. #define MMQ_Y_Q5_1_PASCAL 64
  3236. #define NWARPS_Q5_1_PASCAL 8
  3237. template <bool need_check> static __global__ void
  3238. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3239. #if defined(RDNA3) || defined(RDNA2)
  3240. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3241. #endif // defined(RDNA3) || defined(RDNA2)
  3242. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3243. mul_mat_q5_1(
  3244. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3245. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3246. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3247. #if defined(RDNA3) || defined(RDNA2)
  3248. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3249. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3250. const int nwarps = NWARPS_Q5_1_RDNA2;
  3251. #else
  3252. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3253. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3254. const int nwarps = NWARPS_Q5_1_RDNA1;
  3255. #endif // defined(RDNA3) || defined(RDNA2)
  3256. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3257. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3258. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3259. #elif __CUDA_ARCH__ >= CC_VOLTA
  3260. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3261. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3262. const int nwarps = NWARPS_Q5_1_AMPERE;
  3263. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3264. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3265. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3266. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3267. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3268. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3269. const int nwarps = NWARPS_Q5_1_PASCAL;
  3270. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3271. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3272. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3273. #else
  3274. (void) vec_dot_q5_1_q8_1_mul_mat;
  3275. bad_arch();
  3276. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3277. }
  3278. #define MMQ_X_Q8_0_RDNA2 64
  3279. #define MMQ_Y_Q8_0_RDNA2 128
  3280. #define NWARPS_Q8_0_RDNA2 8
  3281. #define MMQ_X_Q8_0_RDNA1 64
  3282. #define MMQ_Y_Q8_0_RDNA1 64
  3283. #define NWARPS_Q8_0_RDNA1 8
  3284. #if defined(CUDA_USE_TENSOR_CORES)
  3285. #define MMQ_X_Q8_0_AMPERE 4
  3286. #define MMQ_Y_Q8_0_AMPERE 32
  3287. #define NWARPS_Q8_0_AMPERE 4
  3288. #else
  3289. #define MMQ_X_Q8_0_AMPERE 128
  3290. #define MMQ_Y_Q8_0_AMPERE 64
  3291. #define NWARPS_Q8_0_AMPERE 4
  3292. #endif
  3293. #define MMQ_X_Q8_0_PASCAL 64
  3294. #define MMQ_Y_Q8_0_PASCAL 64
  3295. #define NWARPS_Q8_0_PASCAL 8
  3296. template <bool need_check> static __global__ void
  3297. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3298. #if defined(RDNA3) || defined(RDNA2)
  3299. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3300. #endif // defined(RDNA3) || defined(RDNA2)
  3301. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3302. mul_mat_q8_0(
  3303. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3304. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3305. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3306. #if defined(RDNA3) || defined(RDNA2)
  3307. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3308. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3309. const int nwarps = NWARPS_Q8_0_RDNA2;
  3310. #else
  3311. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3312. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3313. const int nwarps = NWARPS_Q8_0_RDNA1;
  3314. #endif // defined(RDNA3) || defined(RDNA2)
  3315. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3316. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3317. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3318. #elif __CUDA_ARCH__ >= CC_VOLTA
  3319. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3320. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3321. const int nwarps = NWARPS_Q8_0_AMPERE;
  3322. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3323. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3324. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3325. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3326. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3327. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3328. const int nwarps = NWARPS_Q8_0_PASCAL;
  3329. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3330. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3331. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3332. #else
  3333. (void) vec_dot_q8_0_q8_1_mul_mat;
  3334. bad_arch();
  3335. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3336. }
  3337. #define MMQ_X_Q2_K_RDNA2 64
  3338. #define MMQ_Y_Q2_K_RDNA2 128
  3339. #define NWARPS_Q2_K_RDNA2 8
  3340. #define MMQ_X_Q2_K_RDNA1 128
  3341. #define MMQ_Y_Q2_K_RDNA1 32
  3342. #define NWARPS_Q2_K_RDNA1 8
  3343. #if defined(CUDA_USE_TENSOR_CORES)
  3344. #define MMQ_X_Q2_K_AMPERE 4
  3345. #define MMQ_Y_Q2_K_AMPERE 32
  3346. #define NWARPS_Q2_K_AMPERE 4
  3347. #else
  3348. #define MMQ_X_Q2_K_AMPERE 64
  3349. #define MMQ_Y_Q2_K_AMPERE 128
  3350. #define NWARPS_Q2_K_AMPERE 4
  3351. #endif
  3352. #define MMQ_X_Q2_K_PASCAL 64
  3353. #define MMQ_Y_Q2_K_PASCAL 64
  3354. #define NWARPS_Q2_K_PASCAL 8
  3355. template <bool need_check> static __global__ void
  3356. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3357. #if defined(RDNA3) || defined(RDNA2)
  3358. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3359. #endif // defined(RDNA3) || defined(RDNA2)
  3360. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3361. mul_mat_q2_K(
  3362. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3363. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3364. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3365. #if defined(RDNA3) || defined(RDNA2)
  3366. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3367. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3368. const int nwarps = NWARPS_Q2_K_RDNA2;
  3369. #else
  3370. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3371. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3372. const int nwarps = NWARPS_Q2_K_RDNA1;
  3373. #endif // defined(RDNA3) || defined(RDNA2)
  3374. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3375. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3376. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3377. #elif __CUDA_ARCH__ >= CC_VOLTA
  3378. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3379. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3380. const int nwarps = NWARPS_Q2_K_AMPERE;
  3381. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3382. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3383. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3384. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3385. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3386. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3387. const int nwarps = NWARPS_Q2_K_PASCAL;
  3388. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3389. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3390. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3391. #else
  3392. (void) vec_dot_q2_K_q8_1_mul_mat;
  3393. bad_arch();
  3394. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3395. }
  3396. #define MMQ_X_Q3_K_RDNA2 128
  3397. #define MMQ_Y_Q3_K_RDNA2 64
  3398. #define NWARPS_Q3_K_RDNA2 8
  3399. #define MMQ_X_Q3_K_RDNA1 32
  3400. #define MMQ_Y_Q3_K_RDNA1 128
  3401. #define NWARPS_Q3_K_RDNA1 8
  3402. #if defined(CUDA_USE_TENSOR_CORES)
  3403. #define MMQ_X_Q3_K_AMPERE 4
  3404. #define MMQ_Y_Q3_K_AMPERE 32
  3405. #define NWARPS_Q3_K_AMPERE 4
  3406. #else
  3407. #define MMQ_X_Q3_K_AMPERE 128
  3408. #define MMQ_Y_Q3_K_AMPERE 128
  3409. #define NWARPS_Q3_K_AMPERE 4
  3410. #endif
  3411. #define MMQ_X_Q3_K_PASCAL 64
  3412. #define MMQ_Y_Q3_K_PASCAL 64
  3413. #define NWARPS_Q3_K_PASCAL 8
  3414. template <bool need_check> static __global__ void
  3415. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3416. #if defined(RDNA3) || defined(RDNA2)
  3417. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3418. #endif // defined(RDNA3) || defined(RDNA2)
  3419. #elif __CUDA_ARCH__ < CC_VOLTA
  3420. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3421. #endif // __CUDA_ARCH__ < CC_VOLTA
  3422. mul_mat_q3_K(
  3423. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3424. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3425. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3426. #if defined(RDNA3) || defined(RDNA2)
  3427. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3428. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3429. const int nwarps = NWARPS_Q3_K_RDNA2;
  3430. #else
  3431. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3432. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3433. const int nwarps = NWARPS_Q3_K_RDNA1;
  3434. #endif // defined(RDNA3) || defined(RDNA2)
  3435. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3436. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3437. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3438. #elif __CUDA_ARCH__ >= CC_VOLTA
  3439. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3440. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3441. const int nwarps = NWARPS_Q3_K_AMPERE;
  3442. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3443. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3444. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3445. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3446. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3447. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3448. const int nwarps = NWARPS_Q3_K_PASCAL;
  3449. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3450. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3451. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3452. #else
  3453. (void) vec_dot_q3_K_q8_1_mul_mat;
  3454. bad_arch();
  3455. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3456. }
  3457. #define MMQ_X_Q4_K_RDNA2 64
  3458. #define MMQ_Y_Q4_K_RDNA2 128
  3459. #define NWARPS_Q4_K_RDNA2 8
  3460. #define MMQ_X_Q4_K_RDNA1 32
  3461. #define MMQ_Y_Q4_K_RDNA1 64
  3462. #define NWARPS_Q4_K_RDNA1 8
  3463. #if defined(CUDA_USE_TENSOR_CORES)
  3464. #define MMQ_X_Q4_K_AMPERE 4
  3465. #define MMQ_Y_Q4_K_AMPERE 32
  3466. #define NWARPS_Q4_K_AMPERE 4
  3467. #else
  3468. #define MMQ_X_Q4_K_AMPERE 64
  3469. #define MMQ_Y_Q4_K_AMPERE 128
  3470. #define NWARPS_Q4_K_AMPERE 4
  3471. #endif
  3472. #define MMQ_X_Q4_K_PASCAL 64
  3473. #define MMQ_Y_Q4_K_PASCAL 64
  3474. #define NWARPS_Q4_K_PASCAL 8
  3475. template <bool need_check> static __global__ void
  3476. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3477. #if defined(RDNA3) || defined(RDNA2)
  3478. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3479. #endif // defined(RDNA3) || defined(RDNA2)
  3480. #elif __CUDA_ARCH__ < CC_VOLTA
  3481. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3482. #endif // __CUDA_ARCH__ < CC_VOLTA
  3483. mul_mat_q4_K(
  3484. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3485. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3486. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3487. #if defined(RDNA3) || defined(RDNA2)
  3488. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3489. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3490. const int nwarps = NWARPS_Q4_K_RDNA2;
  3491. #else
  3492. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3493. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3494. const int nwarps = NWARPS_Q4_K_RDNA1;
  3495. #endif // defined(RDNA3) || defined(RDNA2)
  3496. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3497. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3498. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3499. #elif __CUDA_ARCH__ >= CC_VOLTA
  3500. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3501. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3502. const int nwarps = NWARPS_Q4_K_AMPERE;
  3503. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3504. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3505. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3506. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3507. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3508. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3509. const int nwarps = NWARPS_Q4_K_PASCAL;
  3510. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3511. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3512. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3513. #else
  3514. (void) vec_dot_q4_K_q8_1_mul_mat;
  3515. bad_arch();
  3516. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3517. }
  3518. #define MMQ_X_Q5_K_RDNA2 64
  3519. #define MMQ_Y_Q5_K_RDNA2 128
  3520. #define NWARPS_Q5_K_RDNA2 8
  3521. #define MMQ_X_Q5_K_RDNA1 32
  3522. #define MMQ_Y_Q5_K_RDNA1 64
  3523. #define NWARPS_Q5_K_RDNA1 8
  3524. #if defined(CUDA_USE_TENSOR_CORES)
  3525. #define MMQ_X_Q5_K_AMPERE 4
  3526. #define MMQ_Y_Q5_K_AMPERE 32
  3527. #define NWARPS_Q5_K_AMPERE 4
  3528. #else
  3529. #define MMQ_X_Q5_K_AMPERE 64
  3530. #define MMQ_Y_Q5_K_AMPERE 128
  3531. #define NWARPS_Q5_K_AMPERE 4
  3532. #endif
  3533. #define MMQ_X_Q5_K_PASCAL 64
  3534. #define MMQ_Y_Q5_K_PASCAL 64
  3535. #define NWARPS_Q5_K_PASCAL 8
  3536. template <bool need_check> static __global__ void
  3537. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3538. #if defined(RDNA3) || defined(RDNA2)
  3539. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3540. #endif // defined(RDNA3) || defined(RDNA2)
  3541. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3542. mul_mat_q5_K(
  3543. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3544. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3545. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3546. #if defined(RDNA3) || defined(RDNA2)
  3547. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3548. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3549. const int nwarps = NWARPS_Q5_K_RDNA2;
  3550. #else
  3551. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3552. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3553. const int nwarps = NWARPS_Q5_K_RDNA1;
  3554. #endif // defined(RDNA3) || defined(RDNA2)
  3555. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3556. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3557. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3558. #elif __CUDA_ARCH__ >= CC_VOLTA
  3559. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3560. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3561. const int nwarps = NWARPS_Q5_K_AMPERE;
  3562. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3563. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3564. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3565. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3566. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3567. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3568. const int nwarps = NWARPS_Q5_K_PASCAL;
  3569. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3570. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3571. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3572. #else
  3573. (void) vec_dot_q5_K_q8_1_mul_mat;
  3574. bad_arch();
  3575. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3576. }
  3577. #define MMQ_X_Q6_K_RDNA2 64
  3578. #define MMQ_Y_Q6_K_RDNA2 128
  3579. #define NWARPS_Q6_K_RDNA2 8
  3580. #define MMQ_X_Q6_K_RDNA1 32
  3581. #define MMQ_Y_Q6_K_RDNA1 64
  3582. #define NWARPS_Q6_K_RDNA1 8
  3583. #if defined(CUDA_USE_TENSOR_CORES)
  3584. #define MMQ_X_Q6_K_AMPERE 4
  3585. #define MMQ_Y_Q6_K_AMPERE 32
  3586. #define NWARPS_Q6_K_AMPERE 4
  3587. #else
  3588. #define MMQ_X_Q6_K_AMPERE 64
  3589. #define MMQ_Y_Q6_K_AMPERE 64
  3590. #define NWARPS_Q6_K_AMPERE 4
  3591. #endif
  3592. #define MMQ_X_Q6_K_PASCAL 64
  3593. #define MMQ_Y_Q6_K_PASCAL 64
  3594. #define NWARPS_Q6_K_PASCAL 8
  3595. template <bool need_check> static __global__ void
  3596. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3597. #if defined(RDNA3) || defined(RDNA2)
  3598. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3599. #endif // defined(RDNA3) || defined(RDNA2)
  3600. #elif __CUDA_ARCH__ < CC_VOLTA
  3601. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3602. #endif // __CUDA_ARCH__ < CC_VOLTA
  3603. mul_mat_q6_K(
  3604. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3605. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3606. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3607. #if defined(RDNA3) || defined(RDNA2)
  3608. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3609. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3610. const int nwarps = NWARPS_Q6_K_RDNA2;
  3611. #else
  3612. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3613. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3614. const int nwarps = NWARPS_Q6_K_RDNA1;
  3615. #endif // defined(RDNA3) || defined(RDNA2)
  3616. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3617. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3618. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3619. #elif __CUDA_ARCH__ >= CC_VOLTA
  3620. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3621. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3622. const int nwarps = NWARPS_Q6_K_AMPERE;
  3623. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3624. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3625. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3626. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3627. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3628. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3629. const int nwarps = NWARPS_Q6_K_PASCAL;
  3630. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3631. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3632. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3633. #else
  3634. (void) vec_dot_q6_K_q8_1_mul_mat;
  3635. bad_arch();
  3636. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3637. }
  3638. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3639. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3640. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3641. if (row >= nrows) {
  3642. return;
  3643. }
  3644. const int blocks_per_row = ncols / qk;
  3645. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3646. // partial sum for each thread
  3647. float tmp = 0.0f;
  3648. const block_q_t * x = (const block_q_t *) vx;
  3649. const block_q8_1 * y = (const block_q8_1 *) vy;
  3650. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3651. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3652. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3653. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3654. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3655. }
  3656. // sum up partial sums and write back result
  3657. #pragma unroll
  3658. for (int mask = 16; mask > 0; mask >>= 1) {
  3659. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3660. }
  3661. if (threadIdx.x == 0) {
  3662. dst[row] = tmp;
  3663. }
  3664. }
  3665. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3666. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3667. // qk = quantized weights per x block
  3668. // qr = number of quantized weights per data value in x block
  3669. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3670. if (row >= nrows) {
  3671. return;
  3672. }
  3673. const int tid = threadIdx.x;
  3674. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3675. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3676. const int y_offset = qr == 1 ? 1 : qk/2;
  3677. // partial sum for each thread
  3678. #ifdef GGML_CUDA_F16
  3679. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3680. #else
  3681. float tmp = 0.0f;
  3682. #endif // GGML_CUDA_F16
  3683. for (int i = 0; i < ncols; i += iter_stride) {
  3684. const int col = i + vals_per_iter*tid;
  3685. const int ib = (row*ncols + col)/qk; // x block index
  3686. const int iqs = (col%qk)/qr; // x quant index
  3687. const int iybs = col - col%qk; // y block start index
  3688. // processing >2 values per i iter is faster for fast GPUs
  3689. #pragma unroll
  3690. for (int j = 0; j < vals_per_iter; j += 2) {
  3691. // process 2 vals per j iter
  3692. // dequantize
  3693. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3694. dfloat2 v;
  3695. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3696. // matrix multiplication
  3697. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3698. #ifdef GGML_CUDA_F16
  3699. tmp += __hmul2(v, {
  3700. y[iybs + iqs + j/qr + 0],
  3701. y[iybs + iqs + j/qr + y_offset]
  3702. });
  3703. #else
  3704. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3705. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3706. #endif // GGML_CUDA_F16
  3707. }
  3708. }
  3709. // sum up partial sums and write back result
  3710. #pragma unroll
  3711. for (int mask = 16; mask > 0; mask >>= 1) {
  3712. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3713. }
  3714. if (tid == 0) {
  3715. #ifdef GGML_CUDA_F16
  3716. dst[row] = tmp.x + tmp.y;
  3717. #else
  3718. dst[row] = tmp;
  3719. #endif // GGML_CUDA_F16
  3720. }
  3721. }
  3722. static __global__ void mul_mat_p021_f16_f32(
  3723. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3724. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3725. const half * x = (const half *) vx;
  3726. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3727. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3728. const int channel_x = channel / (nchannels_y / nchannels_x);
  3729. const int nrows_y = ncols_x;
  3730. const int nrows_dst = nrows_x;
  3731. const int row_dst = row_x;
  3732. float tmp = 0.0f;
  3733. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3734. const int col_x = col_x0 + threadIdx.x;
  3735. if (col_x >= ncols_x) {
  3736. break;
  3737. }
  3738. // x is transposed and permuted
  3739. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3740. const float xi = __half2float(x[ix]);
  3741. const int row_y = col_x;
  3742. // y is not transposed but permuted
  3743. const int iy = channel*nrows_y + row_y;
  3744. tmp += xi * y[iy];
  3745. }
  3746. // dst is not transposed and not permuted
  3747. const int idst = channel*nrows_dst + row_dst;
  3748. // sum up partial sums and write back result
  3749. #pragma unroll
  3750. for (int mask = 16; mask > 0; mask >>= 1) {
  3751. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3752. }
  3753. if (threadIdx.x == 0) {
  3754. dst[idst] = tmp;
  3755. }
  3756. }
  3757. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3758. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3759. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3760. const half * x = (const half *) vx;
  3761. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3762. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3763. const int channel_x = channel / channel_x_divisor;
  3764. const int nrows_y = ncols_x;
  3765. const int nrows_dst = nrows_x;
  3766. const int row_dst = row_x;
  3767. const int idst = channel*nrows_dst + row_dst;
  3768. float tmp = 0.0f;
  3769. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3770. const int col_x = col_x0 + threadIdx.x;
  3771. if (col_x >= ncols_x) {
  3772. break;
  3773. }
  3774. const int row_y = col_x;
  3775. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3776. const int iy = channel*nrows_y + row_y;
  3777. const float xi = __half2float(x[ix]);
  3778. tmp += xi * y[iy];
  3779. }
  3780. // sum up partial sums and write back result
  3781. #pragma unroll
  3782. for (int mask = 16; mask > 0; mask >>= 1) {
  3783. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3784. }
  3785. if (threadIdx.x == 0) {
  3786. dst[idst] = tmp;
  3787. }
  3788. }
  3789. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3790. const float * xi = (const float *) cxi;
  3791. float * dsti = (float *) cdsti;
  3792. *dsti = *xi;
  3793. }
  3794. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3795. const float * xi = (const float *) cxi;
  3796. half * dsti = (half *) cdsti;
  3797. *dsti = __float2half(*xi);
  3798. }
  3799. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3800. const half * xi = (const half *) cxi;
  3801. half * dsti = (half *) cdsti;
  3802. *dsti = *xi;
  3803. }
  3804. template <cpy_kernel_t cpy_1>
  3805. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3806. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3807. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3808. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3809. if (i >= ne) {
  3810. return;
  3811. }
  3812. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3813. // then combine those indices with the corresponding byte offsets to get the total offsets
  3814. const int i02 = i / (ne00*ne01);
  3815. const int i01 = (i - i02*ne01*ne00) / ne00;
  3816. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3817. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3818. const int i12 = i / (ne10*ne11);
  3819. const int i11 = (i - i12*ne10*ne11) / ne10;
  3820. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3821. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3822. cpy_1(cx + x_offset, cdst + dst_offset);
  3823. }
  3824. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  3825. const float * xi = (const float *) cxi;
  3826. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  3827. float amax = 0.0f; // absolute max
  3828. for (int j = 0; j < QK8_0; j++) {
  3829. const float v = xi[j];
  3830. amax = fmaxf(amax, fabsf(v));
  3831. }
  3832. const float d = amax / ((1 << 7) - 1);
  3833. const float id = d ? 1.0f/d : 0.0f;
  3834. dsti->d = d;
  3835. for (int j = 0; j < QK8_0; ++j) {
  3836. const float x0 = xi[j]*id;
  3837. dsti->qs[j] = roundf(x0);
  3838. }
  3839. }
  3840. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  3841. const float * xi = (const float *) cxi;
  3842. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  3843. float amax = 0.0f;
  3844. float vmax = 0.0f;
  3845. for (int j = 0; j < QK4_0; ++j) {
  3846. const float v = xi[j];
  3847. if (amax < fabsf(v)) {
  3848. amax = fabsf(v);
  3849. vmax = v;
  3850. }
  3851. }
  3852. const float d = vmax / -8;
  3853. const float id = d ? 1.0f/d : 0.0f;
  3854. dsti->d = d;
  3855. for (int j = 0; j < QK4_0/2; ++j) {
  3856. const float x0 = xi[0 + j]*id;
  3857. const float x1 = xi[QK4_0/2 + j]*id;
  3858. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  3859. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  3860. dsti->qs[j] = xi0;
  3861. dsti->qs[j] |= xi1 << 4;
  3862. }
  3863. }
  3864. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  3865. const float * xi = (const float *) cxi;
  3866. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  3867. float vmin = FLT_MAX;
  3868. float vmax = -FLT_MAX;
  3869. for (int j = 0; j < QK4_1; ++j) {
  3870. const float v = xi[j];
  3871. if (v < vmin) vmin = v;
  3872. if (v > vmax) vmax = v;
  3873. }
  3874. const float d = (vmax - vmin) / ((1 << 4) - 1);
  3875. const float id = d ? 1.0f/d : 0.0f;
  3876. dsti->dm.x = d;
  3877. dsti->dm.y = vmin;
  3878. for (int j = 0; j < QK4_1/2; ++j) {
  3879. const float x0 = (xi[0 + j] - vmin)*id;
  3880. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  3881. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  3882. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  3883. dsti->qs[j] = xi0;
  3884. dsti->qs[j] |= xi1 << 4;
  3885. }
  3886. }
  3887. template <cpy_kernel_t cpy_blck, int qk>
  3888. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  3889. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3890. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3891. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  3892. if (i >= ne) {
  3893. return;
  3894. }
  3895. const int i02 = i / (ne00*ne01);
  3896. const int i01 = (i - i02*ne01*ne00) / ne00;
  3897. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  3898. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3899. const int i12 = i / (ne10*ne11);
  3900. const int i11 = (i - i12*ne10*ne11) / ne10;
  3901. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  3902. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3903. cpy_blck(cx + x_offset, cdst + dst_offset);
  3904. }
  3905. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3906. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3907. return 1.0f - min(1.0f, max(0.0f, y));
  3908. }
  3909. struct rope_corr_dims {
  3910. float v[4];
  3911. };
  3912. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3913. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3914. static __device__ void rope_yarn(
  3915. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3916. float * cos_theta, float * sin_theta
  3917. ) {
  3918. // Get n-d rotational scaling corrected for extrapolation
  3919. float theta_interp = freq_scale * theta_extrap;
  3920. float theta = theta_interp;
  3921. if (ext_factor != 0.0f) {
  3922. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3923. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3924. // Get n-d magnitude scaling corrected for interpolation
  3925. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3926. }
  3927. *cos_theta = cosf(theta) * mscale;
  3928. *sin_theta = sinf(theta) * mscale;
  3929. }
  3930. // rope == RoPE == rotary positional embedding
  3931. template<typename T, bool has_pos>
  3932. static __global__ void rope(
  3933. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3934. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3935. ) {
  3936. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3937. if (col >= ncols) {
  3938. return;
  3939. }
  3940. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3941. const int i = row*ncols + col;
  3942. const int i2 = row/p_delta_rows;
  3943. const int p = has_pos ? pos[i2] : 0;
  3944. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3945. float cos_theta, sin_theta;
  3946. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3947. const float x0 = x[i + 0];
  3948. const float x1 = x[i + 1];
  3949. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3950. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3951. }
  3952. template<typename T, bool has_pos>
  3953. static __global__ void rope_neox(
  3954. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  3955. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  3956. ) {
  3957. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3958. if (col >= ncols) {
  3959. return;
  3960. }
  3961. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3962. const int ib = col / n_dims;
  3963. const int ic = col % n_dims;
  3964. if (ib > 0) {
  3965. const int i = row*ncols + ib*n_dims + ic;
  3966. dst[i + 0] = x[i + 0];
  3967. dst[i + 1] = x[i + 1];
  3968. return;
  3969. }
  3970. const int i = row*ncols + ib*n_dims + ic/2;
  3971. const int i2 = row/p_delta_rows;
  3972. float cur_rot = inv_ndims * ic - ib;
  3973. const int p = has_pos ? pos[i2] : 0;
  3974. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  3975. float cos_theta, sin_theta;
  3976. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3977. const float x0 = x[i + 0];
  3978. const float x1 = x[i + n_dims/2];
  3979. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3980. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  3981. }
  3982. static __global__ void rope_glm_f32(
  3983. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3984. int n_ctx
  3985. ) {
  3986. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3987. const int half_n_dims = ncols/4;
  3988. if (col >= half_n_dims) {
  3989. return;
  3990. }
  3991. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3992. const int i = row*ncols + col;
  3993. const int i2 = row/p_delta_rows;
  3994. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  3995. // FIXME: this is likely wrong
  3996. const int p = pos != nullptr ? pos[i2] : 0;
  3997. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  3998. const float sin_theta = sinf(theta);
  3999. const float cos_theta = cosf(theta);
  4000. const float x0 = x[i + 0];
  4001. const float x1 = x[i + half_n_dims];
  4002. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4003. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4004. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4005. const float sin_block_theta = sinf(block_theta);
  4006. const float cos_block_theta = cosf(block_theta);
  4007. const float x2 = x[i + half_n_dims * 2];
  4008. const float x3 = x[i + half_n_dims * 3];
  4009. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4010. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4011. }
  4012. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4013. const int n_heads_log2_floor, const float m0, const float m1) {
  4014. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4015. if (col >= ncols) {
  4016. return;
  4017. }
  4018. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4019. const int i = row*ncols + col;
  4020. const int k = row/k_rows;
  4021. float m_k;
  4022. if (k < n_heads_log2_floor) {
  4023. m_k = powf(m0, k + 1);
  4024. } else {
  4025. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4026. }
  4027. dst[i] = col * m_k + x[i];
  4028. }
  4029. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4030. const int row = blockIdx.y;
  4031. const int col = threadIdx.x;
  4032. float sum = 0.0f;
  4033. for (int i = col; i < ncols; i += blockDim.x) {
  4034. sum += x[row * ncols + i];
  4035. }
  4036. sum = warp_reduce_sum(sum);
  4037. if (col == 0) {
  4038. dst[row] = sum;
  4039. }
  4040. }
  4041. template<typename T>
  4042. static inline __device__ void swap(T & a, T & b) {
  4043. T tmp = a;
  4044. a = b;
  4045. b = tmp;
  4046. }
  4047. template<ggml_sort_order order>
  4048. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4049. // bitonic sort
  4050. int col = threadIdx.x;
  4051. int row = blockIdx.y;
  4052. if (col >= ncols) return;
  4053. const float * x_row = x + row * ncols;
  4054. int * dst_row = dst + row * ncols;
  4055. // initialize indices
  4056. if (col < ncols) {
  4057. dst_row[col] = col;
  4058. }
  4059. __syncthreads();
  4060. for (int k = 2; k <= ncols; k *= 2) {
  4061. for (int j = k / 2; j > 0; j /= 2) {
  4062. int ixj = col ^ j;
  4063. if (ixj > col) {
  4064. if ((col & k) == 0) {
  4065. 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]]) {
  4066. swap(dst_row[col], dst_row[ixj]);
  4067. }
  4068. } else {
  4069. 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]]) {
  4070. swap(dst_row[col], dst_row[ixj]);
  4071. }
  4072. }
  4073. }
  4074. __syncthreads();
  4075. }
  4076. }
  4077. }
  4078. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4079. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4080. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4081. if (col >= ncols) {
  4082. return;
  4083. }
  4084. const int i = row*ncols + col;
  4085. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4086. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4087. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4088. }
  4089. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {
  4090. const int tid = threadIdx.x;
  4091. const int rowx = blockIdx.x;
  4092. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4093. const int block_size = blockDim.x;
  4094. const int warp_id = threadIdx.x / WARP_SIZE;
  4095. const int lane_id = threadIdx.x % WARP_SIZE;
  4096. __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];
  4097. float max_val = -INFINITY;
  4098. for (int col = tid; col < ncols; col += block_size) {
  4099. const int ix = rowx*ncols + col;
  4100. const int iy = rowy*ncols + col;
  4101. max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));
  4102. }
  4103. // find the max value in the block
  4104. max_val = warp_reduce_max(max_val);
  4105. if (block_size > WARP_SIZE) {
  4106. if (warp_id == 0) {
  4107. buf[lane_id] = -INFINITY;
  4108. }
  4109. __syncthreads();
  4110. if (lane_id == 0) {
  4111. buf[warp_id] = max_val;
  4112. }
  4113. __syncthreads();
  4114. max_val = buf[lane_id];
  4115. max_val = warp_reduce_max(max_val);
  4116. }
  4117. float tmp = 0.f;
  4118. for (int col = tid; col < ncols; col += block_size) {
  4119. const int ix = rowx*ncols + col;
  4120. const int iy = rowy*ncols + col;
  4121. const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);
  4122. tmp += val;
  4123. dst[ix] = val;
  4124. }
  4125. // find the sum of exps in the block
  4126. tmp = warp_reduce_sum(tmp);
  4127. if (block_size > WARP_SIZE) {
  4128. if (warp_id == 0) {
  4129. buf[lane_id] = 0.f;
  4130. }
  4131. __syncthreads();
  4132. if (lane_id == 0) {
  4133. buf[warp_id] = tmp;
  4134. }
  4135. __syncthreads();
  4136. tmp = buf[lane_id];
  4137. tmp = warp_reduce_sum(tmp);
  4138. }
  4139. const float inv_tmp = 1.f / tmp;
  4140. for (int col = tid; col < ncols; col += block_size) {
  4141. const int i = rowx*ncols + col;
  4142. dst[i] *= inv_tmp;
  4143. }
  4144. }
  4145. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4146. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4147. if (i >= k) {
  4148. return;
  4149. }
  4150. dst[i] = scale * x[i];
  4151. }
  4152. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4153. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4154. if (i >= k) {
  4155. return;
  4156. }
  4157. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4158. }
  4159. static __global__ void im2col_f32_f16(
  4160. const float * x, half * dst,
  4161. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4162. int s0, int s1, int p0, int p1, int d0, int d1) {
  4163. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4164. if (i >= pelements) {
  4165. return;
  4166. }
  4167. const int ksize = OW * (KH > 1 ? KW : 1);
  4168. const int kx = i / ksize;
  4169. const int kd = kx * ksize;
  4170. const int ky = (i - kd) / OW;
  4171. const int ix = i % OW;
  4172. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4173. const int64_t iih = blockIdx.y * s1 + ky * d1 - p1;
  4174. const int64_t offset_dst =
  4175. (blockIdx.y * OW + ix) * CHW +
  4176. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4177. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4178. dst[offset_dst] = __float2half(0.0f);
  4179. } else {
  4180. const int64_t offset_src = blockIdx.z * offset_delta;
  4181. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4182. }
  4183. }
  4184. template<int qk, int qr, dequantize_kernel_t dq>
  4185. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4186. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4187. GGML_TENSOR_BINARY_OP_LOCALS
  4188. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4189. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4190. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4191. // strides in elements
  4192. //const size_t s0 = nb0 / ggml_element_size(dst);
  4193. const size_t s1 = nb1 / ggml_element_size(dst);
  4194. const size_t s2 = nb2 / ggml_element_size(dst);
  4195. const size_t s3 = nb3 / ggml_element_size(dst);
  4196. const size_t s10 = nb10 / ggml_element_size(src1);
  4197. const size_t s11 = nb11 / ggml_element_size(src1);
  4198. const size_t s12 = nb12 / ggml_element_size(src1);
  4199. //const size_t s13 = nb13 / ggml_element_size(src1);
  4200. GGML_ASSERT(ne00 % 2 == 0);
  4201. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4202. src0_dd, src1_dd, dst_dd,
  4203. ne00, /*ne01, ne02, ne03,*/
  4204. /*ne10, ne11,*/ ne12, /*ne13,*/
  4205. /* s0,*/ s1, s2, s3,
  4206. /* nb00,*/ nb01, nb02, nb03,
  4207. s10, s11, s12/*, s13*/);
  4208. (void) dst;
  4209. }
  4210. template<typename src0_t>
  4211. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4212. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4213. GGML_TENSOR_BINARY_OP_LOCALS
  4214. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4215. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4216. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4217. // strides in elements
  4218. //const size_t s0 = nb0 / ggml_element_size(dst);
  4219. const size_t s1 = nb1 / ggml_element_size(dst);
  4220. const size_t s2 = nb2 / ggml_element_size(dst);
  4221. const size_t s3 = nb3 / ggml_element_size(dst);
  4222. const size_t s10 = nb10 / ggml_element_size(src1);
  4223. const size_t s11 = nb11 / ggml_element_size(src1);
  4224. const size_t s12 = nb12 / ggml_element_size(src1);
  4225. //const size_t s13 = nb13 / ggml_element_size(src1);
  4226. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4227. src0_dd, src1_dd, dst_dd,
  4228. ne00, /*ne01, ne02, ne03,*/
  4229. /*ne10, ne11,*/ ne12, /*ne13,*/
  4230. /* s0,*/ s1, s2, s3,
  4231. /* nb00,*/ nb01, nb02, nb03,
  4232. s10, s11, s12/*, s13*/);
  4233. (void) dst;
  4234. }
  4235. template<float (*bin_op)(const float, const float)>
  4236. struct bin_bcast_cuda {
  4237. template<typename src0_t, typename src1_t, typename dst_t>
  4238. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4239. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4240. cudaStream_t stream) {
  4241. GGML_TENSOR_BINARY_OP_LOCALS
  4242. int nr0 = ne10/ne0;
  4243. int nr1 = ne11/ne1;
  4244. int nr2 = ne12/ne2;
  4245. int nr3 = ne13/ne3;
  4246. int nr[4] = { nr0, nr1, nr2, nr3 };
  4247. // collapse dimensions until first broadcast dimension
  4248. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4249. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4250. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4251. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4252. auto collapse = [](int64_t cne[]) {
  4253. cne[0] *= cne[1];
  4254. cne[1] = cne[2];
  4255. cne[2] = cne[3];
  4256. cne[3] = 1;
  4257. };
  4258. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  4259. cnb[1] *= cne[1];
  4260. cnb[2] *= cne[2];
  4261. cnb[3] *= cne[3];
  4262. };
  4263. for (int i = 0; i < 4; i++) {
  4264. if (nr[i] != 1) {
  4265. break;
  4266. }
  4267. if (i > 0) {
  4268. collapse_nb(cnb0, cne0);
  4269. collapse_nb(cnb1, cne1);
  4270. collapse(cne0);
  4271. collapse(cne1);
  4272. }
  4273. }
  4274. {
  4275. int64_t ne0 = cne0[0];
  4276. int64_t ne1 = cne0[1];
  4277. int64_t ne2 = cne0[2];
  4278. int64_t ne3 = cne0[3];
  4279. int64_t ne10 = cne1[0];
  4280. int64_t ne11 = cne1[1];
  4281. int64_t ne12 = cne1[2];
  4282. int64_t ne13 = cne1[3];
  4283. size_t nb0 = cnb0[0];
  4284. size_t nb1 = cnb0[1];
  4285. size_t nb2 = cnb0[2];
  4286. size_t nb3 = cnb0[3];
  4287. size_t nb10 = cnb1[0];
  4288. size_t nb11 = cnb1[1];
  4289. size_t nb12 = cnb1[2];
  4290. size_t nb13 = cnb1[3];
  4291. size_t s0 = nb0 / sizeof(dst_t);
  4292. size_t s1 = nb1 / sizeof(dst_t);
  4293. size_t s2 = nb2 / sizeof(dst_t);
  4294. size_t s3 = nb3 / sizeof(dst_t);
  4295. size_t s10 = nb10 / sizeof(src1_t);
  4296. size_t s11 = nb11 / sizeof(src1_t);
  4297. size_t s12 = nb12 / sizeof(src1_t);
  4298. size_t s13 = nb13 / sizeof(src1_t);
  4299. GGML_ASSERT(s0 == 1);
  4300. GGML_ASSERT(s10 == 1);
  4301. const int block_size = 128;
  4302. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4303. dim3 block_dims;
  4304. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4305. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4306. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4307. dim3 block_nums(
  4308. (hne0 + block_dims.x - 1) / block_dims.x,
  4309. (ne1 + block_dims.y - 1) / block_dims.y,
  4310. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4311. );
  4312. if (block_nums.z > 65535) {
  4313. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4314. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4315. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4316. src0_dd, src1_dd, dst_dd,
  4317. ne0, ne1, ne2, ne3,
  4318. ne10, ne11, ne12, ne13,
  4319. /* s0, */ s1, s2, s3,
  4320. /* s10, */ s11, s12, s13);
  4321. } else {
  4322. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4323. src0_dd, src1_dd, dst_dd,
  4324. ne0, ne1, ne2, ne3,
  4325. ne10, ne11, ne12, ne13,
  4326. /* s0, */ s1, s2, s3,
  4327. /* s10, */ s11, s12, s13);
  4328. }
  4329. }
  4330. }
  4331. };
  4332. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4333. const int ne10, const int ne11, const int ne12,
  4334. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4335. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4336. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4337. }
  4338. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4339. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4340. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4341. }
  4342. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4343. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4344. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4345. }
  4346. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4347. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4348. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4349. }
  4350. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4351. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4352. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4353. }
  4354. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4355. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4356. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4357. }
  4358. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4359. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4360. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4361. }
  4362. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4363. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4364. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4365. }
  4366. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4367. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4368. if (ncols < 1024) {
  4369. const dim3 block_dims(WARP_SIZE, 1, 1);
  4370. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4371. } else {
  4372. const dim3 block_dims(1024, 1, 1);
  4373. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4374. }
  4375. }
  4376. static void group_norm_f32_cuda(const float * x, float * dst, const int num_groups, const int group_size, const int ne_elements, cudaStream_t stream) {
  4377. static const float eps = 1e-6f;
  4378. if (group_size < 1024) {
  4379. const dim3 block_dims(WARP_SIZE, 1, 1);
  4380. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4381. } else {
  4382. const dim3 block_dims(1024, 1, 1);
  4383. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4384. }
  4385. }
  4386. static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
  4387. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4388. dim3 gridDim(num_blocks, ne1, ne2);
  4389. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4390. }
  4391. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int scale_factor, cudaStream_t stream) {
  4392. int ne0 = (ne00 * scale_factor);
  4393. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4394. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4395. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4396. }
  4397. static void pad_f32_cuda(const float * x, float * dst,
  4398. const int ne00, const int ne01, const int ne02,
  4399. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4400. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4401. dim3 gridDim(num_blocks, ne1, ne2);
  4402. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4403. }
  4404. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4405. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4406. if (ncols < 1024) {
  4407. const dim3 block_dims(WARP_SIZE, 1, 1);
  4408. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4409. } else {
  4410. const dim3 block_dims(1024, 1, 1);
  4411. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4412. }
  4413. }
  4414. 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) {
  4415. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4416. const dim3 num_blocks(block_num_x, ky, 1);
  4417. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4418. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4419. }
  4420. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4421. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4422. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4423. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4424. }
  4425. template<typename dst_t>
  4426. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4427. const int nb = k / QK_K;
  4428. #if QK_K == 256
  4429. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4430. #else
  4431. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4432. #endif
  4433. }
  4434. template<typename dst_t>
  4435. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4436. const int nb = k / QK_K;
  4437. #if QK_K == 256
  4438. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4439. #else
  4440. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4441. #endif
  4442. }
  4443. template<typename dst_t>
  4444. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4445. const int nb = k / QK_K;
  4446. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4447. }
  4448. template<typename dst_t>
  4449. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4450. const int nb = k / QK_K;
  4451. #if QK_K == 256
  4452. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4453. #else
  4454. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4455. #endif
  4456. }
  4457. template<typename dst_t>
  4458. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4459. const int nb = k / QK_K;
  4460. #if QK_K == 256
  4461. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4462. #else
  4463. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4464. #endif
  4465. }
  4466. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4467. switch (type) {
  4468. case GGML_TYPE_Q4_0:
  4469. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4470. case GGML_TYPE_Q4_1:
  4471. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4472. case GGML_TYPE_Q5_0:
  4473. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4474. case GGML_TYPE_Q5_1:
  4475. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4476. case GGML_TYPE_Q8_0:
  4477. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4478. case GGML_TYPE_Q2_K:
  4479. return dequantize_row_q2_K_cuda;
  4480. case GGML_TYPE_Q3_K:
  4481. return dequantize_row_q3_K_cuda;
  4482. case GGML_TYPE_Q4_K:
  4483. return dequantize_row_q4_K_cuda;
  4484. case GGML_TYPE_Q5_K:
  4485. return dequantize_row_q5_K_cuda;
  4486. case GGML_TYPE_Q6_K:
  4487. return dequantize_row_q6_K_cuda;
  4488. case GGML_TYPE_F32:
  4489. return dequantize_block_cuda<1, 1, convert_f32>;
  4490. default:
  4491. return nullptr;
  4492. }
  4493. }
  4494. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4495. switch (type) {
  4496. case GGML_TYPE_Q4_0:
  4497. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4498. case GGML_TYPE_Q4_1:
  4499. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4500. case GGML_TYPE_Q5_0:
  4501. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4502. case GGML_TYPE_Q5_1:
  4503. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4504. case GGML_TYPE_Q8_0:
  4505. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4506. case GGML_TYPE_Q2_K:
  4507. return dequantize_row_q2_K_cuda;
  4508. case GGML_TYPE_Q3_K:
  4509. return dequantize_row_q3_K_cuda;
  4510. case GGML_TYPE_Q4_K:
  4511. return dequantize_row_q4_K_cuda;
  4512. case GGML_TYPE_Q5_K:
  4513. return dequantize_row_q5_K_cuda;
  4514. case GGML_TYPE_Q6_K:
  4515. return dequantize_row_q6_K_cuda;
  4516. case GGML_TYPE_F16:
  4517. return dequantize_block_cuda<1, 1, convert_f16>;
  4518. default:
  4519. return nullptr;
  4520. }
  4521. }
  4522. 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) {
  4523. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4524. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4525. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4526. const dim3 block_nums(block_num_y, 1, 1);
  4527. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4528. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4529. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4530. }
  4531. 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) {
  4532. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4533. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4534. const dim3 block_nums(block_num_y, 1, 1);
  4535. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4536. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4537. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4538. }
  4539. 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) {
  4540. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4541. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4542. const dim3 block_nums(block_num_y, 1, 1);
  4543. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4544. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4545. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4546. }
  4547. 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) {
  4548. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4549. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4550. const dim3 block_nums(block_num_y, 1, 1);
  4551. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4552. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4553. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4554. }
  4555. 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) {
  4556. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4557. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4558. const dim3 block_nums(block_num_y, 1, 1);
  4559. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4560. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4561. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4562. }
  4563. 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) {
  4564. GGML_ASSERT(ncols % QK_K == 0);
  4565. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4566. const int block_num_y = (nrows + ny - 1) / ny;
  4567. const dim3 block_nums(block_num_y, 1, 1);
  4568. const dim3 block_dims(32, ny, 1);
  4569. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4570. }
  4571. 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) {
  4572. GGML_ASSERT(ncols % QK_K == 0);
  4573. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4574. const int block_num_y = (nrows + ny - 1) / ny;
  4575. const dim3 block_nums(block_num_y, 1, 1);
  4576. const dim3 block_dims(32, ny, 1);
  4577. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4578. }
  4579. 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) {
  4580. GGML_ASSERT(ncols % QK_K == 0);
  4581. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4582. const int block_num_y = (nrows + ny - 1) / ny;
  4583. const dim3 block_nums(block_num_y, 1, 1);
  4584. const dim3 block_dims(32, ny, 1);
  4585. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4586. }
  4587. 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) {
  4588. GGML_ASSERT(ncols % QK_K == 0);
  4589. const dim3 block_dims(32, 1, 1);
  4590. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4591. }
  4592. 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) {
  4593. GGML_ASSERT(ncols % QK_K == 0);
  4594. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4595. const int block_num_y = (nrows + ny - 1) / ny;
  4596. const dim3 block_nums(block_num_y, 1, 1);
  4597. const dim3 block_dims(32, ny, 1);
  4598. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4599. }
  4600. 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) {
  4601. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4602. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4603. const dim3 block_nums(block_num_y, 1, 1);
  4604. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4605. dequantize_mul_mat_vec<1, 1, convert_f16>
  4606. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4607. }
  4608. 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) {
  4609. GGML_ASSERT(ncols % QK4_0 == 0);
  4610. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4611. const dim3 block_nums(block_num_y, 1, 1);
  4612. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4613. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4614. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4615. }
  4616. 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) {
  4617. GGML_ASSERT(ncols % QK4_1 == 0);
  4618. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4619. const dim3 block_nums(block_num_y, 1, 1);
  4620. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4621. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4622. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4623. }
  4624. 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) {
  4625. GGML_ASSERT(ncols % QK5_0 == 0);
  4626. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4627. const dim3 block_nums(block_num_y, 1, 1);
  4628. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4629. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4630. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4631. }
  4632. 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) {
  4633. GGML_ASSERT(ncols % QK5_1 == 0);
  4634. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4635. const dim3 block_nums(block_num_y, 1, 1);
  4636. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4637. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4638. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4639. }
  4640. 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) {
  4641. GGML_ASSERT(ncols % QK8_0 == 0);
  4642. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4643. const dim3 block_nums(block_num_y, 1, 1);
  4644. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4645. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4646. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4647. }
  4648. 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) {
  4649. GGML_ASSERT(ncols % QK_K == 0);
  4650. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4651. const dim3 block_nums(block_num_y, 1, 1);
  4652. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4653. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  4654. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4655. }
  4656. 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) {
  4657. GGML_ASSERT(ncols % QK_K == 0);
  4658. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4659. const dim3 block_nums(block_num_y, 1, 1);
  4660. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4661. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4662. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4663. }
  4664. 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) {
  4665. GGML_ASSERT(ncols % QK_K == 0);
  4666. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4667. const dim3 block_nums(block_num_y, 1, 1);
  4668. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4669. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4670. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4671. }
  4672. 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) {
  4673. GGML_ASSERT(ncols % QK_K == 0);
  4674. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4675. const dim3 block_nums(block_num_y, 1, 1);
  4676. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4677. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4678. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4679. }
  4680. 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) {
  4681. GGML_ASSERT(ncols % QK_K == 0);
  4682. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4683. const dim3 block_nums(block_num_y, 1, 1);
  4684. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4685. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4686. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4687. }
  4688. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4689. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4690. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4691. int id;
  4692. CUDA_CHECK(cudaGetDevice(&id));
  4693. const int compute_capability = g_device_caps[id].cc;
  4694. int mmq_x, mmq_y, nwarps;
  4695. if (compute_capability >= CC_RDNA2) {
  4696. mmq_x = MMQ_X_Q4_0_RDNA2;
  4697. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4698. nwarps = NWARPS_Q4_0_RDNA2;
  4699. } else if (compute_capability >= CC_OFFSET_AMD) {
  4700. mmq_x = MMQ_X_Q4_0_RDNA1;
  4701. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4702. nwarps = NWARPS_Q4_0_RDNA1;
  4703. } else if (compute_capability >= CC_VOLTA) {
  4704. mmq_x = MMQ_X_Q4_0_AMPERE;
  4705. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4706. nwarps = NWARPS_Q4_0_AMPERE;
  4707. } else if (compute_capability >= MIN_CC_DP4A) {
  4708. mmq_x = MMQ_X_Q4_0_PASCAL;
  4709. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4710. nwarps = NWARPS_Q4_0_PASCAL;
  4711. } else {
  4712. GGML_ASSERT(false);
  4713. }
  4714. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4715. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4716. const dim3 block_nums(block_num_x, block_num_y, 1);
  4717. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4718. if (nrows_x % mmq_y == 0) {
  4719. const bool need_check = false;
  4720. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4721. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4722. } else {
  4723. const bool need_check = true;
  4724. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4725. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4726. }
  4727. }
  4728. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4729. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4730. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4731. int id;
  4732. CUDA_CHECK(cudaGetDevice(&id));
  4733. const int compute_capability = g_device_caps[id].cc;
  4734. int mmq_x, mmq_y, nwarps;
  4735. if (compute_capability >= CC_RDNA2) {
  4736. mmq_x = MMQ_X_Q4_1_RDNA2;
  4737. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4738. nwarps = NWARPS_Q4_1_RDNA2;
  4739. } else if (compute_capability >= CC_OFFSET_AMD) {
  4740. mmq_x = MMQ_X_Q4_1_RDNA1;
  4741. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4742. nwarps = NWARPS_Q4_1_RDNA1;
  4743. } else if (compute_capability >= CC_VOLTA) {
  4744. mmq_x = MMQ_X_Q4_1_AMPERE;
  4745. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4746. nwarps = NWARPS_Q4_1_AMPERE;
  4747. } else if (compute_capability >= MIN_CC_DP4A) {
  4748. mmq_x = MMQ_X_Q4_1_PASCAL;
  4749. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4750. nwarps = NWARPS_Q4_1_PASCAL;
  4751. } else {
  4752. GGML_ASSERT(false);
  4753. }
  4754. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4755. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4756. const dim3 block_nums(block_num_x, block_num_y, 1);
  4757. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4758. if (nrows_x % mmq_y == 0) {
  4759. const bool need_check = false;
  4760. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4761. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4762. } else {
  4763. const bool need_check = true;
  4764. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4765. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4766. }
  4767. }
  4768. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4769. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4770. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4771. int id;
  4772. CUDA_CHECK(cudaGetDevice(&id));
  4773. const int compute_capability = g_device_caps[id].cc;
  4774. int mmq_x, mmq_y, nwarps;
  4775. if (compute_capability >= CC_RDNA2) {
  4776. mmq_x = MMQ_X_Q5_0_RDNA2;
  4777. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4778. nwarps = NWARPS_Q5_0_RDNA2;
  4779. } else if (compute_capability >= CC_OFFSET_AMD) {
  4780. mmq_x = MMQ_X_Q5_0_RDNA1;
  4781. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4782. nwarps = NWARPS_Q5_0_RDNA1;
  4783. } else if (compute_capability >= CC_VOLTA) {
  4784. mmq_x = MMQ_X_Q5_0_AMPERE;
  4785. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4786. nwarps = NWARPS_Q5_0_AMPERE;
  4787. } else if (compute_capability >= MIN_CC_DP4A) {
  4788. mmq_x = MMQ_X_Q5_0_PASCAL;
  4789. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4790. nwarps = NWARPS_Q5_0_PASCAL;
  4791. } else {
  4792. GGML_ASSERT(false);
  4793. }
  4794. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4795. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4796. const dim3 block_nums(block_num_x, block_num_y, 1);
  4797. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4798. if (nrows_x % mmq_y == 0) {
  4799. const bool need_check = false;
  4800. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4801. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4802. } else {
  4803. const bool need_check = true;
  4804. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4805. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4806. }
  4807. }
  4808. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4809. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4810. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4811. int id;
  4812. CUDA_CHECK(cudaGetDevice(&id));
  4813. const int compute_capability = g_device_caps[id].cc;
  4814. int mmq_x, mmq_y, nwarps;
  4815. if (compute_capability >= CC_RDNA2) {
  4816. mmq_x = MMQ_X_Q5_1_RDNA2;
  4817. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4818. nwarps = NWARPS_Q5_1_RDNA2;
  4819. } else if (compute_capability >= CC_OFFSET_AMD) {
  4820. mmq_x = MMQ_X_Q5_1_RDNA1;
  4821. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4822. nwarps = NWARPS_Q5_1_RDNA1;
  4823. } else if (compute_capability >= CC_VOLTA) {
  4824. mmq_x = MMQ_X_Q5_1_AMPERE;
  4825. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4826. nwarps = NWARPS_Q5_1_AMPERE;
  4827. } else if (compute_capability >= MIN_CC_DP4A) {
  4828. mmq_x = MMQ_X_Q5_1_PASCAL;
  4829. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4830. nwarps = NWARPS_Q5_1_PASCAL;
  4831. } else {
  4832. GGML_ASSERT(false);
  4833. }
  4834. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4835. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4836. const dim3 block_nums(block_num_x, block_num_y, 1);
  4837. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4838. if (nrows_x % mmq_y == 0) {
  4839. const bool need_check = false;
  4840. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4841. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4842. } else {
  4843. const bool need_check = true;
  4844. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4845. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4846. }
  4847. }
  4848. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4849. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4850. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4851. int id;
  4852. CUDA_CHECK(cudaGetDevice(&id));
  4853. const int compute_capability = g_device_caps[id].cc;
  4854. int mmq_x, mmq_y, nwarps;
  4855. if (compute_capability >= CC_RDNA2) {
  4856. mmq_x = MMQ_X_Q8_0_RDNA2;
  4857. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4858. nwarps = NWARPS_Q8_0_RDNA2;
  4859. } else if (compute_capability >= CC_OFFSET_AMD) {
  4860. mmq_x = MMQ_X_Q8_0_RDNA1;
  4861. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4862. nwarps = NWARPS_Q8_0_RDNA1;
  4863. } else if (compute_capability >= CC_VOLTA) {
  4864. mmq_x = MMQ_X_Q8_0_AMPERE;
  4865. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4866. nwarps = NWARPS_Q8_0_AMPERE;
  4867. } else if (compute_capability >= MIN_CC_DP4A) {
  4868. mmq_x = MMQ_X_Q8_0_PASCAL;
  4869. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4870. nwarps = NWARPS_Q8_0_PASCAL;
  4871. } else {
  4872. GGML_ASSERT(false);
  4873. }
  4874. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4875. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4876. const dim3 block_nums(block_num_x, block_num_y, 1);
  4877. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4878. if (nrows_x % mmq_y == 0) {
  4879. const bool need_check = false;
  4880. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4881. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4882. } else {
  4883. const bool need_check = true;
  4884. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4885. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4886. }
  4887. }
  4888. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4889. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4890. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4891. int id;
  4892. CUDA_CHECK(cudaGetDevice(&id));
  4893. const int compute_capability = g_device_caps[id].cc;
  4894. int mmq_x, mmq_y, nwarps;
  4895. if (compute_capability >= CC_RDNA2) {
  4896. mmq_x = MMQ_X_Q2_K_RDNA2;
  4897. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4898. nwarps = NWARPS_Q2_K_RDNA2;
  4899. } else if (compute_capability >= CC_OFFSET_AMD) {
  4900. mmq_x = MMQ_X_Q2_K_RDNA1;
  4901. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4902. nwarps = NWARPS_Q2_K_RDNA1;
  4903. } else if (compute_capability >= CC_VOLTA) {
  4904. mmq_x = MMQ_X_Q2_K_AMPERE;
  4905. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4906. nwarps = NWARPS_Q2_K_AMPERE;
  4907. } else if (compute_capability >= MIN_CC_DP4A) {
  4908. mmq_x = MMQ_X_Q2_K_PASCAL;
  4909. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4910. nwarps = NWARPS_Q2_K_PASCAL;
  4911. } else {
  4912. GGML_ASSERT(false);
  4913. }
  4914. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4915. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4916. const dim3 block_nums(block_num_x, block_num_y, 1);
  4917. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4918. if (nrows_x % mmq_y == 0) {
  4919. const bool need_check = false;
  4920. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4921. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4922. } else {
  4923. const bool need_check = true;
  4924. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4925. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4926. }
  4927. }
  4928. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4929. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4930. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4931. #if QK_K == 256
  4932. int id;
  4933. CUDA_CHECK(cudaGetDevice(&id));
  4934. const int compute_capability = g_device_caps[id].cc;
  4935. int mmq_x, mmq_y, nwarps;
  4936. if (compute_capability >= CC_RDNA2) {
  4937. mmq_x = MMQ_X_Q3_K_RDNA2;
  4938. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4939. nwarps = NWARPS_Q3_K_RDNA2;
  4940. } else if (compute_capability >= CC_OFFSET_AMD) {
  4941. mmq_x = MMQ_X_Q3_K_RDNA1;
  4942. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4943. nwarps = NWARPS_Q3_K_RDNA1;
  4944. } else if (compute_capability >= CC_VOLTA) {
  4945. mmq_x = MMQ_X_Q3_K_AMPERE;
  4946. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4947. nwarps = NWARPS_Q3_K_AMPERE;
  4948. } else if (compute_capability >= MIN_CC_DP4A) {
  4949. mmq_x = MMQ_X_Q3_K_PASCAL;
  4950. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4951. nwarps = NWARPS_Q3_K_PASCAL;
  4952. } else {
  4953. GGML_ASSERT(false);
  4954. }
  4955. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4956. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4957. const dim3 block_nums(block_num_x, block_num_y, 1);
  4958. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4959. if (nrows_x % mmq_y == 0) {
  4960. const bool need_check = false;
  4961. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4962. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4963. } else {
  4964. const bool need_check = true;
  4965. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4966. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4967. }
  4968. #endif
  4969. }
  4970. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4971. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4972. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4973. int id;
  4974. CUDA_CHECK(cudaGetDevice(&id));
  4975. const int compute_capability = g_device_caps[id].cc;
  4976. int mmq_x, mmq_y, nwarps;
  4977. if (compute_capability >= CC_RDNA2) {
  4978. mmq_x = MMQ_X_Q4_K_RDNA2;
  4979. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4980. nwarps = NWARPS_Q4_K_RDNA2;
  4981. } else if (compute_capability >= CC_OFFSET_AMD) {
  4982. mmq_x = MMQ_X_Q4_K_RDNA1;
  4983. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4984. nwarps = NWARPS_Q4_K_RDNA1;
  4985. } else if (compute_capability >= CC_VOLTA) {
  4986. mmq_x = MMQ_X_Q4_K_AMPERE;
  4987. mmq_y = MMQ_Y_Q4_K_AMPERE;
  4988. nwarps = NWARPS_Q4_K_AMPERE;
  4989. } else if (compute_capability >= MIN_CC_DP4A) {
  4990. mmq_x = MMQ_X_Q4_K_PASCAL;
  4991. mmq_y = MMQ_Y_Q4_K_PASCAL;
  4992. nwarps = NWARPS_Q4_K_PASCAL;
  4993. } else {
  4994. GGML_ASSERT(false);
  4995. }
  4996. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4997. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4998. const dim3 block_nums(block_num_x, block_num_y, 1);
  4999. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5000. if (nrows_x % mmq_y == 0) {
  5001. const bool need_check = false;
  5002. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5003. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5004. } else {
  5005. const bool need_check = true;
  5006. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5007. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5008. }
  5009. }
  5010. static void ggml_mul_mat_q5_K_q8_1_cuda(
  5011. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5012. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5013. int id;
  5014. CUDA_CHECK(cudaGetDevice(&id));
  5015. const int compute_capability = g_device_caps[id].cc;
  5016. int mmq_x, mmq_y, nwarps;
  5017. if (compute_capability >= CC_RDNA2) {
  5018. mmq_x = MMQ_X_Q5_K_RDNA2;
  5019. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5020. nwarps = NWARPS_Q5_K_RDNA2;
  5021. } else if (compute_capability >= CC_OFFSET_AMD) {
  5022. mmq_x = MMQ_X_Q5_K_RDNA1;
  5023. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5024. nwarps = NWARPS_Q5_K_RDNA1;
  5025. } else if (compute_capability >= CC_VOLTA) {
  5026. mmq_x = MMQ_X_Q5_K_AMPERE;
  5027. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5028. nwarps = NWARPS_Q5_K_AMPERE;
  5029. } else if (compute_capability >= MIN_CC_DP4A) {
  5030. mmq_x = MMQ_X_Q5_K_PASCAL;
  5031. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5032. nwarps = NWARPS_Q5_K_PASCAL;
  5033. } else {
  5034. GGML_ASSERT(false);
  5035. }
  5036. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5037. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5038. const dim3 block_nums(block_num_x, block_num_y, 1);
  5039. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5040. if (nrows_x % mmq_y == 0) {
  5041. const bool need_check = false;
  5042. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5043. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5044. } else {
  5045. const bool need_check = true;
  5046. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5047. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5048. }
  5049. }
  5050. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5051. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5052. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5053. int id;
  5054. CUDA_CHECK(cudaGetDevice(&id));
  5055. const int compute_capability = g_device_caps[id].cc;
  5056. int mmq_x, mmq_y, nwarps;
  5057. if (compute_capability >= CC_RDNA2) {
  5058. mmq_x = MMQ_X_Q6_K_RDNA2;
  5059. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5060. nwarps = NWARPS_Q6_K_RDNA2;
  5061. } else if (compute_capability >= CC_OFFSET_AMD) {
  5062. mmq_x = MMQ_X_Q6_K_RDNA1;
  5063. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5064. nwarps = NWARPS_Q6_K_RDNA1;
  5065. } else if (compute_capability >= CC_VOLTA) {
  5066. mmq_x = MMQ_X_Q6_K_AMPERE;
  5067. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5068. nwarps = NWARPS_Q6_K_AMPERE;
  5069. } else if (compute_capability >= MIN_CC_DP4A) {
  5070. mmq_x = MMQ_X_Q6_K_PASCAL;
  5071. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5072. nwarps = NWARPS_Q6_K_PASCAL;
  5073. } else {
  5074. GGML_ASSERT(false);
  5075. }
  5076. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5077. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5078. const dim3 block_nums(block_num_x, block_num_y, 1);
  5079. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5080. if (nrows_x % mmq_y == 0) {
  5081. const bool need_check = false;
  5082. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5083. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5084. } else {
  5085. const bool need_check = true;
  5086. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5087. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5088. }
  5089. }
  5090. static void ggml_mul_mat_p021_f16_f32_cuda(
  5091. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5092. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5093. const dim3 block_nums(1, nrows_x, nchannels_y);
  5094. const dim3 block_dims(WARP_SIZE, 1, 1);
  5095. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5096. }
  5097. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5098. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5099. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5100. const dim3 block_nums(1, nrows_x, nchannels_y);
  5101. const dim3 block_dims(WARP_SIZE, 1, 1);
  5102. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5103. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5104. }
  5105. static void ggml_cpy_f32_f32_cuda(
  5106. const char * cx, char * cdst, const int ne,
  5107. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5108. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5109. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5110. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5111. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5112. }
  5113. static void ggml_cpy_f32_f16_cuda(
  5114. const char * cx, char * cdst, const int ne,
  5115. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5116. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5117. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5118. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5119. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5120. }
  5121. static void ggml_cpy_f32_q8_0_cuda(
  5122. const char * cx, char * cdst, const int ne,
  5123. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5124. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5125. GGML_ASSERT(ne % QK8_0 == 0);
  5126. const int num_blocks = ne / QK8_0;
  5127. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5128. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5129. }
  5130. static void ggml_cpy_f32_q4_0_cuda(
  5131. const char * cx, char * cdst, const int ne,
  5132. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5133. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5134. GGML_ASSERT(ne % QK4_0 == 0);
  5135. const int num_blocks = ne / QK4_0;
  5136. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5137. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5138. }
  5139. static void ggml_cpy_f32_q4_1_cuda(
  5140. const char * cx, char * cdst, const int ne,
  5141. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5142. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5143. GGML_ASSERT(ne % QK4_1 == 0);
  5144. const int num_blocks = ne / QK4_1;
  5145. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5146. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5147. }
  5148. static void ggml_cpy_f16_f16_cuda(
  5149. const char * cx, char * cdst, const int ne,
  5150. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5151. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5152. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5153. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5154. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5155. }
  5156. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5157. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5158. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5159. }
  5160. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5161. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5162. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5163. }
  5164. template<typename T>
  5165. static void rope_cuda(
  5166. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5167. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5168. ) {
  5169. GGML_ASSERT(ncols % 2 == 0);
  5170. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5171. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5172. const dim3 block_nums(nrows, num_blocks_x, 1);
  5173. if (pos == nullptr) {
  5174. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5175. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5176. );
  5177. } else {
  5178. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5179. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5180. );
  5181. }
  5182. }
  5183. template<typename T>
  5184. static void rope_neox_cuda(
  5185. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5186. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5187. ) {
  5188. GGML_ASSERT(ncols % 2 == 0);
  5189. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5190. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5191. const dim3 block_nums(nrows, num_blocks_x, 1);
  5192. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5193. const float inv_ndims = -1.0f / n_dims;
  5194. if (pos == nullptr) {
  5195. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5196. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5197. theta_scale, inv_ndims
  5198. );
  5199. } else {
  5200. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5201. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5202. theta_scale, inv_ndims
  5203. );
  5204. }
  5205. }
  5206. static void rope_glm_f32_cuda(
  5207. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5208. float freq_base, int n_ctx, cudaStream_t stream
  5209. ) {
  5210. GGML_ASSERT(ncols % 4 == 0);
  5211. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5212. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5213. const dim3 block_nums(num_blocks_x, nrows, 1);
  5214. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5215. }
  5216. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5217. const int k_rows, const int n_heads_log2_floor, const float m0,
  5218. const float m1, cudaStream_t stream) {
  5219. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5220. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5221. const dim3 block_nums(num_blocks_x, nrows, 1);
  5222. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5223. }
  5224. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5225. const dim3 block_dims(WARP_SIZE, 1, 1);
  5226. const dim3 block_nums(1, nrows, 1);
  5227. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5228. }
  5229. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5230. // bitonic sort requires ncols to be power of 2
  5231. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5232. const dim3 block_dims(ncols, 1, 1);
  5233. const dim3 block_nums(1, nrows, 1);
  5234. if (order == GGML_SORT_ASC) {
  5235. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5236. } else if (order == GGML_SORT_DESC) {
  5237. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5238. } else {
  5239. GGML_ASSERT(false);
  5240. }
  5241. }
  5242. 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) {
  5243. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5244. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5245. const dim3 block_nums(nrows_x, block_num_x, 1);
  5246. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5247. }
  5248. 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) {
  5249. int nth = WARP_SIZE;
  5250. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5251. const dim3 block_dims(nth, 1, 1);
  5252. const dim3 block_nums(nrows_x, 1, 1);
  5253. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5254. }
  5255. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5256. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5257. int offset_delta,
  5258. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5259. const int parallel_elements = OW * KW * KH;
  5260. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5261. dim3 block_nums(num_blocks, OH, IC);
  5262. im2col_f32_f16<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, offset_delta, IW, IH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  5263. }
  5264. // buffer pool for cuda
  5265. #define MAX_CUDA_BUFFERS 256
  5266. struct scoped_spin_lock {
  5267. std::atomic_flag& lock;
  5268. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5269. while (lock.test_and_set(std::memory_order_acquire)) {
  5270. ; // spin
  5271. }
  5272. }
  5273. ~scoped_spin_lock() {
  5274. lock.clear(std::memory_order_release);
  5275. }
  5276. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5277. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5278. };
  5279. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5280. // #define DEBUG_CUDA_MALLOC
  5281. struct cuda_buffer {
  5282. void * ptr = nullptr;
  5283. size_t size = 0;
  5284. };
  5285. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5286. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  5287. static void * ggml_cuda_pool_malloc_leg(size_t size, size_t * actual_size) {
  5288. scoped_spin_lock lock(g_cuda_pool_lock);
  5289. int id;
  5290. CUDA_CHECK(cudaGetDevice(&id));
  5291. #ifdef DEBUG_CUDA_MALLOC
  5292. int nnz = 0;
  5293. size_t max_size = 0;
  5294. #endif
  5295. size_t best_diff = 1ull << 36;
  5296. int ibest = -1;
  5297. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5298. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5299. if (b.ptr != nullptr) {
  5300. #ifdef DEBUG_CUDA_MALLOC
  5301. ++nnz;
  5302. if (b.size > max_size) max_size = b.size;
  5303. #endif
  5304. if (b.size >= size) {
  5305. size_t diff = b.size - size;
  5306. if (diff < best_diff) {
  5307. best_diff = diff;
  5308. ibest = i;
  5309. if (!best_diff) {
  5310. void * ptr = b.ptr;
  5311. *actual_size = b.size;
  5312. b.ptr = nullptr;
  5313. b.size = 0;
  5314. return ptr;
  5315. }
  5316. }
  5317. }
  5318. }
  5319. }
  5320. if (ibest >= 0) {
  5321. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  5322. void * ptr = b.ptr;
  5323. *actual_size = b.size;
  5324. b.ptr = nullptr;
  5325. b.size = 0;
  5326. return ptr;
  5327. }
  5328. void * ptr;
  5329. size_t look_ahead_size = (size_t) (1.05 * size);
  5330. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5331. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5332. *actual_size = look_ahead_size;
  5333. g_cuda_pool_size[id] += look_ahead_size;
  5334. #ifdef DEBUG_CUDA_MALLOC
  5335. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  5336. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  5337. #endif
  5338. return ptr;
  5339. }
  5340. static void ggml_cuda_pool_free_leg(void * ptr, size_t size) {
  5341. scoped_spin_lock lock(g_cuda_pool_lock);
  5342. int id;
  5343. CUDA_CHECK(cudaGetDevice(&id));
  5344. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5345. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5346. if (b.ptr == nullptr) {
  5347. b.ptr = ptr;
  5348. b.size = size;
  5349. return;
  5350. }
  5351. }
  5352. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5353. CUDA_CHECK(cudaFree(ptr));
  5354. g_cuda_pool_size[id] -= size;
  5355. }
  5356. #if !defined(GGML_USE_HIPBLAS)
  5357. // pool with virtual memory
  5358. static std::vector<CUmemGenericAllocationHandle> g_cuda_pool_handles[GGML_CUDA_MAX_DEVICES];
  5359. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  5360. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  5361. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 36; // 64 GB
  5362. static void * ggml_cuda_pool_malloc_vmm(size_t size, size_t * actual_size) {
  5363. scoped_spin_lock lock(g_cuda_pool_lock);
  5364. int id;
  5365. CUDA_CHECK(cudaGetDevice(&id));
  5366. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  5367. const size_t alignment = 128;
  5368. size = alignment * ((size + alignment - 1) / alignment);
  5369. size_t avail = g_cuda_pool_size[id] - g_cuda_pool_used[id];
  5370. if (size > avail) {
  5371. // round up to the next multiple of the granularity
  5372. size_t reserve_size = size - avail;
  5373. const size_t granularity = g_device_caps[id].vmm_granularity;
  5374. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  5375. GGML_ASSERT(g_cuda_pool_size[id] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  5376. // allocate more physical memory
  5377. CUmemAllocationProp prop = {};
  5378. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5379. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5380. prop.location.id = id;
  5381. CUmemGenericAllocationHandle handle;
  5382. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  5383. // reserve virtual address space (if not already reserved)
  5384. if (g_cuda_pool_addr[id] == 0) {
  5385. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[id], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  5386. }
  5387. // map at the end of the pool
  5388. CU_CHECK(cuMemMap(g_cuda_pool_addr[id] + g_cuda_pool_size[id], reserve_size, 0, handle, 0));
  5389. // set access
  5390. CUmemAccessDesc access = {};
  5391. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5392. access.location.id = id;
  5393. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  5394. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[id] + g_cuda_pool_size[id], reserve_size, &access, 1));
  5395. // add to the pool
  5396. g_cuda_pool_handles[id].push_back(handle);
  5397. g_cuda_pool_size[id] += reserve_size;
  5398. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  5399. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  5400. // (unsigned long long) (reserve_size/1024/1024));
  5401. }
  5402. GGML_ASSERT(g_cuda_pool_addr[id] != 0);
  5403. void * ptr = (void *) (g_cuda_pool_addr[id] + g_cuda_pool_used[id]);
  5404. *actual_size = size;
  5405. g_cuda_pool_used[id] += size;
  5406. #ifdef DEBUG_CUDA_MALLOC
  5407. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  5408. #endif
  5409. return ptr;
  5410. }
  5411. static void ggml_cuda_pool_free_vmm(void * ptr, size_t size) {
  5412. scoped_spin_lock lock(g_cuda_pool_lock);
  5413. int id;
  5414. CUDA_CHECK(cudaGetDevice(&id));
  5415. #ifdef DEBUG_CUDA_MALLOC
  5416. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  5417. #endif
  5418. g_cuda_pool_used[id] -= size;
  5419. // all deallocations must be in reverse order of the allocations
  5420. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[id] + g_cuda_pool_used[id]));
  5421. }
  5422. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  5423. int id;
  5424. CUDA_CHECK(cudaGetDevice(&id));
  5425. if (g_device_caps[id].vmm) {
  5426. return ggml_cuda_pool_malloc_vmm(size, actual_size);
  5427. } else {
  5428. return ggml_cuda_pool_malloc_leg(size, actual_size);
  5429. }
  5430. }
  5431. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  5432. int id;
  5433. CUDA_CHECK(cudaGetDevice(&id));
  5434. if (g_device_caps[id].vmm) {
  5435. ggml_cuda_pool_free_vmm(ptr, size);
  5436. } else {
  5437. ggml_cuda_pool_free_leg(ptr, size);
  5438. }
  5439. }
  5440. #else
  5441. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  5442. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  5443. #endif // !defined(GGML_USE_HIPBLAS)
  5444. template<typename T>
  5445. struct cuda_pool_alloc {
  5446. T * ptr = nullptr;
  5447. size_t actual_size = 0;
  5448. // size is in number of elements
  5449. T * alloc(size_t size) {
  5450. GGML_ASSERT(ptr == nullptr);
  5451. ptr = (T *) ggml_cuda_pool_malloc(size * sizeof(T), &this->actual_size);
  5452. return ptr;
  5453. }
  5454. cuda_pool_alloc(size_t size) {
  5455. alloc(size);
  5456. }
  5457. ~cuda_pool_alloc() {
  5458. if (ptr != nullptr) {
  5459. ggml_cuda_pool_free(ptr, actual_size);
  5460. }
  5461. }
  5462. T * get() {
  5463. return ptr;
  5464. }
  5465. cuda_pool_alloc() = default;
  5466. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  5467. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  5468. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  5469. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  5470. };
  5471. static bool g_cublas_loaded = false;
  5472. bool ggml_cublas_loaded(void) {
  5473. return g_cublas_loaded;
  5474. }
  5475. void ggml_init_cublas() {
  5476. static bool initialized = false;
  5477. if (!initialized) {
  5478. #ifdef __HIP_PLATFORM_AMD__
  5479. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5480. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5481. rocblas_initialize();
  5482. CUDA_CHECK(cudaDeviceSynchronize());
  5483. #endif
  5484. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5485. initialized = true;
  5486. g_cublas_loaded = false;
  5487. return;
  5488. }
  5489. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5490. int64_t total_vram = 0;
  5491. #if defined(GGML_CUDA_FORCE_MMQ)
  5492. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5493. #else
  5494. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5495. #endif
  5496. #if defined(CUDA_USE_TENSOR_CORES)
  5497. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5498. #else
  5499. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5500. #endif
  5501. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5502. for (int id = 0; id < g_device_count; ++id) {
  5503. int device_vmm = 0;
  5504. #if !defined(GGML_USE_HIPBLAS)
  5505. CUdevice device;
  5506. CU_CHECK(cuDeviceGet(&device, id));
  5507. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  5508. if (device_vmm) {
  5509. CUmemAllocationProp alloc_prop = {};
  5510. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5511. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5512. alloc_prop.location.id = id;
  5513. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM));
  5514. }
  5515. #endif // !defined(GGML_USE_HIPBLAS)
  5516. g_device_caps[id].vmm = !!device_vmm;
  5517. cudaDeviceProp prop;
  5518. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5519. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  5520. g_tensor_split[id] = total_vram;
  5521. total_vram += prop.totalGlobalMem;
  5522. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5523. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5524. #else
  5525. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  5526. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5527. }
  5528. for (int id = 0; id < g_device_count; ++id) {
  5529. g_tensor_split[id] /= total_vram;
  5530. }
  5531. for (int id = 0; id < g_device_count; ++id) {
  5532. CUDA_CHECK(ggml_cuda_set_device(id));
  5533. // create cuda streams
  5534. for (int is = 0; is < MAX_STREAMS; ++is) {
  5535. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5536. }
  5537. // create cublas handle
  5538. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5539. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5540. }
  5541. // configure logging to stdout
  5542. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5543. initialized = true;
  5544. g_cublas_loaded = true;
  5545. }
  5546. }
  5547. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5548. if (tensor_split == nullptr) {
  5549. return;
  5550. }
  5551. bool all_zero = true;
  5552. for (int i = 0; i < g_device_count; ++i) {
  5553. if (tensor_split[i] != 0.0f) {
  5554. all_zero = false;
  5555. break;
  5556. }
  5557. }
  5558. if (all_zero) {
  5559. return;
  5560. }
  5561. float split_sum = 0.0f;
  5562. for (int i = 0; i < g_device_count; ++i) {
  5563. g_tensor_split[i] = split_sum;
  5564. split_sum += tensor_split[i];
  5565. }
  5566. for (int i = 0; i < g_device_count; ++i) {
  5567. g_tensor_split[i] /= split_sum;
  5568. }
  5569. }
  5570. void * ggml_cuda_host_malloc(size_t size) {
  5571. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  5572. return nullptr;
  5573. }
  5574. void * ptr = nullptr;
  5575. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  5576. if (err != cudaSuccess) {
  5577. // clear the error
  5578. cudaGetLastError();
  5579. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  5580. size/1024.0/1024.0, cudaGetErrorString(err));
  5581. return nullptr;
  5582. }
  5583. return ptr;
  5584. }
  5585. void ggml_cuda_host_free(void * ptr) {
  5586. CUDA_CHECK(cudaFreeHost(ptr));
  5587. }
  5588. static cudaError_t ggml_cuda_cpy_tensor_2d(
  5589. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  5590. cudaMemcpyKind kind;
  5591. char * src_ptr;
  5592. if (src->backend == GGML_BACKEND_CPU) {
  5593. kind = cudaMemcpyHostToDevice;
  5594. src_ptr = (char *) src->data;
  5595. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  5596. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  5597. kind = cudaMemcpyDeviceToDevice;
  5598. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  5599. int id;
  5600. CUDA_CHECK(cudaGetDevice(&id));
  5601. src_ptr = (char *) extra->data_device[id];
  5602. } else {
  5603. GGML_ASSERT(false);
  5604. }
  5605. char * dst_ptr = (char *) dst;
  5606. const int64_t ne0 = src->ne[0];
  5607. const int64_t nb0 = src->nb[0];
  5608. const int64_t nb1 = src->nb[1];
  5609. const int64_t nb2 = src->nb[2];
  5610. const int64_t nb3 = src->nb[3];
  5611. const enum ggml_type type = src->type;
  5612. const int64_t ts = ggml_type_size(type);
  5613. const int64_t bs = ggml_blck_size(type);
  5614. int64_t i1_diff = i1_high - i1_low;
  5615. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  5616. if (nb0 == ts && nb1 == ts*ne0/bs) {
  5617. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  5618. } else if (nb0 == ts) {
  5619. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  5620. } else {
  5621. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  5622. const void * rx = (const void *) ((const char *) x + i1*nb1);
  5623. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  5624. // pretend the row is a matrix with cols=1
  5625. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  5626. if (r != cudaSuccess) return r;
  5627. }
  5628. return cudaSuccess;
  5629. }
  5630. }
  5631. static void ggml_cuda_op_get_rows(
  5632. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5633. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & stream) {
  5634. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5635. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5636. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  5637. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  5638. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  5639. const int32_t * src1_i32 = (const int32_t *) src1_d;
  5640. switch (src0->type) {
  5641. case GGML_TYPE_F16:
  5642. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  5643. break;
  5644. case GGML_TYPE_F32:
  5645. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5646. break;
  5647. case GGML_TYPE_Q4_0:
  5648. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5649. break;
  5650. case GGML_TYPE_Q4_1:
  5651. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5652. break;
  5653. case GGML_TYPE_Q5_0:
  5654. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5655. break;
  5656. case GGML_TYPE_Q5_1:
  5657. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5658. break;
  5659. case GGML_TYPE_Q8_0:
  5660. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5661. break;
  5662. default:
  5663. // TODO: k-quants
  5664. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  5665. GGML_ASSERT(false);
  5666. break;
  5667. }
  5668. }
  5669. template<class op>
  5670. inline void ggml_cuda_op_bin_bcast(
  5671. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5672. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5673. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5674. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5675. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5676. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5677. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  5678. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5679. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  5680. } else {
  5681. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  5682. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  5683. GGML_ASSERT(false);
  5684. }
  5685. }
  5686. static void ggml_cuda_op_repeat(
  5687. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5688. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & main_stream) {
  5689. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  5690. (void) src1;
  5691. (void) src1_d;
  5692. }
  5693. inline void ggml_cuda_op_add(
  5694. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5695. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5696. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5697. }
  5698. inline void ggml_cuda_op_acc(
  5699. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5700. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5701. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5702. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5703. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5704. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  5705. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5706. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5707. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5708. int offset = dst->op_params[3] / 4; // offset in bytes
  5709. acc_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  5710. (void) dst;
  5711. }
  5712. inline void ggml_cuda_op_mul(
  5713. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5714. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5715. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5716. }
  5717. inline void ggml_cuda_op_div(
  5718. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5719. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5720. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5721. }
  5722. inline void ggml_cuda_op_gelu(
  5723. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5724. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5725. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5726. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5727. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5728. (void) src1;
  5729. (void) dst;
  5730. (void) src1_dd;
  5731. }
  5732. inline void ggml_cuda_op_silu(
  5733. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5734. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5735. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5736. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5737. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5738. (void) src1;
  5739. (void) dst;
  5740. (void) src1_dd;
  5741. }
  5742. inline void ggml_cuda_op_gelu_quick(
  5743. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5744. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5745. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5746. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5747. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5748. (void) src1;
  5749. (void) dst;
  5750. (void) src1_dd;
  5751. }
  5752. inline void ggml_cuda_op_tanh(
  5753. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5754. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5755. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5756. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5757. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5758. (void) src1;
  5759. (void) dst;
  5760. (void) src1_dd;
  5761. }
  5762. inline void ggml_cuda_op_relu(
  5763. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5764. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5765. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5766. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5767. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5768. (void) src1;
  5769. (void) dst;
  5770. (void) src1_dd;
  5771. }
  5772. inline void ggml_cuda_op_leaky_relu(
  5773. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5774. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5775. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5776. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5777. float negative_slope;
  5778. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5779. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  5780. (void) src1;
  5781. (void) dst;
  5782. (void) src1_dd;
  5783. }
  5784. inline void ggml_cuda_op_sqr(
  5785. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5786. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5787. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5788. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5789. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5790. (void) src1;
  5791. (void) dst;
  5792. (void) src1_dd;
  5793. }
  5794. inline void ggml_cuda_op_norm(
  5795. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5796. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5797. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5798. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5799. const int64_t ne00 = src0->ne[0];
  5800. const int64_t nrows = ggml_nrows(src0);
  5801. float eps;
  5802. memcpy(&eps, dst->op_params, sizeof(float));
  5803. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5804. (void) src1;
  5805. (void) dst;
  5806. (void) src1_dd;
  5807. }
  5808. inline void ggml_cuda_op_group_norm(
  5809. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5810. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5811. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5812. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5813. int num_groups = dst->op_params[0];
  5814. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5815. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  5816. (void) src1;
  5817. (void) dst;
  5818. (void) src1_dd;
  5819. }
  5820. inline void ggml_cuda_op_concat(
  5821. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5822. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5823. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5824. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5825. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5826. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  5827. concat_f32_cuda(src0_dd + i3 * (src0->nb[3] / 4), src1_dd + i3 * (src1->nb[3] / 4), dst_dd + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], main_stream);
  5828. }
  5829. (void) src1;
  5830. (void) dst;
  5831. }
  5832. inline void ggml_cuda_op_upscale(
  5833. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5834. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5835. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5836. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5837. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5838. const int scale_factor = dst->op_params[0];
  5839. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  5840. (void) src1;
  5841. (void) dst;
  5842. (void) src1_dd;
  5843. }
  5844. inline void ggml_cuda_op_pad(
  5845. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5846. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5847. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5848. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5849. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5850. pad_f32_cuda(src0_dd, dst_dd,
  5851. src0->ne[0], src0->ne[1], src0->ne[2],
  5852. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  5853. (void) src1;
  5854. (void) dst;
  5855. (void) src1_dd;
  5856. }
  5857. inline void ggml_cuda_op_rms_norm(
  5858. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5859. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5860. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5861. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5862. const int64_t ne00 = src0->ne[0];
  5863. const int64_t nrows = ggml_nrows(src0);
  5864. float eps;
  5865. memcpy(&eps, dst->op_params, sizeof(float));
  5866. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5867. (void) src1;
  5868. (void) dst;
  5869. (void) src1_dd;
  5870. }
  5871. inline void ggml_cuda_op_mul_mat_q(
  5872. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5873. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5874. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5875. const int64_t ne00 = src0->ne[0];
  5876. const int64_t ne10 = src1->ne[0];
  5877. GGML_ASSERT(ne10 % QK8_1 == 0);
  5878. const int64_t ne0 = dst->ne[0];
  5879. const int64_t row_diff = row_high - row_low;
  5880. int id;
  5881. CUDA_CHECK(cudaGetDevice(&id));
  5882. // the main device has a larger memory buffer to hold the results from all GPUs
  5883. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5884. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5885. switch (src0->type) {
  5886. case GGML_TYPE_Q4_0:
  5887. 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);
  5888. break;
  5889. case GGML_TYPE_Q4_1:
  5890. 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);
  5891. break;
  5892. case GGML_TYPE_Q5_0:
  5893. 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);
  5894. break;
  5895. case GGML_TYPE_Q5_1:
  5896. 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);
  5897. break;
  5898. case GGML_TYPE_Q8_0:
  5899. 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);
  5900. break;
  5901. case GGML_TYPE_Q2_K:
  5902. 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);
  5903. break;
  5904. case GGML_TYPE_Q3_K:
  5905. 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);
  5906. break;
  5907. case GGML_TYPE_Q4_K:
  5908. 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);
  5909. break;
  5910. case GGML_TYPE_Q5_K:
  5911. 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);
  5912. break;
  5913. case GGML_TYPE_Q6_K:
  5914. 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);
  5915. break;
  5916. default:
  5917. GGML_ASSERT(false);
  5918. break;
  5919. }
  5920. (void) src1;
  5921. (void) dst;
  5922. (void) src1_ddf_i;
  5923. }
  5924. static int64_t get_row_rounding(ggml_type type) {
  5925. int64_t min_compute_capability = INT_MAX;
  5926. int64_t max_compute_capability = INT_MIN;
  5927. for (int64_t id = 0; id < g_device_count; ++id) {
  5928. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5929. if (min_compute_capability > g_device_caps[id].cc) {
  5930. min_compute_capability = g_device_caps[id].cc;
  5931. }
  5932. if (max_compute_capability < g_device_caps[id].cc) {
  5933. max_compute_capability = g_device_caps[id].cc;
  5934. }
  5935. }
  5936. }
  5937. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5938. switch(type) {
  5939. case GGML_TYPE_Q4_0:
  5940. case GGML_TYPE_Q4_1:
  5941. case GGML_TYPE_Q5_0:
  5942. case GGML_TYPE_Q5_1:
  5943. case GGML_TYPE_Q8_0:
  5944. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5945. case GGML_TYPE_F16:
  5946. case GGML_TYPE_F32:
  5947. return 1;
  5948. case GGML_TYPE_Q2_K:
  5949. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5950. case GGML_TYPE_Q3_K:
  5951. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5952. case GGML_TYPE_Q4_K:
  5953. case GGML_TYPE_Q5_K:
  5954. case GGML_TYPE_Q6_K:
  5955. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5956. default:
  5957. GGML_ASSERT(false);
  5958. }
  5959. #else
  5960. switch(type) {
  5961. case GGML_TYPE_Q4_0:
  5962. case GGML_TYPE_Q4_1:
  5963. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5964. case GGML_TYPE_Q5_0:
  5965. case GGML_TYPE_Q5_1:
  5966. case GGML_TYPE_Q8_0:
  5967. return 64;
  5968. case GGML_TYPE_F16:
  5969. case GGML_TYPE_F32:
  5970. return 1;
  5971. case GGML_TYPE_Q2_K:
  5972. case GGML_TYPE_Q3_K:
  5973. case GGML_TYPE_Q4_K:
  5974. case GGML_TYPE_Q5_K:
  5975. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5976. case GGML_TYPE_Q6_K:
  5977. return 64;
  5978. default:
  5979. GGML_ASSERT(false);
  5980. }
  5981. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5982. }
  5983. inline void ggml_cuda_op_mul_mat_vec_q(
  5984. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5985. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5986. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5987. GGML_ASSERT(ggml_nrows(src1) == 1);
  5988. const int64_t ne00 = src0->ne[0];
  5989. const int64_t row_diff = row_high - row_low;
  5990. switch (src0->type) {
  5991. case GGML_TYPE_Q4_0:
  5992. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5993. break;
  5994. case GGML_TYPE_Q4_1:
  5995. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5996. break;
  5997. case GGML_TYPE_Q5_0:
  5998. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5999. break;
  6000. case GGML_TYPE_Q5_1:
  6001. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6002. break;
  6003. case GGML_TYPE_Q8_0:
  6004. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6005. break;
  6006. case GGML_TYPE_Q2_K:
  6007. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6008. break;
  6009. case GGML_TYPE_Q3_K:
  6010. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6011. break;
  6012. case GGML_TYPE_Q4_K:
  6013. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6014. break;
  6015. case GGML_TYPE_Q5_K:
  6016. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6017. break;
  6018. case GGML_TYPE_Q6_K:
  6019. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6020. break;
  6021. default:
  6022. GGML_ASSERT(false);
  6023. break;
  6024. }
  6025. (void) src1;
  6026. (void) dst;
  6027. (void) src1_ddf_i;
  6028. (void) src1_ncols;
  6029. (void) src1_padded_row_size;
  6030. }
  6031. inline void ggml_cuda_op_dequantize_mul_mat_vec(
  6032. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6033. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6034. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  6035. const int64_t ne00 = src0->ne[0];
  6036. const int64_t row_diff = row_high - row_low;
  6037. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  6038. #ifdef GGML_CUDA_F16
  6039. cuda_pool_alloc<half> src1_dfloat_a;
  6040. half * src1_dfloat = nullptr; // dfloat == half
  6041. bool src1_convert_f16 =
  6042. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  6043. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  6044. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  6045. if (src1_convert_f16) {
  6046. src1_dfloat = src1_dfloat_a.alloc(ne00);
  6047. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  6048. ne00, 1, sizeof(float), 0, 0,
  6049. ne00, 1, sizeof(half), 0, 0, stream);
  6050. }
  6051. #else
  6052. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  6053. #endif // GGML_CUDA_F16
  6054. switch (src0->type) {
  6055. case GGML_TYPE_Q4_0:
  6056. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6057. break;
  6058. case GGML_TYPE_Q4_1:
  6059. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6060. break;
  6061. case GGML_TYPE_Q5_0:
  6062. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6063. break;
  6064. case GGML_TYPE_Q5_1:
  6065. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6066. break;
  6067. case GGML_TYPE_Q8_0:
  6068. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6069. break;
  6070. case GGML_TYPE_Q2_K:
  6071. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6072. break;
  6073. case GGML_TYPE_Q3_K:
  6074. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6075. break;
  6076. case GGML_TYPE_Q4_K:
  6077. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6078. break;
  6079. case GGML_TYPE_Q5_K:
  6080. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6081. break;
  6082. case GGML_TYPE_Q6_K:
  6083. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6084. break;
  6085. case GGML_TYPE_F16:
  6086. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6087. break;
  6088. default:
  6089. GGML_ASSERT(false);
  6090. break;
  6091. }
  6092. (void) src1;
  6093. (void) dst;
  6094. (void) src1_ddq_i;
  6095. (void) src1_ncols;
  6096. (void) src1_padded_row_size;
  6097. }
  6098. inline void ggml_cuda_op_mul_mat_cublas(
  6099. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6100. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6101. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  6102. GGML_ASSERT(src0_dd_i != nullptr);
  6103. GGML_ASSERT(src1_ddf_i != nullptr);
  6104. GGML_ASSERT(dst_dd_i != nullptr);
  6105. const int64_t ne00 = src0->ne[0];
  6106. const int64_t ne10 = src1->ne[0];
  6107. const int64_t ne0 = dst->ne[0];
  6108. const int64_t row_diff = row_high - row_low;
  6109. int id;
  6110. CUDA_CHECK(cudaGetDevice(&id));
  6111. // the main device has a larger memory buffer to hold the results from all GPUs
  6112. // ldc == nrows of the matrix that cuBLAS writes into
  6113. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6114. const int compute_capability = g_device_caps[id].cc;
  6115. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  6116. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  6117. cuda_pool_alloc<half> src0_as_f16;
  6118. if (src0->type != GGML_TYPE_F16) {
  6119. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  6120. GGML_ASSERT(to_fp16_cuda != nullptr);
  6121. size_t ne = row_diff*ne00;
  6122. src0_as_f16.alloc(ne);
  6123. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  6124. }
  6125. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  6126. cuda_pool_alloc<half> src1_as_f16;
  6127. if (src1->type != GGML_TYPE_F16) {
  6128. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6129. GGML_ASSERT(to_fp16_cuda != nullptr);
  6130. size_t ne = src1_ncols*ne10;
  6131. src1_as_f16.alloc(ne);
  6132. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  6133. }
  6134. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  6135. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  6136. const half alpha_f16 = 1.0f;
  6137. const half beta_f16 = 0.0f;
  6138. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6139. CUBLAS_CHECK(
  6140. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6141. row_diff, src1_ncols, ne10,
  6142. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6143. src1_ptr, CUDA_R_16F, ne10,
  6144. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  6145. CUBLAS_COMPUTE_16F,
  6146. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6147. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6148. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  6149. }
  6150. else {
  6151. cuda_pool_alloc<float> src0_ddq_as_f32;
  6152. if (src0->type != GGML_TYPE_F32) {
  6153. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6154. GGML_ASSERT(to_fp32_cuda != nullptr);
  6155. src0_ddq_as_f32.alloc(row_diff*ne00);
  6156. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  6157. }
  6158. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  6159. const float alpha = 1.0f;
  6160. const float beta = 0.0f;
  6161. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6162. CUBLAS_CHECK(
  6163. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6164. row_diff, src1_ncols, ne10,
  6165. &alpha, src0_ddf_i, ne00,
  6166. src1_ddf_i, ne10,
  6167. &beta, dst_dd_i, ldc));
  6168. }
  6169. (void) dst;
  6170. (void) src1_ddq_i;
  6171. (void) src1_padded_row_size;
  6172. }
  6173. inline void ggml_cuda_op_rope(
  6174. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6175. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6176. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6177. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6178. GGML_ASSERT(src0->type == dst->type);
  6179. const int64_t ne00 = src0->ne[0];
  6180. const int64_t ne01 = src0->ne[1];
  6181. const int64_t ne2 = dst->ne[2];
  6182. const int64_t nrows = ggml_nrows(src0);
  6183. //const int n_past = ((int32_t *) dst->op_params)[0];
  6184. const int n_dims = ((int32_t *) dst->op_params)[1];
  6185. const int mode = ((int32_t *) dst->op_params)[2];
  6186. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6187. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6188. // RoPE alteration for extended context
  6189. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6190. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6191. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6192. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6193. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6194. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6195. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6196. const int32_t * pos = nullptr;
  6197. if ((mode & 1) == 0) {
  6198. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6199. GGML_ASSERT(src1->ne[0] == ne2);
  6200. pos = (const int32_t *) src1_dd;
  6201. }
  6202. const bool is_neox = mode & 2;
  6203. const bool is_glm = mode & 4;
  6204. rope_corr_dims corr_dims;
  6205. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6206. // compute
  6207. if (is_glm) {
  6208. GGML_ASSERT(false);
  6209. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6210. } else if (is_neox) {
  6211. if (src0->type == GGML_TYPE_F32) {
  6212. rope_neox_cuda(
  6213. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6214. attn_factor, corr_dims, main_stream
  6215. );
  6216. } else if (src0->type == GGML_TYPE_F16) {
  6217. rope_neox_cuda(
  6218. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6219. attn_factor, corr_dims, main_stream
  6220. );
  6221. } else {
  6222. GGML_ASSERT(false);
  6223. }
  6224. } else {
  6225. if (src0->type == GGML_TYPE_F32) {
  6226. rope_cuda(
  6227. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6228. attn_factor, corr_dims, main_stream
  6229. );
  6230. } else if (src0->type == GGML_TYPE_F16) {
  6231. rope_cuda(
  6232. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6233. attn_factor, corr_dims, main_stream
  6234. );
  6235. } else {
  6236. GGML_ASSERT(false);
  6237. }
  6238. }
  6239. (void) src1;
  6240. (void) dst;
  6241. (void) src1_dd;
  6242. }
  6243. inline void ggml_cuda_op_alibi(
  6244. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6245. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6246. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6247. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6248. const int64_t ne00 = src0->ne[0];
  6249. const int64_t ne01 = src0->ne[1];
  6250. const int64_t ne02 = src0->ne[2];
  6251. const int64_t nrows = ggml_nrows(src0);
  6252. //const int n_past = ((int32_t *) dst->op_params)[0];
  6253. const int n_head = ((int32_t *) dst->op_params)[1];
  6254. float max_bias;
  6255. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6256. //GGML_ASSERT(ne01 + n_past == ne00);
  6257. GGML_ASSERT(n_head == ne02);
  6258. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6259. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6260. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6261. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6262. (void) src1;
  6263. (void) src1_dd;
  6264. }
  6265. inline void ggml_cuda_op_im2col(
  6266. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6267. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6268. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6269. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6270. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6271. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6272. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6273. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6274. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6275. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6276. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6277. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6278. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6279. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6280. const int64_t IW = src1->ne[0];
  6281. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6282. const int64_t KW = src0->ne[0];
  6283. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6284. const int64_t OW = dst->ne[1];
  6285. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6286. im2col_f32_f16_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  6287. (void) src0;
  6288. (void) src0_dd;
  6289. }
  6290. inline void ggml_cuda_op_sum_rows(
  6291. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6292. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6293. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6294. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6295. const int64_t ncols = src0->ne[0];
  6296. const int64_t nrows = ggml_nrows(src0);
  6297. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6298. (void) src1;
  6299. (void) dst;
  6300. (void) src1_dd;
  6301. }
  6302. inline void ggml_cuda_op_argsort(
  6303. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6304. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6305. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6306. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6307. const int64_t ncols = src0->ne[0];
  6308. const int64_t nrows = ggml_nrows(src0);
  6309. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6310. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6311. (void) src1;
  6312. (void) dst;
  6313. (void) src1_dd;
  6314. }
  6315. inline void ggml_cuda_op_diag_mask_inf(
  6316. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6317. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6318. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6319. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6320. const int64_t ne00 = src0->ne[0];
  6321. const int64_t ne01 = src0->ne[1];
  6322. const int nrows0 = ggml_nrows(src0);
  6323. const int n_past = ((int32_t *) dst->op_params)[0];
  6324. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6325. (void) src1;
  6326. (void) dst;
  6327. (void) src1_dd;
  6328. }
  6329. inline void ggml_cuda_op_soft_max(
  6330. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6331. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6332. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6333. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6334. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6335. const int64_t ne00 = src0->ne[0];
  6336. const int64_t nrows_x = ggml_nrows(src0);
  6337. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6338. float scale = 1.0f;
  6339. memcpy(&scale, dst->op_params, sizeof(float));
  6340. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6341. (void) dst;
  6342. }
  6343. inline void ggml_cuda_op_scale(
  6344. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6345. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6346. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6347. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6348. float scale;
  6349. memcpy(&scale, dst->op_params, sizeof(float));
  6350. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6351. CUDA_CHECK(cudaGetLastError());
  6352. (void) src1;
  6353. (void) dst;
  6354. (void) src1_dd;
  6355. }
  6356. inline void ggml_cuda_op_clamp(
  6357. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6358. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6359. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6360. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6361. float min;
  6362. float max;
  6363. memcpy(&min, dst->op_params, sizeof(float));
  6364. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6365. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6366. CUDA_CHECK(cudaGetLastError());
  6367. (void) src1;
  6368. (void) dst;
  6369. (void) src1_dd;
  6370. }
  6371. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6372. const int64_t nrows0 = ggml_nrows(src0);
  6373. const bool use_src1 = src1 != nullptr;
  6374. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6375. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6376. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6377. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6378. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6379. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6380. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6381. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6382. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6383. // dd = data device
  6384. float * src0_ddf = nullptr;
  6385. float * src1_ddf = nullptr;
  6386. float * dst_ddf = nullptr;
  6387. cuda_pool_alloc<float> src0_f;
  6388. cuda_pool_alloc<float> src1_f;
  6389. cuda_pool_alloc<float> dst_f;
  6390. ggml_cuda_set_device(g_main_device);
  6391. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6392. if (src0_on_device) {
  6393. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6394. } else {
  6395. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  6396. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6397. }
  6398. if (use_src1) {
  6399. if (src1_on_device) {
  6400. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6401. } else {
  6402. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  6403. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6404. }
  6405. }
  6406. if (dst_on_device) {
  6407. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6408. } else {
  6409. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  6410. }
  6411. // do the computation
  6412. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6413. CUDA_CHECK(cudaGetLastError());
  6414. // copy dst to host if necessary
  6415. if (!dst_on_device) {
  6416. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6417. }
  6418. if (dst->backend == GGML_BACKEND_CPU) {
  6419. CUDA_CHECK(cudaDeviceSynchronize());
  6420. }
  6421. }
  6422. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6423. static bool peer_access_enabled = false;
  6424. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6425. if (peer_access_enabled == enable_peer_access) {
  6426. return;
  6427. }
  6428. #ifdef NDEBUG
  6429. for (int id = 0; id < g_device_count; ++id) {
  6430. CUDA_CHECK(ggml_cuda_set_device(id));
  6431. CUDA_CHECK(cudaDeviceSynchronize());
  6432. }
  6433. for (int id = 0; id < g_device_count; ++id) {
  6434. CUDA_CHECK(ggml_cuda_set_device(id));
  6435. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6436. if (id == id_other) {
  6437. continue;
  6438. }
  6439. if (id != g_main_device && id_other != g_main_device) {
  6440. continue;
  6441. }
  6442. int can_access_peer;
  6443. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6444. if (can_access_peer) {
  6445. if (enable_peer_access) {
  6446. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6447. } else {
  6448. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6449. }
  6450. }
  6451. }
  6452. }
  6453. #endif // NDEBUG
  6454. peer_access_enabled = enable_peer_access;
  6455. }
  6456. static void ggml_cuda_op_mul_mat(
  6457. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6458. const bool convert_src1_to_q8_1) {
  6459. const int64_t ne00 = src0->ne[0];
  6460. const int64_t ne01 = src0->ne[1];
  6461. const int64_t ne02 = src0->ne[2];
  6462. const int64_t ne03 = src0->ne[3];
  6463. const int64_t nrows0 = ggml_nrows(src0);
  6464. const int64_t ne10 = src1->ne[0];
  6465. const int64_t ne11 = src1->ne[1];
  6466. const int64_t ne12 = src1->ne[2];
  6467. const int64_t ne13 = src1->ne[3];
  6468. const int64_t nrows1 = ggml_nrows(src1);
  6469. GGML_ASSERT(ne03 == ne13);
  6470. const int64_t ne0 = dst->ne[0];
  6471. const int64_t ne1 = dst->ne[1];
  6472. const int nb2 = dst->nb[2];
  6473. const int nb3 = dst->nb[3];
  6474. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6475. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6476. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6477. const int64_t i02_divisor = ne12 / ne02;
  6478. const size_t src0_ts = ggml_type_size(src0->type);
  6479. const size_t src0_bs = ggml_blck_size(src0->type);
  6480. const size_t q8_1_ts = sizeof(block_q8_1);
  6481. const size_t q8_1_bs = QK8_1;
  6482. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6483. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6484. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6485. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6486. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6487. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6488. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6489. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6490. GGML_ASSERT(!(split && ne02 > 1));
  6491. GGML_ASSERT(!(split && ne03 > 1));
  6492. GGML_ASSERT(!(split && ne02 < ne12));
  6493. // dd = data device
  6494. char * src0_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6495. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  6496. char * src1_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // q8_1
  6497. float * dst_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6498. // as = actual size
  6499. size_t src0_as[GGML_CUDA_MAX_DEVICES] = {0};
  6500. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  6501. size_t src1_asq[GGML_CUDA_MAX_DEVICES] = {0};
  6502. size_t dst_as[GGML_CUDA_MAX_DEVICES] = {0};
  6503. int64_t row_low[GGML_CUDA_MAX_DEVICES];
  6504. int64_t row_high[GGML_CUDA_MAX_DEVICES];
  6505. int used_devices = 0;
  6506. for (int64_t id = 0; id < g_device_count; ++id) {
  6507. // by default, use all rows
  6508. row_low[id] = 0;
  6509. row_high[id] = ne01;
  6510. // for multi GPU, get the row boundaries from tensor split
  6511. // and round to mul_mat_q tile sizes
  6512. if (split) {
  6513. const int64_t rounding = get_row_rounding(src0->type);
  6514. if (id != 0) {
  6515. row_low[id] = ne01*g_tensor_split[id];
  6516. if (row_low[id] < ne01) {
  6517. row_low[id] -= row_low[id] % rounding;
  6518. }
  6519. }
  6520. if (id != g_device_count - 1) {
  6521. row_high[id] = ne01*g_tensor_split[id + 1];
  6522. if (row_high[id] < ne01) {
  6523. row_high[id] -= row_high[id] % rounding;
  6524. }
  6525. }
  6526. }
  6527. }
  6528. for (int64_t id = 0; id < g_device_count; ++id) {
  6529. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6530. continue;
  6531. }
  6532. used_devices++;
  6533. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6534. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6535. ggml_cuda_set_device(id);
  6536. const cudaStream_t stream = g_cudaStreams[id][0];
  6537. if (src0_on_device && src0_is_contiguous) {
  6538. src0_dd[id] = (char *) src0_extra->data_device[id];
  6539. } else {
  6540. // const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  6541. src0_dd[id] = (char *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  6542. }
  6543. if (src1_on_device && src1_is_contiguous) {
  6544. src1_ddf[id] = (float *) src1_extra->data_device[id];
  6545. } else {
  6546. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  6547. }
  6548. if (convert_src1_to_q8_1) {
  6549. src1_ddq[id] = (char *) ggml_cuda_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  6550. if (src1_on_device && src1_is_contiguous) {
  6551. quantize_row_q8_1_cuda(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  6552. CUDA_CHECK(cudaGetLastError());
  6553. }
  6554. }
  6555. if (dst_on_device) {
  6556. dst_dd[id] = (float *) dst_extra->data_device[id];
  6557. } else {
  6558. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  6559. dst_dd[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_as[id]);
  6560. }
  6561. }
  6562. // if multiple devices are used they need to wait for the main device
  6563. // here an event is recorded that signals that the main device has finished calculating the input data
  6564. if (split && used_devices > 1) {
  6565. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6566. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  6567. }
  6568. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  6569. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  6570. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  6571. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  6572. for (int64_t id = 0; id < g_device_count; ++id) {
  6573. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6574. continue;
  6575. }
  6576. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6577. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6578. const int64_t row_diff = row_high[id] - row_low[id];
  6579. ggml_cuda_set_device(id);
  6580. const cudaStream_t stream = g_cudaStreams[id][is];
  6581. // wait for main GPU data if necessary
  6582. if (split && (id != g_main_device || is != 0)) {
  6583. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  6584. }
  6585. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  6586. const int64_t i03 = i0 / ne12;
  6587. const int64_t i02 = i0 % ne12;
  6588. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  6589. // for split tensors the data begins at i0 == i0_offset_low
  6590. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  6591. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  6592. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  6593. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  6594. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  6595. // in that case an offset on dst_ddf_i is needed
  6596. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  6597. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  6598. }
  6599. // copy src0, src1 to device if necessary
  6600. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  6601. if (id != g_main_device) {
  6602. if (convert_src1_to_q8_1) {
  6603. char * src1_ddq_i_source = src1_ddq[g_main_device] + src1_ddq_i_offset;
  6604. CUDA_CHECK(cudaMemcpyAsync(src1_ddq_i, src1_ddq_i_source, src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs,
  6605. cudaMemcpyDeviceToDevice, stream));
  6606. } else {
  6607. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  6608. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  6609. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_ncols*ne10*sizeof(float),
  6610. cudaMemcpyDeviceToDevice, stream));
  6611. }
  6612. }
  6613. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  6614. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  6615. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  6616. } else {
  6617. GGML_ASSERT(false);
  6618. }
  6619. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  6620. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  6621. CUDA_CHECK(cudaGetLastError());
  6622. }
  6623. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  6624. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  6625. }
  6626. // do the computation
  6627. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  6628. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  6629. CUDA_CHECK(cudaGetLastError());
  6630. // copy dst to host or other device if necessary
  6631. if (!dst_on_device) {
  6632. void * dst_off_device;
  6633. cudaMemcpyKind kind;
  6634. if (dst->backend == GGML_BACKEND_CPU) {
  6635. dst_off_device = dst->data;
  6636. kind = cudaMemcpyDeviceToHost;
  6637. } else if (dst->backend == GGML_BACKEND_GPU) {
  6638. dst_off_device = dst_extra->data_device[g_main_device];
  6639. kind = cudaMemcpyDeviceToDevice;
  6640. } else {
  6641. GGML_ASSERT(false);
  6642. }
  6643. if (split) {
  6644. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  6645. // dst is NOT transposed.
  6646. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  6647. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  6648. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  6649. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6650. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6651. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  6652. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_dd_i, row_diff*sizeof(float),
  6653. row_diff*sizeof(float), src1_ncols, kind, stream));
  6654. } else {
  6655. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6656. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6657. dhf_dst_i += src1_col_0*ne0;
  6658. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  6659. }
  6660. }
  6661. // add event for the main device to wait on until other device is done
  6662. if (split && (id != g_main_device || is != 0)) {
  6663. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  6664. }
  6665. }
  6666. }
  6667. }
  6668. for (int64_t id = 0; id < g_device_count; ++id) {
  6669. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6670. continue;
  6671. }
  6672. CUDA_CHECK(ggml_cuda_set_device(id));
  6673. // free buffers again when done
  6674. if (dst_as[id] > 0) {
  6675. ggml_cuda_pool_free(dst_dd[id], dst_as[id]);
  6676. }
  6677. if (src1_asq[id] > 0) {
  6678. ggml_cuda_pool_free(src1_ddq[id], src1_asq[id]);
  6679. }
  6680. if (src1_asf[id] > 0) {
  6681. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  6682. }
  6683. if (src0_as[id] > 0) {
  6684. ggml_cuda_pool_free(src0_dd[id], src0_as[id]);
  6685. }
  6686. }
  6687. // main device waits for all other devices to be finished
  6688. if (split && g_device_count > 1) {
  6689. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  6690. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  6691. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6692. for (int64_t id = 0; id < g_device_count; ++id) {
  6693. if (row_low[id] == row_high[id]) {
  6694. continue;
  6695. }
  6696. for (int64_t is = 0; is < is_max; ++is) {
  6697. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  6698. }
  6699. }
  6700. }
  6701. if (dst->backend == GGML_BACKEND_CPU) {
  6702. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6703. CUDA_CHECK(cudaDeviceSynchronize());
  6704. }
  6705. }
  6706. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6707. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  6708. }
  6709. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6710. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  6711. }
  6712. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6713. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  6714. }
  6715. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6716. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  6717. }
  6718. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6719. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  6720. }
  6721. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6722. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  6723. }
  6724. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6725. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  6726. }
  6727. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6728. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  6729. }
  6730. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6731. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  6732. }
  6733. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6734. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  6735. }
  6736. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6737. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  6738. }
  6739. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6740. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  6741. }
  6742. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6743. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  6744. }
  6745. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6746. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  6747. }
  6748. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6749. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  6750. }
  6751. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6752. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  6753. }
  6754. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6755. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  6756. }
  6757. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6758. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  6759. }
  6760. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6761. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  6762. }
  6763. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  6764. if (!g_cublas_loaded) return false;
  6765. const int64_t ne10 = src1->ne[0];
  6766. const int64_t ne0 = dst->ne[0];
  6767. const int64_t ne1 = dst->ne[1];
  6768. // TODO: find the optimal values for these
  6769. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  6770. src1->type == GGML_TYPE_F32 &&
  6771. dst->type == GGML_TYPE_F32 &&
  6772. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  6773. }
  6774. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6775. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  6776. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6777. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  6778. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  6779. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6780. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6781. const int64_t ne00 = src0->ne[0];
  6782. const int64_t ne01 = src0->ne[1];
  6783. const int64_t ne02 = src0->ne[2];
  6784. const int64_t ne12 = src1->ne[2];
  6785. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6786. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6787. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6788. void * src0_ddq = src0_extra->data_device[g_main_device];
  6789. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6790. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6791. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6792. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6793. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  6794. }
  6795. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6796. GGML_ASSERT(!ggml_is_transposed(src0));
  6797. GGML_ASSERT(!ggml_is_transposed(src1));
  6798. GGML_ASSERT(!ggml_is_permuted(src0));
  6799. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6800. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6801. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6802. const int64_t ne00 = src0->ne[0];
  6803. const int64_t ne01 = src0->ne[1];
  6804. const int64_t ne02 = src0->ne[2];
  6805. const int64_t nb01 = src0->nb[1];
  6806. const int64_t nb02 = src0->nb[2];
  6807. const int64_t ne12 = src1->ne[2];
  6808. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6809. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6810. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6811. void * src0_ddq = src0_extra->data_device[g_main_device];
  6812. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6813. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6814. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6815. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6816. const int64_t row_stride_x = nb01 / sizeof(half);
  6817. const int64_t channel_stride_x = nb02 / sizeof(half);
  6818. 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);
  6819. }
  6820. static __global__ void k_compute_batched_ptrs(
  6821. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  6822. const void ** ptrs_src, void ** ptrs_dst,
  6823. int64_t ne12, int64_t ne13,
  6824. int64_t ne23,
  6825. size_t nb02, size_t nb03,
  6826. size_t nb12, size_t nb13,
  6827. size_t nbd2, size_t nbd3,
  6828. int64_t r2, int64_t r3) {
  6829. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6830. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6831. if (i13 >= ne13 || i12 >= ne12) {
  6832. return;
  6833. }
  6834. int64_t i03 = i13 / r3;
  6835. int64_t i02 = i12 / r2;
  6836. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  6837. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  6838. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  6839. }
  6840. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6841. GGML_ASSERT(!ggml_is_transposed(src0));
  6842. GGML_ASSERT(!ggml_is_transposed(src1));
  6843. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6844. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6845. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6846. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  6847. const int64_t ne01 = src0->ne[1];
  6848. const int64_t ne02 = src0->ne[2];
  6849. const int64_t ne03 = src0->ne[3];
  6850. const int64_t nb01 = src0->nb[1];
  6851. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  6852. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  6853. const int64_t ne10 = src1->ne[0];
  6854. const int64_t ne11 = src1->ne[1];
  6855. const int64_t ne12 = src1->ne[2];
  6856. const int64_t ne13 = src1->ne[3];
  6857. const int64_t nb11 = src1->nb[1];
  6858. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6859. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6860. const int64_t ne1 = ggml_nelements(src1);
  6861. const int64_t ne = ggml_nelements(dst);
  6862. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6863. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6864. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6865. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6866. void * src0_ddq = src0_extra->data_device[g_main_device];
  6867. half * src0_as_f16 = (half *) src0_ddq;
  6868. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6869. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6870. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6871. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6872. // convert src1 to fp16
  6873. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6874. GGML_ASSERT(to_fp16_cuda != nullptr);
  6875. cuda_pool_alloc<half> src1_as_f16(ne1);
  6876. to_fp16_cuda(src1_ddf, src1_as_f16.get(), ne1, main_stream);
  6877. cuda_pool_alloc<half> dst_f16;
  6878. char * dst_t;
  6879. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  6880. cudaDataType_t cu_data_type = CUDA_R_16F;
  6881. // dst strides
  6882. size_t nbd2 = dst->nb[2];
  6883. size_t nbd3 = dst->nb[3];
  6884. const half alpha_f16 = 1.0f;
  6885. const half beta_f16 = 0.0f;
  6886. const float alpha_f32 = 1.0f;
  6887. const float beta_f32 = 0.0f;
  6888. const void * alpha = &alpha_f16;
  6889. const void * beta = &beta_f16;
  6890. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6891. dst_t = (char *) dst_f16.alloc(ne);
  6892. nbd2 /= sizeof(float) / sizeof(half);
  6893. nbd3 /= sizeof(float) / sizeof(half);
  6894. } else {
  6895. dst_t = (char *) dst_ddf;
  6896. cu_compute_type = CUBLAS_COMPUTE_32F;
  6897. cu_data_type = CUDA_R_32F;
  6898. alpha = &alpha_f32;
  6899. beta = &beta_f32;
  6900. }
  6901. GGML_ASSERT(ne12 % ne02 == 0);
  6902. GGML_ASSERT(ne13 % ne03 == 0);
  6903. // broadcast factors
  6904. const int64_t r2 = ne12/ne02;
  6905. const int64_t r3 = ne13/ne03;
  6906. #if 0
  6907. // use cublasGemmEx
  6908. {
  6909. for (int i13 = 0; i13 < ne13; ++i13) {
  6910. for (int i12 = 0; i12 < ne12; ++i12) {
  6911. int i03 = i13 / r3;
  6912. int i02 = i12 / r2;
  6913. CUBLAS_CHECK(
  6914. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6915. ne01, ne11, ne10,
  6916. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6917. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6918. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  6919. cu_compute_type,
  6920. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6921. }
  6922. }
  6923. }
  6924. #else
  6925. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6926. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6927. // use cublasGemmStridedBatchedEx
  6928. CUBLAS_CHECK(
  6929. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6930. ne01, ne11, ne10,
  6931. alpha, (const char *) src0_as_f16, CUDA_R_16F, nb01/sizeof(half), src0->nb[2]/sizeof(half), // strideA
  6932. (const char *) src1_as_f16.get(), CUDA_R_16F, nb11/sizeof(float), src1->nb[2]/sizeof(float), // strideB
  6933. beta, ( char *) dst_t, cu_data_type, ne01, dst->nb[2]/sizeof(float), // strideC
  6934. ne12*ne13,
  6935. cu_compute_type,
  6936. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6937. } else {
  6938. // use cublasGemmBatchedEx
  6939. const int ne23 = ne12*ne13;
  6940. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  6941. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  6942. dim3 block_dims(ne13, ne12);
  6943. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6944. src0_as_f16, src1_as_f16.get(), dst_t,
  6945. ptrs_src.get(), ptrs_dst.get(),
  6946. ne12, ne13,
  6947. ne23,
  6948. nb02, nb03,
  6949. nb12, nb13,
  6950. nbd2, nbd3,
  6951. r2, r3);
  6952. CUDA_CHECK(cudaGetLastError());
  6953. CUBLAS_CHECK(
  6954. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6955. ne01, ne11, ne10,
  6956. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/sizeof(half),
  6957. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/sizeof(float),
  6958. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  6959. ne23,
  6960. cu_compute_type,
  6961. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6962. }
  6963. #endif
  6964. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6965. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6966. to_fp32_cuda(dst_f16.get(), dst_ddf, ne, main_stream);
  6967. }
  6968. }
  6969. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6970. const bool all_on_device =
  6971. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6972. (src1->backend == GGML_BACKEND_GPU) &&
  6973. ( dst->backend == GGML_BACKEND_GPU);
  6974. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6975. int64_t min_compute_capability = INT_MAX;
  6976. for (int64_t id = 0; id < g_device_count; ++id) {
  6977. if (min_compute_capability > g_device_caps[id].cc && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6978. min_compute_capability = g_device_caps[id].cc;
  6979. }
  6980. }
  6981. #ifdef CUDA_USE_TENSOR_CORES
  6982. const bool use_tensor_cores = true;
  6983. #else
  6984. const bool use_tensor_cores = false;
  6985. #endif
  6986. // debug helpers
  6987. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  6988. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  6989. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  6990. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  6991. //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);
  6992. //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);
  6993. 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) {
  6994. // KQ single-batch
  6995. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  6996. } 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) {
  6997. // KQV single-batch
  6998. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  6999. } 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)) {
  7000. // KQ + KQV multi-batch
  7001. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  7002. } else if (src0->type == GGML_TYPE_F32) {
  7003. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7004. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  7005. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  7006. #ifdef GGML_CUDA_FORCE_DMMV
  7007. const bool use_mul_mat_vec_q = false;
  7008. #else
  7009. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  7010. #endif // GGML_CUDA_FORCE_DMMV
  7011. if (use_mul_mat_vec_q) {
  7012. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  7013. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  7014. } else {
  7015. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  7016. }
  7017. } else {
  7018. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  7019. // when tensor cores are available, use them for large batch size
  7020. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  7021. if (use_tensor_cores && min_compute_capability >= CC_VOLTA && src1->ne[1] > MMQ_MAX_BATCH_SIZE) {
  7022. use_mul_mat_q = false;
  7023. }
  7024. if (use_mul_mat_q) {
  7025. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  7026. } else {
  7027. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7028. }
  7029. }
  7030. } else {
  7031. GGML_ASSERT(false);
  7032. }
  7033. }
  7034. #if 0
  7035. template<typename ... Srcs>
  7036. static __global__ void k_compute_batched_ptrs_id(
  7037. const void ** ptrs_src, void ** ptrs_dst,
  7038. int ne12, int ne13,
  7039. int ne23,
  7040. int nb02, int nb03,
  7041. int nb12, int nb13,
  7042. int nb2, int nb3,
  7043. int r2, int r3,
  7044. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  7045. const half * src1_f16, half * dst_f16,
  7046. const int32_t * ids, const int id,
  7047. Srcs... src0s) {
  7048. int i = ids[id];
  7049. half * src0_f16;
  7050. const void * srcs_ar[] = { (const half *) src0s... };
  7051. if (src0_type == GGML_TYPE_F16) {
  7052. src0_f16 = (half *) srcs_ar[i];
  7053. } else {
  7054. src0_f16 = src0_as_f16;
  7055. if (threadIdx.x == 0 && threadIdx.y == 0) {
  7056. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  7057. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  7058. }
  7059. }
  7060. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7061. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7062. if (i13 >= ne13 || i12 >= ne12) {
  7063. return;
  7064. }
  7065. int i03 = i13 / r3;
  7066. int i02 = i12 / r2;
  7067. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  7068. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  7069. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  7070. }
  7071. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  7072. const struct ggml_tensor * ids = dst->src[0];
  7073. const struct ggml_tensor * src1 = dst->src[1];
  7074. const struct ggml_tensor * src00 = dst->src[2];
  7075. const int id = dst->op_params[0];
  7076. GGML_ASSERT(!ggml_is_transposed(src00));
  7077. GGML_ASSERT(!ggml_is_transposed(src1));
  7078. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  7079. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7080. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  7081. const int64_t ne01 = src00->ne[1];
  7082. const int64_t ne02 = src00->ne[2];
  7083. const int64_t ne03 = src00->ne[3];
  7084. //const int64_t nb01 = src00->nb[1];
  7085. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  7086. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  7087. const int64_t ne10 = src1->ne[0];
  7088. const int64_t ne11 = src1->ne[1];
  7089. const int64_t ne12 = src1->ne[2];
  7090. const int64_t ne13 = src1->ne[3];
  7091. //const int64_t nb11 = src1->nb[1];
  7092. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  7093. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  7094. const int64_t ne1 = ggml_nelements(src1);
  7095. const int64_t ne = ggml_nelements(dst);
  7096. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7097. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7098. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7099. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7100. //void * src0_ddq = src0_extra->data_device[g_main_device];
  7101. //half * src0_as_f16 = (half *) src0_ddq;
  7102. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7103. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7104. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7105. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7106. // convert src1 to fp16
  7107. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7108. GGML_ASSERT(to_fp16_cuda != nullptr);
  7109. size_t src1_as = 0;
  7110. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7111. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7112. size_t dst_as = 0;
  7113. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7114. GGML_ASSERT(ne12 % ne02 == 0);
  7115. GGML_ASSERT(ne13 % ne03 == 0);
  7116. // broadcast factors
  7117. const int64_t r2 = ne12/ne02;
  7118. const int64_t r3 = ne13/ne03;
  7119. const half alpha_f16 = 1.0f;
  7120. const half beta_f16 = 0.0f;
  7121. // use cublasGemmBatchedEx
  7122. const int ne23 = ne12*ne13;
  7123. const void ** ptrs_src = nullptr;
  7124. void ** ptrs_dst = nullptr;
  7125. size_t ptrs_src_s = 0;
  7126. size_t ptrs_dst_s = 0;
  7127. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7128. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7129. int64_t src0_ne = ggml_nelements(src00);
  7130. half * src0_as_f16 = nullptr;
  7131. size_t src0_as = 0;
  7132. if (src00->type != GGML_TYPE_F16) {
  7133. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7134. }
  7135. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7136. dim3 block_dims(ne13, ne12);
  7137. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7138. ptrs_src, ptrs_dst,
  7139. ne12, ne13,
  7140. ne23,
  7141. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7142. nb12, nb13,
  7143. dst->nb[2], dst->nb[3],
  7144. r2, r3,
  7145. src00->type, src0_as_f16, src0_ne,
  7146. src1_as_f16, dst_f16,
  7147. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7148. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7149. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7150. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7151. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7152. );
  7153. CUDA_CHECK(cudaGetLastError());
  7154. CUBLAS_CHECK(
  7155. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7156. ne01, ne11, ne10,
  7157. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7158. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7159. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7160. ne23,
  7161. CUBLAS_COMPUTE_16F,
  7162. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7163. if (src0_as != 0) {
  7164. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7165. }
  7166. if (ptrs_src_s != 0) {
  7167. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7168. }
  7169. if (ptrs_dst_s != 0) {
  7170. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7171. }
  7172. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7173. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7174. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7175. ggml_cuda_pool_free(dst_f16, dst_as);
  7176. }
  7177. #endif
  7178. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7179. #if 0
  7180. ggml_cuda_mul_mat_id_cublas(dst);
  7181. // TODO: mmq/mmv support
  7182. #endif
  7183. const int64_t nb11 = src1->nb[1];
  7184. const int64_t nb1 = dst->nb[1];
  7185. const struct ggml_tensor * ids = src0;
  7186. const int32_t id = ((int32_t *) dst->op_params)[0];
  7187. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7188. std::vector<char> ids_host(ggml_nbytes(ids));
  7189. const cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7190. if (ids->backend == GGML_BACKEND_GPU) {
  7191. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7192. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7193. CUDA_CHECK(cudaStreamSynchronize(stream));
  7194. } else {
  7195. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7196. }
  7197. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7198. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7199. ggml_tensor_extra_gpu src1_row_extra;
  7200. ggml_tensor_extra_gpu dst_row_extra;
  7201. ggml_tensor src1_row = *src1;
  7202. ggml_tensor dst_row = *dst;
  7203. src1_row.backend = GGML_BACKEND_GPU;
  7204. dst_row.backend = GGML_BACKEND_GPU;
  7205. src1_row.extra = &src1_row_extra;
  7206. dst_row.extra = &dst_row_extra;
  7207. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7208. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7209. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7210. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7211. if (src1->ne[1] == 1) {
  7212. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7213. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7214. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7215. //int32_t row_id;
  7216. //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]));
  7217. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7218. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7219. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7220. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7221. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7222. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7223. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7224. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7225. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7226. }
  7227. } else {
  7228. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  7229. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  7230. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  7231. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  7232. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7233. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7234. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7235. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  7236. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7237. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7238. int64_t num_src1_rows = 0;
  7239. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7240. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7241. if (row_id_i != row_id) {
  7242. continue;
  7243. }
  7244. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7245. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  7246. nb11, src1_kind, stream));
  7247. num_src1_rows++;
  7248. }
  7249. if (num_src1_rows == 0) {
  7250. continue;
  7251. }
  7252. src1_row.ne[1] = num_src1_rows;
  7253. dst_row.ne[1] = num_src1_rows;
  7254. src1_row.nb[1] = nb11;
  7255. src1_row.nb[2] = num_src1_rows*nb11;
  7256. src1_row.nb[3] = num_src1_rows*nb11;
  7257. dst_row.nb[1] = nb1;
  7258. dst_row.nb[2] = num_src1_rows*nb1;
  7259. dst_row.nb[3] = num_src1_rows*nb1;
  7260. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7261. num_src1_rows = 0;
  7262. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7263. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7264. if (row_id_i != row_id) {
  7265. continue;
  7266. }
  7267. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7268. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  7269. nb1, dst_kind, stream));
  7270. num_src1_rows++;
  7271. }
  7272. }
  7273. }
  7274. if (dst->backend == GGML_BACKEND_CPU) {
  7275. CUDA_CHECK(cudaStreamSynchronize(stream));
  7276. }
  7277. }
  7278. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7279. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7280. }
  7281. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7282. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7283. }
  7284. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7285. const int64_t ne = ggml_nelements(src0);
  7286. GGML_ASSERT(ne == ggml_nelements(src1));
  7287. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7288. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7289. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7290. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7291. const int64_t ne00 = src0->ne[0];
  7292. const int64_t ne01 = src0->ne[1];
  7293. GGML_ASSERT(src0->ne[3] == 1);
  7294. const int64_t nb00 = src0->nb[0];
  7295. const int64_t nb01 = src0->nb[1];
  7296. const int64_t nb02 = src0->nb[2];
  7297. const int64_t ne10 = src1->ne[0];
  7298. const int64_t ne11 = src1->ne[1];
  7299. GGML_ASSERT(src1->ne[3] == 1);
  7300. const int64_t nb10 = src1->nb[0];
  7301. const int64_t nb11 = src1->nb[1];
  7302. const int64_t nb12 = src1->nb[2];
  7303. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7304. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7305. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7306. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7307. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7308. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7309. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7310. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7311. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7312. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7313. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7314. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7315. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7316. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7317. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7318. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7319. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7320. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7321. } else {
  7322. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7323. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7324. GGML_ASSERT(false);
  7325. }
  7326. (void) dst;
  7327. }
  7328. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7329. // TODO: why do we pass dst as src1 here?
  7330. ggml_cuda_cpy(src0, dst, nullptr);
  7331. (void) src1;
  7332. }
  7333. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7334. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7335. }
  7336. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7337. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7338. }
  7339. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7340. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7341. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7342. }
  7343. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7344. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7345. }
  7346. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7347. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7348. }
  7349. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7350. GGML_ASSERT(ggml_is_contiguous(src0));
  7351. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7352. }
  7353. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7354. GGML_ASSERT(ggml_is_contiguous(src0));
  7355. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7356. }
  7357. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7358. (void) src0;
  7359. (void) src1;
  7360. (void) dst;
  7361. }
  7362. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7363. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7364. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7365. }
  7366. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7367. const int64_t nrows = ggml_nrows(tensor);
  7368. const int64_t ne0 = tensor->ne[0];
  7369. const size_t nb1 = tensor->nb[1];
  7370. ggml_backend_type backend = tensor->backend;
  7371. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7372. memset(extra, 0, sizeof(*extra));
  7373. for (int64_t id = 0; id < g_device_count; ++id) {
  7374. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7375. continue;
  7376. }
  7377. ggml_cuda_set_device(id);
  7378. int64_t row_low, row_high;
  7379. if (backend == GGML_BACKEND_GPU) {
  7380. row_low = 0;
  7381. row_high = nrows;
  7382. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7383. const int64_t rounding = get_row_rounding(tensor->type);
  7384. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7385. row_low -= row_low % rounding;
  7386. if (id == g_device_count - 1) {
  7387. row_high = nrows;
  7388. } else {
  7389. row_high = nrows*g_tensor_split[id + 1];
  7390. row_high -= row_high % rounding;
  7391. }
  7392. } else {
  7393. GGML_ASSERT(false);
  7394. }
  7395. if (row_low == row_high) {
  7396. continue;
  7397. }
  7398. int64_t nrows_split = row_high - row_low;
  7399. const size_t offset_split = row_low*nb1;
  7400. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7401. const size_t original_size = size;
  7402. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7403. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7404. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7405. }
  7406. char * buf;
  7407. CUDA_CHECK(cudaMalloc(&buf, size));
  7408. char * buf_host = (char *)data + offset_split;
  7409. // set padding to 0 to avoid possible NaN values
  7410. if (size > original_size) {
  7411. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7412. }
  7413. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7414. extra->data_device[id] = buf;
  7415. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7416. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7417. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7418. }
  7419. }
  7420. }
  7421. tensor->extra = extra;
  7422. }
  7423. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7424. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7425. return;
  7426. }
  7427. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7428. for (int64_t id = 0; id < g_device_count; ++id) {
  7429. if (extra->data_device[id] != nullptr) {
  7430. CUDA_CHECK(ggml_cuda_set_device(id));
  7431. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7432. }
  7433. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7434. if (extra->events[id][is] != nullptr) {
  7435. CUDA_CHECK(ggml_cuda_set_device(id));
  7436. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7437. }
  7438. }
  7439. }
  7440. delete extra;
  7441. }
  7442. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7443. static size_t g_temp_tensor_extra_index = 0;
  7444. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7445. if (g_temp_tensor_extras == nullptr) {
  7446. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7447. }
  7448. size_t alloc_index = g_temp_tensor_extra_index;
  7449. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7450. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7451. memset(extra, 0, sizeof(*extra));
  7452. return extra;
  7453. }
  7454. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7455. if (scratch && g_scratch_size == 0) {
  7456. return;
  7457. }
  7458. tensor->backend = GGML_BACKEND_GPU;
  7459. // recursively assign CUDA buffers until a compute tensor is found
  7460. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7461. const ggml_op src0_op = tensor->src[0]->op;
  7462. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7463. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7464. }
  7465. }
  7466. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7467. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7468. }
  7469. if (scratch && no_alloc) {
  7470. return;
  7471. }
  7472. ggml_tensor_extra_gpu * extra;
  7473. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7474. tensor->op == GGML_OP_VIEW ||
  7475. force_inplace;
  7476. const size_t size = ggml_nbytes(tensor);
  7477. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7478. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7479. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7480. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7481. size_t offset = 0;
  7482. if (tensor->op == GGML_OP_VIEW) {
  7483. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7484. }
  7485. extra = ggml_cuda_alloc_temp_tensor_extra();
  7486. extra->data_device[g_main_device] = src0_ddc + offset;
  7487. } else if (tensor->op == GGML_OP_CPY) {
  7488. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7489. void * src1_ddv = src1_extra->data_device[g_main_device];
  7490. extra = ggml_cuda_alloc_temp_tensor_extra();
  7491. extra->data_device[g_main_device] = src1_ddv;
  7492. } else if (scratch) {
  7493. GGML_ASSERT(size <= g_scratch_size);
  7494. if (g_scratch_offset + size > g_scratch_size) {
  7495. g_scratch_offset = 0;
  7496. }
  7497. char * data = (char *) g_scratch_buffer;
  7498. if (data == nullptr) {
  7499. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7500. g_scratch_buffer = data;
  7501. }
  7502. extra = ggml_cuda_alloc_temp_tensor_extra();
  7503. extra->data_device[g_main_device] = data + g_scratch_offset;
  7504. g_scratch_offset += size;
  7505. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7506. } else { // allocate new buffers outside of scratch
  7507. void * data;
  7508. CUDA_CHECK(cudaMalloc(&data, size));
  7509. CUDA_CHECK(cudaMemset(data, 0, size));
  7510. extra = new ggml_tensor_extra_gpu;
  7511. memset(extra, 0, sizeof(*extra));
  7512. extra->data_device[g_main_device] = data;
  7513. }
  7514. tensor->extra = extra;
  7515. }
  7516. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7517. if (g_scratch_size == 0) {
  7518. return;
  7519. }
  7520. if (g_scratch_buffer == nullptr) {
  7521. ggml_cuda_set_device(g_main_device);
  7522. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7523. }
  7524. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7525. const bool inplace = tensor->view_src != nullptr;
  7526. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  7527. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  7528. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7529. size_t view_offset = 0;
  7530. if (tensor->op == GGML_OP_VIEW) {
  7531. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7532. }
  7533. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7534. } else {
  7535. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7536. }
  7537. tensor->extra = extra;
  7538. }
  7539. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7540. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7541. GGML_ASSERT(ggml_is_contiguous(tensor));
  7542. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7543. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7544. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7545. }
  7546. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7547. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7548. }
  7549. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7550. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7551. }
  7552. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  7553. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  7554. }
  7555. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  7556. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  7557. }
  7558. void ggml_cuda_set_main_device(const int main_device) {
  7559. if (main_device >= g_device_count) {
  7560. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  7561. main_device, g_device_count, g_main_device);
  7562. return;
  7563. }
  7564. if (g_main_device != main_device && g_device_count > 1) {
  7565. g_main_device = main_device;
  7566. cudaDeviceProp prop;
  7567. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  7568. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  7569. }
  7570. }
  7571. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  7572. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  7573. // it still won't always work as expected, but it's better than nothing
  7574. if (scratch_size > g_scratch_size) {
  7575. ggml_cuda_free_scratch();
  7576. }
  7577. g_scratch_size = std::max(g_scratch_size, scratch_size);
  7578. }
  7579. void ggml_cuda_free_scratch() {
  7580. if (g_scratch_buffer == nullptr) {
  7581. return;
  7582. }
  7583. CUDA_CHECK(cudaFree(g_scratch_buffer));
  7584. g_scratch_buffer = nullptr;
  7585. }
  7586. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  7587. if (!g_cublas_loaded) return false;
  7588. ggml_cuda_func_t func;
  7589. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  7590. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  7591. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  7592. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  7593. return false;
  7594. }
  7595. if (tensor->op == GGML_OP_MUL_MAT) {
  7596. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  7597. #ifndef NDEBUG
  7598. 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]);
  7599. #endif
  7600. return false;
  7601. }
  7602. }
  7603. switch (tensor->op) {
  7604. case GGML_OP_REPEAT:
  7605. func = ggml_cuda_repeat;
  7606. break;
  7607. case GGML_OP_GET_ROWS:
  7608. func = ggml_cuda_get_rows;
  7609. break;
  7610. case GGML_OP_DUP:
  7611. func = ggml_cuda_dup;
  7612. break;
  7613. case GGML_OP_ADD:
  7614. func = ggml_cuda_add;
  7615. break;
  7616. case GGML_OP_ACC:
  7617. func = ggml_cuda_acc;
  7618. break;
  7619. case GGML_OP_MUL:
  7620. func = ggml_cuda_mul;
  7621. break;
  7622. case GGML_OP_DIV:
  7623. func = ggml_cuda_div;
  7624. break;
  7625. case GGML_OP_UNARY:
  7626. switch (ggml_get_unary_op(tensor)) {
  7627. case GGML_UNARY_OP_GELU:
  7628. func = ggml_cuda_gelu;
  7629. break;
  7630. case GGML_UNARY_OP_SILU:
  7631. func = ggml_cuda_silu;
  7632. break;
  7633. case GGML_UNARY_OP_GELU_QUICK:
  7634. func = ggml_cuda_gelu_quick;
  7635. break;
  7636. case GGML_UNARY_OP_TANH:
  7637. func = ggml_cuda_tanh;
  7638. break;
  7639. case GGML_UNARY_OP_RELU:
  7640. func = ggml_cuda_relu;
  7641. break;
  7642. default:
  7643. return false;
  7644. }
  7645. break;
  7646. case GGML_OP_NORM:
  7647. func = ggml_cuda_norm;
  7648. break;
  7649. case GGML_OP_GROUP_NORM:
  7650. func = ggml_cuda_group_norm;
  7651. break;
  7652. case GGML_OP_CONCAT:
  7653. func = ggml_cuda_concat;
  7654. break;
  7655. case GGML_OP_UPSCALE:
  7656. func = ggml_cuda_upscale;
  7657. break;
  7658. case GGML_OP_PAD:
  7659. func = ggml_cuda_pad;
  7660. break;
  7661. case GGML_OP_LEAKY_RELU:
  7662. func = ggml_cuda_leaky_relu;
  7663. break;
  7664. case GGML_OP_RMS_NORM:
  7665. func = ggml_cuda_rms_norm;
  7666. break;
  7667. case GGML_OP_MUL_MAT:
  7668. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  7669. return false;
  7670. }
  7671. func = ggml_cuda_mul_mat;
  7672. break;
  7673. case GGML_OP_MUL_MAT_ID:
  7674. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  7675. return false;
  7676. }
  7677. func = ggml_cuda_mul_mat_id;
  7678. break;
  7679. case GGML_OP_SCALE:
  7680. func = ggml_cuda_scale;
  7681. break;
  7682. case GGML_OP_SQR:
  7683. func = ggml_cuda_sqr;
  7684. break;
  7685. case GGML_OP_CLAMP:
  7686. func = ggml_cuda_clamp;
  7687. break;
  7688. case GGML_OP_CPY:
  7689. func = ggml_cuda_cpy;
  7690. break;
  7691. case GGML_OP_CONT:
  7692. func = ggml_cuda_dup;
  7693. break;
  7694. case GGML_OP_NONE:
  7695. case GGML_OP_RESHAPE:
  7696. case GGML_OP_VIEW:
  7697. case GGML_OP_PERMUTE:
  7698. case GGML_OP_TRANSPOSE:
  7699. func = ggml_cuda_nop;
  7700. break;
  7701. case GGML_OP_DIAG_MASK_INF:
  7702. func = ggml_cuda_diag_mask_inf;
  7703. break;
  7704. case GGML_OP_SOFT_MAX:
  7705. func = ggml_cuda_soft_max;
  7706. break;
  7707. case GGML_OP_ROPE:
  7708. func = ggml_cuda_rope;
  7709. break;
  7710. case GGML_OP_ALIBI:
  7711. func = ggml_cuda_alibi;
  7712. break;
  7713. case GGML_OP_IM2COL:
  7714. func = ggml_cuda_im2col;
  7715. break;
  7716. case GGML_OP_SUM_ROWS:
  7717. func = ggml_cuda_sum_rows;
  7718. break;
  7719. case GGML_OP_ARGSORT:
  7720. func = ggml_cuda_argsort;
  7721. break;
  7722. default:
  7723. return false;
  7724. }
  7725. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  7726. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  7727. }
  7728. if (params->ith != 0) {
  7729. return true;
  7730. }
  7731. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  7732. return true;
  7733. }
  7734. func(tensor->src[0], tensor->src[1], tensor);
  7735. return true;
  7736. }
  7737. int ggml_cuda_get_device_count() {
  7738. int device_count;
  7739. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  7740. return 0;
  7741. }
  7742. return device_count;
  7743. }
  7744. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  7745. cudaDeviceProp prop;
  7746. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  7747. snprintf(description, description_size, "%s", prop.name);
  7748. }
  7749. ////////////////////////////////////////////////////////////////////////////////
  7750. // backend interface
  7751. #define UNUSED GGML_UNUSED
  7752. // cuda buffer
  7753. struct ggml_backend_buffer_context_cuda {
  7754. int device;
  7755. void * dev_ptr = nullptr;
  7756. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  7757. size_t temp_tensor_extra_index = 0;
  7758. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  7759. ~ggml_backend_buffer_context_cuda() {
  7760. delete[] temp_tensor_extras;
  7761. }
  7762. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7763. if (temp_tensor_extras == nullptr) {
  7764. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7765. }
  7766. size_t alloc_index = temp_tensor_extra_index;
  7767. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7768. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  7769. memset(extra, 0, sizeof(*extra));
  7770. return extra;
  7771. }
  7772. };
  7773. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7774. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7775. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  7776. delete ctx;
  7777. }
  7778. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  7779. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7780. return ctx->dev_ptr;
  7781. }
  7782. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7783. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7784. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  7785. assert(tensor->view_src->buffer->buft == buffer->buft);
  7786. tensor->backend = tensor->view_src->backend;
  7787. tensor->extra = tensor->view_src->extra;
  7788. return;
  7789. }
  7790. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  7791. extra->data_device[ctx->device] = tensor->data;
  7792. tensor->backend = GGML_BACKEND_GPU;
  7793. tensor->extra = extra;
  7794. if (ggml_is_quantized(tensor->type)) {
  7795. // initialize padding to 0 to avoid possible NaN values
  7796. int64_t row_low = 0;
  7797. int64_t row_high = ggml_nrows(tensor);
  7798. int64_t nrows_split = row_high - row_low;
  7799. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  7800. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  7801. if (padded_size > original_size && tensor->view_src == nullptr) {
  7802. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  7803. }
  7804. }
  7805. UNUSED(buffer);
  7806. }
  7807. 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) {
  7808. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7809. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7810. ggml_cuda_set_device(ctx->device);
  7811. CUDA_CHECK(cudaDeviceSynchronize());
  7812. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  7813. }
  7814. 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) {
  7815. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7816. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7817. ggml_cuda_set_device(ctx->device);
  7818. CUDA_CHECK(cudaDeviceSynchronize());
  7819. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  7820. }
  7821. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7822. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7823. ggml_cuda_set_device(ctx->device);
  7824. CUDA_CHECK(cudaDeviceSynchronize());
  7825. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  7826. }
  7827. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  7828. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  7829. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  7830. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  7831. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  7832. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  7833. /* .cpy_tensor_from = */ NULL,
  7834. /* .cpy_tensor_to = */ NULL,
  7835. /* .clear = */ ggml_backend_cuda_buffer_clear,
  7836. };
  7837. // cuda buffer type
  7838. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7839. int device = (int) (intptr_t) buft->context;
  7840. ggml_cuda_set_device(device);
  7841. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  7842. void * dev_ptr;
  7843. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  7844. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  7845. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  7846. }
  7847. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7848. return 128;
  7849. UNUSED(buft);
  7850. }
  7851. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  7852. int64_t row_low = 0;
  7853. int64_t row_high = ggml_nrows(tensor);
  7854. int64_t nrows_split = row_high - row_low;
  7855. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7856. int64_t ne0 = tensor->ne[0];
  7857. if (ggml_is_quantized(tensor->type)) {
  7858. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7859. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7860. }
  7861. }
  7862. return size;
  7863. UNUSED(buft);
  7864. }
  7865. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  7866. return ggml_backend_is_cuda(backend);
  7867. UNUSED(buft);
  7868. }
  7869. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  7870. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  7871. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  7872. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  7873. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  7874. /* .is_host = */ nullptr,
  7875. };
  7876. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  7877. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  7878. static bool ggml_backend_cuda_buffer_type_initialized = false;
  7879. if (!ggml_backend_cuda_buffer_type_initialized) {
  7880. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  7881. ggml_backend_cuda_buffer_types[i] = {
  7882. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  7883. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  7884. };
  7885. }
  7886. ggml_backend_cuda_buffer_type_initialized = true;
  7887. }
  7888. return &ggml_backend_cuda_buffer_types[device];
  7889. }
  7890. // host buffer type
  7891. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7892. ggml_cuda_host_free(buffer->context);
  7893. }
  7894. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7895. void * ptr = ggml_cuda_host_malloc(size);
  7896. if (ptr == nullptr) {
  7897. // fallback to cpu buffer
  7898. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7899. }
  7900. // FIXME: this is a hack to avoid having to implement a new buffer type
  7901. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7902. buffer->buft = buft;
  7903. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  7904. return buffer;
  7905. }
  7906. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  7907. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  7908. /* .iface = */ {
  7909. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  7910. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  7911. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7912. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  7913. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7914. },
  7915. /* .context = */ nullptr,
  7916. };
  7917. return &ggml_backend_cuda_buffer_type_host;
  7918. }
  7919. // backend
  7920. struct ggml_backend_context_cuda {
  7921. int device;
  7922. };
  7923. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  7924. return GGML_CUDA_NAME;
  7925. UNUSED(backend);
  7926. }
  7927. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  7928. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7929. delete cuda_ctx;
  7930. delete backend;
  7931. }
  7932. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  7933. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7934. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  7935. }
  7936. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7937. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7938. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7939. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7940. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  7941. }
  7942. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7943. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7944. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7945. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7946. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  7947. }
  7948. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  7949. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7950. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  7951. UNUSED(backend);
  7952. }
  7953. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7954. GGML_ASSERT(!"not implemented");
  7955. return nullptr;
  7956. UNUSED(backend);
  7957. UNUSED(cgraph);
  7958. }
  7959. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7960. GGML_ASSERT(!"not implemented");
  7961. UNUSED(backend);
  7962. UNUSED(plan);
  7963. }
  7964. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7965. GGML_ASSERT(!"not implemented");
  7966. UNUSED(backend);
  7967. UNUSED(plan);
  7968. }
  7969. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7970. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7971. ggml_cuda_set_main_device(cuda_ctx->device);
  7972. ggml_compute_params params = {};
  7973. params.type = GGML_TASK_COMPUTE;
  7974. params.ith = 0;
  7975. for (int i = 0; i < cgraph->n_nodes; i++) {
  7976. ggml_tensor * node = cgraph->nodes[i];
  7977. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  7978. continue;
  7979. assert(node->backend == GGML_BACKEND_GPU);
  7980. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7981. assert(node->extra != nullptr);
  7982. for (int j = 0; j < GGML_MAX_SRC; j++) {
  7983. if (node->src[j] != nullptr) {
  7984. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  7985. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7986. assert(node->src[j]->extra != nullptr);
  7987. }
  7988. }
  7989. bool ok = ggml_cuda_compute_forward(&params, node);
  7990. if (!ok) {
  7991. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  7992. }
  7993. GGML_ASSERT(ok);
  7994. #if 0
  7995. if (node->type == GGML_TYPE_F32) {
  7996. cudaDeviceSynchronize();
  7997. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  7998. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  7999. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  8000. ggml_type_name(node->src[0]->type),
  8001. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  8002. node->src[0]->name,
  8003. node->src[1] ? node->src[1]->name : "none");
  8004. double sum = 0.0;
  8005. double sq_sum = 0.0;
  8006. for (int i = 0; i < ggml_nelements(node); i++) {
  8007. printf("%f ", tmp[i]);
  8008. sum += tmp[i];
  8009. sq_sum += tmp[i]*tmp[i];
  8010. }
  8011. printf("\n");
  8012. printf("sum: %f, ", sum);
  8013. printf("sq_sum: %f\n", sq_sum);
  8014. }
  8015. #endif
  8016. }
  8017. UNUSED(backend);
  8018. }
  8019. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  8020. switch (op->op) {
  8021. case GGML_OP_UNARY:
  8022. switch (ggml_get_unary_op(op)) {
  8023. case GGML_UNARY_OP_GELU:
  8024. case GGML_UNARY_OP_SILU:
  8025. case GGML_UNARY_OP_RELU:
  8026. case GGML_UNARY_OP_GELU_QUICK:
  8027. case GGML_UNARY_OP_TANH:
  8028. return true;
  8029. default:
  8030. return false;
  8031. }
  8032. break;
  8033. case GGML_OP_MUL_MAT:
  8034. case GGML_OP_MUL_MAT_ID:
  8035. {
  8036. struct ggml_tensor * a;
  8037. struct ggml_tensor * b;
  8038. if (op->op == GGML_OP_MUL_MAT) {
  8039. a = op->src[0];
  8040. b = op->src[1];
  8041. } else {
  8042. a = op->src[2];
  8043. b = op->src[1];
  8044. }
  8045. if (a->ne[3] != b->ne[3]) {
  8046. return false;
  8047. }
  8048. return true;
  8049. } break;
  8050. case GGML_OP_GET_ROWS:
  8051. {
  8052. switch (op->src[0]->type) {
  8053. case GGML_TYPE_F16:
  8054. case GGML_TYPE_F32:
  8055. case GGML_TYPE_Q4_0:
  8056. case GGML_TYPE_Q4_1:
  8057. case GGML_TYPE_Q5_0:
  8058. case GGML_TYPE_Q5_1:
  8059. case GGML_TYPE_Q8_0:
  8060. return true;
  8061. default:
  8062. return false;
  8063. }
  8064. } break;
  8065. case GGML_OP_CPY:
  8066. {
  8067. ggml_type src0_type = op->src[0]->type;
  8068. ggml_type src1_type = op->src[1]->type;
  8069. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  8070. return true;
  8071. }
  8072. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  8073. return true;
  8074. }
  8075. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  8076. return true;
  8077. }
  8078. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  8079. return true;
  8080. }
  8081. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  8082. return true;
  8083. }
  8084. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8085. return true;
  8086. }
  8087. return false;
  8088. } break;
  8089. case GGML_OP_NONE:
  8090. case GGML_OP_RESHAPE:
  8091. case GGML_OP_VIEW:
  8092. case GGML_OP_PERMUTE:
  8093. case GGML_OP_TRANSPOSE:
  8094. case GGML_OP_NORM:
  8095. case GGML_OP_REPEAT:
  8096. case GGML_OP_DUP:
  8097. case GGML_OP_ADD:
  8098. case GGML_OP_MUL:
  8099. case GGML_OP_DIV:
  8100. case GGML_OP_RMS_NORM:
  8101. case GGML_OP_SCALE:
  8102. case GGML_OP_SQR:
  8103. case GGML_OP_CLAMP:
  8104. case GGML_OP_CONT:
  8105. case GGML_OP_DIAG_MASK_INF:
  8106. case GGML_OP_SOFT_MAX:
  8107. case GGML_OP_ROPE:
  8108. case GGML_OP_ALIBI:
  8109. case GGML_OP_IM2COL:
  8110. case GGML_OP_SUM_ROWS:
  8111. case GGML_OP_ARGSORT:
  8112. case GGML_OP_ACC:
  8113. case GGML_OP_CONCAT:
  8114. case GGML_OP_GROUP_NORM:
  8115. case GGML_OP_UPSCALE:
  8116. case GGML_OP_PAD:
  8117. case GGML_OP_LEAKY_RELU:
  8118. return true;
  8119. default:
  8120. return false;
  8121. }
  8122. UNUSED(backend);
  8123. }
  8124. static ggml_backend_i cuda_backend_i = {
  8125. /* .get_name = */ ggml_backend_cuda_name,
  8126. /* .free = */ ggml_backend_cuda_free,
  8127. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8128. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8129. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8130. /* .cpy_tensor_from_async = */ NULL,
  8131. /* .cpy_tensor_to_async = */ NULL,
  8132. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8133. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8134. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8135. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8136. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8137. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8138. };
  8139. ggml_backend_t ggml_backend_cuda_init(int device) {
  8140. ggml_init_cublas(); // TODO: remove from ggml.c
  8141. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8142. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8143. return nullptr;
  8144. }
  8145. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8146. ggml_cuda_set_main_device(device);
  8147. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8148. /* .device = */ device
  8149. };
  8150. ggml_backend_t cuda_backend = new ggml_backend {
  8151. /* .interface = */ cuda_backend_i,
  8152. /* .context = */ ctx
  8153. };
  8154. return cuda_backend;
  8155. }
  8156. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8157. return backend->iface.get_name == ggml_backend_cuda_name;
  8158. }
  8159. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8160. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8161. return cuda_backend;
  8162. UNUSED(params);
  8163. }
  8164. extern "C" int ggml_backend_cuda_reg_devices();
  8165. int ggml_backend_cuda_reg_devices() {
  8166. int device_count = ggml_cuda_get_device_count();
  8167. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8168. for (int i = 0; i < device_count; i++) {
  8169. char name[128];
  8170. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8171. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8172. }
  8173. return device_count;
  8174. }