ggml-cuda.cu 370 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140
  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 cudaMemcpyAsync hipMemcpyAsync
  70. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  71. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  72. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  73. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  74. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  75. #define cudaMemcpyKind hipMemcpyKind
  76. #define cudaMemset hipMemset
  77. #define cudaMemsetAsync hipMemsetAsync
  78. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  79. #define cudaSetDevice hipSetDevice
  80. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  81. #define cudaStreamFireAndForget hipStreamFireAndForget
  82. #define cudaStreamNonBlocking hipStreamNonBlocking
  83. #define cudaStreamSynchronize hipStreamSynchronize
  84. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  85. #define cudaStream_t hipStream_t
  86. #define cudaSuccess hipSuccess
  87. #define __trap abort
  88. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  89. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  90. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  91. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  92. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  93. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  94. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  95. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  96. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  97. #else
  98. #include <cuda_runtime.h>
  99. #include <cuda.h>
  100. #include <cublas_v2.h>
  101. #include <cuda_fp16.h>
  102. #if CUDART_VERSION < 11020
  103. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  104. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  105. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  106. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  107. #define cublasComputeType_t cudaDataType_t
  108. #endif // CUDART_VERSION < 11020
  109. #endif // defined(GGML_USE_HIPBLAS)
  110. #include "ggml-cuda.h"
  111. #include "ggml.h"
  112. #include "ggml-backend-impl.h"
  113. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  114. #define CC_VOLTA 700
  115. #define CC_OFFSET_AMD 1000000
  116. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  117. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  118. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  119. #define GGML_CUDA_MAX_NODES 8192
  120. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  121. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  122. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  123. // - 7B quantum model: +100-200 MB
  124. // - 13B quantum model: +200-400 MB
  125. //
  126. //#define GGML_CUDA_FORCE_MMQ
  127. // TODO: improve this to be correct for more hardware
  128. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  129. #if !defined(GGML_CUDA_FORCE_MMQ)
  130. #define CUDA_USE_TENSOR_CORES
  131. #endif
  132. // max batch size to use MMQ kernels when tensor cores are available
  133. #define MMQ_MAX_BATCH_SIZE 32
  134. #if defined(GGML_USE_HIPBLAS)
  135. #define __CUDA_ARCH__ 1300
  136. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  137. defined(__gfx1150__) || defined(__gfx1151__)
  138. #define RDNA3
  139. #endif
  140. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  141. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  142. #define RDNA2
  143. #endif
  144. #ifndef __has_builtin
  145. #define __has_builtin(x) 0
  146. #endif
  147. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  148. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  149. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  150. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  151. #if __has_builtin(__builtin_elementwise_sub_sat)
  152. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  153. return reinterpret_cast<const int &>(c);
  154. #else
  155. int8x4_t c;
  156. int16_t tmp;
  157. #pragma unroll
  158. for (int i = 0; i < 4; i++) {
  159. tmp = va[i] - vb[i];
  160. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  161. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  162. c[i] = tmp;
  163. }
  164. return reinterpret_cast<int &>(c);
  165. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  166. }
  167. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  168. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  169. c = __builtin_amdgcn_sdot4(a, b, c, false);
  170. #elif defined(__gfx1100__)
  171. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  172. #elif defined(__gfx1010__) || defined(__gfx900__)
  173. int tmp1;
  174. int tmp2;
  175. asm("\n \
  176. 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 \
  177. 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 \
  178. v_add3_u32 %0, %1, %2, %0 \n \
  179. 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 \
  180. 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 \
  181. v_add3_u32 %0, %1, %2, %0 \n \
  182. "
  183. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  184. : "v"(a), "v"(b)
  185. );
  186. #else
  187. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  188. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  189. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  190. #endif
  191. return c;
  192. }
  193. #endif // defined(GGML_USE_HIPBLAS)
  194. #if defined(_MSC_VER)
  195. #pragma warning(disable: 4244 4267) // possible loss of data
  196. #endif
  197. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  198. [[noreturn]]
  199. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  200. int id = -1; // in case cudaGetDevice fails
  201. cudaGetDevice(&id);
  202. fprintf(stderr, "CUDA error: %s\n", msg);
  203. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  204. fprintf(stderr, " %s\n", stmt);
  205. // abort with GGML_ASSERT to get a stack trace
  206. GGML_ASSERT(!"CUDA error");
  207. }
  208. #define CUDA_CHECK_GEN(err, success, error_fn) \
  209. do { \
  210. auto err_ = (err); \
  211. if (err_ != (success)) { \
  212. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  213. } \
  214. } while (0)
  215. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  216. #if CUDART_VERSION >= 12000
  217. static const char * cublas_get_error_str(const cublasStatus_t err) {
  218. return cublasGetStatusString(err);
  219. }
  220. #else
  221. static const char * cublas_get_error_str(const cublasStatus_t err) {
  222. switch (err) {
  223. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  224. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  225. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  226. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  227. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  228. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  229. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  230. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  231. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  232. default: return "unknown error";
  233. }
  234. }
  235. #endif // CUDART_VERSION >= 12000
  236. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  237. #if !defined(GGML_USE_HIPBLAS)
  238. static const char * cu_get_error_str(CUresult err) {
  239. const char * err_str;
  240. cuGetErrorString(err, &err_str);
  241. return err_str;
  242. }
  243. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  244. #endif
  245. #if CUDART_VERSION >= 11100
  246. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  247. #else
  248. #define GGML_CUDA_ASSUME(x)
  249. #endif // CUDART_VERSION >= 11100
  250. #ifdef GGML_CUDA_F16
  251. typedef half dfloat; // dequantize float
  252. typedef half2 dfloat2;
  253. #else
  254. typedef float dfloat; // dequantize float
  255. typedef float2 dfloat2;
  256. #endif //GGML_CUDA_F16
  257. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  258. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  259. int x32 = 0;
  260. x32 |= x16[0] << 0;
  261. x32 |= x16[1] << 16;
  262. return x32;
  263. }
  264. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  265. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  266. int x32 = 0;
  267. x32 |= x16[0] << 0;
  268. x32 |= x16[1] << 16;
  269. return x32;
  270. }
  271. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  272. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  273. }
  274. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  275. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  276. }
  277. template<typename T>
  278. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  279. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  280. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  281. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  282. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  283. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  284. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  285. typedef void (*ggml_cuda_op_mul_mat_t)(
  286. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  287. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  288. const int64_t src1_padded_row_size, cudaStream_t stream);
  289. typedef void (*ggml_cuda_op_flatten_t)(
  290. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  291. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  292. // QK = number of values after dequantization
  293. // QR = QK / number of values before dequantization
  294. // QI = number of 32 bit integers before dequantization
  295. #define QK4_0 32
  296. #define QR4_0 2
  297. #define QI4_0 (QK4_0 / (4 * QR4_0))
  298. typedef struct {
  299. half d; // delta
  300. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  301. } block_q4_0;
  302. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  303. #define QK4_1 32
  304. #define QR4_1 2
  305. #define QI4_1 (QK4_1 / (4 * QR4_1))
  306. typedef struct {
  307. half2 dm; // dm.x = delta, dm.y = min
  308. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  309. } block_q4_1;
  310. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  311. #define QK5_0 32
  312. #define QR5_0 2
  313. #define QI5_0 (QK5_0 / (4 * QR5_0))
  314. typedef struct {
  315. half d; // delta
  316. uint8_t qh[4]; // 5-th bit of quants
  317. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  318. } block_q5_0;
  319. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  320. #define QK5_1 32
  321. #define QR5_1 2
  322. #define QI5_1 (QK5_1 / (4 * QR5_1))
  323. typedef struct {
  324. half2 dm; // dm.x = delta, dm.y = min
  325. uint8_t qh[4]; // 5-th bit of quants
  326. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  327. } block_q5_1;
  328. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  329. #define QK8_0 32
  330. #define QR8_0 1
  331. #define QI8_0 (QK8_0 / (4 * QR8_0))
  332. typedef struct {
  333. half d; // delta
  334. int8_t qs[QK8_0]; // quants
  335. } block_q8_0;
  336. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  337. #define QK8_1 32
  338. #define QR8_1 1
  339. #define QI8_1 (QK8_1 / (4 * QR8_1))
  340. typedef struct {
  341. half2 ds; // ds.x = delta, ds.y = sum
  342. int8_t qs[QK8_0]; // quants
  343. } block_q8_1;
  344. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  345. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  346. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  347. typedef void (*load_tiles_cuda_t)(
  348. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  349. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  350. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  351. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  352. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  353. //================================= k-quants
  354. #ifdef GGML_QKK_64
  355. #define QK_K 64
  356. #define K_SCALE_SIZE 4
  357. #else
  358. #define QK_K 256
  359. #define K_SCALE_SIZE 12
  360. #endif
  361. #define QR2_K 4
  362. #define QI2_K (QK_K / (4*QR2_K))
  363. typedef struct {
  364. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  365. uint8_t qs[QK_K/4]; // quants
  366. half2 dm; // super-block scale for quantized scales/mins
  367. } block_q2_K;
  368. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  369. #define QR3_K 4
  370. #define QI3_K (QK_K / (4*QR3_K))
  371. typedef struct {
  372. uint8_t hmask[QK_K/8]; // quants - high bit
  373. uint8_t qs[QK_K/4]; // quants - low 2 bits
  374. #ifdef GGML_QKK_64
  375. uint8_t scales[2]; // scales, quantized with 8 bits
  376. #else
  377. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  378. #endif
  379. half d; // super-block scale
  380. } block_q3_K;
  381. //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");
  382. #define QR4_K 2
  383. #define QI4_K (QK_K / (4*QR4_K))
  384. #ifdef GGML_QKK_64
  385. typedef struct {
  386. half dm[2]; // super-block scales/mins
  387. uint8_t scales[2]; // 4-bit block scales/mins
  388. uint8_t qs[QK_K/2]; // 4--bit quants
  389. } block_q4_K;
  390. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  391. #else
  392. typedef struct {
  393. half2 dm; // super-block scale for quantized scales/mins
  394. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  395. uint8_t qs[QK_K/2]; // 4--bit quants
  396. } block_q4_K;
  397. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  398. #endif
  399. #define QR5_K 2
  400. #define QI5_K (QK_K / (4*QR5_K))
  401. #ifdef GGML_QKK_64
  402. typedef struct {
  403. half d; // super-block scale
  404. int8_t scales[QK_K/16]; // block scales
  405. uint8_t qh[QK_K/8]; // quants, high bit
  406. uint8_t qs[QK_K/2]; // quants, low 4 bits
  407. } block_q5_K;
  408. 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");
  409. #else
  410. typedef struct {
  411. half2 dm; // super-block scale for quantized scales/mins
  412. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  413. uint8_t qh[QK_K/8]; // quants, high bit
  414. uint8_t qs[QK_K/2]; // quants, low 4 bits
  415. } block_q5_K;
  416. 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");
  417. #endif
  418. #define QR6_K 2
  419. #define QI6_K (QK_K / (4*QR6_K))
  420. typedef struct {
  421. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  422. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  423. int8_t scales[QK_K/16]; // scales
  424. half d; // delta
  425. } block_q6_K;
  426. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  427. #define WARP_SIZE 32
  428. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  429. #define CUDA_GELU_BLOCK_SIZE 256
  430. #define CUDA_SILU_BLOCK_SIZE 256
  431. #define CUDA_TANH_BLOCK_SIZE 256
  432. #define CUDA_RELU_BLOCK_SIZE 256
  433. #define CUDA_SQR_BLOCK_SIZE 256
  434. #define CUDA_CPY_BLOCK_SIZE 32
  435. #define CUDA_SCALE_BLOCK_SIZE 256
  436. #define CUDA_CLAMP_BLOCK_SIZE 256
  437. #define CUDA_ROPE_BLOCK_SIZE 256
  438. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  439. #define CUDA_ALIBI_BLOCK_SIZE 32
  440. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  441. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  442. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  443. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  444. #define CUDA_UPSCALE_BLOCK_SIZE 256
  445. #define CUDA_CONCAT_BLOCK_SIZE 256
  446. #define CUDA_PAD_BLOCK_SIZE 256
  447. #define CUDA_ACC_BLOCK_SIZE 256
  448. #define CUDA_IM2COL_BLOCK_SIZE 256
  449. // dmmv = dequantize_mul_mat_vec
  450. #ifndef GGML_CUDA_DMMV_X
  451. #define GGML_CUDA_DMMV_X 32
  452. #endif
  453. #ifndef GGML_CUDA_MMV_Y
  454. #define GGML_CUDA_MMV_Y 1
  455. #endif
  456. #ifndef K_QUANTS_PER_ITERATION
  457. #define K_QUANTS_PER_ITERATION 2
  458. #else
  459. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  460. #endif
  461. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  462. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  463. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  464. #define MUL_MAT_SRC1_COL_STRIDE 128
  465. #define MAX_STREAMS 8
  466. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  467. struct ggml_tensor_extra_gpu {
  468. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  469. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  470. };
  471. // this is faster on Windows
  472. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  473. static void ggml_cuda_set_device(const int device) {
  474. int current_device;
  475. CUDA_CHECK(cudaGetDevice(&current_device));
  476. if (device == current_device) {
  477. return;
  478. }
  479. CUDA_CHECK(cudaSetDevice(device));
  480. }
  481. static int g_device_count = -1;
  482. static int g_main_device = 0;
  483. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  484. struct cuda_device_capabilities {
  485. int cc; // compute capability
  486. bool vmm; // virtual memory support
  487. size_t vmm_granularity; // granularity of virtual memory
  488. };
  489. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, false, 0} };
  490. static void * g_scratch_buffer = nullptr;
  491. static size_t g_scratch_size = 0; // disabled by default
  492. static size_t g_scratch_offset = 0;
  493. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  494. [[noreturn]]
  495. static __device__ void bad_arch() {
  496. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  497. __trap();
  498. (void) bad_arch; // suppress unused function warning
  499. }
  500. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  501. #pragma unroll
  502. for (int mask = 16; mask > 0; mask >>= 1) {
  503. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  504. }
  505. return x;
  506. }
  507. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  508. #pragma unroll
  509. for (int mask = 16; mask > 0; mask >>= 1) {
  510. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  511. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  512. }
  513. return a;
  514. }
  515. static __device__ __forceinline__ float warp_reduce_max(float x) {
  516. #pragma unroll
  517. for (int mask = 16; mask > 0; mask >>= 1) {
  518. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  519. }
  520. return x;
  521. }
  522. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  523. return b;
  524. GGML_UNUSED(a);
  525. }
  526. static __device__ __forceinline__ float op_add(const float a, const float b) {
  527. return a + b;
  528. }
  529. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  530. return a * b;
  531. }
  532. static __device__ __forceinline__ float op_div(const float a, const float b) {
  533. return a / b;
  534. }
  535. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  536. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  537. int ne0, int ne1, int ne2, int ne3,
  538. int ne10, int ne11, int ne12, int ne13,
  539. /*int s0, */ int s1, int s2, int s3,
  540. /*int s10,*/ int s11, int s12, int s13) {
  541. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  542. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  543. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  544. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  545. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  546. return;
  547. }
  548. const int i11 = i1 % ne11;
  549. const int i12 = i2 % ne12;
  550. const int i13 = i3 % ne13;
  551. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  552. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  553. const size_t i_dst = i_src0;
  554. const src0_t * src0_row = src0 + i_src0;
  555. const src1_t * src1_row = src1 + i_src1;
  556. dst_t * dst_row = dst + i_dst;
  557. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  558. const int i10 = i0 % ne10;
  559. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  560. }
  561. }
  562. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  563. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  564. int ne0, int ne1, int ne2, int ne3,
  565. int ne10, int ne11, int ne12, int ne13,
  566. /*int s0, */ int s1, int s2, int s3,
  567. /*int s10,*/ int s11, int s12, int s13) {
  568. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  569. const int i3 = i/(ne2*ne1*ne0);
  570. const int i2 = (i/(ne1*ne0)) % ne2;
  571. const int i1 = (i/ne0) % ne1;
  572. const int i0 = i % ne0;
  573. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  574. return;
  575. }
  576. const int i11 = i1 % ne11;
  577. const int i12 = i2 % ne12;
  578. const int i13 = i3 % ne13;
  579. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  580. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  581. const size_t i_dst = i_src0;
  582. const src0_t * src0_row = src0 + i_src0;
  583. const src1_t * src1_row = src1 + i_src1;
  584. dst_t * dst_row = dst + i_dst;
  585. const int i10 = i0 % ne10;
  586. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  587. }
  588. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  589. const int ne10, const int ne11, const int ne12,
  590. const int nb1, const int nb2, int offset) {
  591. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  592. if (i >= ne) {
  593. return;
  594. }
  595. int src1_idx = i - offset;
  596. int oz = src1_idx / nb2;
  597. int oy = (src1_idx - (oz * nb2)) / nb1;
  598. int ox = src1_idx % nb1;
  599. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  600. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  601. } else {
  602. dst[i] = x[i];
  603. }
  604. }
  605. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  606. const float GELU_COEF_A = 0.044715f;
  607. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  608. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  609. if (i >= k) {
  610. return;
  611. }
  612. float xi = x[i];
  613. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  614. }
  615. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  616. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  617. if (i >= k) {
  618. return;
  619. }
  620. dst[i] = x[i] / (1.0f + expf(-x[i]));
  621. }
  622. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  623. const float GELU_QUICK_COEF = -1.702f;
  624. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  625. if (i >= k) {
  626. return;
  627. }
  628. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  629. }
  630. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  631. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  632. if (i >= k) {
  633. return;
  634. }
  635. dst[i] = tanhf(x[i]);
  636. }
  637. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  638. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  639. if (i >= k) {
  640. return;
  641. }
  642. dst[i] = fmaxf(x[i], 0);
  643. }
  644. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  645. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  646. if (i >= k) {
  647. return;
  648. }
  649. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  650. }
  651. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  652. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  653. if (i >= k) {
  654. return;
  655. }
  656. dst[i] = x[i] * x[i];
  657. }
  658. template <int block_size>
  659. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  660. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  661. const int tid = threadIdx.x;
  662. float2 mean_var = make_float2(0.f, 0.f);
  663. for (int col = tid; col < ncols; col += block_size) {
  664. const float xi = x[row*ncols + col];
  665. mean_var.x += xi;
  666. mean_var.y += xi * xi;
  667. }
  668. // sum up partial sums
  669. mean_var = warp_reduce_sum(mean_var);
  670. if (block_size > WARP_SIZE) {
  671. __shared__ float2 s_sum[32];
  672. int warp_id = threadIdx.x / WARP_SIZE;
  673. int lane_id = threadIdx.x % WARP_SIZE;
  674. if (lane_id == 0) {
  675. s_sum[warp_id] = mean_var;
  676. }
  677. __syncthreads();
  678. mean_var = s_sum[lane_id];
  679. mean_var = warp_reduce_sum(mean_var);
  680. }
  681. const float mean = mean_var.x / ncols;
  682. const float var = mean_var.y / ncols - mean * mean;
  683. const float inv_std = rsqrtf(var + eps);
  684. for (int col = tid; col < ncols; col += block_size) {
  685. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  686. }
  687. }
  688. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  689. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  690. if (nidx >= ne0) {
  691. return;
  692. }
  693. // operation
  694. int offset_dst =
  695. nidx +
  696. blockIdx.y * ne0 +
  697. blockIdx.z * ne0 * gridDim.y;
  698. if (blockIdx.z < ne02) { // src0
  699. int offset_src =
  700. nidx +
  701. blockIdx.y * ne0 +
  702. blockIdx.z * ne0 * gridDim.y;
  703. dst[offset_dst] = x[offset_src];
  704. } else {
  705. int offset_src =
  706. nidx +
  707. blockIdx.y * ne0 +
  708. (blockIdx.z - ne02) * ne0 * gridDim.y;
  709. dst[offset_dst] = y[offset_src];
  710. }
  711. }
  712. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  713. int ne0 = ne00 * scale_factor;
  714. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  715. if (nidx >= ne0) {
  716. return;
  717. }
  718. // operation
  719. int i00 = nidx / scale_factor;
  720. int i01 = blockIdx.y / scale_factor;
  721. int offset_src =
  722. i00 +
  723. i01 * ne00 +
  724. blockIdx.z * nb02;
  725. int offset_dst =
  726. nidx +
  727. blockIdx.y * ne0 +
  728. blockIdx.z * ne0 * gridDim.y;
  729. dst[offset_dst] = x[offset_src];
  730. }
  731. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  732. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  733. if (nidx >= ne0) {
  734. return;
  735. }
  736. // operation
  737. int offset_dst =
  738. nidx +
  739. blockIdx.y * ne0 +
  740. blockIdx.z * ne0 * gridDim.y;
  741. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  742. int offset_src =
  743. nidx +
  744. blockIdx.y * ne00 +
  745. blockIdx.z * ne00 * ne01;
  746. dst[offset_dst] = x[offset_src];
  747. } else {
  748. dst[offset_dst] = 0.0f;
  749. }
  750. }
  751. template <int block_size>
  752. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  753. int start = blockIdx.x * group_size;
  754. int end = start + group_size;
  755. start += threadIdx.x;
  756. if (end >= ne_elements) {
  757. end = ne_elements;
  758. }
  759. float tmp = 0.0f; // partial sum for thread in warp
  760. for (int j = start; j < end; j += block_size) {
  761. tmp += x[j];
  762. }
  763. tmp = warp_reduce_sum(tmp);
  764. if (block_size > WARP_SIZE) {
  765. __shared__ float s_sum[32];
  766. int warp_id = threadIdx.x / WARP_SIZE;
  767. int lane_id = threadIdx.x % WARP_SIZE;
  768. if (lane_id == 0) {
  769. s_sum[warp_id] = tmp;
  770. }
  771. __syncthreads();
  772. tmp = s_sum[lane_id];
  773. tmp = warp_reduce_sum(tmp);
  774. }
  775. float mean = tmp / group_size;
  776. tmp = 0.0f;
  777. for (int j = start; j < end; j += block_size) {
  778. float xi = x[j] - mean;
  779. dst[j] = xi;
  780. tmp += xi * xi;
  781. }
  782. tmp = warp_reduce_sum(tmp);
  783. if (block_size > WARP_SIZE) {
  784. __shared__ float s_sum[32];
  785. int warp_id = threadIdx.x / WARP_SIZE;
  786. int lane_id = threadIdx.x % WARP_SIZE;
  787. if (lane_id == 0) {
  788. s_sum[warp_id] = tmp;
  789. }
  790. __syncthreads();
  791. tmp = s_sum[lane_id];
  792. tmp = warp_reduce_sum(tmp);
  793. }
  794. float variance = tmp / group_size;
  795. float scale = rsqrtf(variance + eps);
  796. for (int j = start; j < end; j += block_size) {
  797. dst[j] *= scale;
  798. }
  799. }
  800. template <int block_size>
  801. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  802. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  803. const int tid = threadIdx.x;
  804. float tmp = 0.0f; // partial sum for thread in warp
  805. for (int col = tid; col < ncols; col += block_size) {
  806. const float xi = x[row*ncols + col];
  807. tmp += xi * xi;
  808. }
  809. // sum up partial sums
  810. tmp = warp_reduce_sum(tmp);
  811. if (block_size > WARP_SIZE) {
  812. __shared__ float s_sum[32];
  813. int warp_id = threadIdx.x / WARP_SIZE;
  814. int lane_id = threadIdx.x % WARP_SIZE;
  815. if (lane_id == 0) {
  816. s_sum[warp_id] = tmp;
  817. }
  818. __syncthreads();
  819. tmp = s_sum[lane_id];
  820. tmp = warp_reduce_sum(tmp);
  821. }
  822. const float mean = tmp / ncols;
  823. const float scale = rsqrtf(mean + eps);
  824. for (int col = tid; col < ncols; col += block_size) {
  825. dst[row*ncols + col] = scale * x[row*ncols + col];
  826. }
  827. }
  828. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  829. const block_q4_0 * x = (const block_q4_0 *) vx;
  830. const dfloat d = x[ib].d;
  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 = __hsub2(v, {8.0f, 8.0f});
  836. v = __hmul2(v, {d, d});
  837. #else
  838. v.x = (v.x - 8.0f) * d;
  839. v.y = (v.y - 8.0f) * d;
  840. #endif // GGML_CUDA_F16
  841. }
  842. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  843. const block_q4_1 * x = (const block_q4_1 *) vx;
  844. const dfloat d = __low2half(x[ib].dm);
  845. const dfloat m = __high2half(x[ib].dm);
  846. const int vui = x[ib].qs[iqs];
  847. v.x = vui & 0xF;
  848. v.y = vui >> 4;
  849. #ifdef GGML_CUDA_F16
  850. v = __hmul2(v, {d, d});
  851. v = __hadd2(v, {m, m});
  852. #else
  853. v.x = (v.x * d) + m;
  854. v.y = (v.y * d) + m;
  855. #endif // GGML_CUDA_F16
  856. }
  857. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  858. const block_q5_0 * x = (const block_q5_0 *) vx;
  859. const dfloat d = x[ib].d;
  860. uint32_t qh;
  861. memcpy(&qh, x[ib].qh, sizeof(qh));
  862. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  863. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  864. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  865. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  866. #ifdef GGML_CUDA_F16
  867. v = __hsub2(v, {16.0f, 16.0f});
  868. v = __hmul2(v, {d, d});
  869. #else
  870. v.x = (v.x - 16.0f) * d;
  871. v.y = (v.y - 16.0f) * d;
  872. #endif // GGML_CUDA_F16
  873. }
  874. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  875. const block_q5_1 * x = (const block_q5_1 *) vx;
  876. const dfloat d = __low2half(x[ib].dm);
  877. const dfloat m = __high2half(x[ib].dm);
  878. uint32_t qh;
  879. memcpy(&qh, x[ib].qh, sizeof(qh));
  880. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  881. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  882. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  883. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  884. #ifdef GGML_CUDA_F16
  885. v = __hmul2(v, {d, d});
  886. v = __hadd2(v, {m, m});
  887. #else
  888. v.x = (v.x * d) + m;
  889. v.y = (v.y * d) + m;
  890. #endif // GGML_CUDA_F16
  891. }
  892. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  893. const block_q8_0 * x = (const block_q8_0 *) vx;
  894. const dfloat d = x[ib].d;
  895. v.x = x[ib].qs[iqs + 0];
  896. v.y = x[ib].qs[iqs + 1];
  897. #ifdef GGML_CUDA_F16
  898. v = __hmul2(v, {d, d});
  899. #else
  900. v.x *= d;
  901. v.y *= d;
  902. #endif // GGML_CUDA_F16
  903. }
  904. //================================== k-quants
  905. template<typename dst_t>
  906. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  907. const int i = blockIdx.x;
  908. const block_q2_K * x = (const block_q2_K *) vx;
  909. const int tid = threadIdx.x;
  910. #if QK_K == 256
  911. const int n = tid/32;
  912. const int l = tid - 32*n;
  913. const int is = 8*n + l/16;
  914. const uint8_t q = x[i].qs[32*n + l];
  915. dst_t * y = yy + i*QK_K + 128*n;
  916. float dall = __low2half(x[i].dm);
  917. float dmin = __high2half(x[i].dm);
  918. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  919. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  920. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  921. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  922. #else
  923. const int is = tid/16; // 0 or 1
  924. const int il = tid%16; // 0...15
  925. const uint8_t q = x[i].qs[il] >> (2*is);
  926. dst_t * y = yy + i*QK_K + 16*is + il;
  927. float dall = __low2half(x[i].dm);
  928. float dmin = __high2half(x[i].dm);
  929. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  930. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  931. #endif
  932. }
  933. template<typename dst_t>
  934. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  935. const int i = blockIdx.x;
  936. const block_q3_K * x = (const block_q3_K *) vx;
  937. #if QK_K == 256
  938. const int r = threadIdx.x/4;
  939. const int tid = r/2;
  940. const int is0 = r%2;
  941. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  942. const int n = tid / 4;
  943. const int j = tid - 4*n;
  944. uint8_t m = 1 << (4*n + j);
  945. int is = 8*n + 2*j + is0;
  946. int shift = 2*j;
  947. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  948. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  949. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  950. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  951. float d_all = x[i].d;
  952. float dl = d_all * (us - 32);
  953. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  954. const uint8_t * q = x[i].qs + 32*n;
  955. const uint8_t * hm = x[i].hmask;
  956. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  957. #else
  958. const int tid = threadIdx.x;
  959. const int is = tid/16; // 0 or 1
  960. const int il = tid%16; // 0...15
  961. const int im = il/8; // 0...1
  962. const int in = il%8; // 0...7
  963. dst_t * y = yy + i*QK_K + 16*is + il;
  964. const uint8_t q = x[i].qs[il] >> (2*is);
  965. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  966. const float d = (float)x[i].d;
  967. if (is == 0) {
  968. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  969. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  970. } else {
  971. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  972. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  973. }
  974. #endif
  975. }
  976. #if QK_K == 256
  977. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  978. if (j < 4) {
  979. d = q[j] & 63; m = q[j + 4] & 63;
  980. } else {
  981. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  982. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  983. }
  984. }
  985. #endif
  986. template<typename dst_t>
  987. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  988. const block_q4_K * x = (const block_q4_K *) vx;
  989. const int i = blockIdx.x;
  990. #if QK_K == 256
  991. // assume 32 threads
  992. const int tid = threadIdx.x;
  993. const int il = tid/8;
  994. const int ir = tid%8;
  995. const int is = 2*il;
  996. const int n = 4;
  997. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  998. const float dall = __low2half(x[i].dm);
  999. const float dmin = __high2half(x[i].dm);
  1000. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1001. uint8_t sc, m;
  1002. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1003. const float d1 = dall * sc; const float m1 = dmin * m;
  1004. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1005. const float d2 = dall * sc; const float m2 = dmin * m;
  1006. for (int l = 0; l < n; ++l) {
  1007. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1008. y[l +32] = d2 * (q[l] >> 4) - m2;
  1009. }
  1010. #else
  1011. const int tid = threadIdx.x;
  1012. const uint8_t * q = x[i].qs;
  1013. dst_t * y = yy + i*QK_K;
  1014. const float d = (float)x[i].dm[0];
  1015. const float m = (float)x[i].dm[1];
  1016. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1017. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1018. #endif
  1019. }
  1020. template<typename dst_t>
  1021. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1022. const block_q5_K * x = (const block_q5_K *) vx;
  1023. const int i = blockIdx.x;
  1024. #if QK_K == 256
  1025. // assume 64 threads - this is very slightly better than the one below
  1026. const int tid = threadIdx.x;
  1027. const int il = tid/16; // il is in 0...3
  1028. const int ir = tid%16; // ir is in 0...15
  1029. const int is = 2*il; // is is in 0...6
  1030. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1031. const float dall = __low2half(x[i].dm);
  1032. const float dmin = __high2half(x[i].dm);
  1033. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1034. const uint8_t * qh = x[i].qh + 2*ir;
  1035. uint8_t sc, m;
  1036. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1037. const float d1 = dall * sc; const float m1 = dmin * m;
  1038. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1039. const float d2 = dall * sc; const float m2 = dmin * m;
  1040. uint8_t hm = 1 << (2*il);
  1041. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1042. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1043. hm <<= 1;
  1044. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1045. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1046. #else
  1047. const int tid = threadIdx.x;
  1048. const uint8_t q = x[i].qs[tid];
  1049. const int im = tid/8; // 0...3
  1050. const int in = tid%8; // 0...7
  1051. const int is = tid/16; // 0 or 1
  1052. const uint8_t h = x[i].qh[in] >> im;
  1053. const float d = x[i].d;
  1054. dst_t * y = yy + i*QK_K + tid;
  1055. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1056. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1057. #endif
  1058. }
  1059. template<typename dst_t>
  1060. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1061. const block_q6_K * x = (const block_q6_K *) vx;
  1062. const int i = blockIdx.x;
  1063. #if QK_K == 256
  1064. // assume 64 threads - this is very slightly better than the one below
  1065. const int tid = threadIdx.x;
  1066. const int ip = tid/32; // ip is 0 or 1
  1067. const int il = tid - 32*ip; // 0...32
  1068. const int is = 8*ip + il/16;
  1069. dst_t * y = yy + i*QK_K + 128*ip + il;
  1070. const float d = x[i].d;
  1071. const uint8_t * ql = x[i].ql + 64*ip + il;
  1072. const uint8_t qh = x[i].qh[32*ip + il];
  1073. const int8_t * sc = x[i].scales + is;
  1074. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1075. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1076. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1077. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1078. #else
  1079. // assume 32 threads
  1080. const int tid = threadIdx.x;
  1081. const int ip = tid/16; // 0 or 1
  1082. const int il = tid - 16*ip; // 0...15
  1083. dst_t * y = yy + i*QK_K + 16*ip + il;
  1084. const float d = x[i].d;
  1085. const uint8_t ql = x[i].ql[16*ip + il];
  1086. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1087. const int8_t * sc = x[i].scales;
  1088. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1089. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1090. #endif
  1091. }
  1092. 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) {
  1093. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1094. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1095. if (row > nrows) return;
  1096. const int num_blocks_per_row = ncols / QK_K;
  1097. const int ib0 = row*num_blocks_per_row;
  1098. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1099. float tmp = 0; // partial sum for thread in warp
  1100. #if QK_K == 256
  1101. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1102. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1103. const int step = 16/K_QUANTS_PER_ITERATION;
  1104. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1105. const int in = tid - step*im; // 0...15 or 0...7
  1106. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1107. const int q_offset = 32*im + l0;
  1108. const int s_offset = 8*im;
  1109. const int y_offset = 128*im + l0;
  1110. uint32_t aux[4];
  1111. const uint8_t * d = (const uint8_t *)aux;
  1112. const uint8_t * m = (const uint8_t *)(aux + 2);
  1113. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1114. const float * y = yy + i * QK_K + y_offset;
  1115. const uint8_t * q = x[i].qs + q_offset;
  1116. const float dall = __low2half(x[i].dm);
  1117. const float dmin = __high2half(x[i].dm);
  1118. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1119. aux[0] = a[0] & 0x0f0f0f0f;
  1120. aux[1] = a[1] & 0x0f0f0f0f;
  1121. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1122. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1123. float sum1 = 0, sum2 = 0;
  1124. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1125. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1126. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1127. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1128. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1129. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1130. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1131. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1132. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1133. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1134. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1135. }
  1136. tmp += dall * sum1 - dmin * sum2;
  1137. }
  1138. #else
  1139. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1140. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1141. const int offset = tid * K_QUANTS_PER_ITERATION;
  1142. uint32_t uaux[2];
  1143. const uint8_t * d = (const uint8_t *)uaux;
  1144. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1145. const float * y = yy + i * QK_K + offset;
  1146. const uint8_t * q = x[i].qs + offset;
  1147. const uint32_t * s = (const uint32_t *)x[i].scales;
  1148. uaux[0] = s[0] & 0x0f0f0f0f;
  1149. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1150. const float2 dall = __half22float2(x[i].dm);
  1151. float sum1 = 0, sum2 = 0;
  1152. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1153. const uint8_t ql = q[l];
  1154. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1155. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1156. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1157. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1158. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1159. }
  1160. tmp += dall.x * sum1 - dall.y * sum2;
  1161. }
  1162. #endif
  1163. // sum up partial sums and write back result
  1164. #pragma unroll
  1165. for (int mask = 16; mask > 0; mask >>= 1) {
  1166. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1167. }
  1168. if (threadIdx.x == 0) {
  1169. dst[row] = tmp;
  1170. }
  1171. }
  1172. 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) {
  1173. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1174. if (row > nrows) return;
  1175. const int num_blocks_per_row = ncols / QK_K;
  1176. const int ib0 = row*num_blocks_per_row;
  1177. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1178. float tmp = 0; // partial sum for thread in warp
  1179. #if QK_K == 256
  1180. const uint16_t kmask1 = 0x0303;
  1181. const uint16_t kmask2 = 0x0f0f;
  1182. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1183. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1184. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1185. const int step = 16/K_QUANTS_PER_ITERATION;
  1186. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1187. const int in = tid - step*im; // 0....15 or 0...7
  1188. const uint8_t m = 1 << (4*im);
  1189. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1190. const int q_offset = 32*im + l0;
  1191. const int y_offset = 128*im + l0;
  1192. uint16_t utmp[4];
  1193. const int8_t * s = (const int8_t *)utmp;
  1194. const uint16_t s_shift = 4*im;
  1195. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1196. const float * y = yy + i * QK_K + y_offset;
  1197. const uint8_t * q = x[i].qs + q_offset;
  1198. const uint8_t * h = x[i].hmask + l0;
  1199. const uint16_t * a = (const uint16_t *)x[i].scales;
  1200. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1201. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1202. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1203. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1204. const float d = x[i].d;
  1205. float sum = 0;
  1206. for (int l = 0; l < n; ++l) {
  1207. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1208. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1209. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1210. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1211. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1212. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1213. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1214. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1215. }
  1216. tmp += d * sum;
  1217. }
  1218. #else
  1219. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1220. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1221. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1222. const int in = offset/8; // 0 or 1
  1223. const int im = offset%8; // 0...7
  1224. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1225. const float * y = yy + i * QK_K + offset;
  1226. const uint8_t * q = x[i].qs + offset;
  1227. const uint8_t * s = x[i].scales;
  1228. const float dall = (float)x[i].d;
  1229. float sum = 0;
  1230. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1231. const uint8_t hl = x[i].hmask[im+l] >> in;
  1232. const uint8_t ql = q[l];
  1233. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1234. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1235. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1236. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1237. }
  1238. tmp += sum;
  1239. }
  1240. #endif
  1241. // sum up partial sums and write back result
  1242. #pragma unroll
  1243. for (int mask = 16; mask > 0; mask >>= 1) {
  1244. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1245. }
  1246. if (threadIdx.x == 0) {
  1247. dst[row] = tmp;
  1248. }
  1249. }
  1250. 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) {
  1251. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1252. if (row > nrows) return;
  1253. const int num_blocks_per_row = ncols / QK_K;
  1254. const int ib0 = row*num_blocks_per_row;
  1255. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1256. #if QK_K == 256
  1257. const uint16_t kmask1 = 0x3f3f;
  1258. const uint16_t kmask2 = 0x0f0f;
  1259. const uint16_t kmask3 = 0xc0c0;
  1260. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1261. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1262. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1263. const int il = tid/step; // 0...3
  1264. const int ir = tid - step*il; // 0...7 or 0...3
  1265. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1266. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1267. const int in = il%2;
  1268. const int l0 = n*(2*ir + in);
  1269. const int q_offset = 32*im + l0;
  1270. const int y_offset = 64*im + l0;
  1271. uint16_t aux[4];
  1272. const uint8_t * sc = (const uint8_t *)aux;
  1273. #if K_QUANTS_PER_ITERATION == 2
  1274. uint32_t q32[4];
  1275. const uint8_t * q4 = (const uint8_t *)q32;
  1276. #else
  1277. uint16_t q16[4];
  1278. const uint8_t * q4 = (const uint8_t *)q16;
  1279. #endif
  1280. float tmp = 0; // partial sum for thread in warp
  1281. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1282. const float * y1 = yy + i*QK_K + y_offset;
  1283. const float * y2 = y1 + 128;
  1284. const float dall = __low2half(x[i].dm);
  1285. const float dmin = __high2half(x[i].dm);
  1286. const uint16_t * a = (const uint16_t *)x[i].scales;
  1287. aux[0] = a[im+0] & kmask1;
  1288. aux[1] = a[im+2] & kmask1;
  1289. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1290. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1291. #if K_QUANTS_PER_ITERATION == 2
  1292. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1293. const uint32_t * q2 = q1 + 16;
  1294. q32[0] = q1[0] & 0x0f0f0f0f;
  1295. q32[1] = q1[0] & 0xf0f0f0f0;
  1296. q32[2] = q2[0] & 0x0f0f0f0f;
  1297. q32[3] = q2[0] & 0xf0f0f0f0;
  1298. float4 s = {0.f, 0.f, 0.f, 0.f};
  1299. float smin = 0;
  1300. for (int l = 0; l < 4; ++l) {
  1301. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1302. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  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. #else
  1307. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1308. const uint16_t * q2 = q1 + 32;
  1309. q16[0] = q1[0] & 0x0f0f;
  1310. q16[1] = q1[0] & 0xf0f0;
  1311. q16[2] = q2[0] & 0x0f0f;
  1312. q16[3] = q2[0] & 0xf0f0;
  1313. float4 s = {0.f, 0.f, 0.f, 0.f};
  1314. float smin = 0;
  1315. for (int l = 0; l < 2; ++l) {
  1316. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1317. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1318. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1319. }
  1320. 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;
  1321. #endif
  1322. }
  1323. #else
  1324. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1325. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1326. const int step = tid * K_QUANTS_PER_ITERATION;
  1327. uint16_t aux16[2];
  1328. const uint8_t * s = (const uint8_t *)aux16;
  1329. float tmp = 0;
  1330. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1331. const uint8_t * q = x[i].qs + step;
  1332. const float * y = yy + i*QK_K + step;
  1333. const uint16_t * a = (const uint16_t *)x[i].scales;
  1334. aux16[0] = a[0] & 0x0f0f;
  1335. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1336. const float d = (float)x[i].dm[0];
  1337. const float m = (float)x[i].dm[1];
  1338. float sum = 0.f;
  1339. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1340. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1341. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1342. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1343. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1344. }
  1345. tmp += sum;
  1346. }
  1347. #endif
  1348. // sum up partial sums and write back result
  1349. #pragma unroll
  1350. for (int mask = 16; mask > 0; mask >>= 1) {
  1351. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1352. }
  1353. if (tid == 0) {
  1354. dst[row] = tmp;
  1355. }
  1356. }
  1357. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1358. const int row = blockIdx.x;
  1359. const int num_blocks_per_row = ncols / QK_K;
  1360. const int ib0 = row*num_blocks_per_row;
  1361. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1362. float tmp = 0; // partial sum for thread in warp
  1363. #if QK_K == 256
  1364. const uint16_t kmask1 = 0x3f3f;
  1365. const uint16_t kmask2 = 0x0f0f;
  1366. const uint16_t kmask3 = 0xc0c0;
  1367. const int tid = threadIdx.x/2; // 0...15
  1368. const int ix = threadIdx.x%2;
  1369. const int il = tid/4; // 0...3
  1370. const int ir = tid - 4*il;// 0...3
  1371. const int n = 2;
  1372. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1373. const int in = il%2;
  1374. const int l0 = n*(2*ir + in);
  1375. const int q_offset = 32*im + l0;
  1376. const int y_offset = 64*im + l0;
  1377. const uint8_t hm1 = 1 << (2*im);
  1378. const uint8_t hm2 = hm1 << 4;
  1379. uint16_t aux[4];
  1380. const uint8_t * sc = (const uint8_t *)aux;
  1381. uint16_t q16[8];
  1382. const uint8_t * q4 = (const uint8_t *)q16;
  1383. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1384. const uint8_t * ql1 = x[i].qs + q_offset;
  1385. const uint8_t * qh = x[i].qh + l0;
  1386. const float * y1 = yy + i*QK_K + y_offset;
  1387. const float * y2 = y1 + 128;
  1388. const float dall = __low2half(x[i].dm);
  1389. const float dmin = __high2half(x[i].dm);
  1390. const uint16_t * a = (const uint16_t *)x[i].scales;
  1391. aux[0] = a[im+0] & kmask1;
  1392. aux[1] = a[im+2] & kmask1;
  1393. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1394. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1395. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1396. float smin = 0;
  1397. const uint16_t * q1 = (const uint16_t *)ql1;
  1398. const uint16_t * q2 = q1 + 32;
  1399. q16[0] = q1[0] & 0x0f0f;
  1400. q16[1] = q1[8] & 0x0f0f;
  1401. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1402. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1403. q16[4] = q2[0] & 0x0f0f;
  1404. q16[5] = q2[8] & 0x0f0f;
  1405. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1406. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1407. for (int l = 0; l < n; ++l) {
  1408. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1409. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1410. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1411. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1412. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1413. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1414. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1415. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1416. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1417. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1418. }
  1419. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1420. }
  1421. #else
  1422. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1423. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1424. const int step = tid * K_QUANTS_PER_ITERATION;
  1425. const int im = step/8;
  1426. const int in = step%8;
  1427. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1428. const uint8_t * q = x[i].qs + step;
  1429. const int8_t * s = x[i].scales;
  1430. const float * y = yy + i*QK_K + step;
  1431. const float d = x[i].d;
  1432. float sum = 0.f;
  1433. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1434. const uint8_t h = x[i].qh[in+j] >> im;
  1435. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1436. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1437. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1438. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1439. }
  1440. tmp += sum;
  1441. }
  1442. #endif
  1443. // sum up partial sums and write back result
  1444. #pragma unroll
  1445. for (int mask = 16; mask > 0; mask >>= 1) {
  1446. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1447. }
  1448. if (threadIdx.x == 0) {
  1449. dst[row] = tmp;
  1450. }
  1451. }
  1452. 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) {
  1453. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1454. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1455. if (row > nrows) return;
  1456. const int num_blocks_per_row = ncols / QK_K;
  1457. const int ib0 = row*num_blocks_per_row;
  1458. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1459. #if QK_K == 256
  1460. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1461. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1462. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1463. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1464. const int in = tid - step*im; // 0...15 or 0...7
  1465. #if K_QUANTS_PER_ITERATION == 1
  1466. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1467. const int is = 0;
  1468. #else
  1469. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1470. const int is = in / 4;
  1471. #endif
  1472. const int ql_offset = 64*im + l0;
  1473. const int qh_offset = 32*im + l0;
  1474. const int s_offset = 8*im + is;
  1475. const int y_offset = 128*im + l0;
  1476. float tmp = 0; // partial sum for thread in warp
  1477. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1478. const float * y = yy + i * QK_K + y_offset;
  1479. const uint8_t * ql = x[i].ql + ql_offset;
  1480. const uint8_t * qh = x[i].qh + qh_offset;
  1481. const int8_t * s = x[i].scales + s_offset;
  1482. const float d = x[i].d;
  1483. #if K_QUANTS_PER_ITERATION == 1
  1484. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1485. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1486. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1487. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1488. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1489. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1490. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1491. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1492. tmp += sum;
  1493. #else
  1494. float sum = 0;
  1495. for (int l = 0; l < 4; ++l) {
  1496. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1497. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1498. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1499. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1500. }
  1501. tmp += sum;
  1502. #endif
  1503. }
  1504. #else
  1505. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1506. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1507. const int step = tid * K_QUANTS_PER_ITERATION;
  1508. float tmp = 0; // partial sum for thread in warp
  1509. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1510. const float * y = yy + i * QK_K + step;
  1511. const uint8_t * ql = x[i].ql + step;
  1512. const uint8_t * qh = x[i].qh + step;
  1513. const int8_t * s = x[i].scales;
  1514. const float d = x[i+0].d;
  1515. float sum = 0;
  1516. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1517. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1518. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1519. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1520. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1521. }
  1522. tmp += sum;
  1523. }
  1524. #endif
  1525. // sum up partial sums and write back result
  1526. #pragma unroll
  1527. for (int mask = 16; mask > 0; mask >>= 1) {
  1528. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1529. }
  1530. if (tid == 0) {
  1531. dst[row] = tmp;
  1532. }
  1533. }
  1534. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1535. const half * x = (const half *) vx;
  1536. // automatic half -> float type cast if dfloat == float
  1537. v.x = x[ib + iqs + 0];
  1538. v.y = x[ib + iqs + 1];
  1539. }
  1540. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1541. const float * x = (const float *) vx;
  1542. // automatic half -> float type cast if dfloat == float
  1543. v.x = x[ib + iqs + 0];
  1544. v.y = x[ib + iqs + 1];
  1545. }
  1546. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1547. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1548. if (ix >= kx_padded) {
  1549. return;
  1550. }
  1551. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1552. const int i_padded = iy*kx_padded + ix;
  1553. block_q8_1 * y = (block_q8_1 *) vy;
  1554. const int ib = i_padded / QK8_1; // block index
  1555. const int iqs = i_padded % QK8_1; // quant index
  1556. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1557. float amax = fabsf(xi);
  1558. float sum = xi;
  1559. #pragma unroll
  1560. for (int mask = 16; mask > 0; mask >>= 1) {
  1561. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1562. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1563. }
  1564. const float d = amax / 127;
  1565. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1566. y[ib].qs[iqs] = q;
  1567. if (iqs > 0) {
  1568. return;
  1569. }
  1570. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1571. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1572. }
  1573. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1574. static __global__ void k_get_rows(
  1575. const void * src0, const int32_t * src1, dst_t * dst,
  1576. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1577. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1578. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1579. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1580. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1581. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1582. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1583. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1584. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1585. if (i00 >= ne00) {
  1586. return;
  1587. }
  1588. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1589. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1590. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1591. const int ib = i00/qk; // block index
  1592. const int iqs = (i00%qk)/qr; // quant index
  1593. const int iybs = i00 - i00%qk; // dst block start index
  1594. const int y_offset = qr == 1 ? 1 : qk/2;
  1595. // dequantize
  1596. dfloat2 v;
  1597. dequantize_kernel(src0_row, ib, iqs, v);
  1598. dst_row[iybs + iqs + 0] = v.x;
  1599. dst_row[iybs + iqs + y_offset] = v.y;
  1600. }
  1601. template<typename src0_t, typename dst_t>
  1602. static __global__ void k_get_rows_float(
  1603. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1604. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1605. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1606. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1607. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1608. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1609. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1610. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1611. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1612. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1613. if (i00 >= ne00) {
  1614. return;
  1615. }
  1616. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1617. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1618. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1619. dst_row[i00] = src0_row[i00];
  1620. }
  1621. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1622. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1623. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1624. if (i >= k) {
  1625. return;
  1626. }
  1627. const int ib = i/qk; // block index
  1628. const int iqs = (i%qk)/qr; // quant index
  1629. const int iybs = i - i%qk; // y block start index
  1630. const int y_offset = qr == 1 ? 1 : qk/2;
  1631. // dequantize
  1632. dfloat2 v;
  1633. dequantize_kernel(vx, ib, iqs, v);
  1634. y[iybs + iqs + 0] = v.x;
  1635. y[iybs + iqs + y_offset] = v.y;
  1636. }
  1637. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1638. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1639. #define VDR_Q4_0_Q8_1_MMVQ 2
  1640. #define VDR_Q4_0_Q8_1_MMQ 4
  1641. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1642. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1643. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1644. int sumi = 0;
  1645. #pragma unroll
  1646. for (int i = 0; i < vdr; ++i) {
  1647. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1648. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1649. // SIMD dot product of quantized values
  1650. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1651. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1652. }
  1653. const float2 ds8f = __half22float2(ds8);
  1654. // second part effectively subtracts 8 from each quant value
  1655. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1656. #else
  1657. bad_arch();
  1658. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1659. }
  1660. #define VDR_Q4_1_Q8_1_MMVQ 2
  1661. #define VDR_Q4_1_Q8_1_MMQ 4
  1662. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1663. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1664. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1665. int sumi = 0;
  1666. #pragma unroll
  1667. for (int i = 0; i < vdr; ++i) {
  1668. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1669. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1670. // SIMD dot product of quantized values
  1671. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1672. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1673. }
  1674. #ifdef GGML_CUDA_F16
  1675. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1676. const float d4d8 = tmp.x;
  1677. const float m4s8 = tmp.y;
  1678. #else
  1679. const float2 dm4f = __half22float2(dm4);
  1680. const float2 ds8f = __half22float2(ds8);
  1681. const float d4d8 = dm4f.x * ds8f.x;
  1682. const float m4s8 = dm4f.y * ds8f.y;
  1683. #endif // GGML_CUDA_F16
  1684. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1685. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1686. #else
  1687. bad_arch();
  1688. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1689. }
  1690. #define VDR_Q5_0_Q8_1_MMVQ 2
  1691. #define VDR_Q5_0_Q8_1_MMQ 4
  1692. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1693. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1694. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1695. int sumi = 0;
  1696. #pragma unroll
  1697. for (int i = 0; i < vdr; ++i) {
  1698. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1699. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1700. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1701. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1702. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1703. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1704. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1705. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1706. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1707. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1708. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1709. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1710. }
  1711. const float2 ds8f = __half22float2(ds8);
  1712. // second part effectively subtracts 16 from each quant value
  1713. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1714. #else
  1715. bad_arch();
  1716. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1717. }
  1718. #define VDR_Q5_1_Q8_1_MMVQ 2
  1719. #define VDR_Q5_1_Q8_1_MMQ 4
  1720. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1721. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1722. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1723. int sumi = 0;
  1724. #pragma unroll
  1725. for (int i = 0; i < vdr; ++i) {
  1726. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1727. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1728. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1729. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1730. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1731. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1732. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1733. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1734. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1735. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1736. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1737. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1738. }
  1739. #ifdef GGML_CUDA_F16
  1740. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1741. const float d5d8 = tmp.x;
  1742. const float m5s8 = tmp.y;
  1743. #else
  1744. const float2 dm5f = __half22float2(dm5);
  1745. const float2 ds8f = __half22float2(ds8);
  1746. const float d5d8 = dm5f.x * ds8f.x;
  1747. const float m5s8 = dm5f.y * ds8f.y;
  1748. #endif // GGML_CUDA_F16
  1749. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1750. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1751. #else
  1752. bad_arch();
  1753. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1754. }
  1755. #define VDR_Q8_0_Q8_1_MMVQ 2
  1756. #define VDR_Q8_0_Q8_1_MMQ 8
  1757. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1758. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1759. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1760. int sumi = 0;
  1761. #pragma unroll
  1762. for (int i = 0; i < vdr; ++i) {
  1763. // SIMD dot product of quantized values
  1764. sumi = __dp4a(v[i], u[i], sumi);
  1765. }
  1766. return d8_0*d8_1 * sumi;
  1767. #else
  1768. bad_arch();
  1769. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1770. }
  1771. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1772. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1773. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1774. int sumi = 0;
  1775. #pragma unroll
  1776. for (int i = 0; i < vdr; ++i) {
  1777. // SIMD dot product of quantized values
  1778. sumi = __dp4a(v[i], u[i], sumi);
  1779. }
  1780. #ifdef GGML_CUDA_F16
  1781. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1782. const float d8d8 = tmp.x;
  1783. const float m8s8 = tmp.y;
  1784. #else
  1785. const float2 dm8f = __half22float2(dm8);
  1786. const float2 ds8f = __half22float2(ds8);
  1787. const float d8d8 = dm8f.x * ds8f.x;
  1788. const float m8s8 = dm8f.y * ds8f.y;
  1789. #endif // GGML_CUDA_F16
  1790. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1791. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1792. #else
  1793. bad_arch();
  1794. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1795. }
  1796. #define VDR_Q2_K_Q8_1_MMVQ 1
  1797. #define VDR_Q2_K_Q8_1_MMQ 2
  1798. // contiguous v/x values
  1799. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1800. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1801. const half2 & dm2, const float * __restrict__ d8) {
  1802. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1803. float sumf_d = 0.0f;
  1804. float sumf_m = 0.0f;
  1805. #pragma unroll
  1806. for (int i = 0; i < QR2_K; ++i) {
  1807. const int sc = scales[2*i];
  1808. const int vi = (v >> (2*i)) & 0x03030303;
  1809. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1810. // fill int with 4x m
  1811. int m = sc >> 4;
  1812. m |= m << 8;
  1813. m |= m << 16;
  1814. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1815. }
  1816. const float2 dm2f = __half22float2(dm2);
  1817. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1818. #else
  1819. bad_arch();
  1820. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1821. }
  1822. // contiguous u/y values
  1823. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1824. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1825. const half2 & dm2, const float & d8) {
  1826. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1827. int sumi_d = 0;
  1828. int sumi_m = 0;
  1829. #pragma unroll
  1830. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1831. int sumi_d_sc = 0;
  1832. const int sc = scales[i0 / (QI8_1/2)];
  1833. // fill int with 4x m
  1834. int m = sc >> 4;
  1835. m |= m << 8;
  1836. m |= m << 16;
  1837. #pragma unroll
  1838. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1839. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1840. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1841. }
  1842. sumi_d += sumi_d_sc * (sc & 0xF);
  1843. }
  1844. const float2 dm2f = __half22float2(dm2);
  1845. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1846. #else
  1847. bad_arch();
  1848. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1849. }
  1850. #define VDR_Q3_K_Q8_1_MMVQ 1
  1851. #define VDR_Q3_K_Q8_1_MMQ 2
  1852. // contiguous v/x values
  1853. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1854. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1855. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1856. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1857. float sumf = 0.0f;
  1858. #pragma unroll
  1859. for (int i = 0; i < QR3_K; ++i) {
  1860. const int isc = scale_offset + 2*i;
  1861. const int isc_low = isc % (QK_K/32);
  1862. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1863. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1864. const int isc_high = isc % (QK_K/64);
  1865. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1866. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1867. const int sc = (sc_low | sc_high) - 32;
  1868. const int vil = (vl >> (2*i)) & 0x03030303;
  1869. const int vih = ((vh >> i) << 2) & 0x04040404;
  1870. const int vi = __vsubss4(vil, vih);
  1871. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1872. }
  1873. return d3 * sumf;
  1874. #else
  1875. bad_arch();
  1876. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1877. }
  1878. // contiguous u/y values
  1879. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1880. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1881. const float & d3, const float & d8) {
  1882. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1883. int sumi = 0;
  1884. #pragma unroll
  1885. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1886. int sumi_sc = 0;
  1887. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1888. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1889. }
  1890. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1891. }
  1892. return d3*d8 * sumi;
  1893. #else
  1894. bad_arch();
  1895. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1896. }
  1897. #define VDR_Q4_K_Q8_1_MMVQ 2
  1898. #define VDR_Q4_K_Q8_1_MMQ 8
  1899. // contiguous v/x values
  1900. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1901. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1902. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1903. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1904. float sumf_d = 0.0f;
  1905. float sumf_m = 0.0f;
  1906. #pragma unroll
  1907. for (int i = 0; i < QR4_K; ++i) {
  1908. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1909. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1910. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1911. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1912. sumf_d += d8[i] * (dot1 * sc[i]);
  1913. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1914. }
  1915. const float2 dm4f = __half22float2(dm4);
  1916. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1917. #else
  1918. bad_arch();
  1919. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1920. }
  1921. // contiguous u/y values
  1922. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1923. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1924. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1925. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1926. float sumf_d = 0.0f;
  1927. float sumf_m = 0.0f;
  1928. #pragma unroll
  1929. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1930. int sumi_d = 0;
  1931. #pragma unroll
  1932. for (int j = 0; j < QI8_1; ++j) {
  1933. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1934. }
  1935. const float2 ds8f = __half22float2(ds8[i]);
  1936. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1937. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1938. }
  1939. const float2 dm4f = __half22float2(dm4);
  1940. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1941. #else
  1942. bad_arch();
  1943. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1944. }
  1945. #define VDR_Q5_K_Q8_1_MMVQ 2
  1946. #define VDR_Q5_K_Q8_1_MMQ 8
  1947. // contiguous v/x values
  1948. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1949. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1950. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1951. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1952. float sumf_d = 0.0f;
  1953. float sumf_m = 0.0f;
  1954. #pragma unroll
  1955. for (int i = 0; i < QR5_K; ++i) {
  1956. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1957. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1958. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1959. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1960. const int v0i = vl0i | vh0i;
  1961. const int v1i = vl1i | vh1i;
  1962. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1963. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1964. sumf_d += d8[i] * (dot1 * sc[i]);
  1965. sumf_m += d8[i] * (dot2 * m[i]);
  1966. }
  1967. const float2 dm5f = __half22float2(dm5);
  1968. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1969. #else
  1970. bad_arch();
  1971. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1972. }
  1973. // contiguous u/y values
  1974. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1975. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1976. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1977. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1978. float sumf_d = 0.0f;
  1979. float sumf_m = 0.0f;
  1980. #pragma unroll
  1981. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1982. int sumi_d = 0;
  1983. #pragma unroll
  1984. for (int j = 0; j < QI8_1; ++j) {
  1985. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1986. }
  1987. const float2 ds8f = __half22float2(ds8[i]);
  1988. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1989. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1990. }
  1991. const float2 dm4f = __half22float2(dm4);
  1992. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1993. #else
  1994. bad_arch();
  1995. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1996. }
  1997. #define VDR_Q6_K_Q8_1_MMVQ 1
  1998. #define VDR_Q6_K_Q8_1_MMQ 8
  1999. // contiguous v/x values
  2000. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2001. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2002. const float & d, const float * __restrict__ d8) {
  2003. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2004. float sumf = 0.0f;
  2005. #pragma unroll
  2006. for (int i = 0; i < QR6_K; ++i) {
  2007. const int sc = scales[4*i];
  2008. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2009. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2010. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2011. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2012. }
  2013. return d*sumf;
  2014. #else
  2015. bad_arch();
  2016. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2017. }
  2018. // contiguous u/y values
  2019. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2020. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2021. const float & d6, const float * __restrict__ d8) {
  2022. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2023. float sumf_d = 0.0f;
  2024. #pragma unroll
  2025. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2026. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2027. #pragma unroll
  2028. for (int i = i0; i < i0 + 2; ++i) {
  2029. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2030. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2031. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2032. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2033. }
  2034. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2035. }
  2036. return d6 * sumf_d;
  2037. #else
  2038. bad_arch();
  2039. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2040. }
  2041. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2042. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2043. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2044. int v[VDR_Q4_0_Q8_1_MMVQ];
  2045. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2046. #pragma unroll
  2047. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2048. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2049. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2050. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2051. }
  2052. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2053. }
  2054. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2055. (void)x_qh; (void)x_sc;
  2056. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2057. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2058. *x_ql = tile_x_qs;
  2059. *x_dm = (half2 *) tile_x_d;
  2060. }
  2061. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2062. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2063. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2064. (void)x_qh; (void)x_sc;
  2065. GGML_CUDA_ASSUME(i_offset >= 0);
  2066. GGML_CUDA_ASSUME(i_offset < nwarps);
  2067. GGML_CUDA_ASSUME(k >= 0);
  2068. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2069. const int kbx = k / QI4_0;
  2070. const int kqsx = k % QI4_0;
  2071. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2072. float * x_dmf = (float *) x_dm;
  2073. #pragma unroll
  2074. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2075. int i = i0 + i_offset;
  2076. if (need_check) {
  2077. i = min(i, i_max);
  2078. }
  2079. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2080. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2081. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2082. }
  2083. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2084. const int kbxd = k % blocks_per_tile_x_row;
  2085. #pragma unroll
  2086. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2087. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2088. if (need_check) {
  2089. i = min(i, i_max);
  2090. }
  2091. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2092. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2093. }
  2094. }
  2095. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2096. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2097. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2098. (void)x_qh; (void)x_sc;
  2099. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2100. const float * x_dmf = (const float *) x_dm;
  2101. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2102. #pragma unroll
  2103. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2104. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2105. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2106. }
  2107. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2108. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2109. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2110. }
  2111. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2112. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2113. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2114. int v[VDR_Q4_1_Q8_1_MMVQ];
  2115. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2116. #pragma unroll
  2117. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2118. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2119. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2120. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2121. }
  2122. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2123. }
  2124. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2125. (void)x_qh; (void)x_sc;
  2126. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2127. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2128. *x_ql = tile_x_qs;
  2129. *x_dm = tile_x_dm;
  2130. }
  2131. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2132. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2133. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2134. (void)x_qh; (void)x_sc;
  2135. GGML_CUDA_ASSUME(i_offset >= 0);
  2136. GGML_CUDA_ASSUME(i_offset < nwarps);
  2137. GGML_CUDA_ASSUME(k >= 0);
  2138. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2139. const int kbx = k / QI4_1;
  2140. const int kqsx = k % QI4_1;
  2141. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2142. #pragma unroll
  2143. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2144. int i = i0 + i_offset;
  2145. if (need_check) {
  2146. i = min(i, i_max);
  2147. }
  2148. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2149. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2150. }
  2151. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2152. const int kbxd = k % blocks_per_tile_x_row;
  2153. #pragma unroll
  2154. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2155. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2156. if (need_check) {
  2157. i = min(i, i_max);
  2158. }
  2159. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2160. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2161. }
  2162. }
  2163. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2164. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2165. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2166. (void)x_qh; (void)x_sc;
  2167. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2168. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2169. #pragma unroll
  2170. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2171. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2172. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2173. }
  2174. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2175. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2176. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2177. }
  2178. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2179. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2180. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2181. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2182. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2183. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2184. #pragma unroll
  2185. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2186. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2187. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2188. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2189. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2190. }
  2191. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2192. }
  2193. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2194. (void)x_qh; (void)x_sc;
  2195. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2196. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2197. *x_ql = tile_x_ql;
  2198. *x_dm = (half2 *) tile_x_d;
  2199. }
  2200. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2201. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2202. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2203. (void)x_qh; (void)x_sc;
  2204. GGML_CUDA_ASSUME(i_offset >= 0);
  2205. GGML_CUDA_ASSUME(i_offset < nwarps);
  2206. GGML_CUDA_ASSUME(k >= 0);
  2207. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2208. const int kbx = k / QI5_0;
  2209. const int kqsx = k % QI5_0;
  2210. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2211. #pragma unroll
  2212. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2213. int i = i0 + i_offset;
  2214. if (need_check) {
  2215. i = min(i, i_max);
  2216. }
  2217. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2218. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2219. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2220. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2221. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2222. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2223. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2224. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2225. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2226. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2227. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2228. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2229. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2230. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2231. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2232. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2233. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2234. }
  2235. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2236. const int kbxd = k % blocks_per_tile_x_row;
  2237. float * x_dmf = (float *) x_dm;
  2238. #pragma unroll
  2239. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2240. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2241. if (need_check) {
  2242. i = min(i, i_max);
  2243. }
  2244. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2245. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2246. }
  2247. }
  2248. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2249. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2250. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2251. (void)x_qh; (void)x_sc;
  2252. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2253. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2254. const float * x_dmf = (const float *) x_dm;
  2255. const float * y_df = (const float *) y_ds;
  2256. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2257. #pragma unroll
  2258. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2259. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2260. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2261. }
  2262. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2263. (&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)]);
  2264. }
  2265. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2266. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2267. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2268. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2269. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2270. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2271. #pragma unroll
  2272. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2273. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2274. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2275. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2276. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2277. }
  2278. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2279. }
  2280. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2281. (void)x_qh; (void)x_sc;
  2282. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2283. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2284. *x_ql = tile_x_ql;
  2285. *x_dm = tile_x_dm;
  2286. }
  2287. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2288. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2289. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2290. (void)x_qh; (void)x_sc;
  2291. GGML_CUDA_ASSUME(i_offset >= 0);
  2292. GGML_CUDA_ASSUME(i_offset < nwarps);
  2293. GGML_CUDA_ASSUME(k >= 0);
  2294. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2295. const int kbx = k / QI5_1;
  2296. const int kqsx = k % QI5_1;
  2297. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2298. #pragma unroll
  2299. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2300. int i = i0 + i_offset;
  2301. if (need_check) {
  2302. i = min(i, i_max);
  2303. }
  2304. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2305. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2306. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2307. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2308. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2309. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2310. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2311. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2312. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2313. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2314. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2315. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2316. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2317. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2318. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2319. }
  2320. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2321. const int kbxd = k % blocks_per_tile_x_row;
  2322. #pragma unroll
  2323. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2324. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2325. if (need_check) {
  2326. i = min(i, i_max);
  2327. }
  2328. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2329. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2330. }
  2331. }
  2332. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2333. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2334. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2335. (void)x_qh; (void)x_sc;
  2336. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2337. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2338. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2339. #pragma unroll
  2340. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2341. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2342. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2343. }
  2344. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2345. (&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)]);
  2346. }
  2347. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2348. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2349. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2350. int v[VDR_Q8_0_Q8_1_MMVQ];
  2351. int u[VDR_Q8_0_Q8_1_MMVQ];
  2352. #pragma unroll
  2353. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2354. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2355. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2356. }
  2357. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2358. }
  2359. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2360. (void)x_qh; (void)x_sc;
  2361. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2362. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2363. *x_ql = tile_x_qs;
  2364. *x_dm = (half2 *) tile_x_d;
  2365. }
  2366. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2367. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2368. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2369. (void)x_qh; (void)x_sc;
  2370. GGML_CUDA_ASSUME(i_offset >= 0);
  2371. GGML_CUDA_ASSUME(i_offset < nwarps);
  2372. GGML_CUDA_ASSUME(k >= 0);
  2373. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2374. const int kbx = k / QI8_0;
  2375. const int kqsx = k % QI8_0;
  2376. float * x_dmf = (float *) x_dm;
  2377. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2378. #pragma unroll
  2379. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2380. int i = i0 + i_offset;
  2381. if (need_check) {
  2382. i = min(i, i_max);
  2383. }
  2384. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2385. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2386. }
  2387. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2388. const int kbxd = k % blocks_per_tile_x_row;
  2389. #pragma unroll
  2390. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2391. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2392. if (need_check) {
  2393. i = min(i, i_max);
  2394. }
  2395. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2396. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2397. }
  2398. }
  2399. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2400. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2401. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2402. (void)x_qh; (void)x_sc;
  2403. const float * x_dmf = (const float *) x_dm;
  2404. const float * y_df = (const float *) y_ds;
  2405. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2406. (&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],
  2407. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2408. }
  2409. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2410. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2411. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2412. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2413. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2414. const uint8_t * scales = bq2_K->scales + scale_offset;
  2415. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2416. int u[QR2_K];
  2417. float d8[QR2_K];
  2418. #pragma unroll
  2419. for (int i = 0; i < QR2_K; ++ i) {
  2420. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2421. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2422. }
  2423. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2424. }
  2425. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2426. (void)x_qh;
  2427. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2428. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2429. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2430. *x_ql = tile_x_ql;
  2431. *x_dm = tile_x_dm;
  2432. *x_sc = tile_x_sc;
  2433. }
  2434. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2435. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2436. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2437. (void)x_qh;
  2438. GGML_CUDA_ASSUME(i_offset >= 0);
  2439. GGML_CUDA_ASSUME(i_offset < nwarps);
  2440. GGML_CUDA_ASSUME(k >= 0);
  2441. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2442. const int kbx = k / QI2_K;
  2443. const int kqsx = k % QI2_K;
  2444. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2445. #pragma unroll
  2446. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2447. int i = i0 + i_offset;
  2448. if (need_check) {
  2449. i = min(i, i_max);
  2450. }
  2451. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2452. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2453. }
  2454. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2455. const int kbxd = k % blocks_per_tile_x_row;
  2456. #pragma unroll
  2457. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2458. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2459. if (need_check) {
  2460. i = min(i, i_max);
  2461. }
  2462. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2463. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2464. }
  2465. #pragma unroll
  2466. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2467. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2468. if (need_check) {
  2469. i = min(i, i_max);
  2470. }
  2471. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2472. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2473. }
  2474. }
  2475. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2476. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2477. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2478. (void)x_qh;
  2479. const int kbx = k / QI2_K;
  2480. const int ky = (k % QI2_K) * QR2_K;
  2481. const float * y_df = (const float *) y_ds;
  2482. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2483. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2484. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2485. #pragma unroll
  2486. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2487. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2488. }
  2489. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2490. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2491. 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]);
  2492. }
  2493. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2494. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2495. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2496. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2497. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2498. const float d = bq3_K->d;
  2499. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2500. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2501. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2502. int u[QR3_K];
  2503. float d8[QR3_K];
  2504. #pragma unroll
  2505. for (int i = 0; i < QR3_K; ++i) {
  2506. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2507. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2508. }
  2509. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2510. }
  2511. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2512. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2513. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2514. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2515. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2516. *x_ql = tile_x_ql;
  2517. *x_dm = tile_x_dm;
  2518. *x_qh = tile_x_qh;
  2519. *x_sc = tile_x_sc;
  2520. }
  2521. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2522. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2523. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2524. GGML_CUDA_ASSUME(i_offset >= 0);
  2525. GGML_CUDA_ASSUME(i_offset < nwarps);
  2526. GGML_CUDA_ASSUME(k >= 0);
  2527. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2528. const int kbx = k / QI3_K;
  2529. const int kqsx = k % QI3_K;
  2530. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2531. #pragma unroll
  2532. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2533. int i = i0 + i_offset;
  2534. if (need_check) {
  2535. i = min(i, i_max);
  2536. }
  2537. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2538. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2539. }
  2540. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2541. const int kbxd = k % blocks_per_tile_x_row;
  2542. float * x_dmf = (float *) x_dm;
  2543. #pragma unroll
  2544. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2545. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2546. if (need_check) {
  2547. i = min(i, i_max);
  2548. }
  2549. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2550. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2551. }
  2552. #pragma unroll
  2553. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2554. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2555. if (need_check) {
  2556. i = min(i, i_max);
  2557. }
  2558. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2559. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2560. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2561. }
  2562. #pragma unroll
  2563. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2564. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2565. if (need_check) {
  2566. i = min(i, i_max);
  2567. }
  2568. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2569. const int ksc = k % (QI3_K/4);
  2570. const int ksc_low = ksc % (QI3_K/8);
  2571. const int shift_low = 4 * (ksc / (QI3_K/8));
  2572. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2573. const int ksc_high = QI3_K/8;
  2574. const int shift_high = 2 * ksc;
  2575. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2576. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2577. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2578. }
  2579. }
  2580. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2581. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2582. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2583. const int kbx = k / QI3_K;
  2584. const int ky = (k % QI3_K) * QR3_K;
  2585. const float * x_dmf = (const float *) x_dm;
  2586. const float * y_df = (const float *) y_ds;
  2587. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2588. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2589. #pragma unroll
  2590. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2591. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2592. const int shift = 2 * ((ky % 32) / 8);
  2593. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2594. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2595. const int vlh = (vh << 2) & 0x04040404;
  2596. v[l] = __vsubss4(vll, vlh);
  2597. }
  2598. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2599. 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]);
  2600. }
  2601. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2602. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2603. #ifndef GGML_QKK_64
  2604. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2605. int v[2];
  2606. int u[2*QR4_K];
  2607. float d8[QR4_K];
  2608. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2609. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2610. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2611. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2612. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2613. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2614. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2615. v[0] = q4[0];
  2616. v[1] = q4[4];
  2617. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2618. uint16_t aux[2];
  2619. const int j = bq8_offset/2;
  2620. if (j < 2) {
  2621. aux[0] = scales[j+0] & 0x3f3f;
  2622. aux[1] = scales[j+2] & 0x3f3f;
  2623. } else {
  2624. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2625. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2626. }
  2627. const uint8_t * sc = (const uint8_t *)aux;
  2628. const uint8_t * m = sc + 2;
  2629. for (int i = 0; i < QR4_K; ++i) {
  2630. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2631. d8[i] = __low2half(bq8i->ds);
  2632. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2633. u[2*i+0] = q8[0];
  2634. u[2*i+1] = q8[4];
  2635. }
  2636. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2637. #else
  2638. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2639. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2640. float sumf_d = 0.0f;
  2641. float sumf_m = 0.0f;
  2642. uint16_t aux16[2];
  2643. const uint8_t * s = (const uint8_t *)aux16;
  2644. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2645. aux16[0] = a[0] & 0x0f0f;
  2646. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2647. const float dall = bq4_K->dm[0];
  2648. const float dmin = bq4_K->dm[1];
  2649. const float d8_1 = __low2float(bq8_1[0].ds);
  2650. const float d8_2 = __low2float(bq8_1[1].ds);
  2651. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2652. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2653. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2654. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2655. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2656. const int v1 = q4[0];
  2657. const int v2 = q4[4];
  2658. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2659. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2660. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2661. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2662. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2663. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2664. return dall * sumf_d - dmin * sumf_m;
  2665. #else
  2666. bad_arch();
  2667. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2668. #endif
  2669. }
  2670. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2671. (void)x_qh;
  2672. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2673. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2674. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2675. *x_ql = tile_x_ql;
  2676. *x_dm = tile_x_dm;
  2677. *x_sc = tile_x_sc;
  2678. }
  2679. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2680. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2681. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2682. (void)x_qh;
  2683. GGML_CUDA_ASSUME(i_offset >= 0);
  2684. GGML_CUDA_ASSUME(i_offset < nwarps);
  2685. GGML_CUDA_ASSUME(k >= 0);
  2686. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2687. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2688. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2689. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2690. #pragma unroll
  2691. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2692. int i = i0 + i_offset;
  2693. if (need_check) {
  2694. i = min(i, i_max);
  2695. }
  2696. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2697. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2698. }
  2699. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2700. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2701. #pragma unroll
  2702. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2703. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2704. if (need_check) {
  2705. i = min(i, i_max);
  2706. }
  2707. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2708. #if QK_K == 256
  2709. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2710. #else
  2711. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2712. #endif
  2713. }
  2714. #pragma unroll
  2715. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2716. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2717. if (need_check) {
  2718. i = min(i, i_max);
  2719. }
  2720. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2721. const int * scales = (const int *) bxi->scales;
  2722. const int ksc = k % (WARP_SIZE/8);
  2723. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2724. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2725. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2726. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2727. }
  2728. }
  2729. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2730. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2731. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2732. (void)x_qh;
  2733. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2734. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2735. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2736. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2737. }
  2738. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2739. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2740. #ifndef GGML_QKK_64
  2741. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2742. int vl[2];
  2743. int vh[2];
  2744. int u[2*QR5_K];
  2745. float d8[QR5_K];
  2746. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2747. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2748. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2749. vl[0] = ql[0];
  2750. vl[1] = ql[4];
  2751. vh[0] = qh[0] >> bq8_offset;
  2752. vh[1] = qh[4] >> bq8_offset;
  2753. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2754. uint16_t aux[2];
  2755. const int j = bq8_offset/2;
  2756. if (j < 2) {
  2757. aux[0] = scales[j+0] & 0x3f3f;
  2758. aux[1] = scales[j+2] & 0x3f3f;
  2759. } else {
  2760. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2761. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2762. }
  2763. const uint8_t * sc = (const uint8_t *)aux;
  2764. const uint8_t * m = sc + 2;
  2765. #pragma unroll
  2766. for (int i = 0; i < QR5_K; ++i) {
  2767. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2768. d8[i] = __low2float(bq8i->ds);
  2769. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2770. u[2*i+0] = q8[0];
  2771. u[2*i+1] = q8[4];
  2772. }
  2773. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2774. #else
  2775. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2776. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2777. const int8_t * s = bq5_K->scales;
  2778. const float d = bq5_K->d;
  2779. const float d8_1 = __low2half(bq8_1[0].ds);
  2780. const float d8_2 = __low2half(bq8_1[1].ds);
  2781. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2782. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2783. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2784. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2785. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2786. const int vl1 = ql[0];
  2787. const int vl2 = ql[4];
  2788. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2789. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2790. const int in = step%8; // 0, 4, 0, 4
  2791. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2792. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2793. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2794. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2795. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2796. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2797. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2798. return d * sumf_d;
  2799. #else
  2800. bad_arch();
  2801. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2802. #endif
  2803. }
  2804. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2805. (void)x_qh;
  2806. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2807. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2808. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2809. *x_ql = tile_x_ql;
  2810. *x_dm = tile_x_dm;
  2811. *x_sc = tile_x_sc;
  2812. }
  2813. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2814. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2815. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2816. (void)x_qh;
  2817. GGML_CUDA_ASSUME(i_offset >= 0);
  2818. GGML_CUDA_ASSUME(i_offset < nwarps);
  2819. GGML_CUDA_ASSUME(k >= 0);
  2820. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2821. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2822. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2823. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2824. #pragma unroll
  2825. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2826. int i = i0 + i_offset;
  2827. if (need_check) {
  2828. i = min(i, i_max);
  2829. }
  2830. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2831. const int ky = QR5_K*kqsx;
  2832. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2833. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2834. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2835. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2836. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2837. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2838. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2839. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2840. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2841. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2842. }
  2843. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2844. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2845. #pragma unroll
  2846. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2847. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2848. if (need_check) {
  2849. i = min(i, i_max);
  2850. }
  2851. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2852. #if QK_K == 256
  2853. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2854. #endif
  2855. }
  2856. #pragma unroll
  2857. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2858. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2859. if (need_check) {
  2860. i = min(i, i_max);
  2861. }
  2862. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2863. const int * scales = (const int *) bxi->scales;
  2864. const int ksc = k % (WARP_SIZE/8);
  2865. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2866. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2867. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2868. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2869. }
  2870. }
  2871. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2872. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2873. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2874. (void)x_qh;
  2875. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2876. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2877. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2878. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2879. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2880. }
  2881. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2882. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2883. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2884. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2885. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2886. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2887. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2888. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2889. const int8_t * scales = bq6_K->scales + scale_offset;
  2890. int u[QR6_K];
  2891. float d8[QR6_K];
  2892. #pragma unroll
  2893. for (int i = 0; i < QR6_K; ++i) {
  2894. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2895. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2896. }
  2897. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2898. }
  2899. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2900. (void)x_qh;
  2901. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2902. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2903. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2904. *x_ql = tile_x_ql;
  2905. *x_dm = tile_x_dm;
  2906. *x_sc = tile_x_sc;
  2907. }
  2908. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2909. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2910. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2911. (void)x_qh;
  2912. GGML_CUDA_ASSUME(i_offset >= 0);
  2913. GGML_CUDA_ASSUME(i_offset < nwarps);
  2914. GGML_CUDA_ASSUME(k >= 0);
  2915. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2916. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2917. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2918. const block_q6_K * bx0 = (const block_q6_K *) vx;
  2919. #pragma unroll
  2920. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2921. int i = i0 + i_offset;
  2922. if (need_check) {
  2923. i = min(i, i_max);
  2924. }
  2925. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2926. const int ky = QR6_K*kqsx;
  2927. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2928. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2929. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2930. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2931. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2932. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2933. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2934. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2935. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2936. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2937. }
  2938. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2939. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2940. float * x_dmf = (float *) x_dm;
  2941. #pragma unroll
  2942. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2943. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2944. if (need_check) {
  2945. i = min(i, i_max);
  2946. }
  2947. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2948. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2949. }
  2950. #pragma unroll
  2951. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2952. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2953. if (need_check) {
  2954. i = min(i, i_max);
  2955. }
  2956. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2957. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2958. }
  2959. }
  2960. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2961. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2962. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2963. (void)x_qh;
  2964. const float * x_dmf = (const float *) x_dm;
  2965. const float * y_df = (const float *) y_ds;
  2966. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2967. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2968. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2969. 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]);
  2970. }
  2971. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2972. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2973. static __device__ __forceinline__ void mul_mat_q(
  2974. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2975. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2976. const block_q_t * x = (const block_q_t *) vx;
  2977. const block_q8_1 * y = (const block_q8_1 *) vy;
  2978. const int blocks_per_row_x = ncols_x / qk;
  2979. const int blocks_per_col_y = nrows_y / QK8_1;
  2980. const int blocks_per_warp = WARP_SIZE / qi;
  2981. const int & ncols_dst = ncols_y;
  2982. const int row_dst_0 = blockIdx.x*mmq_y;
  2983. const int & row_x_0 = row_dst_0;
  2984. const int col_dst_0 = blockIdx.y*mmq_x;
  2985. const int & col_y_0 = col_dst_0;
  2986. int * tile_x_ql = nullptr;
  2987. half2 * tile_x_dm = nullptr;
  2988. int * tile_x_qh = nullptr;
  2989. int * tile_x_sc = nullptr;
  2990. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2991. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2992. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2993. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  2994. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2995. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2996. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2997. #pragma unroll
  2998. for (int ir = 0; ir < qr; ++ir) {
  2999. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3000. const int kbxd = kqs / QI8_1;
  3001. #pragma unroll
  3002. for (int i = 0; i < mmq_x; i += nwarps) {
  3003. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3004. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3005. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3006. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3007. }
  3008. #pragma unroll
  3009. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3010. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3011. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3012. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3013. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3014. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3015. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3016. if (need_sum) {
  3017. *dsi_dst = *dsi_src;
  3018. } else {
  3019. float * dfi_dst = (float *) dsi_dst;
  3020. *dfi_dst = __low2half(*dsi_src);
  3021. }
  3022. }
  3023. __syncthreads();
  3024. // #pragma unroll // unrolling this loop causes too much register pressure
  3025. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3026. #pragma unroll
  3027. for (int j = 0; j < mmq_x; j += nwarps) {
  3028. #pragma unroll
  3029. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3030. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3031. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3032. threadIdx.x + i, threadIdx.y + j, k);
  3033. }
  3034. }
  3035. }
  3036. __syncthreads();
  3037. }
  3038. }
  3039. #pragma unroll
  3040. for (int j = 0; j < mmq_x; j += nwarps) {
  3041. const int col_dst = col_dst_0 + j + threadIdx.y;
  3042. if (col_dst >= ncols_dst) {
  3043. return;
  3044. }
  3045. #pragma unroll
  3046. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3047. const int row_dst = row_dst_0 + threadIdx.x + i;
  3048. if (row_dst >= nrows_dst) {
  3049. continue;
  3050. }
  3051. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3052. }
  3053. }
  3054. }
  3055. #define MMQ_X_Q4_0_RDNA2 64
  3056. #define MMQ_Y_Q4_0_RDNA2 128
  3057. #define NWARPS_Q4_0_RDNA2 8
  3058. #define MMQ_X_Q4_0_RDNA1 64
  3059. #define MMQ_Y_Q4_0_RDNA1 64
  3060. #define NWARPS_Q4_0_RDNA1 8
  3061. #if defined(CUDA_USE_TENSOR_CORES)
  3062. #define MMQ_X_Q4_0_AMPERE 4
  3063. #define MMQ_Y_Q4_0_AMPERE 32
  3064. #define NWARPS_Q4_0_AMPERE 4
  3065. #else
  3066. #define MMQ_X_Q4_0_AMPERE 64
  3067. #define MMQ_Y_Q4_0_AMPERE 128
  3068. #define NWARPS_Q4_0_AMPERE 4
  3069. #endif
  3070. #define MMQ_X_Q4_0_PASCAL 64
  3071. #define MMQ_Y_Q4_0_PASCAL 64
  3072. #define NWARPS_Q4_0_PASCAL 8
  3073. template <bool need_check> static __global__ void
  3074. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3075. #if defined(RDNA3) || defined(RDNA2)
  3076. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3077. #endif // defined(RDNA3) || defined(RDNA2)
  3078. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3079. mul_mat_q4_0(
  3080. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3081. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3082. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3083. #if defined(RDNA3) || defined(RDNA2)
  3084. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3085. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3086. const int nwarps = NWARPS_Q4_0_RDNA2;
  3087. #else
  3088. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3089. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3090. const int nwarps = NWARPS_Q4_0_RDNA1;
  3091. #endif // defined(RDNA3) || defined(RDNA2)
  3092. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3093. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3094. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3095. #elif __CUDA_ARCH__ >= CC_VOLTA
  3096. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3097. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3098. const int nwarps = NWARPS_Q4_0_AMPERE;
  3099. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3100. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3101. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3102. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3103. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3104. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3105. const int nwarps = NWARPS_Q4_0_PASCAL;
  3106. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3107. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3108. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3109. #else
  3110. (void) vec_dot_q4_0_q8_1_mul_mat;
  3111. bad_arch();
  3112. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3113. }
  3114. #define MMQ_X_Q4_1_RDNA2 64
  3115. #define MMQ_Y_Q4_1_RDNA2 128
  3116. #define NWARPS_Q4_1_RDNA2 8
  3117. #define MMQ_X_Q4_1_RDNA1 64
  3118. #define MMQ_Y_Q4_1_RDNA1 64
  3119. #define NWARPS_Q4_1_RDNA1 8
  3120. #if defined(CUDA_USE_TENSOR_CORES)
  3121. #define MMQ_X_Q4_1_AMPERE 4
  3122. #define MMQ_Y_Q4_1_AMPERE 32
  3123. #define NWARPS_Q4_1_AMPERE 4
  3124. #else
  3125. #define MMQ_X_Q4_1_AMPERE 64
  3126. #define MMQ_Y_Q4_1_AMPERE 128
  3127. #define NWARPS_Q4_1_AMPERE 4
  3128. #endif
  3129. #define MMQ_X_Q4_1_PASCAL 64
  3130. #define MMQ_Y_Q4_1_PASCAL 64
  3131. #define NWARPS_Q4_1_PASCAL 8
  3132. template <bool need_check> static __global__ void
  3133. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3134. #if defined(RDNA3) || defined(RDNA2)
  3135. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3136. #endif // defined(RDNA3) || defined(RDNA2)
  3137. #elif __CUDA_ARCH__ < CC_VOLTA
  3138. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3139. #endif // __CUDA_ARCH__ < CC_VOLTA
  3140. mul_mat_q4_1(
  3141. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3142. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3143. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3144. #if defined(RDNA3) || defined(RDNA2)
  3145. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3146. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3147. const int nwarps = NWARPS_Q4_1_RDNA2;
  3148. #else
  3149. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3150. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3151. const int nwarps = NWARPS_Q4_1_RDNA1;
  3152. #endif // defined(RDNA3) || defined(RDNA2)
  3153. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3154. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3155. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3156. #elif __CUDA_ARCH__ >= CC_VOLTA
  3157. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3158. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3159. const int nwarps = NWARPS_Q4_1_AMPERE;
  3160. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3161. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3162. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3163. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3164. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3165. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3166. const int nwarps = NWARPS_Q4_1_PASCAL;
  3167. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3168. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3169. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3170. #else
  3171. (void) vec_dot_q4_1_q8_1_mul_mat;
  3172. bad_arch();
  3173. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3174. }
  3175. #define MMQ_X_Q5_0_RDNA2 64
  3176. #define MMQ_Y_Q5_0_RDNA2 128
  3177. #define NWARPS_Q5_0_RDNA2 8
  3178. #define MMQ_X_Q5_0_RDNA1 64
  3179. #define MMQ_Y_Q5_0_RDNA1 64
  3180. #define NWARPS_Q5_0_RDNA1 8
  3181. #if defined(CUDA_USE_TENSOR_CORES)
  3182. #define MMQ_X_Q5_0_AMPERE 4
  3183. #define MMQ_Y_Q5_0_AMPERE 32
  3184. #define NWARPS_Q5_0_AMPERE 4
  3185. #else
  3186. #define MMQ_X_Q5_0_AMPERE 128
  3187. #define MMQ_Y_Q5_0_AMPERE 64
  3188. #define NWARPS_Q5_0_AMPERE 4
  3189. #endif
  3190. #define MMQ_X_Q5_0_PASCAL 64
  3191. #define MMQ_Y_Q5_0_PASCAL 64
  3192. #define NWARPS_Q5_0_PASCAL 8
  3193. template <bool need_check> static __global__ void
  3194. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3195. #if defined(RDNA3) || defined(RDNA2)
  3196. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3197. #endif // defined(RDNA3) || defined(RDNA2)
  3198. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3199. mul_mat_q5_0(
  3200. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3201. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3202. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3203. #if defined(RDNA3) || defined(RDNA2)
  3204. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3205. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3206. const int nwarps = NWARPS_Q5_0_RDNA2;
  3207. #else
  3208. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3209. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3210. const int nwarps = NWARPS_Q5_0_RDNA1;
  3211. #endif // defined(RDNA3) || defined(RDNA2)
  3212. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3213. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3214. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3215. #elif __CUDA_ARCH__ >= CC_VOLTA
  3216. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3217. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3218. const int nwarps = NWARPS_Q5_0_AMPERE;
  3219. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3220. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3221. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3222. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3223. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3224. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3225. const int nwarps = NWARPS_Q5_0_PASCAL;
  3226. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3227. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3228. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3229. #else
  3230. (void) vec_dot_q5_0_q8_1_mul_mat;
  3231. bad_arch();
  3232. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3233. }
  3234. #define MMQ_X_Q5_1_RDNA2 64
  3235. #define MMQ_Y_Q5_1_RDNA2 128
  3236. #define NWARPS_Q5_1_RDNA2 8
  3237. #define MMQ_X_Q5_1_RDNA1 64
  3238. #define MMQ_Y_Q5_1_RDNA1 64
  3239. #define NWARPS_Q5_1_RDNA1 8
  3240. #if defined(CUDA_USE_TENSOR_CORES)
  3241. #define MMQ_X_Q5_1_AMPERE 4
  3242. #define MMQ_Y_Q5_1_AMPERE 32
  3243. #define NWARPS_Q5_1_AMPERE 4
  3244. #else
  3245. #define MMQ_X_Q5_1_AMPERE 128
  3246. #define MMQ_Y_Q5_1_AMPERE 64
  3247. #define NWARPS_Q5_1_AMPERE 4
  3248. #endif
  3249. #define MMQ_X_Q5_1_PASCAL 64
  3250. #define MMQ_Y_Q5_1_PASCAL 64
  3251. #define NWARPS_Q5_1_PASCAL 8
  3252. template <bool need_check> static __global__ void
  3253. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3254. #if defined(RDNA3) || defined(RDNA2)
  3255. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3256. #endif // defined(RDNA3) || defined(RDNA2)
  3257. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3258. mul_mat_q5_1(
  3259. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3260. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3261. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3262. #if defined(RDNA3) || defined(RDNA2)
  3263. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3264. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3265. const int nwarps = NWARPS_Q5_1_RDNA2;
  3266. #else
  3267. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3268. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3269. const int nwarps = NWARPS_Q5_1_RDNA1;
  3270. #endif // defined(RDNA3) || defined(RDNA2)
  3271. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3272. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3273. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3274. #elif __CUDA_ARCH__ >= CC_VOLTA
  3275. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3276. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3277. const int nwarps = NWARPS_Q5_1_AMPERE;
  3278. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3279. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3280. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3281. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3282. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3283. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3284. const int nwarps = NWARPS_Q5_1_PASCAL;
  3285. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3286. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3287. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3288. #else
  3289. (void) vec_dot_q5_1_q8_1_mul_mat;
  3290. bad_arch();
  3291. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3292. }
  3293. #define MMQ_X_Q8_0_RDNA2 64
  3294. #define MMQ_Y_Q8_0_RDNA2 128
  3295. #define NWARPS_Q8_0_RDNA2 8
  3296. #define MMQ_X_Q8_0_RDNA1 64
  3297. #define MMQ_Y_Q8_0_RDNA1 64
  3298. #define NWARPS_Q8_0_RDNA1 8
  3299. #if defined(CUDA_USE_TENSOR_CORES)
  3300. #define MMQ_X_Q8_0_AMPERE 4
  3301. #define MMQ_Y_Q8_0_AMPERE 32
  3302. #define NWARPS_Q8_0_AMPERE 4
  3303. #else
  3304. #define MMQ_X_Q8_0_AMPERE 128
  3305. #define MMQ_Y_Q8_0_AMPERE 64
  3306. #define NWARPS_Q8_0_AMPERE 4
  3307. #endif
  3308. #define MMQ_X_Q8_0_PASCAL 64
  3309. #define MMQ_Y_Q8_0_PASCAL 64
  3310. #define NWARPS_Q8_0_PASCAL 8
  3311. template <bool need_check> static __global__ void
  3312. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3313. #if defined(RDNA3) || defined(RDNA2)
  3314. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3315. #endif // defined(RDNA3) || defined(RDNA2)
  3316. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3317. mul_mat_q8_0(
  3318. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3319. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3320. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3321. #if defined(RDNA3) || defined(RDNA2)
  3322. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3323. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3324. const int nwarps = NWARPS_Q8_0_RDNA2;
  3325. #else
  3326. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3327. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3328. const int nwarps = NWARPS_Q8_0_RDNA1;
  3329. #endif // defined(RDNA3) || defined(RDNA2)
  3330. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3331. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3332. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3333. #elif __CUDA_ARCH__ >= CC_VOLTA
  3334. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3335. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3336. const int nwarps = NWARPS_Q8_0_AMPERE;
  3337. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3338. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3339. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3340. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3341. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3342. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3343. const int nwarps = NWARPS_Q8_0_PASCAL;
  3344. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3345. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3346. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3347. #else
  3348. (void) vec_dot_q8_0_q8_1_mul_mat;
  3349. bad_arch();
  3350. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3351. }
  3352. #define MMQ_X_Q2_K_RDNA2 64
  3353. #define MMQ_Y_Q2_K_RDNA2 128
  3354. #define NWARPS_Q2_K_RDNA2 8
  3355. #define MMQ_X_Q2_K_RDNA1 128
  3356. #define MMQ_Y_Q2_K_RDNA1 32
  3357. #define NWARPS_Q2_K_RDNA1 8
  3358. #if defined(CUDA_USE_TENSOR_CORES)
  3359. #define MMQ_X_Q2_K_AMPERE 4
  3360. #define MMQ_Y_Q2_K_AMPERE 32
  3361. #define NWARPS_Q2_K_AMPERE 4
  3362. #else
  3363. #define MMQ_X_Q2_K_AMPERE 64
  3364. #define MMQ_Y_Q2_K_AMPERE 128
  3365. #define NWARPS_Q2_K_AMPERE 4
  3366. #endif
  3367. #define MMQ_X_Q2_K_PASCAL 64
  3368. #define MMQ_Y_Q2_K_PASCAL 64
  3369. #define NWARPS_Q2_K_PASCAL 8
  3370. template <bool need_check> static __global__ void
  3371. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3372. #if defined(RDNA3) || defined(RDNA2)
  3373. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3374. #endif // defined(RDNA3) || defined(RDNA2)
  3375. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3376. mul_mat_q2_K(
  3377. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3378. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3379. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3380. #if defined(RDNA3) || defined(RDNA2)
  3381. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3382. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3383. const int nwarps = NWARPS_Q2_K_RDNA2;
  3384. #else
  3385. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3386. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3387. const int nwarps = NWARPS_Q2_K_RDNA1;
  3388. #endif // defined(RDNA3) || defined(RDNA2)
  3389. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3390. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3391. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3392. #elif __CUDA_ARCH__ >= CC_VOLTA
  3393. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3394. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3395. const int nwarps = NWARPS_Q2_K_AMPERE;
  3396. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3397. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3398. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3399. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3400. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3401. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3402. const int nwarps = NWARPS_Q2_K_PASCAL;
  3403. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3404. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3405. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3406. #else
  3407. (void) vec_dot_q2_K_q8_1_mul_mat;
  3408. bad_arch();
  3409. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3410. }
  3411. #define MMQ_X_Q3_K_RDNA2 128
  3412. #define MMQ_Y_Q3_K_RDNA2 64
  3413. #define NWARPS_Q3_K_RDNA2 8
  3414. #define MMQ_X_Q3_K_RDNA1 32
  3415. #define MMQ_Y_Q3_K_RDNA1 128
  3416. #define NWARPS_Q3_K_RDNA1 8
  3417. #if defined(CUDA_USE_TENSOR_CORES)
  3418. #define MMQ_X_Q3_K_AMPERE 4
  3419. #define MMQ_Y_Q3_K_AMPERE 32
  3420. #define NWARPS_Q3_K_AMPERE 4
  3421. #else
  3422. #define MMQ_X_Q3_K_AMPERE 128
  3423. #define MMQ_Y_Q3_K_AMPERE 128
  3424. #define NWARPS_Q3_K_AMPERE 4
  3425. #endif
  3426. #define MMQ_X_Q3_K_PASCAL 64
  3427. #define MMQ_Y_Q3_K_PASCAL 64
  3428. #define NWARPS_Q3_K_PASCAL 8
  3429. template <bool need_check> static __global__ void
  3430. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3431. #if defined(RDNA3) || defined(RDNA2)
  3432. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3433. #endif // defined(RDNA3) || defined(RDNA2)
  3434. #elif __CUDA_ARCH__ < CC_VOLTA
  3435. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3436. #endif // __CUDA_ARCH__ < CC_VOLTA
  3437. mul_mat_q3_K(
  3438. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3439. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3440. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3441. #if defined(RDNA3) || defined(RDNA2)
  3442. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3443. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3444. const int nwarps = NWARPS_Q3_K_RDNA2;
  3445. #else
  3446. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3447. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3448. const int nwarps = NWARPS_Q3_K_RDNA1;
  3449. #endif // defined(RDNA3) || defined(RDNA2)
  3450. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3451. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3452. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3453. #elif __CUDA_ARCH__ >= CC_VOLTA
  3454. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3455. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3456. const int nwarps = NWARPS_Q3_K_AMPERE;
  3457. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3458. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3459. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3460. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3461. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3462. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3463. const int nwarps = NWARPS_Q3_K_PASCAL;
  3464. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3465. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3466. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3467. #else
  3468. (void) vec_dot_q3_K_q8_1_mul_mat;
  3469. bad_arch();
  3470. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3471. }
  3472. #define MMQ_X_Q4_K_RDNA2 64
  3473. #define MMQ_Y_Q4_K_RDNA2 128
  3474. #define NWARPS_Q4_K_RDNA2 8
  3475. #define MMQ_X_Q4_K_RDNA1 32
  3476. #define MMQ_Y_Q4_K_RDNA1 64
  3477. #define NWARPS_Q4_K_RDNA1 8
  3478. #if defined(CUDA_USE_TENSOR_CORES)
  3479. #define MMQ_X_Q4_K_AMPERE 4
  3480. #define MMQ_Y_Q4_K_AMPERE 32
  3481. #define NWARPS_Q4_K_AMPERE 4
  3482. #else
  3483. #define MMQ_X_Q4_K_AMPERE 64
  3484. #define MMQ_Y_Q4_K_AMPERE 128
  3485. #define NWARPS_Q4_K_AMPERE 4
  3486. #endif
  3487. #define MMQ_X_Q4_K_PASCAL 64
  3488. #define MMQ_Y_Q4_K_PASCAL 64
  3489. #define NWARPS_Q4_K_PASCAL 8
  3490. template <bool need_check> static __global__ void
  3491. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3492. #if defined(RDNA3) || defined(RDNA2)
  3493. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3494. #endif // defined(RDNA3) || defined(RDNA2)
  3495. #elif __CUDA_ARCH__ < CC_VOLTA
  3496. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3497. #endif // __CUDA_ARCH__ < CC_VOLTA
  3498. mul_mat_q4_K(
  3499. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3500. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3501. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3502. #if defined(RDNA3) || defined(RDNA2)
  3503. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3504. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3505. const int nwarps = NWARPS_Q4_K_RDNA2;
  3506. #else
  3507. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3508. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3509. const int nwarps = NWARPS_Q4_K_RDNA1;
  3510. #endif // defined(RDNA3) || defined(RDNA2)
  3511. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3512. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3513. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3514. #elif __CUDA_ARCH__ >= CC_VOLTA
  3515. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3516. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3517. const int nwarps = NWARPS_Q4_K_AMPERE;
  3518. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3519. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3520. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3521. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3522. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3523. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3524. const int nwarps = NWARPS_Q4_K_PASCAL;
  3525. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3526. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3527. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3528. #else
  3529. (void) vec_dot_q4_K_q8_1_mul_mat;
  3530. bad_arch();
  3531. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3532. }
  3533. #define MMQ_X_Q5_K_RDNA2 64
  3534. #define MMQ_Y_Q5_K_RDNA2 128
  3535. #define NWARPS_Q5_K_RDNA2 8
  3536. #define MMQ_X_Q5_K_RDNA1 32
  3537. #define MMQ_Y_Q5_K_RDNA1 64
  3538. #define NWARPS_Q5_K_RDNA1 8
  3539. #if defined(CUDA_USE_TENSOR_CORES)
  3540. #define MMQ_X_Q5_K_AMPERE 4
  3541. #define MMQ_Y_Q5_K_AMPERE 32
  3542. #define NWARPS_Q5_K_AMPERE 4
  3543. #else
  3544. #define MMQ_X_Q5_K_AMPERE 64
  3545. #define MMQ_Y_Q5_K_AMPERE 128
  3546. #define NWARPS_Q5_K_AMPERE 4
  3547. #endif
  3548. #define MMQ_X_Q5_K_PASCAL 64
  3549. #define MMQ_Y_Q5_K_PASCAL 64
  3550. #define NWARPS_Q5_K_PASCAL 8
  3551. template <bool need_check> static __global__ void
  3552. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3553. #if defined(RDNA3) || defined(RDNA2)
  3554. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3555. #endif // defined(RDNA3) || defined(RDNA2)
  3556. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3557. mul_mat_q5_K(
  3558. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3559. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3560. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3561. #if defined(RDNA3) || defined(RDNA2)
  3562. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3563. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3564. const int nwarps = NWARPS_Q5_K_RDNA2;
  3565. #else
  3566. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3567. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3568. const int nwarps = NWARPS_Q5_K_RDNA1;
  3569. #endif // defined(RDNA3) || defined(RDNA2)
  3570. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3571. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3572. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3573. #elif __CUDA_ARCH__ >= CC_VOLTA
  3574. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3575. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3576. const int nwarps = NWARPS_Q5_K_AMPERE;
  3577. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3578. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3579. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3580. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3581. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3582. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3583. const int nwarps = NWARPS_Q5_K_PASCAL;
  3584. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3585. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3586. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3587. #else
  3588. (void) vec_dot_q5_K_q8_1_mul_mat;
  3589. bad_arch();
  3590. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3591. }
  3592. #define MMQ_X_Q6_K_RDNA2 64
  3593. #define MMQ_Y_Q6_K_RDNA2 128
  3594. #define NWARPS_Q6_K_RDNA2 8
  3595. #define MMQ_X_Q6_K_RDNA1 32
  3596. #define MMQ_Y_Q6_K_RDNA1 64
  3597. #define NWARPS_Q6_K_RDNA1 8
  3598. #if defined(CUDA_USE_TENSOR_CORES)
  3599. #define MMQ_X_Q6_K_AMPERE 4
  3600. #define MMQ_Y_Q6_K_AMPERE 32
  3601. #define NWARPS_Q6_K_AMPERE 4
  3602. #else
  3603. #define MMQ_X_Q6_K_AMPERE 64
  3604. #define MMQ_Y_Q6_K_AMPERE 64
  3605. #define NWARPS_Q6_K_AMPERE 4
  3606. #endif
  3607. #define MMQ_X_Q6_K_PASCAL 64
  3608. #define MMQ_Y_Q6_K_PASCAL 64
  3609. #define NWARPS_Q6_K_PASCAL 8
  3610. template <bool need_check> static __global__ void
  3611. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3612. #if defined(RDNA3) || defined(RDNA2)
  3613. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3614. #endif // defined(RDNA3) || defined(RDNA2)
  3615. #elif __CUDA_ARCH__ < CC_VOLTA
  3616. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3617. #endif // __CUDA_ARCH__ < CC_VOLTA
  3618. mul_mat_q6_K(
  3619. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3620. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3621. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3622. #if defined(RDNA3) || defined(RDNA2)
  3623. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3624. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3625. const int nwarps = NWARPS_Q6_K_RDNA2;
  3626. #else
  3627. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3628. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3629. const int nwarps = NWARPS_Q6_K_RDNA1;
  3630. #endif // defined(RDNA3) || defined(RDNA2)
  3631. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3632. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3633. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3634. #elif __CUDA_ARCH__ >= CC_VOLTA
  3635. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3636. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3637. const int nwarps = NWARPS_Q6_K_AMPERE;
  3638. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3639. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3640. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3641. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3642. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3643. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3644. const int nwarps = NWARPS_Q6_K_PASCAL;
  3645. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3646. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3647. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3648. #else
  3649. (void) vec_dot_q6_K_q8_1_mul_mat;
  3650. bad_arch();
  3651. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3652. }
  3653. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3654. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3655. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3656. if (row >= nrows) {
  3657. return;
  3658. }
  3659. const int blocks_per_row = ncols / qk;
  3660. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3661. // partial sum for each thread
  3662. float tmp = 0.0f;
  3663. const block_q_t * x = (const block_q_t *) vx;
  3664. const block_q8_1 * y = (const block_q8_1 *) vy;
  3665. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3666. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3667. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3668. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3669. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3670. }
  3671. // sum up partial sums and write back result
  3672. #pragma unroll
  3673. for (int mask = 16; mask > 0; mask >>= 1) {
  3674. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3675. }
  3676. if (threadIdx.x == 0) {
  3677. dst[row] = tmp;
  3678. }
  3679. }
  3680. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3681. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3682. // qk = quantized weights per x block
  3683. // qr = number of quantized weights per data value in x block
  3684. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3685. if (row >= nrows) {
  3686. return;
  3687. }
  3688. const int tid = threadIdx.x;
  3689. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3690. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3691. const int y_offset = qr == 1 ? 1 : qk/2;
  3692. // partial sum for each thread
  3693. #ifdef GGML_CUDA_F16
  3694. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3695. #else
  3696. float tmp = 0.0f;
  3697. #endif // GGML_CUDA_F16
  3698. for (int i = 0; i < ncols; i += iter_stride) {
  3699. const int col = i + vals_per_iter*tid;
  3700. const int ib = (row*ncols + col)/qk; // x block index
  3701. const int iqs = (col%qk)/qr; // x quant index
  3702. const int iybs = col - col%qk; // y block start index
  3703. // processing >2 values per i iter is faster for fast GPUs
  3704. #pragma unroll
  3705. for (int j = 0; j < vals_per_iter; j += 2) {
  3706. // process 2 vals per j iter
  3707. // dequantize
  3708. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3709. dfloat2 v;
  3710. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3711. // matrix multiplication
  3712. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3713. #ifdef GGML_CUDA_F16
  3714. tmp += __hmul2(v, {
  3715. y[iybs + iqs + j/qr + 0],
  3716. y[iybs + iqs + j/qr + y_offset]
  3717. });
  3718. #else
  3719. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3720. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3721. #endif // GGML_CUDA_F16
  3722. }
  3723. }
  3724. // sum up partial sums and write back result
  3725. #pragma unroll
  3726. for (int mask = 16; mask > 0; mask >>= 1) {
  3727. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3728. }
  3729. if (tid == 0) {
  3730. #ifdef GGML_CUDA_F16
  3731. dst[row] = tmp.x + tmp.y;
  3732. #else
  3733. dst[row] = tmp;
  3734. #endif // GGML_CUDA_F16
  3735. }
  3736. }
  3737. static __global__ void mul_mat_p021_f16_f32(
  3738. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3739. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3740. const half * x = (const half *) vx;
  3741. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3742. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3743. const int channel_x = channel / (nchannels_y / nchannels_x);
  3744. const int nrows_y = ncols_x;
  3745. const int nrows_dst = nrows_x;
  3746. const int row_dst = row_x;
  3747. float tmp = 0.0f;
  3748. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3749. const int col_x = col_x0 + threadIdx.x;
  3750. if (col_x >= ncols_x) {
  3751. break;
  3752. }
  3753. // x is transposed and permuted
  3754. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3755. const float xi = __half2float(x[ix]);
  3756. const int row_y = col_x;
  3757. // y is not transposed but permuted
  3758. const int iy = channel*nrows_y + row_y;
  3759. tmp += xi * y[iy];
  3760. }
  3761. // dst is not transposed and not permuted
  3762. const int idst = channel*nrows_dst + row_dst;
  3763. // sum up partial sums and write back result
  3764. #pragma unroll
  3765. for (int mask = 16; mask > 0; mask >>= 1) {
  3766. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3767. }
  3768. if (threadIdx.x == 0) {
  3769. dst[idst] = tmp;
  3770. }
  3771. }
  3772. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3773. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3774. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3775. const half * x = (const half *) vx;
  3776. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3777. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3778. const int channel_x = channel / channel_x_divisor;
  3779. const int nrows_y = ncols_x;
  3780. const int nrows_dst = nrows_x;
  3781. const int row_dst = row_x;
  3782. const int idst = channel*nrows_dst + row_dst;
  3783. float tmp = 0.0f;
  3784. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3785. const int col_x = col_x0 + threadIdx.x;
  3786. if (col_x >= ncols_x) {
  3787. break;
  3788. }
  3789. const int row_y = col_x;
  3790. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3791. const int iy = channel*nrows_y + row_y;
  3792. const float xi = __half2float(x[ix]);
  3793. tmp += xi * y[iy];
  3794. }
  3795. // sum up partial sums and write back result
  3796. #pragma unroll
  3797. for (int mask = 16; mask > 0; mask >>= 1) {
  3798. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3799. }
  3800. if (threadIdx.x == 0) {
  3801. dst[idst] = tmp;
  3802. }
  3803. }
  3804. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3805. const float * xi = (const float *) cxi;
  3806. float * dsti = (float *) cdsti;
  3807. *dsti = *xi;
  3808. }
  3809. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3810. const float * xi = (const float *) cxi;
  3811. half * dsti = (half *) cdsti;
  3812. *dsti = __float2half(*xi);
  3813. }
  3814. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3815. const half * xi = (const half *) cxi;
  3816. half * dsti = (half *) cdsti;
  3817. *dsti = *xi;
  3818. }
  3819. template <cpy_kernel_t cpy_1>
  3820. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3821. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3822. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3823. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3824. if (i >= ne) {
  3825. return;
  3826. }
  3827. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3828. // then combine those indices with the corresponding byte offsets to get the total offsets
  3829. const int i02 = i / (ne00*ne01);
  3830. const int i01 = (i - i02*ne01*ne00) / ne00;
  3831. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3832. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3833. const int i12 = i / (ne10*ne11);
  3834. const int i11 = (i - i12*ne10*ne11) / ne10;
  3835. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3836. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3837. cpy_1(cx + x_offset, cdst + dst_offset);
  3838. }
  3839. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  3840. const float * xi = (const float *) cxi;
  3841. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  3842. float amax = 0.0f; // absolute max
  3843. for (int j = 0; j < QK8_0; j++) {
  3844. const float v = xi[j];
  3845. amax = fmaxf(amax, fabsf(v));
  3846. }
  3847. const float d = amax / ((1 << 7) - 1);
  3848. const float id = d ? 1.0f/d : 0.0f;
  3849. dsti->d = d;
  3850. for (int j = 0; j < QK8_0; ++j) {
  3851. const float x0 = xi[j]*id;
  3852. dsti->qs[j] = roundf(x0);
  3853. }
  3854. }
  3855. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  3856. const float * xi = (const float *) cxi;
  3857. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  3858. float amax = 0.0f;
  3859. float vmax = 0.0f;
  3860. for (int j = 0; j < QK4_0; ++j) {
  3861. const float v = xi[j];
  3862. if (amax < fabsf(v)) {
  3863. amax = fabsf(v);
  3864. vmax = v;
  3865. }
  3866. }
  3867. const float d = vmax / -8;
  3868. const float id = d ? 1.0f/d : 0.0f;
  3869. dsti->d = d;
  3870. for (int j = 0; j < QK4_0/2; ++j) {
  3871. const float x0 = xi[0 + j]*id;
  3872. const float x1 = xi[QK4_0/2 + j]*id;
  3873. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  3874. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  3875. dsti->qs[j] = xi0;
  3876. dsti->qs[j] |= xi1 << 4;
  3877. }
  3878. }
  3879. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  3880. const float * xi = (const float *) cxi;
  3881. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  3882. float vmin = FLT_MAX;
  3883. float vmax = -FLT_MAX;
  3884. for (int j = 0; j < QK4_1; ++j) {
  3885. const float v = xi[j];
  3886. if (v < vmin) vmin = v;
  3887. if (v > vmax) vmax = v;
  3888. }
  3889. const float d = (vmax - vmin) / ((1 << 4) - 1);
  3890. const float id = d ? 1.0f/d : 0.0f;
  3891. dsti->dm.x = d;
  3892. dsti->dm.y = vmin;
  3893. for (int j = 0; j < QK4_1/2; ++j) {
  3894. const float x0 = (xi[0 + j] - vmin)*id;
  3895. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  3896. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  3897. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  3898. dsti->qs[j] = xi0;
  3899. dsti->qs[j] |= xi1 << 4;
  3900. }
  3901. }
  3902. template <cpy_kernel_t cpy_blck, int qk>
  3903. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  3904. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3905. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3906. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  3907. if (i >= ne) {
  3908. return;
  3909. }
  3910. const int i02 = i / (ne00*ne01);
  3911. const int i01 = (i - i02*ne01*ne00) / ne00;
  3912. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  3913. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3914. const int i12 = i / (ne10*ne11);
  3915. const int i11 = (i - i12*ne10*ne11) / ne10;
  3916. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  3917. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3918. cpy_blck(cx + x_offset, cdst + dst_offset);
  3919. }
  3920. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3921. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3922. return 1.0f - min(1.0f, max(0.0f, y));
  3923. }
  3924. struct rope_corr_dims {
  3925. float v[4];
  3926. };
  3927. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3928. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3929. static __device__ void rope_yarn(
  3930. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3931. float * cos_theta, float * sin_theta
  3932. ) {
  3933. // Get n-d rotational scaling corrected for extrapolation
  3934. float theta_interp = freq_scale * theta_extrap;
  3935. float theta = theta_interp;
  3936. if (ext_factor != 0.0f) {
  3937. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3938. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3939. // Get n-d magnitude scaling corrected for interpolation
  3940. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3941. }
  3942. *cos_theta = cosf(theta) * mscale;
  3943. *sin_theta = sinf(theta) * mscale;
  3944. }
  3945. // rope == RoPE == rotary positional embedding
  3946. template<typename T, bool has_pos>
  3947. static __global__ void rope(
  3948. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3949. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3950. ) {
  3951. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3952. if (col >= ncols) {
  3953. return;
  3954. }
  3955. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3956. const int i = row*ncols + col;
  3957. const int i2 = row/p_delta_rows;
  3958. const int p = has_pos ? pos[i2] : 0;
  3959. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3960. float cos_theta, sin_theta;
  3961. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3962. const float x0 = x[i + 0];
  3963. const float x1 = x[i + 1];
  3964. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3965. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3966. }
  3967. template<typename T, bool has_pos>
  3968. static __global__ void rope_neox(
  3969. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  3970. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  3971. ) {
  3972. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3973. if (col >= ncols) {
  3974. return;
  3975. }
  3976. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3977. const int ib = col / n_dims;
  3978. const int ic = col % n_dims;
  3979. if (ib > 0) {
  3980. const int i = row*ncols + ib*n_dims + ic;
  3981. dst[i + 0] = x[i + 0];
  3982. dst[i + 1] = x[i + 1];
  3983. return;
  3984. }
  3985. const int i = row*ncols + ib*n_dims + ic/2;
  3986. const int i2 = row/p_delta_rows;
  3987. float cur_rot = inv_ndims * ic - ib;
  3988. const int p = has_pos ? pos[i2] : 0;
  3989. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  3990. float cos_theta, sin_theta;
  3991. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3992. const float x0 = x[i + 0];
  3993. const float x1 = x[i + n_dims/2];
  3994. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3995. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  3996. }
  3997. static __global__ void rope_glm_f32(
  3998. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3999. int n_ctx
  4000. ) {
  4001. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4002. const int half_n_dims = ncols/4;
  4003. if (col >= half_n_dims) {
  4004. return;
  4005. }
  4006. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4007. const int i = row*ncols + col;
  4008. const int i2 = row/p_delta_rows;
  4009. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4010. // FIXME: this is likely wrong
  4011. const int p = pos != nullptr ? pos[i2] : 0;
  4012. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4013. const float sin_theta = sinf(theta);
  4014. const float cos_theta = cosf(theta);
  4015. const float x0 = x[i + 0];
  4016. const float x1 = x[i + half_n_dims];
  4017. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4018. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4019. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4020. const float sin_block_theta = sinf(block_theta);
  4021. const float cos_block_theta = cosf(block_theta);
  4022. const float x2 = x[i + half_n_dims * 2];
  4023. const float x3 = x[i + half_n_dims * 3];
  4024. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4025. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4026. }
  4027. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4028. const int n_heads_log2_floor, const float m0, const float m1) {
  4029. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4030. if (col >= ncols) {
  4031. return;
  4032. }
  4033. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4034. const int i = row*ncols + col;
  4035. const int k = row/k_rows;
  4036. float m_k;
  4037. if (k < n_heads_log2_floor) {
  4038. m_k = powf(m0, k + 1);
  4039. } else {
  4040. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4041. }
  4042. dst[i] = col * m_k + x[i];
  4043. }
  4044. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4045. const int row = blockIdx.y;
  4046. const int col = threadIdx.x;
  4047. float sum = 0.0f;
  4048. for (int i = col; i < ncols; i += blockDim.x) {
  4049. sum += x[row * ncols + i];
  4050. }
  4051. sum = warp_reduce_sum(sum);
  4052. if (col == 0) {
  4053. dst[row] = sum;
  4054. }
  4055. }
  4056. template<typename T>
  4057. static inline __device__ void swap(T & a, T & b) {
  4058. T tmp = a;
  4059. a = b;
  4060. b = tmp;
  4061. }
  4062. template<ggml_sort_order order>
  4063. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4064. // bitonic sort
  4065. int col = threadIdx.x;
  4066. int row = blockIdx.y;
  4067. if (col >= ncols) return;
  4068. const float * x_row = x + row * ncols;
  4069. int * dst_row = dst + row * ncols;
  4070. // initialize indices
  4071. if (col < ncols) {
  4072. dst_row[col] = col;
  4073. }
  4074. __syncthreads();
  4075. for (int k = 2; k <= ncols; k *= 2) {
  4076. for (int j = k / 2; j > 0; j /= 2) {
  4077. int ixj = col ^ j;
  4078. if (ixj > col) {
  4079. if ((col & k) == 0) {
  4080. 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]]) {
  4081. swap(dst_row[col], dst_row[ixj]);
  4082. }
  4083. } else {
  4084. 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]]) {
  4085. swap(dst_row[col], dst_row[ixj]);
  4086. }
  4087. }
  4088. }
  4089. __syncthreads();
  4090. }
  4091. }
  4092. }
  4093. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4094. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4095. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4096. if (col >= ncols) {
  4097. return;
  4098. }
  4099. const int i = row*ncols + col;
  4100. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4101. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4102. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4103. }
  4104. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {
  4105. const int tid = threadIdx.x;
  4106. const int rowx = blockIdx.x;
  4107. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4108. const int block_size = blockDim.x;
  4109. const int warp_id = threadIdx.x / WARP_SIZE;
  4110. const int lane_id = threadIdx.x % WARP_SIZE;
  4111. __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];
  4112. float max_val = -INFINITY;
  4113. for (int col = tid; col < ncols; col += block_size) {
  4114. const int ix = rowx*ncols + col;
  4115. const int iy = rowy*ncols + col;
  4116. max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));
  4117. }
  4118. // find the max value in the block
  4119. max_val = warp_reduce_max(max_val);
  4120. if (block_size > WARP_SIZE) {
  4121. if (warp_id == 0) {
  4122. buf[lane_id] = -INFINITY;
  4123. }
  4124. __syncthreads();
  4125. if (lane_id == 0) {
  4126. buf[warp_id] = max_val;
  4127. }
  4128. __syncthreads();
  4129. max_val = buf[lane_id];
  4130. max_val = warp_reduce_max(max_val);
  4131. }
  4132. float tmp = 0.f;
  4133. for (int col = tid; col < ncols; col += block_size) {
  4134. const int ix = rowx*ncols + col;
  4135. const int iy = rowy*ncols + col;
  4136. const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);
  4137. tmp += val;
  4138. dst[ix] = val;
  4139. }
  4140. // find the sum of exps in the block
  4141. tmp = warp_reduce_sum(tmp);
  4142. if (block_size > WARP_SIZE) {
  4143. if (warp_id == 0) {
  4144. buf[lane_id] = 0.f;
  4145. }
  4146. __syncthreads();
  4147. if (lane_id == 0) {
  4148. buf[warp_id] = tmp;
  4149. }
  4150. __syncthreads();
  4151. tmp = buf[lane_id];
  4152. tmp = warp_reduce_sum(tmp);
  4153. }
  4154. const float inv_tmp = 1.f / tmp;
  4155. for (int col = tid; col < ncols; col += block_size) {
  4156. const int i = rowx*ncols + col;
  4157. dst[i] *= inv_tmp;
  4158. }
  4159. }
  4160. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4161. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4162. if (i >= k) {
  4163. return;
  4164. }
  4165. dst[i] = scale * x[i];
  4166. }
  4167. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4168. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4169. if (i >= k) {
  4170. return;
  4171. }
  4172. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4173. }
  4174. static __global__ void im2col_f32_f16(
  4175. const float * x, half * dst,
  4176. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4177. int s0, int s1, int p0, int p1, int d0, int d1) {
  4178. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4179. if (i >= pelements) {
  4180. return;
  4181. }
  4182. const int ksize = OW * (KH > 1 ? KW : 1);
  4183. const int kx = i / ksize;
  4184. const int kd = kx * ksize;
  4185. const int ky = (i - kd) / OW;
  4186. const int ix = i % OW;
  4187. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4188. const int64_t iih = blockIdx.y * s1 + ky * d1 - p1;
  4189. const int64_t offset_dst =
  4190. (blockIdx.y * OW + ix) * CHW +
  4191. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4192. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4193. dst[offset_dst] = __float2half(0.0f);
  4194. } else {
  4195. const int64_t offset_src = blockIdx.z * offset_delta;
  4196. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4197. }
  4198. }
  4199. template<int qk, int qr, dequantize_kernel_t dq>
  4200. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4201. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4202. GGML_TENSOR_BINARY_OP_LOCALS
  4203. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4204. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4205. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4206. // strides in elements
  4207. //const size_t s0 = nb0 / ggml_element_size(dst);
  4208. const size_t s1 = nb1 / ggml_element_size(dst);
  4209. const size_t s2 = nb2 / ggml_element_size(dst);
  4210. const size_t s3 = nb3 / ggml_element_size(dst);
  4211. const size_t s10 = nb10 / ggml_element_size(src1);
  4212. const size_t s11 = nb11 / ggml_element_size(src1);
  4213. const size_t s12 = nb12 / ggml_element_size(src1);
  4214. //const size_t s13 = nb13 / ggml_element_size(src1);
  4215. GGML_ASSERT(ne00 % 2 == 0);
  4216. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4217. src0_dd, src1_dd, dst_dd,
  4218. ne00, /*ne01, ne02, ne03,*/
  4219. /*ne10, ne11,*/ ne12, /*ne13,*/
  4220. /* s0,*/ s1, s2, s3,
  4221. /* nb00,*/ nb01, nb02, nb03,
  4222. s10, s11, s12/*, s13*/);
  4223. (void) dst;
  4224. }
  4225. template<typename src0_t>
  4226. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4227. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4228. GGML_TENSOR_BINARY_OP_LOCALS
  4229. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4230. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4231. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4232. // strides in elements
  4233. //const size_t s0 = nb0 / ggml_element_size(dst);
  4234. const size_t s1 = nb1 / ggml_element_size(dst);
  4235. const size_t s2 = nb2 / ggml_element_size(dst);
  4236. const size_t s3 = nb3 / ggml_element_size(dst);
  4237. const size_t s10 = nb10 / ggml_element_size(src1);
  4238. const size_t s11 = nb11 / ggml_element_size(src1);
  4239. const size_t s12 = nb12 / ggml_element_size(src1);
  4240. //const size_t s13 = nb13 / ggml_element_size(src1);
  4241. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4242. src0_dd, src1_dd, dst_dd,
  4243. ne00, /*ne01, ne02, ne03,*/
  4244. /*ne10, ne11,*/ ne12, /*ne13,*/
  4245. /* s0,*/ s1, s2, s3,
  4246. /* nb00,*/ nb01, nb02, nb03,
  4247. s10, s11, s12/*, s13*/);
  4248. (void) dst;
  4249. }
  4250. template<float (*bin_op)(const float, const float)>
  4251. struct bin_bcast_cuda {
  4252. template<typename src0_t, typename src1_t, typename dst_t>
  4253. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4254. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4255. cudaStream_t stream) {
  4256. GGML_TENSOR_BINARY_OP_LOCALS
  4257. int nr0 = ne10/ne0;
  4258. int nr1 = ne11/ne1;
  4259. int nr2 = ne12/ne2;
  4260. int nr3 = ne13/ne3;
  4261. int nr[4] = { nr0, nr1, nr2, nr3 };
  4262. // collapse dimensions until first broadcast dimension
  4263. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4264. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4265. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4266. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4267. auto collapse = [](int64_t cne[]) {
  4268. cne[0] *= cne[1];
  4269. cne[1] = cne[2];
  4270. cne[2] = cne[3];
  4271. cne[3] = 1;
  4272. };
  4273. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  4274. cnb[1] *= cne[1];
  4275. cnb[2] *= cne[2];
  4276. cnb[3] *= cne[3];
  4277. };
  4278. for (int i = 0; i < 4; i++) {
  4279. if (nr[i] != 1) {
  4280. break;
  4281. }
  4282. if (i > 0) {
  4283. collapse_nb(cnb0, cne0);
  4284. collapse_nb(cnb1, cne1);
  4285. collapse(cne0);
  4286. collapse(cne1);
  4287. }
  4288. }
  4289. {
  4290. int64_t ne0 = cne0[0];
  4291. int64_t ne1 = cne0[1];
  4292. int64_t ne2 = cne0[2];
  4293. int64_t ne3 = cne0[3];
  4294. int64_t ne10 = cne1[0];
  4295. int64_t ne11 = cne1[1];
  4296. int64_t ne12 = cne1[2];
  4297. int64_t ne13 = cne1[3];
  4298. size_t nb0 = cnb0[0];
  4299. size_t nb1 = cnb0[1];
  4300. size_t nb2 = cnb0[2];
  4301. size_t nb3 = cnb0[3];
  4302. size_t nb10 = cnb1[0];
  4303. size_t nb11 = cnb1[1];
  4304. size_t nb12 = cnb1[2];
  4305. size_t nb13 = cnb1[3];
  4306. size_t s0 = nb0 / sizeof(dst_t);
  4307. size_t s1 = nb1 / sizeof(dst_t);
  4308. size_t s2 = nb2 / sizeof(dst_t);
  4309. size_t s3 = nb3 / sizeof(dst_t);
  4310. size_t s10 = nb10 / sizeof(src1_t);
  4311. size_t s11 = nb11 / sizeof(src1_t);
  4312. size_t s12 = nb12 / sizeof(src1_t);
  4313. size_t s13 = nb13 / sizeof(src1_t);
  4314. GGML_ASSERT(s0 == 1);
  4315. GGML_ASSERT(s10 == 1);
  4316. const int block_size = 128;
  4317. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4318. dim3 block_dims;
  4319. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4320. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4321. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4322. dim3 block_nums(
  4323. (hne0 + block_dims.x - 1) / block_dims.x,
  4324. (ne1 + block_dims.y - 1) / block_dims.y,
  4325. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4326. );
  4327. if (block_nums.z > 65535) {
  4328. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4329. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4330. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4331. src0_dd, src1_dd, dst_dd,
  4332. ne0, ne1, ne2, ne3,
  4333. ne10, ne11, ne12, ne13,
  4334. /* s0, */ s1, s2, s3,
  4335. /* s10, */ s11, s12, s13);
  4336. } else {
  4337. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4338. src0_dd, src1_dd, dst_dd,
  4339. ne0, ne1, ne2, ne3,
  4340. ne10, ne11, ne12, ne13,
  4341. /* s0, */ s1, s2, s3,
  4342. /* s10, */ s11, s12, s13);
  4343. }
  4344. }
  4345. }
  4346. };
  4347. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4348. const int ne10, const int ne11, const int ne12,
  4349. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4350. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4351. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4352. }
  4353. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4354. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4355. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4356. }
  4357. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4358. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4359. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4360. }
  4361. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4362. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4363. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4364. }
  4365. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4366. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4367. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4368. }
  4369. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4370. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4371. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4372. }
  4373. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4374. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4375. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4376. }
  4377. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4378. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4379. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4380. }
  4381. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4382. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4383. if (ncols < 1024) {
  4384. const dim3 block_dims(WARP_SIZE, 1, 1);
  4385. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4386. } else {
  4387. const dim3 block_dims(1024, 1, 1);
  4388. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4389. }
  4390. }
  4391. 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) {
  4392. static const float eps = 1e-6f;
  4393. if (group_size < 1024) {
  4394. const dim3 block_dims(WARP_SIZE, 1, 1);
  4395. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4396. } else {
  4397. const dim3 block_dims(1024, 1, 1);
  4398. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4399. }
  4400. }
  4401. 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) {
  4402. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4403. dim3 gridDim(num_blocks, ne1, ne2);
  4404. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4405. }
  4406. 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) {
  4407. int ne0 = (ne00 * scale_factor);
  4408. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4409. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4410. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4411. }
  4412. static void pad_f32_cuda(const float * x, float * dst,
  4413. const int ne00, const int ne01, const int ne02,
  4414. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4415. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4416. dim3 gridDim(num_blocks, ne1, ne2);
  4417. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4418. }
  4419. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4420. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4421. if (ncols < 1024) {
  4422. const dim3 block_dims(WARP_SIZE, 1, 1);
  4423. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4424. } else {
  4425. const dim3 block_dims(1024, 1, 1);
  4426. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4427. }
  4428. }
  4429. 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) {
  4430. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4431. const dim3 num_blocks(block_num_x, ky, 1);
  4432. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4433. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4434. }
  4435. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4436. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4437. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4438. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4439. }
  4440. template<typename dst_t>
  4441. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4442. const int nb = k / QK_K;
  4443. #if QK_K == 256
  4444. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4445. #else
  4446. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4447. #endif
  4448. }
  4449. template<typename dst_t>
  4450. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4451. const int nb = k / QK_K;
  4452. #if QK_K == 256
  4453. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4454. #else
  4455. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4456. #endif
  4457. }
  4458. template<typename dst_t>
  4459. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4460. const int nb = k / QK_K;
  4461. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4462. }
  4463. template<typename dst_t>
  4464. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4465. const int nb = k / QK_K;
  4466. #if QK_K == 256
  4467. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4468. #else
  4469. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4470. #endif
  4471. }
  4472. template<typename dst_t>
  4473. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4474. const int nb = k / QK_K;
  4475. #if QK_K == 256
  4476. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4477. #else
  4478. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4479. #endif
  4480. }
  4481. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4482. switch (type) {
  4483. case GGML_TYPE_Q4_0:
  4484. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4485. case GGML_TYPE_Q4_1:
  4486. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4487. case GGML_TYPE_Q5_0:
  4488. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4489. case GGML_TYPE_Q5_1:
  4490. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4491. case GGML_TYPE_Q8_0:
  4492. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4493. case GGML_TYPE_Q2_K:
  4494. return dequantize_row_q2_K_cuda;
  4495. case GGML_TYPE_Q3_K:
  4496. return dequantize_row_q3_K_cuda;
  4497. case GGML_TYPE_Q4_K:
  4498. return dequantize_row_q4_K_cuda;
  4499. case GGML_TYPE_Q5_K:
  4500. return dequantize_row_q5_K_cuda;
  4501. case GGML_TYPE_Q6_K:
  4502. return dequantize_row_q6_K_cuda;
  4503. case GGML_TYPE_F32:
  4504. return dequantize_block_cuda<1, 1, convert_f32>;
  4505. default:
  4506. return nullptr;
  4507. }
  4508. }
  4509. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4510. switch (type) {
  4511. case GGML_TYPE_Q4_0:
  4512. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4513. case GGML_TYPE_Q4_1:
  4514. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4515. case GGML_TYPE_Q5_0:
  4516. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4517. case GGML_TYPE_Q5_1:
  4518. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4519. case GGML_TYPE_Q8_0:
  4520. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4521. case GGML_TYPE_Q2_K:
  4522. return dequantize_row_q2_K_cuda;
  4523. case GGML_TYPE_Q3_K:
  4524. return dequantize_row_q3_K_cuda;
  4525. case GGML_TYPE_Q4_K:
  4526. return dequantize_row_q4_K_cuda;
  4527. case GGML_TYPE_Q5_K:
  4528. return dequantize_row_q5_K_cuda;
  4529. case GGML_TYPE_Q6_K:
  4530. return dequantize_row_q6_K_cuda;
  4531. case GGML_TYPE_F16:
  4532. return dequantize_block_cuda<1, 1, convert_f16>;
  4533. default:
  4534. return nullptr;
  4535. }
  4536. }
  4537. 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) {
  4538. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4539. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4540. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4541. const dim3 block_nums(block_num_y, 1, 1);
  4542. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4543. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4544. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4545. }
  4546. 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) {
  4547. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4548. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4549. const dim3 block_nums(block_num_y, 1, 1);
  4550. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4551. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4552. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4553. }
  4554. 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) {
  4555. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4556. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4557. const dim3 block_nums(block_num_y, 1, 1);
  4558. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4559. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4560. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4561. }
  4562. 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) {
  4563. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4564. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4565. const dim3 block_nums(block_num_y, 1, 1);
  4566. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4567. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4568. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4569. }
  4570. 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) {
  4571. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4572. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4573. const dim3 block_nums(block_num_y, 1, 1);
  4574. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4575. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4576. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4577. }
  4578. 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) {
  4579. GGML_ASSERT(ncols % QK_K == 0);
  4580. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4581. const int block_num_y = (nrows + ny - 1) / ny;
  4582. const dim3 block_nums(block_num_y, 1, 1);
  4583. const dim3 block_dims(32, ny, 1);
  4584. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4585. }
  4586. 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) {
  4587. GGML_ASSERT(ncols % QK_K == 0);
  4588. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4589. const int block_num_y = (nrows + ny - 1) / ny;
  4590. const dim3 block_nums(block_num_y, 1, 1);
  4591. const dim3 block_dims(32, ny, 1);
  4592. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4593. }
  4594. 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) {
  4595. GGML_ASSERT(ncols % QK_K == 0);
  4596. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4597. const int block_num_y = (nrows + ny - 1) / ny;
  4598. const dim3 block_nums(block_num_y, 1, 1);
  4599. const dim3 block_dims(32, ny, 1);
  4600. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4601. }
  4602. 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) {
  4603. GGML_ASSERT(ncols % QK_K == 0);
  4604. const dim3 block_dims(32, 1, 1);
  4605. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4606. }
  4607. 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) {
  4608. GGML_ASSERT(ncols % QK_K == 0);
  4609. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4610. const int block_num_y = (nrows + ny - 1) / ny;
  4611. const dim3 block_nums(block_num_y, 1, 1);
  4612. const dim3 block_dims(32, ny, 1);
  4613. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4614. }
  4615. 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) {
  4616. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4617. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4618. const dim3 block_nums(block_num_y, 1, 1);
  4619. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4620. dequantize_mul_mat_vec<1, 1, convert_f16>
  4621. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4622. }
  4623. 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) {
  4624. GGML_ASSERT(ncols % QK4_0 == 0);
  4625. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4626. const dim3 block_nums(block_num_y, 1, 1);
  4627. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4628. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4629. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4630. }
  4631. 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) {
  4632. GGML_ASSERT(ncols % QK4_1 == 0);
  4633. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4634. const dim3 block_nums(block_num_y, 1, 1);
  4635. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4636. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4637. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4638. }
  4639. 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) {
  4640. GGML_ASSERT(ncols % QK5_0 == 0);
  4641. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4642. const dim3 block_nums(block_num_y, 1, 1);
  4643. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4644. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4645. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4646. }
  4647. 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) {
  4648. GGML_ASSERT(ncols % QK5_1 == 0);
  4649. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4650. const dim3 block_nums(block_num_y, 1, 1);
  4651. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4652. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4653. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4654. }
  4655. 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) {
  4656. GGML_ASSERT(ncols % QK8_0 == 0);
  4657. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4658. const dim3 block_nums(block_num_y, 1, 1);
  4659. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4660. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4661. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4662. }
  4663. 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) {
  4664. GGML_ASSERT(ncols % QK_K == 0);
  4665. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4666. const dim3 block_nums(block_num_y, 1, 1);
  4667. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4668. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  4669. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4670. }
  4671. 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) {
  4672. GGML_ASSERT(ncols % QK_K == 0);
  4673. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4674. const dim3 block_nums(block_num_y, 1, 1);
  4675. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4676. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4677. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4678. }
  4679. 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) {
  4680. GGML_ASSERT(ncols % QK_K == 0);
  4681. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4682. const dim3 block_nums(block_num_y, 1, 1);
  4683. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4684. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4685. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4686. }
  4687. 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) {
  4688. GGML_ASSERT(ncols % QK_K == 0);
  4689. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4690. const dim3 block_nums(block_num_y, 1, 1);
  4691. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4692. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4693. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4694. }
  4695. 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) {
  4696. GGML_ASSERT(ncols % QK_K == 0);
  4697. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4698. const dim3 block_nums(block_num_y, 1, 1);
  4699. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4700. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4701. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4702. }
  4703. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4704. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4705. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4706. int id;
  4707. CUDA_CHECK(cudaGetDevice(&id));
  4708. const int compute_capability = g_device_caps[id].cc;
  4709. int mmq_x, mmq_y, nwarps;
  4710. if (compute_capability >= CC_RDNA2) {
  4711. mmq_x = MMQ_X_Q4_0_RDNA2;
  4712. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4713. nwarps = NWARPS_Q4_0_RDNA2;
  4714. } else if (compute_capability >= CC_OFFSET_AMD) {
  4715. mmq_x = MMQ_X_Q4_0_RDNA1;
  4716. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4717. nwarps = NWARPS_Q4_0_RDNA1;
  4718. } else if (compute_capability >= CC_VOLTA) {
  4719. mmq_x = MMQ_X_Q4_0_AMPERE;
  4720. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4721. nwarps = NWARPS_Q4_0_AMPERE;
  4722. } else if (compute_capability >= MIN_CC_DP4A) {
  4723. mmq_x = MMQ_X_Q4_0_PASCAL;
  4724. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4725. nwarps = NWARPS_Q4_0_PASCAL;
  4726. } else {
  4727. GGML_ASSERT(false);
  4728. }
  4729. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4730. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4731. const dim3 block_nums(block_num_x, block_num_y, 1);
  4732. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4733. if (nrows_x % mmq_y == 0) {
  4734. const bool need_check = false;
  4735. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4736. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4737. } else {
  4738. const bool need_check = true;
  4739. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4740. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4741. }
  4742. }
  4743. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4744. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4745. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4746. int id;
  4747. CUDA_CHECK(cudaGetDevice(&id));
  4748. const int compute_capability = g_device_caps[id].cc;
  4749. int mmq_x, mmq_y, nwarps;
  4750. if (compute_capability >= CC_RDNA2) {
  4751. mmq_x = MMQ_X_Q4_1_RDNA2;
  4752. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4753. nwarps = NWARPS_Q4_1_RDNA2;
  4754. } else if (compute_capability >= CC_OFFSET_AMD) {
  4755. mmq_x = MMQ_X_Q4_1_RDNA1;
  4756. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4757. nwarps = NWARPS_Q4_1_RDNA1;
  4758. } else if (compute_capability >= CC_VOLTA) {
  4759. mmq_x = MMQ_X_Q4_1_AMPERE;
  4760. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4761. nwarps = NWARPS_Q4_1_AMPERE;
  4762. } else if (compute_capability >= MIN_CC_DP4A) {
  4763. mmq_x = MMQ_X_Q4_1_PASCAL;
  4764. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4765. nwarps = NWARPS_Q4_1_PASCAL;
  4766. } else {
  4767. GGML_ASSERT(false);
  4768. }
  4769. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4770. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4771. const dim3 block_nums(block_num_x, block_num_y, 1);
  4772. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4773. if (nrows_x % mmq_y == 0) {
  4774. const bool need_check = false;
  4775. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4776. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4777. } else {
  4778. const bool need_check = true;
  4779. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4780. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4781. }
  4782. }
  4783. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4784. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4785. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4786. int id;
  4787. CUDA_CHECK(cudaGetDevice(&id));
  4788. const int compute_capability = g_device_caps[id].cc;
  4789. int mmq_x, mmq_y, nwarps;
  4790. if (compute_capability >= CC_RDNA2) {
  4791. mmq_x = MMQ_X_Q5_0_RDNA2;
  4792. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4793. nwarps = NWARPS_Q5_0_RDNA2;
  4794. } else if (compute_capability >= CC_OFFSET_AMD) {
  4795. mmq_x = MMQ_X_Q5_0_RDNA1;
  4796. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4797. nwarps = NWARPS_Q5_0_RDNA1;
  4798. } else if (compute_capability >= CC_VOLTA) {
  4799. mmq_x = MMQ_X_Q5_0_AMPERE;
  4800. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4801. nwarps = NWARPS_Q5_0_AMPERE;
  4802. } else if (compute_capability >= MIN_CC_DP4A) {
  4803. mmq_x = MMQ_X_Q5_0_PASCAL;
  4804. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4805. nwarps = NWARPS_Q5_0_PASCAL;
  4806. } else {
  4807. GGML_ASSERT(false);
  4808. }
  4809. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4810. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4811. const dim3 block_nums(block_num_x, block_num_y, 1);
  4812. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4813. if (nrows_x % mmq_y == 0) {
  4814. const bool need_check = false;
  4815. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4816. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4817. } else {
  4818. const bool need_check = true;
  4819. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4820. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4821. }
  4822. }
  4823. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4824. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4825. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4826. int id;
  4827. CUDA_CHECK(cudaGetDevice(&id));
  4828. const int compute_capability = g_device_caps[id].cc;
  4829. int mmq_x, mmq_y, nwarps;
  4830. if (compute_capability >= CC_RDNA2) {
  4831. mmq_x = MMQ_X_Q5_1_RDNA2;
  4832. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4833. nwarps = NWARPS_Q5_1_RDNA2;
  4834. } else if (compute_capability >= CC_OFFSET_AMD) {
  4835. mmq_x = MMQ_X_Q5_1_RDNA1;
  4836. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4837. nwarps = NWARPS_Q5_1_RDNA1;
  4838. } else if (compute_capability >= CC_VOLTA) {
  4839. mmq_x = MMQ_X_Q5_1_AMPERE;
  4840. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4841. nwarps = NWARPS_Q5_1_AMPERE;
  4842. } else if (compute_capability >= MIN_CC_DP4A) {
  4843. mmq_x = MMQ_X_Q5_1_PASCAL;
  4844. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4845. nwarps = NWARPS_Q5_1_PASCAL;
  4846. } else {
  4847. GGML_ASSERT(false);
  4848. }
  4849. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4850. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4851. const dim3 block_nums(block_num_x, block_num_y, 1);
  4852. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4853. if (nrows_x % mmq_y == 0) {
  4854. const bool need_check = false;
  4855. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4856. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4857. } else {
  4858. const bool need_check = true;
  4859. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4860. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4861. }
  4862. }
  4863. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4864. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4865. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4866. int id;
  4867. CUDA_CHECK(cudaGetDevice(&id));
  4868. const int compute_capability = g_device_caps[id].cc;
  4869. int mmq_x, mmq_y, nwarps;
  4870. if (compute_capability >= CC_RDNA2) {
  4871. mmq_x = MMQ_X_Q8_0_RDNA2;
  4872. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4873. nwarps = NWARPS_Q8_0_RDNA2;
  4874. } else if (compute_capability >= CC_OFFSET_AMD) {
  4875. mmq_x = MMQ_X_Q8_0_RDNA1;
  4876. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4877. nwarps = NWARPS_Q8_0_RDNA1;
  4878. } else if (compute_capability >= CC_VOLTA) {
  4879. mmq_x = MMQ_X_Q8_0_AMPERE;
  4880. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4881. nwarps = NWARPS_Q8_0_AMPERE;
  4882. } else if (compute_capability >= MIN_CC_DP4A) {
  4883. mmq_x = MMQ_X_Q8_0_PASCAL;
  4884. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4885. nwarps = NWARPS_Q8_0_PASCAL;
  4886. } else {
  4887. GGML_ASSERT(false);
  4888. }
  4889. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4890. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4891. const dim3 block_nums(block_num_x, block_num_y, 1);
  4892. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4893. if (nrows_x % mmq_y == 0) {
  4894. const bool need_check = false;
  4895. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4896. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4897. } else {
  4898. const bool need_check = true;
  4899. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4900. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4901. }
  4902. }
  4903. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4904. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4905. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4906. int id;
  4907. CUDA_CHECK(cudaGetDevice(&id));
  4908. const int compute_capability = g_device_caps[id].cc;
  4909. int mmq_x, mmq_y, nwarps;
  4910. if (compute_capability >= CC_RDNA2) {
  4911. mmq_x = MMQ_X_Q2_K_RDNA2;
  4912. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4913. nwarps = NWARPS_Q2_K_RDNA2;
  4914. } else if (compute_capability >= CC_OFFSET_AMD) {
  4915. mmq_x = MMQ_X_Q2_K_RDNA1;
  4916. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4917. nwarps = NWARPS_Q2_K_RDNA1;
  4918. } else if (compute_capability >= CC_VOLTA) {
  4919. mmq_x = MMQ_X_Q2_K_AMPERE;
  4920. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4921. nwarps = NWARPS_Q2_K_AMPERE;
  4922. } else if (compute_capability >= MIN_CC_DP4A) {
  4923. mmq_x = MMQ_X_Q2_K_PASCAL;
  4924. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4925. nwarps = NWARPS_Q2_K_PASCAL;
  4926. } else {
  4927. GGML_ASSERT(false);
  4928. }
  4929. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4930. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4931. const dim3 block_nums(block_num_x, block_num_y, 1);
  4932. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4933. if (nrows_x % mmq_y == 0) {
  4934. const bool need_check = false;
  4935. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4936. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4937. } else {
  4938. const bool need_check = true;
  4939. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4940. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4941. }
  4942. }
  4943. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4944. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4945. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4946. #if QK_K == 256
  4947. int id;
  4948. CUDA_CHECK(cudaGetDevice(&id));
  4949. const int compute_capability = g_device_caps[id].cc;
  4950. int mmq_x, mmq_y, nwarps;
  4951. if (compute_capability >= CC_RDNA2) {
  4952. mmq_x = MMQ_X_Q3_K_RDNA2;
  4953. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4954. nwarps = NWARPS_Q3_K_RDNA2;
  4955. } else if (compute_capability >= CC_OFFSET_AMD) {
  4956. mmq_x = MMQ_X_Q3_K_RDNA1;
  4957. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4958. nwarps = NWARPS_Q3_K_RDNA1;
  4959. } else if (compute_capability >= CC_VOLTA) {
  4960. mmq_x = MMQ_X_Q3_K_AMPERE;
  4961. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4962. nwarps = NWARPS_Q3_K_AMPERE;
  4963. } else if (compute_capability >= MIN_CC_DP4A) {
  4964. mmq_x = MMQ_X_Q3_K_PASCAL;
  4965. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4966. nwarps = NWARPS_Q3_K_PASCAL;
  4967. } else {
  4968. GGML_ASSERT(false);
  4969. }
  4970. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4971. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4972. const dim3 block_nums(block_num_x, block_num_y, 1);
  4973. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4974. if (nrows_x % mmq_y == 0) {
  4975. const bool need_check = false;
  4976. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4977. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4978. } else {
  4979. const bool need_check = true;
  4980. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4981. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4982. }
  4983. #endif
  4984. }
  4985. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4986. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4987. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4988. int id;
  4989. CUDA_CHECK(cudaGetDevice(&id));
  4990. const int compute_capability = g_device_caps[id].cc;
  4991. int mmq_x, mmq_y, nwarps;
  4992. if (compute_capability >= CC_RDNA2) {
  4993. mmq_x = MMQ_X_Q4_K_RDNA2;
  4994. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4995. nwarps = NWARPS_Q4_K_RDNA2;
  4996. } else if (compute_capability >= CC_OFFSET_AMD) {
  4997. mmq_x = MMQ_X_Q4_K_RDNA1;
  4998. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4999. nwarps = NWARPS_Q4_K_RDNA1;
  5000. } else if (compute_capability >= CC_VOLTA) {
  5001. mmq_x = MMQ_X_Q4_K_AMPERE;
  5002. mmq_y = MMQ_Y_Q4_K_AMPERE;
  5003. nwarps = NWARPS_Q4_K_AMPERE;
  5004. } else if (compute_capability >= MIN_CC_DP4A) {
  5005. mmq_x = MMQ_X_Q4_K_PASCAL;
  5006. mmq_y = MMQ_Y_Q4_K_PASCAL;
  5007. nwarps = NWARPS_Q4_K_PASCAL;
  5008. } else {
  5009. GGML_ASSERT(false);
  5010. }
  5011. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5012. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5013. const dim3 block_nums(block_num_x, block_num_y, 1);
  5014. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5015. if (nrows_x % mmq_y == 0) {
  5016. const bool need_check = false;
  5017. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5018. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5019. } else {
  5020. const bool need_check = true;
  5021. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5022. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5023. }
  5024. }
  5025. static void ggml_mul_mat_q5_K_q8_1_cuda(
  5026. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5027. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5028. int id;
  5029. CUDA_CHECK(cudaGetDevice(&id));
  5030. const int compute_capability = g_device_caps[id].cc;
  5031. int mmq_x, mmq_y, nwarps;
  5032. if (compute_capability >= CC_RDNA2) {
  5033. mmq_x = MMQ_X_Q5_K_RDNA2;
  5034. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5035. nwarps = NWARPS_Q5_K_RDNA2;
  5036. } else if (compute_capability >= CC_OFFSET_AMD) {
  5037. mmq_x = MMQ_X_Q5_K_RDNA1;
  5038. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5039. nwarps = NWARPS_Q5_K_RDNA1;
  5040. } else if (compute_capability >= CC_VOLTA) {
  5041. mmq_x = MMQ_X_Q5_K_AMPERE;
  5042. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5043. nwarps = NWARPS_Q5_K_AMPERE;
  5044. } else if (compute_capability >= MIN_CC_DP4A) {
  5045. mmq_x = MMQ_X_Q5_K_PASCAL;
  5046. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5047. nwarps = NWARPS_Q5_K_PASCAL;
  5048. } else {
  5049. GGML_ASSERT(false);
  5050. }
  5051. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5052. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5053. const dim3 block_nums(block_num_x, block_num_y, 1);
  5054. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5055. if (nrows_x % mmq_y == 0) {
  5056. const bool need_check = false;
  5057. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5058. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5059. } else {
  5060. const bool need_check = true;
  5061. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5062. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5063. }
  5064. }
  5065. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5066. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5067. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5068. int id;
  5069. CUDA_CHECK(cudaGetDevice(&id));
  5070. const int compute_capability = g_device_caps[id].cc;
  5071. int mmq_x, mmq_y, nwarps;
  5072. if (compute_capability >= CC_RDNA2) {
  5073. mmq_x = MMQ_X_Q6_K_RDNA2;
  5074. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5075. nwarps = NWARPS_Q6_K_RDNA2;
  5076. } else if (compute_capability >= CC_OFFSET_AMD) {
  5077. mmq_x = MMQ_X_Q6_K_RDNA1;
  5078. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5079. nwarps = NWARPS_Q6_K_RDNA1;
  5080. } else if (compute_capability >= CC_VOLTA) {
  5081. mmq_x = MMQ_X_Q6_K_AMPERE;
  5082. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5083. nwarps = NWARPS_Q6_K_AMPERE;
  5084. } else if (compute_capability >= MIN_CC_DP4A) {
  5085. mmq_x = MMQ_X_Q6_K_PASCAL;
  5086. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5087. nwarps = NWARPS_Q6_K_PASCAL;
  5088. } else {
  5089. GGML_ASSERT(false);
  5090. }
  5091. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5092. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5093. const dim3 block_nums(block_num_x, block_num_y, 1);
  5094. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5095. if (nrows_x % mmq_y == 0) {
  5096. const bool need_check = false;
  5097. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5098. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5099. } else {
  5100. const bool need_check = true;
  5101. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5102. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5103. }
  5104. }
  5105. static void ggml_mul_mat_p021_f16_f32_cuda(
  5106. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5107. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5108. const dim3 block_nums(1, nrows_x, nchannels_y);
  5109. const dim3 block_dims(WARP_SIZE, 1, 1);
  5110. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5111. }
  5112. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5113. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5114. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5115. const dim3 block_nums(1, nrows_x, nchannels_y);
  5116. const dim3 block_dims(WARP_SIZE, 1, 1);
  5117. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5118. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5119. }
  5120. static void ggml_cpy_f32_f32_cuda(
  5121. const char * cx, char * cdst, const int ne,
  5122. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5123. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5124. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5125. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5126. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5127. }
  5128. static void ggml_cpy_f32_f16_cuda(
  5129. const char * cx, char * cdst, const int ne,
  5130. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5131. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5132. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5133. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5134. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5135. }
  5136. static void ggml_cpy_f32_q8_0_cuda(
  5137. const char * cx, char * cdst, const int ne,
  5138. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5139. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5140. GGML_ASSERT(ne % QK8_0 == 0);
  5141. const int num_blocks = ne / QK8_0;
  5142. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5143. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5144. }
  5145. static void ggml_cpy_f32_q4_0_cuda(
  5146. const char * cx, char * cdst, const int ne,
  5147. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5148. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5149. GGML_ASSERT(ne % QK4_0 == 0);
  5150. const int num_blocks = ne / QK4_0;
  5151. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5152. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5153. }
  5154. static void ggml_cpy_f32_q4_1_cuda(
  5155. const char * cx, char * cdst, const int ne,
  5156. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5157. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5158. GGML_ASSERT(ne % QK4_1 == 0);
  5159. const int num_blocks = ne / QK4_1;
  5160. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5161. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5162. }
  5163. static void ggml_cpy_f16_f16_cuda(
  5164. const char * cx, char * cdst, const int ne,
  5165. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5166. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5167. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5168. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5169. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5170. }
  5171. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5172. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5173. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5174. }
  5175. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5176. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5177. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5178. }
  5179. template<typename T>
  5180. static void rope_cuda(
  5181. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5182. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5183. ) {
  5184. GGML_ASSERT(ncols % 2 == 0);
  5185. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5186. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5187. const dim3 block_nums(nrows, num_blocks_x, 1);
  5188. if (pos == nullptr) {
  5189. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5190. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5191. );
  5192. } else {
  5193. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5194. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5195. );
  5196. }
  5197. }
  5198. template<typename T>
  5199. static void rope_neox_cuda(
  5200. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5201. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5202. ) {
  5203. GGML_ASSERT(ncols % 2 == 0);
  5204. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5205. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5206. const dim3 block_nums(nrows, num_blocks_x, 1);
  5207. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5208. const float inv_ndims = -1.0f / n_dims;
  5209. if (pos == nullptr) {
  5210. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5211. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5212. theta_scale, inv_ndims
  5213. );
  5214. } else {
  5215. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5216. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5217. theta_scale, inv_ndims
  5218. );
  5219. }
  5220. }
  5221. static void rope_glm_f32_cuda(
  5222. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5223. float freq_base, int n_ctx, cudaStream_t stream
  5224. ) {
  5225. GGML_ASSERT(ncols % 4 == 0);
  5226. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5227. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5228. const dim3 block_nums(num_blocks_x, nrows, 1);
  5229. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5230. }
  5231. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5232. const int k_rows, const int n_heads_log2_floor, const float m0,
  5233. const float m1, cudaStream_t stream) {
  5234. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5235. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5236. const dim3 block_nums(num_blocks_x, nrows, 1);
  5237. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5238. }
  5239. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5240. const dim3 block_dims(WARP_SIZE, 1, 1);
  5241. const dim3 block_nums(1, nrows, 1);
  5242. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5243. }
  5244. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5245. // bitonic sort requires ncols to be power of 2
  5246. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5247. const dim3 block_dims(ncols, 1, 1);
  5248. const dim3 block_nums(1, nrows, 1);
  5249. if (order == GGML_SORT_ASC) {
  5250. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5251. } else if (order == GGML_SORT_DESC) {
  5252. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5253. } else {
  5254. GGML_ASSERT(false);
  5255. }
  5256. }
  5257. 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) {
  5258. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5259. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5260. const dim3 block_nums(nrows_x, block_num_x, 1);
  5261. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5262. }
  5263. 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) {
  5264. int nth = WARP_SIZE;
  5265. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5266. const dim3 block_dims(nth, 1, 1);
  5267. const dim3 block_nums(nrows_x, 1, 1);
  5268. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5269. }
  5270. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5271. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5272. int offset_delta,
  5273. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5274. const int parallel_elements = OW * KW * KH;
  5275. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5276. dim3 block_nums(num_blocks, OH, IC);
  5277. 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);
  5278. }
  5279. // buffer pool for cuda
  5280. #define MAX_CUDA_BUFFERS 256
  5281. struct scoped_spin_lock {
  5282. std::atomic_flag& lock;
  5283. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5284. while (lock.test_and_set(std::memory_order_acquire)) {
  5285. ; // spin
  5286. }
  5287. }
  5288. ~scoped_spin_lock() {
  5289. lock.clear(std::memory_order_release);
  5290. }
  5291. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5292. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5293. };
  5294. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5295. // #define DEBUG_CUDA_MALLOC
  5296. struct ggml_cuda_buffer {
  5297. void * ptr = nullptr;
  5298. size_t size = 0;
  5299. };
  5300. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5301. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  5302. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  5303. scoped_spin_lock lock(g_cuda_pool_lock);
  5304. #ifdef DEBUG_CUDA_MALLOC
  5305. int nnz = 0;
  5306. size_t max_size = 0;
  5307. #endif
  5308. size_t best_diff = 1ull << 36;
  5309. int ibest = -1;
  5310. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5311. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5312. if (b.ptr != nullptr) {
  5313. #ifdef DEBUG_CUDA_MALLOC
  5314. ++nnz;
  5315. if (b.size > max_size) max_size = b.size;
  5316. #endif
  5317. if (b.size >= size) {
  5318. size_t diff = b.size - size;
  5319. if (diff < best_diff) {
  5320. best_diff = diff;
  5321. ibest = i;
  5322. if (!best_diff) {
  5323. void * ptr = b.ptr;
  5324. *actual_size = b.size;
  5325. b.ptr = nullptr;
  5326. b.size = 0;
  5327. return ptr;
  5328. }
  5329. }
  5330. }
  5331. }
  5332. }
  5333. if (ibest >= 0) {
  5334. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  5335. void * ptr = b.ptr;
  5336. *actual_size = b.size;
  5337. b.ptr = nullptr;
  5338. b.size = 0;
  5339. return ptr;
  5340. }
  5341. void * ptr;
  5342. size_t look_ahead_size = (size_t) (1.05 * size);
  5343. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5344. ggml_cuda_set_device(device);
  5345. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5346. *actual_size = look_ahead_size;
  5347. g_cuda_pool_size[device] += look_ahead_size;
  5348. #ifdef DEBUG_CUDA_MALLOC
  5349. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  5350. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  5351. #endif
  5352. return ptr;
  5353. }
  5354. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  5355. scoped_spin_lock lock(g_cuda_pool_lock);
  5356. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5357. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5358. if (b.ptr == nullptr) {
  5359. b.ptr = ptr;
  5360. b.size = size;
  5361. return;
  5362. }
  5363. }
  5364. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5365. ggml_cuda_set_device(device);
  5366. CUDA_CHECK(cudaFree(ptr));
  5367. g_cuda_pool_size[device] -= size;
  5368. }
  5369. #if !defined(GGML_USE_HIPBLAS)
  5370. // pool with virtual memory
  5371. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  5372. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  5373. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  5374. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  5375. scoped_spin_lock lock(g_cuda_pool_lock);
  5376. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  5377. const size_t alignment = 128;
  5378. size = alignment * ((size + alignment - 1) / alignment);
  5379. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  5380. if (size > avail) {
  5381. // round up to the next multiple of the granularity
  5382. size_t reserve_size = size - avail;
  5383. const size_t granularity = g_device_caps[device].vmm_granularity;
  5384. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  5385. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  5386. // allocate more physical memory
  5387. CUmemAllocationProp prop = {};
  5388. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5389. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5390. prop.location.id = device;
  5391. CUmemGenericAllocationHandle handle;
  5392. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  5393. // reserve virtual address space (if not already reserved)
  5394. if (g_cuda_pool_addr[device] == 0) {
  5395. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  5396. }
  5397. // map at the end of the pool
  5398. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  5399. // the memory allocation handle is no longer needed after mapping
  5400. CU_CHECK(cuMemRelease(handle));
  5401. // set access
  5402. CUmemAccessDesc access = {};
  5403. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5404. access.location.id = device;
  5405. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  5406. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  5407. // add to the pool
  5408. g_cuda_pool_size[device] += reserve_size;
  5409. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  5410. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  5411. // (unsigned long long) (reserve_size/1024/1024));
  5412. }
  5413. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  5414. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  5415. *actual_size = size;
  5416. g_cuda_pool_used[device] += size;
  5417. #ifdef DEBUG_CUDA_MALLOC
  5418. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  5419. #endif
  5420. return ptr;
  5421. }
  5422. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  5423. scoped_spin_lock lock(g_cuda_pool_lock);
  5424. #ifdef DEBUG_CUDA_MALLOC
  5425. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  5426. #endif
  5427. g_cuda_pool_used[device] -= size;
  5428. // all deallocations must be in reverse order of the allocations
  5429. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  5430. }
  5431. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  5432. if (g_device_caps[device].vmm) {
  5433. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  5434. } else {
  5435. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  5436. }
  5437. }
  5438. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  5439. if (g_device_caps[device].vmm) {
  5440. ggml_cuda_pool_free_vmm(device, ptr, size);
  5441. } else {
  5442. ggml_cuda_pool_free_leg(device, ptr, size);
  5443. }
  5444. }
  5445. #else
  5446. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  5447. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  5448. #endif // !defined(GGML_USE_HIPBLAS)
  5449. template<typename T>
  5450. struct cuda_pool_alloc {
  5451. int device = -1;
  5452. T * ptr = nullptr;
  5453. size_t actual_size = 0;
  5454. // size is in number of elements
  5455. T * alloc(size_t size) {
  5456. GGML_ASSERT(ptr == nullptr);
  5457. CUDA_CHECK(cudaGetDevice(&device));
  5458. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  5459. return ptr;
  5460. }
  5461. cuda_pool_alloc(size_t size) {
  5462. alloc(size);
  5463. }
  5464. ~cuda_pool_alloc() {
  5465. if (ptr != nullptr) {
  5466. ggml_cuda_pool_free(device, ptr, actual_size);
  5467. }
  5468. }
  5469. T * get() {
  5470. return ptr;
  5471. }
  5472. cuda_pool_alloc() = default;
  5473. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  5474. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  5475. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  5476. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  5477. };
  5478. static bool g_cublas_loaded = false;
  5479. bool ggml_cublas_loaded(void) {
  5480. return g_cublas_loaded;
  5481. }
  5482. void ggml_init_cublas() {
  5483. static bool initialized = false;
  5484. if (!initialized) {
  5485. #ifdef __HIP_PLATFORM_AMD__
  5486. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5487. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5488. rocblas_initialize();
  5489. CUDA_CHECK(cudaDeviceSynchronize());
  5490. #endif
  5491. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5492. initialized = true;
  5493. g_cublas_loaded = false;
  5494. return;
  5495. }
  5496. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5497. int64_t total_vram = 0;
  5498. #if defined(GGML_CUDA_FORCE_MMQ)
  5499. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5500. #else
  5501. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5502. #endif
  5503. #if defined(CUDA_USE_TENSOR_CORES)
  5504. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5505. #else
  5506. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5507. #endif
  5508. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5509. for (int id = 0; id < g_device_count; ++id) {
  5510. int device_vmm = 0;
  5511. #if !defined(GGML_USE_HIPBLAS)
  5512. CUdevice device;
  5513. CU_CHECK(cuDeviceGet(&device, id));
  5514. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  5515. if (device_vmm) {
  5516. CUmemAllocationProp alloc_prop = {};
  5517. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5518. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5519. alloc_prop.location.id = id;
  5520. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  5521. }
  5522. #endif // !defined(GGML_USE_HIPBLAS)
  5523. g_device_caps[id].vmm = !!device_vmm;
  5524. cudaDeviceProp prop;
  5525. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5526. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  5527. g_tensor_split[id] = total_vram;
  5528. total_vram += prop.totalGlobalMem;
  5529. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5530. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5531. #else
  5532. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  5533. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5534. }
  5535. for (int id = 0; id < g_device_count; ++id) {
  5536. g_tensor_split[id] /= total_vram;
  5537. }
  5538. for (int id = 0; id < g_device_count; ++id) {
  5539. ggml_cuda_set_device(id);
  5540. // create cuda streams
  5541. for (int is = 0; is < MAX_STREAMS; ++is) {
  5542. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5543. }
  5544. // create cublas handle
  5545. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5546. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5547. }
  5548. // configure logging to stdout
  5549. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5550. initialized = true;
  5551. g_cublas_loaded = true;
  5552. }
  5553. }
  5554. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5555. if (tensor_split == nullptr) {
  5556. return;
  5557. }
  5558. bool all_zero = true;
  5559. for (int i = 0; i < g_device_count; ++i) {
  5560. if (tensor_split[i] != 0.0f) {
  5561. all_zero = false;
  5562. break;
  5563. }
  5564. }
  5565. if (all_zero) {
  5566. return;
  5567. }
  5568. float split_sum = 0.0f;
  5569. for (int i = 0; i < g_device_count; ++i) {
  5570. g_tensor_split[i] = split_sum;
  5571. split_sum += tensor_split[i];
  5572. }
  5573. for (int i = 0; i < g_device_count; ++i) {
  5574. g_tensor_split[i] /= split_sum;
  5575. }
  5576. }
  5577. void * ggml_cuda_host_malloc(size_t size) {
  5578. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  5579. return nullptr;
  5580. }
  5581. void * ptr = nullptr;
  5582. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  5583. if (err != cudaSuccess) {
  5584. // clear the error
  5585. cudaGetLastError();
  5586. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  5587. size/1024.0/1024.0, cudaGetErrorString(err));
  5588. return nullptr;
  5589. }
  5590. return ptr;
  5591. }
  5592. void ggml_cuda_host_free(void * ptr) {
  5593. CUDA_CHECK(cudaFreeHost(ptr));
  5594. }
  5595. static cudaError_t ggml_cuda_cpy_tensor_2d(
  5596. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  5597. cudaMemcpyKind kind;
  5598. char * src_ptr;
  5599. if (src->backend == GGML_BACKEND_CPU) {
  5600. kind = cudaMemcpyHostToDevice;
  5601. src_ptr = (char *) src->data;
  5602. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  5603. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  5604. kind = cudaMemcpyDeviceToDevice;
  5605. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  5606. int id;
  5607. CUDA_CHECK(cudaGetDevice(&id));
  5608. src_ptr = (char *) extra->data_device[id];
  5609. } else {
  5610. GGML_ASSERT(false);
  5611. }
  5612. char * dst_ptr = (char *) dst;
  5613. const int64_t ne0 = src->ne[0];
  5614. const int64_t nb0 = src->nb[0];
  5615. const int64_t nb1 = src->nb[1];
  5616. const int64_t nb2 = src->nb[2];
  5617. const int64_t nb3 = src->nb[3];
  5618. const enum ggml_type type = src->type;
  5619. const int64_t ts = ggml_type_size(type);
  5620. const int64_t bs = ggml_blck_size(type);
  5621. int64_t i1_diff = i1_high - i1_low;
  5622. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  5623. if (nb0 == ts && nb1 == ts*ne0/bs) {
  5624. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  5625. } else if (nb0 == ts) {
  5626. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  5627. } else {
  5628. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  5629. const void * rx = (const void *) ((const char *) x + i1*nb1);
  5630. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  5631. // pretend the row is a matrix with cols=1
  5632. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  5633. if (r != cudaSuccess) return r;
  5634. }
  5635. return cudaSuccess;
  5636. }
  5637. }
  5638. static void ggml_cuda_op_get_rows(
  5639. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5640. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  5641. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5642. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5643. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  5644. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  5645. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  5646. const int32_t * src1_i32 = (const int32_t *) src1_d;
  5647. switch (src0->type) {
  5648. case GGML_TYPE_F16:
  5649. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  5650. break;
  5651. case GGML_TYPE_F32:
  5652. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5653. break;
  5654. case GGML_TYPE_Q4_0:
  5655. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5656. break;
  5657. case GGML_TYPE_Q4_1:
  5658. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5659. break;
  5660. case GGML_TYPE_Q5_0:
  5661. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5662. break;
  5663. case GGML_TYPE_Q5_1:
  5664. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5665. break;
  5666. case GGML_TYPE_Q8_0:
  5667. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5668. break;
  5669. default:
  5670. // TODO: k-quants
  5671. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  5672. GGML_ASSERT(false);
  5673. break;
  5674. }
  5675. }
  5676. template<class op>
  5677. static void ggml_cuda_op_bin_bcast(
  5678. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5679. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5680. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5681. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5682. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5683. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5684. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  5685. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5686. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  5687. } else {
  5688. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  5689. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  5690. GGML_ASSERT(false);
  5691. }
  5692. }
  5693. static void ggml_cuda_op_repeat(
  5694. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5695. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  5696. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  5697. (void) src1;
  5698. (void) src1_d;
  5699. }
  5700. static void ggml_cuda_op_add(
  5701. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5702. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5703. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5704. }
  5705. static void ggml_cuda_op_acc(
  5706. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5707. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5708. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5709. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5710. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5711. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  5712. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5713. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5714. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5715. int offset = dst->op_params[3] / 4; // offset in bytes
  5716. 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);
  5717. (void) dst;
  5718. }
  5719. static void ggml_cuda_op_mul(
  5720. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5721. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5722. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5723. }
  5724. static void ggml_cuda_op_div(
  5725. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5726. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5727. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5728. }
  5729. static void ggml_cuda_op_gelu(
  5730. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5731. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5732. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5733. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5734. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5735. (void) src1;
  5736. (void) dst;
  5737. (void) src1_dd;
  5738. }
  5739. static void ggml_cuda_op_silu(
  5740. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5741. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5742. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5743. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5744. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5745. (void) src1;
  5746. (void) dst;
  5747. (void) src1_dd;
  5748. }
  5749. static void ggml_cuda_op_gelu_quick(
  5750. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5751. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5752. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5753. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5754. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5755. (void) src1;
  5756. (void) dst;
  5757. (void) src1_dd;
  5758. }
  5759. static void ggml_cuda_op_tanh(
  5760. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5761. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5762. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5763. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5764. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5765. (void) src1;
  5766. (void) dst;
  5767. (void) src1_dd;
  5768. }
  5769. static void ggml_cuda_op_relu(
  5770. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5771. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5772. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5773. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5774. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5775. (void) src1;
  5776. (void) dst;
  5777. (void) src1_dd;
  5778. }
  5779. static void ggml_cuda_op_leaky_relu(
  5780. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5781. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5782. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5783. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5784. float negative_slope;
  5785. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5786. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  5787. (void) src1;
  5788. (void) dst;
  5789. (void) src1_dd;
  5790. }
  5791. static void ggml_cuda_op_sqr(
  5792. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5793. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5794. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5795. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5796. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5797. (void) src1;
  5798. (void) dst;
  5799. (void) src1_dd;
  5800. }
  5801. static void ggml_cuda_op_norm(
  5802. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5803. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5804. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5805. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5806. const int64_t ne00 = src0->ne[0];
  5807. const int64_t nrows = ggml_nrows(src0);
  5808. float eps;
  5809. memcpy(&eps, dst->op_params, sizeof(float));
  5810. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5811. (void) src1;
  5812. (void) dst;
  5813. (void) src1_dd;
  5814. }
  5815. static void ggml_cuda_op_group_norm(
  5816. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5817. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5818. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5819. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5820. int num_groups = dst->op_params[0];
  5821. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5822. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  5823. (void) src1;
  5824. (void) dst;
  5825. (void) src1_dd;
  5826. }
  5827. static void ggml_cuda_op_concat(
  5828. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5829. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5830. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5831. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5832. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5833. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  5834. 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);
  5835. }
  5836. (void) src1;
  5837. (void) dst;
  5838. }
  5839. static void ggml_cuda_op_upscale(
  5840. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5841. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5842. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5843. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5844. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5845. const int scale_factor = dst->op_params[0];
  5846. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  5847. (void) src1;
  5848. (void) dst;
  5849. (void) src1_dd;
  5850. }
  5851. static void ggml_cuda_op_pad(
  5852. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5853. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5854. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5855. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5856. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5857. pad_f32_cuda(src0_dd, dst_dd,
  5858. src0->ne[0], src0->ne[1], src0->ne[2],
  5859. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  5860. (void) src1;
  5861. (void) dst;
  5862. (void) src1_dd;
  5863. }
  5864. static void ggml_cuda_op_rms_norm(
  5865. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5866. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5867. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5868. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5869. const int64_t ne00 = src0->ne[0];
  5870. const int64_t nrows = ggml_nrows(src0);
  5871. float eps;
  5872. memcpy(&eps, dst->op_params, sizeof(float));
  5873. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5874. (void) src1;
  5875. (void) dst;
  5876. (void) src1_dd;
  5877. }
  5878. static void ggml_cuda_op_mul_mat_q(
  5879. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5880. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5881. const int64_t src1_padded_row_size, cudaStream_t stream) {
  5882. const int64_t ne00 = src0->ne[0];
  5883. const int64_t ne10 = src1->ne[0];
  5884. GGML_ASSERT(ne10 % QK8_1 == 0);
  5885. const int64_t ne0 = dst->ne[0];
  5886. const int64_t row_diff = row_high - row_low;
  5887. int id;
  5888. CUDA_CHECK(cudaGetDevice(&id));
  5889. // the main device has a larger memory buffer to hold the results from all GPUs
  5890. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5891. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5892. switch (src0->type) {
  5893. case GGML_TYPE_Q4_0:
  5894. 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);
  5895. break;
  5896. case GGML_TYPE_Q4_1:
  5897. 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);
  5898. break;
  5899. case GGML_TYPE_Q5_0:
  5900. 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);
  5901. break;
  5902. case GGML_TYPE_Q5_1:
  5903. 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);
  5904. break;
  5905. case GGML_TYPE_Q8_0:
  5906. 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);
  5907. break;
  5908. case GGML_TYPE_Q2_K:
  5909. 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);
  5910. break;
  5911. case GGML_TYPE_Q3_K:
  5912. 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);
  5913. break;
  5914. case GGML_TYPE_Q4_K:
  5915. 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);
  5916. break;
  5917. case GGML_TYPE_Q5_K:
  5918. 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);
  5919. break;
  5920. case GGML_TYPE_Q6_K:
  5921. 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);
  5922. break;
  5923. default:
  5924. GGML_ASSERT(false);
  5925. break;
  5926. }
  5927. (void) src1;
  5928. (void) dst;
  5929. (void) src1_ddf_i;
  5930. }
  5931. static int64_t get_row_rounding(ggml_type type) {
  5932. int64_t min_compute_capability = INT_MAX;
  5933. int64_t max_compute_capability = INT_MIN;
  5934. for (int id = 0; id < g_device_count; ++id) {
  5935. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5936. if (min_compute_capability > g_device_caps[id].cc) {
  5937. min_compute_capability = g_device_caps[id].cc;
  5938. }
  5939. if (max_compute_capability < g_device_caps[id].cc) {
  5940. max_compute_capability = g_device_caps[id].cc;
  5941. }
  5942. }
  5943. }
  5944. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5945. switch(type) {
  5946. case GGML_TYPE_Q4_0:
  5947. case GGML_TYPE_Q4_1:
  5948. case GGML_TYPE_Q5_0:
  5949. case GGML_TYPE_Q5_1:
  5950. case GGML_TYPE_Q8_0:
  5951. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5952. case GGML_TYPE_F16:
  5953. case GGML_TYPE_F32:
  5954. return 1;
  5955. case GGML_TYPE_Q2_K:
  5956. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5957. case GGML_TYPE_Q3_K:
  5958. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5959. case GGML_TYPE_Q4_K:
  5960. case GGML_TYPE_Q5_K:
  5961. case GGML_TYPE_Q6_K:
  5962. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5963. default:
  5964. GGML_ASSERT(false);
  5965. }
  5966. #else
  5967. switch(type) {
  5968. case GGML_TYPE_Q4_0:
  5969. case GGML_TYPE_Q4_1:
  5970. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5971. case GGML_TYPE_Q5_0:
  5972. case GGML_TYPE_Q5_1:
  5973. case GGML_TYPE_Q8_0:
  5974. return 64;
  5975. case GGML_TYPE_F16:
  5976. case GGML_TYPE_F32:
  5977. return 1;
  5978. case GGML_TYPE_Q2_K:
  5979. case GGML_TYPE_Q3_K:
  5980. case GGML_TYPE_Q4_K:
  5981. case GGML_TYPE_Q5_K:
  5982. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5983. case GGML_TYPE_Q6_K:
  5984. return 64;
  5985. default:
  5986. GGML_ASSERT(false);
  5987. }
  5988. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5989. }
  5990. static void ggml_cuda_op_mul_mat_vec_q(
  5991. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5992. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5993. const int64_t src1_padded_row_size, cudaStream_t stream) {
  5994. GGML_ASSERT(ggml_nrows(src1) == 1);
  5995. const int64_t ne00 = src0->ne[0];
  5996. const int64_t row_diff = row_high - row_low;
  5997. switch (src0->type) {
  5998. case GGML_TYPE_Q4_0:
  5999. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6000. break;
  6001. case GGML_TYPE_Q4_1:
  6002. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6003. break;
  6004. case GGML_TYPE_Q5_0:
  6005. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6006. break;
  6007. case GGML_TYPE_Q5_1:
  6008. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6009. break;
  6010. case GGML_TYPE_Q8_0:
  6011. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6012. break;
  6013. case GGML_TYPE_Q2_K:
  6014. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6015. break;
  6016. case GGML_TYPE_Q3_K:
  6017. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6018. break;
  6019. case GGML_TYPE_Q4_K:
  6020. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6021. break;
  6022. case GGML_TYPE_Q5_K:
  6023. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6024. break;
  6025. case GGML_TYPE_Q6_K:
  6026. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6027. break;
  6028. default:
  6029. GGML_ASSERT(false);
  6030. break;
  6031. }
  6032. (void) src1;
  6033. (void) dst;
  6034. (void) src1_ddf_i;
  6035. (void) src1_ncols;
  6036. (void) src1_padded_row_size;
  6037. }
  6038. static void ggml_cuda_op_dequantize_mul_mat_vec(
  6039. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6040. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6041. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6042. const int64_t ne00 = src0->ne[0];
  6043. const int64_t row_diff = row_high - row_low;
  6044. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6045. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  6046. #ifdef GGML_CUDA_F16
  6047. cuda_pool_alloc<half> src1_dfloat_a;
  6048. half * src1_dfloat = nullptr; // dfloat == half
  6049. bool src1_convert_f16 =
  6050. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  6051. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  6052. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  6053. if (src1_convert_f16) {
  6054. src1_dfloat = src1_dfloat_a.alloc(ne00);
  6055. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  6056. ne00, 1, sizeof(float), 0, 0,
  6057. ne00, 1, sizeof(half), 0, 0, stream);
  6058. }
  6059. #else
  6060. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  6061. #endif // GGML_CUDA_F16
  6062. switch (src0->type) {
  6063. case GGML_TYPE_Q4_0:
  6064. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6065. break;
  6066. case GGML_TYPE_Q4_1:
  6067. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6068. break;
  6069. case GGML_TYPE_Q5_0:
  6070. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6071. break;
  6072. case GGML_TYPE_Q5_1:
  6073. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6074. break;
  6075. case GGML_TYPE_Q8_0:
  6076. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6077. break;
  6078. case GGML_TYPE_Q2_K:
  6079. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6080. break;
  6081. case GGML_TYPE_Q3_K:
  6082. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6083. break;
  6084. case GGML_TYPE_Q4_K:
  6085. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6086. break;
  6087. case GGML_TYPE_Q5_K:
  6088. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6089. break;
  6090. case GGML_TYPE_Q6_K:
  6091. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6092. break;
  6093. case GGML_TYPE_F16:
  6094. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6095. break;
  6096. default:
  6097. GGML_ASSERT(false);
  6098. break;
  6099. }
  6100. (void) src1;
  6101. (void) dst;
  6102. (void) src1_ddq_i;
  6103. (void) src1_ncols;
  6104. (void) src1_padded_row_size;
  6105. }
  6106. static void ggml_cuda_op_mul_mat_cublas(
  6107. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6108. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6109. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6110. GGML_ASSERT(src0_dd_i != nullptr);
  6111. GGML_ASSERT(src1_ddf_i != nullptr);
  6112. GGML_ASSERT(dst_dd_i != nullptr);
  6113. const int64_t ne00 = src0->ne[0];
  6114. const int64_t ne10 = src1->ne[0];
  6115. const int64_t ne0 = dst->ne[0];
  6116. const int64_t row_diff = row_high - row_low;
  6117. int id;
  6118. CUDA_CHECK(cudaGetDevice(&id));
  6119. // the main device has a larger memory buffer to hold the results from all GPUs
  6120. // ldc == nrows of the matrix that cuBLAS writes into
  6121. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6122. const int compute_capability = g_device_caps[id].cc;
  6123. 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) {
  6124. //printf("this branch\n");
  6125. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  6126. cuda_pool_alloc<half> src0_as_f16;
  6127. if (src0->type != GGML_TYPE_F16) {
  6128. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  6129. GGML_ASSERT(to_fp16_cuda != nullptr);
  6130. size_t ne = row_diff*ne00;
  6131. src0_as_f16.alloc(ne);
  6132. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  6133. }
  6134. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  6135. cuda_pool_alloc<half> src1_as_f16;
  6136. if (src1->type != GGML_TYPE_F16) {
  6137. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6138. GGML_ASSERT(to_fp16_cuda != nullptr);
  6139. size_t ne = src1_ncols*ne10;
  6140. src1_as_f16.alloc(ne);
  6141. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  6142. }
  6143. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  6144. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  6145. const half alpha_f16 = 1.0f;
  6146. const half beta_f16 = 0.0f;
  6147. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6148. CUBLAS_CHECK(
  6149. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6150. row_diff, src1_ncols, ne10,
  6151. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6152. src1_ptr, CUDA_R_16F, ne10,
  6153. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  6154. CUBLAS_COMPUTE_16F,
  6155. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6156. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6157. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  6158. } else {
  6159. cuda_pool_alloc<float> src0_ddq_as_f32;
  6160. cuda_pool_alloc<float> src1_ddq_as_f32;
  6161. if (src0->type != GGML_TYPE_F32) {
  6162. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6163. GGML_ASSERT(to_fp32_cuda != nullptr);
  6164. src0_ddq_as_f32.alloc(row_diff*ne00);
  6165. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  6166. }
  6167. if (src1->type != GGML_TYPE_F32) {
  6168. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  6169. GGML_ASSERT(to_fp32_cuda != nullptr);
  6170. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  6171. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  6172. }
  6173. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  6174. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  6175. const float alpha = 1.0f;
  6176. const float beta = 0.0f;
  6177. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6178. CUBLAS_CHECK(
  6179. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6180. row_diff, src1_ncols, ne10,
  6181. &alpha, src0_ddf_i, ne00,
  6182. src1_ddf1_i, ne10,
  6183. &beta, dst_dd_i, ldc));
  6184. }
  6185. (void) dst;
  6186. (void) src1_ddq_i;
  6187. (void) src1_padded_row_size;
  6188. }
  6189. static void ggml_cuda_op_rope(
  6190. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6191. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6192. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6193. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6194. GGML_ASSERT(src0->type == dst->type);
  6195. const int64_t ne00 = src0->ne[0];
  6196. const int64_t ne01 = src0->ne[1];
  6197. const int64_t ne2 = dst->ne[2];
  6198. const int64_t nrows = ggml_nrows(src0);
  6199. //const int n_past = ((int32_t *) dst->op_params)[0];
  6200. const int n_dims = ((int32_t *) dst->op_params)[1];
  6201. const int mode = ((int32_t *) dst->op_params)[2];
  6202. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6203. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6204. // RoPE alteration for extended context
  6205. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6206. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6207. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6208. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6209. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6210. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6211. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6212. const int32_t * pos = nullptr;
  6213. if ((mode & 1) == 0) {
  6214. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6215. GGML_ASSERT(src1->ne[0] == ne2);
  6216. pos = (const int32_t *) src1_dd;
  6217. }
  6218. const bool is_neox = mode & 2;
  6219. const bool is_glm = mode & 4;
  6220. rope_corr_dims corr_dims;
  6221. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6222. // compute
  6223. if (is_glm) {
  6224. GGML_ASSERT(false);
  6225. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6226. } else if (is_neox) {
  6227. if (src0->type == GGML_TYPE_F32) {
  6228. rope_neox_cuda(
  6229. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6230. attn_factor, corr_dims, main_stream
  6231. );
  6232. } else if (src0->type == GGML_TYPE_F16) {
  6233. rope_neox_cuda(
  6234. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6235. attn_factor, corr_dims, main_stream
  6236. );
  6237. } else {
  6238. GGML_ASSERT(false);
  6239. }
  6240. } else {
  6241. if (src0->type == GGML_TYPE_F32) {
  6242. rope_cuda(
  6243. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6244. attn_factor, corr_dims, main_stream
  6245. );
  6246. } else if (src0->type == GGML_TYPE_F16) {
  6247. rope_cuda(
  6248. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6249. attn_factor, corr_dims, main_stream
  6250. );
  6251. } else {
  6252. GGML_ASSERT(false);
  6253. }
  6254. }
  6255. (void) src1;
  6256. (void) dst;
  6257. (void) src1_dd;
  6258. }
  6259. static void ggml_cuda_op_alibi(
  6260. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6261. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6262. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6263. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6264. const int64_t ne00 = src0->ne[0];
  6265. const int64_t ne01 = src0->ne[1];
  6266. const int64_t ne02 = src0->ne[2];
  6267. const int64_t nrows = ggml_nrows(src0);
  6268. //const int n_past = ((int32_t *) dst->op_params)[0];
  6269. const int n_head = ((int32_t *) dst->op_params)[1];
  6270. float max_bias;
  6271. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6272. //GGML_ASSERT(ne01 + n_past == ne00);
  6273. GGML_ASSERT(n_head == ne02);
  6274. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6275. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6276. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6277. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6278. (void) src1;
  6279. (void) src1_dd;
  6280. }
  6281. static void ggml_cuda_op_im2col(
  6282. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6283. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6284. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6285. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6286. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6287. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6288. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6289. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6290. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6291. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6292. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6293. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6294. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6295. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6296. const int64_t IW = src1->ne[0];
  6297. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6298. const int64_t KW = src0->ne[0];
  6299. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6300. const int64_t OW = dst->ne[1];
  6301. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6302. 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);
  6303. (void) src0;
  6304. (void) src0_dd;
  6305. }
  6306. static void ggml_cuda_op_sum_rows(
  6307. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6308. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6309. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6310. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6311. const int64_t ncols = src0->ne[0];
  6312. const int64_t nrows = ggml_nrows(src0);
  6313. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6314. (void) src1;
  6315. (void) dst;
  6316. (void) src1_dd;
  6317. }
  6318. static void ggml_cuda_op_argsort(
  6319. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6320. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6321. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6322. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6323. const int64_t ncols = src0->ne[0];
  6324. const int64_t nrows = ggml_nrows(src0);
  6325. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6326. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6327. (void) src1;
  6328. (void) dst;
  6329. (void) src1_dd;
  6330. }
  6331. static void ggml_cuda_op_diag_mask_inf(
  6332. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6333. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6334. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6335. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6336. const int64_t ne00 = src0->ne[0];
  6337. const int64_t ne01 = src0->ne[1];
  6338. const int nrows0 = ggml_nrows(src0);
  6339. const int n_past = ((int32_t *) dst->op_params)[0];
  6340. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6341. (void) src1;
  6342. (void) dst;
  6343. (void) src1_dd;
  6344. }
  6345. static void ggml_cuda_op_soft_max(
  6346. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6347. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6348. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6349. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6350. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6351. const int64_t ne00 = src0->ne[0];
  6352. const int64_t nrows_x = ggml_nrows(src0);
  6353. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6354. float scale = 1.0f;
  6355. memcpy(&scale, dst->op_params, sizeof(float));
  6356. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6357. (void) dst;
  6358. }
  6359. static void ggml_cuda_op_scale(
  6360. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6361. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6362. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6363. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6364. float scale;
  6365. memcpy(&scale, dst->op_params, sizeof(float));
  6366. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6367. CUDA_CHECK(cudaGetLastError());
  6368. (void) src1;
  6369. (void) dst;
  6370. (void) src1_dd;
  6371. }
  6372. static void ggml_cuda_op_clamp(
  6373. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6374. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6375. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6376. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6377. float min;
  6378. float max;
  6379. memcpy(&min, dst->op_params, sizeof(float));
  6380. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6381. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6382. CUDA_CHECK(cudaGetLastError());
  6383. (void) src1;
  6384. (void) dst;
  6385. (void) src1_dd;
  6386. }
  6387. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6388. const int64_t nrows0 = ggml_nrows(src0);
  6389. const bool use_src1 = src1 != nullptr;
  6390. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6391. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6392. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6393. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6394. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6395. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6396. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6397. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6398. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6399. // dd = data device
  6400. float * src0_ddf = nullptr;
  6401. float * src1_ddf = nullptr;
  6402. float * dst_ddf = nullptr;
  6403. cuda_pool_alloc<float> src0_f;
  6404. cuda_pool_alloc<float> src1_f;
  6405. cuda_pool_alloc<float> dst_f;
  6406. ggml_cuda_set_device(g_main_device);
  6407. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6408. if (src0_on_device) {
  6409. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6410. } else {
  6411. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  6412. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6413. }
  6414. if (use_src1) {
  6415. if (src1_on_device) {
  6416. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6417. } else {
  6418. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  6419. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6420. }
  6421. }
  6422. if (dst_on_device) {
  6423. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6424. } else {
  6425. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  6426. }
  6427. // do the computation
  6428. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6429. CUDA_CHECK(cudaGetLastError());
  6430. // copy dst to host if necessary
  6431. if (!dst_on_device) {
  6432. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6433. }
  6434. if (dst->backend == GGML_BACKEND_CPU) {
  6435. CUDA_CHECK(cudaDeviceSynchronize());
  6436. }
  6437. }
  6438. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6439. static bool peer_access_enabled = false;
  6440. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6441. if (peer_access_enabled == enable_peer_access) {
  6442. return;
  6443. }
  6444. #ifdef NDEBUG
  6445. for (int id = 0; id < g_device_count; ++id) {
  6446. ggml_cuda_set_device(id);
  6447. CUDA_CHECK(cudaDeviceSynchronize());
  6448. }
  6449. for (int id = 0; id < g_device_count; ++id) {
  6450. ggml_cuda_set_device(id);
  6451. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6452. if (id == id_other) {
  6453. continue;
  6454. }
  6455. if (id != g_main_device && id_other != g_main_device) {
  6456. continue;
  6457. }
  6458. int can_access_peer;
  6459. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6460. if (can_access_peer) {
  6461. if (enable_peer_access) {
  6462. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6463. } else {
  6464. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6465. }
  6466. }
  6467. }
  6468. }
  6469. #endif // NDEBUG
  6470. peer_access_enabled = enable_peer_access;
  6471. }
  6472. static void ggml_cuda_op_mul_mat(
  6473. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6474. const bool convert_src1_to_q8_1) {
  6475. const int64_t ne00 = src0->ne[0];
  6476. const int64_t ne01 = src0->ne[1];
  6477. const int64_t ne02 = src0->ne[2];
  6478. const int64_t ne03 = src0->ne[3];
  6479. const int64_t ne10 = src1->ne[0];
  6480. const int64_t ne11 = src1->ne[1];
  6481. const int64_t ne12 = src1->ne[2];
  6482. const int64_t ne13 = src1->ne[3];
  6483. const int64_t nrows1 = ggml_nrows(src1);
  6484. GGML_ASSERT(ne03 == ne13);
  6485. const int64_t ne0 = dst->ne[0];
  6486. const int64_t ne1 = dst->ne[1];
  6487. const int nb2 = dst->nb[2];
  6488. const int nb3 = dst->nb[3];
  6489. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6490. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6491. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  6492. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6493. const int64_t i02_divisor = ne12 / ne02;
  6494. const size_t src0_ts = ggml_type_size(src0->type);
  6495. const size_t src0_bs = ggml_blck_size(src0->type);
  6496. const size_t q8_1_ts = sizeof(block_q8_1);
  6497. const size_t q8_1_bs = QK8_1;
  6498. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6499. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6500. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6501. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6502. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6503. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6504. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6505. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6506. GGML_ASSERT(!(split && ne02 > 1));
  6507. GGML_ASSERT(!(split && ne03 > 1));
  6508. GGML_ASSERT(!(split && ne02 < ne12));
  6509. struct dev_data {
  6510. cuda_pool_alloc<char> src0_dd_alloc;
  6511. cuda_pool_alloc<float> src1_ddf_alloc;
  6512. cuda_pool_alloc<char> src1_ddq_alloc;
  6513. cuda_pool_alloc<float> dst_dd_alloc;
  6514. char * src0_dd = nullptr;
  6515. float * src1_ddf = nullptr; // float
  6516. char * src1_ddq = nullptr; // q8_1
  6517. float * dst_dd = nullptr;
  6518. int64_t row_low;
  6519. int64_t row_high;
  6520. };
  6521. dev_data dev[GGML_CUDA_MAX_DEVICES];
  6522. int used_devices = 0;
  6523. for (int id = 0; id < g_device_count; ++id) {
  6524. // by default, use all rows
  6525. dev[id].row_low = 0;
  6526. dev[id].row_high = ne01;
  6527. // for multi GPU, get the row boundaries from tensor split
  6528. // and round to mul_mat_q tile sizes
  6529. if (split) {
  6530. const int64_t rounding = get_row_rounding(src0->type);
  6531. if (id != 0) {
  6532. dev[id].row_low = ne01*g_tensor_split[id];
  6533. if (dev[id].row_low < ne01) {
  6534. dev[id].row_low -= dev[id].row_low % rounding;
  6535. }
  6536. }
  6537. if (id != g_device_count - 1) {
  6538. dev[id].row_high = ne01*g_tensor_split[id + 1];
  6539. if (dev[id].row_high < ne01) {
  6540. dev[id].row_high -= dev[id].row_high % rounding;
  6541. }
  6542. }
  6543. }
  6544. }
  6545. for (int id = 0; id < g_device_count; ++id) {
  6546. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  6547. continue;
  6548. }
  6549. used_devices++;
  6550. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6551. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6552. ggml_cuda_set_device(id);
  6553. cudaStream_t stream = g_cudaStreams[id][0];
  6554. if (src0_on_device && src0_is_contiguous) {
  6555. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  6556. } else {
  6557. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  6558. }
  6559. if (src1_on_device && src1_is_contiguous) {
  6560. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  6561. } else {
  6562. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  6563. }
  6564. if (convert_src1_to_q8_1) {
  6565. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  6566. if (src1_on_device && src1_is_contiguous) {
  6567. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  6568. CUDA_CHECK(cudaGetLastError());
  6569. }
  6570. }
  6571. if (dst_on_device) {
  6572. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  6573. } else {
  6574. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  6575. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  6576. }
  6577. }
  6578. // if multiple devices are used they need to wait for the main device
  6579. // here an event is recorded that signals that the main device has finished calculating the input data
  6580. if (split && used_devices > 1) {
  6581. ggml_cuda_set_device(g_main_device);
  6582. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  6583. }
  6584. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  6585. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  6586. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  6587. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  6588. for (int id = 0; id < g_device_count; ++id) {
  6589. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  6590. continue;
  6591. }
  6592. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6593. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6594. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  6595. ggml_cuda_set_device(id);
  6596. cudaStream_t stream = g_cudaStreams[id][is];
  6597. // wait for main GPU data if necessary
  6598. if (split && (id != g_main_device || is != 0)) {
  6599. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  6600. }
  6601. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  6602. const int64_t i03 = i0 / ne12;
  6603. const int64_t i02 = i0 % ne12;
  6604. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  6605. // for split tensors the data begins at i0 == i0_offset_low
  6606. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  6607. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  6608. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  6609. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  6610. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  6611. // in that case an offset on dst_ddf_i is needed
  6612. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  6613. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  6614. }
  6615. // copy src0, src1 to device if necessary
  6616. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  6617. if (id != g_main_device) {
  6618. if (convert_src1_to_q8_1) {
  6619. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  6620. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  6621. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  6622. } else {
  6623. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  6624. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  6625. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  6626. src1_ncols*ne10*sizeof(float), stream));
  6627. }
  6628. }
  6629. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  6630. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  6631. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  6632. } else {
  6633. GGML_ASSERT(false);
  6634. }
  6635. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  6636. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  6637. CUDA_CHECK(cudaGetLastError());
  6638. }
  6639. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  6640. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  6641. }
  6642. // do the computation
  6643. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  6644. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  6645. CUDA_CHECK(cudaGetLastError());
  6646. // copy dst to host or other device if necessary
  6647. if (!dst_on_device) {
  6648. void * dst_off_device;
  6649. cudaMemcpyKind kind;
  6650. if (dst->backend == GGML_BACKEND_CPU) {
  6651. dst_off_device = dst->data;
  6652. kind = cudaMemcpyDeviceToHost;
  6653. } else if (dst->backend == GGML_BACKEND_GPU) {
  6654. dst_off_device = dst_extra->data_device[g_main_device];
  6655. kind = cudaMemcpyDeviceToDevice;
  6656. } else {
  6657. GGML_ASSERT(false);
  6658. }
  6659. if (split) {
  6660. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  6661. // dst is NOT transposed.
  6662. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  6663. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  6664. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  6665. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6666. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6667. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  6668. #if !defined(GGML_USE_HIPBLAS)
  6669. if (kind == cudaMemcpyDeviceToDevice) {
  6670. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  6671. cudaMemcpy3DPeerParms p = {};
  6672. p.dstDevice = g_main_device;
  6673. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  6674. p.srcDevice = id;
  6675. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  6676. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  6677. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  6678. } else
  6679. #endif
  6680. {
  6681. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  6682. dst_dd_i, row_diff*sizeof(float),
  6683. row_diff*sizeof(float), src1_ncols,
  6684. kind, stream));
  6685. }
  6686. } else {
  6687. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6688. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6689. dhf_dst_i += src1_col_0*ne0;
  6690. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  6691. }
  6692. }
  6693. // add event for the main device to wait on until other device is done
  6694. if (split && (id != g_main_device || is != 0)) {
  6695. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  6696. }
  6697. }
  6698. }
  6699. }
  6700. // main device waits for all other devices to be finished
  6701. if (split && g_device_count > 1) {
  6702. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  6703. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  6704. ggml_cuda_set_device(g_main_device);
  6705. for (int id = 0; id < g_device_count; ++id) {
  6706. if (dev[id].row_low == dev[id].row_high) {
  6707. continue;
  6708. }
  6709. for (int64_t is = 0; is < is_max; ++is) {
  6710. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  6711. }
  6712. }
  6713. }
  6714. if (dst->backend == GGML_BACKEND_CPU) {
  6715. ggml_cuda_set_device(g_main_device);
  6716. CUDA_CHECK(cudaDeviceSynchronize());
  6717. }
  6718. }
  6719. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6720. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  6721. }
  6722. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6723. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  6724. }
  6725. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6726. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  6727. }
  6728. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6729. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  6730. }
  6731. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6732. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  6733. }
  6734. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6735. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  6736. }
  6737. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6738. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  6739. }
  6740. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6741. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  6742. }
  6743. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6744. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  6745. }
  6746. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6747. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  6748. }
  6749. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6750. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  6751. }
  6752. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6753. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  6754. }
  6755. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6756. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  6757. }
  6758. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6759. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  6760. }
  6761. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6762. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  6763. }
  6764. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6765. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  6766. }
  6767. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6768. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  6769. }
  6770. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6771. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  6772. }
  6773. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6774. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  6775. }
  6776. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  6777. if (!g_cublas_loaded) return false;
  6778. const int64_t ne10 = src1->ne[0];
  6779. const int64_t ne0 = dst->ne[0];
  6780. const int64_t ne1 = dst->ne[1];
  6781. // TODO: find the optimal values for these
  6782. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  6783. src1->type == GGML_TYPE_F32 &&
  6784. dst->type == GGML_TYPE_F32 &&
  6785. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  6786. }
  6787. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6788. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  6789. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6790. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  6791. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  6792. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6793. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6794. const int64_t ne00 = src0->ne[0];
  6795. const int64_t ne01 = src0->ne[1];
  6796. const int64_t ne02 = src0->ne[2];
  6797. const int64_t ne12 = src1->ne[2];
  6798. ggml_cuda_set_device(g_main_device);
  6799. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6800. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6801. void * src0_ddq = src0_extra->data_device[g_main_device];
  6802. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6803. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6804. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6805. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6806. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  6807. }
  6808. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6809. GGML_ASSERT(!ggml_is_transposed(src0));
  6810. GGML_ASSERT(!ggml_is_transposed(src1));
  6811. GGML_ASSERT(!ggml_is_permuted(src0));
  6812. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6813. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6814. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6815. const int64_t ne00 = src0->ne[0];
  6816. const int64_t ne01 = src0->ne[1];
  6817. const int64_t ne02 = src0->ne[2];
  6818. const int64_t nb01 = src0->nb[1];
  6819. const int64_t nb02 = src0->nb[2];
  6820. const int64_t ne12 = src1->ne[2];
  6821. ggml_cuda_set_device(g_main_device);
  6822. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6823. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6824. void * src0_ddq = src0_extra->data_device[g_main_device];
  6825. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6826. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6827. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6828. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6829. const int64_t row_stride_x = nb01 / sizeof(half);
  6830. const int64_t channel_stride_x = nb02 / sizeof(half);
  6831. 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);
  6832. }
  6833. static __global__ void k_compute_batched_ptrs(
  6834. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  6835. const void ** ptrs_src, void ** ptrs_dst,
  6836. int64_t ne12, int64_t ne13,
  6837. int64_t ne23,
  6838. size_t nb02, size_t nb03,
  6839. size_t nb12, size_t nb13,
  6840. size_t nbd2, size_t nbd3,
  6841. int64_t r2, int64_t r3) {
  6842. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6843. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6844. if (i13 >= ne13 || i12 >= ne12) {
  6845. return;
  6846. }
  6847. int64_t i03 = i13 / r3;
  6848. int64_t i02 = i12 / r2;
  6849. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  6850. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  6851. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  6852. }
  6853. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6854. GGML_ASSERT(!ggml_is_transposed(src0));
  6855. GGML_ASSERT(!ggml_is_transposed(src1));
  6856. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6857. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6858. GGML_TENSOR_BINARY_OP_LOCALS
  6859. const int64_t ne_dst = ggml_nelements(dst);
  6860. ggml_cuda_set_device(g_main_device);
  6861. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6862. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6863. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6864. void * src0_ddq = src0_extra->data_device[g_main_device];
  6865. half * src0_f16 = (half *) src0_ddq;
  6866. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6867. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6868. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6869. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6870. // convert src1 to fp16
  6871. cuda_pool_alloc<half> src1_f16_alloc;
  6872. if (src1->type != GGML_TYPE_F16) {
  6873. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6874. const int64_t ne_src1 = ggml_nelements(src1);
  6875. src1_f16_alloc.alloc(ne_src1);
  6876. GGML_ASSERT(to_fp16_cuda != nullptr);
  6877. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  6878. }
  6879. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  6880. cuda_pool_alloc<half> dst_f16;
  6881. char * dst_t;
  6882. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  6883. cudaDataType_t cu_data_type = CUDA_R_16F;
  6884. // dst strides
  6885. size_t nbd2 = dst->nb[2];
  6886. size_t nbd3 = dst->nb[3];
  6887. const half alpha_f16 = 1.0f;
  6888. const half beta_f16 = 0.0f;
  6889. const float alpha_f32 = 1.0f;
  6890. const float beta_f32 = 0.0f;
  6891. const void * alpha = &alpha_f16;
  6892. const void * beta = &beta_f16;
  6893. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6894. dst_t = (char *) dst_f16.alloc(ne_dst);
  6895. nbd2 /= sizeof(float) / sizeof(half);
  6896. nbd3 /= sizeof(float) / sizeof(half);
  6897. } else {
  6898. dst_t = (char *) dst_ddf;
  6899. cu_compute_type = CUBLAS_COMPUTE_32F;
  6900. cu_data_type = CUDA_R_32F;
  6901. alpha = &alpha_f32;
  6902. beta = &beta_f32;
  6903. }
  6904. GGML_ASSERT(ne12 % ne02 == 0);
  6905. GGML_ASSERT(ne13 % ne03 == 0);
  6906. // broadcast factors
  6907. const int64_t r2 = ne12/ne02;
  6908. const int64_t r3 = ne13/ne03;
  6909. #if 0
  6910. // use cublasGemmEx
  6911. {
  6912. for (int i13 = 0; i13 < ne13; ++i13) {
  6913. for (int i12 = 0; i12 < ne12; ++i12) {
  6914. int i03 = i13 / r3;
  6915. int i02 = i12 / r2;
  6916. CUBLAS_CHECK(
  6917. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6918. ne01, ne11, ne10,
  6919. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6920. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6921. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  6922. cu_compute_type,
  6923. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6924. }
  6925. }
  6926. }
  6927. #else
  6928. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6929. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6930. // use cublasGemmStridedBatchedEx
  6931. CUBLAS_CHECK(
  6932. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6933. ne01, ne11, ne10,
  6934. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  6935. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  6936. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  6937. ne12*ne13,
  6938. cu_compute_type,
  6939. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6940. } else {
  6941. // use cublasGemmBatchedEx
  6942. const int ne23 = ne12*ne13;
  6943. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  6944. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  6945. dim3 block_dims(ne13, ne12);
  6946. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6947. src0_f16, src1_f16, dst_t,
  6948. ptrs_src.get(), ptrs_dst.get(),
  6949. ne12, ne13,
  6950. ne23,
  6951. nb02, nb03,
  6952. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  6953. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  6954. nbd2, nbd3,
  6955. r2, r3);
  6956. CUDA_CHECK(cudaGetLastError());
  6957. CUBLAS_CHECK(
  6958. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6959. ne01, ne11, ne10,
  6960. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  6961. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  6962. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  6963. ne23,
  6964. cu_compute_type,
  6965. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6966. }
  6967. #endif
  6968. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6969. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6970. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  6971. }
  6972. }
  6973. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6974. const bool all_on_device =
  6975. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6976. (src1->backend == GGML_BACKEND_GPU) &&
  6977. ( dst->backend == GGML_BACKEND_GPU);
  6978. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6979. int64_t min_compute_capability = INT_MAX;
  6980. for (int id = 0; id < g_device_count; ++id) {
  6981. 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)) {
  6982. min_compute_capability = g_device_caps[id].cc;
  6983. }
  6984. }
  6985. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6986. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  6987. bool use_mul_mat_q = ggml_is_quantized(src0->type);
  6988. #ifdef CUDA_USE_TENSOR_CORES
  6989. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  6990. #endif // CUDA_USE_TENSOR_CORES
  6991. #else
  6992. const bool fp16_performance_good = min_compute_capability >= CC_VOLTA;
  6993. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  6994. #ifdef CUDA_USE_TENSOR_CORES
  6995. // when tensor cores are available, use them for large batch size
  6996. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  6997. use_mul_mat_q = use_mul_mat_q && !(fp16_performance_good && src1->ne[1] > MMQ_MAX_BATCH_SIZE);
  6998. #endif // CUDA_USE_TENSOR_CORES
  6999. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7000. // debug helpers
  7001. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  7002. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  7003. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  7004. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  7005. //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);
  7006. //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);
  7007. if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  7008. // KQ single-batch
  7009. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  7010. } else if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  7011. // KQV single-batch
  7012. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  7013. } else if (!split && all_on_device && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  7014. // KQ + KQV multi-batch
  7015. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  7016. } else if (src0->type == GGML_TYPE_F32) {
  7017. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7018. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  7019. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->type == GGML_TYPE_F32) {
  7020. #ifdef GGML_CUDA_FORCE_DMMV
  7021. const bool use_mul_mat_vec_q = false;
  7022. #else
  7023. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  7024. #endif // GGML_CUDA_FORCE_DMMV
  7025. if (use_mul_mat_vec_q) {
  7026. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  7027. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  7028. } else {
  7029. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  7030. }
  7031. } else {
  7032. if (use_mul_mat_q) {
  7033. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  7034. } else {
  7035. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7036. }
  7037. }
  7038. } else {
  7039. GGML_ASSERT(false);
  7040. }
  7041. }
  7042. #if 0
  7043. template<typename ... Srcs>
  7044. static __global__ void k_compute_batched_ptrs_id(
  7045. const void ** ptrs_src, void ** ptrs_dst,
  7046. int ne12, int ne13,
  7047. int ne23,
  7048. int nb02, int nb03,
  7049. int nb12, int nb13,
  7050. int nb2, int nb3,
  7051. int r2, int r3,
  7052. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  7053. const half * src1_f16, half * dst_f16,
  7054. const int32_t * ids, const int id,
  7055. Srcs... src0s) {
  7056. int i = ids[id];
  7057. half * src0_f16;
  7058. const void * srcs_ar[] = { (const half *) src0s... };
  7059. if (src0_type == GGML_TYPE_F16) {
  7060. src0_f16 = (half *) srcs_ar[i];
  7061. } else {
  7062. src0_f16 = src0_as_f16;
  7063. if (threadIdx.x == 0 && threadIdx.y == 0) {
  7064. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  7065. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  7066. }
  7067. }
  7068. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7069. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7070. if (i13 >= ne13 || i12 >= ne12) {
  7071. return;
  7072. }
  7073. int i03 = i13 / r3;
  7074. int i02 = i12 / r2;
  7075. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  7076. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  7077. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  7078. }
  7079. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  7080. const struct ggml_tensor * ids = dst->src[0];
  7081. const struct ggml_tensor * src1 = dst->src[1];
  7082. const struct ggml_tensor * src00 = dst->src[2];
  7083. const int id = dst->op_params[0];
  7084. GGML_ASSERT(!ggml_is_transposed(src00));
  7085. GGML_ASSERT(!ggml_is_transposed(src1));
  7086. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  7087. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7088. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  7089. const int64_t ne01 = src00->ne[1];
  7090. const int64_t ne02 = src00->ne[2];
  7091. const int64_t ne03 = src00->ne[3];
  7092. //const int64_t nb01 = src00->nb[1];
  7093. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  7094. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  7095. const int64_t ne10 = src1->ne[0];
  7096. const int64_t ne11 = src1->ne[1];
  7097. const int64_t ne12 = src1->ne[2];
  7098. const int64_t ne13 = src1->ne[3];
  7099. //const int64_t nb11 = src1->nb[1];
  7100. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  7101. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  7102. const int64_t ne1 = ggml_nelements(src1);
  7103. const int64_t ne = ggml_nelements(dst);
  7104. ggml_cuda_set_device(g_main_device);
  7105. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7106. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7107. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7108. //void * src0_ddq = src0_extra->data_device[g_main_device];
  7109. //half * src0_as_f16 = (half *) src0_ddq;
  7110. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7111. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7112. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7113. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7114. // convert src1 to fp16
  7115. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7116. GGML_ASSERT(to_fp16_cuda != nullptr);
  7117. size_t src1_as = 0;
  7118. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7119. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7120. size_t dst_as = 0;
  7121. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7122. GGML_ASSERT(ne12 % ne02 == 0);
  7123. GGML_ASSERT(ne13 % ne03 == 0);
  7124. // broadcast factors
  7125. const int64_t r2 = ne12/ne02;
  7126. const int64_t r3 = ne13/ne03;
  7127. const half alpha_f16 = 1.0f;
  7128. const half beta_f16 = 0.0f;
  7129. // use cublasGemmBatchedEx
  7130. const int ne23 = ne12*ne13;
  7131. const void ** ptrs_src = nullptr;
  7132. void ** ptrs_dst = nullptr;
  7133. size_t ptrs_src_s = 0;
  7134. size_t ptrs_dst_s = 0;
  7135. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7136. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7137. int64_t src0_ne = ggml_nelements(src00);
  7138. half * src0_as_f16 = nullptr;
  7139. size_t src0_as = 0;
  7140. if (src00->type != GGML_TYPE_F16) {
  7141. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7142. }
  7143. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7144. dim3 block_dims(ne13, ne12);
  7145. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7146. ptrs_src, ptrs_dst,
  7147. ne12, ne13,
  7148. ne23,
  7149. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7150. nb12, nb13,
  7151. dst->nb[2], dst->nb[3],
  7152. r2, r3,
  7153. src00->type, src0_as_f16, src0_ne,
  7154. src1_as_f16, dst_f16,
  7155. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7156. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7157. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7158. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7159. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7160. );
  7161. CUDA_CHECK(cudaGetLastError());
  7162. CUBLAS_CHECK(
  7163. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7164. ne01, ne11, ne10,
  7165. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7166. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7167. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7168. ne23,
  7169. CUBLAS_COMPUTE_16F,
  7170. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7171. if (src0_as != 0) {
  7172. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7173. }
  7174. if (ptrs_src_s != 0) {
  7175. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7176. }
  7177. if (ptrs_dst_s != 0) {
  7178. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7179. }
  7180. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7181. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7182. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7183. ggml_cuda_pool_free(dst_f16, dst_as);
  7184. }
  7185. #endif
  7186. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7187. #if 0
  7188. ggml_cuda_mul_mat_id_cublas(dst);
  7189. // TODO: mmq/mmv support
  7190. #endif
  7191. const int64_t nb11 = src1->nb[1];
  7192. const int64_t nb1 = dst->nb[1];
  7193. const struct ggml_tensor * ids = src0;
  7194. const int32_t id = ((int32_t *) dst->op_params)[0];
  7195. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7196. std::vector<char> ids_host(ggml_nbytes(ids));
  7197. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7198. if (ids->backend == GGML_BACKEND_GPU) {
  7199. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7200. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7201. CUDA_CHECK(cudaStreamSynchronize(stream));
  7202. } else {
  7203. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7204. }
  7205. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7206. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7207. ggml_tensor_extra_gpu src1_row_extra;
  7208. ggml_tensor_extra_gpu dst_row_extra;
  7209. ggml_tensor src1_row = *src1;
  7210. ggml_tensor dst_row = *dst;
  7211. src1_row.backend = GGML_BACKEND_GPU;
  7212. dst_row.backend = GGML_BACKEND_GPU;
  7213. src1_row.extra = &src1_row_extra;
  7214. dst_row.extra = &dst_row_extra;
  7215. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7216. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7217. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7218. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7219. if (src1->ne[1] == 1) {
  7220. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7221. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7222. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7223. //int32_t row_id;
  7224. //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]));
  7225. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7226. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7227. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7228. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7229. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7230. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7231. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7232. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7233. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7234. }
  7235. } else {
  7236. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  7237. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  7238. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  7239. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  7240. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7241. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7242. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7243. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  7244. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7245. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7246. int64_t num_src1_rows = 0;
  7247. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7248. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7249. if (row_id_i != row_id) {
  7250. continue;
  7251. }
  7252. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7253. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  7254. nb11, src1_kind, stream));
  7255. num_src1_rows++;
  7256. }
  7257. if (num_src1_rows == 0) {
  7258. continue;
  7259. }
  7260. src1_row.ne[1] = num_src1_rows;
  7261. dst_row.ne[1] = num_src1_rows;
  7262. src1_row.nb[1] = nb11;
  7263. src1_row.nb[2] = num_src1_rows*nb11;
  7264. src1_row.nb[3] = num_src1_rows*nb11;
  7265. dst_row.nb[1] = nb1;
  7266. dst_row.nb[2] = num_src1_rows*nb1;
  7267. dst_row.nb[3] = num_src1_rows*nb1;
  7268. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7269. num_src1_rows = 0;
  7270. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7271. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7272. if (row_id_i != row_id) {
  7273. continue;
  7274. }
  7275. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7276. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  7277. nb1, dst_kind, stream));
  7278. num_src1_rows++;
  7279. }
  7280. }
  7281. }
  7282. if (dst->backend == GGML_BACKEND_CPU) {
  7283. CUDA_CHECK(cudaStreamSynchronize(stream));
  7284. }
  7285. }
  7286. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7287. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7288. }
  7289. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7290. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7291. }
  7292. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7293. const int64_t ne = ggml_nelements(src0);
  7294. GGML_ASSERT(ne == ggml_nelements(src1));
  7295. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7296. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7297. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7298. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7299. const int64_t ne00 = src0->ne[0];
  7300. const int64_t ne01 = src0->ne[1];
  7301. GGML_ASSERT(src0->ne[3] == 1);
  7302. const int64_t nb00 = src0->nb[0];
  7303. const int64_t nb01 = src0->nb[1];
  7304. const int64_t nb02 = src0->nb[2];
  7305. const int64_t ne10 = src1->ne[0];
  7306. const int64_t ne11 = src1->ne[1];
  7307. GGML_ASSERT(src1->ne[3] == 1);
  7308. const int64_t nb10 = src1->nb[0];
  7309. const int64_t nb11 = src1->nb[1];
  7310. const int64_t nb12 = src1->nb[2];
  7311. ggml_cuda_set_device(g_main_device);
  7312. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7313. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7314. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7315. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7316. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7317. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7318. ggml_cpy_f32_f32_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_F32 && src1->type == GGML_TYPE_F16) {
  7320. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7321. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7322. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7323. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7324. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7325. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7326. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7327. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7328. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7329. } else {
  7330. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7331. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7332. GGML_ASSERT(false);
  7333. }
  7334. (void) dst;
  7335. }
  7336. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7337. // TODO: why do we pass dst as src1 here?
  7338. ggml_cuda_cpy(src0, dst, nullptr);
  7339. (void) src1;
  7340. }
  7341. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7342. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7343. }
  7344. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7345. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7346. }
  7347. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7348. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7349. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7350. }
  7351. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7352. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7353. }
  7354. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7355. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7356. }
  7357. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7358. GGML_ASSERT(ggml_is_contiguous(src0));
  7359. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7360. }
  7361. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7362. GGML_ASSERT(ggml_is_contiguous(src0));
  7363. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7364. }
  7365. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7366. (void) src0;
  7367. (void) src1;
  7368. (void) dst;
  7369. }
  7370. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7371. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7372. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7373. }
  7374. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7375. const int64_t nrows = ggml_nrows(tensor);
  7376. const int64_t ne0 = tensor->ne[0];
  7377. const size_t nb1 = tensor->nb[1];
  7378. ggml_backend_type backend = tensor->backend;
  7379. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7380. memset(extra, 0, sizeof(*extra));
  7381. for (int id = 0; id < g_device_count; ++id) {
  7382. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7383. continue;
  7384. }
  7385. ggml_cuda_set_device(id);
  7386. int64_t row_low, row_high;
  7387. if (backend == GGML_BACKEND_GPU) {
  7388. row_low = 0;
  7389. row_high = nrows;
  7390. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7391. const int64_t rounding = get_row_rounding(tensor->type);
  7392. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7393. row_low -= row_low % rounding;
  7394. if (id == g_device_count - 1) {
  7395. row_high = nrows;
  7396. } else {
  7397. row_high = nrows*g_tensor_split[id + 1];
  7398. row_high -= row_high % rounding;
  7399. }
  7400. } else {
  7401. GGML_ASSERT(false);
  7402. }
  7403. if (row_low == row_high) {
  7404. continue;
  7405. }
  7406. int64_t nrows_split = row_high - row_low;
  7407. const size_t offset_split = row_low*nb1;
  7408. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7409. const size_t original_size = size;
  7410. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7411. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7412. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7413. }
  7414. char * buf;
  7415. CUDA_CHECK(cudaMalloc(&buf, size));
  7416. char * buf_host = (char *)data + offset_split;
  7417. // set padding to 0 to avoid possible NaN values
  7418. if (size > original_size) {
  7419. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7420. }
  7421. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7422. extra->data_device[id] = buf;
  7423. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7424. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7425. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7426. }
  7427. }
  7428. }
  7429. tensor->extra = extra;
  7430. }
  7431. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7432. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7433. return;
  7434. }
  7435. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7436. for (int id = 0; id < g_device_count; ++id) {
  7437. ggml_cuda_set_device(id);
  7438. if (extra->data_device[id] != nullptr) {
  7439. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7440. }
  7441. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7442. if (extra->events[id][is] != nullptr) {
  7443. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7444. }
  7445. }
  7446. }
  7447. delete extra;
  7448. }
  7449. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7450. static size_t g_temp_tensor_extra_index = 0;
  7451. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7452. if (g_temp_tensor_extras == nullptr) {
  7453. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7454. }
  7455. size_t alloc_index = g_temp_tensor_extra_index;
  7456. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7457. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7458. memset(extra, 0, sizeof(*extra));
  7459. return extra;
  7460. }
  7461. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7462. if (scratch && g_scratch_size == 0) {
  7463. return;
  7464. }
  7465. tensor->backend = GGML_BACKEND_GPU;
  7466. // recursively assign CUDA buffers until a compute tensor is found
  7467. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7468. const ggml_op src0_op = tensor->src[0]->op;
  7469. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7470. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7471. }
  7472. }
  7473. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7474. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7475. }
  7476. if (scratch && no_alloc) {
  7477. return;
  7478. }
  7479. ggml_tensor_extra_gpu * extra;
  7480. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7481. tensor->op == GGML_OP_VIEW ||
  7482. force_inplace;
  7483. const size_t size = ggml_nbytes(tensor);
  7484. ggml_cuda_set_device(g_main_device);
  7485. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7486. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7487. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7488. size_t offset = 0;
  7489. if (tensor->op == GGML_OP_VIEW) {
  7490. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7491. }
  7492. extra = ggml_cuda_alloc_temp_tensor_extra();
  7493. extra->data_device[g_main_device] = src0_ddc + offset;
  7494. } else if (tensor->op == GGML_OP_CPY) {
  7495. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7496. void * src1_ddv = src1_extra->data_device[g_main_device];
  7497. extra = ggml_cuda_alloc_temp_tensor_extra();
  7498. extra->data_device[g_main_device] = src1_ddv;
  7499. } else if (scratch) {
  7500. GGML_ASSERT(size <= g_scratch_size);
  7501. if (g_scratch_offset + size > g_scratch_size) {
  7502. g_scratch_offset = 0;
  7503. }
  7504. char * data = (char *) g_scratch_buffer;
  7505. if (data == nullptr) {
  7506. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7507. g_scratch_buffer = data;
  7508. }
  7509. extra = ggml_cuda_alloc_temp_tensor_extra();
  7510. extra->data_device[g_main_device] = data + g_scratch_offset;
  7511. g_scratch_offset += size;
  7512. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7513. } else { // allocate new buffers outside of scratch
  7514. void * data;
  7515. CUDA_CHECK(cudaMalloc(&data, size));
  7516. CUDA_CHECK(cudaMemset(data, 0, size));
  7517. extra = new ggml_tensor_extra_gpu;
  7518. memset(extra, 0, sizeof(*extra));
  7519. extra->data_device[g_main_device] = data;
  7520. }
  7521. tensor->extra = extra;
  7522. }
  7523. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7524. if (g_scratch_size == 0) {
  7525. return;
  7526. }
  7527. if (g_scratch_buffer == nullptr) {
  7528. ggml_cuda_set_device(g_main_device);
  7529. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7530. }
  7531. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7532. const bool inplace = tensor->view_src != nullptr;
  7533. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  7534. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  7535. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7536. size_t view_offset = 0;
  7537. if (tensor->op == GGML_OP_VIEW) {
  7538. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7539. }
  7540. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7541. } else {
  7542. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7543. }
  7544. tensor->extra = extra;
  7545. }
  7546. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7547. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7548. GGML_ASSERT(ggml_is_contiguous(tensor));
  7549. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7550. ggml_cuda_set_device(g_main_device);
  7551. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7552. }
  7553. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7554. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7555. }
  7556. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7557. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7558. }
  7559. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  7560. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  7561. }
  7562. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  7563. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  7564. }
  7565. void ggml_cuda_set_main_device(const int main_device) {
  7566. if (main_device >= g_device_count) {
  7567. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  7568. main_device, g_device_count, g_main_device);
  7569. return;
  7570. }
  7571. if (g_main_device != main_device && g_device_count > 1) {
  7572. g_main_device = main_device;
  7573. cudaDeviceProp prop;
  7574. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  7575. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  7576. }
  7577. }
  7578. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  7579. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  7580. // it still won't always work as expected, but it's better than nothing
  7581. if (scratch_size > g_scratch_size) {
  7582. ggml_cuda_free_scratch();
  7583. }
  7584. g_scratch_size = std::max(g_scratch_size, scratch_size);
  7585. }
  7586. void ggml_cuda_free_scratch() {
  7587. if (g_scratch_buffer == nullptr) {
  7588. return;
  7589. }
  7590. CUDA_CHECK(cudaFree(g_scratch_buffer));
  7591. g_scratch_buffer = nullptr;
  7592. }
  7593. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  7594. if (!g_cublas_loaded) return false;
  7595. ggml_cuda_func_t func;
  7596. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  7597. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  7598. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  7599. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  7600. return false;
  7601. }
  7602. if (tensor->op == GGML_OP_MUL_MAT) {
  7603. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  7604. #ifndef NDEBUG
  7605. 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]);
  7606. #endif
  7607. return false;
  7608. }
  7609. }
  7610. switch (tensor->op) {
  7611. case GGML_OP_REPEAT:
  7612. func = ggml_cuda_repeat;
  7613. break;
  7614. case GGML_OP_GET_ROWS:
  7615. func = ggml_cuda_get_rows;
  7616. break;
  7617. case GGML_OP_DUP:
  7618. func = ggml_cuda_dup;
  7619. break;
  7620. case GGML_OP_ADD:
  7621. func = ggml_cuda_add;
  7622. break;
  7623. case GGML_OP_ACC:
  7624. func = ggml_cuda_acc;
  7625. break;
  7626. case GGML_OP_MUL:
  7627. func = ggml_cuda_mul;
  7628. break;
  7629. case GGML_OP_DIV:
  7630. func = ggml_cuda_div;
  7631. break;
  7632. case GGML_OP_UNARY:
  7633. switch (ggml_get_unary_op(tensor)) {
  7634. case GGML_UNARY_OP_GELU:
  7635. func = ggml_cuda_gelu;
  7636. break;
  7637. case GGML_UNARY_OP_SILU:
  7638. func = ggml_cuda_silu;
  7639. break;
  7640. case GGML_UNARY_OP_GELU_QUICK:
  7641. func = ggml_cuda_gelu_quick;
  7642. break;
  7643. case GGML_UNARY_OP_TANH:
  7644. func = ggml_cuda_tanh;
  7645. break;
  7646. case GGML_UNARY_OP_RELU:
  7647. func = ggml_cuda_relu;
  7648. break;
  7649. default:
  7650. return false;
  7651. }
  7652. break;
  7653. case GGML_OP_NORM:
  7654. func = ggml_cuda_norm;
  7655. break;
  7656. case GGML_OP_GROUP_NORM:
  7657. func = ggml_cuda_group_norm;
  7658. break;
  7659. case GGML_OP_CONCAT:
  7660. func = ggml_cuda_concat;
  7661. break;
  7662. case GGML_OP_UPSCALE:
  7663. func = ggml_cuda_upscale;
  7664. break;
  7665. case GGML_OP_PAD:
  7666. func = ggml_cuda_pad;
  7667. break;
  7668. case GGML_OP_LEAKY_RELU:
  7669. func = ggml_cuda_leaky_relu;
  7670. break;
  7671. case GGML_OP_RMS_NORM:
  7672. func = ggml_cuda_rms_norm;
  7673. break;
  7674. case GGML_OP_MUL_MAT:
  7675. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  7676. return false;
  7677. }
  7678. func = ggml_cuda_mul_mat;
  7679. break;
  7680. case GGML_OP_MUL_MAT_ID:
  7681. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  7682. return false;
  7683. }
  7684. func = ggml_cuda_mul_mat_id;
  7685. break;
  7686. case GGML_OP_SCALE:
  7687. func = ggml_cuda_scale;
  7688. break;
  7689. case GGML_OP_SQR:
  7690. func = ggml_cuda_sqr;
  7691. break;
  7692. case GGML_OP_CLAMP:
  7693. func = ggml_cuda_clamp;
  7694. break;
  7695. case GGML_OP_CPY:
  7696. func = ggml_cuda_cpy;
  7697. break;
  7698. case GGML_OP_CONT:
  7699. func = ggml_cuda_dup;
  7700. break;
  7701. case GGML_OP_NONE:
  7702. case GGML_OP_RESHAPE:
  7703. case GGML_OP_VIEW:
  7704. case GGML_OP_PERMUTE:
  7705. case GGML_OP_TRANSPOSE:
  7706. func = ggml_cuda_nop;
  7707. break;
  7708. case GGML_OP_DIAG_MASK_INF:
  7709. func = ggml_cuda_diag_mask_inf;
  7710. break;
  7711. case GGML_OP_SOFT_MAX:
  7712. func = ggml_cuda_soft_max;
  7713. break;
  7714. case GGML_OP_ROPE:
  7715. func = ggml_cuda_rope;
  7716. break;
  7717. case GGML_OP_ALIBI:
  7718. func = ggml_cuda_alibi;
  7719. break;
  7720. case GGML_OP_IM2COL:
  7721. func = ggml_cuda_im2col;
  7722. break;
  7723. case GGML_OP_SUM_ROWS:
  7724. func = ggml_cuda_sum_rows;
  7725. break;
  7726. case GGML_OP_ARGSORT:
  7727. func = ggml_cuda_argsort;
  7728. break;
  7729. default:
  7730. return false;
  7731. }
  7732. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  7733. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  7734. }
  7735. if (params->ith != 0) {
  7736. return true;
  7737. }
  7738. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  7739. return true;
  7740. }
  7741. func(tensor->src[0], tensor->src[1], tensor);
  7742. return true;
  7743. }
  7744. int ggml_cuda_get_device_count() {
  7745. int device_count;
  7746. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  7747. return 0;
  7748. }
  7749. return device_count;
  7750. }
  7751. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  7752. cudaDeviceProp prop;
  7753. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  7754. snprintf(description, description_size, "%s", prop.name);
  7755. }
  7756. ////////////////////////////////////////////////////////////////////////////////
  7757. // backend interface
  7758. #define UNUSED GGML_UNUSED
  7759. // cuda buffer
  7760. struct ggml_backend_buffer_context_cuda {
  7761. int device;
  7762. void * dev_ptr = nullptr;
  7763. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  7764. size_t temp_tensor_extra_index = 0;
  7765. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  7766. ~ggml_backend_buffer_context_cuda() {
  7767. delete[] temp_tensor_extras;
  7768. }
  7769. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7770. if (temp_tensor_extras == nullptr) {
  7771. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7772. }
  7773. size_t alloc_index = temp_tensor_extra_index;
  7774. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7775. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  7776. memset(extra, 0, sizeof(*extra));
  7777. return extra;
  7778. }
  7779. };
  7780. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7781. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7782. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  7783. delete ctx;
  7784. }
  7785. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  7786. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7787. return ctx->dev_ptr;
  7788. }
  7789. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7790. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7791. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  7792. assert(tensor->view_src->buffer->buft == buffer->buft);
  7793. tensor->backend = tensor->view_src->backend;
  7794. tensor->extra = tensor->view_src->extra;
  7795. return;
  7796. }
  7797. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  7798. extra->data_device[ctx->device] = tensor->data;
  7799. tensor->backend = GGML_BACKEND_GPU;
  7800. tensor->extra = extra;
  7801. if (ggml_is_quantized(tensor->type)) {
  7802. // initialize padding to 0 to avoid possible NaN values
  7803. int64_t row_low = 0;
  7804. int64_t row_high = ggml_nrows(tensor);
  7805. int64_t nrows_split = row_high - row_low;
  7806. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  7807. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  7808. if (padded_size > original_size && tensor->view_src == nullptr) {
  7809. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  7810. }
  7811. }
  7812. UNUSED(buffer);
  7813. }
  7814. 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) {
  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((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  7820. }
  7821. 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) {
  7822. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7823. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7824. ggml_cuda_set_device(ctx->device);
  7825. CUDA_CHECK(cudaDeviceSynchronize());
  7826. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  7827. }
  7828. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7829. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7830. ggml_cuda_set_device(ctx->device);
  7831. CUDA_CHECK(cudaDeviceSynchronize());
  7832. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  7833. }
  7834. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  7835. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  7836. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  7837. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  7838. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  7839. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  7840. /* .cpy_tensor_from = */ NULL,
  7841. /* .cpy_tensor_to = */ NULL,
  7842. /* .clear = */ ggml_backend_cuda_buffer_clear,
  7843. };
  7844. // cuda buffer type
  7845. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7846. int device = (int) (intptr_t) buft->context;
  7847. ggml_cuda_set_device(device);
  7848. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  7849. void * dev_ptr;
  7850. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  7851. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  7852. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  7853. }
  7854. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7855. return 128;
  7856. UNUSED(buft);
  7857. }
  7858. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  7859. int64_t row_low = 0;
  7860. int64_t row_high = ggml_nrows(tensor);
  7861. int64_t nrows_split = row_high - row_low;
  7862. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7863. int64_t ne0 = tensor->ne[0];
  7864. if (ggml_is_quantized(tensor->type)) {
  7865. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7866. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7867. }
  7868. }
  7869. return size;
  7870. UNUSED(buft);
  7871. }
  7872. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  7873. return ggml_backend_is_cuda(backend);
  7874. UNUSED(buft);
  7875. }
  7876. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  7877. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  7878. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  7879. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  7880. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  7881. /* .is_host = */ nullptr,
  7882. };
  7883. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  7884. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  7885. static bool ggml_backend_cuda_buffer_type_initialized = false;
  7886. if (!ggml_backend_cuda_buffer_type_initialized) {
  7887. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  7888. ggml_backend_cuda_buffer_types[i] = {
  7889. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  7890. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  7891. };
  7892. }
  7893. ggml_backend_cuda_buffer_type_initialized = true;
  7894. }
  7895. return &ggml_backend_cuda_buffer_types[device];
  7896. }
  7897. // host buffer type
  7898. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7899. ggml_cuda_host_free(buffer->context);
  7900. }
  7901. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7902. void * ptr = ggml_cuda_host_malloc(size);
  7903. if (ptr == nullptr) {
  7904. // fallback to cpu buffer
  7905. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7906. }
  7907. // FIXME: this is a hack to avoid having to implement a new buffer type
  7908. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7909. buffer->buft = buft;
  7910. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  7911. return buffer;
  7912. }
  7913. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  7914. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  7915. /* .iface = */ {
  7916. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  7917. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  7918. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7919. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  7920. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7921. },
  7922. /* .context = */ nullptr,
  7923. };
  7924. return &ggml_backend_cuda_buffer_type_host;
  7925. }
  7926. // backend
  7927. struct ggml_backend_context_cuda {
  7928. int device;
  7929. };
  7930. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  7931. return GGML_CUDA_NAME;
  7932. UNUSED(backend);
  7933. }
  7934. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  7935. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7936. delete cuda_ctx;
  7937. delete backend;
  7938. }
  7939. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  7940. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7941. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  7942. }
  7943. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7944. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7945. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7946. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7947. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  7948. }
  7949. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7950. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7951. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7952. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7953. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  7954. }
  7955. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  7956. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7957. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  7958. UNUSED(backend);
  7959. }
  7960. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7961. GGML_ASSERT(!"not implemented");
  7962. return nullptr;
  7963. UNUSED(backend);
  7964. UNUSED(cgraph);
  7965. }
  7966. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7967. GGML_ASSERT(!"not implemented");
  7968. UNUSED(backend);
  7969. UNUSED(plan);
  7970. }
  7971. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7972. GGML_ASSERT(!"not implemented");
  7973. UNUSED(backend);
  7974. UNUSED(plan);
  7975. }
  7976. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7977. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7978. ggml_cuda_set_main_device(cuda_ctx->device);
  7979. ggml_compute_params params = {};
  7980. params.type = GGML_TASK_COMPUTE;
  7981. params.ith = 0;
  7982. for (int i = 0; i < cgraph->n_nodes; i++) {
  7983. ggml_tensor * node = cgraph->nodes[i];
  7984. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  7985. continue;
  7986. assert(node->backend == GGML_BACKEND_GPU);
  7987. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7988. assert(node->extra != nullptr);
  7989. for (int j = 0; j < GGML_MAX_SRC; j++) {
  7990. if (node->src[j] != nullptr) {
  7991. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  7992. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7993. assert(node->src[j]->extra != nullptr);
  7994. }
  7995. }
  7996. bool ok = ggml_cuda_compute_forward(&params, node);
  7997. if (!ok) {
  7998. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  7999. }
  8000. GGML_ASSERT(ok);
  8001. #if 0
  8002. if (node->type == GGML_TYPE_F32) {
  8003. cudaDeviceSynchronize();
  8004. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  8005. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  8006. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  8007. ggml_type_name(node->src[0]->type),
  8008. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  8009. node->src[0]->name,
  8010. node->src[1] ? node->src[1]->name : "none");
  8011. double sum = 0.0;
  8012. double sq_sum = 0.0;
  8013. for (int i = 0; i < ggml_nelements(node); i++) {
  8014. printf("%f ", tmp[i]);
  8015. sum += tmp[i];
  8016. sq_sum += tmp[i]*tmp[i];
  8017. }
  8018. printf("\n");
  8019. printf("sum: %f, ", sum);
  8020. printf("sq_sum: %f\n", sq_sum);
  8021. }
  8022. #endif
  8023. }
  8024. UNUSED(backend);
  8025. }
  8026. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  8027. switch (op->op) {
  8028. case GGML_OP_UNARY:
  8029. switch (ggml_get_unary_op(op)) {
  8030. case GGML_UNARY_OP_GELU:
  8031. case GGML_UNARY_OP_SILU:
  8032. case GGML_UNARY_OP_RELU:
  8033. case GGML_UNARY_OP_GELU_QUICK:
  8034. case GGML_UNARY_OP_TANH:
  8035. return true;
  8036. default:
  8037. return false;
  8038. }
  8039. break;
  8040. case GGML_OP_MUL_MAT:
  8041. case GGML_OP_MUL_MAT_ID:
  8042. {
  8043. struct ggml_tensor * a;
  8044. struct ggml_tensor * b;
  8045. if (op->op == GGML_OP_MUL_MAT) {
  8046. a = op->src[0];
  8047. b = op->src[1];
  8048. } else {
  8049. a = op->src[2];
  8050. b = op->src[1];
  8051. }
  8052. if (a->ne[3] != b->ne[3]) {
  8053. return false;
  8054. }
  8055. return true;
  8056. } break;
  8057. case GGML_OP_GET_ROWS:
  8058. {
  8059. switch (op->src[0]->type) {
  8060. case GGML_TYPE_F16:
  8061. case GGML_TYPE_F32:
  8062. case GGML_TYPE_Q4_0:
  8063. case GGML_TYPE_Q4_1:
  8064. case GGML_TYPE_Q5_0:
  8065. case GGML_TYPE_Q5_1:
  8066. case GGML_TYPE_Q8_0:
  8067. return true;
  8068. default:
  8069. return false;
  8070. }
  8071. } break;
  8072. case GGML_OP_CPY:
  8073. {
  8074. ggml_type src0_type = op->src[0]->type;
  8075. ggml_type src1_type = op->src[1]->type;
  8076. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  8077. return true;
  8078. }
  8079. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  8080. return true;
  8081. }
  8082. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  8083. return true;
  8084. }
  8085. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  8086. return true;
  8087. }
  8088. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  8089. return true;
  8090. }
  8091. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8092. return true;
  8093. }
  8094. return false;
  8095. } break;
  8096. case GGML_OP_NONE:
  8097. case GGML_OP_RESHAPE:
  8098. case GGML_OP_VIEW:
  8099. case GGML_OP_PERMUTE:
  8100. case GGML_OP_TRANSPOSE:
  8101. case GGML_OP_NORM:
  8102. case GGML_OP_REPEAT:
  8103. case GGML_OP_DUP:
  8104. case GGML_OP_ADD:
  8105. case GGML_OP_MUL:
  8106. case GGML_OP_DIV:
  8107. case GGML_OP_RMS_NORM:
  8108. case GGML_OP_SCALE:
  8109. case GGML_OP_SQR:
  8110. case GGML_OP_CLAMP:
  8111. case GGML_OP_CONT:
  8112. case GGML_OP_DIAG_MASK_INF:
  8113. case GGML_OP_SOFT_MAX:
  8114. case GGML_OP_ROPE:
  8115. case GGML_OP_ALIBI:
  8116. case GGML_OP_IM2COL:
  8117. case GGML_OP_SUM_ROWS:
  8118. case GGML_OP_ARGSORT:
  8119. case GGML_OP_ACC:
  8120. case GGML_OP_CONCAT:
  8121. case GGML_OP_GROUP_NORM:
  8122. case GGML_OP_UPSCALE:
  8123. case GGML_OP_PAD:
  8124. case GGML_OP_LEAKY_RELU:
  8125. return true;
  8126. default:
  8127. return false;
  8128. }
  8129. UNUSED(backend);
  8130. }
  8131. static ggml_backend_i cuda_backend_i = {
  8132. /* .get_name = */ ggml_backend_cuda_name,
  8133. /* .free = */ ggml_backend_cuda_free,
  8134. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8135. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8136. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8137. /* .cpy_tensor_from_async = */ NULL,
  8138. /* .cpy_tensor_to_async = */ NULL,
  8139. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8140. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8141. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8142. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8143. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8144. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8145. };
  8146. ggml_backend_t ggml_backend_cuda_init(int device) {
  8147. ggml_init_cublas(); // TODO: remove from ggml.c
  8148. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8149. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8150. return nullptr;
  8151. }
  8152. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8153. ggml_cuda_set_main_device(device);
  8154. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8155. /* .device = */ device
  8156. };
  8157. ggml_backend_t cuda_backend = new ggml_backend {
  8158. /* .interface = */ cuda_backend_i,
  8159. /* .context = */ ctx
  8160. };
  8161. return cuda_backend;
  8162. }
  8163. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8164. return backend->iface.get_name == ggml_backend_cuda_name;
  8165. }
  8166. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8167. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8168. return cuda_backend;
  8169. UNUSED(params);
  8170. }
  8171. extern "C" int ggml_backend_cuda_reg_devices();
  8172. int ggml_backend_cuda_reg_devices() {
  8173. int device_count = ggml_cuda_get_device_count();
  8174. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8175. for (int i = 0; i < device_count; i++) {
  8176. char name[128];
  8177. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8178. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8179. }
  8180. return device_count;
  8181. }