ggml-cuda.cu 406 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863
  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 CC_PASCAL 600
  114. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  115. #define CC_VOLTA 700
  116. #define CC_OFFSET_AMD 1000000
  117. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  118. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  119. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  120. #define GGML_CUDA_MAX_NODES 8192
  121. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  122. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  123. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  124. // - 7B quantum model: +100-200 MB
  125. // - 13B quantum model: +200-400 MB
  126. //
  127. //#define GGML_CUDA_FORCE_MMQ
  128. // TODO: improve this to be correct for more hardware
  129. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  130. #if !defined(GGML_CUDA_FORCE_MMQ)
  131. #define CUDA_USE_TENSOR_CORES
  132. #endif
  133. // max batch size to use MMQ kernels when tensor cores are available
  134. #define MMQ_MAX_BATCH_SIZE 32
  135. #if defined(GGML_USE_HIPBLAS)
  136. #define __CUDA_ARCH__ 1300
  137. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  138. defined(__gfx1150__) || defined(__gfx1151__)
  139. #define RDNA3
  140. #endif
  141. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  142. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  143. #define RDNA2
  144. #endif
  145. #ifndef __has_builtin
  146. #define __has_builtin(x) 0
  147. #endif
  148. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  149. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  150. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  151. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  152. #if __has_builtin(__builtin_elementwise_sub_sat)
  153. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  154. return reinterpret_cast<const int &>(c);
  155. #else
  156. int8x4_t c;
  157. int16_t tmp;
  158. #pragma unroll
  159. for (int i = 0; i < 4; i++) {
  160. tmp = va[i] - vb[i];
  161. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  162. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  163. c[i] = tmp;
  164. }
  165. return reinterpret_cast<int &>(c);
  166. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  167. }
  168. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  169. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  170. c = __builtin_amdgcn_sdot4(a, b, c, false);
  171. #elif defined(RDNA3)
  172. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  173. #elif defined(__gfx1010__) || defined(__gfx900__)
  174. int tmp1;
  175. int tmp2;
  176. asm("\n \
  177. 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 \
  178. 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 \
  179. v_add3_u32 %0, %1, %2, %0 \n \
  180. 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 \
  181. 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 \
  182. v_add3_u32 %0, %1, %2, %0 \n \
  183. "
  184. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  185. : "v"(a), "v"(b)
  186. );
  187. #else
  188. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  189. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  190. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  191. #endif
  192. return c;
  193. }
  194. #endif // defined(GGML_USE_HIPBLAS)
  195. #if defined(_MSC_VER)
  196. #pragma warning(disable: 4244 4267) // possible loss of data
  197. #endif
  198. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  199. [[noreturn]]
  200. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  201. int id = -1; // in case cudaGetDevice fails
  202. cudaGetDevice(&id);
  203. fprintf(stderr, "CUDA error: %s\n", msg);
  204. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  205. fprintf(stderr, " %s\n", stmt);
  206. // abort with GGML_ASSERT to get a stack trace
  207. GGML_ASSERT(!"CUDA error");
  208. }
  209. #define CUDA_CHECK_GEN(err, success, error_fn) \
  210. do { \
  211. auto err_ = (err); \
  212. if (err_ != (success)) { \
  213. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  214. } \
  215. } while (0)
  216. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  217. #if CUDART_VERSION >= 12000
  218. static const char * cublas_get_error_str(const cublasStatus_t err) {
  219. return cublasGetStatusString(err);
  220. }
  221. #else
  222. static const char * cublas_get_error_str(const cublasStatus_t err) {
  223. switch (err) {
  224. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  225. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  226. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  227. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  228. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  229. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  230. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  231. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  232. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  233. default: return "unknown error";
  234. }
  235. }
  236. #endif // CUDART_VERSION >= 12000
  237. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  238. #if !defined(GGML_USE_HIPBLAS)
  239. static const char * cu_get_error_str(CUresult err) {
  240. const char * err_str;
  241. cuGetErrorString(err, &err_str);
  242. return err_str;
  243. }
  244. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  245. #endif
  246. #if CUDART_VERSION >= 11100
  247. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  248. #else
  249. #define GGML_CUDA_ASSUME(x)
  250. #endif // CUDART_VERSION >= 11100
  251. #ifdef GGML_CUDA_F16
  252. typedef half dfloat; // dequantize float
  253. typedef half2 dfloat2;
  254. #else
  255. typedef float dfloat; // dequantize float
  256. typedef float2 dfloat2;
  257. #endif //GGML_CUDA_F16
  258. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  259. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  260. int x32 = 0;
  261. x32 |= x16[0] << 0;
  262. x32 |= x16[1] << 16;
  263. return x32;
  264. }
  265. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  266. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  267. int x32 = 0;
  268. x32 |= x16[0] << 0;
  269. x32 |= x16[1] << 16;
  270. return x32;
  271. }
  272. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  273. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  274. }
  275. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  276. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  277. }
  278. template<typename T>
  279. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  280. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  281. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  282. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  283. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  284. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  285. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  286. typedef void (*ggml_cuda_op_mul_mat_t)(
  287. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  288. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  289. const int64_t src1_padded_row_size, cudaStream_t stream);
  290. typedef void (*ggml_cuda_op_flatten_t)(
  291. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  292. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  293. // QK = number of values after dequantization
  294. // QR = QK / number of values before dequantization
  295. // QI = number of 32 bit integers before dequantization
  296. #define QK4_0 32
  297. #define QR4_0 2
  298. #define QI4_0 (QK4_0 / (4 * QR4_0))
  299. typedef struct {
  300. half d; // delta
  301. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  302. } block_q4_0;
  303. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  304. #define QK4_1 32
  305. #define QR4_1 2
  306. #define QI4_1 (QK4_1 / (4 * QR4_1))
  307. typedef struct {
  308. half2 dm; // dm.x = delta, dm.y = min
  309. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  310. } block_q4_1;
  311. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  312. #define QK5_0 32
  313. #define QR5_0 2
  314. #define QI5_0 (QK5_0 / (4 * QR5_0))
  315. typedef struct {
  316. half d; // delta
  317. uint8_t qh[4]; // 5-th bit of quants
  318. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  319. } block_q5_0;
  320. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  321. #define QK5_1 32
  322. #define QR5_1 2
  323. #define QI5_1 (QK5_1 / (4 * QR5_1))
  324. typedef struct {
  325. half2 dm; // dm.x = delta, dm.y = min
  326. uint8_t qh[4]; // 5-th bit of quants
  327. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  328. } block_q5_1;
  329. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  330. #define QK8_0 32
  331. #define QR8_0 1
  332. #define QI8_0 (QK8_0 / (4 * QR8_0))
  333. typedef struct {
  334. half d; // delta
  335. int8_t qs[QK8_0]; // quants
  336. } block_q8_0;
  337. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  338. #define QK8_1 32
  339. #define QR8_1 1
  340. #define QI8_1 (QK8_1 / (4 * QR8_1))
  341. typedef struct {
  342. half2 ds; // ds.x = delta, ds.y = sum
  343. int8_t qs[QK8_0]; // quants
  344. } block_q8_1;
  345. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  346. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  347. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  348. typedef void (*load_tiles_cuda_t)(
  349. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  350. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  351. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  352. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  353. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  354. //================================= k-quants
  355. #ifdef GGML_QKK_64
  356. #define QK_K 64
  357. #define K_SCALE_SIZE 4
  358. #else
  359. #define QK_K 256
  360. #define K_SCALE_SIZE 12
  361. #endif
  362. #define QR2_K 4
  363. #define QI2_K (QK_K / (4*QR2_K))
  364. typedef struct {
  365. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  366. uint8_t qs[QK_K/4]; // quants
  367. half2 dm; // super-block scale for quantized scales/mins
  368. } block_q2_K;
  369. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  370. #define QR3_K 4
  371. #define QI3_K (QK_K / (4*QR3_K))
  372. typedef struct {
  373. uint8_t hmask[QK_K/8]; // quants - high bit
  374. uint8_t qs[QK_K/4]; // quants - low 2 bits
  375. #ifdef GGML_QKK_64
  376. uint8_t scales[2]; // scales, quantized with 8 bits
  377. #else
  378. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  379. #endif
  380. half d; // super-block scale
  381. } block_q3_K;
  382. //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");
  383. #define QR4_K 2
  384. #define QI4_K (QK_K / (4*QR4_K))
  385. #ifdef GGML_QKK_64
  386. typedef struct {
  387. half dm[2]; // super-block scales/mins
  388. uint8_t scales[2]; // 4-bit block scales/mins
  389. uint8_t qs[QK_K/2]; // 4--bit quants
  390. } block_q4_K;
  391. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  392. #else
  393. typedef struct {
  394. half2 dm; // super-block scale for quantized scales/mins
  395. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  396. uint8_t qs[QK_K/2]; // 4--bit quants
  397. } block_q4_K;
  398. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  399. #endif
  400. #define QR5_K 2
  401. #define QI5_K (QK_K / (4*QR5_K))
  402. #ifdef GGML_QKK_64
  403. typedef struct {
  404. half d; // super-block scale
  405. int8_t scales[QK_K/16]; // block scales
  406. uint8_t qh[QK_K/8]; // quants, high bit
  407. uint8_t qs[QK_K/2]; // quants, low 4 bits
  408. } block_q5_K;
  409. 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");
  410. #else
  411. typedef struct {
  412. half2 dm; // super-block scale for quantized scales/mins
  413. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  414. uint8_t qh[QK_K/8]; // quants, high bit
  415. uint8_t qs[QK_K/2]; // quants, low 4 bits
  416. } block_q5_K;
  417. 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");
  418. #endif
  419. #define QR6_K 2
  420. #define QI6_K (QK_K / (4*QR6_K))
  421. typedef struct {
  422. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  423. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  424. int8_t scales[QK_K/16]; // scales
  425. half d; // delta
  426. } block_q6_K;
  427. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  428. #define QR2_XXS 8
  429. #define QI2_XXS (QK_K / (4*QR2_XXS))
  430. typedef struct {
  431. half d;
  432. uint16_t qs[QK_K/8];
  433. } block_iq2_xxs;
  434. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  435. #define QR2_XS 8
  436. #define QI2_XS (QK_K / (4*QR2_XS))
  437. typedef struct {
  438. half d;
  439. uint16_t qs[QK_K/8];
  440. uint8_t scales[QK_K/32];
  441. } block_iq2_xs;
  442. static_assert(sizeof(block_iq2_xs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");
  443. #define WARP_SIZE 32
  444. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  445. #define CUDA_GELU_BLOCK_SIZE 256
  446. #define CUDA_SILU_BLOCK_SIZE 256
  447. #define CUDA_TANH_BLOCK_SIZE 256
  448. #define CUDA_RELU_BLOCK_SIZE 256
  449. #define CUDA_SQR_BLOCK_SIZE 256
  450. #define CUDA_CPY_BLOCK_SIZE 32
  451. #define CUDA_SCALE_BLOCK_SIZE 256
  452. #define CUDA_CLAMP_BLOCK_SIZE 256
  453. #define CUDA_ROPE_BLOCK_SIZE 256
  454. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  455. #define CUDA_ALIBI_BLOCK_SIZE 32
  456. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  457. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  458. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  459. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  460. #define CUDA_UPSCALE_BLOCK_SIZE 256
  461. #define CUDA_CONCAT_BLOCK_SIZE 256
  462. #define CUDA_PAD_BLOCK_SIZE 256
  463. #define CUDA_ACC_BLOCK_SIZE 256
  464. #define CUDA_IM2COL_BLOCK_SIZE 256
  465. // dmmv = dequantize_mul_mat_vec
  466. #ifndef GGML_CUDA_DMMV_X
  467. #define GGML_CUDA_DMMV_X 32
  468. #endif
  469. #ifndef GGML_CUDA_MMV_Y
  470. #define GGML_CUDA_MMV_Y 1
  471. #endif
  472. #ifndef K_QUANTS_PER_ITERATION
  473. #define K_QUANTS_PER_ITERATION 2
  474. #else
  475. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  476. #endif
  477. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  478. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  479. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  480. #define MUL_MAT_SRC1_COL_STRIDE 128
  481. #define MAX_STREAMS 8
  482. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  483. struct ggml_tensor_extra_gpu {
  484. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  485. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  486. };
  487. // this is faster on Windows
  488. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  489. static void ggml_cuda_set_device(const int device) {
  490. int current_device;
  491. CUDA_CHECK(cudaGetDevice(&current_device));
  492. if (device == current_device) {
  493. return;
  494. }
  495. CUDA_CHECK(cudaSetDevice(device));
  496. }
  497. static int g_device_count = -1;
  498. static int g_main_device = 0;
  499. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  500. struct cuda_device_capabilities {
  501. int cc; // compute capability
  502. size_t smpb; // max. shared memory per block
  503. bool vmm; // virtual memory support
  504. size_t vmm_granularity; // granularity of virtual memory
  505. };
  506. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  507. static void * g_scratch_buffer = nullptr;
  508. static size_t g_scratch_size = 0; // disabled by default
  509. static size_t g_scratch_offset = 0;
  510. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  511. [[noreturn]]
  512. static __device__ void bad_arch() {
  513. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  514. __trap();
  515. (void) bad_arch; // suppress unused function warning
  516. }
  517. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  518. #pragma unroll
  519. for (int mask = 16; mask > 0; mask >>= 1) {
  520. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  521. }
  522. return x;
  523. }
  524. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  525. #pragma unroll
  526. for (int mask = 16; mask > 0; mask >>= 1) {
  527. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  528. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  529. }
  530. return a;
  531. }
  532. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  533. #if __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  534. (void) a;
  535. bad_arch();
  536. #else
  537. #pragma unroll
  538. for (int mask = 16; mask > 0; mask >>= 1) {
  539. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  540. }
  541. return a;
  542. #endif // __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  543. }
  544. static __device__ __forceinline__ float warp_reduce_max(float x) {
  545. #pragma unroll
  546. for (int mask = 16; mask > 0; mask >>= 1) {
  547. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  548. }
  549. return x;
  550. }
  551. static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  552. #if __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  553. (void) x;
  554. bad_arch();
  555. #else
  556. #pragma unroll
  557. for (int mask = 16; mask > 0; mask >>= 1) {
  558. x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  559. }
  560. return x;
  561. #endif // __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  562. }
  563. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  564. return b;
  565. GGML_UNUSED(a);
  566. }
  567. static __device__ __forceinline__ float op_add(const float a, const float b) {
  568. return a + b;
  569. }
  570. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  571. return a * b;
  572. }
  573. static __device__ __forceinline__ float op_div(const float a, const float b) {
  574. return a / b;
  575. }
  576. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  577. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  578. int ne0, int ne1, int ne2, int ne3,
  579. int ne10, int ne11, int ne12, int ne13,
  580. /*int s0, */ int s1, int s2, int s3,
  581. /*int s10,*/ int s11, int s12, int s13) {
  582. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  583. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  584. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  585. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  586. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  587. return;
  588. }
  589. const int i11 = i1 % ne11;
  590. const int i12 = i2 % ne12;
  591. const int i13 = i3 % ne13;
  592. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  593. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  594. const size_t i_dst = i_src0;
  595. const src0_t * src0_row = src0 + i_src0;
  596. const src1_t * src1_row = src1 + i_src1;
  597. dst_t * dst_row = dst + i_dst;
  598. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  599. const int i10 = i0 % ne10;
  600. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  601. }
  602. }
  603. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  604. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  605. int ne0, int ne1, int ne2, int ne3,
  606. int ne10, int ne11, int ne12, int ne13,
  607. /*int s0, */ int s1, int s2, int s3,
  608. /*int s10,*/ int s11, int s12, int s13) {
  609. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  610. const int i3 = i/(ne2*ne1*ne0);
  611. const int i2 = (i/(ne1*ne0)) % ne2;
  612. const int i1 = (i/ne0) % ne1;
  613. const int i0 = i % ne0;
  614. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  615. return;
  616. }
  617. const int i11 = i1 % ne11;
  618. const int i12 = i2 % ne12;
  619. const int i13 = i3 % ne13;
  620. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  621. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  622. const size_t i_dst = i_src0;
  623. const src0_t * src0_row = src0 + i_src0;
  624. const src1_t * src1_row = src1 + i_src1;
  625. dst_t * dst_row = dst + i_dst;
  626. const int i10 = i0 % ne10;
  627. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  628. }
  629. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  630. const int ne10, const int ne11, const int ne12,
  631. const int nb1, const int nb2, int offset) {
  632. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  633. if (i >= ne) {
  634. return;
  635. }
  636. int src1_idx = i - offset;
  637. int oz = src1_idx / nb2;
  638. int oy = (src1_idx - (oz * nb2)) / nb1;
  639. int ox = src1_idx % nb1;
  640. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  641. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  642. } else {
  643. dst[i] = x[i];
  644. }
  645. }
  646. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  647. const float GELU_COEF_A = 0.044715f;
  648. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  649. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  650. if (i >= k) {
  651. return;
  652. }
  653. float xi = x[i];
  654. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  655. }
  656. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  657. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  658. if (i >= k) {
  659. return;
  660. }
  661. dst[i] = x[i] / (1.0f + expf(-x[i]));
  662. }
  663. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  664. const float GELU_QUICK_COEF = -1.702f;
  665. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  666. if (i >= k) {
  667. return;
  668. }
  669. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  670. }
  671. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  672. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  673. if (i >= k) {
  674. return;
  675. }
  676. dst[i] = tanhf(x[i]);
  677. }
  678. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  679. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  680. if (i >= k) {
  681. return;
  682. }
  683. dst[i] = fmaxf(x[i], 0);
  684. }
  685. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  686. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  687. if (i >= k) {
  688. return;
  689. }
  690. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  691. }
  692. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  693. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  694. if (i >= k) {
  695. return;
  696. }
  697. dst[i] = x[i] * x[i];
  698. }
  699. template <int block_size>
  700. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  701. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  702. const int tid = threadIdx.x;
  703. float2 mean_var = make_float2(0.f, 0.f);
  704. for (int col = tid; col < ncols; col += block_size) {
  705. const float xi = x[row*ncols + col];
  706. mean_var.x += xi;
  707. mean_var.y += xi * xi;
  708. }
  709. // sum up partial sums
  710. mean_var = warp_reduce_sum(mean_var);
  711. if (block_size > WARP_SIZE) {
  712. __shared__ float2 s_sum[32];
  713. int warp_id = threadIdx.x / WARP_SIZE;
  714. int lane_id = threadIdx.x % WARP_SIZE;
  715. if (lane_id == 0) {
  716. s_sum[warp_id] = mean_var;
  717. }
  718. __syncthreads();
  719. mean_var = s_sum[lane_id];
  720. mean_var = warp_reduce_sum(mean_var);
  721. }
  722. const float mean = mean_var.x / ncols;
  723. const float var = mean_var.y / ncols - mean * mean;
  724. const float inv_std = rsqrtf(var + eps);
  725. for (int col = tid; col < ncols; col += block_size) {
  726. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  727. }
  728. }
  729. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  730. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  731. if (nidx >= ne0) {
  732. return;
  733. }
  734. // operation
  735. int offset_dst =
  736. nidx +
  737. blockIdx.y * ne0 +
  738. blockIdx.z * ne0 * gridDim.y;
  739. if (blockIdx.z < ne02) { // src0
  740. int offset_src =
  741. nidx +
  742. blockIdx.y * ne0 +
  743. blockIdx.z * ne0 * gridDim.y;
  744. dst[offset_dst] = x[offset_src];
  745. } else {
  746. int offset_src =
  747. nidx +
  748. blockIdx.y * ne0 +
  749. (blockIdx.z - ne02) * ne0 * gridDim.y;
  750. dst[offset_dst] = y[offset_src];
  751. }
  752. }
  753. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  754. int ne0 = ne00 * scale_factor;
  755. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  756. if (nidx >= ne0) {
  757. return;
  758. }
  759. // operation
  760. int i00 = nidx / scale_factor;
  761. int i01 = blockIdx.y / scale_factor;
  762. int offset_src =
  763. i00 +
  764. i01 * ne00 +
  765. blockIdx.z * nb02;
  766. int offset_dst =
  767. nidx +
  768. blockIdx.y * ne0 +
  769. blockIdx.z * ne0 * gridDim.y;
  770. dst[offset_dst] = x[offset_src];
  771. }
  772. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  773. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  774. if (nidx >= ne0) {
  775. return;
  776. }
  777. // operation
  778. int offset_dst =
  779. nidx +
  780. blockIdx.y * ne0 +
  781. blockIdx.z * ne0 * gridDim.y;
  782. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  783. int offset_src =
  784. nidx +
  785. blockIdx.y * ne00 +
  786. blockIdx.z * ne00 * ne01;
  787. dst[offset_dst] = x[offset_src];
  788. } else {
  789. dst[offset_dst] = 0.0f;
  790. }
  791. }
  792. template <int block_size>
  793. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  794. int start = blockIdx.x * group_size;
  795. int end = start + group_size;
  796. start += threadIdx.x;
  797. if (end >= ne_elements) {
  798. end = ne_elements;
  799. }
  800. float tmp = 0.0f; // partial sum for thread in warp
  801. for (int j = start; j < end; j += block_size) {
  802. tmp += x[j];
  803. }
  804. tmp = warp_reduce_sum(tmp);
  805. if (block_size > WARP_SIZE) {
  806. __shared__ float s_sum[32];
  807. int warp_id = threadIdx.x / WARP_SIZE;
  808. int lane_id = threadIdx.x % WARP_SIZE;
  809. if (lane_id == 0) {
  810. s_sum[warp_id] = tmp;
  811. }
  812. __syncthreads();
  813. tmp = s_sum[lane_id];
  814. tmp = warp_reduce_sum(tmp);
  815. }
  816. float mean = tmp / group_size;
  817. tmp = 0.0f;
  818. for (int j = start; j < end; j += block_size) {
  819. float xi = x[j] - mean;
  820. dst[j] = xi;
  821. tmp += xi * xi;
  822. }
  823. tmp = warp_reduce_sum(tmp);
  824. if (block_size > WARP_SIZE) {
  825. __shared__ float s_sum[32];
  826. int warp_id = threadIdx.x / WARP_SIZE;
  827. int lane_id = threadIdx.x % WARP_SIZE;
  828. if (lane_id == 0) {
  829. s_sum[warp_id] = tmp;
  830. }
  831. __syncthreads();
  832. tmp = s_sum[lane_id];
  833. tmp = warp_reduce_sum(tmp);
  834. }
  835. float variance = tmp / group_size;
  836. float scale = rsqrtf(variance + eps);
  837. for (int j = start; j < end; j += block_size) {
  838. dst[j] *= scale;
  839. }
  840. }
  841. template <int block_size>
  842. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  843. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  844. const int tid = threadIdx.x;
  845. float tmp = 0.0f; // partial sum for thread in warp
  846. for (int col = tid; col < ncols; col += block_size) {
  847. const float xi = x[row*ncols + col];
  848. tmp += xi * xi;
  849. }
  850. // sum up partial sums
  851. tmp = warp_reduce_sum(tmp);
  852. if (block_size > WARP_SIZE) {
  853. __shared__ float s_sum[32];
  854. int warp_id = threadIdx.x / WARP_SIZE;
  855. int lane_id = threadIdx.x % WARP_SIZE;
  856. if (lane_id == 0) {
  857. s_sum[warp_id] = tmp;
  858. }
  859. __syncthreads();
  860. tmp = s_sum[lane_id];
  861. tmp = warp_reduce_sum(tmp);
  862. }
  863. const float mean = tmp / ncols;
  864. const float scale = rsqrtf(mean + eps);
  865. for (int col = tid; col < ncols; col += block_size) {
  866. dst[row*ncols + col] = scale * x[row*ncols + col];
  867. }
  868. }
  869. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  870. const block_q4_0 * x = (const block_q4_0 *) vx;
  871. const dfloat d = x[ib].d;
  872. const int vui = x[ib].qs[iqs];
  873. v.x = vui & 0xF;
  874. v.y = vui >> 4;
  875. #ifdef GGML_CUDA_F16
  876. v = __hsub2(v, {8.0f, 8.0f});
  877. v = __hmul2(v, {d, d});
  878. #else
  879. v.x = (v.x - 8.0f) * d;
  880. v.y = (v.y - 8.0f) * d;
  881. #endif // GGML_CUDA_F16
  882. }
  883. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  884. const block_q4_1 * x = (const block_q4_1 *) vx;
  885. const dfloat d = __low2half(x[ib].dm);
  886. const dfloat m = __high2half(x[ib].dm);
  887. const int vui = x[ib].qs[iqs];
  888. v.x = vui & 0xF;
  889. v.y = vui >> 4;
  890. #ifdef GGML_CUDA_F16
  891. v = __hmul2(v, {d, d});
  892. v = __hadd2(v, {m, m});
  893. #else
  894. v.x = (v.x * d) + m;
  895. v.y = (v.y * d) + m;
  896. #endif // GGML_CUDA_F16
  897. }
  898. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  899. const block_q5_0 * x = (const block_q5_0 *) vx;
  900. const dfloat d = x[ib].d;
  901. uint32_t qh;
  902. memcpy(&qh, x[ib].qh, sizeof(qh));
  903. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  904. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  905. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  906. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  907. #ifdef GGML_CUDA_F16
  908. v = __hsub2(v, {16.0f, 16.0f});
  909. v = __hmul2(v, {d, d});
  910. #else
  911. v.x = (v.x - 16.0f) * d;
  912. v.y = (v.y - 16.0f) * d;
  913. #endif // GGML_CUDA_F16
  914. }
  915. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  916. const block_q5_1 * x = (const block_q5_1 *) vx;
  917. const dfloat d = __low2half(x[ib].dm);
  918. const dfloat m = __high2half(x[ib].dm);
  919. uint32_t qh;
  920. memcpy(&qh, x[ib].qh, sizeof(qh));
  921. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  922. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  923. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  924. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  925. #ifdef GGML_CUDA_F16
  926. v = __hmul2(v, {d, d});
  927. v = __hadd2(v, {m, m});
  928. #else
  929. v.x = (v.x * d) + m;
  930. v.y = (v.y * d) + m;
  931. #endif // GGML_CUDA_F16
  932. }
  933. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  934. const block_q8_0 * x = (const block_q8_0 *) vx;
  935. const dfloat d = x[ib].d;
  936. v.x = x[ib].qs[iqs + 0];
  937. v.y = x[ib].qs[iqs + 1];
  938. #ifdef GGML_CUDA_F16
  939. v = __hmul2(v, {d, d});
  940. #else
  941. v.x *= d;
  942. v.y *= d;
  943. #endif // GGML_CUDA_F16
  944. }
  945. //================================== k-quants
  946. template<typename dst_t>
  947. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  948. const int i = blockIdx.x;
  949. const block_q2_K * x = (const block_q2_K *) vx;
  950. const int tid = threadIdx.x;
  951. #if QK_K == 256
  952. const int n = tid/32;
  953. const int l = tid - 32*n;
  954. const int is = 8*n + l/16;
  955. const uint8_t q = x[i].qs[32*n + l];
  956. dst_t * y = yy + i*QK_K + 128*n;
  957. float dall = __low2half(x[i].dm);
  958. float dmin = __high2half(x[i].dm);
  959. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  960. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  961. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  962. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  963. #else
  964. const int is = tid/16; // 0 or 1
  965. const int il = tid%16; // 0...15
  966. const uint8_t q = x[i].qs[il] >> (2*is);
  967. dst_t * y = yy + i*QK_K + 16*is + il;
  968. float dall = __low2half(x[i].dm);
  969. float dmin = __high2half(x[i].dm);
  970. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  971. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  972. #endif
  973. }
  974. template<typename dst_t>
  975. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  976. const int i = blockIdx.x;
  977. const block_q3_K * x = (const block_q3_K *) vx;
  978. #if QK_K == 256
  979. const int r = threadIdx.x/4;
  980. const int tid = r/2;
  981. const int is0 = r%2;
  982. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  983. const int n = tid / 4;
  984. const int j = tid - 4*n;
  985. uint8_t m = 1 << (4*n + j);
  986. int is = 8*n + 2*j + is0;
  987. int shift = 2*j;
  988. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  989. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  990. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  991. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  992. float d_all = x[i].d;
  993. float dl = d_all * (us - 32);
  994. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  995. const uint8_t * q = x[i].qs + 32*n;
  996. const uint8_t * hm = x[i].hmask;
  997. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  998. #else
  999. const int tid = threadIdx.x;
  1000. const int is = tid/16; // 0 or 1
  1001. const int il = tid%16; // 0...15
  1002. const int im = il/8; // 0...1
  1003. const int in = il%8; // 0...7
  1004. dst_t * y = yy + i*QK_K + 16*is + il;
  1005. const uint8_t q = x[i].qs[il] >> (2*is);
  1006. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1007. const float d = (float)x[i].d;
  1008. if (is == 0) {
  1009. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1010. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1011. } else {
  1012. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1013. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1014. }
  1015. #endif
  1016. }
  1017. #if QK_K == 256
  1018. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1019. if (j < 4) {
  1020. d = q[j] & 63; m = q[j + 4] & 63;
  1021. } else {
  1022. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1023. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1024. }
  1025. }
  1026. #endif
  1027. template<typename dst_t>
  1028. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1029. const block_q4_K * x = (const block_q4_K *) vx;
  1030. const int i = blockIdx.x;
  1031. #if QK_K == 256
  1032. // assume 32 threads
  1033. const int tid = threadIdx.x;
  1034. const int il = tid/8;
  1035. const int ir = tid%8;
  1036. const int is = 2*il;
  1037. const int n = 4;
  1038. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1039. const float dall = __low2half(x[i].dm);
  1040. const float dmin = __high2half(x[i].dm);
  1041. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1042. uint8_t sc, m;
  1043. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1044. const float d1 = dall * sc; const float m1 = dmin * m;
  1045. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1046. const float d2 = dall * sc; const float m2 = dmin * m;
  1047. for (int l = 0; l < n; ++l) {
  1048. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1049. y[l +32] = d2 * (q[l] >> 4) - m2;
  1050. }
  1051. #else
  1052. const int tid = threadIdx.x;
  1053. const uint8_t * q = x[i].qs;
  1054. dst_t * y = yy + i*QK_K;
  1055. const float d = (float)x[i].dm[0];
  1056. const float m = (float)x[i].dm[1];
  1057. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1058. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1059. #endif
  1060. }
  1061. template<typename dst_t>
  1062. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1063. const block_q5_K * x = (const block_q5_K *) vx;
  1064. const int i = blockIdx.x;
  1065. #if QK_K == 256
  1066. // assume 64 threads - this is very slightly better than the one below
  1067. const int tid = threadIdx.x;
  1068. const int il = tid/16; // il is in 0...3
  1069. const int ir = tid%16; // ir is in 0...15
  1070. const int is = 2*il; // is is in 0...6
  1071. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1072. const float dall = __low2half(x[i].dm);
  1073. const float dmin = __high2half(x[i].dm);
  1074. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1075. const uint8_t * qh = x[i].qh + 2*ir;
  1076. uint8_t sc, m;
  1077. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1078. const float d1 = dall * sc; const float m1 = dmin * m;
  1079. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1080. const float d2 = dall * sc; const float m2 = dmin * m;
  1081. uint8_t hm = 1 << (2*il);
  1082. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1083. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1084. hm <<= 1;
  1085. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1086. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1087. #else
  1088. const int tid = threadIdx.x;
  1089. const uint8_t q = x[i].qs[tid];
  1090. const int im = tid/8; // 0...3
  1091. const int in = tid%8; // 0...7
  1092. const int is = tid/16; // 0 or 1
  1093. const uint8_t h = x[i].qh[in] >> im;
  1094. const float d = x[i].d;
  1095. dst_t * y = yy + i*QK_K + tid;
  1096. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1097. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1098. #endif
  1099. }
  1100. template<typename dst_t>
  1101. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1102. const block_q6_K * x = (const block_q6_K *) vx;
  1103. const int i = blockIdx.x;
  1104. #if QK_K == 256
  1105. // assume 64 threads - this is very slightly better than the one below
  1106. const int tid = threadIdx.x;
  1107. const int ip = tid/32; // ip is 0 or 1
  1108. const int il = tid - 32*ip; // 0...32
  1109. const int is = 8*ip + il/16;
  1110. dst_t * y = yy + i*QK_K + 128*ip + il;
  1111. const float d = x[i].d;
  1112. const uint8_t * ql = x[i].ql + 64*ip + il;
  1113. const uint8_t qh = x[i].qh[32*ip + il];
  1114. const int8_t * sc = x[i].scales + is;
  1115. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1116. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1117. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1118. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1119. #else
  1120. // assume 32 threads
  1121. const int tid = threadIdx.x;
  1122. const int ip = tid/16; // 0 or 1
  1123. const int il = tid - 16*ip; // 0...15
  1124. dst_t * y = yy + i*QK_K + 16*ip + il;
  1125. const float d = x[i].d;
  1126. const uint8_t ql = x[i].ql[16*ip + il];
  1127. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1128. const int8_t * sc = x[i].scales;
  1129. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1130. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1131. #endif
  1132. }
  1133. static const __device__ uint64_t iq2xxs_grid[256] = {
  1134. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1135. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1136. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1137. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1138. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1139. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1140. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1141. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1142. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1143. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1144. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1145. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1146. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1147. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1148. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1149. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1150. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1151. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1152. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1153. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1154. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1155. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1156. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1157. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1158. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1159. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1160. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1161. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1162. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1163. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1164. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1165. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1166. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1167. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1168. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1169. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1170. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1171. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1172. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1173. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1174. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1175. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1176. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1177. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1178. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1179. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1180. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1181. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1182. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1183. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1184. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1185. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1186. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1187. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1188. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1189. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1190. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1191. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1192. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1193. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1194. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1195. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1196. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1197. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1198. };
  1199. static const __device__ uint64_t iq2xs_grid[512] = {
  1200. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1201. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1202. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1203. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1204. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1205. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  1206. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  1207. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  1208. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  1209. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  1210. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  1211. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  1212. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  1213. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  1214. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  1215. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  1216. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  1217. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  1218. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  1219. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  1220. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  1221. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  1222. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  1223. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  1224. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  1225. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  1226. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  1227. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  1228. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  1229. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  1230. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  1231. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  1232. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  1233. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  1234. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  1235. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  1236. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  1237. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  1238. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  1239. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  1240. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  1241. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  1242. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  1243. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  1244. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  1245. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  1246. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  1247. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  1248. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  1249. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  1250. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  1251. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  1252. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  1253. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  1254. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  1255. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  1256. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  1257. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  1258. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  1259. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  1260. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  1261. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  1262. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  1263. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  1264. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  1265. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  1266. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  1267. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  1268. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  1269. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  1270. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  1271. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  1272. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  1273. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  1274. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  1275. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  1276. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  1277. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  1278. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  1279. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  1280. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  1281. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  1282. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  1283. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  1284. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  1285. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  1286. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  1287. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  1288. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  1289. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  1290. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  1291. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  1292. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  1293. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  1294. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  1295. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  1296. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  1297. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  1298. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  1299. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  1300. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  1301. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  1302. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  1303. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  1304. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  1305. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  1306. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  1307. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  1308. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  1309. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  1310. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  1311. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  1312. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  1313. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  1314. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  1315. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  1316. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  1317. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  1318. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  1319. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  1320. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  1321. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  1322. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  1323. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  1324. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  1325. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  1326. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  1327. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  1328. };
  1329. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1330. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1331. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1332. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1333. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1334. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1335. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1336. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1337. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1338. };
  1339. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  1340. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1341. switch (type) {
  1342. case GGML_TYPE_Q4_0:
  1343. case GGML_TYPE_Q4_1:
  1344. case GGML_TYPE_Q5_0:
  1345. case GGML_TYPE_Q5_1:
  1346. case GGML_TYPE_Q8_0:
  1347. case GGML_TYPE_Q2_K:
  1348. case GGML_TYPE_Q3_K:
  1349. case GGML_TYPE_Q4_K:
  1350. case GGML_TYPE_Q5_K:
  1351. case GGML_TYPE_Q6_K:
  1352. return true;
  1353. default:
  1354. return false;
  1355. }
  1356. }
  1357. template<typename dst_t>
  1358. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1359. const int i = blockIdx.x;
  1360. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1361. const int tid = threadIdx.x;
  1362. #if QK_K == 256
  1363. const int il = tid/8; // 0...3
  1364. const int ib = tid%8; // 0...7
  1365. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1366. const uint16_t * q2 = x[i].qs + 4*ib;
  1367. const uint8_t * aux8 = (const uint8_t *)q2;
  1368. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  1369. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1370. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1371. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1372. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1373. #else
  1374. assert(false);
  1375. #endif
  1376. }
  1377. template<typename dst_t>
  1378. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1379. const int i = blockIdx.x;
  1380. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  1381. const int tid = threadIdx.x;
  1382. #if QK_K == 256
  1383. const int il = tid/8; // 0...3
  1384. const int ib = tid%8; // 0...7
  1385. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1386. const uint16_t * q2 = x[i].qs + 4*ib;
  1387. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  1388. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1389. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  1390. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1391. #else
  1392. assert(false);
  1393. #endif
  1394. }
  1395. 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) {
  1396. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1397. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1398. if (row > nrows) return;
  1399. const int num_blocks_per_row = ncols / QK_K;
  1400. const int ib0 = row*num_blocks_per_row;
  1401. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1402. float tmp = 0; // partial sum for thread in warp
  1403. #if QK_K == 256
  1404. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1405. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1406. const int step = 16/K_QUANTS_PER_ITERATION;
  1407. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1408. const int in = tid - step*im; // 0...15 or 0...7
  1409. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1410. const int q_offset = 32*im + l0;
  1411. const int s_offset = 8*im;
  1412. const int y_offset = 128*im + l0;
  1413. uint32_t aux[4];
  1414. const uint8_t * d = (const uint8_t *)aux;
  1415. const uint8_t * m = (const uint8_t *)(aux + 2);
  1416. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1417. const float * y = yy + i * QK_K + y_offset;
  1418. const uint8_t * q = x[i].qs + q_offset;
  1419. const float dall = __low2half(x[i].dm);
  1420. const float dmin = __high2half(x[i].dm);
  1421. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1422. aux[0] = a[0] & 0x0f0f0f0f;
  1423. aux[1] = a[1] & 0x0f0f0f0f;
  1424. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1425. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1426. float sum1 = 0, sum2 = 0;
  1427. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1428. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1429. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1430. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1431. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1432. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1433. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1434. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1435. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1436. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1437. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1438. }
  1439. tmp += dall * sum1 - dmin * sum2;
  1440. }
  1441. #else
  1442. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1443. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1444. const int offset = tid * K_QUANTS_PER_ITERATION;
  1445. uint32_t uaux[2];
  1446. const uint8_t * d = (const uint8_t *)uaux;
  1447. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1448. const float * y = yy + i * QK_K + offset;
  1449. const uint8_t * q = x[i].qs + offset;
  1450. const uint32_t * s = (const uint32_t *)x[i].scales;
  1451. uaux[0] = s[0] & 0x0f0f0f0f;
  1452. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1453. const float2 dall = __half22float2(x[i].dm);
  1454. float sum1 = 0, sum2 = 0;
  1455. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1456. const uint8_t ql = q[l];
  1457. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1458. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1459. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1460. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1461. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1462. }
  1463. tmp += dall.x * sum1 - dall.y * sum2;
  1464. }
  1465. #endif
  1466. // sum up partial sums and write back result
  1467. #pragma unroll
  1468. for (int mask = 16; mask > 0; mask >>= 1) {
  1469. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1470. }
  1471. if (threadIdx.x == 0) {
  1472. dst[row] = tmp;
  1473. }
  1474. }
  1475. 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) {
  1476. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1477. if (row > nrows) return;
  1478. const int num_blocks_per_row = ncols / QK_K;
  1479. const int ib0 = row*num_blocks_per_row;
  1480. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1481. float tmp = 0; // partial sum for thread in warp
  1482. #if QK_K == 256
  1483. const uint16_t kmask1 = 0x0303;
  1484. const uint16_t kmask2 = 0x0f0f;
  1485. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1486. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1487. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1488. const int step = 16/K_QUANTS_PER_ITERATION;
  1489. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1490. const int in = tid - step*im; // 0....15 or 0...7
  1491. const uint8_t m = 1 << (4*im);
  1492. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1493. const int q_offset = 32*im + l0;
  1494. const int y_offset = 128*im + l0;
  1495. uint16_t utmp[4];
  1496. const int8_t * s = (const int8_t *)utmp;
  1497. const uint16_t s_shift = 4*im;
  1498. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1499. const float * y = yy + i * QK_K + y_offset;
  1500. const uint8_t * q = x[i].qs + q_offset;
  1501. const uint8_t * h = x[i].hmask + l0;
  1502. const uint16_t * a = (const uint16_t *)x[i].scales;
  1503. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1504. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1505. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1506. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1507. const float d = x[i].d;
  1508. float sum = 0;
  1509. for (int l = 0; l < n; ++l) {
  1510. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1511. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1512. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1513. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1514. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1515. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1516. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1517. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1518. }
  1519. tmp += d * sum;
  1520. }
  1521. #else
  1522. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1523. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1524. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1525. const int in = offset/8; // 0 or 1
  1526. const int im = offset%8; // 0...7
  1527. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1528. const float * y = yy + i * QK_K + offset;
  1529. const uint8_t * q = x[i].qs + offset;
  1530. const uint8_t * s = x[i].scales;
  1531. const float dall = (float)x[i].d;
  1532. float sum = 0;
  1533. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1534. const uint8_t hl = x[i].hmask[im+l] >> in;
  1535. const uint8_t ql = q[l];
  1536. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1537. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1538. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1539. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1540. }
  1541. tmp += sum;
  1542. }
  1543. #endif
  1544. // sum up partial sums and write back result
  1545. #pragma unroll
  1546. for (int mask = 16; mask > 0; mask >>= 1) {
  1547. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1548. }
  1549. if (threadIdx.x == 0) {
  1550. dst[row] = tmp;
  1551. }
  1552. }
  1553. 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) {
  1554. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1555. if (row > nrows) return;
  1556. const int num_blocks_per_row = ncols / QK_K;
  1557. const int ib0 = row*num_blocks_per_row;
  1558. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1559. #if QK_K == 256
  1560. const uint16_t kmask1 = 0x3f3f;
  1561. const uint16_t kmask2 = 0x0f0f;
  1562. const uint16_t kmask3 = 0xc0c0;
  1563. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1564. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1565. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1566. const int il = tid/step; // 0...3
  1567. const int ir = tid - step*il; // 0...7 or 0...3
  1568. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1569. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1570. const int in = il%2;
  1571. const int l0 = n*(2*ir + in);
  1572. const int q_offset = 32*im + l0;
  1573. const int y_offset = 64*im + l0;
  1574. uint16_t aux[4];
  1575. const uint8_t * sc = (const uint8_t *)aux;
  1576. #if K_QUANTS_PER_ITERATION == 2
  1577. uint32_t q32[4];
  1578. const uint8_t * q4 = (const uint8_t *)q32;
  1579. #else
  1580. uint16_t q16[4];
  1581. const uint8_t * q4 = (const uint8_t *)q16;
  1582. #endif
  1583. float tmp = 0; // partial sum for thread in warp
  1584. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1585. const float * y1 = yy + i*QK_K + y_offset;
  1586. const float * y2 = y1 + 128;
  1587. const float dall = __low2half(x[i].dm);
  1588. const float dmin = __high2half(x[i].dm);
  1589. const uint16_t * a = (const uint16_t *)x[i].scales;
  1590. aux[0] = a[im+0] & kmask1;
  1591. aux[1] = a[im+2] & kmask1;
  1592. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1593. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1594. #if K_QUANTS_PER_ITERATION == 2
  1595. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1596. const uint32_t * q2 = q1 + 16;
  1597. q32[0] = q1[0] & 0x0f0f0f0f;
  1598. q32[1] = q1[0] & 0xf0f0f0f0;
  1599. q32[2] = q2[0] & 0x0f0f0f0f;
  1600. q32[3] = q2[0] & 0xf0f0f0f0;
  1601. float4 s = {0.f, 0.f, 0.f, 0.f};
  1602. float smin = 0;
  1603. for (int l = 0; l < 4; ++l) {
  1604. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1605. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1606. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1607. }
  1608. 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;
  1609. #else
  1610. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1611. const uint16_t * q2 = q1 + 32;
  1612. q16[0] = q1[0] & 0x0f0f;
  1613. q16[1] = q1[0] & 0xf0f0;
  1614. q16[2] = q2[0] & 0x0f0f;
  1615. q16[3] = q2[0] & 0xf0f0;
  1616. float4 s = {0.f, 0.f, 0.f, 0.f};
  1617. float smin = 0;
  1618. for (int l = 0; l < 2; ++l) {
  1619. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1620. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1621. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1622. }
  1623. 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;
  1624. #endif
  1625. }
  1626. #else
  1627. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1628. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1629. const int step = tid * K_QUANTS_PER_ITERATION;
  1630. uint16_t aux16[2];
  1631. const uint8_t * s = (const uint8_t *)aux16;
  1632. float tmp = 0;
  1633. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1634. const uint8_t * q = x[i].qs + step;
  1635. const float * y = yy + i*QK_K + step;
  1636. const uint16_t * a = (const uint16_t *)x[i].scales;
  1637. aux16[0] = a[0] & 0x0f0f;
  1638. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1639. const float d = (float)x[i].dm[0];
  1640. const float m = (float)x[i].dm[1];
  1641. float sum = 0.f;
  1642. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1643. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1644. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1645. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1646. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1647. }
  1648. tmp += sum;
  1649. }
  1650. #endif
  1651. // sum up partial sums and write back result
  1652. #pragma unroll
  1653. for (int mask = 16; mask > 0; mask >>= 1) {
  1654. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1655. }
  1656. if (tid == 0) {
  1657. dst[row] = tmp;
  1658. }
  1659. }
  1660. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1661. const int row = blockIdx.x;
  1662. const int num_blocks_per_row = ncols / QK_K;
  1663. const int ib0 = row*num_blocks_per_row;
  1664. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1665. float tmp = 0; // partial sum for thread in warp
  1666. #if QK_K == 256
  1667. const uint16_t kmask1 = 0x3f3f;
  1668. const uint16_t kmask2 = 0x0f0f;
  1669. const uint16_t kmask3 = 0xc0c0;
  1670. const int tid = threadIdx.x/2; // 0...15
  1671. const int ix = threadIdx.x%2;
  1672. const int il = tid/4; // 0...3
  1673. const int ir = tid - 4*il;// 0...3
  1674. const int n = 2;
  1675. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1676. const int in = il%2;
  1677. const int l0 = n*(2*ir + in);
  1678. const int q_offset = 32*im + l0;
  1679. const int y_offset = 64*im + l0;
  1680. const uint8_t hm1 = 1 << (2*im);
  1681. const uint8_t hm2 = hm1 << 4;
  1682. uint16_t aux[4];
  1683. const uint8_t * sc = (const uint8_t *)aux;
  1684. uint16_t q16[8];
  1685. const uint8_t * q4 = (const uint8_t *)q16;
  1686. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1687. const uint8_t * ql1 = x[i].qs + q_offset;
  1688. const uint8_t * qh = x[i].qh + l0;
  1689. const float * y1 = yy + i*QK_K + y_offset;
  1690. const float * y2 = y1 + 128;
  1691. const float dall = __low2half(x[i].dm);
  1692. const float dmin = __high2half(x[i].dm);
  1693. const uint16_t * a = (const uint16_t *)x[i].scales;
  1694. aux[0] = a[im+0] & kmask1;
  1695. aux[1] = a[im+2] & kmask1;
  1696. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1697. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1698. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1699. float smin = 0;
  1700. const uint16_t * q1 = (const uint16_t *)ql1;
  1701. const uint16_t * q2 = q1 + 32;
  1702. q16[0] = q1[0] & 0x0f0f;
  1703. q16[1] = q1[8] & 0x0f0f;
  1704. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1705. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1706. q16[4] = q2[0] & 0x0f0f;
  1707. q16[5] = q2[8] & 0x0f0f;
  1708. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1709. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1710. for (int l = 0; l < n; ++l) {
  1711. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1712. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1713. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1714. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1715. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1716. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1717. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1718. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1719. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1720. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1721. }
  1722. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1723. }
  1724. #else
  1725. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1726. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1727. const int step = tid * K_QUANTS_PER_ITERATION;
  1728. const int im = step/8;
  1729. const int in = step%8;
  1730. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1731. const uint8_t * q = x[i].qs + step;
  1732. const int8_t * s = x[i].scales;
  1733. const float * y = yy + i*QK_K + step;
  1734. const float d = x[i].d;
  1735. float sum = 0.f;
  1736. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1737. const uint8_t h = x[i].qh[in+j] >> im;
  1738. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1739. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1740. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1741. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1742. }
  1743. tmp += sum;
  1744. }
  1745. #endif
  1746. // sum up partial sums and write back result
  1747. #pragma unroll
  1748. for (int mask = 16; mask > 0; mask >>= 1) {
  1749. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1750. }
  1751. if (threadIdx.x == 0) {
  1752. dst[row] = tmp;
  1753. }
  1754. }
  1755. 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) {
  1756. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1757. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1758. if (row > nrows) return;
  1759. const int num_blocks_per_row = ncols / QK_K;
  1760. const int ib0 = row*num_blocks_per_row;
  1761. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1762. #if QK_K == 256
  1763. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1764. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1765. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1766. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1767. const int in = tid - step*im; // 0...15 or 0...7
  1768. #if K_QUANTS_PER_ITERATION == 1
  1769. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1770. const int is = 0;
  1771. #else
  1772. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1773. const int is = in / 4;
  1774. #endif
  1775. const int ql_offset = 64*im + l0;
  1776. const int qh_offset = 32*im + l0;
  1777. const int s_offset = 8*im + is;
  1778. const int y_offset = 128*im + l0;
  1779. float tmp = 0; // partial sum for thread in warp
  1780. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1781. const float * y = yy + i * QK_K + y_offset;
  1782. const uint8_t * ql = x[i].ql + ql_offset;
  1783. const uint8_t * qh = x[i].qh + qh_offset;
  1784. const int8_t * s = x[i].scales + s_offset;
  1785. const float d = x[i].d;
  1786. #if K_QUANTS_PER_ITERATION == 1
  1787. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1788. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1789. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1790. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1791. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1792. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1793. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1794. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1795. tmp += sum;
  1796. #else
  1797. float sum = 0;
  1798. for (int l = 0; l < 4; ++l) {
  1799. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1800. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1801. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1802. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1803. }
  1804. tmp += sum;
  1805. #endif
  1806. }
  1807. #else
  1808. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1809. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1810. const int step = tid * K_QUANTS_PER_ITERATION;
  1811. float tmp = 0; // partial sum for thread in warp
  1812. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1813. const float * y = yy + i * QK_K + step;
  1814. const uint8_t * ql = x[i].ql + step;
  1815. const uint8_t * qh = x[i].qh + step;
  1816. const int8_t * s = x[i].scales;
  1817. const float d = x[i+0].d;
  1818. float sum = 0;
  1819. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1820. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1821. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1822. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1823. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1824. }
  1825. tmp += sum;
  1826. }
  1827. #endif
  1828. // sum up partial sums and write back result
  1829. #pragma unroll
  1830. for (int mask = 16; mask > 0; mask >>= 1) {
  1831. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1832. }
  1833. if (tid == 0) {
  1834. dst[row] = tmp;
  1835. }
  1836. }
  1837. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1838. const half * x = (const half *) vx;
  1839. // automatic half -> float type cast if dfloat == float
  1840. v.x = x[ib + iqs + 0];
  1841. v.y = x[ib + iqs + 1];
  1842. }
  1843. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1844. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1845. if (ix >= kx_padded) {
  1846. return;
  1847. }
  1848. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1849. const int i_padded = iy*kx_padded + ix;
  1850. block_q8_1 * y = (block_q8_1 *) vy;
  1851. const int ib = i_padded / QK8_1; // block index
  1852. const int iqs = i_padded % QK8_1; // quant index
  1853. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1854. float amax = fabsf(xi);
  1855. float sum = xi;
  1856. #pragma unroll
  1857. for (int mask = 16; mask > 0; mask >>= 1) {
  1858. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1859. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1860. }
  1861. const float d = amax / 127;
  1862. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1863. y[ib].qs[iqs] = q;
  1864. if (iqs > 0) {
  1865. return;
  1866. }
  1867. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1868. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1869. }
  1870. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1871. static __global__ void k_get_rows(
  1872. const void * src0, const int32_t * src1, dst_t * dst,
  1873. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1874. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1875. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1876. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1877. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1878. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1879. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1880. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1881. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1882. if (i00 >= ne00) {
  1883. return;
  1884. }
  1885. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1886. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1887. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1888. const int ib = i00/qk; // block index
  1889. const int iqs = (i00%qk)/qr; // quant index
  1890. const int iybs = i00 - i00%qk; // dst block start index
  1891. const int y_offset = qr == 1 ? 1 : qk/2;
  1892. // dequantize
  1893. dfloat2 v;
  1894. dequantize_kernel(src0_row, ib, iqs, v);
  1895. dst_row[iybs + iqs + 0] = v.x;
  1896. dst_row[iybs + iqs + y_offset] = v.y;
  1897. }
  1898. template<typename src0_t, typename dst_t>
  1899. static __global__ void k_get_rows_float(
  1900. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1901. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1902. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1903. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1904. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1905. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1906. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1907. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1908. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1909. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1910. if (i00 >= ne00) {
  1911. return;
  1912. }
  1913. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1914. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1915. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1916. dst_row[i00] = src0_row[i00];
  1917. }
  1918. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1919. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1920. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  1921. if (i >= k) {
  1922. return;
  1923. }
  1924. const int ib = i/qk; // block index
  1925. const int iqs = (i%qk)/qr; // quant index
  1926. const int iybs = i - i%qk; // y block start index
  1927. const int y_offset = qr == 1 ? 1 : qk/2;
  1928. // dequantize
  1929. dfloat2 v;
  1930. dequantize_kernel(vx, ib, iqs, v);
  1931. y[iybs + iqs + 0] = v.x;
  1932. y[iybs + iqs + y_offset] = v.y;
  1933. }
  1934. template <typename src_t, typename dst_t>
  1935. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1936. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1937. if (i >= k) {
  1938. return;
  1939. }
  1940. const src_t * x = (src_t *) vx;
  1941. y[i] = x[i];
  1942. }
  1943. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1944. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1945. #define VDR_Q4_0_Q8_1_MMVQ 2
  1946. #define VDR_Q4_0_Q8_1_MMQ 4
  1947. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1948. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1949. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1950. int sumi = 0;
  1951. #pragma unroll
  1952. for (int i = 0; i < vdr; ++i) {
  1953. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1954. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1955. // SIMD dot product of quantized values
  1956. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1957. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1958. }
  1959. const float2 ds8f = __half22float2(ds8);
  1960. // second part effectively subtracts 8 from each quant value
  1961. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1962. #else
  1963. bad_arch();
  1964. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1965. }
  1966. #define VDR_Q4_1_Q8_1_MMVQ 2
  1967. #define VDR_Q4_1_Q8_1_MMQ 4
  1968. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1969. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1970. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1971. int sumi = 0;
  1972. #pragma unroll
  1973. for (int i = 0; i < vdr; ++i) {
  1974. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1975. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1976. // SIMD dot product of quantized values
  1977. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1978. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1979. }
  1980. #ifdef GGML_CUDA_F16
  1981. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1982. const float d4d8 = tmp.x;
  1983. const float m4s8 = tmp.y;
  1984. #else
  1985. const float2 dm4f = __half22float2(dm4);
  1986. const float2 ds8f = __half22float2(ds8);
  1987. const float d4d8 = dm4f.x * ds8f.x;
  1988. const float m4s8 = dm4f.y * ds8f.y;
  1989. #endif // GGML_CUDA_F16
  1990. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1991. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1992. #else
  1993. bad_arch();
  1994. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1995. }
  1996. #define VDR_Q5_0_Q8_1_MMVQ 2
  1997. #define VDR_Q5_0_Q8_1_MMQ 4
  1998. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1999. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2000. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2001. int sumi = 0;
  2002. #pragma unroll
  2003. for (int i = 0; i < vdr; ++i) {
  2004. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2005. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2006. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2007. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2008. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2009. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2010. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2011. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2012. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2013. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2014. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2015. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2016. }
  2017. const float2 ds8f = __half22float2(ds8);
  2018. // second part effectively subtracts 16 from each quant value
  2019. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2020. #else
  2021. bad_arch();
  2022. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2023. }
  2024. #define VDR_Q5_1_Q8_1_MMVQ 2
  2025. #define VDR_Q5_1_Q8_1_MMQ 4
  2026. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2027. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2028. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2029. int sumi = 0;
  2030. #pragma unroll
  2031. for (int i = 0; i < vdr; ++i) {
  2032. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2033. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2034. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2035. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2036. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2037. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2038. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2039. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2040. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2041. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2042. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2043. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2044. }
  2045. #ifdef GGML_CUDA_F16
  2046. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2047. const float d5d8 = tmp.x;
  2048. const float m5s8 = tmp.y;
  2049. #else
  2050. const float2 dm5f = __half22float2(dm5);
  2051. const float2 ds8f = __half22float2(ds8);
  2052. const float d5d8 = dm5f.x * ds8f.x;
  2053. const float m5s8 = dm5f.y * ds8f.y;
  2054. #endif // GGML_CUDA_F16
  2055. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2056. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2057. #else
  2058. bad_arch();
  2059. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2060. }
  2061. #define VDR_Q8_0_Q8_1_MMVQ 2
  2062. #define VDR_Q8_0_Q8_1_MMQ 8
  2063. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2064. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2065. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2066. int sumi = 0;
  2067. #pragma unroll
  2068. for (int i = 0; i < vdr; ++i) {
  2069. // SIMD dot product of quantized values
  2070. sumi = __dp4a(v[i], u[i], sumi);
  2071. }
  2072. return d8_0*d8_1 * sumi;
  2073. #else
  2074. bad_arch();
  2075. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2076. }
  2077. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2078. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2079. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2080. int sumi = 0;
  2081. #pragma unroll
  2082. for (int i = 0; i < vdr; ++i) {
  2083. // SIMD dot product of quantized values
  2084. sumi = __dp4a(v[i], u[i], sumi);
  2085. }
  2086. #ifdef GGML_CUDA_F16
  2087. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2088. const float d8d8 = tmp.x;
  2089. const float m8s8 = tmp.y;
  2090. #else
  2091. const float2 dm8f = __half22float2(dm8);
  2092. const float2 ds8f = __half22float2(ds8);
  2093. const float d8d8 = dm8f.x * ds8f.x;
  2094. const float m8s8 = dm8f.y * ds8f.y;
  2095. #endif // GGML_CUDA_F16
  2096. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2097. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2098. #else
  2099. bad_arch();
  2100. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2101. }
  2102. #define VDR_Q2_K_Q8_1_MMVQ 1
  2103. #define VDR_Q2_K_Q8_1_MMQ 2
  2104. // contiguous v/x values
  2105. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2106. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2107. const half2 & dm2, const float * __restrict__ d8) {
  2108. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2109. float sumf_d = 0.0f;
  2110. float sumf_m = 0.0f;
  2111. #pragma unroll
  2112. for (int i = 0; i < QR2_K; ++i) {
  2113. const int sc = scales[2*i];
  2114. const int vi = (v >> (2*i)) & 0x03030303;
  2115. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2116. // fill int with 4x m
  2117. int m = sc >> 4;
  2118. m |= m << 8;
  2119. m |= m << 16;
  2120. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2121. }
  2122. const float2 dm2f = __half22float2(dm2);
  2123. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2124. #else
  2125. bad_arch();
  2126. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2127. }
  2128. // contiguous u/y values
  2129. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2130. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2131. const half2 & dm2, const float & d8) {
  2132. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2133. int sumi_d = 0;
  2134. int sumi_m = 0;
  2135. #pragma unroll
  2136. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2137. int sumi_d_sc = 0;
  2138. const int sc = scales[i0 / (QI8_1/2)];
  2139. // fill int with 4x m
  2140. int m = sc >> 4;
  2141. m |= m << 8;
  2142. m |= m << 16;
  2143. #pragma unroll
  2144. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2145. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2146. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2147. }
  2148. sumi_d += sumi_d_sc * (sc & 0xF);
  2149. }
  2150. const float2 dm2f = __half22float2(dm2);
  2151. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2152. #else
  2153. bad_arch();
  2154. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2155. }
  2156. #define VDR_Q3_K_Q8_1_MMVQ 1
  2157. #define VDR_Q3_K_Q8_1_MMQ 2
  2158. // contiguous v/x values
  2159. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2160. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2161. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2162. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2163. float sumf = 0.0f;
  2164. #pragma unroll
  2165. for (int i = 0; i < QR3_K; ++i) {
  2166. const int isc = scale_offset + 2*i;
  2167. const int isc_low = isc % (QK_K/32);
  2168. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2169. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2170. const int isc_high = isc % (QK_K/64);
  2171. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2172. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2173. const int sc = (sc_low | sc_high) - 32;
  2174. const int vil = (vl >> (2*i)) & 0x03030303;
  2175. const int vih = ((vh >> i) << 2) & 0x04040404;
  2176. const int vi = __vsubss4(vil, vih);
  2177. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2178. }
  2179. return d3 * sumf;
  2180. #else
  2181. bad_arch();
  2182. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2183. }
  2184. // contiguous u/y values
  2185. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2186. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2187. const float & d3, const float & d8) {
  2188. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2189. int sumi = 0;
  2190. #pragma unroll
  2191. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2192. int sumi_sc = 0;
  2193. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2194. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2195. }
  2196. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2197. }
  2198. return d3*d8 * sumi;
  2199. #else
  2200. bad_arch();
  2201. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2202. }
  2203. #define VDR_Q4_K_Q8_1_MMVQ 2
  2204. #define VDR_Q4_K_Q8_1_MMQ 8
  2205. // contiguous v/x values
  2206. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2207. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2208. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2209. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2210. float sumf_d = 0.0f;
  2211. float sumf_m = 0.0f;
  2212. #pragma unroll
  2213. for (int i = 0; i < QR4_K; ++i) {
  2214. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2215. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2216. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2217. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2218. sumf_d += d8[i] * (dot1 * sc[i]);
  2219. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2220. }
  2221. const float2 dm4f = __half22float2(dm4);
  2222. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2223. #else
  2224. bad_arch();
  2225. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2226. }
  2227. // contiguous u/y values
  2228. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2229. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2230. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2231. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2232. float sumf_d = 0.0f;
  2233. float sumf_m = 0.0f;
  2234. #pragma unroll
  2235. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2236. int sumi_d = 0;
  2237. #pragma unroll
  2238. for (int j = 0; j < QI8_1; ++j) {
  2239. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2240. }
  2241. const float2 ds8f = __half22float2(ds8[i]);
  2242. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2243. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2244. }
  2245. const float2 dm4f = __half22float2(dm4);
  2246. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2247. #else
  2248. bad_arch();
  2249. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2250. }
  2251. #define VDR_Q5_K_Q8_1_MMVQ 2
  2252. #define VDR_Q5_K_Q8_1_MMQ 8
  2253. // contiguous v/x values
  2254. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2255. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2256. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2257. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2258. float sumf_d = 0.0f;
  2259. float sumf_m = 0.0f;
  2260. #pragma unroll
  2261. for (int i = 0; i < QR5_K; ++i) {
  2262. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2263. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2264. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2265. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2266. const int v0i = vl0i | vh0i;
  2267. const int v1i = vl1i | vh1i;
  2268. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2269. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2270. sumf_d += d8[i] * (dot1 * sc[i]);
  2271. sumf_m += d8[i] * (dot2 * m[i]);
  2272. }
  2273. const float2 dm5f = __half22float2(dm5);
  2274. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2275. #else
  2276. bad_arch();
  2277. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2278. }
  2279. // contiguous u/y values
  2280. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2281. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2282. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2283. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2284. float sumf_d = 0.0f;
  2285. float sumf_m = 0.0f;
  2286. #pragma unroll
  2287. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2288. int sumi_d = 0;
  2289. #pragma unroll
  2290. for (int j = 0; j < QI8_1; ++j) {
  2291. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2292. }
  2293. const float2 ds8f = __half22float2(ds8[i]);
  2294. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2295. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2296. }
  2297. const float2 dm4f = __half22float2(dm4);
  2298. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2299. #else
  2300. bad_arch();
  2301. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2302. }
  2303. #define VDR_Q6_K_Q8_1_MMVQ 1
  2304. #define VDR_Q6_K_Q8_1_MMQ 8
  2305. // contiguous v/x values
  2306. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2307. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2308. const float & d, const float * __restrict__ d8) {
  2309. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2310. float sumf = 0.0f;
  2311. #pragma unroll
  2312. for (int i = 0; i < QR6_K; ++i) {
  2313. const int sc = scales[4*i];
  2314. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2315. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2316. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2317. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2318. }
  2319. return d*sumf;
  2320. #else
  2321. bad_arch();
  2322. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2323. }
  2324. // contiguous u/y values
  2325. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2326. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2327. const float & d6, const float * __restrict__ d8) {
  2328. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2329. float sumf_d = 0.0f;
  2330. #pragma unroll
  2331. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2332. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2333. #pragma unroll
  2334. for (int i = i0; i < i0 + 2; ++i) {
  2335. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2336. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2337. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2338. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2339. }
  2340. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2341. }
  2342. return d6 * sumf_d;
  2343. #else
  2344. bad_arch();
  2345. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2346. }
  2347. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2348. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2349. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2350. int v[VDR_Q4_0_Q8_1_MMVQ];
  2351. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2352. #pragma unroll
  2353. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2354. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2355. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2356. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2357. }
  2358. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2359. }
  2360. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2361. (void)x_qh; (void)x_sc;
  2362. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2363. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2364. *x_ql = tile_x_qs;
  2365. *x_dm = (half2 *) tile_x_d;
  2366. }
  2367. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2368. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2369. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2370. (void)x_qh; (void)x_sc;
  2371. GGML_CUDA_ASSUME(i_offset >= 0);
  2372. GGML_CUDA_ASSUME(i_offset < nwarps);
  2373. GGML_CUDA_ASSUME(k >= 0);
  2374. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2375. const int kbx = k / QI4_0;
  2376. const int kqsx = k % QI4_0;
  2377. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2378. float * x_dmf = (float *) x_dm;
  2379. #pragma unroll
  2380. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2381. int i = i0 + i_offset;
  2382. if (need_check) {
  2383. i = min(i, i_max);
  2384. }
  2385. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2386. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2387. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2388. }
  2389. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2390. const int kbxd = k % blocks_per_tile_x_row;
  2391. #pragma unroll
  2392. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2393. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2394. if (need_check) {
  2395. i = min(i, i_max);
  2396. }
  2397. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2398. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2399. }
  2400. }
  2401. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2402. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2403. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2404. (void)x_qh; (void)x_sc;
  2405. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2406. const float * x_dmf = (const float *) x_dm;
  2407. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2408. #pragma unroll
  2409. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2410. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2411. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2412. }
  2413. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2414. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2415. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2416. }
  2417. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2418. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2419. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2420. int v[VDR_Q4_1_Q8_1_MMVQ];
  2421. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2422. #pragma unroll
  2423. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2424. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2425. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2426. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2427. }
  2428. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2429. }
  2430. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2431. (void)x_qh; (void)x_sc;
  2432. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2433. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2434. *x_ql = tile_x_qs;
  2435. *x_dm = tile_x_dm;
  2436. }
  2437. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2438. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2439. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2440. (void)x_qh; (void)x_sc;
  2441. GGML_CUDA_ASSUME(i_offset >= 0);
  2442. GGML_CUDA_ASSUME(i_offset < nwarps);
  2443. GGML_CUDA_ASSUME(k >= 0);
  2444. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2445. const int kbx = k / QI4_1;
  2446. const int kqsx = k % QI4_1;
  2447. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2448. #pragma unroll
  2449. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2450. int i = i0 + i_offset;
  2451. if (need_check) {
  2452. i = min(i, i_max);
  2453. }
  2454. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2455. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2456. }
  2457. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2458. const int kbxd = k % blocks_per_tile_x_row;
  2459. #pragma unroll
  2460. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2461. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2462. if (need_check) {
  2463. i = min(i, i_max);
  2464. }
  2465. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2466. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2467. }
  2468. }
  2469. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2470. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2471. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2472. (void)x_qh; (void)x_sc;
  2473. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2474. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2475. #pragma unroll
  2476. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2477. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2478. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2479. }
  2480. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2481. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2482. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2483. }
  2484. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2485. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2486. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2487. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2488. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2489. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2490. #pragma unroll
  2491. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2492. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2493. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2494. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2495. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2496. }
  2497. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2498. }
  2499. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2500. (void)x_qh; (void)x_sc;
  2501. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2502. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2503. *x_ql = tile_x_ql;
  2504. *x_dm = (half2 *) tile_x_d;
  2505. }
  2506. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2507. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2508. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2509. (void)x_qh; (void)x_sc;
  2510. GGML_CUDA_ASSUME(i_offset >= 0);
  2511. GGML_CUDA_ASSUME(i_offset < nwarps);
  2512. GGML_CUDA_ASSUME(k >= 0);
  2513. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2514. const int kbx = k / QI5_0;
  2515. const int kqsx = k % QI5_0;
  2516. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2517. #pragma unroll
  2518. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2519. int i = i0 + i_offset;
  2520. if (need_check) {
  2521. i = min(i, i_max);
  2522. }
  2523. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2524. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2525. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2526. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2527. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2528. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2529. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2530. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2531. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2532. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2533. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2534. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2535. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2536. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2537. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2538. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2539. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2540. }
  2541. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2542. const int kbxd = k % blocks_per_tile_x_row;
  2543. float * x_dmf = (float *) x_dm;
  2544. #pragma unroll
  2545. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2546. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2547. if (need_check) {
  2548. i = min(i, i_max);
  2549. }
  2550. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2551. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2552. }
  2553. }
  2554. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2555. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2556. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2557. (void)x_qh; (void)x_sc;
  2558. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2559. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2560. const float * x_dmf = (const float *) x_dm;
  2561. const float * y_df = (const float *) y_ds;
  2562. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2563. #pragma unroll
  2564. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2565. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2566. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2567. }
  2568. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2569. (&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)]);
  2570. }
  2571. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2572. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2573. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2574. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2575. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2576. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2577. #pragma unroll
  2578. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2579. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2580. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2581. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2582. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2583. }
  2584. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2585. }
  2586. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2587. (void)x_qh; (void)x_sc;
  2588. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2589. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2590. *x_ql = tile_x_ql;
  2591. *x_dm = tile_x_dm;
  2592. }
  2593. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2594. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2595. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2596. (void)x_qh; (void)x_sc;
  2597. GGML_CUDA_ASSUME(i_offset >= 0);
  2598. GGML_CUDA_ASSUME(i_offset < nwarps);
  2599. GGML_CUDA_ASSUME(k >= 0);
  2600. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2601. const int kbx = k / QI5_1;
  2602. const int kqsx = k % QI5_1;
  2603. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2604. #pragma unroll
  2605. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2606. int i = i0 + i_offset;
  2607. if (need_check) {
  2608. i = min(i, i_max);
  2609. }
  2610. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2611. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2612. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2613. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2614. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2615. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2616. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2617. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2618. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2619. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2620. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2621. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2622. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2623. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2624. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2625. }
  2626. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2627. const int kbxd = k % blocks_per_tile_x_row;
  2628. #pragma unroll
  2629. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2630. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2631. if (need_check) {
  2632. i = min(i, i_max);
  2633. }
  2634. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2635. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2636. }
  2637. }
  2638. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2639. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2640. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2641. (void)x_qh; (void)x_sc;
  2642. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2643. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2644. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2645. #pragma unroll
  2646. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2647. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2648. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2649. }
  2650. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2651. (&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)]);
  2652. }
  2653. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2654. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2655. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2656. int v[VDR_Q8_0_Q8_1_MMVQ];
  2657. int u[VDR_Q8_0_Q8_1_MMVQ];
  2658. #pragma unroll
  2659. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2660. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2661. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2662. }
  2663. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2664. }
  2665. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2666. (void)x_qh; (void)x_sc;
  2667. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2668. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2669. *x_ql = tile_x_qs;
  2670. *x_dm = (half2 *) tile_x_d;
  2671. }
  2672. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2673. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2674. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2675. (void)x_qh; (void)x_sc;
  2676. GGML_CUDA_ASSUME(i_offset >= 0);
  2677. GGML_CUDA_ASSUME(i_offset < nwarps);
  2678. GGML_CUDA_ASSUME(k >= 0);
  2679. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2680. const int kbx = k / QI8_0;
  2681. const int kqsx = k % QI8_0;
  2682. float * x_dmf = (float *) x_dm;
  2683. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2684. #pragma unroll
  2685. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2686. int i = i0 + i_offset;
  2687. if (need_check) {
  2688. i = min(i, i_max);
  2689. }
  2690. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2691. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2692. }
  2693. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2694. const int kbxd = k % blocks_per_tile_x_row;
  2695. #pragma unroll
  2696. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2697. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2698. if (need_check) {
  2699. i = min(i, i_max);
  2700. }
  2701. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2702. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2703. }
  2704. }
  2705. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2706. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2707. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2708. (void)x_qh; (void)x_sc;
  2709. const float * x_dmf = (const float *) x_dm;
  2710. const float * y_df = (const float *) y_ds;
  2711. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2712. (&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],
  2713. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2714. }
  2715. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2716. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2717. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2718. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2719. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2720. const uint8_t * scales = bq2_K->scales + scale_offset;
  2721. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2722. int u[QR2_K];
  2723. float d8[QR2_K];
  2724. #pragma unroll
  2725. for (int i = 0; i < QR2_K; ++ i) {
  2726. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2727. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2728. }
  2729. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2730. }
  2731. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2732. (void)x_qh;
  2733. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2734. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2735. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2736. *x_ql = tile_x_ql;
  2737. *x_dm = tile_x_dm;
  2738. *x_sc = tile_x_sc;
  2739. }
  2740. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2741. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2742. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2743. (void)x_qh;
  2744. GGML_CUDA_ASSUME(i_offset >= 0);
  2745. GGML_CUDA_ASSUME(i_offset < nwarps);
  2746. GGML_CUDA_ASSUME(k >= 0);
  2747. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2748. const int kbx = k / QI2_K;
  2749. const int kqsx = k % QI2_K;
  2750. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2751. #pragma unroll
  2752. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2753. int i = i0 + i_offset;
  2754. if (need_check) {
  2755. i = min(i, i_max);
  2756. }
  2757. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2758. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2759. }
  2760. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2761. const int kbxd = k % blocks_per_tile_x_row;
  2762. #pragma unroll
  2763. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2764. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2765. if (need_check) {
  2766. i = min(i, i_max);
  2767. }
  2768. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2769. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2770. }
  2771. #pragma unroll
  2772. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2773. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2774. if (need_check) {
  2775. i = min(i, i_max);
  2776. }
  2777. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2778. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2779. }
  2780. }
  2781. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2782. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2783. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2784. (void)x_qh;
  2785. const int kbx = k / QI2_K;
  2786. const int ky = (k % QI2_K) * QR2_K;
  2787. const float * y_df = (const float *) y_ds;
  2788. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2789. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2790. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2791. #pragma unroll
  2792. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2793. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2794. }
  2795. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2796. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2797. 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]);
  2798. }
  2799. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2800. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2801. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2802. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2803. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2804. const float d = bq3_K->d;
  2805. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2806. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2807. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2808. int u[QR3_K];
  2809. float d8[QR3_K];
  2810. #pragma unroll
  2811. for (int i = 0; i < QR3_K; ++i) {
  2812. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2813. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2814. }
  2815. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2816. }
  2817. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2818. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2819. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2820. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2821. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2822. *x_ql = tile_x_ql;
  2823. *x_dm = tile_x_dm;
  2824. *x_qh = tile_x_qh;
  2825. *x_sc = tile_x_sc;
  2826. }
  2827. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2828. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2829. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2830. GGML_CUDA_ASSUME(i_offset >= 0);
  2831. GGML_CUDA_ASSUME(i_offset < nwarps);
  2832. GGML_CUDA_ASSUME(k >= 0);
  2833. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2834. const int kbx = k / QI3_K;
  2835. const int kqsx = k % QI3_K;
  2836. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2837. #pragma unroll
  2838. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2839. int i = i0 + i_offset;
  2840. if (need_check) {
  2841. i = min(i, i_max);
  2842. }
  2843. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2844. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2845. }
  2846. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2847. const int kbxd = k % blocks_per_tile_x_row;
  2848. float * x_dmf = (float *) x_dm;
  2849. #pragma unroll
  2850. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2851. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2852. if (need_check) {
  2853. i = min(i, i_max);
  2854. }
  2855. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2856. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2857. }
  2858. #pragma unroll
  2859. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2860. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2861. if (need_check) {
  2862. i = min(i, i_max);
  2863. }
  2864. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2865. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2866. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2867. }
  2868. #pragma unroll
  2869. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2870. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2871. if (need_check) {
  2872. i = min(i, i_max);
  2873. }
  2874. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2875. const int ksc = k % (QI3_K/4);
  2876. const int ksc_low = ksc % (QI3_K/8);
  2877. const int shift_low = 4 * (ksc / (QI3_K/8));
  2878. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2879. const int ksc_high = QI3_K/8;
  2880. const int shift_high = 2 * ksc;
  2881. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2882. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2883. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2884. }
  2885. }
  2886. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2887. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2888. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2889. const int kbx = k / QI3_K;
  2890. const int ky = (k % QI3_K) * QR3_K;
  2891. const float * x_dmf = (const float *) x_dm;
  2892. const float * y_df = (const float *) y_ds;
  2893. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2894. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2895. #pragma unroll
  2896. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2897. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2898. const int shift = 2 * ((ky % 32) / 8);
  2899. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2900. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2901. const int vlh = (vh << 2) & 0x04040404;
  2902. v[l] = __vsubss4(vll, vlh);
  2903. }
  2904. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2905. 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]);
  2906. }
  2907. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2908. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2909. #ifndef GGML_QKK_64
  2910. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2911. int v[2];
  2912. int u[2*QR4_K];
  2913. float d8[QR4_K];
  2914. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2915. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2916. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2917. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2918. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2919. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2920. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2921. v[0] = q4[0];
  2922. v[1] = q4[4];
  2923. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2924. uint16_t aux[2];
  2925. const int j = bq8_offset/2;
  2926. if (j < 2) {
  2927. aux[0] = scales[j+0] & 0x3f3f;
  2928. aux[1] = scales[j+2] & 0x3f3f;
  2929. } else {
  2930. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2931. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2932. }
  2933. const uint8_t * sc = (const uint8_t *)aux;
  2934. const uint8_t * m = sc + 2;
  2935. for (int i = 0; i < QR4_K; ++i) {
  2936. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2937. d8[i] = __low2half(bq8i->ds);
  2938. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2939. u[2*i+0] = q8[0];
  2940. u[2*i+1] = q8[4];
  2941. }
  2942. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2943. #else
  2944. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2945. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2946. float sumf_d = 0.0f;
  2947. float sumf_m = 0.0f;
  2948. uint16_t aux16[2];
  2949. const uint8_t * s = (const uint8_t *)aux16;
  2950. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2951. aux16[0] = a[0] & 0x0f0f;
  2952. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2953. const float dall = bq4_K->dm[0];
  2954. const float dmin = bq4_K->dm[1];
  2955. const float d8_1 = __low2float(bq8_1[0].ds);
  2956. const float d8_2 = __low2float(bq8_1[1].ds);
  2957. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2958. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2959. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2960. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2961. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2962. const int v1 = q4[0];
  2963. const int v2 = q4[4];
  2964. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2965. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2966. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2967. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2968. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2969. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2970. return dall * sumf_d - dmin * sumf_m;
  2971. #else
  2972. bad_arch();
  2973. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2974. #endif
  2975. }
  2976. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2977. (void)x_qh;
  2978. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2979. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2980. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2981. *x_ql = tile_x_ql;
  2982. *x_dm = tile_x_dm;
  2983. *x_sc = tile_x_sc;
  2984. }
  2985. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2986. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2987. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2988. (void)x_qh;
  2989. GGML_CUDA_ASSUME(i_offset >= 0);
  2990. GGML_CUDA_ASSUME(i_offset < nwarps);
  2991. GGML_CUDA_ASSUME(k >= 0);
  2992. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2993. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2994. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2995. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2996. #pragma unroll
  2997. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2998. int i = i0 + i_offset;
  2999. if (need_check) {
  3000. i = min(i, i_max);
  3001. }
  3002. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3003. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3004. }
  3005. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3006. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3007. #pragma unroll
  3008. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3009. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3010. if (need_check) {
  3011. i = min(i, i_max);
  3012. }
  3013. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3014. #if QK_K == 256
  3015. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3016. #else
  3017. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3018. #endif
  3019. }
  3020. #pragma unroll
  3021. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3022. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3023. if (need_check) {
  3024. i = min(i, i_max);
  3025. }
  3026. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3027. const int * scales = (const int *) bxi->scales;
  3028. const int ksc = k % (WARP_SIZE/8);
  3029. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3030. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3031. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3032. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3033. }
  3034. }
  3035. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3036. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3037. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3038. (void)x_qh;
  3039. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3040. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3041. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3042. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3043. }
  3044. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3045. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3046. #ifndef GGML_QKK_64
  3047. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3048. int vl[2];
  3049. int vh[2];
  3050. int u[2*QR5_K];
  3051. float d8[QR5_K];
  3052. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3053. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3054. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3055. vl[0] = ql[0];
  3056. vl[1] = ql[4];
  3057. vh[0] = qh[0] >> bq8_offset;
  3058. vh[1] = qh[4] >> bq8_offset;
  3059. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3060. uint16_t aux[2];
  3061. const int j = bq8_offset/2;
  3062. if (j < 2) {
  3063. aux[0] = scales[j+0] & 0x3f3f;
  3064. aux[1] = scales[j+2] & 0x3f3f;
  3065. } else {
  3066. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3067. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3068. }
  3069. const uint8_t * sc = (const uint8_t *)aux;
  3070. const uint8_t * m = sc + 2;
  3071. #pragma unroll
  3072. for (int i = 0; i < QR5_K; ++i) {
  3073. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3074. d8[i] = __low2float(bq8i->ds);
  3075. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3076. u[2*i+0] = q8[0];
  3077. u[2*i+1] = q8[4];
  3078. }
  3079. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3080. #else
  3081. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3082. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3083. const int8_t * s = bq5_K->scales;
  3084. const float d = bq5_K->d;
  3085. const float d8_1 = __low2half(bq8_1[0].ds);
  3086. const float d8_2 = __low2half(bq8_1[1].ds);
  3087. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3088. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3089. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3090. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3091. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3092. const int vl1 = ql[0];
  3093. const int vl2 = ql[4];
  3094. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3095. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3096. const int in = step%8; // 0, 4, 0, 4
  3097. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3098. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3099. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3100. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3101. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3102. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3103. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3104. return d * sumf_d;
  3105. #else
  3106. bad_arch();
  3107. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3108. #endif
  3109. }
  3110. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3111. (void)x_qh;
  3112. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3113. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3114. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3115. *x_ql = tile_x_ql;
  3116. *x_dm = tile_x_dm;
  3117. *x_sc = tile_x_sc;
  3118. }
  3119. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3120. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3121. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3122. (void)x_qh;
  3123. GGML_CUDA_ASSUME(i_offset >= 0);
  3124. GGML_CUDA_ASSUME(i_offset < nwarps);
  3125. GGML_CUDA_ASSUME(k >= 0);
  3126. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3127. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3128. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3129. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3130. #pragma unroll
  3131. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3132. int i = i0 + i_offset;
  3133. if (need_check) {
  3134. i = min(i, i_max);
  3135. }
  3136. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3137. const int ky = QR5_K*kqsx;
  3138. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3139. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3140. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3141. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3142. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3143. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3144. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3145. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3146. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3147. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3148. }
  3149. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3150. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3151. #pragma unroll
  3152. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3153. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3154. if (need_check) {
  3155. i = min(i, i_max);
  3156. }
  3157. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3158. #if QK_K == 256
  3159. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3160. #endif
  3161. }
  3162. #pragma unroll
  3163. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3164. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3165. if (need_check) {
  3166. i = min(i, i_max);
  3167. }
  3168. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3169. const int * scales = (const int *) bxi->scales;
  3170. const int ksc = k % (WARP_SIZE/8);
  3171. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3172. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3173. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3174. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3175. }
  3176. }
  3177. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3178. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3179. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3180. (void)x_qh;
  3181. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3182. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3183. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3184. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3185. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3186. }
  3187. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3188. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3189. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3190. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3191. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3192. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3193. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3194. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3195. const int8_t * scales = bq6_K->scales + scale_offset;
  3196. int u[QR6_K];
  3197. float d8[QR6_K];
  3198. #pragma unroll
  3199. for (int i = 0; i < QR6_K; ++i) {
  3200. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3201. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3202. }
  3203. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3204. }
  3205. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3206. (void)x_qh;
  3207. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3208. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3209. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3210. *x_ql = tile_x_ql;
  3211. *x_dm = tile_x_dm;
  3212. *x_sc = tile_x_sc;
  3213. }
  3214. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3215. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3216. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3217. (void)x_qh;
  3218. GGML_CUDA_ASSUME(i_offset >= 0);
  3219. GGML_CUDA_ASSUME(i_offset < nwarps);
  3220. GGML_CUDA_ASSUME(k >= 0);
  3221. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3222. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3223. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3224. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3225. #pragma unroll
  3226. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3227. int i = i0 + i_offset;
  3228. if (need_check) {
  3229. i = min(i, i_max);
  3230. }
  3231. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3232. const int ky = QR6_K*kqsx;
  3233. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3234. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3235. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3236. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3237. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3238. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3239. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3240. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3241. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3242. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3243. }
  3244. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3245. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3246. float * x_dmf = (float *) x_dm;
  3247. #pragma unroll
  3248. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3249. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3250. if (need_check) {
  3251. i = min(i, i_max);
  3252. }
  3253. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3254. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3255. }
  3256. #pragma unroll
  3257. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3258. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3259. if (need_check) {
  3260. i = min(i, i_max);
  3261. }
  3262. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3263. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3264. }
  3265. }
  3266. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3267. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3268. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3269. (void)x_qh;
  3270. const float * x_dmf = (const float *) x_dm;
  3271. const float * y_df = (const float *) y_ds;
  3272. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3273. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3274. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3275. 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]);
  3276. }
  3277. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3278. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3279. #if QK_K == 256
  3280. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3281. #if QR2_XXS == 8
  3282. const int ib32 = iqs;
  3283. const uint16_t * q2 = bq2->qs + 4*ib32;
  3284. const uint8_t * aux8 = (const uint8_t *)q2;
  3285. const int8_t * q8 = bq8_1[ib32].qs;
  3286. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3287. int sumi = 0;
  3288. for (int l = 0; l < 4; ++l) {
  3289. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3290. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3291. for (int j = 0; j < 8; ++j) {
  3292. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3293. }
  3294. q8 += 8;
  3295. aux32 >>= 7;
  3296. }
  3297. const float d = (float)bq2->d * (0.5f + aux32) * (float)bq8_1[ib32].ds.x * 0.25f;
  3298. return d * sumi;
  3299. #else
  3300. // iqs is 0...15
  3301. const int ib32 = iqs/2;
  3302. const int il = iqs%2;
  3303. const uint16_t * q2 = bq2->qs + 4*ib32;
  3304. const uint8_t * aux8 = (const uint8_t *)q2;
  3305. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  3306. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  3307. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3308. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * (float)bq8_1[ib32].ds.x * 0.25f;
  3309. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3310. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3311. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3312. int sumi1 = 0, sumi2 = 0;
  3313. for (int j = 0; j < 8; ++j) {
  3314. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3315. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3316. }
  3317. return d * (sumi1 + sumi2);
  3318. #endif
  3319. #else
  3320. assert(false);
  3321. return 0.f;
  3322. #endif
  3323. }
  3324. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  3325. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3326. #if QK_K == 256
  3327. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  3328. const int ib32 = iqs;
  3329. const uint16_t * q2 = bq2->qs + 4*ib32;
  3330. const int8_t * q8 = bq8_1[ib32].qs;
  3331. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3332. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3333. int sumi1 = 0;
  3334. for (int l = 0; l < 2; ++l) {
  3335. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  3336. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  3337. for (int j = 0; j < 8; ++j) {
  3338. sumi1 += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3339. }
  3340. q8 += 8;
  3341. }
  3342. int sumi2 = 0;
  3343. for (int l = 2; l < 4; ++l) {
  3344. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  3345. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  3346. for (int j = 0; j < 8; ++j) {
  3347. sumi2 += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3348. }
  3349. q8 += 8;
  3350. }
  3351. const float d = (float)bq2->d * (float)bq8_1[ib32].ds.x * 0.25f;
  3352. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3353. #else
  3354. assert(false);
  3355. return 0.f;
  3356. #endif
  3357. }
  3358. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3359. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3360. static __device__ __forceinline__ void mul_mat_q(
  3361. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3362. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3363. const block_q_t * x = (const block_q_t *) vx;
  3364. const block_q8_1 * y = (const block_q8_1 *) vy;
  3365. const int blocks_per_row_x = ncols_x / qk;
  3366. const int blocks_per_col_y = nrows_y / QK8_1;
  3367. const int blocks_per_warp = WARP_SIZE / qi;
  3368. const int & ncols_dst = ncols_y;
  3369. const int row_dst_0 = blockIdx.x*mmq_y;
  3370. const int & row_x_0 = row_dst_0;
  3371. const int col_dst_0 = blockIdx.y*mmq_x;
  3372. const int & col_y_0 = col_dst_0;
  3373. int * tile_x_ql = nullptr;
  3374. half2 * tile_x_dm = nullptr;
  3375. int * tile_x_qh = nullptr;
  3376. int * tile_x_sc = nullptr;
  3377. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3378. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3379. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3380. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3381. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3382. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3383. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3384. #pragma unroll
  3385. for (int ir = 0; ir < qr; ++ir) {
  3386. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3387. const int kbxd = kqs / QI8_1;
  3388. #pragma unroll
  3389. for (int i = 0; i < mmq_x; i += nwarps) {
  3390. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3391. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3392. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3393. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3394. }
  3395. #pragma unroll
  3396. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3397. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3398. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3399. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3400. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3401. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3402. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3403. if (need_sum) {
  3404. *dsi_dst = *dsi_src;
  3405. } else {
  3406. float * dfi_dst = (float *) dsi_dst;
  3407. *dfi_dst = __low2half(*dsi_src);
  3408. }
  3409. }
  3410. __syncthreads();
  3411. // #pragma unroll // unrolling this loop causes too much register pressure
  3412. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3413. #pragma unroll
  3414. for (int j = 0; j < mmq_x; j += nwarps) {
  3415. #pragma unroll
  3416. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3417. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3418. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3419. threadIdx.x + i, threadIdx.y + j, k);
  3420. }
  3421. }
  3422. }
  3423. __syncthreads();
  3424. }
  3425. }
  3426. #pragma unroll
  3427. for (int j = 0; j < mmq_x; j += nwarps) {
  3428. const int col_dst = col_dst_0 + j + threadIdx.y;
  3429. if (col_dst >= ncols_dst) {
  3430. return;
  3431. }
  3432. #pragma unroll
  3433. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3434. const int row_dst = row_dst_0 + threadIdx.x + i;
  3435. if (row_dst >= nrows_dst) {
  3436. continue;
  3437. }
  3438. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3439. }
  3440. }
  3441. }
  3442. #define MMQ_X_Q4_0_RDNA2 64
  3443. #define MMQ_Y_Q4_0_RDNA2 128
  3444. #define NWARPS_Q4_0_RDNA2 8
  3445. #define MMQ_X_Q4_0_RDNA1 64
  3446. #define MMQ_Y_Q4_0_RDNA1 64
  3447. #define NWARPS_Q4_0_RDNA1 8
  3448. #if defined(CUDA_USE_TENSOR_CORES)
  3449. #define MMQ_X_Q4_0_AMPERE 4
  3450. #define MMQ_Y_Q4_0_AMPERE 32
  3451. #define NWARPS_Q4_0_AMPERE 4
  3452. #else
  3453. #define MMQ_X_Q4_0_AMPERE 64
  3454. #define MMQ_Y_Q4_0_AMPERE 128
  3455. #define NWARPS_Q4_0_AMPERE 4
  3456. #endif
  3457. #define MMQ_X_Q4_0_PASCAL 64
  3458. #define MMQ_Y_Q4_0_PASCAL 64
  3459. #define NWARPS_Q4_0_PASCAL 8
  3460. template <bool need_check> static __global__ void
  3461. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3462. #if defined(RDNA3) || defined(RDNA2)
  3463. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3464. #endif // defined(RDNA3) || defined(RDNA2)
  3465. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3466. mul_mat_q4_0(
  3467. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3468. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3469. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3470. #if defined(RDNA3) || defined(RDNA2)
  3471. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3472. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3473. const int nwarps = NWARPS_Q4_0_RDNA2;
  3474. #else
  3475. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3476. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3477. const int nwarps = NWARPS_Q4_0_RDNA1;
  3478. #endif // defined(RDNA3) || defined(RDNA2)
  3479. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3480. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3481. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3482. #elif __CUDA_ARCH__ >= CC_VOLTA
  3483. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3484. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3485. const int nwarps = NWARPS_Q4_0_AMPERE;
  3486. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3487. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3488. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3489. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3490. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3491. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3492. const int nwarps = NWARPS_Q4_0_PASCAL;
  3493. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3494. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3495. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3496. #else
  3497. (void) vec_dot_q4_0_q8_1_mul_mat;
  3498. bad_arch();
  3499. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3500. }
  3501. #define MMQ_X_Q4_1_RDNA2 64
  3502. #define MMQ_Y_Q4_1_RDNA2 128
  3503. #define NWARPS_Q4_1_RDNA2 8
  3504. #define MMQ_X_Q4_1_RDNA1 64
  3505. #define MMQ_Y_Q4_1_RDNA1 64
  3506. #define NWARPS_Q4_1_RDNA1 8
  3507. #if defined(CUDA_USE_TENSOR_CORES)
  3508. #define MMQ_X_Q4_1_AMPERE 4
  3509. #define MMQ_Y_Q4_1_AMPERE 32
  3510. #define NWARPS_Q4_1_AMPERE 4
  3511. #else
  3512. #define MMQ_X_Q4_1_AMPERE 64
  3513. #define MMQ_Y_Q4_1_AMPERE 128
  3514. #define NWARPS_Q4_1_AMPERE 4
  3515. #endif
  3516. #define MMQ_X_Q4_1_PASCAL 64
  3517. #define MMQ_Y_Q4_1_PASCAL 64
  3518. #define NWARPS_Q4_1_PASCAL 8
  3519. template <bool need_check> static __global__ void
  3520. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3521. #if defined(RDNA3) || defined(RDNA2)
  3522. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3523. #endif // defined(RDNA3) || defined(RDNA2)
  3524. #elif __CUDA_ARCH__ < CC_VOLTA
  3525. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3526. #endif // __CUDA_ARCH__ < CC_VOLTA
  3527. mul_mat_q4_1(
  3528. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3529. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3530. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3531. #if defined(RDNA3) || defined(RDNA2)
  3532. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3533. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3534. const int nwarps = NWARPS_Q4_1_RDNA2;
  3535. #else
  3536. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3537. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3538. const int nwarps = NWARPS_Q4_1_RDNA1;
  3539. #endif // defined(RDNA3) || defined(RDNA2)
  3540. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3541. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3542. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3543. #elif __CUDA_ARCH__ >= CC_VOLTA
  3544. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3545. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3546. const int nwarps = NWARPS_Q4_1_AMPERE;
  3547. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3548. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3549. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3550. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3551. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3552. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3553. const int nwarps = NWARPS_Q4_1_PASCAL;
  3554. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3555. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3556. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3557. #else
  3558. (void) vec_dot_q4_1_q8_1_mul_mat;
  3559. bad_arch();
  3560. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3561. }
  3562. #define MMQ_X_Q5_0_RDNA2 64
  3563. #define MMQ_Y_Q5_0_RDNA2 128
  3564. #define NWARPS_Q5_0_RDNA2 8
  3565. #define MMQ_X_Q5_0_RDNA1 64
  3566. #define MMQ_Y_Q5_0_RDNA1 64
  3567. #define NWARPS_Q5_0_RDNA1 8
  3568. #if defined(CUDA_USE_TENSOR_CORES)
  3569. #define MMQ_X_Q5_0_AMPERE 4
  3570. #define MMQ_Y_Q5_0_AMPERE 32
  3571. #define NWARPS_Q5_0_AMPERE 4
  3572. #else
  3573. #define MMQ_X_Q5_0_AMPERE 128
  3574. #define MMQ_Y_Q5_0_AMPERE 64
  3575. #define NWARPS_Q5_0_AMPERE 4
  3576. #endif
  3577. #define MMQ_X_Q5_0_PASCAL 64
  3578. #define MMQ_Y_Q5_0_PASCAL 64
  3579. #define NWARPS_Q5_0_PASCAL 8
  3580. template <bool need_check> static __global__ void
  3581. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3582. #if defined(RDNA3) || defined(RDNA2)
  3583. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3584. #endif // defined(RDNA3) || defined(RDNA2)
  3585. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3586. mul_mat_q5_0(
  3587. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3588. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3589. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3590. #if defined(RDNA3) || defined(RDNA2)
  3591. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3592. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3593. const int nwarps = NWARPS_Q5_0_RDNA2;
  3594. #else
  3595. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3596. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3597. const int nwarps = NWARPS_Q5_0_RDNA1;
  3598. #endif // defined(RDNA3) || defined(RDNA2)
  3599. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3600. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3601. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3602. #elif __CUDA_ARCH__ >= CC_VOLTA
  3603. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3604. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3605. const int nwarps = NWARPS_Q5_0_AMPERE;
  3606. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3607. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3608. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3609. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3610. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3611. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3612. const int nwarps = NWARPS_Q5_0_PASCAL;
  3613. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3614. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3615. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3616. #else
  3617. (void) vec_dot_q5_0_q8_1_mul_mat;
  3618. bad_arch();
  3619. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3620. }
  3621. #define MMQ_X_Q5_1_RDNA2 64
  3622. #define MMQ_Y_Q5_1_RDNA2 128
  3623. #define NWARPS_Q5_1_RDNA2 8
  3624. #define MMQ_X_Q5_1_RDNA1 64
  3625. #define MMQ_Y_Q5_1_RDNA1 64
  3626. #define NWARPS_Q5_1_RDNA1 8
  3627. #if defined(CUDA_USE_TENSOR_CORES)
  3628. #define MMQ_X_Q5_1_AMPERE 4
  3629. #define MMQ_Y_Q5_1_AMPERE 32
  3630. #define NWARPS_Q5_1_AMPERE 4
  3631. #else
  3632. #define MMQ_X_Q5_1_AMPERE 128
  3633. #define MMQ_Y_Q5_1_AMPERE 64
  3634. #define NWARPS_Q5_1_AMPERE 4
  3635. #endif
  3636. #define MMQ_X_Q5_1_PASCAL 64
  3637. #define MMQ_Y_Q5_1_PASCAL 64
  3638. #define NWARPS_Q5_1_PASCAL 8
  3639. template <bool need_check> static __global__ void
  3640. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3641. #if defined(RDNA3) || defined(RDNA2)
  3642. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3643. #endif // defined(RDNA3) || defined(RDNA2)
  3644. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3645. mul_mat_q5_1(
  3646. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3647. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3648. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3649. #if defined(RDNA3) || defined(RDNA2)
  3650. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3651. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3652. const int nwarps = NWARPS_Q5_1_RDNA2;
  3653. #else
  3654. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3655. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3656. const int nwarps = NWARPS_Q5_1_RDNA1;
  3657. #endif // defined(RDNA3) || defined(RDNA2)
  3658. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3659. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3660. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3661. #elif __CUDA_ARCH__ >= CC_VOLTA
  3662. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3663. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3664. const int nwarps = NWARPS_Q5_1_AMPERE;
  3665. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3666. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3667. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3668. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3669. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3670. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3671. const int nwarps = NWARPS_Q5_1_PASCAL;
  3672. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3673. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3674. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3675. #else
  3676. (void) vec_dot_q5_1_q8_1_mul_mat;
  3677. bad_arch();
  3678. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3679. }
  3680. #define MMQ_X_Q8_0_RDNA2 64
  3681. #define MMQ_Y_Q8_0_RDNA2 128
  3682. #define NWARPS_Q8_0_RDNA2 8
  3683. #define MMQ_X_Q8_0_RDNA1 64
  3684. #define MMQ_Y_Q8_0_RDNA1 64
  3685. #define NWARPS_Q8_0_RDNA1 8
  3686. #if defined(CUDA_USE_TENSOR_CORES)
  3687. #define MMQ_X_Q8_0_AMPERE 4
  3688. #define MMQ_Y_Q8_0_AMPERE 32
  3689. #define NWARPS_Q8_0_AMPERE 4
  3690. #else
  3691. #define MMQ_X_Q8_0_AMPERE 128
  3692. #define MMQ_Y_Q8_0_AMPERE 64
  3693. #define NWARPS_Q8_0_AMPERE 4
  3694. #endif
  3695. #define MMQ_X_Q8_0_PASCAL 64
  3696. #define MMQ_Y_Q8_0_PASCAL 64
  3697. #define NWARPS_Q8_0_PASCAL 8
  3698. template <bool need_check> static __global__ void
  3699. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3700. #if defined(RDNA3) || defined(RDNA2)
  3701. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3702. #endif // defined(RDNA3) || defined(RDNA2)
  3703. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3704. mul_mat_q8_0(
  3705. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3706. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3707. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3708. #if defined(RDNA3) || defined(RDNA2)
  3709. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3710. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3711. const int nwarps = NWARPS_Q8_0_RDNA2;
  3712. #else
  3713. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3714. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3715. const int nwarps = NWARPS_Q8_0_RDNA1;
  3716. #endif // defined(RDNA3) || defined(RDNA2)
  3717. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3718. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3719. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3720. #elif __CUDA_ARCH__ >= CC_VOLTA
  3721. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3722. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3723. const int nwarps = NWARPS_Q8_0_AMPERE;
  3724. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3725. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3726. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3727. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3728. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3729. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3730. const int nwarps = NWARPS_Q8_0_PASCAL;
  3731. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3732. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3733. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3734. #else
  3735. (void) vec_dot_q8_0_q8_1_mul_mat;
  3736. bad_arch();
  3737. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3738. }
  3739. #define MMQ_X_Q2_K_RDNA2 64
  3740. #define MMQ_Y_Q2_K_RDNA2 128
  3741. #define NWARPS_Q2_K_RDNA2 8
  3742. #define MMQ_X_Q2_K_RDNA1 128
  3743. #define MMQ_Y_Q2_K_RDNA1 32
  3744. #define NWARPS_Q2_K_RDNA1 8
  3745. #if defined(CUDA_USE_TENSOR_CORES)
  3746. #define MMQ_X_Q2_K_AMPERE 4
  3747. #define MMQ_Y_Q2_K_AMPERE 32
  3748. #define NWARPS_Q2_K_AMPERE 4
  3749. #else
  3750. #define MMQ_X_Q2_K_AMPERE 64
  3751. #define MMQ_Y_Q2_K_AMPERE 128
  3752. #define NWARPS_Q2_K_AMPERE 4
  3753. #endif
  3754. #define MMQ_X_Q2_K_PASCAL 64
  3755. #define MMQ_Y_Q2_K_PASCAL 64
  3756. #define NWARPS_Q2_K_PASCAL 8
  3757. template <bool need_check> static __global__ void
  3758. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3759. #if defined(RDNA3) || defined(RDNA2)
  3760. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3761. #endif // defined(RDNA3) || defined(RDNA2)
  3762. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3763. mul_mat_q2_K(
  3764. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3765. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3766. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3767. #if defined(RDNA3) || defined(RDNA2)
  3768. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3769. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3770. const int nwarps = NWARPS_Q2_K_RDNA2;
  3771. #else
  3772. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3773. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3774. const int nwarps = NWARPS_Q2_K_RDNA1;
  3775. #endif // defined(RDNA3) || defined(RDNA2)
  3776. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3777. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3778. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3779. #elif __CUDA_ARCH__ >= CC_VOLTA
  3780. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3781. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3782. const int nwarps = NWARPS_Q2_K_AMPERE;
  3783. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3784. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3785. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3786. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3787. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3788. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3789. const int nwarps = NWARPS_Q2_K_PASCAL;
  3790. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3791. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3792. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3793. #else
  3794. (void) vec_dot_q2_K_q8_1_mul_mat;
  3795. bad_arch();
  3796. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3797. }
  3798. #define MMQ_X_Q3_K_RDNA2 128
  3799. #define MMQ_Y_Q3_K_RDNA2 64
  3800. #define NWARPS_Q3_K_RDNA2 8
  3801. #define MMQ_X_Q3_K_RDNA1 32
  3802. #define MMQ_Y_Q3_K_RDNA1 128
  3803. #define NWARPS_Q3_K_RDNA1 8
  3804. #if defined(CUDA_USE_TENSOR_CORES)
  3805. #define MMQ_X_Q3_K_AMPERE 4
  3806. #define MMQ_Y_Q3_K_AMPERE 32
  3807. #define NWARPS_Q3_K_AMPERE 4
  3808. #else
  3809. #define MMQ_X_Q3_K_AMPERE 128
  3810. #define MMQ_Y_Q3_K_AMPERE 128
  3811. #define NWARPS_Q3_K_AMPERE 4
  3812. #endif
  3813. #define MMQ_X_Q3_K_PASCAL 64
  3814. #define MMQ_Y_Q3_K_PASCAL 64
  3815. #define NWARPS_Q3_K_PASCAL 8
  3816. template <bool need_check> static __global__ void
  3817. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3818. #if defined(RDNA3) || defined(RDNA2)
  3819. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3820. #endif // defined(RDNA3) || defined(RDNA2)
  3821. #elif __CUDA_ARCH__ < CC_VOLTA
  3822. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3823. #endif // __CUDA_ARCH__ < CC_VOLTA
  3824. mul_mat_q3_K(
  3825. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3826. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3827. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3828. #if defined(RDNA3) || defined(RDNA2)
  3829. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3830. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3831. const int nwarps = NWARPS_Q3_K_RDNA2;
  3832. #else
  3833. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3834. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3835. const int nwarps = NWARPS_Q3_K_RDNA1;
  3836. #endif // defined(RDNA3) || defined(RDNA2)
  3837. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3838. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3839. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3840. #elif __CUDA_ARCH__ >= CC_VOLTA
  3841. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3842. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3843. const int nwarps = NWARPS_Q3_K_AMPERE;
  3844. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3845. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3846. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3847. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3848. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3849. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3850. const int nwarps = NWARPS_Q3_K_PASCAL;
  3851. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3852. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3853. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3854. #else
  3855. (void) vec_dot_q3_K_q8_1_mul_mat;
  3856. bad_arch();
  3857. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3858. }
  3859. #define MMQ_X_Q4_K_RDNA2 64
  3860. #define MMQ_Y_Q4_K_RDNA2 128
  3861. #define NWARPS_Q4_K_RDNA2 8
  3862. #define MMQ_X_Q4_K_RDNA1 32
  3863. #define MMQ_Y_Q4_K_RDNA1 64
  3864. #define NWARPS_Q4_K_RDNA1 8
  3865. #if defined(CUDA_USE_TENSOR_CORES)
  3866. #define MMQ_X_Q4_K_AMPERE 4
  3867. #define MMQ_Y_Q4_K_AMPERE 32
  3868. #define NWARPS_Q4_K_AMPERE 4
  3869. #else
  3870. #define MMQ_X_Q4_K_AMPERE 64
  3871. #define MMQ_Y_Q4_K_AMPERE 128
  3872. #define NWARPS_Q4_K_AMPERE 4
  3873. #endif
  3874. #define MMQ_X_Q4_K_PASCAL 64
  3875. #define MMQ_Y_Q4_K_PASCAL 64
  3876. #define NWARPS_Q4_K_PASCAL 8
  3877. template <bool need_check> static __global__ void
  3878. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3879. #if defined(RDNA3) || defined(RDNA2)
  3880. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3881. #endif // defined(RDNA3) || defined(RDNA2)
  3882. #elif __CUDA_ARCH__ < CC_VOLTA
  3883. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3884. #endif // __CUDA_ARCH__ < CC_VOLTA
  3885. mul_mat_q4_K(
  3886. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3887. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3888. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3889. #if defined(RDNA3) || defined(RDNA2)
  3890. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3891. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3892. const int nwarps = NWARPS_Q4_K_RDNA2;
  3893. #else
  3894. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3895. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3896. const int nwarps = NWARPS_Q4_K_RDNA1;
  3897. #endif // defined(RDNA3) || defined(RDNA2)
  3898. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3899. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3900. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3901. #elif __CUDA_ARCH__ >= CC_VOLTA
  3902. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3903. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3904. const int nwarps = NWARPS_Q4_K_AMPERE;
  3905. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3906. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3907. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3908. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3909. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3910. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3911. const int nwarps = NWARPS_Q4_K_PASCAL;
  3912. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3913. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3914. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3915. #else
  3916. (void) vec_dot_q4_K_q8_1_mul_mat;
  3917. bad_arch();
  3918. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3919. }
  3920. #define MMQ_X_Q5_K_RDNA2 64
  3921. #define MMQ_Y_Q5_K_RDNA2 128
  3922. #define NWARPS_Q5_K_RDNA2 8
  3923. #define MMQ_X_Q5_K_RDNA1 32
  3924. #define MMQ_Y_Q5_K_RDNA1 64
  3925. #define NWARPS_Q5_K_RDNA1 8
  3926. #if defined(CUDA_USE_TENSOR_CORES)
  3927. #define MMQ_X_Q5_K_AMPERE 4
  3928. #define MMQ_Y_Q5_K_AMPERE 32
  3929. #define NWARPS_Q5_K_AMPERE 4
  3930. #else
  3931. #define MMQ_X_Q5_K_AMPERE 64
  3932. #define MMQ_Y_Q5_K_AMPERE 128
  3933. #define NWARPS_Q5_K_AMPERE 4
  3934. #endif
  3935. #define MMQ_X_Q5_K_PASCAL 64
  3936. #define MMQ_Y_Q5_K_PASCAL 64
  3937. #define NWARPS_Q5_K_PASCAL 8
  3938. template <bool need_check> static __global__ void
  3939. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3940. #if defined(RDNA3) || defined(RDNA2)
  3941. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3942. #endif // defined(RDNA3) || defined(RDNA2)
  3943. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3944. mul_mat_q5_K(
  3945. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3946. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3947. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3948. #if defined(RDNA3) || defined(RDNA2)
  3949. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3950. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3951. const int nwarps = NWARPS_Q5_K_RDNA2;
  3952. #else
  3953. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3954. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3955. const int nwarps = NWARPS_Q5_K_RDNA1;
  3956. #endif // defined(RDNA3) || defined(RDNA2)
  3957. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3958. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3959. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3960. #elif __CUDA_ARCH__ >= CC_VOLTA
  3961. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3962. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3963. const int nwarps = NWARPS_Q5_K_AMPERE;
  3964. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3965. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3966. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3967. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3968. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3969. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3970. const int nwarps = NWARPS_Q5_K_PASCAL;
  3971. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3972. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3973. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3974. #else
  3975. (void) vec_dot_q5_K_q8_1_mul_mat;
  3976. bad_arch();
  3977. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3978. }
  3979. #define MMQ_X_Q6_K_RDNA2 64
  3980. #define MMQ_Y_Q6_K_RDNA2 128
  3981. #define NWARPS_Q6_K_RDNA2 8
  3982. #define MMQ_X_Q6_K_RDNA1 32
  3983. #define MMQ_Y_Q6_K_RDNA1 64
  3984. #define NWARPS_Q6_K_RDNA1 8
  3985. #if defined(CUDA_USE_TENSOR_CORES)
  3986. #define MMQ_X_Q6_K_AMPERE 4
  3987. #define MMQ_Y_Q6_K_AMPERE 32
  3988. #define NWARPS_Q6_K_AMPERE 4
  3989. #else
  3990. #define MMQ_X_Q6_K_AMPERE 64
  3991. #define MMQ_Y_Q6_K_AMPERE 64
  3992. #define NWARPS_Q6_K_AMPERE 4
  3993. #endif
  3994. #define MMQ_X_Q6_K_PASCAL 64
  3995. #define MMQ_Y_Q6_K_PASCAL 64
  3996. #define NWARPS_Q6_K_PASCAL 8
  3997. template <bool need_check> static __global__ void
  3998. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3999. #if defined(RDNA3) || defined(RDNA2)
  4000. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4001. #endif // defined(RDNA3) || defined(RDNA2)
  4002. #elif __CUDA_ARCH__ < CC_VOLTA
  4003. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4004. #endif // __CUDA_ARCH__ < CC_VOLTA
  4005. mul_mat_q6_K(
  4006. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4007. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4008. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4009. #if defined(RDNA3) || defined(RDNA2)
  4010. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  4011. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  4012. const int nwarps = NWARPS_Q6_K_RDNA2;
  4013. #else
  4014. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  4015. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  4016. const int nwarps = NWARPS_Q6_K_RDNA1;
  4017. #endif // defined(RDNA3) || defined(RDNA2)
  4018. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4019. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4020. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4021. #elif __CUDA_ARCH__ >= CC_VOLTA
  4022. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  4023. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  4024. const int nwarps = NWARPS_Q6_K_AMPERE;
  4025. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4026. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4027. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4028. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4029. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  4030. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  4031. const int nwarps = NWARPS_Q6_K_PASCAL;
  4032. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4033. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4034. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4035. #else
  4036. (void) vec_dot_q6_K_q8_1_mul_mat;
  4037. bad_arch();
  4038. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4039. }
  4040. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  4041. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  4042. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4043. if (row >= nrows) {
  4044. return;
  4045. }
  4046. const int blocks_per_row = ncols / qk;
  4047. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  4048. // partial sum for each thread
  4049. float tmp = 0.0f;
  4050. const block_q_t * x = (const block_q_t *) vx;
  4051. const block_q8_1 * y = (const block_q8_1 *) vy;
  4052. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  4053. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  4054. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  4055. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  4056. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  4057. }
  4058. // sum up partial sums and write back result
  4059. #pragma unroll
  4060. for (int mask = 16; mask > 0; mask >>= 1) {
  4061. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4062. }
  4063. if (threadIdx.x == 0) {
  4064. dst[row] = tmp;
  4065. }
  4066. }
  4067. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  4068. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  4069. // qk = quantized weights per x block
  4070. // qr = number of quantized weights per data value in x block
  4071. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4072. if (row >= nrows) {
  4073. return;
  4074. }
  4075. const int tid = threadIdx.x;
  4076. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  4077. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  4078. const int y_offset = qr == 1 ? 1 : qk/2;
  4079. // partial sum for each thread
  4080. #ifdef GGML_CUDA_F16
  4081. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  4082. #else
  4083. float tmp = 0.0f;
  4084. #endif // GGML_CUDA_F16
  4085. for (int i = 0; i < ncols; i += iter_stride) {
  4086. const int col = i + vals_per_iter*tid;
  4087. const int ib = (row*ncols + col)/qk; // x block index
  4088. const int iqs = (col%qk)/qr; // x quant index
  4089. const int iybs = col - col%qk; // y block start index
  4090. // processing >2 values per i iter is faster for fast GPUs
  4091. #pragma unroll
  4092. for (int j = 0; j < vals_per_iter; j += 2) {
  4093. // process 2 vals per j iter
  4094. // dequantize
  4095. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  4096. dfloat2 v;
  4097. dequantize_kernel(vx, ib, iqs + j/qr, v);
  4098. // matrix multiplication
  4099. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  4100. #ifdef GGML_CUDA_F16
  4101. tmp += __hmul2(v, {
  4102. y[iybs + iqs + j/qr + 0],
  4103. y[iybs + iqs + j/qr + y_offset]
  4104. });
  4105. #else
  4106. tmp += v.x * y[iybs + iqs + j/qr + 0];
  4107. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  4108. #endif // GGML_CUDA_F16
  4109. }
  4110. }
  4111. // sum up partial sums and write back result
  4112. #pragma unroll
  4113. for (int mask = 16; mask > 0; mask >>= 1) {
  4114. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4115. }
  4116. if (tid == 0) {
  4117. #ifdef GGML_CUDA_F16
  4118. dst[row] = tmp.x + tmp.y;
  4119. #else
  4120. dst[row] = tmp;
  4121. #endif // GGML_CUDA_F16
  4122. }
  4123. }
  4124. static __global__ void mul_mat_p021_f16_f32(
  4125. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  4126. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  4127. const half * x = (const half *) vx;
  4128. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4129. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4130. const int channel_x = channel / (nchannels_y / nchannels_x);
  4131. const int nrows_y = ncols_x;
  4132. const int nrows_dst = nrows_x;
  4133. const int row_dst = row_x;
  4134. float tmp = 0.0f;
  4135. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4136. const int col_x = col_x0 + threadIdx.x;
  4137. if (col_x >= ncols_x) {
  4138. break;
  4139. }
  4140. // x is transposed and permuted
  4141. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  4142. const float xi = __half2float(x[ix]);
  4143. const int row_y = col_x;
  4144. // y is not transposed but permuted
  4145. const int iy = channel*nrows_y + row_y;
  4146. tmp += xi * y[iy];
  4147. }
  4148. // dst is not transposed and not permuted
  4149. const int idst = channel*nrows_dst + row_dst;
  4150. // sum up partial sums and write back result
  4151. #pragma unroll
  4152. for (int mask = 16; mask > 0; mask >>= 1) {
  4153. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4154. }
  4155. if (threadIdx.x == 0) {
  4156. dst[idst] = tmp;
  4157. }
  4158. }
  4159. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  4160. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  4161. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  4162. const half * x = (const half *) vx;
  4163. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4164. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4165. const int channel_x = channel / channel_x_divisor;
  4166. const int nrows_y = ncols_x;
  4167. const int nrows_dst = nrows_x;
  4168. const int row_dst = row_x;
  4169. const int idst = channel*nrows_dst + row_dst;
  4170. float tmp = 0.0f;
  4171. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4172. const int col_x = col_x0 + threadIdx.x;
  4173. if (col_x >= ncols_x) {
  4174. break;
  4175. }
  4176. const int row_y = col_x;
  4177. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  4178. const int iy = channel*nrows_y + row_y;
  4179. const float xi = __half2float(x[ix]);
  4180. tmp += xi * y[iy];
  4181. }
  4182. // sum up partial sums and write back result
  4183. #pragma unroll
  4184. for (int mask = 16; mask > 0; mask >>= 1) {
  4185. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4186. }
  4187. if (threadIdx.x == 0) {
  4188. dst[idst] = tmp;
  4189. }
  4190. }
  4191. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4192. const float * xi = (const float *) cxi;
  4193. float * dsti = (float *) cdsti;
  4194. *dsti = *xi;
  4195. }
  4196. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4197. const float * xi = (const float *) cxi;
  4198. half * dsti = (half *) cdsti;
  4199. *dsti = __float2half(*xi);
  4200. }
  4201. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4202. const half * xi = (const half *) cxi;
  4203. half * dsti = (half *) cdsti;
  4204. *dsti = *xi;
  4205. }
  4206. template <cpy_kernel_t cpy_1>
  4207. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4208. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4209. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  4210. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4211. if (i >= ne) {
  4212. return;
  4213. }
  4214. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4215. // then combine those indices with the corresponding byte offsets to get the total offsets
  4216. const int i02 = i / (ne00*ne01);
  4217. const int i01 = (i - i02*ne01*ne00) / ne00;
  4218. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  4219. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  4220. const int i12 = i / (ne10*ne11);
  4221. const int i11 = (i - i12*ne10*ne11) / ne10;
  4222. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  4223. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  4224. cpy_1(cx + x_offset, cdst + dst_offset);
  4225. }
  4226. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4227. const float * xi = (const float *) cxi;
  4228. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4229. float amax = 0.0f; // absolute max
  4230. for (int j = 0; j < QK8_0; j++) {
  4231. const float v = xi[j];
  4232. amax = fmaxf(amax, fabsf(v));
  4233. }
  4234. const float d = amax / ((1 << 7) - 1);
  4235. const float id = d ? 1.0f/d : 0.0f;
  4236. dsti->d = d;
  4237. for (int j = 0; j < QK8_0; ++j) {
  4238. const float x0 = xi[j]*id;
  4239. dsti->qs[j] = roundf(x0);
  4240. }
  4241. }
  4242. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4243. const float * xi = (const float *) cxi;
  4244. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4245. float amax = 0.0f;
  4246. float vmax = 0.0f;
  4247. for (int j = 0; j < QK4_0; ++j) {
  4248. const float v = xi[j];
  4249. if (amax < fabsf(v)) {
  4250. amax = fabsf(v);
  4251. vmax = v;
  4252. }
  4253. }
  4254. const float d = vmax / -8;
  4255. const float id = d ? 1.0f/d : 0.0f;
  4256. dsti->d = d;
  4257. for (int j = 0; j < QK4_0/2; ++j) {
  4258. const float x0 = xi[0 + j]*id;
  4259. const float x1 = xi[QK4_0/2 + j]*id;
  4260. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4261. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4262. dsti->qs[j] = xi0;
  4263. dsti->qs[j] |= xi1 << 4;
  4264. }
  4265. }
  4266. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4267. const float * xi = (const float *) cxi;
  4268. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4269. float vmin = FLT_MAX;
  4270. float vmax = -FLT_MAX;
  4271. for (int j = 0; j < QK4_1; ++j) {
  4272. const float v = xi[j];
  4273. if (v < vmin) vmin = v;
  4274. if (v > vmax) vmax = v;
  4275. }
  4276. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4277. const float id = d ? 1.0f/d : 0.0f;
  4278. dsti->dm.x = d;
  4279. dsti->dm.y = vmin;
  4280. for (int j = 0; j < QK4_1/2; ++j) {
  4281. const float x0 = (xi[0 + j] - vmin)*id;
  4282. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4283. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4284. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4285. dsti->qs[j] = xi0;
  4286. dsti->qs[j] |= xi1 << 4;
  4287. }
  4288. }
  4289. template <cpy_kernel_t cpy_blck, int qk>
  4290. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4291. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4292. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  4293. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4294. if (i >= ne) {
  4295. return;
  4296. }
  4297. const int i02 = i / (ne00*ne01);
  4298. const int i01 = (i - i02*ne01*ne00) / ne00;
  4299. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  4300. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  4301. const int i12 = i / (ne10*ne11);
  4302. const int i11 = (i - i12*ne10*ne11) / ne10;
  4303. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  4304. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  4305. cpy_blck(cx + x_offset, cdst + dst_offset);
  4306. }
  4307. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4308. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4309. return 1.0f - min(1.0f, max(0.0f, y));
  4310. }
  4311. struct rope_corr_dims {
  4312. float v[4];
  4313. };
  4314. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4315. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4316. static __device__ void rope_yarn(
  4317. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4318. float * cos_theta, float * sin_theta
  4319. ) {
  4320. // Get n-d rotational scaling corrected for extrapolation
  4321. float theta_interp = freq_scale * theta_extrap;
  4322. float theta = theta_interp;
  4323. if (ext_factor != 0.0f) {
  4324. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4325. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4326. // Get n-d magnitude scaling corrected for interpolation
  4327. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4328. }
  4329. *cos_theta = cosf(theta) * mscale;
  4330. *sin_theta = sinf(theta) * mscale;
  4331. }
  4332. // rope == RoPE == rotary positional embedding
  4333. template<typename T, bool has_pos>
  4334. static __global__ void rope(
  4335. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4336. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4337. ) {
  4338. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4339. if (col >= ncols) {
  4340. return;
  4341. }
  4342. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4343. const int i = row*ncols + col;
  4344. const int i2 = row/p_delta_rows;
  4345. const int p = has_pos ? pos[i2] : 0;
  4346. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4347. float cos_theta, sin_theta;
  4348. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4349. const float x0 = x[i + 0];
  4350. const float x1 = x[i + 1];
  4351. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4352. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4353. }
  4354. template<typename T, bool has_pos>
  4355. static __global__ void rope_neox(
  4356. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4357. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4358. ) {
  4359. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4360. if (col >= ncols) {
  4361. return;
  4362. }
  4363. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4364. const int ib = col / n_dims;
  4365. const int ic = col % n_dims;
  4366. if (ib > 0) {
  4367. const int i = row*ncols + ib*n_dims + ic;
  4368. dst[i + 0] = x[i + 0];
  4369. dst[i + 1] = x[i + 1];
  4370. return;
  4371. }
  4372. const int i = row*ncols + ib*n_dims + ic/2;
  4373. const int i2 = row/p_delta_rows;
  4374. float cur_rot = inv_ndims * ic - ib;
  4375. const int p = has_pos ? pos[i2] : 0;
  4376. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4377. float cos_theta, sin_theta;
  4378. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4379. const float x0 = x[i + 0];
  4380. const float x1 = x[i + n_dims/2];
  4381. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4382. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4383. }
  4384. static __global__ void rope_glm_f32(
  4385. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4386. int n_ctx
  4387. ) {
  4388. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4389. const int half_n_dims = ncols/4;
  4390. if (col >= half_n_dims) {
  4391. return;
  4392. }
  4393. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4394. const int i = row*ncols + col;
  4395. const int i2 = row/p_delta_rows;
  4396. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4397. // FIXME: this is likely wrong
  4398. const int p = pos != nullptr ? pos[i2] : 0;
  4399. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4400. const float sin_theta = sinf(theta);
  4401. const float cos_theta = cosf(theta);
  4402. const float x0 = x[i + 0];
  4403. const float x1 = x[i + half_n_dims];
  4404. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4405. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4406. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4407. const float sin_block_theta = sinf(block_theta);
  4408. const float cos_block_theta = cosf(block_theta);
  4409. const float x2 = x[i + half_n_dims * 2];
  4410. const float x3 = x[i + half_n_dims * 3];
  4411. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4412. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4413. }
  4414. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4415. const int n_heads_log2_floor, const float m0, const float m1) {
  4416. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4417. if (col >= ncols) {
  4418. return;
  4419. }
  4420. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4421. const int i = row*ncols + col;
  4422. const int k = row/k_rows;
  4423. float m_k;
  4424. if (k < n_heads_log2_floor) {
  4425. m_k = powf(m0, k + 1);
  4426. } else {
  4427. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4428. }
  4429. dst[i] = col * m_k + x[i];
  4430. }
  4431. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4432. const int row = blockIdx.y;
  4433. const int col = threadIdx.x;
  4434. float sum = 0.0f;
  4435. for (int i = col; i < ncols; i += blockDim.x) {
  4436. sum += x[row * ncols + i];
  4437. }
  4438. sum = warp_reduce_sum(sum);
  4439. if (col == 0) {
  4440. dst[row] = sum;
  4441. }
  4442. }
  4443. template<typename T>
  4444. static inline __device__ void swap(T & a, T & b) {
  4445. T tmp = a;
  4446. a = b;
  4447. b = tmp;
  4448. }
  4449. template<ggml_sort_order order>
  4450. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4451. // bitonic sort
  4452. int col = threadIdx.x;
  4453. int row = blockIdx.y;
  4454. if (col >= ncols) return;
  4455. const float * x_row = x + row * ncols;
  4456. int * dst_row = dst + row * ncols;
  4457. // initialize indices
  4458. if (col < ncols) {
  4459. dst_row[col] = col;
  4460. }
  4461. __syncthreads();
  4462. for (int k = 2; k <= ncols; k *= 2) {
  4463. for (int j = k / 2; j > 0; j /= 2) {
  4464. int ixj = col ^ j;
  4465. if (ixj > col) {
  4466. if ((col & k) == 0) {
  4467. 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]]) {
  4468. swap(dst_row[col], dst_row[ixj]);
  4469. }
  4470. } else {
  4471. 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]]) {
  4472. swap(dst_row[col], dst_row[ixj]);
  4473. }
  4474. }
  4475. }
  4476. __syncthreads();
  4477. }
  4478. }
  4479. }
  4480. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4481. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4482. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4483. if (col >= ncols) {
  4484. return;
  4485. }
  4486. const int i = row*ncols + col;
  4487. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4488. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4489. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4490. }
  4491. template <bool vals_smem, int ncols_template, int block_size_template, bool need_check>
  4492. static __global__ void soft_max_f16(const float * x, const float * y, float * dst, const int ncols_par, const int nrows_y, const float scale) {
  4493. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  4494. const int ncols_data = ncols_template == 0 ? ncols_par : ncols_template;
  4495. const int ncols_smem = GGML_PAD(ncols_data, 2*WARP_SIZE)/2;
  4496. const int tid = threadIdx.x;
  4497. const int rowx = blockIdx.x;
  4498. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4499. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  4500. const int warp_id = threadIdx.x / WARP_SIZE;
  4501. const int lane_id = threadIdx.x % WARP_SIZE;
  4502. extern __shared__ half data_soft_max_f16[];
  4503. half * buf_iw = data_soft_max_f16 + 0; // shared memory buffer for inter-warp communication
  4504. // (shared memory) buffer to cache values between iterations:
  4505. half2 * vals = vals_smem ? (half2 *) (buf_iw + WARP_SIZE) : (half2 *) (dst + rowx*ncols_data);
  4506. // if the buffer is larger than max. shared memory per block, use dst as temp. buffer instead
  4507. // in that case col_smem == col_data must be enforced to avoid race conditions
  4508. half2 max_val = make_half2(-INFINITY, -INFINITY);
  4509. #pragma unroll
  4510. for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {
  4511. const int col_data = 2*col0 + 2*WARP_SIZE*warp_id + lane_id;
  4512. const int col_smem = vals_smem ? col0 + tid : col_data;
  4513. const int ix = rowx*ncols_data + col_data;
  4514. const int iy = rowy*ncols_data + col_data;
  4515. half2 val;
  4516. if (need_check && col_data + 0 >= ncols_data) {
  4517. val.x = -INFINITY;
  4518. } else {
  4519. val.x = x[ix + 0]*scale + (y ? y[iy + 0] : 0.0f);
  4520. }
  4521. if (need_check && col_data + WARP_SIZE >= ncols_data) {
  4522. val.y = -INFINITY;
  4523. } else {
  4524. val.y = x[ix + WARP_SIZE]*scale + (y ? y[iy + WARP_SIZE] : 0.0f);
  4525. }
  4526. if (!need_check || col_smem < (vals_smem ? ncols_smem : ncols_data)) {
  4527. vals[col_smem] = val;
  4528. }
  4529. max_val = __hmax2(max_val, val);
  4530. }
  4531. // find the max value in the block
  4532. max_val = warp_reduce_max(max_val);
  4533. if (block_size > WARP_SIZE) {
  4534. if (warp_id == 0) {
  4535. buf_iw[lane_id] = -INFINITY;
  4536. }
  4537. __syncthreads();
  4538. if (lane_id == 0) {
  4539. buf_iw[warp_id] = __hmax(max_val.x, max_val.y);
  4540. }
  4541. __syncthreads();
  4542. max_val = __half2half2(buf_iw[lane_id]);
  4543. max_val = warp_reduce_max(max_val);
  4544. } else {
  4545. max_val = __half2half2(__hmax(max_val.x, max_val.y));
  4546. }
  4547. half2 tmp = make_half2(0.0f, 0.0f); // partial sums
  4548. #pragma unroll
  4549. for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {
  4550. const int col_smem = vals_smem ? col0 + tid : 2*col0 + 2*warp_id*WARP_SIZE + lane_id;
  4551. if (ncols_template == 0 && col_smem >= (vals_smem ? ncols_smem : ncols_data)) {
  4552. break;
  4553. }
  4554. const half2 val = h2exp(vals[col_smem] - max_val);
  4555. tmp += val;
  4556. vals[col_smem] = val;
  4557. }
  4558. // find the sum of exps in the block
  4559. tmp = warp_reduce_sum(tmp);
  4560. if (block_size > WARP_SIZE) {
  4561. if (warp_id == 0) {
  4562. buf_iw[lane_id] = 0.0f;
  4563. }
  4564. __syncthreads();
  4565. if (lane_id == 0) {
  4566. buf_iw[warp_id] = tmp.x + tmp.y;
  4567. }
  4568. __syncthreads();
  4569. tmp = __half2half2(buf_iw[lane_id]);
  4570. tmp = warp_reduce_sum(tmp);
  4571. } else {
  4572. tmp = __half2half2(tmp.x + tmp.y);
  4573. }
  4574. const half2 inv_sum = make_half2(1.0f, 1.0f) / tmp;
  4575. #pragma unroll
  4576. for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {
  4577. const int col_data = 2*col0 + 2*WARP_SIZE*warp_id + lane_id;
  4578. const int col_smem = vals_smem ? col0 + tid : col_data;
  4579. const int idst = rowx*ncols_data + col_data;
  4580. const half2 result = vals[col_smem] * inv_sum;
  4581. if (need_check && col_data + 0 >= ncols_data) {
  4582. return;
  4583. }
  4584. dst[idst] = result.x;
  4585. if (need_check && col_data + WARP_SIZE >= ncols_data) {
  4586. return;
  4587. }
  4588. dst[idst + WARP_SIZE] = result.y;
  4589. }
  4590. #else
  4591. (void) x; (void) y; (void) dst; (void) ncols_par; (void) nrows_y; (void) scale;
  4592. bad_arch();
  4593. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  4594. }
  4595. template <bool vals_smem, int ncols_template, int block_size_template>
  4596. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols_par, const int nrows_y, const float scale) {
  4597. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  4598. const int tid = threadIdx.x;
  4599. const int rowx = blockIdx.x;
  4600. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4601. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  4602. const int warp_id = threadIdx.x / WARP_SIZE;
  4603. const int lane_id = threadIdx.x % WARP_SIZE;
  4604. extern __shared__ float data_soft_max_f32[];
  4605. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  4606. // shared memory buffer to cache values between iterations:
  4607. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  4608. float max_val = -INFINITY;
  4609. #pragma unroll
  4610. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4611. const int col = col0 + tid;
  4612. if (ncols_template == 0 && col >= ncols) {
  4613. break;
  4614. }
  4615. const int ix = rowx*ncols + col;
  4616. const int iy = rowy*ncols + col;
  4617. const float val = x[ix]*scale + (y ? y[iy] : 0.0f);
  4618. vals[col] = val;
  4619. max_val = max(max_val, val);
  4620. }
  4621. // find the max value in the block
  4622. max_val = warp_reduce_max(max_val);
  4623. if (block_size > WARP_SIZE) {
  4624. if (warp_id == 0) {
  4625. buf_iw[lane_id] = -INFINITY;
  4626. }
  4627. __syncthreads();
  4628. if (lane_id == 0) {
  4629. buf_iw[warp_id] = max_val;
  4630. }
  4631. __syncthreads();
  4632. max_val = buf_iw[lane_id];
  4633. max_val = warp_reduce_max(max_val);
  4634. }
  4635. float tmp = 0.0f; // partial sum
  4636. #pragma unroll
  4637. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4638. const int col = col0 + tid;
  4639. if (ncols_template == 0 && col >= ncols) {
  4640. break;
  4641. }
  4642. const float val = expf(vals[col] - max_val);
  4643. tmp += val;
  4644. vals[col] = val;
  4645. }
  4646. // find the sum of exps in the block
  4647. tmp = warp_reduce_sum(tmp);
  4648. if (block_size > WARP_SIZE) {
  4649. if (warp_id == 0) {
  4650. buf_iw[lane_id] = 0.0f;
  4651. }
  4652. __syncthreads();
  4653. if (lane_id == 0) {
  4654. buf_iw[warp_id] = tmp;
  4655. }
  4656. __syncthreads();
  4657. tmp = buf_iw[lane_id];
  4658. tmp = warp_reduce_sum(tmp);
  4659. }
  4660. const float inv_sum = 1.0f / tmp;
  4661. #pragma unroll
  4662. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4663. const int col = col0 + tid;
  4664. if (ncols_template == 0 && col >= ncols) {
  4665. return;
  4666. }
  4667. const int idst = rowx*ncols + col;
  4668. dst[idst] = vals[col] * inv_sum;
  4669. }
  4670. }
  4671. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4672. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4673. if (i >= k) {
  4674. return;
  4675. }
  4676. dst[i] = scale * x[i];
  4677. }
  4678. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4679. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4680. if (i >= k) {
  4681. return;
  4682. }
  4683. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4684. }
  4685. static __global__ void im2col_f32_f16(
  4686. const float * x, half * dst,
  4687. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4688. int s0, int s1, int p0, int p1, int d0, int d1) {
  4689. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4690. if (i >= pelements) {
  4691. return;
  4692. }
  4693. const int ksize = OW * (KH > 1 ? KW : 1);
  4694. const int kx = i / ksize;
  4695. const int kd = kx * ksize;
  4696. const int ky = (i - kd) / OW;
  4697. const int ix = i % OW;
  4698. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4699. const int64_t iih = blockIdx.y * s1 + ky * d1 - p1;
  4700. const int64_t offset_dst =
  4701. (blockIdx.y * OW + ix) * CHW +
  4702. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4703. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4704. dst[offset_dst] = __float2half(0.0f);
  4705. } else {
  4706. const int64_t offset_src = blockIdx.z * offset_delta;
  4707. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4708. }
  4709. }
  4710. template<int qk, int qr, dequantize_kernel_t dq>
  4711. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4712. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4713. GGML_TENSOR_BINARY_OP_LOCALS
  4714. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4715. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4716. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4717. // strides in elements
  4718. //const size_t s0 = nb0 / ggml_element_size(dst);
  4719. const size_t s1 = nb1 / ggml_element_size(dst);
  4720. const size_t s2 = nb2 / ggml_element_size(dst);
  4721. const size_t s3 = nb3 / ggml_element_size(dst);
  4722. const size_t s10 = nb10 / ggml_element_size(src1);
  4723. const size_t s11 = nb11 / ggml_element_size(src1);
  4724. const size_t s12 = nb12 / ggml_element_size(src1);
  4725. //const size_t s13 = nb13 / ggml_element_size(src1);
  4726. GGML_ASSERT(ne00 % 2 == 0);
  4727. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4728. src0_dd, src1_dd, dst_dd,
  4729. ne00, /*ne01, ne02, ne03,*/
  4730. /*ne10, ne11,*/ ne12, /*ne13,*/
  4731. /* s0,*/ s1, s2, s3,
  4732. /* nb00,*/ nb01, nb02, nb03,
  4733. s10, s11, s12/*, s13*/);
  4734. (void) dst;
  4735. }
  4736. template<typename src0_t>
  4737. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4738. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4739. GGML_TENSOR_BINARY_OP_LOCALS
  4740. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4741. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4742. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4743. // strides in elements
  4744. //const size_t s0 = nb0 / ggml_element_size(dst);
  4745. const size_t s1 = nb1 / ggml_element_size(dst);
  4746. const size_t s2 = nb2 / ggml_element_size(dst);
  4747. const size_t s3 = nb3 / ggml_element_size(dst);
  4748. const size_t s10 = nb10 / ggml_element_size(src1);
  4749. const size_t s11 = nb11 / ggml_element_size(src1);
  4750. const size_t s12 = nb12 / ggml_element_size(src1);
  4751. //const size_t s13 = nb13 / ggml_element_size(src1);
  4752. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4753. src0_dd, src1_dd, dst_dd,
  4754. ne00, /*ne01, ne02, ne03,*/
  4755. /*ne10, ne11,*/ ne12, /*ne13,*/
  4756. /* s0,*/ s1, s2, s3,
  4757. /* nb00,*/ nb01, nb02, nb03,
  4758. s10, s11, s12/*, s13*/);
  4759. (void) dst;
  4760. }
  4761. template<float (*bin_op)(const float, const float)>
  4762. struct bin_bcast_cuda {
  4763. template<typename src0_t, typename src1_t, typename dst_t>
  4764. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4765. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4766. cudaStream_t stream) {
  4767. GGML_TENSOR_BINARY_OP_LOCALS
  4768. int nr0 = ne10/ne0;
  4769. int nr1 = ne11/ne1;
  4770. int nr2 = ne12/ne2;
  4771. int nr3 = ne13/ne3;
  4772. int nr[4] = { nr0, nr1, nr2, nr3 };
  4773. // collapse dimensions until first broadcast dimension
  4774. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4775. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4776. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4777. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4778. auto collapse = [](int64_t cne[]) {
  4779. cne[0] *= cne[1];
  4780. cne[1] = cne[2];
  4781. cne[2] = cne[3];
  4782. cne[3] = 1;
  4783. };
  4784. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  4785. cnb[1] *= cne[1];
  4786. cnb[2] *= cne[2];
  4787. cnb[3] *= cne[3];
  4788. };
  4789. for (int i = 0; i < 4; i++) {
  4790. if (nr[i] != 1) {
  4791. break;
  4792. }
  4793. if (i > 0) {
  4794. collapse_nb(cnb0, cne0);
  4795. collapse_nb(cnb1, cne1);
  4796. collapse(cne0);
  4797. collapse(cne1);
  4798. }
  4799. }
  4800. {
  4801. int64_t ne0 = cne0[0];
  4802. int64_t ne1 = cne0[1];
  4803. int64_t ne2 = cne0[2];
  4804. int64_t ne3 = cne0[3];
  4805. int64_t ne10 = cne1[0];
  4806. int64_t ne11 = cne1[1];
  4807. int64_t ne12 = cne1[2];
  4808. int64_t ne13 = cne1[3];
  4809. size_t nb0 = cnb0[0];
  4810. size_t nb1 = cnb0[1];
  4811. size_t nb2 = cnb0[2];
  4812. size_t nb3 = cnb0[3];
  4813. size_t nb10 = cnb1[0];
  4814. size_t nb11 = cnb1[1];
  4815. size_t nb12 = cnb1[2];
  4816. size_t nb13 = cnb1[3];
  4817. size_t s0 = nb0 / sizeof(dst_t);
  4818. size_t s1 = nb1 / sizeof(dst_t);
  4819. size_t s2 = nb2 / sizeof(dst_t);
  4820. size_t s3 = nb3 / sizeof(dst_t);
  4821. size_t s10 = nb10 / sizeof(src1_t);
  4822. size_t s11 = nb11 / sizeof(src1_t);
  4823. size_t s12 = nb12 / sizeof(src1_t);
  4824. size_t s13 = nb13 / sizeof(src1_t);
  4825. GGML_ASSERT(s0 == 1);
  4826. GGML_ASSERT(s10 == 1);
  4827. const int block_size = 128;
  4828. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4829. dim3 block_dims;
  4830. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4831. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4832. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4833. dim3 block_nums(
  4834. (hne0 + block_dims.x - 1) / block_dims.x,
  4835. (ne1 + block_dims.y - 1) / block_dims.y,
  4836. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4837. );
  4838. if (block_nums.z > 65535) {
  4839. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4840. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4841. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4842. src0_dd, src1_dd, dst_dd,
  4843. ne0, ne1, ne2, ne3,
  4844. ne10, ne11, ne12, ne13,
  4845. /* s0, */ s1, s2, s3,
  4846. /* s10, */ s11, s12, s13);
  4847. } else {
  4848. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4849. src0_dd, src1_dd, dst_dd,
  4850. ne0, ne1, ne2, ne3,
  4851. ne10, ne11, ne12, ne13,
  4852. /* s0, */ s1, s2, s3,
  4853. /* s10, */ s11, s12, s13);
  4854. }
  4855. }
  4856. }
  4857. };
  4858. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4859. const int ne10, const int ne11, const int ne12,
  4860. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4861. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4862. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4863. }
  4864. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4865. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4866. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4867. }
  4868. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4869. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4870. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4871. }
  4872. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4873. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4874. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4875. }
  4876. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4877. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4878. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4879. }
  4880. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4881. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4882. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4883. }
  4884. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4885. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4886. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4887. }
  4888. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4889. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4890. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4891. }
  4892. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4893. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4894. if (ncols < 1024) {
  4895. const dim3 block_dims(WARP_SIZE, 1, 1);
  4896. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4897. } else {
  4898. const dim3 block_dims(1024, 1, 1);
  4899. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4900. }
  4901. }
  4902. 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) {
  4903. static const float eps = 1e-6f;
  4904. if (group_size < 1024) {
  4905. const dim3 block_dims(WARP_SIZE, 1, 1);
  4906. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4907. } else {
  4908. const dim3 block_dims(1024, 1, 1);
  4909. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4910. }
  4911. }
  4912. 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) {
  4913. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4914. dim3 gridDim(num_blocks, ne1, ne2);
  4915. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4916. }
  4917. 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) {
  4918. int ne0 = (ne00 * scale_factor);
  4919. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4920. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4921. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4922. }
  4923. static void pad_f32_cuda(const float * x, float * dst,
  4924. const int ne00, const int ne01, const int ne02,
  4925. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4926. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4927. dim3 gridDim(num_blocks, ne1, ne2);
  4928. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4929. }
  4930. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4931. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4932. if (ncols < 1024) {
  4933. const dim3 block_dims(WARP_SIZE, 1, 1);
  4934. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4935. } else {
  4936. const dim3 block_dims(1024, 1, 1);
  4937. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4938. }
  4939. }
  4940. 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) {
  4941. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4942. const dim3 num_blocks(block_num_x, ky, 1);
  4943. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4944. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4945. }
  4946. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4947. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4948. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  4949. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4950. }
  4951. template<typename dst_t>
  4952. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4953. const int nb = k / QK_K;
  4954. #if QK_K == 256
  4955. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4956. #else
  4957. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4958. #endif
  4959. }
  4960. template<typename dst_t>
  4961. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4962. const int nb = k / QK_K;
  4963. #if QK_K == 256
  4964. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4965. #else
  4966. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4967. #endif
  4968. }
  4969. template<typename dst_t>
  4970. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4971. const int nb = k / QK_K;
  4972. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4973. }
  4974. template<typename dst_t>
  4975. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4976. const int nb = k / QK_K;
  4977. #if QK_K == 256
  4978. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4979. #else
  4980. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4981. #endif
  4982. }
  4983. template<typename dst_t>
  4984. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4985. const int nb = k / QK_K;
  4986. #if QK_K == 256
  4987. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4988. #else
  4989. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4990. #endif
  4991. }
  4992. template<typename dst_t>
  4993. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4994. const int nb = k / QK_K;
  4995. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  4996. }
  4997. template<typename dst_t>
  4998. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4999. const int nb = k / QK_K;
  5000. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  5001. }
  5002. template <typename src_t, typename dst_t>
  5003. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5004. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  5005. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5006. }
  5007. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  5008. switch (type) {
  5009. case GGML_TYPE_Q4_0:
  5010. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  5011. case GGML_TYPE_Q4_1:
  5012. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  5013. case GGML_TYPE_Q5_0:
  5014. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5015. case GGML_TYPE_Q5_1:
  5016. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5017. case GGML_TYPE_Q8_0:
  5018. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5019. case GGML_TYPE_Q2_K:
  5020. return dequantize_row_q2_K_cuda;
  5021. case GGML_TYPE_Q3_K:
  5022. return dequantize_row_q3_K_cuda;
  5023. case GGML_TYPE_Q4_K:
  5024. return dequantize_row_q4_K_cuda;
  5025. case GGML_TYPE_Q5_K:
  5026. return dequantize_row_q5_K_cuda;
  5027. case GGML_TYPE_Q6_K:
  5028. return dequantize_row_q6_K_cuda;
  5029. case GGML_TYPE_IQ2_XXS:
  5030. return dequantize_row_iq2_xxs_cuda;
  5031. case GGML_TYPE_IQ2_XS:
  5032. return dequantize_row_iq2_xs_cuda;
  5033. case GGML_TYPE_F32:
  5034. return convert_unary_cuda<float>;
  5035. default:
  5036. return nullptr;
  5037. }
  5038. }
  5039. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  5040. switch (type) {
  5041. case GGML_TYPE_Q4_0:
  5042. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  5043. case GGML_TYPE_Q4_1:
  5044. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  5045. case GGML_TYPE_Q5_0:
  5046. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5047. case GGML_TYPE_Q5_1:
  5048. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5049. case GGML_TYPE_Q8_0:
  5050. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5051. case GGML_TYPE_Q2_K:
  5052. return dequantize_row_q2_K_cuda;
  5053. case GGML_TYPE_Q3_K:
  5054. return dequantize_row_q3_K_cuda;
  5055. case GGML_TYPE_Q4_K:
  5056. return dequantize_row_q4_K_cuda;
  5057. case GGML_TYPE_Q5_K:
  5058. return dequantize_row_q5_K_cuda;
  5059. case GGML_TYPE_Q6_K:
  5060. return dequantize_row_q6_K_cuda;
  5061. case GGML_TYPE_IQ2_XXS:
  5062. return dequantize_row_iq2_xxs_cuda;
  5063. case GGML_TYPE_IQ2_XS:
  5064. return dequantize_row_iq2_xs_cuda;
  5065. case GGML_TYPE_F16:
  5066. return convert_unary_cuda<half>;
  5067. default:
  5068. return nullptr;
  5069. }
  5070. }
  5071. 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) {
  5072. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5073. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5074. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  5075. const dim3 block_nums(block_num_y, 1, 1);
  5076. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5077. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  5078. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5079. }
  5080. 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) {
  5081. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5082. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5083. const dim3 block_nums(block_num_y, 1, 1);
  5084. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5085. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  5086. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5087. }
  5088. 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) {
  5089. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5090. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5091. const dim3 block_nums(block_num_y, 1, 1);
  5092. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5093. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  5094. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5095. }
  5096. 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) {
  5097. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5098. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5099. const dim3 block_nums(block_num_y, 1, 1);
  5100. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5101. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  5102. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5103. }
  5104. 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) {
  5105. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5106. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5107. const dim3 block_nums(block_num_y, 1, 1);
  5108. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5109. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  5110. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5111. }
  5112. 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) {
  5113. GGML_ASSERT(ncols % QK_K == 0);
  5114. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  5115. const int block_num_y = (nrows + ny - 1) / ny;
  5116. const dim3 block_nums(block_num_y, 1, 1);
  5117. const dim3 block_dims(32, ny, 1);
  5118. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5119. }
  5120. 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) {
  5121. GGML_ASSERT(ncols % QK_K == 0);
  5122. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5123. const int block_num_y = (nrows + ny - 1) / ny;
  5124. const dim3 block_nums(block_num_y, 1, 1);
  5125. const dim3 block_dims(32, ny, 1);
  5126. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5127. }
  5128. 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) {
  5129. GGML_ASSERT(ncols % QK_K == 0);
  5130. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5131. const int block_num_y = (nrows + ny - 1) / ny;
  5132. const dim3 block_nums(block_num_y, 1, 1);
  5133. const dim3 block_dims(32, ny, 1);
  5134. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5135. }
  5136. 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) {
  5137. GGML_ASSERT(ncols % QK_K == 0);
  5138. const dim3 block_dims(32, 1, 1);
  5139. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  5140. }
  5141. 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) {
  5142. GGML_ASSERT(ncols % QK_K == 0);
  5143. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5144. const int block_num_y = (nrows + ny - 1) / ny;
  5145. const dim3 block_nums(block_num_y, 1, 1);
  5146. const dim3 block_dims(32, ny, 1);
  5147. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5148. }
  5149. 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) {
  5150. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5151. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5152. const dim3 block_nums(block_num_y, 1, 1);
  5153. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5154. dequantize_mul_mat_vec<1, 1, convert_f16>
  5155. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5156. }
  5157. 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) {
  5158. GGML_ASSERT(ncols % QK4_0 == 0);
  5159. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5160. const dim3 block_nums(block_num_y, 1, 1);
  5161. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5162. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  5163. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5164. }
  5165. 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) {
  5166. GGML_ASSERT(ncols % QK4_1 == 0);
  5167. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5168. const dim3 block_nums(block_num_y, 1, 1);
  5169. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5170. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  5171. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5172. }
  5173. 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) {
  5174. GGML_ASSERT(ncols % QK5_0 == 0);
  5175. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5176. const dim3 block_nums(block_num_y, 1, 1);
  5177. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5178. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  5179. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5180. }
  5181. 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) {
  5182. GGML_ASSERT(ncols % QK5_1 == 0);
  5183. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5184. const dim3 block_nums(block_num_y, 1, 1);
  5185. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5186. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  5187. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5188. }
  5189. 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) {
  5190. GGML_ASSERT(ncols % QK8_0 == 0);
  5191. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5192. const dim3 block_nums(block_num_y, 1, 1);
  5193. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5194. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  5195. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5196. }
  5197. 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) {
  5198. GGML_ASSERT(ncols % QK_K == 0);
  5199. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5200. const dim3 block_nums(block_num_y, 1, 1);
  5201. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5202. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  5203. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5204. }
  5205. 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) {
  5206. GGML_ASSERT(ncols % QK_K == 0);
  5207. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5208. const dim3 block_nums(block_num_y, 1, 1);
  5209. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5210. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  5211. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5212. }
  5213. 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) {
  5214. GGML_ASSERT(ncols % QK_K == 0);
  5215. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5216. const dim3 block_nums(block_num_y, 1, 1);
  5217. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5218. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  5219. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5220. }
  5221. 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) {
  5222. GGML_ASSERT(ncols % QK_K == 0);
  5223. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5224. const dim3 block_nums(block_num_y, 1, 1);
  5225. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5226. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  5227. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5228. }
  5229. 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) {
  5230. GGML_ASSERT(ncols % QK_K == 0);
  5231. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5232. const dim3 block_nums(block_num_y, 1, 1);
  5233. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5234. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  5235. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5236. }
  5237. static void mul_mat_vec_iq2_xxs_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5238. GGML_ASSERT(ncols % QK_K == 0);
  5239. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5240. const dim3 block_nums(block_num_y, 1, 1);
  5241. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5242. mul_mat_vec_q<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  5243. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5244. }
  5245. static void mul_mat_vec_iq2_xs_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5246. GGML_ASSERT(ncols % QK_K == 0);
  5247. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5248. const dim3 block_nums(block_num_y, 1, 1);
  5249. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5250. mul_mat_vec_q<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  5251. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  5252. }
  5253. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5254. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5255. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5256. int id;
  5257. CUDA_CHECK(cudaGetDevice(&id));
  5258. const int compute_capability = g_device_caps[id].cc;
  5259. int mmq_x, mmq_y, nwarps;
  5260. if (compute_capability >= CC_RDNA2) {
  5261. mmq_x = MMQ_X_Q4_0_RDNA2;
  5262. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5263. nwarps = NWARPS_Q4_0_RDNA2;
  5264. } else if (compute_capability >= CC_OFFSET_AMD) {
  5265. mmq_x = MMQ_X_Q4_0_RDNA1;
  5266. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5267. nwarps = NWARPS_Q4_0_RDNA1;
  5268. } else if (compute_capability >= CC_VOLTA) {
  5269. mmq_x = MMQ_X_Q4_0_AMPERE;
  5270. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5271. nwarps = NWARPS_Q4_0_AMPERE;
  5272. } else if (compute_capability >= MIN_CC_DP4A) {
  5273. mmq_x = MMQ_X_Q4_0_PASCAL;
  5274. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5275. nwarps = NWARPS_Q4_0_PASCAL;
  5276. } else {
  5277. GGML_ASSERT(false);
  5278. }
  5279. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5280. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5281. const dim3 block_nums(block_num_x, block_num_y, 1);
  5282. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5283. if (nrows_x % mmq_y == 0) {
  5284. const bool need_check = false;
  5285. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5286. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5287. } else {
  5288. const bool need_check = true;
  5289. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5290. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5291. }
  5292. }
  5293. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5294. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5295. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5296. int id;
  5297. CUDA_CHECK(cudaGetDevice(&id));
  5298. const int compute_capability = g_device_caps[id].cc;
  5299. int mmq_x, mmq_y, nwarps;
  5300. if (compute_capability >= CC_RDNA2) {
  5301. mmq_x = MMQ_X_Q4_1_RDNA2;
  5302. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5303. nwarps = NWARPS_Q4_1_RDNA2;
  5304. } else if (compute_capability >= CC_OFFSET_AMD) {
  5305. mmq_x = MMQ_X_Q4_1_RDNA1;
  5306. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5307. nwarps = NWARPS_Q4_1_RDNA1;
  5308. } else if (compute_capability >= CC_VOLTA) {
  5309. mmq_x = MMQ_X_Q4_1_AMPERE;
  5310. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5311. nwarps = NWARPS_Q4_1_AMPERE;
  5312. } else if (compute_capability >= MIN_CC_DP4A) {
  5313. mmq_x = MMQ_X_Q4_1_PASCAL;
  5314. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5315. nwarps = NWARPS_Q4_1_PASCAL;
  5316. } else {
  5317. GGML_ASSERT(false);
  5318. }
  5319. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5320. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5321. const dim3 block_nums(block_num_x, block_num_y, 1);
  5322. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5323. if (nrows_x % mmq_y == 0) {
  5324. const bool need_check = false;
  5325. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5326. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5327. } else {
  5328. const bool need_check = true;
  5329. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5330. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5331. }
  5332. }
  5333. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5334. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5335. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5336. int id;
  5337. CUDA_CHECK(cudaGetDevice(&id));
  5338. const int compute_capability = g_device_caps[id].cc;
  5339. int mmq_x, mmq_y, nwarps;
  5340. if (compute_capability >= CC_RDNA2) {
  5341. mmq_x = MMQ_X_Q5_0_RDNA2;
  5342. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5343. nwarps = NWARPS_Q5_0_RDNA2;
  5344. } else if (compute_capability >= CC_OFFSET_AMD) {
  5345. mmq_x = MMQ_X_Q5_0_RDNA1;
  5346. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5347. nwarps = NWARPS_Q5_0_RDNA1;
  5348. } else if (compute_capability >= CC_VOLTA) {
  5349. mmq_x = MMQ_X_Q5_0_AMPERE;
  5350. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5351. nwarps = NWARPS_Q5_0_AMPERE;
  5352. } else if (compute_capability >= MIN_CC_DP4A) {
  5353. mmq_x = MMQ_X_Q5_0_PASCAL;
  5354. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5355. nwarps = NWARPS_Q5_0_PASCAL;
  5356. } else {
  5357. GGML_ASSERT(false);
  5358. }
  5359. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5360. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5361. const dim3 block_nums(block_num_x, block_num_y, 1);
  5362. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5363. if (nrows_x % mmq_y == 0) {
  5364. const bool need_check = false;
  5365. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5366. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5367. } else {
  5368. const bool need_check = true;
  5369. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5370. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5371. }
  5372. }
  5373. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5374. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5375. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5376. int id;
  5377. CUDA_CHECK(cudaGetDevice(&id));
  5378. const int compute_capability = g_device_caps[id].cc;
  5379. int mmq_x, mmq_y, nwarps;
  5380. if (compute_capability >= CC_RDNA2) {
  5381. mmq_x = MMQ_X_Q5_1_RDNA2;
  5382. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5383. nwarps = NWARPS_Q5_1_RDNA2;
  5384. } else if (compute_capability >= CC_OFFSET_AMD) {
  5385. mmq_x = MMQ_X_Q5_1_RDNA1;
  5386. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5387. nwarps = NWARPS_Q5_1_RDNA1;
  5388. } else if (compute_capability >= CC_VOLTA) {
  5389. mmq_x = MMQ_X_Q5_1_AMPERE;
  5390. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5391. nwarps = NWARPS_Q5_1_AMPERE;
  5392. } else if (compute_capability >= MIN_CC_DP4A) {
  5393. mmq_x = MMQ_X_Q5_1_PASCAL;
  5394. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5395. nwarps = NWARPS_Q5_1_PASCAL;
  5396. } else {
  5397. GGML_ASSERT(false);
  5398. }
  5399. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5400. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5401. const dim3 block_nums(block_num_x, block_num_y, 1);
  5402. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5403. if (nrows_x % mmq_y == 0) {
  5404. const bool need_check = false;
  5405. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5406. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5407. } else {
  5408. const bool need_check = true;
  5409. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5410. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5411. }
  5412. }
  5413. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5414. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5415. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5416. int id;
  5417. CUDA_CHECK(cudaGetDevice(&id));
  5418. const int compute_capability = g_device_caps[id].cc;
  5419. int mmq_x, mmq_y, nwarps;
  5420. if (compute_capability >= CC_RDNA2) {
  5421. mmq_x = MMQ_X_Q8_0_RDNA2;
  5422. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5423. nwarps = NWARPS_Q8_0_RDNA2;
  5424. } else if (compute_capability >= CC_OFFSET_AMD) {
  5425. mmq_x = MMQ_X_Q8_0_RDNA1;
  5426. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5427. nwarps = NWARPS_Q8_0_RDNA1;
  5428. } else if (compute_capability >= CC_VOLTA) {
  5429. mmq_x = MMQ_X_Q8_0_AMPERE;
  5430. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5431. nwarps = NWARPS_Q8_0_AMPERE;
  5432. } else if (compute_capability >= MIN_CC_DP4A) {
  5433. mmq_x = MMQ_X_Q8_0_PASCAL;
  5434. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5435. nwarps = NWARPS_Q8_0_PASCAL;
  5436. } else {
  5437. GGML_ASSERT(false);
  5438. }
  5439. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5440. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5441. const dim3 block_nums(block_num_x, block_num_y, 1);
  5442. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5443. if (nrows_x % mmq_y == 0) {
  5444. const bool need_check = false;
  5445. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5446. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5447. } else {
  5448. const bool need_check = true;
  5449. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5450. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5451. }
  5452. }
  5453. static void ggml_mul_mat_q2_K_q8_1_cuda(
  5454. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5455. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5456. int id;
  5457. CUDA_CHECK(cudaGetDevice(&id));
  5458. const int compute_capability = g_device_caps[id].cc;
  5459. int mmq_x, mmq_y, nwarps;
  5460. if (compute_capability >= CC_RDNA2) {
  5461. mmq_x = MMQ_X_Q2_K_RDNA2;
  5462. mmq_y = MMQ_Y_Q2_K_RDNA2;
  5463. nwarps = NWARPS_Q2_K_RDNA2;
  5464. } else if (compute_capability >= CC_OFFSET_AMD) {
  5465. mmq_x = MMQ_X_Q2_K_RDNA1;
  5466. mmq_y = MMQ_Y_Q2_K_RDNA1;
  5467. nwarps = NWARPS_Q2_K_RDNA1;
  5468. } else if (compute_capability >= CC_VOLTA) {
  5469. mmq_x = MMQ_X_Q2_K_AMPERE;
  5470. mmq_y = MMQ_Y_Q2_K_AMPERE;
  5471. nwarps = NWARPS_Q2_K_AMPERE;
  5472. } else if (compute_capability >= MIN_CC_DP4A) {
  5473. mmq_x = MMQ_X_Q2_K_PASCAL;
  5474. mmq_y = MMQ_Y_Q2_K_PASCAL;
  5475. nwarps = NWARPS_Q2_K_PASCAL;
  5476. } else {
  5477. GGML_ASSERT(false);
  5478. }
  5479. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5480. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5481. const dim3 block_nums(block_num_x, block_num_y, 1);
  5482. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5483. if (nrows_x % mmq_y == 0) {
  5484. const bool need_check = false;
  5485. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5486. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5487. } else {
  5488. const bool need_check = true;
  5489. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5490. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5491. }
  5492. }
  5493. static void ggml_mul_mat_q3_K_q8_1_cuda(
  5494. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5495. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5496. #if QK_K == 256
  5497. int id;
  5498. CUDA_CHECK(cudaGetDevice(&id));
  5499. const int compute_capability = g_device_caps[id].cc;
  5500. int mmq_x, mmq_y, nwarps;
  5501. if (compute_capability >= CC_RDNA2) {
  5502. mmq_x = MMQ_X_Q3_K_RDNA2;
  5503. mmq_y = MMQ_Y_Q3_K_RDNA2;
  5504. nwarps = NWARPS_Q3_K_RDNA2;
  5505. } else if (compute_capability >= CC_OFFSET_AMD) {
  5506. mmq_x = MMQ_X_Q3_K_RDNA1;
  5507. mmq_y = MMQ_Y_Q3_K_RDNA1;
  5508. nwarps = NWARPS_Q3_K_RDNA1;
  5509. } else if (compute_capability >= CC_VOLTA) {
  5510. mmq_x = MMQ_X_Q3_K_AMPERE;
  5511. mmq_y = MMQ_Y_Q3_K_AMPERE;
  5512. nwarps = NWARPS_Q3_K_AMPERE;
  5513. } else if (compute_capability >= MIN_CC_DP4A) {
  5514. mmq_x = MMQ_X_Q3_K_PASCAL;
  5515. mmq_y = MMQ_Y_Q3_K_PASCAL;
  5516. nwarps = NWARPS_Q3_K_PASCAL;
  5517. } else {
  5518. GGML_ASSERT(false);
  5519. }
  5520. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5521. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5522. const dim3 block_nums(block_num_x, block_num_y, 1);
  5523. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5524. if (nrows_x % mmq_y == 0) {
  5525. const bool need_check = false;
  5526. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5527. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5528. } else {
  5529. const bool need_check = true;
  5530. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5531. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5532. }
  5533. #endif
  5534. }
  5535. static void ggml_mul_mat_q4_K_q8_1_cuda(
  5536. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5537. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5538. int id;
  5539. CUDA_CHECK(cudaGetDevice(&id));
  5540. const int compute_capability = g_device_caps[id].cc;
  5541. int mmq_x, mmq_y, nwarps;
  5542. if (compute_capability >= CC_RDNA2) {
  5543. mmq_x = MMQ_X_Q4_K_RDNA2;
  5544. mmq_y = MMQ_Y_Q4_K_RDNA2;
  5545. nwarps = NWARPS_Q4_K_RDNA2;
  5546. } else if (compute_capability >= CC_OFFSET_AMD) {
  5547. mmq_x = MMQ_X_Q4_K_RDNA1;
  5548. mmq_y = MMQ_Y_Q4_K_RDNA1;
  5549. nwarps = NWARPS_Q4_K_RDNA1;
  5550. } else if (compute_capability >= CC_VOLTA) {
  5551. mmq_x = MMQ_X_Q4_K_AMPERE;
  5552. mmq_y = MMQ_Y_Q4_K_AMPERE;
  5553. nwarps = NWARPS_Q4_K_AMPERE;
  5554. } else if (compute_capability >= MIN_CC_DP4A) {
  5555. mmq_x = MMQ_X_Q4_K_PASCAL;
  5556. mmq_y = MMQ_Y_Q4_K_PASCAL;
  5557. nwarps = NWARPS_Q4_K_PASCAL;
  5558. } else {
  5559. GGML_ASSERT(false);
  5560. }
  5561. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5562. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5563. const dim3 block_nums(block_num_x, block_num_y, 1);
  5564. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5565. if (nrows_x % mmq_y == 0) {
  5566. const bool need_check = false;
  5567. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5568. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5569. } else {
  5570. const bool need_check = true;
  5571. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5572. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5573. }
  5574. }
  5575. static void ggml_mul_mat_q5_K_q8_1_cuda(
  5576. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5577. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5578. int id;
  5579. CUDA_CHECK(cudaGetDevice(&id));
  5580. const int compute_capability = g_device_caps[id].cc;
  5581. int mmq_x, mmq_y, nwarps;
  5582. if (compute_capability >= CC_RDNA2) {
  5583. mmq_x = MMQ_X_Q5_K_RDNA2;
  5584. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5585. nwarps = NWARPS_Q5_K_RDNA2;
  5586. } else if (compute_capability >= CC_OFFSET_AMD) {
  5587. mmq_x = MMQ_X_Q5_K_RDNA1;
  5588. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5589. nwarps = NWARPS_Q5_K_RDNA1;
  5590. } else if (compute_capability >= CC_VOLTA) {
  5591. mmq_x = MMQ_X_Q5_K_AMPERE;
  5592. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5593. nwarps = NWARPS_Q5_K_AMPERE;
  5594. } else if (compute_capability >= MIN_CC_DP4A) {
  5595. mmq_x = MMQ_X_Q5_K_PASCAL;
  5596. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5597. nwarps = NWARPS_Q5_K_PASCAL;
  5598. } else {
  5599. GGML_ASSERT(false);
  5600. }
  5601. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5602. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5603. const dim3 block_nums(block_num_x, block_num_y, 1);
  5604. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5605. if (nrows_x % mmq_y == 0) {
  5606. const bool need_check = false;
  5607. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5608. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5609. } else {
  5610. const bool need_check = true;
  5611. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5612. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5613. }
  5614. }
  5615. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5616. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5617. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5618. int id;
  5619. CUDA_CHECK(cudaGetDevice(&id));
  5620. const int compute_capability = g_device_caps[id].cc;
  5621. int mmq_x, mmq_y, nwarps;
  5622. if (compute_capability >= CC_RDNA2) {
  5623. mmq_x = MMQ_X_Q6_K_RDNA2;
  5624. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5625. nwarps = NWARPS_Q6_K_RDNA2;
  5626. } else if (compute_capability >= CC_OFFSET_AMD) {
  5627. mmq_x = MMQ_X_Q6_K_RDNA1;
  5628. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5629. nwarps = NWARPS_Q6_K_RDNA1;
  5630. } else if (compute_capability >= CC_VOLTA) {
  5631. mmq_x = MMQ_X_Q6_K_AMPERE;
  5632. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5633. nwarps = NWARPS_Q6_K_AMPERE;
  5634. } else if (compute_capability >= MIN_CC_DP4A) {
  5635. mmq_x = MMQ_X_Q6_K_PASCAL;
  5636. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5637. nwarps = NWARPS_Q6_K_PASCAL;
  5638. } else {
  5639. GGML_ASSERT(false);
  5640. }
  5641. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5642. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5643. const dim3 block_nums(block_num_x, block_num_y, 1);
  5644. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5645. if (nrows_x % mmq_y == 0) {
  5646. const bool need_check = false;
  5647. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5648. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5649. } else {
  5650. const bool need_check = true;
  5651. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5652. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5653. }
  5654. }
  5655. static void ggml_mul_mat_p021_f16_f32_cuda(
  5656. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5657. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5658. const dim3 block_nums(1, nrows_x, nchannels_y);
  5659. const dim3 block_dims(WARP_SIZE, 1, 1);
  5660. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5661. }
  5662. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5663. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5664. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5665. const dim3 block_nums(1, nrows_x, nchannels_y);
  5666. const dim3 block_dims(WARP_SIZE, 1, 1);
  5667. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5668. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5669. }
  5670. static void ggml_cpy_f32_f32_cuda(
  5671. const char * cx, char * cdst, const int ne,
  5672. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5673. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5674. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5675. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5676. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5677. }
  5678. static void ggml_cpy_f32_f16_cuda(
  5679. const char * cx, char * cdst, const int ne,
  5680. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5681. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5682. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5683. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5684. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5685. }
  5686. static void ggml_cpy_f32_q8_0_cuda(
  5687. const char * cx, char * cdst, const int ne,
  5688. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5689. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5690. GGML_ASSERT(ne % QK8_0 == 0);
  5691. const int num_blocks = ne / QK8_0;
  5692. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5693. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5694. }
  5695. static void ggml_cpy_f32_q4_0_cuda(
  5696. const char * cx, char * cdst, const int ne,
  5697. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5698. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5699. GGML_ASSERT(ne % QK4_0 == 0);
  5700. const int num_blocks = ne / QK4_0;
  5701. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5702. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5703. }
  5704. static void ggml_cpy_f32_q4_1_cuda(
  5705. const char * cx, char * cdst, const int ne,
  5706. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5707. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5708. GGML_ASSERT(ne % QK4_1 == 0);
  5709. const int num_blocks = ne / QK4_1;
  5710. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5711. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5712. }
  5713. static void ggml_cpy_f16_f16_cuda(
  5714. const char * cx, char * cdst, const int ne,
  5715. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5716. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5717. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5718. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5719. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5720. }
  5721. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5722. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5723. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5724. }
  5725. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5726. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5727. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5728. }
  5729. template<typename T>
  5730. static void rope_cuda(
  5731. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5732. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5733. ) {
  5734. GGML_ASSERT(ncols % 2 == 0);
  5735. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5736. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5737. const dim3 block_nums(nrows, num_blocks_x, 1);
  5738. if (pos == nullptr) {
  5739. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5740. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5741. );
  5742. } else {
  5743. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5744. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5745. );
  5746. }
  5747. }
  5748. template<typename T>
  5749. static void rope_neox_cuda(
  5750. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5751. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5752. ) {
  5753. GGML_ASSERT(ncols % 2 == 0);
  5754. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5755. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5756. const dim3 block_nums(nrows, num_blocks_x, 1);
  5757. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5758. const float inv_ndims = -1.0f / n_dims;
  5759. if (pos == nullptr) {
  5760. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5761. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5762. theta_scale, inv_ndims
  5763. );
  5764. } else {
  5765. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5766. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5767. theta_scale, inv_ndims
  5768. );
  5769. }
  5770. }
  5771. static void rope_glm_f32_cuda(
  5772. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5773. float freq_base, int n_ctx, cudaStream_t stream
  5774. ) {
  5775. GGML_ASSERT(ncols % 4 == 0);
  5776. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5777. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5778. const dim3 block_nums(num_blocks_x, nrows, 1);
  5779. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5780. }
  5781. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5782. const int k_rows, const int n_heads_log2_floor, const float m0,
  5783. const float m1, cudaStream_t stream) {
  5784. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5785. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5786. const dim3 block_nums(num_blocks_x, nrows, 1);
  5787. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5788. }
  5789. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5790. const dim3 block_dims(WARP_SIZE, 1, 1);
  5791. const dim3 block_nums(1, nrows, 1);
  5792. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5793. }
  5794. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5795. // bitonic sort requires ncols to be power of 2
  5796. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5797. const dim3 block_dims(ncols, 1, 1);
  5798. const dim3 block_nums(1, nrows, 1);
  5799. if (order == GGML_SORT_ASC) {
  5800. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5801. } else if (order == GGML_SORT_DESC) {
  5802. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5803. } else {
  5804. GGML_ASSERT(false);
  5805. }
  5806. }
  5807. 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) {
  5808. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5809. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5810. const dim3 block_nums(nrows_x, block_num_x, 1);
  5811. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5812. }
  5813. static void soft_max_f16_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) {
  5814. int nth = WARP_SIZE;
  5815. while (nth < ncols_x/2 && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5816. const dim3 block_dims(nth, 1, 1);
  5817. const dim3 block_nums(nrows_x, 1, 1);
  5818. const size_t shmem = (GGML_PAD(ncols_x, 2*WARP_SIZE) + WARP_SIZE)*sizeof(half);
  5819. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  5820. if (shmem <= g_device_caps[g_main_device].smpb) {
  5821. switch (ncols_x) {
  5822. case 32:
  5823. soft_max_f16<true, 32, 32, true><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5824. break;
  5825. case 64:
  5826. soft_max_f16<true, 64, 32, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5827. break;
  5828. case 128:
  5829. soft_max_f16<true, 128, 64, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5830. break;
  5831. case 256:
  5832. soft_max_f16<true, 256, 128, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5833. break;
  5834. case 512:
  5835. soft_max_f16<true, 512, 256, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5836. break;
  5837. case 1024:
  5838. soft_max_f16<true, 1024, 512, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5839. break;
  5840. case 2048:
  5841. soft_max_f16<true, 2048, 1024, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5842. break;
  5843. case 4096:
  5844. soft_max_f16<true, 4096, 1024, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5845. break;
  5846. default:
  5847. soft_max_f16<true, 0, 0, true><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5848. break;
  5849. }
  5850. } else {
  5851. const size_t shmem_low = WARP_SIZE*sizeof(half);
  5852. soft_max_f16<false, 0, 0, true><<<block_nums, block_dims, shmem_low, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5853. }
  5854. }
  5855. 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) {
  5856. int nth = WARP_SIZE;
  5857. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5858. const dim3 block_dims(nth, 1, 1);
  5859. const dim3 block_nums(nrows_x, 1, 1);
  5860. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  5861. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  5862. if (shmem < g_device_caps[g_main_device].smpb) {
  5863. switch (ncols_x) {
  5864. case 32:
  5865. soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5866. break;
  5867. case 64:
  5868. soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5869. break;
  5870. case 128:
  5871. soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5872. break;
  5873. case 256:
  5874. soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5875. break;
  5876. case 512:
  5877. soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5878. break;
  5879. case 1024:
  5880. soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5881. break;
  5882. case 2048:
  5883. soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5884. break;
  5885. case 4096:
  5886. soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5887. break;
  5888. default:
  5889. soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5890. break;
  5891. }
  5892. } else {
  5893. const size_t shmem_low = WARP_SIZE*sizeof(float);
  5894. soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5895. }
  5896. }
  5897. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5898. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5899. int offset_delta,
  5900. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5901. const int parallel_elements = OW * KW * KH;
  5902. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5903. dim3 block_nums(num_blocks, OH, IC);
  5904. 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);
  5905. }
  5906. // buffer pool for cuda
  5907. #define MAX_CUDA_BUFFERS 256
  5908. struct scoped_spin_lock {
  5909. std::atomic_flag& lock;
  5910. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5911. while (lock.test_and_set(std::memory_order_acquire)) {
  5912. ; // spin
  5913. }
  5914. }
  5915. ~scoped_spin_lock() {
  5916. lock.clear(std::memory_order_release);
  5917. }
  5918. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5919. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5920. };
  5921. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5922. // #define DEBUG_CUDA_MALLOC
  5923. struct ggml_cuda_buffer {
  5924. void * ptr = nullptr;
  5925. size_t size = 0;
  5926. };
  5927. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5928. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  5929. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  5930. scoped_spin_lock lock(g_cuda_pool_lock);
  5931. #ifdef DEBUG_CUDA_MALLOC
  5932. int nnz = 0;
  5933. size_t max_size = 0;
  5934. #endif
  5935. size_t best_diff = 1ull << 36;
  5936. int ibest = -1;
  5937. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5938. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5939. if (b.ptr != nullptr) {
  5940. #ifdef DEBUG_CUDA_MALLOC
  5941. ++nnz;
  5942. if (b.size > max_size) max_size = b.size;
  5943. #endif
  5944. if (b.size >= size) {
  5945. size_t diff = b.size - size;
  5946. if (diff < best_diff) {
  5947. best_diff = diff;
  5948. ibest = i;
  5949. if (!best_diff) {
  5950. void * ptr = b.ptr;
  5951. *actual_size = b.size;
  5952. b.ptr = nullptr;
  5953. b.size = 0;
  5954. return ptr;
  5955. }
  5956. }
  5957. }
  5958. }
  5959. }
  5960. if (ibest >= 0) {
  5961. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  5962. void * ptr = b.ptr;
  5963. *actual_size = b.size;
  5964. b.ptr = nullptr;
  5965. b.size = 0;
  5966. return ptr;
  5967. }
  5968. void * ptr;
  5969. size_t look_ahead_size = (size_t) (1.05 * size);
  5970. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5971. ggml_cuda_set_device(device);
  5972. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5973. *actual_size = look_ahead_size;
  5974. g_cuda_pool_size[device] += look_ahead_size;
  5975. #ifdef DEBUG_CUDA_MALLOC
  5976. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  5977. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  5978. #endif
  5979. return ptr;
  5980. }
  5981. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  5982. scoped_spin_lock lock(g_cuda_pool_lock);
  5983. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5984. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5985. if (b.ptr == nullptr) {
  5986. b.ptr = ptr;
  5987. b.size = size;
  5988. return;
  5989. }
  5990. }
  5991. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5992. ggml_cuda_set_device(device);
  5993. CUDA_CHECK(cudaFree(ptr));
  5994. g_cuda_pool_size[device] -= size;
  5995. }
  5996. #if !defined(GGML_USE_HIPBLAS)
  5997. // pool with virtual memory
  5998. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  5999. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  6000. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  6001. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  6002. scoped_spin_lock lock(g_cuda_pool_lock);
  6003. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  6004. const size_t alignment = 128;
  6005. size = alignment * ((size + alignment - 1) / alignment);
  6006. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  6007. if (size > avail) {
  6008. // round up to the next multiple of the granularity
  6009. size_t reserve_size = size - avail;
  6010. const size_t granularity = g_device_caps[device].vmm_granularity;
  6011. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  6012. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  6013. // allocate more physical memory
  6014. CUmemAllocationProp prop = {};
  6015. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6016. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6017. prop.location.id = device;
  6018. CUmemGenericAllocationHandle handle;
  6019. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  6020. // reserve virtual address space (if not already reserved)
  6021. if (g_cuda_pool_addr[device] == 0) {
  6022. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  6023. }
  6024. // map at the end of the pool
  6025. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  6026. // the memory allocation handle is no longer needed after mapping
  6027. CU_CHECK(cuMemRelease(handle));
  6028. // set access
  6029. CUmemAccessDesc access = {};
  6030. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6031. access.location.id = device;
  6032. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  6033. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  6034. // add to the pool
  6035. g_cuda_pool_size[device] += reserve_size;
  6036. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  6037. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  6038. // (unsigned long long) (reserve_size/1024/1024));
  6039. }
  6040. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  6041. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  6042. *actual_size = size;
  6043. g_cuda_pool_used[device] += size;
  6044. #ifdef DEBUG_CUDA_MALLOC
  6045. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  6046. #endif
  6047. return ptr;
  6048. }
  6049. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  6050. scoped_spin_lock lock(g_cuda_pool_lock);
  6051. #ifdef DEBUG_CUDA_MALLOC
  6052. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  6053. #endif
  6054. g_cuda_pool_used[device] -= size;
  6055. // all deallocations must be in reverse order of the allocations
  6056. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  6057. }
  6058. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  6059. if (g_device_caps[device].vmm) {
  6060. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  6061. } else {
  6062. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  6063. }
  6064. }
  6065. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  6066. if (g_device_caps[device].vmm) {
  6067. ggml_cuda_pool_free_vmm(device, ptr, size);
  6068. } else {
  6069. ggml_cuda_pool_free_leg(device, ptr, size);
  6070. }
  6071. }
  6072. #else
  6073. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  6074. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  6075. #endif // !defined(GGML_USE_HIPBLAS)
  6076. template<typename T>
  6077. struct cuda_pool_alloc {
  6078. int device = -1;
  6079. T * ptr = nullptr;
  6080. size_t actual_size = 0;
  6081. // size is in number of elements
  6082. T * alloc(size_t size) {
  6083. GGML_ASSERT(ptr == nullptr);
  6084. CUDA_CHECK(cudaGetDevice(&device));
  6085. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  6086. return ptr;
  6087. }
  6088. cuda_pool_alloc(size_t size) {
  6089. alloc(size);
  6090. }
  6091. ~cuda_pool_alloc() {
  6092. if (ptr != nullptr) {
  6093. ggml_cuda_pool_free(device, ptr, actual_size);
  6094. }
  6095. }
  6096. T * get() {
  6097. return ptr;
  6098. }
  6099. cuda_pool_alloc() = default;
  6100. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  6101. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  6102. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  6103. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  6104. };
  6105. static bool g_cublas_loaded = false;
  6106. bool ggml_cublas_loaded(void) {
  6107. return g_cublas_loaded;
  6108. }
  6109. void ggml_init_cublas() {
  6110. static bool initialized = false;
  6111. if (!initialized) {
  6112. #ifdef __HIP_PLATFORM_AMD__
  6113. // Workaround for a rocBLAS bug when using multiple graphics cards:
  6114. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  6115. rocblas_initialize();
  6116. CUDA_CHECK(cudaDeviceSynchronize());
  6117. #endif
  6118. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  6119. initialized = true;
  6120. g_cublas_loaded = false;
  6121. return;
  6122. }
  6123. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  6124. int64_t total_vram = 0;
  6125. #if defined(GGML_CUDA_FORCE_MMQ)
  6126. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  6127. #else
  6128. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  6129. #endif
  6130. #if defined(CUDA_USE_TENSOR_CORES)
  6131. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  6132. #else
  6133. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  6134. #endif
  6135. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  6136. for (int id = 0; id < g_device_count; ++id) {
  6137. int device_vmm = 0;
  6138. #if !defined(GGML_USE_HIPBLAS)
  6139. CUdevice device;
  6140. CU_CHECK(cuDeviceGet(&device, id));
  6141. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  6142. if (device_vmm) {
  6143. CUmemAllocationProp alloc_prop = {};
  6144. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6145. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6146. alloc_prop.location.id = id;
  6147. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  6148. }
  6149. #endif // !defined(GGML_USE_HIPBLAS)
  6150. g_device_caps[id].vmm = !!device_vmm;
  6151. cudaDeviceProp prop;
  6152. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  6153. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  6154. g_tensor_split[id] = total_vram;
  6155. total_vram += prop.totalGlobalMem;
  6156. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6157. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  6158. #else
  6159. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  6160. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6161. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  6162. }
  6163. for (int id = 0; id < g_device_count; ++id) {
  6164. g_tensor_split[id] /= total_vram;
  6165. }
  6166. for (int id = 0; id < g_device_count; ++id) {
  6167. ggml_cuda_set_device(id);
  6168. // create cuda streams
  6169. for (int is = 0; is < MAX_STREAMS; ++is) {
  6170. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  6171. }
  6172. // create cublas handle
  6173. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  6174. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  6175. }
  6176. // configure logging to stdout
  6177. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  6178. initialized = true;
  6179. g_cublas_loaded = true;
  6180. }
  6181. }
  6182. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  6183. if (tensor_split == nullptr) {
  6184. return;
  6185. }
  6186. bool all_zero = true;
  6187. for (int i = 0; i < g_device_count; ++i) {
  6188. if (tensor_split[i] != 0.0f) {
  6189. all_zero = false;
  6190. break;
  6191. }
  6192. }
  6193. if (all_zero) {
  6194. return;
  6195. }
  6196. float split_sum = 0.0f;
  6197. for (int i = 0; i < g_device_count; ++i) {
  6198. g_tensor_split[i] = split_sum;
  6199. split_sum += tensor_split[i];
  6200. }
  6201. for (int i = 0; i < g_device_count; ++i) {
  6202. g_tensor_split[i] /= split_sum;
  6203. }
  6204. }
  6205. void * ggml_cuda_host_malloc(size_t size) {
  6206. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6207. return nullptr;
  6208. }
  6209. void * ptr = nullptr;
  6210. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6211. if (err != cudaSuccess) {
  6212. // clear the error
  6213. cudaGetLastError();
  6214. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6215. size/1024.0/1024.0, cudaGetErrorString(err));
  6216. return nullptr;
  6217. }
  6218. return ptr;
  6219. }
  6220. void ggml_cuda_host_free(void * ptr) {
  6221. CUDA_CHECK(cudaFreeHost(ptr));
  6222. }
  6223. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6224. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6225. cudaMemcpyKind kind;
  6226. char * src_ptr;
  6227. if (src->backend == GGML_BACKEND_CPU) {
  6228. kind = cudaMemcpyHostToDevice;
  6229. src_ptr = (char *) src->data;
  6230. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  6231. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6232. kind = cudaMemcpyDeviceToDevice;
  6233. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6234. int id;
  6235. CUDA_CHECK(cudaGetDevice(&id));
  6236. src_ptr = (char *) extra->data_device[id];
  6237. } else {
  6238. GGML_ASSERT(false);
  6239. }
  6240. char * dst_ptr = (char *) dst;
  6241. const int64_t ne0 = src->ne[0];
  6242. const int64_t nb0 = src->nb[0];
  6243. const int64_t nb1 = src->nb[1];
  6244. const int64_t nb2 = src->nb[2];
  6245. const int64_t nb3 = src->nb[3];
  6246. const enum ggml_type type = src->type;
  6247. const int64_t ts = ggml_type_size(type);
  6248. const int64_t bs = ggml_blck_size(type);
  6249. int64_t i1_diff = i1_high - i1_low;
  6250. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6251. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6252. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6253. } else if (nb0 == ts) {
  6254. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6255. } else {
  6256. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6257. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6258. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6259. // pretend the row is a matrix with cols=1
  6260. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6261. if (r != cudaSuccess) return r;
  6262. }
  6263. return cudaSuccess;
  6264. }
  6265. }
  6266. static void ggml_cuda_op_get_rows(
  6267. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6268. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6269. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6270. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6271. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6272. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6273. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6274. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6275. switch (src0->type) {
  6276. case GGML_TYPE_F16:
  6277. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6278. break;
  6279. case GGML_TYPE_F32:
  6280. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6281. break;
  6282. case GGML_TYPE_Q4_0:
  6283. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6284. break;
  6285. case GGML_TYPE_Q4_1:
  6286. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6287. break;
  6288. case GGML_TYPE_Q5_0:
  6289. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6290. break;
  6291. case GGML_TYPE_Q5_1:
  6292. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6293. break;
  6294. case GGML_TYPE_Q8_0:
  6295. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6296. break;
  6297. default:
  6298. // TODO: k-quants
  6299. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6300. GGML_ASSERT(false);
  6301. break;
  6302. }
  6303. }
  6304. template<class op>
  6305. static void ggml_cuda_op_bin_bcast(
  6306. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6307. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6308. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6309. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6310. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6311. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6312. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6313. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6314. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6315. } else {
  6316. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6317. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6318. GGML_ASSERT(false);
  6319. }
  6320. }
  6321. static void ggml_cuda_op_repeat(
  6322. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6323. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6324. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6325. (void) src1;
  6326. (void) src1_d;
  6327. }
  6328. static void ggml_cuda_op_add(
  6329. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6330. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6331. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6332. }
  6333. static void ggml_cuda_op_acc(
  6334. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6335. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6336. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6337. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6338. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6339. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6340. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6341. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6342. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6343. int offset = dst->op_params[3] / 4; // offset in bytes
  6344. 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);
  6345. (void) dst;
  6346. }
  6347. static void ggml_cuda_op_mul(
  6348. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6349. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6350. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6351. }
  6352. static void ggml_cuda_op_div(
  6353. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6354. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6355. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6356. }
  6357. static void ggml_cuda_op_gelu(
  6358. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6359. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6360. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6361. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6362. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6363. (void) src1;
  6364. (void) dst;
  6365. (void) src1_dd;
  6366. }
  6367. static void ggml_cuda_op_silu(
  6368. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6369. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6370. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6371. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6372. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6373. (void) src1;
  6374. (void) dst;
  6375. (void) src1_dd;
  6376. }
  6377. static void ggml_cuda_op_gelu_quick(
  6378. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6379. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6380. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6381. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6382. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6383. (void) src1;
  6384. (void) dst;
  6385. (void) src1_dd;
  6386. }
  6387. static void ggml_cuda_op_tanh(
  6388. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6389. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6390. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6391. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6392. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6393. (void) src1;
  6394. (void) dst;
  6395. (void) src1_dd;
  6396. }
  6397. static void ggml_cuda_op_relu(
  6398. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6399. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6400. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6401. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6402. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6403. (void) src1;
  6404. (void) dst;
  6405. (void) src1_dd;
  6406. }
  6407. static void ggml_cuda_op_leaky_relu(
  6408. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6409. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6410. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6411. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6412. float negative_slope;
  6413. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6414. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6415. (void) src1;
  6416. (void) dst;
  6417. (void) src1_dd;
  6418. }
  6419. static void ggml_cuda_op_sqr(
  6420. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6421. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6422. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6423. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6424. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6425. (void) src1;
  6426. (void) dst;
  6427. (void) src1_dd;
  6428. }
  6429. static void ggml_cuda_op_norm(
  6430. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6431. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6432. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6433. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6434. const int64_t ne00 = src0->ne[0];
  6435. const int64_t nrows = ggml_nrows(src0);
  6436. float eps;
  6437. memcpy(&eps, dst->op_params, sizeof(float));
  6438. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6439. (void) src1;
  6440. (void) dst;
  6441. (void) src1_dd;
  6442. }
  6443. static void ggml_cuda_op_group_norm(
  6444. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6445. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6446. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6447. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6448. int num_groups = dst->op_params[0];
  6449. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6450. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  6451. (void) src1;
  6452. (void) dst;
  6453. (void) src1_dd;
  6454. }
  6455. static void ggml_cuda_op_concat(
  6456. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6457. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6458. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6459. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6460. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6461. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6462. 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);
  6463. }
  6464. (void) src1;
  6465. (void) dst;
  6466. }
  6467. static void ggml_cuda_op_upscale(
  6468. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6469. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6470. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6471. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6472. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6473. const int scale_factor = dst->op_params[0];
  6474. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  6475. (void) src1;
  6476. (void) dst;
  6477. (void) src1_dd;
  6478. }
  6479. static void ggml_cuda_op_pad(
  6480. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6481. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6482. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6483. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6484. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6485. pad_f32_cuda(src0_dd, dst_dd,
  6486. src0->ne[0], src0->ne[1], src0->ne[2],
  6487. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  6488. (void) src1;
  6489. (void) dst;
  6490. (void) src1_dd;
  6491. }
  6492. static void ggml_cuda_op_rms_norm(
  6493. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6494. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6495. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6496. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6497. const int64_t ne00 = src0->ne[0];
  6498. const int64_t nrows = ggml_nrows(src0);
  6499. float eps;
  6500. memcpy(&eps, dst->op_params, sizeof(float));
  6501. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6502. (void) src1;
  6503. (void) dst;
  6504. (void) src1_dd;
  6505. }
  6506. static void ggml_cuda_op_mul_mat_q(
  6507. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6508. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6509. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6510. const int64_t ne00 = src0->ne[0];
  6511. const int64_t ne10 = src1->ne[0];
  6512. GGML_ASSERT(ne10 % QK8_1 == 0);
  6513. const int64_t ne0 = dst->ne[0];
  6514. const int64_t row_diff = row_high - row_low;
  6515. int id;
  6516. CUDA_CHECK(cudaGetDevice(&id));
  6517. // the main device has a larger memory buffer to hold the results from all GPUs
  6518. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  6519. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6520. switch (src0->type) {
  6521. case GGML_TYPE_Q4_0:
  6522. 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);
  6523. break;
  6524. case GGML_TYPE_Q4_1:
  6525. 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);
  6526. break;
  6527. case GGML_TYPE_Q5_0:
  6528. 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);
  6529. break;
  6530. case GGML_TYPE_Q5_1:
  6531. 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);
  6532. break;
  6533. case GGML_TYPE_Q8_0:
  6534. 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);
  6535. break;
  6536. case GGML_TYPE_Q2_K:
  6537. 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);
  6538. break;
  6539. case GGML_TYPE_Q3_K:
  6540. 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);
  6541. break;
  6542. case GGML_TYPE_Q4_K:
  6543. 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);
  6544. break;
  6545. case GGML_TYPE_Q5_K:
  6546. 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);
  6547. break;
  6548. case GGML_TYPE_Q6_K:
  6549. 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);
  6550. break;
  6551. default:
  6552. GGML_ASSERT(false);
  6553. break;
  6554. }
  6555. (void) src1;
  6556. (void) dst;
  6557. (void) src1_ddf_i;
  6558. }
  6559. static int64_t get_row_rounding(ggml_type type) {
  6560. int64_t min_compute_capability = INT_MAX;
  6561. int64_t max_compute_capability = INT_MIN;
  6562. for (int id = 0; id < g_device_count; ++id) {
  6563. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6564. if (min_compute_capability > g_device_caps[id].cc) {
  6565. min_compute_capability = g_device_caps[id].cc;
  6566. }
  6567. if (max_compute_capability < g_device_caps[id].cc) {
  6568. max_compute_capability = g_device_caps[id].cc;
  6569. }
  6570. }
  6571. }
  6572. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6573. switch(type) {
  6574. case GGML_TYPE_Q4_0:
  6575. case GGML_TYPE_Q4_1:
  6576. case GGML_TYPE_Q5_0:
  6577. case GGML_TYPE_Q5_1:
  6578. case GGML_TYPE_Q8_0:
  6579. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  6580. case GGML_TYPE_F16:
  6581. case GGML_TYPE_F32:
  6582. return 1;
  6583. case GGML_TYPE_Q2_K:
  6584. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  6585. case GGML_TYPE_Q3_K:
  6586. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  6587. case GGML_TYPE_Q4_K:
  6588. case GGML_TYPE_Q5_K:
  6589. case GGML_TYPE_Q6_K:
  6590. case GGML_TYPE_IQ2_XXS:
  6591. case GGML_TYPE_IQ2_XS:
  6592. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  6593. default:
  6594. GGML_ASSERT(false);
  6595. }
  6596. #else
  6597. switch(type) {
  6598. case GGML_TYPE_Q4_0:
  6599. case GGML_TYPE_Q4_1:
  6600. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  6601. case GGML_TYPE_Q5_0:
  6602. case GGML_TYPE_Q5_1:
  6603. case GGML_TYPE_Q8_0:
  6604. return 64;
  6605. case GGML_TYPE_F16:
  6606. case GGML_TYPE_F32:
  6607. return 1;
  6608. case GGML_TYPE_Q2_K:
  6609. case GGML_TYPE_Q3_K:
  6610. case GGML_TYPE_Q4_K:
  6611. case GGML_TYPE_Q5_K:
  6612. case GGML_TYPE_IQ2_XXS:
  6613. case GGML_TYPE_IQ2_XS:
  6614. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  6615. case GGML_TYPE_Q6_K:
  6616. return 64;
  6617. default:
  6618. GGML_ASSERT(false);
  6619. }
  6620. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6621. }
  6622. static void ggml_cuda_op_mul_mat_vec_q(
  6623. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6624. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6625. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6626. GGML_ASSERT(ggml_nrows(src1) == 1);
  6627. const int64_t ne00 = src0->ne[0];
  6628. const int64_t row_diff = row_high - row_low;
  6629. switch (src0->type) {
  6630. case GGML_TYPE_Q4_0:
  6631. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6632. break;
  6633. case GGML_TYPE_Q4_1:
  6634. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6635. break;
  6636. case GGML_TYPE_Q5_0:
  6637. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6638. break;
  6639. case GGML_TYPE_Q5_1:
  6640. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6641. break;
  6642. case GGML_TYPE_Q8_0:
  6643. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6644. break;
  6645. case GGML_TYPE_Q2_K:
  6646. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6647. break;
  6648. case GGML_TYPE_Q3_K:
  6649. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6650. break;
  6651. case GGML_TYPE_Q4_K:
  6652. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6653. break;
  6654. case GGML_TYPE_Q5_K:
  6655. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6656. break;
  6657. case GGML_TYPE_Q6_K:
  6658. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6659. break;
  6660. case GGML_TYPE_IQ2_XXS:
  6661. mul_mat_vec_iq2_xxs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6662. break;
  6663. case GGML_TYPE_IQ2_XS:
  6664. mul_mat_vec_iq2_xs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6665. break;
  6666. default:
  6667. GGML_ASSERT(false);
  6668. break;
  6669. }
  6670. (void) src1;
  6671. (void) dst;
  6672. (void) src1_ddf_i;
  6673. (void) src1_ncols;
  6674. (void) src1_padded_row_size;
  6675. }
  6676. static void ggml_cuda_op_dequantize_mul_mat_vec(
  6677. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6678. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6679. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6680. const int64_t ne00 = src0->ne[0];
  6681. const int64_t row_diff = row_high - row_low;
  6682. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6683. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  6684. #ifdef GGML_CUDA_F16
  6685. cuda_pool_alloc<half> src1_dfloat_a;
  6686. half * src1_dfloat = nullptr; // dfloat == half
  6687. bool src1_convert_f16 =
  6688. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  6689. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  6690. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  6691. if (src1_convert_f16) {
  6692. src1_dfloat = src1_dfloat_a.alloc(ne00);
  6693. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  6694. ne00, 1, sizeof(float), 0, 0,
  6695. ne00, 1, sizeof(half), 0, 0, stream);
  6696. }
  6697. #else
  6698. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  6699. #endif // GGML_CUDA_F16
  6700. switch (src0->type) {
  6701. case GGML_TYPE_Q4_0:
  6702. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6703. break;
  6704. case GGML_TYPE_Q4_1:
  6705. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6706. break;
  6707. case GGML_TYPE_Q5_0:
  6708. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6709. break;
  6710. case GGML_TYPE_Q5_1:
  6711. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6712. break;
  6713. case GGML_TYPE_Q8_0:
  6714. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6715. break;
  6716. case GGML_TYPE_Q2_K:
  6717. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6718. break;
  6719. case GGML_TYPE_Q3_K:
  6720. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6721. break;
  6722. case GGML_TYPE_Q4_K:
  6723. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6724. break;
  6725. case GGML_TYPE_Q5_K:
  6726. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6727. break;
  6728. case GGML_TYPE_Q6_K:
  6729. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6730. break;
  6731. case GGML_TYPE_F16:
  6732. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6733. break;
  6734. default:
  6735. GGML_ASSERT(false);
  6736. break;
  6737. }
  6738. (void) src1;
  6739. (void) dst;
  6740. (void) src1_ddq_i;
  6741. (void) src1_ncols;
  6742. (void) src1_padded_row_size;
  6743. }
  6744. static void ggml_cuda_op_mul_mat_cublas(
  6745. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6746. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6747. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6748. GGML_ASSERT(src0_dd_i != nullptr);
  6749. GGML_ASSERT(src1_ddf_i != nullptr);
  6750. GGML_ASSERT(dst_dd_i != nullptr);
  6751. const int64_t ne00 = src0->ne[0];
  6752. const int64_t ne10 = src1->ne[0];
  6753. const int64_t ne0 = dst->ne[0];
  6754. const int64_t row_diff = row_high - row_low;
  6755. int id;
  6756. CUDA_CHECK(cudaGetDevice(&id));
  6757. // the main device has a larger memory buffer to hold the results from all GPUs
  6758. // ldc == nrows of the matrix that cuBLAS writes into
  6759. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6760. const int compute_capability = g_device_caps[id].cc;
  6761. 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) {
  6762. //printf("this branch\n");
  6763. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  6764. cuda_pool_alloc<half> src0_as_f16;
  6765. if (src0->type != GGML_TYPE_F16) {
  6766. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  6767. GGML_ASSERT(to_fp16_cuda != nullptr);
  6768. size_t ne = row_diff*ne00;
  6769. src0_as_f16.alloc(ne);
  6770. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  6771. }
  6772. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  6773. cuda_pool_alloc<half> src1_as_f16;
  6774. if (src1->type != GGML_TYPE_F16) {
  6775. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6776. GGML_ASSERT(to_fp16_cuda != nullptr);
  6777. size_t ne = src1_ncols*ne10;
  6778. src1_as_f16.alloc(ne);
  6779. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  6780. }
  6781. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  6782. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  6783. const half alpha_f16 = 1.0f;
  6784. const half beta_f16 = 0.0f;
  6785. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6786. CUBLAS_CHECK(
  6787. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6788. row_diff, src1_ncols, ne10,
  6789. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6790. src1_ptr, CUDA_R_16F, ne10,
  6791. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  6792. CUBLAS_COMPUTE_16F,
  6793. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6794. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6795. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  6796. } else {
  6797. cuda_pool_alloc<float> src0_ddq_as_f32;
  6798. cuda_pool_alloc<float> src1_ddq_as_f32;
  6799. if (src0->type != GGML_TYPE_F32) {
  6800. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6801. GGML_ASSERT(to_fp32_cuda != nullptr);
  6802. src0_ddq_as_f32.alloc(row_diff*ne00);
  6803. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  6804. }
  6805. if (src1->type != GGML_TYPE_F32) {
  6806. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  6807. GGML_ASSERT(to_fp32_cuda != nullptr);
  6808. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  6809. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  6810. }
  6811. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  6812. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  6813. const float alpha = 1.0f;
  6814. const float beta = 0.0f;
  6815. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6816. CUBLAS_CHECK(
  6817. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6818. row_diff, src1_ncols, ne10,
  6819. &alpha, src0_ddf_i, ne00,
  6820. src1_ddf1_i, ne10,
  6821. &beta, dst_dd_i, ldc));
  6822. }
  6823. (void) dst;
  6824. (void) src1_ddq_i;
  6825. (void) src1_padded_row_size;
  6826. }
  6827. static void ggml_cuda_op_rope(
  6828. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6829. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6830. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6831. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6832. GGML_ASSERT(src0->type == dst->type);
  6833. const int64_t ne00 = src0->ne[0];
  6834. const int64_t ne01 = src0->ne[1];
  6835. const int64_t ne2 = dst->ne[2];
  6836. const int64_t nrows = ggml_nrows(src0);
  6837. //const int n_past = ((int32_t *) dst->op_params)[0];
  6838. const int n_dims = ((int32_t *) dst->op_params)[1];
  6839. const int mode = ((int32_t *) dst->op_params)[2];
  6840. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6841. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6842. // RoPE alteration for extended context
  6843. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6844. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6845. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6846. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6847. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6848. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6849. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6850. const int32_t * pos = nullptr;
  6851. if ((mode & 1) == 0) {
  6852. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6853. GGML_ASSERT(src1->ne[0] == ne2);
  6854. pos = (const int32_t *) src1_dd;
  6855. }
  6856. const bool is_neox = mode & 2;
  6857. const bool is_glm = mode & 4;
  6858. rope_corr_dims corr_dims;
  6859. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6860. // compute
  6861. if (is_glm) {
  6862. GGML_ASSERT(false);
  6863. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6864. } else if (is_neox) {
  6865. if (src0->type == GGML_TYPE_F32) {
  6866. rope_neox_cuda(
  6867. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6868. attn_factor, corr_dims, main_stream
  6869. );
  6870. } else if (src0->type == GGML_TYPE_F16) {
  6871. rope_neox_cuda(
  6872. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6873. attn_factor, corr_dims, main_stream
  6874. );
  6875. } else {
  6876. GGML_ASSERT(false);
  6877. }
  6878. } else {
  6879. if (src0->type == GGML_TYPE_F32) {
  6880. rope_cuda(
  6881. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6882. attn_factor, corr_dims, main_stream
  6883. );
  6884. } else if (src0->type == GGML_TYPE_F16) {
  6885. rope_cuda(
  6886. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6887. attn_factor, corr_dims, main_stream
  6888. );
  6889. } else {
  6890. GGML_ASSERT(false);
  6891. }
  6892. }
  6893. (void) src1;
  6894. (void) dst;
  6895. (void) src1_dd;
  6896. }
  6897. static void ggml_cuda_op_alibi(
  6898. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6899. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6900. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6901. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6902. const int64_t ne00 = src0->ne[0];
  6903. const int64_t ne01 = src0->ne[1];
  6904. const int64_t ne02 = src0->ne[2];
  6905. const int64_t nrows = ggml_nrows(src0);
  6906. //const int n_past = ((int32_t *) dst->op_params)[0];
  6907. const int n_head = ((int32_t *) dst->op_params)[1];
  6908. float max_bias;
  6909. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6910. //GGML_ASSERT(ne01 + n_past == ne00);
  6911. GGML_ASSERT(n_head == ne02);
  6912. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6913. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6914. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6915. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6916. (void) src1;
  6917. (void) src1_dd;
  6918. }
  6919. static void ggml_cuda_op_im2col(
  6920. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6921. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6922. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6923. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6924. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6925. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6926. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6927. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6928. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6929. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6930. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6931. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6932. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6933. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6934. const int64_t IW = src1->ne[0];
  6935. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6936. const int64_t KW = src0->ne[0];
  6937. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6938. const int64_t OW = dst->ne[1];
  6939. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6940. 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);
  6941. (void) src0;
  6942. (void) src0_dd;
  6943. }
  6944. static void ggml_cuda_op_sum_rows(
  6945. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6946. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6947. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6948. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6949. const int64_t ncols = src0->ne[0];
  6950. const int64_t nrows = ggml_nrows(src0);
  6951. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6952. (void) src1;
  6953. (void) dst;
  6954. (void) src1_dd;
  6955. }
  6956. static void ggml_cuda_op_argsort(
  6957. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6958. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6959. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6960. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6961. const int64_t ncols = src0->ne[0];
  6962. const int64_t nrows = ggml_nrows(src0);
  6963. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6964. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6965. (void) src1;
  6966. (void) dst;
  6967. (void) src1_dd;
  6968. }
  6969. static void ggml_cuda_op_diag_mask_inf(
  6970. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6971. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6972. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6973. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6974. const int64_t ne00 = src0->ne[0];
  6975. const int64_t ne01 = src0->ne[1];
  6976. const int nrows0 = ggml_nrows(src0);
  6977. const int n_past = ((int32_t *) dst->op_params)[0];
  6978. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6979. (void) src1;
  6980. (void) dst;
  6981. (void) src1_dd;
  6982. }
  6983. static void ggml_cuda_op_soft_max(
  6984. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6985. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6986. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6987. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6988. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6989. const int64_t ne00 = src0->ne[0];
  6990. const int64_t nrows_x = ggml_nrows(src0);
  6991. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6992. float scale = 1.0f;
  6993. memcpy(&scale, dst->op_params, sizeof(float));
  6994. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6995. const bool use_f16_soft_max = false;
  6996. #else
  6997. #ifdef GGML_CUDA_F16
  6998. const bool use_f16_soft_max = true;
  6999. #else
  7000. const bool use_f16_soft_max = false;
  7001. #endif // GGML_CUDA_F16
  7002. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7003. if (use_f16_soft_max) {
  7004. soft_max_f16_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  7005. } else {
  7006. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  7007. }
  7008. (void) dst;
  7009. }
  7010. static void ggml_cuda_op_scale(
  7011. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7012. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7013. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7014. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7015. float scale;
  7016. memcpy(&scale, dst->op_params, sizeof(float));
  7017. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  7018. CUDA_CHECK(cudaGetLastError());
  7019. (void) src1;
  7020. (void) dst;
  7021. (void) src1_dd;
  7022. }
  7023. static void ggml_cuda_op_clamp(
  7024. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7025. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7026. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7027. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7028. float min;
  7029. float max;
  7030. memcpy(&min, dst->op_params, sizeof(float));
  7031. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7032. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  7033. CUDA_CHECK(cudaGetLastError());
  7034. (void) src1;
  7035. (void) dst;
  7036. (void) src1_dd;
  7037. }
  7038. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  7039. const int64_t nrows0 = ggml_nrows(src0);
  7040. const bool use_src1 = src1 != nullptr;
  7041. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  7042. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  7043. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  7044. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7045. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  7046. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7047. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7048. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  7049. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  7050. // dd = data device
  7051. float * src0_ddf = nullptr;
  7052. float * src1_ddf = nullptr;
  7053. float * dst_ddf = nullptr;
  7054. cuda_pool_alloc<float> src0_f;
  7055. cuda_pool_alloc<float> src1_f;
  7056. cuda_pool_alloc<float> dst_f;
  7057. ggml_cuda_set_device(g_main_device);
  7058. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7059. if (src0_on_device) {
  7060. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  7061. } else {
  7062. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  7063. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  7064. }
  7065. if (use_src1) {
  7066. if (src1_on_device) {
  7067. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7068. } else {
  7069. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  7070. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  7071. }
  7072. }
  7073. if (dst_on_device) {
  7074. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7075. } else {
  7076. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7077. }
  7078. // do the computation
  7079. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7080. CUDA_CHECK(cudaGetLastError());
  7081. // copy dst to host if necessary
  7082. if (!dst_on_device) {
  7083. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7084. }
  7085. if (dst->backend == GGML_BACKEND_CPU) {
  7086. CUDA_CHECK(cudaDeviceSynchronize());
  7087. }
  7088. }
  7089. static void ggml_cuda_set_peer_access(const int n_tokens) {
  7090. static bool peer_access_enabled = false;
  7091. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  7092. if (peer_access_enabled == enable_peer_access) {
  7093. return;
  7094. }
  7095. #ifdef NDEBUG
  7096. for (int id = 0; id < g_device_count; ++id) {
  7097. ggml_cuda_set_device(id);
  7098. CUDA_CHECK(cudaDeviceSynchronize());
  7099. }
  7100. for (int id = 0; id < g_device_count; ++id) {
  7101. ggml_cuda_set_device(id);
  7102. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  7103. if (id == id_other) {
  7104. continue;
  7105. }
  7106. if (id != g_main_device && id_other != g_main_device) {
  7107. continue;
  7108. }
  7109. int can_access_peer;
  7110. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  7111. if (can_access_peer) {
  7112. if (enable_peer_access) {
  7113. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  7114. } else {
  7115. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  7116. }
  7117. }
  7118. }
  7119. }
  7120. #endif // NDEBUG
  7121. peer_access_enabled = enable_peer_access;
  7122. }
  7123. static void ggml_cuda_op_mul_mat(
  7124. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  7125. const bool convert_src1_to_q8_1) {
  7126. const int64_t ne00 = src0->ne[0];
  7127. const int64_t ne01 = src0->ne[1];
  7128. const int64_t ne02 = src0->ne[2];
  7129. const int64_t ne03 = src0->ne[3];
  7130. const int64_t ne10 = src1->ne[0];
  7131. const int64_t ne11 = src1->ne[1];
  7132. const int64_t ne12 = src1->ne[2];
  7133. const int64_t ne13 = src1->ne[3];
  7134. const int64_t nrows1 = ggml_nrows(src1);
  7135. GGML_ASSERT(ne03 == ne13);
  7136. const int64_t ne0 = dst->ne[0];
  7137. const int64_t ne1 = dst->ne[1];
  7138. const int nb2 = dst->nb[2];
  7139. const int nb3 = dst->nb[3];
  7140. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  7141. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  7142. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  7143. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  7144. const int64_t i02_divisor = ne12 / ne02;
  7145. const size_t src0_ts = ggml_type_size(src0->type);
  7146. const size_t src0_bs = ggml_blck_size(src0->type);
  7147. const size_t q8_1_ts = sizeof(block_q8_1);
  7148. const size_t q8_1_bs = QK8_1;
  7149. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7150. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7151. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7152. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7153. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  7154. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  7155. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  7156. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  7157. GGML_ASSERT(!(split && ne02 > 1));
  7158. GGML_ASSERT(!(split && ne03 > 1));
  7159. GGML_ASSERT(!(split && ne02 < ne12));
  7160. struct dev_data {
  7161. cuda_pool_alloc<char> src0_dd_alloc;
  7162. cuda_pool_alloc<float> src1_ddf_alloc;
  7163. cuda_pool_alloc<char> src1_ddq_alloc;
  7164. cuda_pool_alloc<float> dst_dd_alloc;
  7165. char * src0_dd = nullptr;
  7166. float * src1_ddf = nullptr; // float
  7167. char * src1_ddq = nullptr; // q8_1
  7168. float * dst_dd = nullptr;
  7169. int64_t row_low;
  7170. int64_t row_high;
  7171. };
  7172. dev_data dev[GGML_CUDA_MAX_DEVICES];
  7173. int used_devices = 0;
  7174. for (int id = 0; id < g_device_count; ++id) {
  7175. // by default, use all rows
  7176. dev[id].row_low = 0;
  7177. dev[id].row_high = ne01;
  7178. // for multi GPU, get the row boundaries from tensor split
  7179. // and round to mul_mat_q tile sizes
  7180. if (split) {
  7181. const int64_t rounding = get_row_rounding(src0->type);
  7182. if (id != 0) {
  7183. dev[id].row_low = ne01*g_tensor_split[id];
  7184. if (dev[id].row_low < ne01) {
  7185. dev[id].row_low -= dev[id].row_low % rounding;
  7186. }
  7187. }
  7188. if (id != g_device_count - 1) {
  7189. dev[id].row_high = ne01*g_tensor_split[id + 1];
  7190. if (dev[id].row_high < ne01) {
  7191. dev[id].row_high -= dev[id].row_high % rounding;
  7192. }
  7193. }
  7194. }
  7195. }
  7196. for (int id = 0; id < g_device_count; ++id) {
  7197. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7198. continue;
  7199. }
  7200. used_devices++;
  7201. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7202. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7203. ggml_cuda_set_device(id);
  7204. cudaStream_t stream = g_cudaStreams[id][0];
  7205. if (src0_on_device && src0_is_contiguous) {
  7206. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  7207. } else {
  7208. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  7209. }
  7210. if (src1_on_device && src1_is_contiguous) {
  7211. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7212. } else {
  7213. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7214. }
  7215. if (convert_src1_to_q8_1) {
  7216. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7217. if (src1_on_device && src1_is_contiguous) {
  7218. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7219. CUDA_CHECK(cudaGetLastError());
  7220. }
  7221. }
  7222. if (dst_on_device) {
  7223. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7224. } else {
  7225. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7226. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7227. }
  7228. }
  7229. // if multiple devices are used they need to wait for the main device
  7230. // here an event is recorded that signals that the main device has finished calculating the input data
  7231. if (split && used_devices > 1) {
  7232. ggml_cuda_set_device(g_main_device);
  7233. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7234. }
  7235. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7236. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7237. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7238. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7239. for (int id = 0; id < g_device_count; ++id) {
  7240. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7241. continue;
  7242. }
  7243. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7244. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7245. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7246. ggml_cuda_set_device(id);
  7247. cudaStream_t stream = g_cudaStreams[id][is];
  7248. // wait for main GPU data if necessary
  7249. if (split && (id != g_main_device || is != 0)) {
  7250. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7251. }
  7252. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7253. const int64_t i03 = i0 / ne12;
  7254. const int64_t i02 = i0 % ne12;
  7255. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7256. // for split tensors the data begins at i0 == i0_offset_low
  7257. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7258. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7259. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7260. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7261. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7262. // in that case an offset on dst_ddf_i is needed
  7263. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  7264. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7265. }
  7266. // copy src0, src1 to device if necessary
  7267. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  7268. if (id != g_main_device) {
  7269. if (convert_src1_to_q8_1) {
  7270. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7271. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7272. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7273. } else {
  7274. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7275. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7276. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7277. src1_ncols*ne10*sizeof(float), stream));
  7278. }
  7279. }
  7280. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  7281. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7282. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7283. } else {
  7284. GGML_ASSERT(false);
  7285. }
  7286. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  7287. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7288. CUDA_CHECK(cudaGetLastError());
  7289. }
  7290. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7291. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7292. }
  7293. // do the computation
  7294. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7295. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7296. CUDA_CHECK(cudaGetLastError());
  7297. // copy dst to host or other device if necessary
  7298. if (!dst_on_device) {
  7299. void * dst_off_device;
  7300. cudaMemcpyKind kind;
  7301. if (dst->backend == GGML_BACKEND_CPU) {
  7302. dst_off_device = dst->data;
  7303. kind = cudaMemcpyDeviceToHost;
  7304. } else if (dst->backend == GGML_BACKEND_GPU) {
  7305. dst_off_device = dst_extra->data_device[g_main_device];
  7306. kind = cudaMemcpyDeviceToDevice;
  7307. } else {
  7308. GGML_ASSERT(false);
  7309. }
  7310. if (split) {
  7311. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7312. // dst is NOT transposed.
  7313. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7314. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7315. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7316. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7317. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7318. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7319. #if !defined(GGML_USE_HIPBLAS)
  7320. if (kind == cudaMemcpyDeviceToDevice) {
  7321. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7322. cudaMemcpy3DPeerParms p = {};
  7323. p.dstDevice = g_main_device;
  7324. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7325. p.srcDevice = id;
  7326. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7327. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7328. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7329. } else
  7330. #endif
  7331. {
  7332. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7333. dst_dd_i, row_diff*sizeof(float),
  7334. row_diff*sizeof(float), src1_ncols,
  7335. kind, stream));
  7336. }
  7337. } else {
  7338. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7339. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7340. dhf_dst_i += src1_col_0*ne0;
  7341. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7342. }
  7343. }
  7344. // add event for the main device to wait on until other device is done
  7345. if (split && (id != g_main_device || is != 0)) {
  7346. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7347. }
  7348. }
  7349. }
  7350. }
  7351. // main device waits for all other devices to be finished
  7352. if (split && g_device_count > 1) {
  7353. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7354. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7355. ggml_cuda_set_device(g_main_device);
  7356. for (int id = 0; id < g_device_count; ++id) {
  7357. if (dev[id].row_low == dev[id].row_high) {
  7358. continue;
  7359. }
  7360. for (int64_t is = 0; is < is_max; ++is) {
  7361. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7362. }
  7363. }
  7364. }
  7365. if (dst->backend == GGML_BACKEND_CPU) {
  7366. ggml_cuda_set_device(g_main_device);
  7367. CUDA_CHECK(cudaDeviceSynchronize());
  7368. }
  7369. }
  7370. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7371. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  7372. }
  7373. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7374. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  7375. }
  7376. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7377. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  7378. }
  7379. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7380. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  7381. }
  7382. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7383. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  7384. }
  7385. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7386. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  7387. }
  7388. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7389. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  7390. }
  7391. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7392. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  7393. }
  7394. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7395. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  7396. }
  7397. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7398. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  7399. }
  7400. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7401. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  7402. }
  7403. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7404. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  7405. }
  7406. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7407. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  7408. }
  7409. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7410. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  7411. }
  7412. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7413. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  7414. }
  7415. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7416. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  7417. }
  7418. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7419. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  7420. }
  7421. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7422. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  7423. }
  7424. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7425. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  7426. }
  7427. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  7428. if (!g_cublas_loaded) return false;
  7429. const int64_t ne10 = src1->ne[0];
  7430. const int64_t ne0 = dst->ne[0];
  7431. const int64_t ne1 = dst->ne[1];
  7432. // TODO: find the optimal values for these
  7433. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  7434. src1->type == GGML_TYPE_F32 &&
  7435. dst->type == GGML_TYPE_F32 &&
  7436. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  7437. }
  7438. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7439. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  7440. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  7441. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  7442. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  7443. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7444. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7445. const int64_t ne00 = src0->ne[0];
  7446. const int64_t ne01 = src0->ne[1];
  7447. const int64_t ne02 = src0->ne[2];
  7448. const int64_t ne12 = src1->ne[2];
  7449. ggml_cuda_set_device(g_main_device);
  7450. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7451. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7452. void * src0_ddq = src0_extra->data_device[g_main_device];
  7453. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7454. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7455. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7456. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7457. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  7458. }
  7459. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7460. GGML_ASSERT(!ggml_is_transposed(src0));
  7461. GGML_ASSERT(!ggml_is_transposed(src1));
  7462. GGML_ASSERT(!ggml_is_permuted(src0));
  7463. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  7464. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7465. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7466. const int64_t ne00 = src0->ne[0];
  7467. const int64_t ne01 = src0->ne[1];
  7468. const int64_t ne02 = src0->ne[2];
  7469. const int64_t nb01 = src0->nb[1];
  7470. const int64_t nb02 = src0->nb[2];
  7471. const int64_t ne12 = src1->ne[2];
  7472. ggml_cuda_set_device(g_main_device);
  7473. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7474. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7475. void * src0_ddq = src0_extra->data_device[g_main_device];
  7476. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7477. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7478. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7479. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7480. const int64_t row_stride_x = nb01 / sizeof(half);
  7481. const int64_t channel_stride_x = nb02 / sizeof(half);
  7482. 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);
  7483. }
  7484. static __global__ void k_compute_batched_ptrs(
  7485. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  7486. const void ** ptrs_src, void ** ptrs_dst,
  7487. int64_t ne12, int64_t ne13,
  7488. int64_t ne23,
  7489. size_t nb02, size_t nb03,
  7490. size_t nb12, size_t nb13,
  7491. size_t nbd2, size_t nbd3,
  7492. int64_t r2, int64_t r3) {
  7493. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7494. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7495. if (i13 >= ne13 || i12 >= ne12) {
  7496. return;
  7497. }
  7498. int64_t i03 = i13 / r3;
  7499. int64_t i02 = i12 / r2;
  7500. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  7501. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  7502. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  7503. }
  7504. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7505. GGML_ASSERT(!ggml_is_transposed(src0));
  7506. GGML_ASSERT(!ggml_is_transposed(src1));
  7507. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  7508. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7509. GGML_TENSOR_BINARY_OP_LOCALS
  7510. const int64_t ne_dst = ggml_nelements(dst);
  7511. ggml_cuda_set_device(g_main_device);
  7512. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7513. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7514. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7515. void * src0_ddq = src0_extra->data_device[g_main_device];
  7516. half * src0_f16 = (half *) src0_ddq;
  7517. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7518. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7519. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7520. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7521. // convert src1 to fp16
  7522. cuda_pool_alloc<half> src1_f16_alloc;
  7523. if (src1->type != GGML_TYPE_F16) {
  7524. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7525. const int64_t ne_src1 = ggml_nelements(src1);
  7526. src1_f16_alloc.alloc(ne_src1);
  7527. GGML_ASSERT(to_fp16_cuda != nullptr);
  7528. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  7529. }
  7530. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  7531. cuda_pool_alloc<half> dst_f16;
  7532. char * dst_t;
  7533. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  7534. cudaDataType_t cu_data_type = CUDA_R_16F;
  7535. // dst strides
  7536. size_t nbd2 = dst->nb[2];
  7537. size_t nbd3 = dst->nb[3];
  7538. const half alpha_f16 = 1.0f;
  7539. const half beta_f16 = 0.0f;
  7540. const float alpha_f32 = 1.0f;
  7541. const float beta_f32 = 0.0f;
  7542. const void * alpha = &alpha_f16;
  7543. const void * beta = &beta_f16;
  7544. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  7545. dst_t = (char *) dst_f16.alloc(ne_dst);
  7546. nbd2 /= sizeof(float) / sizeof(half);
  7547. nbd3 /= sizeof(float) / sizeof(half);
  7548. } else {
  7549. dst_t = (char *) dst_ddf;
  7550. cu_compute_type = CUBLAS_COMPUTE_32F;
  7551. cu_data_type = CUDA_R_32F;
  7552. alpha = &alpha_f32;
  7553. beta = &beta_f32;
  7554. }
  7555. GGML_ASSERT(ne12 % ne02 == 0);
  7556. GGML_ASSERT(ne13 % ne03 == 0);
  7557. // broadcast factors
  7558. const int64_t r2 = ne12/ne02;
  7559. const int64_t r3 = ne13/ne03;
  7560. #if 0
  7561. // use cublasGemmEx
  7562. {
  7563. for (int i13 = 0; i13 < ne13; ++i13) {
  7564. for (int i12 = 0; i12 < ne12; ++i12) {
  7565. int i03 = i13 / r3;
  7566. int i02 = i12 / r2;
  7567. CUBLAS_CHECK(
  7568. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7569. ne01, ne11, ne10,
  7570. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  7571. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  7572. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  7573. cu_compute_type,
  7574. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7575. }
  7576. }
  7577. }
  7578. #else
  7579. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  7580. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  7581. // use cublasGemmStridedBatchedEx
  7582. CUBLAS_CHECK(
  7583. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7584. ne01, ne11, ne10,
  7585. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  7586. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  7587. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  7588. ne12*ne13,
  7589. cu_compute_type,
  7590. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7591. } else {
  7592. // use cublasGemmBatchedEx
  7593. const int ne23 = ne12*ne13;
  7594. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  7595. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  7596. dim3 block_dims(ne13, ne12);
  7597. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  7598. src0_f16, src1_f16, dst_t,
  7599. ptrs_src.get(), ptrs_dst.get(),
  7600. ne12, ne13,
  7601. ne23,
  7602. nb02, nb03,
  7603. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  7604. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  7605. nbd2, nbd3,
  7606. r2, r3);
  7607. CUDA_CHECK(cudaGetLastError());
  7608. CUBLAS_CHECK(
  7609. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7610. ne01, ne11, ne10,
  7611. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  7612. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  7613. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  7614. ne23,
  7615. cu_compute_type,
  7616. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7617. }
  7618. #endif
  7619. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  7620. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7621. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  7622. }
  7623. }
  7624. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7625. const bool all_on_device =
  7626. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  7627. (src1->backend == GGML_BACKEND_GPU) &&
  7628. ( dst->backend == GGML_BACKEND_GPU);
  7629. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  7630. int64_t min_compute_capability = INT_MAX;
  7631. for (int id = 0; id < g_device_count; ++id) {
  7632. 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)) {
  7633. min_compute_capability = g_device_caps[id].cc;
  7634. }
  7635. }
  7636. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7637. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  7638. bool use_mul_mat_q = ggml_is_quantized(src0->type);
  7639. #ifdef CUDA_USE_TENSOR_CORES
  7640. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  7641. #endif // CUDA_USE_TENSOR_CORES
  7642. #else
  7643. const bool fp16_performance_good = min_compute_capability >= CC_VOLTA;
  7644. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  7645. #ifdef CUDA_USE_TENSOR_CORES
  7646. // when tensor cores are available, use them for large batch size
  7647. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  7648. use_mul_mat_q = use_mul_mat_q && !(fp16_performance_good && src1->ne[1] > MMQ_MAX_BATCH_SIZE);
  7649. #endif // CUDA_USE_TENSOR_CORES
  7650. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7651. use_mul_mat_q = use_mul_mat_q && ggml_cuda_supports_mmq(src0->type);
  7652. // debug helpers
  7653. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  7654. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  7655. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  7656. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  7657. //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);
  7658. //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);
  7659. 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) {
  7660. // KQ single-batch
  7661. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  7662. } 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) {
  7663. // KQV single-batch
  7664. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  7665. } else if (!split && all_on_device && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  7666. // KQ + KQV multi-batch
  7667. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  7668. } else if (src0->type == GGML_TYPE_F32) {
  7669. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7670. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  7671. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->type == GGML_TYPE_F32) {
  7672. #ifdef GGML_CUDA_FORCE_DMMV
  7673. const bool use_mul_mat_vec_q = false;
  7674. #else
  7675. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  7676. #endif // GGML_CUDA_FORCE_DMMV
  7677. if (use_mul_mat_vec_q) {
  7678. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  7679. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  7680. } else {
  7681. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  7682. }
  7683. } else {
  7684. if (use_mul_mat_q) {
  7685. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  7686. } else {
  7687. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7688. }
  7689. }
  7690. } else {
  7691. GGML_ASSERT(false);
  7692. }
  7693. }
  7694. #if 0
  7695. template<typename ... Srcs>
  7696. static __global__ void k_compute_batched_ptrs_id(
  7697. const void ** ptrs_src, void ** ptrs_dst,
  7698. int ne12, int ne13,
  7699. int ne23,
  7700. int nb02, int nb03,
  7701. int nb12, int nb13,
  7702. int nb2, int nb3,
  7703. int r2, int r3,
  7704. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  7705. const half * src1_f16, half * dst_f16,
  7706. const int32_t * ids, const int id,
  7707. Srcs... src0s) {
  7708. int i = ids[id];
  7709. half * src0_f16;
  7710. const void * srcs_ar[] = { (const half *) src0s... };
  7711. if (src0_type == GGML_TYPE_F16) {
  7712. src0_f16 = (half *) srcs_ar[i];
  7713. } else {
  7714. src0_f16 = src0_as_f16;
  7715. if (threadIdx.x == 0 && threadIdx.y == 0) {
  7716. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  7717. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  7718. }
  7719. }
  7720. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7721. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7722. if (i13 >= ne13 || i12 >= ne12) {
  7723. return;
  7724. }
  7725. int i03 = i13 / r3;
  7726. int i02 = i12 / r2;
  7727. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  7728. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  7729. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  7730. }
  7731. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  7732. const struct ggml_tensor * ids = dst->src[0];
  7733. const struct ggml_tensor * src1 = dst->src[1];
  7734. const struct ggml_tensor * src00 = dst->src[2];
  7735. const int id = dst->op_params[0];
  7736. GGML_ASSERT(!ggml_is_transposed(src00));
  7737. GGML_ASSERT(!ggml_is_transposed(src1));
  7738. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  7739. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7740. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  7741. const int64_t ne01 = src00->ne[1];
  7742. const int64_t ne02 = src00->ne[2];
  7743. const int64_t ne03 = src00->ne[3];
  7744. //const int64_t nb01 = src00->nb[1];
  7745. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  7746. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  7747. const int64_t ne10 = src1->ne[0];
  7748. const int64_t ne11 = src1->ne[1];
  7749. const int64_t ne12 = src1->ne[2];
  7750. const int64_t ne13 = src1->ne[3];
  7751. //const int64_t nb11 = src1->nb[1];
  7752. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  7753. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  7754. const int64_t ne1 = ggml_nelements(src1);
  7755. const int64_t ne = ggml_nelements(dst);
  7756. ggml_cuda_set_device(g_main_device);
  7757. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7758. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7759. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7760. //void * src0_ddq = src0_extra->data_device[g_main_device];
  7761. //half * src0_as_f16 = (half *) src0_ddq;
  7762. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7763. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7764. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7765. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7766. // convert src1 to fp16
  7767. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7768. GGML_ASSERT(to_fp16_cuda != nullptr);
  7769. size_t src1_as = 0;
  7770. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7771. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7772. size_t dst_as = 0;
  7773. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7774. GGML_ASSERT(ne12 % ne02 == 0);
  7775. GGML_ASSERT(ne13 % ne03 == 0);
  7776. // broadcast factors
  7777. const int64_t r2 = ne12/ne02;
  7778. const int64_t r3 = ne13/ne03;
  7779. const half alpha_f16 = 1.0f;
  7780. const half beta_f16 = 0.0f;
  7781. // use cublasGemmBatchedEx
  7782. const int ne23 = ne12*ne13;
  7783. const void ** ptrs_src = nullptr;
  7784. void ** ptrs_dst = nullptr;
  7785. size_t ptrs_src_s = 0;
  7786. size_t ptrs_dst_s = 0;
  7787. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7788. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7789. int64_t src0_ne = ggml_nelements(src00);
  7790. half * src0_as_f16 = nullptr;
  7791. size_t src0_as = 0;
  7792. if (src00->type != GGML_TYPE_F16) {
  7793. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7794. }
  7795. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7796. dim3 block_dims(ne13, ne12);
  7797. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7798. ptrs_src, ptrs_dst,
  7799. ne12, ne13,
  7800. ne23,
  7801. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7802. nb12, nb13,
  7803. dst->nb[2], dst->nb[3],
  7804. r2, r3,
  7805. src00->type, src0_as_f16, src0_ne,
  7806. src1_as_f16, dst_f16,
  7807. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7808. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7809. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7810. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7811. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7812. );
  7813. CUDA_CHECK(cudaGetLastError());
  7814. CUBLAS_CHECK(
  7815. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7816. ne01, ne11, ne10,
  7817. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7818. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7819. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7820. ne23,
  7821. CUBLAS_COMPUTE_16F,
  7822. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7823. if (src0_as != 0) {
  7824. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7825. }
  7826. if (ptrs_src_s != 0) {
  7827. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7828. }
  7829. if (ptrs_dst_s != 0) {
  7830. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7831. }
  7832. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7833. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7834. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7835. ggml_cuda_pool_free(dst_f16, dst_as);
  7836. }
  7837. #endif
  7838. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7839. #if 0
  7840. ggml_cuda_mul_mat_id_cublas(dst);
  7841. // TODO: mmq/mmv support
  7842. #endif
  7843. const int64_t nb11 = src1->nb[1];
  7844. const int64_t nb1 = dst->nb[1];
  7845. const struct ggml_tensor * ids = src0;
  7846. const int32_t id = ((int32_t *) dst->op_params)[0];
  7847. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7848. std::vector<char> ids_host(ggml_nbytes(ids));
  7849. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7850. if (ids->backend == GGML_BACKEND_GPU) {
  7851. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7852. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7853. CUDA_CHECK(cudaStreamSynchronize(stream));
  7854. } else {
  7855. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7856. }
  7857. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7858. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7859. ggml_tensor_extra_gpu src1_row_extra;
  7860. ggml_tensor_extra_gpu dst_row_extra;
  7861. ggml_tensor src1_row = *src1;
  7862. ggml_tensor dst_row = *dst;
  7863. src1_row.backend = GGML_BACKEND_GPU;
  7864. dst_row.backend = GGML_BACKEND_GPU;
  7865. src1_row.extra = &src1_row_extra;
  7866. dst_row.extra = &dst_row_extra;
  7867. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7868. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7869. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7870. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7871. if (src1->ne[1] == 1) {
  7872. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7873. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7874. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7875. //int32_t row_id;
  7876. //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]));
  7877. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7878. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7879. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7880. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7881. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7882. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7883. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7884. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7885. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7886. }
  7887. } else {
  7888. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  7889. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  7890. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  7891. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  7892. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7893. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7894. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7895. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  7896. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7897. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7898. int64_t num_src1_rows = 0;
  7899. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7900. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7901. if (row_id_i != row_id) {
  7902. continue;
  7903. }
  7904. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7905. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  7906. nb11, src1_kind, stream));
  7907. num_src1_rows++;
  7908. }
  7909. if (num_src1_rows == 0) {
  7910. continue;
  7911. }
  7912. src1_row.ne[1] = num_src1_rows;
  7913. dst_row.ne[1] = num_src1_rows;
  7914. src1_row.nb[1] = nb11;
  7915. src1_row.nb[2] = num_src1_rows*nb11;
  7916. src1_row.nb[3] = num_src1_rows*nb11;
  7917. dst_row.nb[1] = nb1;
  7918. dst_row.nb[2] = num_src1_rows*nb1;
  7919. dst_row.nb[3] = num_src1_rows*nb1;
  7920. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7921. num_src1_rows = 0;
  7922. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7923. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7924. if (row_id_i != row_id) {
  7925. continue;
  7926. }
  7927. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7928. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  7929. nb1, dst_kind, stream));
  7930. num_src1_rows++;
  7931. }
  7932. }
  7933. }
  7934. if (dst->backend == GGML_BACKEND_CPU) {
  7935. CUDA_CHECK(cudaStreamSynchronize(stream));
  7936. }
  7937. }
  7938. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7939. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7940. }
  7941. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7942. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7943. }
  7944. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7945. const int64_t ne = ggml_nelements(src0);
  7946. GGML_ASSERT(ne == ggml_nelements(src1));
  7947. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7948. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7949. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7950. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7951. const int64_t ne00 = src0->ne[0];
  7952. const int64_t ne01 = src0->ne[1];
  7953. GGML_ASSERT(src0->ne[3] == 1);
  7954. const int64_t nb00 = src0->nb[0];
  7955. const int64_t nb01 = src0->nb[1];
  7956. const int64_t nb02 = src0->nb[2];
  7957. const int64_t ne10 = src1->ne[0];
  7958. const int64_t ne11 = src1->ne[1];
  7959. GGML_ASSERT(src1->ne[3] == 1);
  7960. const int64_t nb10 = src1->nb[0];
  7961. const int64_t nb11 = src1->nb[1];
  7962. const int64_t nb12 = src1->nb[2];
  7963. ggml_cuda_set_device(g_main_device);
  7964. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7965. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7966. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7967. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7968. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7969. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7970. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7971. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7972. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7973. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7974. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7975. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7976. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7977. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7978. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7979. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7980. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7981. } else {
  7982. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7983. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7984. GGML_ASSERT(false);
  7985. }
  7986. (void) dst;
  7987. }
  7988. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7989. // TODO: why do we pass dst as src1 here?
  7990. ggml_cuda_cpy(src0, dst, nullptr);
  7991. (void) src1;
  7992. }
  7993. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7994. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7995. }
  7996. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7997. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7998. }
  7999. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8000. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8001. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  8002. }
  8003. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8004. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  8005. }
  8006. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8007. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  8008. }
  8009. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8010. GGML_ASSERT(ggml_is_contiguous(src0));
  8011. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  8012. }
  8013. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8014. GGML_ASSERT(ggml_is_contiguous(src0));
  8015. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  8016. }
  8017. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8018. (void) src0;
  8019. (void) src1;
  8020. (void) dst;
  8021. }
  8022. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  8023. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  8024. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  8025. }
  8026. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  8027. const int64_t nrows = ggml_nrows(tensor);
  8028. const int64_t ne0 = tensor->ne[0];
  8029. const size_t nb1 = tensor->nb[1];
  8030. ggml_backend_type backend = tensor->backend;
  8031. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  8032. memset(extra, 0, sizeof(*extra));
  8033. for (int id = 0; id < g_device_count; ++id) {
  8034. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  8035. continue;
  8036. }
  8037. ggml_cuda_set_device(id);
  8038. int64_t row_low, row_high;
  8039. if (backend == GGML_BACKEND_GPU) {
  8040. row_low = 0;
  8041. row_high = nrows;
  8042. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  8043. const int64_t rounding = get_row_rounding(tensor->type);
  8044. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  8045. row_low -= row_low % rounding;
  8046. if (id == g_device_count - 1) {
  8047. row_high = nrows;
  8048. } else {
  8049. row_high = nrows*g_tensor_split[id + 1];
  8050. row_high -= row_high % rounding;
  8051. }
  8052. } else {
  8053. GGML_ASSERT(false);
  8054. }
  8055. if (row_low == row_high) {
  8056. continue;
  8057. }
  8058. int64_t nrows_split = row_high - row_low;
  8059. const size_t offset_split = row_low*nb1;
  8060. size_t size = ggml_nbytes_split(tensor, nrows_split);
  8061. const size_t original_size = size;
  8062. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  8063. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8064. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8065. }
  8066. char * buf;
  8067. CUDA_CHECK(cudaMalloc(&buf, size));
  8068. char * buf_host = (char *)data + offset_split;
  8069. // set padding to 0 to avoid possible NaN values
  8070. if (size > original_size) {
  8071. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  8072. }
  8073. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  8074. extra->data_device[id] = buf;
  8075. if (backend == GGML_BACKEND_GPU_SPLIT) {
  8076. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  8077. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  8078. }
  8079. }
  8080. }
  8081. tensor->extra = extra;
  8082. }
  8083. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  8084. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  8085. return;
  8086. }
  8087. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  8088. for (int id = 0; id < g_device_count; ++id) {
  8089. ggml_cuda_set_device(id);
  8090. if (extra->data_device[id] != nullptr) {
  8091. CUDA_CHECK(cudaFree(extra->data_device[id]));
  8092. }
  8093. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  8094. if (extra->events[id][is] != nullptr) {
  8095. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  8096. }
  8097. }
  8098. }
  8099. delete extra;
  8100. }
  8101. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  8102. static size_t g_temp_tensor_extra_index = 0;
  8103. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8104. if (g_temp_tensor_extras == nullptr) {
  8105. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8106. }
  8107. size_t alloc_index = g_temp_tensor_extra_index;
  8108. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8109. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  8110. memset(extra, 0, sizeof(*extra));
  8111. return extra;
  8112. }
  8113. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  8114. if (scratch && g_scratch_size == 0) {
  8115. return;
  8116. }
  8117. tensor->backend = GGML_BACKEND_GPU;
  8118. // recursively assign CUDA buffers until a compute tensor is found
  8119. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  8120. const ggml_op src0_op = tensor->src[0]->op;
  8121. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  8122. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  8123. }
  8124. }
  8125. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  8126. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  8127. }
  8128. if (scratch && no_alloc) {
  8129. return;
  8130. }
  8131. ggml_tensor_extra_gpu * extra;
  8132. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  8133. tensor->op == GGML_OP_VIEW ||
  8134. force_inplace;
  8135. const size_t size = ggml_nbytes(tensor);
  8136. ggml_cuda_set_device(g_main_device);
  8137. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  8138. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  8139. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8140. size_t offset = 0;
  8141. if (tensor->op == GGML_OP_VIEW) {
  8142. memcpy(&offset, tensor->op_params, sizeof(size_t));
  8143. }
  8144. extra = ggml_cuda_alloc_temp_tensor_extra();
  8145. extra->data_device[g_main_device] = src0_ddc + offset;
  8146. } else if (tensor->op == GGML_OP_CPY) {
  8147. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  8148. void * src1_ddv = src1_extra->data_device[g_main_device];
  8149. extra = ggml_cuda_alloc_temp_tensor_extra();
  8150. extra->data_device[g_main_device] = src1_ddv;
  8151. } else if (scratch) {
  8152. GGML_ASSERT(size <= g_scratch_size);
  8153. if (g_scratch_offset + size > g_scratch_size) {
  8154. g_scratch_offset = 0;
  8155. }
  8156. char * data = (char *) g_scratch_buffer;
  8157. if (data == nullptr) {
  8158. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  8159. g_scratch_buffer = data;
  8160. }
  8161. extra = ggml_cuda_alloc_temp_tensor_extra();
  8162. extra->data_device[g_main_device] = data + g_scratch_offset;
  8163. g_scratch_offset += size;
  8164. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  8165. } else { // allocate new buffers outside of scratch
  8166. void * data;
  8167. CUDA_CHECK(cudaMalloc(&data, size));
  8168. CUDA_CHECK(cudaMemset(data, 0, size));
  8169. extra = new ggml_tensor_extra_gpu;
  8170. memset(extra, 0, sizeof(*extra));
  8171. extra->data_device[g_main_device] = data;
  8172. }
  8173. tensor->extra = extra;
  8174. }
  8175. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  8176. if (g_scratch_size == 0) {
  8177. return;
  8178. }
  8179. if (g_scratch_buffer == nullptr) {
  8180. ggml_cuda_set_device(g_main_device);
  8181. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  8182. }
  8183. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  8184. const bool inplace = tensor->view_src != nullptr;
  8185. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  8186. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  8187. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8188. size_t view_offset = 0;
  8189. if (tensor->op == GGML_OP_VIEW) {
  8190. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  8191. }
  8192. extra->data_device[g_main_device] = src0_ddc + view_offset;
  8193. } else {
  8194. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  8195. }
  8196. tensor->extra = extra;
  8197. }
  8198. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  8199. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8200. GGML_ASSERT(ggml_is_contiguous(tensor));
  8201. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  8202. ggml_cuda_set_device(g_main_device);
  8203. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  8204. }
  8205. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  8206. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  8207. }
  8208. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  8209. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  8210. }
  8211. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  8212. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  8213. }
  8214. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  8215. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  8216. }
  8217. void ggml_cuda_set_main_device(const int main_device) {
  8218. if (main_device >= g_device_count) {
  8219. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8220. main_device, g_device_count, g_main_device);
  8221. return;
  8222. }
  8223. if (g_main_device != main_device && g_device_count > 1) {
  8224. g_main_device = main_device;
  8225. cudaDeviceProp prop;
  8226. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8227. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8228. }
  8229. }
  8230. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  8231. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  8232. // it still won't always work as expected, but it's better than nothing
  8233. if (scratch_size > g_scratch_size) {
  8234. ggml_cuda_free_scratch();
  8235. }
  8236. g_scratch_size = std::max(g_scratch_size, scratch_size);
  8237. }
  8238. void ggml_cuda_free_scratch() {
  8239. if (g_scratch_buffer == nullptr) {
  8240. return;
  8241. }
  8242. CUDA_CHECK(cudaFree(g_scratch_buffer));
  8243. g_scratch_buffer = nullptr;
  8244. }
  8245. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8246. if (!g_cublas_loaded) return false;
  8247. ggml_cuda_func_t func;
  8248. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  8249. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  8250. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  8251. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8252. return false;
  8253. }
  8254. if (tensor->op == GGML_OP_MUL_MAT) {
  8255. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8256. #ifndef NDEBUG
  8257. 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]);
  8258. #endif
  8259. return false;
  8260. }
  8261. }
  8262. switch (tensor->op) {
  8263. case GGML_OP_REPEAT:
  8264. func = ggml_cuda_repeat;
  8265. break;
  8266. case GGML_OP_GET_ROWS:
  8267. func = ggml_cuda_get_rows;
  8268. break;
  8269. case GGML_OP_DUP:
  8270. func = ggml_cuda_dup;
  8271. break;
  8272. case GGML_OP_ADD:
  8273. func = ggml_cuda_add;
  8274. break;
  8275. case GGML_OP_ACC:
  8276. func = ggml_cuda_acc;
  8277. break;
  8278. case GGML_OP_MUL:
  8279. func = ggml_cuda_mul;
  8280. break;
  8281. case GGML_OP_DIV:
  8282. func = ggml_cuda_div;
  8283. break;
  8284. case GGML_OP_UNARY:
  8285. switch (ggml_get_unary_op(tensor)) {
  8286. case GGML_UNARY_OP_GELU:
  8287. func = ggml_cuda_gelu;
  8288. break;
  8289. case GGML_UNARY_OP_SILU:
  8290. func = ggml_cuda_silu;
  8291. break;
  8292. case GGML_UNARY_OP_GELU_QUICK:
  8293. func = ggml_cuda_gelu_quick;
  8294. break;
  8295. case GGML_UNARY_OP_TANH:
  8296. func = ggml_cuda_tanh;
  8297. break;
  8298. case GGML_UNARY_OP_RELU:
  8299. func = ggml_cuda_relu;
  8300. break;
  8301. default:
  8302. return false;
  8303. }
  8304. break;
  8305. case GGML_OP_NORM:
  8306. func = ggml_cuda_norm;
  8307. break;
  8308. case GGML_OP_GROUP_NORM:
  8309. func = ggml_cuda_group_norm;
  8310. break;
  8311. case GGML_OP_CONCAT:
  8312. func = ggml_cuda_concat;
  8313. break;
  8314. case GGML_OP_UPSCALE:
  8315. func = ggml_cuda_upscale;
  8316. break;
  8317. case GGML_OP_PAD:
  8318. func = ggml_cuda_pad;
  8319. break;
  8320. case GGML_OP_LEAKY_RELU:
  8321. func = ggml_cuda_leaky_relu;
  8322. break;
  8323. case GGML_OP_RMS_NORM:
  8324. func = ggml_cuda_rms_norm;
  8325. break;
  8326. case GGML_OP_MUL_MAT:
  8327. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8328. return false;
  8329. }
  8330. func = ggml_cuda_mul_mat;
  8331. break;
  8332. case GGML_OP_MUL_MAT_ID:
  8333. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8334. return false;
  8335. }
  8336. func = ggml_cuda_mul_mat_id;
  8337. break;
  8338. case GGML_OP_SCALE:
  8339. func = ggml_cuda_scale;
  8340. break;
  8341. case GGML_OP_SQR:
  8342. func = ggml_cuda_sqr;
  8343. break;
  8344. case GGML_OP_CLAMP:
  8345. func = ggml_cuda_clamp;
  8346. break;
  8347. case GGML_OP_CPY:
  8348. func = ggml_cuda_cpy;
  8349. break;
  8350. case GGML_OP_CONT:
  8351. func = ggml_cuda_dup;
  8352. break;
  8353. case GGML_OP_NONE:
  8354. case GGML_OP_RESHAPE:
  8355. case GGML_OP_VIEW:
  8356. case GGML_OP_PERMUTE:
  8357. case GGML_OP_TRANSPOSE:
  8358. func = ggml_cuda_nop;
  8359. break;
  8360. case GGML_OP_DIAG_MASK_INF:
  8361. func = ggml_cuda_diag_mask_inf;
  8362. break;
  8363. case GGML_OP_SOFT_MAX:
  8364. func = ggml_cuda_soft_max;
  8365. break;
  8366. case GGML_OP_ROPE:
  8367. func = ggml_cuda_rope;
  8368. break;
  8369. case GGML_OP_ALIBI:
  8370. func = ggml_cuda_alibi;
  8371. break;
  8372. case GGML_OP_IM2COL:
  8373. func = ggml_cuda_im2col;
  8374. break;
  8375. case GGML_OP_SUM_ROWS:
  8376. func = ggml_cuda_sum_rows;
  8377. break;
  8378. case GGML_OP_ARGSORT:
  8379. func = ggml_cuda_argsort;
  8380. break;
  8381. default:
  8382. return false;
  8383. }
  8384. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  8385. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8386. }
  8387. if (params->ith != 0) {
  8388. return true;
  8389. }
  8390. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  8391. return true;
  8392. }
  8393. func(tensor->src[0], tensor->src[1], tensor);
  8394. return true;
  8395. }
  8396. int ggml_cuda_get_device_count() {
  8397. int device_count;
  8398. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8399. return 0;
  8400. }
  8401. return device_count;
  8402. }
  8403. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8404. cudaDeviceProp prop;
  8405. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8406. snprintf(description, description_size, "%s", prop.name);
  8407. }
  8408. ////////////////////////////////////////////////////////////////////////////////
  8409. // backend interface
  8410. #define UNUSED GGML_UNUSED
  8411. // cuda buffer
  8412. struct ggml_backend_buffer_context_cuda {
  8413. int device;
  8414. void * dev_ptr = nullptr;
  8415. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8416. size_t temp_tensor_extra_index = 0;
  8417. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  8418. ~ggml_backend_buffer_context_cuda() {
  8419. delete[] temp_tensor_extras;
  8420. }
  8421. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8422. if (temp_tensor_extras == nullptr) {
  8423. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8424. }
  8425. size_t alloc_index = temp_tensor_extra_index;
  8426. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8427. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8428. memset(extra, 0, sizeof(*extra));
  8429. return extra;
  8430. }
  8431. };
  8432. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8433. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8434. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8435. delete ctx;
  8436. }
  8437. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8438. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8439. return ctx->dev_ptr;
  8440. }
  8441. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8442. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8443. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8444. assert(tensor->view_src->buffer->buft == buffer->buft);
  8445. tensor->backend = tensor->view_src->backend;
  8446. tensor->extra = tensor->view_src->extra;
  8447. return;
  8448. }
  8449. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8450. extra->data_device[ctx->device] = tensor->data;
  8451. tensor->backend = GGML_BACKEND_GPU;
  8452. tensor->extra = extra;
  8453. if (ggml_is_quantized(tensor->type)) {
  8454. // initialize padding to 0 to avoid possible NaN values
  8455. int64_t row_low = 0;
  8456. int64_t row_high = ggml_nrows(tensor);
  8457. int64_t nrows_split = row_high - row_low;
  8458. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  8459. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8460. if (padded_size > original_size && tensor->view_src == nullptr) {
  8461. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  8462. }
  8463. }
  8464. UNUSED(buffer);
  8465. }
  8466. 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) {
  8467. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8468. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8469. ggml_cuda_set_device(ctx->device);
  8470. CUDA_CHECK(cudaDeviceSynchronize());
  8471. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8472. CUDA_CHECK(cudaDeviceSynchronize());
  8473. }
  8474. 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) {
  8475. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8476. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8477. ggml_cuda_set_device(ctx->device);
  8478. CUDA_CHECK(cudaDeviceSynchronize());
  8479. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8480. }
  8481. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8482. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  8483. ggml_cuda_set_device(ctx->device);
  8484. CUDA_CHECK(cudaDeviceSynchronize());
  8485. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8486. }
  8487. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  8488. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8489. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8490. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8491. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8492. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8493. /* .cpy_tensor_from = */ NULL,
  8494. /* .cpy_tensor_to = */ NULL,
  8495. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8496. };
  8497. // cuda buffer type
  8498. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8499. int device = (int) (intptr_t) buft->context;
  8500. ggml_cuda_set_device(device);
  8501. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  8502. void * dev_ptr;
  8503. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  8504. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  8505. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  8506. }
  8507. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8508. return 128;
  8509. UNUSED(buft);
  8510. }
  8511. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  8512. int64_t row_low = 0;
  8513. int64_t row_high = ggml_nrows(tensor);
  8514. int64_t nrows_split = row_high - row_low;
  8515. size_t size = ggml_nbytes_split(tensor, nrows_split);
  8516. int64_t ne0 = tensor->ne[0];
  8517. if (ggml_is_quantized(tensor->type)) {
  8518. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8519. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8520. }
  8521. }
  8522. return size;
  8523. UNUSED(buft);
  8524. }
  8525. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  8526. return ggml_backend_is_cuda(backend);
  8527. UNUSED(buft);
  8528. }
  8529. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  8530. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  8531. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  8532. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  8533. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  8534. /* .is_host = */ nullptr,
  8535. };
  8536. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  8537. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  8538. static bool ggml_backend_cuda_buffer_type_initialized = false;
  8539. if (!ggml_backend_cuda_buffer_type_initialized) {
  8540. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  8541. ggml_backend_cuda_buffer_types[i] = {
  8542. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  8543. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  8544. };
  8545. }
  8546. ggml_backend_cuda_buffer_type_initialized = true;
  8547. }
  8548. return &ggml_backend_cuda_buffer_types[device];
  8549. }
  8550. // host buffer type
  8551. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8552. ggml_cuda_host_free(buffer->context);
  8553. }
  8554. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8555. void * ptr = ggml_cuda_host_malloc(size);
  8556. if (ptr == nullptr) {
  8557. // fallback to cpu buffer
  8558. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  8559. }
  8560. // FIXME: this is a hack to avoid having to implement a new buffer type
  8561. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  8562. buffer->buft = buft;
  8563. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  8564. return buffer;
  8565. }
  8566. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  8567. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  8568. /* .iface = */ {
  8569. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  8570. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  8571. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  8572. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  8573. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  8574. },
  8575. /* .context = */ nullptr,
  8576. };
  8577. return &ggml_backend_cuda_buffer_type_host;
  8578. }
  8579. // backend
  8580. struct ggml_backend_context_cuda {
  8581. int device;
  8582. };
  8583. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  8584. return GGML_CUDA_NAME;
  8585. UNUSED(backend);
  8586. }
  8587. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  8588. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8589. delete cuda_ctx;
  8590. delete backend;
  8591. }
  8592. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  8593. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8594. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  8595. }
  8596. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8597. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8598. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  8599. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8600. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  8601. }
  8602. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8603. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8604. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  8605. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8606. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  8607. }
  8608. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  8609. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8610. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  8611. UNUSED(backend);
  8612. }
  8613. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8614. GGML_ASSERT(!"not implemented");
  8615. return nullptr;
  8616. UNUSED(backend);
  8617. UNUSED(cgraph);
  8618. }
  8619. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  8620. GGML_ASSERT(!"not implemented");
  8621. UNUSED(backend);
  8622. UNUSED(plan);
  8623. }
  8624. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  8625. GGML_ASSERT(!"not implemented");
  8626. UNUSED(backend);
  8627. UNUSED(plan);
  8628. }
  8629. static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8630. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  8631. ggml_cuda_set_main_device(cuda_ctx->device);
  8632. ggml_compute_params params = {};
  8633. params.type = GGML_TASK_COMPUTE;
  8634. params.ith = 0;
  8635. for (int i = 0; i < cgraph->n_nodes; i++) {
  8636. ggml_tensor * node = cgraph->nodes[i];
  8637. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  8638. continue;
  8639. assert(node->backend == GGML_BACKEND_GPU);
  8640. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  8641. assert(node->extra != nullptr);
  8642. for (int j = 0; j < GGML_MAX_SRC; j++) {
  8643. if (node->src[j] != nullptr) {
  8644. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  8645. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  8646. assert(node->src[j]->extra != nullptr);
  8647. }
  8648. }
  8649. bool ok = ggml_cuda_compute_forward(&params, node);
  8650. if (!ok) {
  8651. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  8652. }
  8653. GGML_ASSERT(ok);
  8654. #if 0
  8655. if (node->type == GGML_TYPE_F32) {
  8656. cudaDeviceSynchronize();
  8657. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  8658. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  8659. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  8660. ggml_type_name(node->src[0]->type),
  8661. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  8662. node->src[0]->name,
  8663. node->src[1] ? node->src[1]->name : "none");
  8664. double sum = 0.0;
  8665. double sq_sum = 0.0;
  8666. for (int i = 0; i < ggml_nelements(node); i++) {
  8667. printf("%f ", tmp[i]);
  8668. sum += tmp[i];
  8669. sq_sum += tmp[i]*tmp[i];
  8670. }
  8671. printf("\n");
  8672. printf("sum: %f, ", sum);
  8673. printf("sq_sum: %f\n", sq_sum);
  8674. }
  8675. #endif
  8676. }
  8677. UNUSED(backend);
  8678. return true;
  8679. }
  8680. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  8681. switch (op->op) {
  8682. case GGML_OP_UNARY:
  8683. switch (ggml_get_unary_op(op)) {
  8684. case GGML_UNARY_OP_GELU:
  8685. case GGML_UNARY_OP_SILU:
  8686. case GGML_UNARY_OP_RELU:
  8687. case GGML_UNARY_OP_GELU_QUICK:
  8688. case GGML_UNARY_OP_TANH:
  8689. return true;
  8690. default:
  8691. return false;
  8692. }
  8693. break;
  8694. case GGML_OP_MUL_MAT:
  8695. case GGML_OP_MUL_MAT_ID:
  8696. {
  8697. struct ggml_tensor * a;
  8698. struct ggml_tensor * b;
  8699. if (op->op == GGML_OP_MUL_MAT) {
  8700. a = op->src[0];
  8701. b = op->src[1];
  8702. } else {
  8703. a = op->src[2];
  8704. b = op->src[1];
  8705. }
  8706. if (a->ne[3] != b->ne[3]) {
  8707. return false;
  8708. }
  8709. return true;
  8710. } break;
  8711. case GGML_OP_GET_ROWS:
  8712. {
  8713. switch (op->src[0]->type) {
  8714. case GGML_TYPE_F16:
  8715. case GGML_TYPE_F32:
  8716. case GGML_TYPE_Q4_0:
  8717. case GGML_TYPE_Q4_1:
  8718. case GGML_TYPE_Q5_0:
  8719. case GGML_TYPE_Q5_1:
  8720. case GGML_TYPE_Q8_0:
  8721. return true;
  8722. default:
  8723. return false;
  8724. }
  8725. } break;
  8726. case GGML_OP_CPY:
  8727. {
  8728. ggml_type src0_type = op->src[0]->type;
  8729. ggml_type src1_type = op->src[1]->type;
  8730. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  8731. return true;
  8732. }
  8733. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  8734. return true;
  8735. }
  8736. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  8737. return true;
  8738. }
  8739. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  8740. return true;
  8741. }
  8742. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  8743. return true;
  8744. }
  8745. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8746. return true;
  8747. }
  8748. return false;
  8749. } break;
  8750. case GGML_OP_DUP:
  8751. case GGML_OP_REPEAT:
  8752. case GGML_OP_CONCAT:
  8753. {
  8754. ggml_type src0_type = op->src[0]->type;
  8755. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  8756. } break;
  8757. case GGML_OP_NONE:
  8758. case GGML_OP_RESHAPE:
  8759. case GGML_OP_VIEW:
  8760. case GGML_OP_PERMUTE:
  8761. case GGML_OP_TRANSPOSE:
  8762. case GGML_OP_NORM:
  8763. case GGML_OP_ADD:
  8764. case GGML_OP_MUL:
  8765. case GGML_OP_DIV:
  8766. case GGML_OP_RMS_NORM:
  8767. case GGML_OP_SCALE:
  8768. case GGML_OP_SQR:
  8769. case GGML_OP_CLAMP:
  8770. case GGML_OP_CONT:
  8771. case GGML_OP_DIAG_MASK_INF:
  8772. case GGML_OP_SOFT_MAX:
  8773. case GGML_OP_ROPE:
  8774. case GGML_OP_ALIBI:
  8775. case GGML_OP_IM2COL:
  8776. case GGML_OP_SUM_ROWS:
  8777. case GGML_OP_ARGSORT:
  8778. case GGML_OP_ACC:
  8779. case GGML_OP_GROUP_NORM:
  8780. case GGML_OP_UPSCALE:
  8781. case GGML_OP_PAD:
  8782. case GGML_OP_LEAKY_RELU:
  8783. return true;
  8784. default:
  8785. return false;
  8786. }
  8787. UNUSED(backend);
  8788. }
  8789. static ggml_backend_i cuda_backend_i = {
  8790. /* .get_name = */ ggml_backend_cuda_name,
  8791. /* .free = */ ggml_backend_cuda_free,
  8792. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8793. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8794. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8795. /* .cpy_tensor_from_async = */ NULL,
  8796. /* .cpy_tensor_to_async = */ NULL,
  8797. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8798. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8799. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8800. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8801. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8802. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8803. };
  8804. ggml_backend_t ggml_backend_cuda_init(int device) {
  8805. ggml_init_cublas(); // TODO: remove from ggml.c
  8806. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8807. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8808. return nullptr;
  8809. }
  8810. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8811. ggml_cuda_set_main_device(device);
  8812. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8813. /* .device = */ device
  8814. };
  8815. ggml_backend_t cuda_backend = new ggml_backend {
  8816. /* .interface = */ cuda_backend_i,
  8817. /* .context = */ ctx
  8818. };
  8819. return cuda_backend;
  8820. }
  8821. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8822. return backend->iface.get_name == ggml_backend_cuda_name;
  8823. }
  8824. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8825. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8826. return cuda_backend;
  8827. UNUSED(params);
  8828. }
  8829. extern "C" int ggml_backend_cuda_reg_devices();
  8830. int ggml_backend_cuda_reg_devices() {
  8831. int device_count = ggml_cuda_get_device_count();
  8832. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8833. for (int i = 0; i < device_count; i++) {
  8834. char name[128];
  8835. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8836. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8837. }
  8838. return device_count;
  8839. }