ggml-cuda.cu 446 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616
  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 <string>
  12. #include <vector>
  13. #include <map>
  14. #include <array>
  15. // stringize macro for converting __CUDA_ARCH_LIST__ (list of integers) to string
  16. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  17. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  18. #if defined(GGML_USE_HIPBLAS)
  19. #include <hip/hip_runtime.h>
  20. #include <hipblas/hipblas.h>
  21. #include <hip/hip_fp16.h>
  22. #ifdef __HIP_PLATFORM_AMD__
  23. // for rocblas_initialize()
  24. #include "rocblas/rocblas.h"
  25. #endif // __HIP_PLATFORM_AMD__
  26. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  27. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  28. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  29. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  30. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  31. #define CUBLAS_OP_N HIPBLAS_OP_N
  32. #define CUBLAS_OP_T HIPBLAS_OP_T
  33. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  34. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  35. #define CUDA_R_16F HIPBLAS_R_16F
  36. #define CUDA_R_32F HIPBLAS_R_32F
  37. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  38. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  39. #define cublasCreate hipblasCreate
  40. #define cublasGemmEx hipblasGemmEx
  41. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  42. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  43. #define cublasHandle_t hipblasHandle_t
  44. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  45. #define cublasSetStream hipblasSetStream
  46. #define cublasSgemm hipblasSgemm
  47. #define cublasStatus_t hipblasStatus_t
  48. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  49. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  50. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  51. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  52. #define cudaDeviceProp hipDeviceProp_t
  53. #define cudaDeviceSynchronize hipDeviceSynchronize
  54. #define cudaError_t hipError_t
  55. #define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
  56. #define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
  57. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  58. #define cudaEventDisableTiming hipEventDisableTiming
  59. #define cudaEventRecord hipEventRecord
  60. #define cudaEvent_t hipEvent_t
  61. #define cudaEventDestroy hipEventDestroy
  62. #define cudaFree hipFree
  63. #define cudaFreeHost hipHostFree
  64. #define cudaGetDevice hipGetDevice
  65. #define cudaGetDeviceCount hipGetDeviceCount
  66. #define cudaGetDeviceProperties hipGetDeviceProperties
  67. #define cudaGetErrorString hipGetErrorString
  68. #define cudaGetLastError hipGetLastError
  69. #ifdef GGML_HIP_UMA
  70. #define cudaMalloc hipMallocManaged
  71. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  72. #else
  73. #define cudaMalloc hipMalloc
  74. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  75. #endif
  76. #define cudaMemcpy hipMemcpy
  77. #define cudaMemcpyAsync hipMemcpyAsync
  78. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  79. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  80. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  81. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  82. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  83. #define cudaMemcpyKind hipMemcpyKind
  84. #define cudaMemset hipMemset
  85. #define cudaMemsetAsync hipMemsetAsync
  86. #define cudaMemGetInfo hipMemGetInfo
  87. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  88. #define cudaSetDevice hipSetDevice
  89. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  90. #define cudaStreamFireAndForget hipStreamFireAndForget
  91. #define cudaStreamNonBlocking hipStreamNonBlocking
  92. #define cudaStreamSynchronize hipStreamSynchronize
  93. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  94. #define cudaStream_t hipStream_t
  95. #define cudaSuccess hipSuccess
  96. #define __trap abort
  97. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  98. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  99. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  100. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  101. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  102. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  103. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  104. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  105. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  106. #else
  107. #include <cuda_runtime.h>
  108. #include <cuda.h>
  109. #include <cublas_v2.h>
  110. #include <cuda_fp16.h>
  111. #if CUDART_VERSION < 11020
  112. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  113. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  114. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  115. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  116. #define cublasComputeType_t cudaDataType_t
  117. #endif // CUDART_VERSION < 11020
  118. #endif // defined(GGML_USE_HIPBLAS)
  119. // ggml-cuda need half type so keep ggml headers include at last
  120. #include "ggml-cuda.h"
  121. #include "ggml.h"
  122. #include "ggml-backend-impl.h"
  123. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  124. #define CC_PASCAL 600
  125. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  126. #define CC_VOLTA 700
  127. #define CC_OFFSET_AMD 1000000
  128. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  129. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  130. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  131. #define GGML_CUDA_MAX_NODES 8192
  132. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  133. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  134. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  135. // - 7B quantum model: +100-200 MB
  136. // - 13B quantum model: +200-400 MB
  137. //
  138. //#define GGML_CUDA_FORCE_MMQ
  139. // TODO: improve this to be correct for more hardware
  140. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  141. #if !defined(GGML_CUDA_FORCE_MMQ)
  142. #define CUDA_USE_TENSOR_CORES
  143. #endif
  144. #define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
  145. #define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
  146. #if defined(GGML_USE_HIPBLAS)
  147. #define __CUDA_ARCH__ 1300
  148. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  149. defined(__gfx1150__) || defined(__gfx1151__)
  150. #define RDNA3
  151. #endif
  152. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  153. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  154. #define RDNA2
  155. #endif
  156. #ifndef __has_builtin
  157. #define __has_builtin(x) 0
  158. #endif
  159. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  160. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  161. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  162. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  163. #if __has_builtin(__builtin_elementwise_sub_sat)
  164. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  165. return reinterpret_cast<const int &>(c);
  166. #else
  167. int8x4_t c;
  168. int16_t tmp;
  169. #pragma unroll
  170. for (int i = 0; i < 4; i++) {
  171. tmp = va[i] - vb[i];
  172. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  173. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  174. c[i] = tmp;
  175. }
  176. return reinterpret_cast<int &>(c);
  177. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  178. }
  179. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  180. return __vsubss4(a, b);
  181. }
  182. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  183. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  184. c = __builtin_amdgcn_sdot4(a, b, c, false);
  185. #elif defined(RDNA3)
  186. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  187. #elif defined(__gfx1010__) || defined(__gfx900__)
  188. int tmp1;
  189. int tmp2;
  190. asm("\n \
  191. 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 \
  192. 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 \
  193. v_add3_u32 %0, %1, %2, %0 \n \
  194. 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 \
  195. 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 \
  196. v_add3_u32 %0, %1, %2, %0 \n \
  197. "
  198. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  199. : "v"(a), "v"(b)
  200. );
  201. #else
  202. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  203. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  204. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  205. #endif
  206. return c;
  207. }
  208. #endif // defined(GGML_USE_HIPBLAS)
  209. #if defined(_MSC_VER)
  210. #pragma warning(disable: 4244 4267) // possible loss of data
  211. #endif
  212. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  213. [[noreturn]]
  214. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  215. int id = -1; // in case cudaGetDevice fails
  216. cudaGetDevice(&id);
  217. fprintf(stderr, "CUDA error: %s\n", msg);
  218. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  219. fprintf(stderr, " %s\n", stmt);
  220. // abort with GGML_ASSERT to get a stack trace
  221. GGML_ASSERT(!"CUDA error");
  222. }
  223. #define CUDA_CHECK_GEN(err, success, error_fn) \
  224. do { \
  225. auto err_ = (err); \
  226. if (err_ != (success)) { \
  227. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  228. } \
  229. } while (0)
  230. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  231. #if CUDART_VERSION >= 12000
  232. static const char * cublas_get_error_str(const cublasStatus_t err) {
  233. return cublasGetStatusString(err);
  234. }
  235. #else
  236. static const char * cublas_get_error_str(const cublasStatus_t err) {
  237. switch (err) {
  238. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  239. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  240. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  241. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  242. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  243. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  244. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  245. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  246. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  247. default: return "unknown error";
  248. }
  249. }
  250. #endif // CUDART_VERSION >= 12000
  251. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  252. #if !defined(GGML_USE_HIPBLAS)
  253. static const char * cu_get_error_str(CUresult err) {
  254. const char * err_str;
  255. cuGetErrorString(err, &err_str);
  256. return err_str;
  257. }
  258. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  259. #endif
  260. #if CUDART_VERSION >= 11100
  261. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  262. #else
  263. #define GGML_CUDA_ASSUME(x)
  264. #endif // CUDART_VERSION >= 11100
  265. #ifdef GGML_CUDA_F16
  266. typedef half dfloat; // dequantize float
  267. typedef half2 dfloat2;
  268. #else
  269. typedef float dfloat; // dequantize float
  270. typedef float2 dfloat2;
  271. #endif //GGML_CUDA_F16
  272. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  273. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  274. int x32 = 0;
  275. x32 |= x16[0] << 0;
  276. x32 |= x16[1] << 16;
  277. return x32;
  278. }
  279. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  280. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  281. int x32 = 0;
  282. x32 |= x16[0] << 0;
  283. x32 |= x16[1] << 16;
  284. return x32;
  285. }
  286. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  287. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  288. }
  289. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  290. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  291. }
  292. template<typename T>
  293. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  294. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  295. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  296. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  297. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  298. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  299. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  300. typedef void (*ggml_cuda_op_mul_mat_t)(
  301. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  302. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  303. const int64_t src1_padded_row_size, cudaStream_t stream);
  304. typedef void (*ggml_cuda_op_flatten_t)(
  305. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  306. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  307. // QK = number of values after dequantization
  308. // QR = QK / number of values before dequantization
  309. // QI = number of 32 bit integers before dequantization
  310. #define QK4_0 32
  311. #define QR4_0 2
  312. #define QI4_0 (QK4_0 / (4 * QR4_0))
  313. typedef struct {
  314. half d; // delta
  315. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  316. } block_q4_0;
  317. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  318. #define QK4_1 32
  319. #define QR4_1 2
  320. #define QI4_1 (QK4_1 / (4 * QR4_1))
  321. typedef struct {
  322. half2 dm; // dm.x = delta, dm.y = min
  323. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  324. } block_q4_1;
  325. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  326. #define QK5_0 32
  327. #define QR5_0 2
  328. #define QI5_0 (QK5_0 / (4 * QR5_0))
  329. typedef struct {
  330. half d; // delta
  331. uint8_t qh[4]; // 5-th bit of quants
  332. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  333. } block_q5_0;
  334. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  335. #define QK5_1 32
  336. #define QR5_1 2
  337. #define QI5_1 (QK5_1 / (4 * QR5_1))
  338. typedef struct {
  339. half2 dm; // dm.x = delta, dm.y = min
  340. uint8_t qh[4]; // 5-th bit of quants
  341. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  342. } block_q5_1;
  343. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  344. #define QK8_0 32
  345. #define QR8_0 1
  346. #define QI8_0 (QK8_0 / (4 * QR8_0))
  347. typedef struct {
  348. half d; // delta
  349. int8_t qs[QK8_0]; // quants
  350. } block_q8_0;
  351. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  352. #define QK8_1 32
  353. #define QR8_1 1
  354. #define QI8_1 (QK8_1 / (4 * QR8_1))
  355. typedef struct {
  356. half2 ds; // ds.x = delta, ds.y = sum
  357. int8_t qs[QK8_0]; // quants
  358. } block_q8_1;
  359. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  360. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  361. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  362. typedef void (*load_tiles_cuda_t)(
  363. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  364. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  365. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  366. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  367. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  368. //================================= k-quants
  369. #ifdef GGML_QKK_64
  370. #define QK_K 64
  371. #define K_SCALE_SIZE 4
  372. #else
  373. #define QK_K 256
  374. #define K_SCALE_SIZE 12
  375. #endif
  376. #define QR2_K 4
  377. #define QI2_K (QK_K / (4*QR2_K))
  378. typedef struct {
  379. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  380. uint8_t qs[QK_K/4]; // quants
  381. half2 dm; // super-block scale for quantized scales/mins
  382. } block_q2_K;
  383. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  384. #define QR3_K 4
  385. #define QI3_K (QK_K / (4*QR3_K))
  386. typedef struct {
  387. uint8_t hmask[QK_K/8]; // quants - high bit
  388. uint8_t qs[QK_K/4]; // quants - low 2 bits
  389. #ifdef GGML_QKK_64
  390. uint8_t scales[2]; // scales, quantized with 8 bits
  391. #else
  392. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  393. #endif
  394. half d; // super-block scale
  395. } block_q3_K;
  396. //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");
  397. #define QR4_K 2
  398. #define QI4_K (QK_K / (4*QR4_K))
  399. #ifdef GGML_QKK_64
  400. typedef struct {
  401. half dm[2]; // super-block scales/mins
  402. uint8_t scales[2]; // 4-bit block scales/mins
  403. uint8_t qs[QK_K/2]; // 4--bit quants
  404. } block_q4_K;
  405. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  406. #else
  407. typedef struct {
  408. half2 dm; // super-block scale for quantized scales/mins
  409. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  410. uint8_t qs[QK_K/2]; // 4--bit quants
  411. } block_q4_K;
  412. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  413. #endif
  414. #define QR5_K 2
  415. #define QI5_K (QK_K / (4*QR5_K))
  416. #ifdef GGML_QKK_64
  417. typedef struct {
  418. half d; // super-block scale
  419. int8_t scales[QK_K/16]; // block scales
  420. uint8_t qh[QK_K/8]; // quants, high bit
  421. uint8_t qs[QK_K/2]; // quants, low 4 bits
  422. } block_q5_K;
  423. 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");
  424. #else
  425. typedef struct {
  426. half2 dm; // super-block scale for quantized scales/mins
  427. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  428. uint8_t qh[QK_K/8]; // quants, high bit
  429. uint8_t qs[QK_K/2]; // quants, low 4 bits
  430. } block_q5_K;
  431. 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");
  432. #endif
  433. #define QR6_K 2
  434. #define QI6_K (QK_K / (4*QR6_K))
  435. typedef struct {
  436. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  437. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  438. int8_t scales[QK_K/16]; // scales
  439. half d; // delta
  440. } block_q6_K;
  441. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  442. #define QR2_XXS 8
  443. #define QI2_XXS (QK_K / (4*QR2_XXS))
  444. typedef struct {
  445. half d;
  446. uint16_t qs[QK_K/8];
  447. } block_iq2_xxs;
  448. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  449. #define QR2_XS 8
  450. #define QI2_XS (QK_K / (4*QR2_XS))
  451. typedef struct {
  452. half d;
  453. uint16_t qs[QK_K/8];
  454. uint8_t scales[QK_K/32];
  455. } block_iq2_xs;
  456. 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");
  457. #define QR3_XXS 8
  458. #define QI3_XXS (QK_K / (4*QR3_XXS))
  459. typedef struct {
  460. half d;
  461. uint8_t qs[3*(QK_K/8)];
  462. } block_iq3_xxs;
  463. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_fp16_t) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  464. #define QR1_S 8
  465. #define QI1_S (QK_K / (4*QR1_S))
  466. typedef struct {
  467. half d;
  468. uint8_t qs[QK_K/8];
  469. uint8_t scales[QK_K/16];
  470. } block_iq1_s;
  471. static_assert(sizeof(block_iq1_s) == sizeof(ggml_fp16_t) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  472. #define WARP_SIZE 32
  473. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  474. #define CUDA_GELU_BLOCK_SIZE 256
  475. #define CUDA_SILU_BLOCK_SIZE 256
  476. #define CUDA_TANH_BLOCK_SIZE 256
  477. #define CUDA_RELU_BLOCK_SIZE 256
  478. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  479. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  480. #define CUDA_SQR_BLOCK_SIZE 256
  481. #define CUDA_CPY_BLOCK_SIZE 32
  482. #define CUDA_SCALE_BLOCK_SIZE 256
  483. #define CUDA_CLAMP_BLOCK_SIZE 256
  484. #define CUDA_ROPE_BLOCK_SIZE 256
  485. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  486. #define CUDA_ALIBI_BLOCK_SIZE 32
  487. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  488. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  489. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  490. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  491. #define CUDA_UPSCALE_BLOCK_SIZE 256
  492. #define CUDA_CONCAT_BLOCK_SIZE 256
  493. #define CUDA_PAD_BLOCK_SIZE 256
  494. #define CUDA_ACC_BLOCK_SIZE 256
  495. #define CUDA_IM2COL_BLOCK_SIZE 256
  496. #define CUDA_POOL2D_BLOCK_SIZE 256
  497. #define CUDA_Q8_0_NE_ALIGN 2048
  498. // dmmv = dequantize_mul_mat_vec
  499. #ifndef GGML_CUDA_DMMV_X
  500. #define GGML_CUDA_DMMV_X 32
  501. #endif
  502. #ifndef GGML_CUDA_MMV_Y
  503. #define GGML_CUDA_MMV_Y 1
  504. #endif
  505. #ifndef K_QUANTS_PER_ITERATION
  506. #define K_QUANTS_PER_ITERATION 2
  507. #else
  508. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  509. #endif
  510. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  511. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  512. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  513. #define MUL_MAT_SRC1_COL_STRIDE 128
  514. #define MAX_STREAMS 8
  515. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  516. struct ggml_tensor_extra_gpu {
  517. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  518. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  519. };
  520. // this is faster on Windows
  521. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  522. static void ggml_cuda_set_device(const int device) {
  523. int current_device;
  524. CUDA_CHECK(cudaGetDevice(&current_device));
  525. if (device == current_device) {
  526. return;
  527. }
  528. CUDA_CHECK(cudaSetDevice(device));
  529. }
  530. static int g_device_count = -1;
  531. static int g_main_device = 0;
  532. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  533. struct cuda_device_capabilities {
  534. int cc; // compute capability
  535. size_t smpb; // max. shared memory per block
  536. bool vmm; // virtual memory support
  537. size_t vmm_granularity; // granularity of virtual memory
  538. };
  539. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  540. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  541. [[noreturn]]
  542. static __device__ void no_device_code(
  543. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  544. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  545. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  546. file_name, line, function_name, arch);
  547. (void) arch_list;
  548. #else
  549. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  550. file_name, line, function_name, arch, arch_list);
  551. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  552. __trap();
  553. (void) no_device_code; // suppress unused function warning
  554. }
  555. #ifdef __CUDA_ARCH__
  556. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  557. #else
  558. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  559. #endif // __CUDA_ARCH__
  560. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  561. #pragma unroll
  562. for (int mask = 16; mask > 0; mask >>= 1) {
  563. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  564. }
  565. return x;
  566. }
  567. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  568. #pragma unroll
  569. for (int mask = 16; mask > 0; mask >>= 1) {
  570. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  571. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  572. }
  573. return a;
  574. }
  575. //static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  576. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  577. //#pragma unroll
  578. // for (int mask = 16; mask > 0; mask >>= 1) {
  579. // a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  580. // }
  581. // return a;
  582. //#else
  583. // (void) a;
  584. // NO_DEVICE_CODE;
  585. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  586. //}
  587. static __device__ __forceinline__ float warp_reduce_max(float x) {
  588. #pragma unroll
  589. for (int mask = 16; mask > 0; mask >>= 1) {
  590. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  591. }
  592. return x;
  593. }
  594. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  595. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  596. //#pragma unroll
  597. // for (int mask = 16; mask > 0; mask >>= 1) {
  598. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  599. // }
  600. // return x;
  601. //#else
  602. // (void) x;
  603. // NO_DEVICE_CODE;
  604. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  605. //}
  606. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  607. return b;
  608. GGML_UNUSED(a);
  609. }
  610. static __device__ __forceinline__ float op_add(const float a, const float b) {
  611. return a + b;
  612. }
  613. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  614. return a * b;
  615. }
  616. static __device__ __forceinline__ float op_div(const float a, const float b) {
  617. return a / b;
  618. }
  619. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  620. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  621. int ne0, int ne1, int ne2, int ne3,
  622. int ne10, int ne11, int ne12, int ne13,
  623. /*int s0, */ int s1, int s2, int s3,
  624. /*int s10,*/ int s11, int s12, int s13) {
  625. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  626. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  627. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  628. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  629. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  630. return;
  631. }
  632. const int i11 = i1 % ne11;
  633. const int i12 = i2 % ne12;
  634. const int i13 = i3 % ne13;
  635. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  636. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  637. const size_t i_dst = i_src0;
  638. const src0_t * src0_row = src0 + i_src0;
  639. const src1_t * src1_row = src1 + i_src1;
  640. dst_t * dst_row = dst + i_dst;
  641. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  642. const int i10 = i0 % ne10;
  643. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  644. }
  645. }
  646. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  647. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  648. int ne0, int ne1, int ne2, int ne3,
  649. int ne10, int ne11, int ne12, int ne13,
  650. /*int s0, */ int s1, int s2, int s3,
  651. /*int s10,*/ int s11, int s12, int s13) {
  652. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  653. const int i3 = i/(ne2*ne1*ne0);
  654. const int i2 = (i/(ne1*ne0)) % ne2;
  655. const int i1 = (i/ne0) % ne1;
  656. const int i0 = i % ne0;
  657. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  658. return;
  659. }
  660. const int i11 = i1 % ne11;
  661. const int i12 = i2 % ne12;
  662. const int i13 = i3 % ne13;
  663. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  664. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  665. const size_t i_dst = i_src0;
  666. const src0_t * src0_row = src0 + i_src0;
  667. const src1_t * src1_row = src1 + i_src1;
  668. dst_t * dst_row = dst + i_dst;
  669. const int i10 = i0 % ne10;
  670. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  671. }
  672. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  673. const int ne10, const int ne11, const int ne12,
  674. const int nb1, const int nb2, int offset) {
  675. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  676. if (i >= ne) {
  677. return;
  678. }
  679. int src1_idx = i - offset;
  680. int oz = src1_idx / nb2;
  681. int oy = (src1_idx - (oz * nb2)) / nb1;
  682. int ox = src1_idx % nb1;
  683. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  684. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  685. } else {
  686. dst[i] = x[i];
  687. }
  688. }
  689. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  690. const float GELU_COEF_A = 0.044715f;
  691. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  692. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  693. if (i >= k) {
  694. return;
  695. }
  696. float xi = x[i];
  697. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  698. }
  699. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  700. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  701. if (i >= k) {
  702. return;
  703. }
  704. dst[i] = x[i] / (1.0f + expf(-x[i]));
  705. }
  706. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  707. const float GELU_QUICK_COEF = -1.702f;
  708. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  709. if (i >= k) {
  710. return;
  711. }
  712. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  713. }
  714. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  715. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  716. if (i >= k) {
  717. return;
  718. }
  719. dst[i] = tanhf(x[i]);
  720. }
  721. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  722. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  723. if (i >= k) {
  724. return;
  725. }
  726. dst[i] = fmaxf(x[i], 0);
  727. }
  728. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  729. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  730. if (i >= k) {
  731. return;
  732. }
  733. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  734. }
  735. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  736. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  737. if (i >= k) {
  738. return;
  739. }
  740. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  741. }
  742. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  743. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  744. if (i >= k) {
  745. return;
  746. }
  747. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  748. }
  749. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  750. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  751. if (i >= k) {
  752. return;
  753. }
  754. dst[i] = x[i] * x[i];
  755. }
  756. template <int block_size>
  757. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  758. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  759. const int tid = threadIdx.x;
  760. float2 mean_var = make_float2(0.f, 0.f);
  761. for (int col = tid; col < ncols; col += block_size) {
  762. const float xi = x[row*ncols + col];
  763. mean_var.x += xi;
  764. mean_var.y += xi * xi;
  765. }
  766. // sum up partial sums
  767. mean_var = warp_reduce_sum(mean_var);
  768. if (block_size > WARP_SIZE) {
  769. __shared__ float2 s_sum[32];
  770. int warp_id = threadIdx.x / WARP_SIZE;
  771. int lane_id = threadIdx.x % WARP_SIZE;
  772. if (lane_id == 0) {
  773. s_sum[warp_id] = mean_var;
  774. }
  775. __syncthreads();
  776. mean_var = s_sum[lane_id];
  777. mean_var = warp_reduce_sum(mean_var);
  778. }
  779. const float mean = mean_var.x / ncols;
  780. const float var = mean_var.y / ncols - mean * mean;
  781. const float inv_std = rsqrtf(var + eps);
  782. for (int col = tid; col < ncols; col += block_size) {
  783. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  784. }
  785. }
  786. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  787. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  788. if (nidx >= ne0) {
  789. return;
  790. }
  791. // operation
  792. int offset_dst =
  793. nidx +
  794. blockIdx.y * ne0 +
  795. blockIdx.z * ne0 * gridDim.y;
  796. if (blockIdx.z < ne02) { // src0
  797. int offset_src =
  798. nidx +
  799. blockIdx.y * ne0 +
  800. blockIdx.z * ne0 * gridDim.y;
  801. dst[offset_dst] = x[offset_src];
  802. } else {
  803. int offset_src =
  804. nidx +
  805. blockIdx.y * ne0 +
  806. (blockIdx.z - ne02) * ne0 * gridDim.y;
  807. dst[offset_dst] = y[offset_src];
  808. }
  809. }
  810. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  811. int ne0 = ne00 * scale_factor;
  812. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  813. if (nidx >= ne0) {
  814. return;
  815. }
  816. // operation
  817. int i00 = nidx / scale_factor;
  818. int i01 = blockIdx.y / scale_factor;
  819. int offset_src =
  820. i00 +
  821. i01 * ne00 +
  822. blockIdx.z * nb02;
  823. int offset_dst =
  824. nidx +
  825. blockIdx.y * ne0 +
  826. blockIdx.z * ne0 * gridDim.y;
  827. dst[offset_dst] = x[offset_src];
  828. }
  829. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  830. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  831. if (nidx >= ne0) {
  832. return;
  833. }
  834. // operation
  835. int offset_dst =
  836. nidx +
  837. blockIdx.y * ne0 +
  838. blockIdx.z * ne0 * gridDim.y;
  839. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  840. int offset_src =
  841. nidx +
  842. blockIdx.y * ne00 +
  843. blockIdx.z * ne00 * ne01;
  844. dst[offset_dst] = x[offset_src];
  845. } else {
  846. dst[offset_dst] = 0.0f;
  847. }
  848. }
  849. template <int block_size>
  850. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  851. int start = blockIdx.x * group_size;
  852. int end = start + group_size;
  853. start += threadIdx.x;
  854. if (end >= ne_elements) {
  855. end = ne_elements;
  856. }
  857. float tmp = 0.0f; // partial sum for thread in warp
  858. for (int j = start; j < end; j += block_size) {
  859. tmp += x[j];
  860. }
  861. tmp = warp_reduce_sum(tmp);
  862. if (block_size > WARP_SIZE) {
  863. __shared__ float s_sum[32];
  864. int warp_id = threadIdx.x / WARP_SIZE;
  865. int lane_id = threadIdx.x % WARP_SIZE;
  866. if (lane_id == 0) {
  867. s_sum[warp_id] = tmp;
  868. }
  869. __syncthreads();
  870. tmp = s_sum[lane_id];
  871. tmp = warp_reduce_sum(tmp);
  872. }
  873. float mean = tmp / group_size;
  874. tmp = 0.0f;
  875. for (int j = start; j < end; j += block_size) {
  876. float xi = x[j] - mean;
  877. dst[j] = xi;
  878. tmp += xi * xi;
  879. }
  880. tmp = warp_reduce_sum(tmp);
  881. if (block_size > WARP_SIZE) {
  882. __shared__ float s_sum[32];
  883. int warp_id = threadIdx.x / WARP_SIZE;
  884. int lane_id = threadIdx.x % WARP_SIZE;
  885. if (lane_id == 0) {
  886. s_sum[warp_id] = tmp;
  887. }
  888. __syncthreads();
  889. tmp = s_sum[lane_id];
  890. tmp = warp_reduce_sum(tmp);
  891. }
  892. float variance = tmp / group_size;
  893. float scale = rsqrtf(variance + eps);
  894. for (int j = start; j < end; j += block_size) {
  895. dst[j] *= scale;
  896. }
  897. }
  898. template <int block_size>
  899. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  900. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  901. const int tid = threadIdx.x;
  902. float tmp = 0.0f; // partial sum for thread in warp
  903. for (int col = tid; col < ncols; col += block_size) {
  904. const float xi = x[row*ncols + col];
  905. tmp += xi * xi;
  906. }
  907. // sum up partial sums
  908. tmp = warp_reduce_sum(tmp);
  909. if (block_size > WARP_SIZE) {
  910. __shared__ float s_sum[32];
  911. int warp_id = threadIdx.x / WARP_SIZE;
  912. int lane_id = threadIdx.x % WARP_SIZE;
  913. if (lane_id == 0) {
  914. s_sum[warp_id] = tmp;
  915. }
  916. __syncthreads();
  917. tmp = s_sum[lane_id];
  918. tmp = warp_reduce_sum(tmp);
  919. }
  920. const float mean = tmp / ncols;
  921. const float scale = rsqrtf(mean + eps);
  922. for (int col = tid; col < ncols; col += block_size) {
  923. dst[row*ncols + col] = scale * x[row*ncols + col];
  924. }
  925. }
  926. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  927. const block_q4_0 * x = (const block_q4_0 *) vx;
  928. const dfloat d = x[ib].d;
  929. const int vui = x[ib].qs[iqs];
  930. v.x = vui & 0xF;
  931. v.y = vui >> 4;
  932. #ifdef GGML_CUDA_F16
  933. v = __hsub2(v, {8.0f, 8.0f});
  934. v = __hmul2(v, {d, d});
  935. #else
  936. v.x = (v.x - 8.0f) * d;
  937. v.y = (v.y - 8.0f) * d;
  938. #endif // GGML_CUDA_F16
  939. }
  940. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  941. const block_q4_1 * x = (const block_q4_1 *) vx;
  942. const dfloat d = __low2half(x[ib].dm);
  943. const dfloat m = __high2half(x[ib].dm);
  944. const int vui = x[ib].qs[iqs];
  945. v.x = vui & 0xF;
  946. v.y = vui >> 4;
  947. #ifdef GGML_CUDA_F16
  948. v = __hmul2(v, {d, d});
  949. v = __hadd2(v, {m, m});
  950. #else
  951. v.x = (v.x * d) + m;
  952. v.y = (v.y * d) + m;
  953. #endif // GGML_CUDA_F16
  954. }
  955. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  956. const block_q5_0 * x = (const block_q5_0 *) vx;
  957. const dfloat d = x[ib].d;
  958. uint32_t qh;
  959. memcpy(&qh, x[ib].qh, sizeof(qh));
  960. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  961. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  962. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  963. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  964. #ifdef GGML_CUDA_F16
  965. v = __hsub2(v, {16.0f, 16.0f});
  966. v = __hmul2(v, {d, d});
  967. #else
  968. v.x = (v.x - 16.0f) * d;
  969. v.y = (v.y - 16.0f) * d;
  970. #endif // GGML_CUDA_F16
  971. }
  972. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  973. const block_q5_1 * x = (const block_q5_1 *) vx;
  974. const dfloat d = __low2half(x[ib].dm);
  975. const dfloat m = __high2half(x[ib].dm);
  976. uint32_t qh;
  977. memcpy(&qh, x[ib].qh, sizeof(qh));
  978. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  979. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  980. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  981. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  982. #ifdef GGML_CUDA_F16
  983. v = __hmul2(v, {d, d});
  984. v = __hadd2(v, {m, m});
  985. #else
  986. v.x = (v.x * d) + m;
  987. v.y = (v.y * d) + m;
  988. #endif // GGML_CUDA_F16
  989. }
  990. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  991. const block_q8_0 * x = (const block_q8_0 *) vx;
  992. const dfloat d = x[ib].d;
  993. v.x = x[ib].qs[iqs + 0];
  994. v.y = x[ib].qs[iqs + 1];
  995. #ifdef GGML_CUDA_F16
  996. v = __hmul2(v, {d, d});
  997. #else
  998. v.x *= d;
  999. v.y *= d;
  1000. #endif // GGML_CUDA_F16
  1001. }
  1002. template<typename dst_t>
  1003. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1004. const int i = blockIdx.x;
  1005. // assume 32 threads
  1006. const int tid = threadIdx.x;
  1007. const int il = tid/8;
  1008. const int ir = tid%8;
  1009. const int ib = 8*i + ir;
  1010. if (ib >= nb32) {
  1011. return;
  1012. }
  1013. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1014. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1015. const float d = __half2float(x->d);
  1016. const float dm = -8*d;
  1017. const uint8_t * q = x->qs + 4*il;
  1018. for (int l = 0; l < 4; ++l) {
  1019. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1020. y[l+16] = d * (q[l] >> 4) + dm;
  1021. }
  1022. }
  1023. template<typename dst_t>
  1024. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1025. const int i = blockIdx.x;
  1026. // assume 32 threads
  1027. const int tid = threadIdx.x;
  1028. const int il = tid/8;
  1029. const int ir = tid%8;
  1030. const int ib = 8*i + ir;
  1031. if (ib >= nb32) {
  1032. return;
  1033. }
  1034. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1035. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1036. const float2 d = __half22float2(x->dm);
  1037. const uint8_t * q = x->qs + 4*il;
  1038. for (int l = 0; l < 4; ++l) {
  1039. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1040. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1041. }
  1042. }
  1043. //================================== k-quants
  1044. template<typename dst_t>
  1045. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1046. const int i = blockIdx.x;
  1047. const block_q2_K * x = (const block_q2_K *) vx;
  1048. const int tid = threadIdx.x;
  1049. #if QK_K == 256
  1050. const int n = tid/32;
  1051. const int l = tid - 32*n;
  1052. const int is = 8*n + l/16;
  1053. const uint8_t q = x[i].qs[32*n + l];
  1054. dst_t * y = yy + i*QK_K + 128*n;
  1055. float dall = __low2half(x[i].dm);
  1056. float dmin = __high2half(x[i].dm);
  1057. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1058. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1059. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1060. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1061. #else
  1062. const int is = tid/16; // 0 or 1
  1063. const int il = tid%16; // 0...15
  1064. const uint8_t q = x[i].qs[il] >> (2*is);
  1065. dst_t * y = yy + i*QK_K + 16*is + il;
  1066. float dall = __low2half(x[i].dm);
  1067. float dmin = __high2half(x[i].dm);
  1068. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1069. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1070. #endif
  1071. }
  1072. template<typename dst_t>
  1073. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1074. const int i = blockIdx.x;
  1075. const block_q3_K * x = (const block_q3_K *) vx;
  1076. #if QK_K == 256
  1077. const int r = threadIdx.x/4;
  1078. const int tid = r/2;
  1079. const int is0 = r%2;
  1080. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1081. const int n = tid / 4;
  1082. const int j = tid - 4*n;
  1083. uint8_t m = 1 << (4*n + j);
  1084. int is = 8*n + 2*j + is0;
  1085. int shift = 2*j;
  1086. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1087. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1088. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1089. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1090. float d_all = x[i].d;
  1091. float dl = d_all * (us - 32);
  1092. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1093. const uint8_t * q = x[i].qs + 32*n;
  1094. const uint8_t * hm = x[i].hmask;
  1095. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1096. #else
  1097. const int tid = threadIdx.x;
  1098. const int is = tid/16; // 0 or 1
  1099. const int il = tid%16; // 0...15
  1100. const int im = il/8; // 0...1
  1101. const int in = il%8; // 0...7
  1102. dst_t * y = yy + i*QK_K + 16*is + il;
  1103. const uint8_t q = x[i].qs[il] >> (2*is);
  1104. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1105. const float d = (float)x[i].d;
  1106. if (is == 0) {
  1107. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1108. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1109. } else {
  1110. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1111. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1112. }
  1113. #endif
  1114. }
  1115. #if QK_K == 256
  1116. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1117. if (j < 4) {
  1118. d = q[j] & 63; m = q[j + 4] & 63;
  1119. } else {
  1120. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1121. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1122. }
  1123. }
  1124. #endif
  1125. template<typename dst_t>
  1126. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1127. const block_q4_K * x = (const block_q4_K *) vx;
  1128. const int i = blockIdx.x;
  1129. #if QK_K == 256
  1130. // assume 32 threads
  1131. const int tid = threadIdx.x;
  1132. const int il = tid/8;
  1133. const int ir = tid%8;
  1134. const int is = 2*il;
  1135. const int n = 4;
  1136. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1137. const float dall = __low2half(x[i].dm);
  1138. const float dmin = __high2half(x[i].dm);
  1139. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1140. uint8_t sc, m;
  1141. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1142. const float d1 = dall * sc; const float m1 = dmin * m;
  1143. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1144. const float d2 = dall * sc; const float m2 = dmin * m;
  1145. for (int l = 0; l < n; ++l) {
  1146. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1147. y[l +32] = d2 * (q[l] >> 4) - m2;
  1148. }
  1149. #else
  1150. const int tid = threadIdx.x;
  1151. const uint8_t * q = x[i].qs;
  1152. dst_t * y = yy + i*QK_K;
  1153. const float d = (float)x[i].dm[0];
  1154. const float m = (float)x[i].dm[1];
  1155. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1156. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1157. #endif
  1158. }
  1159. template<typename dst_t>
  1160. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1161. const block_q5_K * x = (const block_q5_K *) vx;
  1162. const int i = blockIdx.x;
  1163. #if QK_K == 256
  1164. // assume 64 threads - this is very slightly better than the one below
  1165. const int tid = threadIdx.x;
  1166. const int il = tid/16; // il is in 0...3
  1167. const int ir = tid%16; // ir is in 0...15
  1168. const int is = 2*il; // is is in 0...6
  1169. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1170. const float dall = __low2half(x[i].dm);
  1171. const float dmin = __high2half(x[i].dm);
  1172. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1173. const uint8_t * qh = x[i].qh + 2*ir;
  1174. uint8_t sc, m;
  1175. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1176. const float d1 = dall * sc; const float m1 = dmin * m;
  1177. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1178. const float d2 = dall * sc; const float m2 = dmin * m;
  1179. uint8_t hm = 1 << (2*il);
  1180. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1181. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1182. hm <<= 1;
  1183. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1184. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1185. #else
  1186. const int tid = threadIdx.x;
  1187. const uint8_t q = x[i].qs[tid];
  1188. const int im = tid/8; // 0...3
  1189. const int in = tid%8; // 0...7
  1190. const int is = tid/16; // 0 or 1
  1191. const uint8_t h = x[i].qh[in] >> im;
  1192. const float d = x[i].d;
  1193. dst_t * y = yy + i*QK_K + tid;
  1194. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1195. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1196. #endif
  1197. }
  1198. template<typename dst_t>
  1199. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1200. const block_q6_K * x = (const block_q6_K *) vx;
  1201. const int i = blockIdx.x;
  1202. #if QK_K == 256
  1203. // assume 64 threads - this is very slightly better than the one below
  1204. const int tid = threadIdx.x;
  1205. const int ip = tid/32; // ip is 0 or 1
  1206. const int il = tid - 32*ip; // 0...32
  1207. const int is = 8*ip + il/16;
  1208. dst_t * y = yy + i*QK_K + 128*ip + il;
  1209. const float d = x[i].d;
  1210. const uint8_t * ql = x[i].ql + 64*ip + il;
  1211. const uint8_t qh = x[i].qh[32*ip + il];
  1212. const int8_t * sc = x[i].scales + is;
  1213. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1214. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1215. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1216. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1217. #else
  1218. // assume 32 threads
  1219. const int tid = threadIdx.x;
  1220. const int ip = tid/16; // 0 or 1
  1221. const int il = tid - 16*ip; // 0...15
  1222. dst_t * y = yy + i*QK_K + 16*ip + il;
  1223. const float d = x[i].d;
  1224. const uint8_t ql = x[i].ql[16*ip + il];
  1225. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1226. const int8_t * sc = x[i].scales;
  1227. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1228. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1229. #endif
  1230. }
  1231. static const __device__ uint64_t iq2xxs_grid[256] = {
  1232. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1233. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1234. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1235. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1236. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1237. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1238. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1239. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1240. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1241. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1242. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1243. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1244. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1245. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1246. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1247. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1248. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1249. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1250. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1251. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1252. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1253. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1254. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1255. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1256. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1257. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1258. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1259. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1260. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1261. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1262. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1263. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1264. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1265. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1266. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1267. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1268. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1269. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1270. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1271. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1272. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1273. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1274. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1275. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1276. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1277. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1278. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1279. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1280. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1281. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1282. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1283. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1284. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1285. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1286. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1287. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1288. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1289. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1290. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1291. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1292. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1293. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1294. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1295. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1296. };
  1297. static const __device__ uint64_t iq2xs_grid[512] = {
  1298. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1299. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1300. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1301. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1302. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1303. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  1304. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  1305. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  1306. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  1307. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  1308. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  1309. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  1310. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  1311. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  1312. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  1313. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  1314. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  1315. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  1316. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  1317. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  1318. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  1319. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  1320. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  1321. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  1322. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  1323. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  1324. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  1325. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  1326. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  1327. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  1328. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  1329. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  1330. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  1331. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  1332. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  1333. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  1334. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  1335. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  1336. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  1337. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  1338. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  1339. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  1340. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  1341. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  1342. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  1343. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  1344. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  1345. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  1346. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  1347. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  1348. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  1349. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  1350. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  1351. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  1352. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  1353. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  1354. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  1355. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  1356. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  1357. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  1358. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  1359. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  1360. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  1361. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  1362. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  1363. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  1364. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  1365. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  1366. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  1367. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  1368. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  1369. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  1370. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  1371. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  1372. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  1373. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  1374. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  1375. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  1376. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  1377. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  1378. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  1379. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  1380. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  1381. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  1382. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  1383. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  1384. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  1385. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  1386. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  1387. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  1388. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  1389. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  1390. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  1391. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  1392. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  1393. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  1394. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  1395. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  1396. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  1397. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  1398. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  1399. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  1400. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  1401. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  1402. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  1403. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  1404. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  1405. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  1406. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  1407. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  1408. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  1409. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  1410. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  1411. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  1412. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  1413. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  1414. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  1415. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  1416. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  1417. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  1418. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  1419. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  1420. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  1421. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  1422. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  1423. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  1424. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  1425. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  1426. };
  1427. static const __device__ uint32_t iq3xxs_grid[256] = {
  1428. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  1429. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  1430. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  1431. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  1432. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  1433. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  1434. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  1435. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  1436. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  1437. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  1438. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  1439. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  1440. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  1441. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  1442. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  1443. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  1444. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  1445. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  1446. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  1447. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  1448. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  1449. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  1450. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  1451. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  1452. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  1453. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  1454. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  1455. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  1456. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  1457. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  1458. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  1459. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  1460. };
  1461. static const __device__ uint64_t iq1s_grid[512] = {
  1462. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  1463. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  1464. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  1465. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  1466. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  1467. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  1468. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  1469. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  1470. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  1471. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  1472. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  1473. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  1474. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  1475. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  1476. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  1477. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  1478. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  1479. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  1480. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  1481. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  1482. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  1483. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  1484. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  1485. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  1486. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  1487. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  1488. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  1489. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  1490. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  1491. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  1492. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  1493. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  1494. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  1495. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  1496. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  1497. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  1498. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  1499. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  1500. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  1501. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  1502. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  1503. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  1504. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  1505. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  1506. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  1507. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  1508. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  1509. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  1510. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  1511. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  1512. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  1513. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  1514. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  1515. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  1516. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1517. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  1518. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  1519. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  1520. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1521. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  1522. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  1523. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  1524. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  1525. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1526. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  1527. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  1528. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  1529. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  1530. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  1531. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  1532. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  1533. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  1534. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  1535. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  1536. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  1537. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  1538. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  1539. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  1540. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  1541. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  1542. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  1543. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  1544. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  1545. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  1546. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  1547. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  1548. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  1549. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  1550. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  1551. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  1552. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  1553. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  1554. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  1555. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  1556. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  1557. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  1558. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  1559. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  1560. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  1561. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  1562. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  1563. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  1564. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  1565. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  1566. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  1567. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  1568. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  1569. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  1570. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  1571. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  1572. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  1573. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  1574. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  1575. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  1576. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  1577. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  1578. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  1579. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  1580. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  1581. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  1582. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  1583. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  1584. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  1585. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  1586. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  1587. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  1588. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  1589. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  1590. };
  1591. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1592. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1593. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1594. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1595. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1596. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1597. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1598. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1599. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1600. };
  1601. //#if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1602. static const __device__ uint64_t ksigns64[128] = {
  1603. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  1604. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  1605. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  1606. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  1607. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  1608. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  1609. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  1610. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  1611. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  1612. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  1613. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  1614. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  1615. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  1616. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  1617. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  1618. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  1619. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  1620. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  1621. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  1622. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  1623. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  1624. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  1625. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  1626. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  1627. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  1628. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  1629. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  1630. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  1631. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  1632. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  1633. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  1634. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  1635. };
  1636. //#endif
  1637. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  1638. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1639. switch (type) {
  1640. case GGML_TYPE_Q4_0:
  1641. case GGML_TYPE_Q4_1:
  1642. case GGML_TYPE_Q5_0:
  1643. case GGML_TYPE_Q5_1:
  1644. case GGML_TYPE_Q8_0:
  1645. case GGML_TYPE_Q2_K:
  1646. case GGML_TYPE_Q3_K:
  1647. case GGML_TYPE_Q4_K:
  1648. case GGML_TYPE_Q5_K:
  1649. case GGML_TYPE_Q6_K:
  1650. return true;
  1651. default:
  1652. return false;
  1653. }
  1654. }
  1655. template<typename dst_t>
  1656. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1657. const int i = blockIdx.x;
  1658. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1659. const int tid = threadIdx.x;
  1660. #if QK_K == 256
  1661. const int il = tid/8; // 0...3
  1662. const int ib = tid%8; // 0...7
  1663. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1664. const uint16_t * q2 = x[i].qs + 4*ib;
  1665. const uint8_t * aux8 = (const uint8_t *)q2;
  1666. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  1667. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1668. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1669. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1670. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1671. #else
  1672. assert(false);
  1673. #endif
  1674. }
  1675. template<typename dst_t>
  1676. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1677. const int i = blockIdx.x;
  1678. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  1679. const int tid = threadIdx.x;
  1680. #if QK_K == 256
  1681. const int il = tid/8; // 0...3
  1682. const int ib = tid%8; // 0...7
  1683. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1684. const uint16_t * q2 = x[i].qs + 4*ib;
  1685. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  1686. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1687. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  1688. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1689. #else
  1690. assert(false);
  1691. #endif
  1692. }
  1693. template<typename dst_t>
  1694. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1695. const int i = blockIdx.x;
  1696. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  1697. const int tid = threadIdx.x;
  1698. #if QK_K == 256
  1699. const int il = tid/8; // 0...3
  1700. const int ib = tid%8; // 0...7
  1701. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1702. const uint8_t * q3 = x[i].qs + 8*ib;
  1703. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  1704. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  1705. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  1706. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  1707. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  1708. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1709. for (int j = 0; j < 4; ++j) {
  1710. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1711. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1712. }
  1713. #else
  1714. assert(false);
  1715. #endif
  1716. }
  1717. template<typename dst_t>
  1718. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1719. const int i = blockIdx.x;
  1720. const block_iq1_s * x = (const block_iq1_s *) vx;
  1721. const int tid = threadIdx.x;
  1722. #if QK_K == 256
  1723. const int il = tid/8; // 0...3
  1724. const int ib = tid%8; // 0...7
  1725. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1726. const int i8 = 4*ib+il;
  1727. uint8_t h = x[i].scales[i8/2] >> 4*(i8%2);
  1728. const int8_t * grid = (const int8_t *)(iq1s_grid + (x[i].qs[i8] | ((h & 8) << 5)));
  1729. const float d = (float)x[i].d * (2*(h & 7) + 1);
  1730. for (int j = 0; j < 8; ++j) y[j] = d * grid[j];
  1731. #else
  1732. assert(false);
  1733. #endif
  1734. }
  1735. 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) {
  1736. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1737. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1738. if (row > nrows) return;
  1739. const int num_blocks_per_row = ncols / QK_K;
  1740. const int ib0 = row*num_blocks_per_row;
  1741. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1742. float tmp = 0; // partial sum for thread in warp
  1743. #if QK_K == 256
  1744. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1745. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1746. const int step = 16/K_QUANTS_PER_ITERATION;
  1747. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1748. const int in = tid - step*im; // 0...15 or 0...7
  1749. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1750. const int q_offset = 32*im + l0;
  1751. const int s_offset = 8*im;
  1752. const int y_offset = 128*im + l0;
  1753. uint32_t aux[4];
  1754. const uint8_t * d = (const uint8_t *)aux;
  1755. const uint8_t * m = (const uint8_t *)(aux + 2);
  1756. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1757. const float * y = yy + i * QK_K + y_offset;
  1758. const uint8_t * q = x[i].qs + q_offset;
  1759. const float dall = __low2half(x[i].dm);
  1760. const float dmin = __high2half(x[i].dm);
  1761. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1762. aux[0] = a[0] & 0x0f0f0f0f;
  1763. aux[1] = a[1] & 0x0f0f0f0f;
  1764. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1765. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1766. float sum1 = 0, sum2 = 0;
  1767. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1768. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1769. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1770. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1771. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1772. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1773. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1774. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1775. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1776. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1777. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1778. }
  1779. tmp += dall * sum1 - dmin * sum2;
  1780. }
  1781. #else
  1782. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1783. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1784. const int offset = tid * K_QUANTS_PER_ITERATION;
  1785. uint32_t uaux[2];
  1786. const uint8_t * d = (const uint8_t *)uaux;
  1787. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1788. const float * y = yy + i * QK_K + offset;
  1789. const uint8_t * q = x[i].qs + offset;
  1790. const uint32_t * s = (const uint32_t *)x[i].scales;
  1791. uaux[0] = s[0] & 0x0f0f0f0f;
  1792. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1793. const float2 dall = __half22float2(x[i].dm);
  1794. float sum1 = 0, sum2 = 0;
  1795. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1796. const uint8_t ql = q[l];
  1797. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1798. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1799. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1800. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1801. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1802. }
  1803. tmp += dall.x * sum1 - dall.y * sum2;
  1804. }
  1805. #endif
  1806. // sum up partial sums and write back result
  1807. #pragma unroll
  1808. for (int mask = 16; mask > 0; mask >>= 1) {
  1809. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1810. }
  1811. if (threadIdx.x == 0) {
  1812. dst[row] = tmp;
  1813. }
  1814. }
  1815. 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) {
  1816. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1817. if (row > nrows) return;
  1818. const int num_blocks_per_row = ncols / QK_K;
  1819. const int ib0 = row*num_blocks_per_row;
  1820. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1821. float tmp = 0; // partial sum for thread in warp
  1822. #if QK_K == 256
  1823. const uint16_t kmask1 = 0x0303;
  1824. const uint16_t kmask2 = 0x0f0f;
  1825. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1826. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1827. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1828. const int step = 16/K_QUANTS_PER_ITERATION;
  1829. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1830. const int in = tid - step*im; // 0....15 or 0...7
  1831. const uint8_t m = 1 << (4*im);
  1832. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1833. const int q_offset = 32*im + l0;
  1834. const int y_offset = 128*im + l0;
  1835. uint16_t utmp[4];
  1836. const int8_t * s = (const int8_t *)utmp;
  1837. const uint16_t s_shift = 4*im;
  1838. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1839. const float * y = yy + i * QK_K + y_offset;
  1840. const uint8_t * q = x[i].qs + q_offset;
  1841. const uint8_t * h = x[i].hmask + l0;
  1842. const uint16_t * a = (const uint16_t *)x[i].scales;
  1843. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1844. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1845. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1846. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1847. const float d = x[i].d;
  1848. float sum = 0;
  1849. for (int l = 0; l < n; ++l) {
  1850. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1851. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1852. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1853. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1854. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1855. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1856. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1857. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1858. }
  1859. tmp += d * sum;
  1860. }
  1861. #else
  1862. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1863. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1864. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1865. const int in = offset/8; // 0 or 1
  1866. const int im = offset%8; // 0...7
  1867. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1868. const float * y = yy + i * QK_K + offset;
  1869. const uint8_t * q = x[i].qs + offset;
  1870. const uint8_t * s = x[i].scales;
  1871. const float dall = (float)x[i].d;
  1872. float sum = 0;
  1873. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1874. const uint8_t hl = x[i].hmask[im+l] >> in;
  1875. const uint8_t ql = q[l];
  1876. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1877. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1878. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1879. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1880. }
  1881. tmp += sum;
  1882. }
  1883. #endif
  1884. // sum up partial sums and write back result
  1885. #pragma unroll
  1886. for (int mask = 16; mask > 0; mask >>= 1) {
  1887. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1888. }
  1889. if (threadIdx.x == 0) {
  1890. dst[row] = tmp;
  1891. }
  1892. }
  1893. 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) {
  1894. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1895. if (row > nrows) return;
  1896. const int num_blocks_per_row = ncols / QK_K;
  1897. const int ib0 = row*num_blocks_per_row;
  1898. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1899. #if QK_K == 256
  1900. const uint16_t kmask1 = 0x3f3f;
  1901. const uint16_t kmask2 = 0x0f0f;
  1902. const uint16_t kmask3 = 0xc0c0;
  1903. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1904. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1905. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1906. const int il = tid/step; // 0...3
  1907. const int ir = tid - step*il; // 0...7 or 0...3
  1908. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1909. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1910. const int in = il%2;
  1911. const int l0 = n*(2*ir + in);
  1912. const int q_offset = 32*im + l0;
  1913. const int y_offset = 64*im + l0;
  1914. uint16_t aux[4];
  1915. const uint8_t * sc = (const uint8_t *)aux;
  1916. #if K_QUANTS_PER_ITERATION == 2
  1917. uint32_t q32[4];
  1918. const uint8_t * q4 = (const uint8_t *)q32;
  1919. #else
  1920. uint16_t q16[4];
  1921. const uint8_t * q4 = (const uint8_t *)q16;
  1922. #endif
  1923. float tmp = 0; // partial sum for thread in warp
  1924. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1925. const float * y1 = yy + i*QK_K + y_offset;
  1926. const float * y2 = y1 + 128;
  1927. const float dall = __low2half(x[i].dm);
  1928. const float dmin = __high2half(x[i].dm);
  1929. const uint16_t * a = (const uint16_t *)x[i].scales;
  1930. aux[0] = a[im+0] & kmask1;
  1931. aux[1] = a[im+2] & kmask1;
  1932. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1933. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1934. #if K_QUANTS_PER_ITERATION == 2
  1935. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1936. const uint32_t * q2 = q1 + 16;
  1937. q32[0] = q1[0] & 0x0f0f0f0f;
  1938. q32[1] = q1[0] & 0xf0f0f0f0;
  1939. q32[2] = q2[0] & 0x0f0f0f0f;
  1940. q32[3] = q2[0] & 0xf0f0f0f0;
  1941. float4 s = {0.f, 0.f, 0.f, 0.f};
  1942. float smin = 0;
  1943. for (int l = 0; l < 4; ++l) {
  1944. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1945. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1946. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1947. }
  1948. 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;
  1949. #else
  1950. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1951. const uint16_t * q2 = q1 + 32;
  1952. q16[0] = q1[0] & 0x0f0f;
  1953. q16[1] = q1[0] & 0xf0f0;
  1954. q16[2] = q2[0] & 0x0f0f;
  1955. q16[3] = q2[0] & 0xf0f0;
  1956. float4 s = {0.f, 0.f, 0.f, 0.f};
  1957. float smin = 0;
  1958. for (int l = 0; l < 2; ++l) {
  1959. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1960. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1961. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1962. }
  1963. 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;
  1964. #endif
  1965. }
  1966. #else
  1967. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1968. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1969. const int step = tid * K_QUANTS_PER_ITERATION;
  1970. uint16_t aux16[2];
  1971. const uint8_t * s = (const uint8_t *)aux16;
  1972. float tmp = 0;
  1973. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1974. const uint8_t * q = x[i].qs + step;
  1975. const float * y = yy + i*QK_K + step;
  1976. const uint16_t * a = (const uint16_t *)x[i].scales;
  1977. aux16[0] = a[0] & 0x0f0f;
  1978. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1979. const float d = (float)x[i].dm[0];
  1980. const float m = (float)x[i].dm[1];
  1981. float sum = 0.f;
  1982. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1983. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1984. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1985. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1986. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1987. }
  1988. tmp += sum;
  1989. }
  1990. #endif
  1991. // sum up partial sums and write back result
  1992. #pragma unroll
  1993. for (int mask = 16; mask > 0; mask >>= 1) {
  1994. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1995. }
  1996. if (tid == 0) {
  1997. dst[row] = tmp;
  1998. }
  1999. }
  2000. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  2001. const int row = blockIdx.x;
  2002. const int num_blocks_per_row = ncols / QK_K;
  2003. const int ib0 = row*num_blocks_per_row;
  2004. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  2005. float tmp = 0; // partial sum for thread in warp
  2006. #if QK_K == 256
  2007. const uint16_t kmask1 = 0x3f3f;
  2008. const uint16_t kmask2 = 0x0f0f;
  2009. const uint16_t kmask3 = 0xc0c0;
  2010. const int tid = threadIdx.x/2; // 0...15
  2011. const int ix = threadIdx.x%2;
  2012. const int il = tid/4; // 0...3
  2013. const int ir = tid - 4*il;// 0...3
  2014. const int n = 2;
  2015. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2016. const int in = il%2;
  2017. const int l0 = n*(2*ir + in);
  2018. const int q_offset = 32*im + l0;
  2019. const int y_offset = 64*im + l0;
  2020. const uint8_t hm1 = 1 << (2*im);
  2021. const uint8_t hm2 = hm1 << 4;
  2022. uint16_t aux[4];
  2023. const uint8_t * sc = (const uint8_t *)aux;
  2024. uint16_t q16[8];
  2025. const uint8_t * q4 = (const uint8_t *)q16;
  2026. for (int i = ix; i < num_blocks_per_row; i += 2) {
  2027. const uint8_t * ql1 = x[i].qs + q_offset;
  2028. const uint8_t * qh = x[i].qh + l0;
  2029. const float * y1 = yy + i*QK_K + y_offset;
  2030. const float * y2 = y1 + 128;
  2031. const float dall = __low2half(x[i].dm);
  2032. const float dmin = __high2half(x[i].dm);
  2033. const uint16_t * a = (const uint16_t *)x[i].scales;
  2034. aux[0] = a[im+0] & kmask1;
  2035. aux[1] = a[im+2] & kmask1;
  2036. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2037. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2038. float4 sum = {0.f, 0.f, 0.f, 0.f};
  2039. float smin = 0;
  2040. const uint16_t * q1 = (const uint16_t *)ql1;
  2041. const uint16_t * q2 = q1 + 32;
  2042. q16[0] = q1[0] & 0x0f0f;
  2043. q16[1] = q1[8] & 0x0f0f;
  2044. q16[2] = (q1[0] >> 4) & 0x0f0f;
  2045. q16[3] = (q1[8] >> 4) & 0x0f0f;
  2046. q16[4] = q2[0] & 0x0f0f;
  2047. q16[5] = q2[8] & 0x0f0f;
  2048. q16[6] = (q2[0] >> 4) & 0x0f0f;
  2049. q16[7] = (q2[8] >> 4) & 0x0f0f;
  2050. for (int l = 0; l < n; ++l) {
  2051. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  2052. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  2053. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  2054. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  2055. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  2056. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  2057. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  2058. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  2059. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  2060. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  2061. }
  2062. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  2063. }
  2064. #else
  2065. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2066. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2067. const int step = tid * K_QUANTS_PER_ITERATION;
  2068. const int im = step/8;
  2069. const int in = step%8;
  2070. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2071. const uint8_t * q = x[i].qs + step;
  2072. const int8_t * s = x[i].scales;
  2073. const float * y = yy + i*QK_K + step;
  2074. const float d = x[i].d;
  2075. float sum = 0.f;
  2076. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2077. const uint8_t h = x[i].qh[in+j] >> im;
  2078. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  2079. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  2080. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  2081. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  2082. }
  2083. tmp += sum;
  2084. }
  2085. #endif
  2086. // sum up partial sums and write back result
  2087. #pragma unroll
  2088. for (int mask = 16; mask > 0; mask >>= 1) {
  2089. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2090. }
  2091. if (threadIdx.x == 0) {
  2092. dst[row] = tmp;
  2093. }
  2094. }
  2095. 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) {
  2096. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2097. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2098. if (row > nrows) return;
  2099. const int num_blocks_per_row = ncols / QK_K;
  2100. const int ib0 = row*num_blocks_per_row;
  2101. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  2102. #if QK_K == 256
  2103. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2104. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  2105. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  2106. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2107. const int in = tid - step*im; // 0...15 or 0...7
  2108. #if K_QUANTS_PER_ITERATION == 1
  2109. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  2110. const int is = 0;
  2111. #else
  2112. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  2113. const int is = in / 4;
  2114. #endif
  2115. const int ql_offset = 64*im + l0;
  2116. const int qh_offset = 32*im + l0;
  2117. const int s_offset = 8*im + is;
  2118. const int y_offset = 128*im + l0;
  2119. float tmp = 0; // partial sum for thread in warp
  2120. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2121. const float * y = yy + i * QK_K + y_offset;
  2122. const uint8_t * ql = x[i].ql + ql_offset;
  2123. const uint8_t * qh = x[i].qh + qh_offset;
  2124. const int8_t * s = x[i].scales + s_offset;
  2125. const float d = x[i].d;
  2126. #if K_QUANTS_PER_ITERATION == 1
  2127. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  2128. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  2129. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  2130. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  2131. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  2132. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  2133. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  2134. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  2135. tmp += sum;
  2136. #else
  2137. float sum = 0;
  2138. for (int l = 0; l < 4; ++l) {
  2139. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  2140. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  2141. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  2142. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  2143. }
  2144. tmp += sum;
  2145. #endif
  2146. }
  2147. #else
  2148. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  2149. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  2150. const int step = tid * K_QUANTS_PER_ITERATION;
  2151. float tmp = 0; // partial sum for thread in warp
  2152. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2153. const float * y = yy + i * QK_K + step;
  2154. const uint8_t * ql = x[i].ql + step;
  2155. const uint8_t * qh = x[i].qh + step;
  2156. const int8_t * s = x[i].scales;
  2157. const float d = x[i+0].d;
  2158. float sum = 0;
  2159. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2160. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  2161. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  2162. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  2163. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  2164. }
  2165. tmp += sum;
  2166. }
  2167. #endif
  2168. // sum up partial sums and write back result
  2169. #pragma unroll
  2170. for (int mask = 16; mask > 0; mask >>= 1) {
  2171. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2172. }
  2173. if (tid == 0) {
  2174. dst[row] = tmp;
  2175. }
  2176. }
  2177. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  2178. const half * x = (const half *) vx;
  2179. // automatic half -> float type cast if dfloat == float
  2180. v.x = x[ib + iqs + 0];
  2181. v.y = x[ib + iqs + 1];
  2182. }
  2183. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  2184. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  2185. if (ix >= kx_padded) {
  2186. return;
  2187. }
  2188. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  2189. const int i_padded = iy*kx_padded + ix;
  2190. block_q8_1 * y = (block_q8_1 *) vy;
  2191. const int ib = i_padded / QK8_1; // block index
  2192. const int iqs = i_padded % QK8_1; // quant index
  2193. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  2194. float amax = fabsf(xi);
  2195. float sum = xi;
  2196. #pragma unroll
  2197. for (int mask = 16; mask > 0; mask >>= 1) {
  2198. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  2199. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  2200. }
  2201. const float d = amax / 127;
  2202. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  2203. y[ib].qs[iqs] = q;
  2204. if (iqs > 0) {
  2205. return;
  2206. }
  2207. reinterpret_cast<half&>(y[ib].ds.x) = d;
  2208. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  2209. }
  2210. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2211. static __global__ void k_get_rows(
  2212. const void * src0, const int32_t * src1, dst_t * dst,
  2213. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2214. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2215. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2216. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2217. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2218. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  2219. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2220. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2221. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2222. if (i00 >= ne00) {
  2223. return;
  2224. }
  2225. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2226. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2227. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  2228. const int ib = i00/qk; // block index
  2229. const int iqs = (i00%qk)/qr; // quant index
  2230. const int iybs = i00 - i00%qk; // dst block start index
  2231. const int y_offset = qr == 1 ? 1 : qk/2;
  2232. // dequantize
  2233. dfloat2 v;
  2234. dequantize_kernel(src0_row, ib, iqs, v);
  2235. dst_row[iybs + iqs + 0] = v.x;
  2236. dst_row[iybs + iqs + y_offset] = v.y;
  2237. }
  2238. template<typename src0_t, typename dst_t>
  2239. static __global__ void k_get_rows_float(
  2240. const src0_t * src0, const int32_t * src1, dst_t * dst,
  2241. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2242. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2243. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2244. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2245. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2246. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2247. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2248. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2249. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2250. if (i00 >= ne00) {
  2251. return;
  2252. }
  2253. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2254. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2255. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2256. dst_row[i00] = src0_row[i00];
  2257. }
  2258. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2259. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2260. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2261. if (i >= k) {
  2262. return;
  2263. }
  2264. const int ib = i/qk; // block index
  2265. const int iqs = (i%qk)/qr; // quant index
  2266. const int iybs = i - i%qk; // y block start index
  2267. const int y_offset = qr == 1 ? 1 : qk/2;
  2268. // dequantize
  2269. dfloat2 v;
  2270. dequantize_kernel(vx, ib, iqs, v);
  2271. y[iybs + iqs + 0] = v.x;
  2272. y[iybs + iqs + y_offset] = v.y;
  2273. }
  2274. template <typename src_t, typename dst_t>
  2275. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2276. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2277. if (i >= k) {
  2278. return;
  2279. }
  2280. const src_t * x = (src_t *) vx;
  2281. y[i] = x[i];
  2282. }
  2283. template <bool need_check>
  2284. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2285. #if __CUDA_ARCH__ >= CC_PASCAL
  2286. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2287. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2288. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2289. half2 * y2 = (half2 *) (y + i0);
  2290. __shared__ int vals[nint];
  2291. #pragma unroll
  2292. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2293. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2294. break;
  2295. }
  2296. const int ix = ix0 + threadIdx.x;
  2297. vals[ix] = x0[ix];
  2298. }
  2299. #pragma unroll
  2300. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2301. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2302. return;
  2303. }
  2304. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2305. const half d = *b0;
  2306. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2307. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2308. }
  2309. #else
  2310. (void) vx; (void) y; (void) k;
  2311. NO_DEVICE_CODE;
  2312. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2313. }
  2314. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2315. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2316. #define VDR_Q4_0_Q8_1_MMVQ 2
  2317. #define VDR_Q4_0_Q8_1_MMQ 4
  2318. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2319. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2320. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2321. int sumi = 0;
  2322. #pragma unroll
  2323. for (int i = 0; i < vdr; ++i) {
  2324. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2325. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2326. // SIMD dot product of quantized values
  2327. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2328. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2329. }
  2330. const float2 ds8f = __half22float2(ds8);
  2331. // second part effectively subtracts 8 from each quant value
  2332. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2333. #else
  2334. NO_DEVICE_CODE;
  2335. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2336. }
  2337. #define VDR_Q4_1_Q8_1_MMVQ 2
  2338. #define VDR_Q4_1_Q8_1_MMQ 4
  2339. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2340. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2341. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2342. int sumi = 0;
  2343. #pragma unroll
  2344. for (int i = 0; i < vdr; ++i) {
  2345. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2346. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2347. // SIMD dot product of quantized values
  2348. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2349. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2350. }
  2351. #ifdef GGML_CUDA_F16
  2352. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2353. const float d4d8 = tmp.x;
  2354. const float m4s8 = tmp.y;
  2355. #else
  2356. const float2 dm4f = __half22float2(dm4);
  2357. const float2 ds8f = __half22float2(ds8);
  2358. const float d4d8 = dm4f.x * ds8f.x;
  2359. const float m4s8 = dm4f.y * ds8f.y;
  2360. #endif // GGML_CUDA_F16
  2361. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2362. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2363. #else
  2364. NO_DEVICE_CODE;
  2365. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2366. }
  2367. #define VDR_Q5_0_Q8_1_MMVQ 2
  2368. #define VDR_Q5_0_Q8_1_MMQ 4
  2369. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2370. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2371. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2372. int sumi = 0;
  2373. #pragma unroll
  2374. for (int i = 0; i < vdr; ++i) {
  2375. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2376. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2377. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2378. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2379. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2380. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2381. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2382. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2383. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2384. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2385. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2386. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2387. }
  2388. const float2 ds8f = __half22float2(ds8);
  2389. // second part effectively subtracts 16 from each quant value
  2390. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2391. #else
  2392. NO_DEVICE_CODE;
  2393. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2394. }
  2395. #define VDR_Q5_1_Q8_1_MMVQ 2
  2396. #define VDR_Q5_1_Q8_1_MMQ 4
  2397. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2398. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2399. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2400. int sumi = 0;
  2401. #pragma unroll
  2402. for (int i = 0; i < vdr; ++i) {
  2403. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2404. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2405. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2406. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2407. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2408. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2409. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2410. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2411. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2412. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2413. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2414. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2415. }
  2416. #ifdef GGML_CUDA_F16
  2417. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2418. const float d5d8 = tmp.x;
  2419. const float m5s8 = tmp.y;
  2420. #else
  2421. const float2 dm5f = __half22float2(dm5);
  2422. const float2 ds8f = __half22float2(ds8);
  2423. const float d5d8 = dm5f.x * ds8f.x;
  2424. const float m5s8 = dm5f.y * ds8f.y;
  2425. #endif // GGML_CUDA_F16
  2426. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2427. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2428. #else
  2429. NO_DEVICE_CODE;
  2430. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2431. }
  2432. #define VDR_Q8_0_Q8_1_MMVQ 2
  2433. #define VDR_Q8_0_Q8_1_MMQ 8
  2434. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2435. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2436. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2437. int sumi = 0;
  2438. #pragma unroll
  2439. for (int i = 0; i < vdr; ++i) {
  2440. // SIMD dot product of quantized values
  2441. sumi = __dp4a(v[i], u[i], sumi);
  2442. }
  2443. return d8_0*d8_1 * sumi;
  2444. #else
  2445. NO_DEVICE_CODE;
  2446. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2447. }
  2448. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2449. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2450. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2451. int sumi = 0;
  2452. #pragma unroll
  2453. for (int i = 0; i < vdr; ++i) {
  2454. // SIMD dot product of quantized values
  2455. sumi = __dp4a(v[i], u[i], sumi);
  2456. }
  2457. #ifdef GGML_CUDA_F16
  2458. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2459. const float d8d8 = tmp.x;
  2460. const float m8s8 = tmp.y;
  2461. #else
  2462. const float2 dm8f = __half22float2(dm8);
  2463. const float2 ds8f = __half22float2(ds8);
  2464. const float d8d8 = dm8f.x * ds8f.x;
  2465. const float m8s8 = dm8f.y * ds8f.y;
  2466. #endif // GGML_CUDA_F16
  2467. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2468. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2469. #else
  2470. NO_DEVICE_CODE;
  2471. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2472. }
  2473. #define VDR_Q2_K_Q8_1_MMVQ 1
  2474. #define VDR_Q2_K_Q8_1_MMQ 2
  2475. // contiguous v/x values
  2476. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2477. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2478. const half2 & dm2, const float * __restrict__ d8) {
  2479. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2480. float sumf_d = 0.0f;
  2481. float sumf_m = 0.0f;
  2482. #pragma unroll
  2483. for (int i = 0; i < QR2_K; ++i) {
  2484. const int sc = scales[2*i];
  2485. const int vi = (v >> (2*i)) & 0x03030303;
  2486. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2487. // fill int with 4x m
  2488. int m = sc >> 4;
  2489. m |= m << 8;
  2490. m |= m << 16;
  2491. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2492. }
  2493. const float2 dm2f = __half22float2(dm2);
  2494. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2495. #else
  2496. NO_DEVICE_CODE;
  2497. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2498. }
  2499. // contiguous u/y values
  2500. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2501. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2502. const half2 & dm2, const float & d8) {
  2503. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2504. int sumi_d = 0;
  2505. int sumi_m = 0;
  2506. #pragma unroll
  2507. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2508. int sumi_d_sc = 0;
  2509. const int sc = scales[i0 / (QI8_1/2)];
  2510. // fill int with 4x m
  2511. int m = sc >> 4;
  2512. m |= m << 8;
  2513. m |= m << 16;
  2514. #pragma unroll
  2515. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2516. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2517. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2518. }
  2519. sumi_d += sumi_d_sc * (sc & 0xF);
  2520. }
  2521. const float2 dm2f = __half22float2(dm2);
  2522. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2523. #else
  2524. NO_DEVICE_CODE;
  2525. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2526. }
  2527. #define VDR_Q3_K_Q8_1_MMVQ 1
  2528. #define VDR_Q3_K_Q8_1_MMQ 2
  2529. // contiguous v/x values
  2530. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2531. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2532. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2533. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2534. float sumf = 0.0f;
  2535. #pragma unroll
  2536. for (int i = 0; i < QR3_K; ++i) {
  2537. const int isc = scale_offset + 2*i;
  2538. const int isc_low = isc % (QK_K/32);
  2539. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2540. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2541. const int isc_high = isc % (QK_K/64);
  2542. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2543. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2544. const int sc = (sc_low | sc_high) - 32;
  2545. const int vil = (vl >> (2*i)) & 0x03030303;
  2546. const int vih = ((vh >> i) << 2) & 0x04040404;
  2547. const int vi = __vsubss4(vil, vih);
  2548. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2549. }
  2550. return d3 * sumf;
  2551. #else
  2552. NO_DEVICE_CODE;
  2553. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2554. }
  2555. // contiguous u/y values
  2556. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2557. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2558. const float & d3, const float & d8) {
  2559. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2560. int sumi = 0;
  2561. #pragma unroll
  2562. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2563. int sumi_sc = 0;
  2564. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2565. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2566. }
  2567. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2568. }
  2569. return d3*d8 * sumi;
  2570. #else
  2571. NO_DEVICE_CODE;
  2572. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2573. }
  2574. #define VDR_Q4_K_Q8_1_MMVQ 2
  2575. #define VDR_Q4_K_Q8_1_MMQ 8
  2576. // contiguous v/x values
  2577. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2578. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2579. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2580. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2581. float sumf_d = 0.0f;
  2582. float sumf_m = 0.0f;
  2583. #pragma unroll
  2584. for (int i = 0; i < QR4_K; ++i) {
  2585. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2586. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2587. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2588. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2589. sumf_d += d8[i] * (dot1 * sc[i]);
  2590. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2591. }
  2592. const float2 dm4f = __half22float2(dm4);
  2593. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2594. #else
  2595. NO_DEVICE_CODE;
  2596. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2597. }
  2598. // contiguous u/y values
  2599. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2600. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2601. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2602. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2603. float sumf_d = 0.0f;
  2604. float sumf_m = 0.0f;
  2605. #pragma unroll
  2606. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2607. int sumi_d = 0;
  2608. #pragma unroll
  2609. for (int j = 0; j < QI8_1; ++j) {
  2610. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2611. }
  2612. const float2 ds8f = __half22float2(ds8[i]);
  2613. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2614. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2615. }
  2616. const float2 dm4f = __half22float2(dm4);
  2617. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2618. #else
  2619. NO_DEVICE_CODE;
  2620. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2621. }
  2622. #define VDR_Q5_K_Q8_1_MMVQ 2
  2623. #define VDR_Q5_K_Q8_1_MMQ 8
  2624. // contiguous v/x values
  2625. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2626. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2627. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2628. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2629. float sumf_d = 0.0f;
  2630. float sumf_m = 0.0f;
  2631. #pragma unroll
  2632. for (int i = 0; i < QR5_K; ++i) {
  2633. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2634. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2635. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2636. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2637. const int v0i = vl0i | vh0i;
  2638. const int v1i = vl1i | vh1i;
  2639. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2640. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2641. sumf_d += d8[i] * (dot1 * sc[i]);
  2642. sumf_m += d8[i] * (dot2 * m[i]);
  2643. }
  2644. const float2 dm5f = __half22float2(dm5);
  2645. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2646. #else
  2647. NO_DEVICE_CODE;
  2648. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2649. }
  2650. // contiguous u/y values
  2651. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2652. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2653. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2654. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2655. float sumf_d = 0.0f;
  2656. float sumf_m = 0.0f;
  2657. #pragma unroll
  2658. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2659. int sumi_d = 0;
  2660. #pragma unroll
  2661. for (int j = 0; j < QI8_1; ++j) {
  2662. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2663. }
  2664. const float2 ds8f = __half22float2(ds8[i]);
  2665. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2666. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2667. }
  2668. const float2 dm4f = __half22float2(dm4);
  2669. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2670. #else
  2671. NO_DEVICE_CODE;
  2672. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2673. }
  2674. #define VDR_Q6_K_Q8_1_MMVQ 1
  2675. #define VDR_Q6_K_Q8_1_MMQ 8
  2676. // contiguous v/x values
  2677. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2678. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2679. const float & d, const float * __restrict__ d8) {
  2680. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2681. float sumf = 0.0f;
  2682. #pragma unroll
  2683. for (int i = 0; i < QR6_K; ++i) {
  2684. const int sc = scales[4*i];
  2685. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2686. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2687. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2688. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2689. }
  2690. return d*sumf;
  2691. #else
  2692. NO_DEVICE_CODE;
  2693. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2694. }
  2695. // contiguous u/y values
  2696. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2697. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2698. const float & d6, const float * __restrict__ d8) {
  2699. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2700. float sumf_d = 0.0f;
  2701. #pragma unroll
  2702. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2703. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2704. #pragma unroll
  2705. for (int i = i0; i < i0 + 2; ++i) {
  2706. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2707. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2708. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2709. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2710. }
  2711. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2712. }
  2713. return d6 * sumf_d;
  2714. #else
  2715. NO_DEVICE_CODE;
  2716. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2717. }
  2718. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2719. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2720. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2721. int v[VDR_Q4_0_Q8_1_MMVQ];
  2722. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2723. #pragma unroll
  2724. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2725. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2726. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2727. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2728. }
  2729. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2730. }
  2731. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2732. (void)x_qh; (void)x_sc;
  2733. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2734. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2735. *x_ql = tile_x_qs;
  2736. *x_dm = (half2 *) tile_x_d;
  2737. }
  2738. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2739. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2740. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2741. (void)x_qh; (void)x_sc;
  2742. GGML_CUDA_ASSUME(i_offset >= 0);
  2743. GGML_CUDA_ASSUME(i_offset < nwarps);
  2744. GGML_CUDA_ASSUME(k >= 0);
  2745. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2746. const int kbx = k / QI4_0;
  2747. const int kqsx = k % QI4_0;
  2748. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2749. float * x_dmf = (float *) x_dm;
  2750. #pragma unroll
  2751. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2752. int i = i0 + i_offset;
  2753. if (need_check) {
  2754. i = min(i, i_max);
  2755. }
  2756. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2757. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2758. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2759. }
  2760. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2761. const int kbxd = k % blocks_per_tile_x_row;
  2762. #pragma unroll
  2763. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2764. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2765. if (need_check) {
  2766. i = min(i, i_max);
  2767. }
  2768. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2769. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2770. }
  2771. }
  2772. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2773. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2774. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2775. (void)x_qh; (void)x_sc;
  2776. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2777. const float * x_dmf = (const float *) x_dm;
  2778. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2779. #pragma unroll
  2780. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2781. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2782. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2783. }
  2784. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2785. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2786. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2787. }
  2788. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2789. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2790. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2791. int v[VDR_Q4_1_Q8_1_MMVQ];
  2792. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2793. #pragma unroll
  2794. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2795. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2796. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2797. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2798. }
  2799. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2800. }
  2801. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2802. (void)x_qh; (void)x_sc;
  2803. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2804. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2805. *x_ql = tile_x_qs;
  2806. *x_dm = tile_x_dm;
  2807. }
  2808. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2809. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2810. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2811. (void)x_qh; (void)x_sc;
  2812. GGML_CUDA_ASSUME(i_offset >= 0);
  2813. GGML_CUDA_ASSUME(i_offset < nwarps);
  2814. GGML_CUDA_ASSUME(k >= 0);
  2815. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2816. const int kbx = k / QI4_1;
  2817. const int kqsx = k % QI4_1;
  2818. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2819. #pragma unroll
  2820. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2821. int i = i0 + i_offset;
  2822. if (need_check) {
  2823. i = min(i, i_max);
  2824. }
  2825. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2826. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2827. }
  2828. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2829. const int kbxd = k % blocks_per_tile_x_row;
  2830. #pragma unroll
  2831. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2832. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2833. if (need_check) {
  2834. i = min(i, i_max);
  2835. }
  2836. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2837. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2838. }
  2839. }
  2840. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2841. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2842. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2843. (void)x_qh; (void)x_sc;
  2844. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2845. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2846. #pragma unroll
  2847. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2848. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2849. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2850. }
  2851. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2852. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2853. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2854. }
  2855. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2856. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2857. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2858. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2859. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2860. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2861. #pragma unroll
  2862. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2863. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2864. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2865. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2866. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2867. }
  2868. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2869. }
  2870. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2871. (void)x_qh; (void)x_sc;
  2872. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2873. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2874. *x_ql = tile_x_ql;
  2875. *x_dm = (half2 *) tile_x_d;
  2876. }
  2877. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2878. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2879. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2880. (void)x_qh; (void)x_sc;
  2881. GGML_CUDA_ASSUME(i_offset >= 0);
  2882. GGML_CUDA_ASSUME(i_offset < nwarps);
  2883. GGML_CUDA_ASSUME(k >= 0);
  2884. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2885. const int kbx = k / QI5_0;
  2886. const int kqsx = k % QI5_0;
  2887. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2888. #pragma unroll
  2889. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2890. int i = i0 + i_offset;
  2891. if (need_check) {
  2892. i = min(i, i_max);
  2893. }
  2894. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2895. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2896. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2897. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2898. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2899. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2900. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2901. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2902. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2903. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2904. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2905. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2906. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2907. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2908. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2909. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2910. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2911. }
  2912. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2913. const int kbxd = k % blocks_per_tile_x_row;
  2914. float * x_dmf = (float *) x_dm;
  2915. #pragma unroll
  2916. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2917. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2918. if (need_check) {
  2919. i = min(i, i_max);
  2920. }
  2921. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2922. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2923. }
  2924. }
  2925. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2926. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2927. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2928. (void)x_qh; (void)x_sc;
  2929. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2930. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2931. const float * x_dmf = (const float *) x_dm;
  2932. const float * y_df = (const float *) y_ds;
  2933. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2934. #pragma unroll
  2935. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2936. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2937. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2938. }
  2939. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2940. (&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)]);
  2941. }
  2942. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2943. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2944. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2945. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2946. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2947. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2948. #pragma unroll
  2949. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2950. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2951. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2952. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2953. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2954. }
  2955. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2956. }
  2957. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2958. (void)x_qh; (void)x_sc;
  2959. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2960. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2961. *x_ql = tile_x_ql;
  2962. *x_dm = tile_x_dm;
  2963. }
  2964. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2965. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2966. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2967. (void)x_qh; (void)x_sc;
  2968. GGML_CUDA_ASSUME(i_offset >= 0);
  2969. GGML_CUDA_ASSUME(i_offset < nwarps);
  2970. GGML_CUDA_ASSUME(k >= 0);
  2971. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2972. const int kbx = k / QI5_1;
  2973. const int kqsx = k % QI5_1;
  2974. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2975. #pragma unroll
  2976. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2977. int i = i0 + i_offset;
  2978. if (need_check) {
  2979. i = min(i, i_max);
  2980. }
  2981. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2982. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2983. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2984. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2985. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2986. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2987. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2988. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2989. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2990. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2991. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2992. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2993. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2994. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2995. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2996. }
  2997. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2998. const int kbxd = k % blocks_per_tile_x_row;
  2999. #pragma unroll
  3000. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  3001. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  3002. if (need_check) {
  3003. i = min(i, i_max);
  3004. }
  3005. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3006. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  3007. }
  3008. }
  3009. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  3010. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3011. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3012. (void)x_qh; (void)x_sc;
  3013. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3014. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  3015. int u[2*VDR_Q5_1_Q8_1_MMQ];
  3016. #pragma unroll
  3017. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  3018. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3019. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  3020. }
  3021. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  3022. (&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)]);
  3023. }
  3024. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  3025. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3026. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  3027. int v[VDR_Q8_0_Q8_1_MMVQ];
  3028. int u[VDR_Q8_0_Q8_1_MMVQ];
  3029. #pragma unroll
  3030. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  3031. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  3032. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3033. }
  3034. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  3035. }
  3036. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3037. (void)x_qh; (void)x_sc;
  3038. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3039. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  3040. *x_ql = tile_x_qs;
  3041. *x_dm = (half2 *) tile_x_d;
  3042. }
  3043. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  3044. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3045. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3046. (void)x_qh; (void)x_sc;
  3047. GGML_CUDA_ASSUME(i_offset >= 0);
  3048. GGML_CUDA_ASSUME(i_offset < nwarps);
  3049. GGML_CUDA_ASSUME(k >= 0);
  3050. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3051. const int kbx = k / QI8_0;
  3052. const int kqsx = k % QI8_0;
  3053. float * x_dmf = (float *) x_dm;
  3054. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  3055. #pragma unroll
  3056. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3057. int i = i0 + i_offset;
  3058. if (need_check) {
  3059. i = min(i, i_max);
  3060. }
  3061. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3062. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  3063. }
  3064. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  3065. const int kbxd = k % blocks_per_tile_x_row;
  3066. #pragma unroll
  3067. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  3068. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  3069. if (need_check) {
  3070. i = min(i, i_max);
  3071. }
  3072. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3073. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  3074. }
  3075. }
  3076. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  3077. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3078. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3079. (void)x_qh; (void)x_sc;
  3080. const float * x_dmf = (const float *) x_dm;
  3081. const float * y_df = (const float *) y_ds;
  3082. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  3083. (&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],
  3084. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  3085. }
  3086. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  3087. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3088. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  3089. const int bq8_offset = QR2_K * (iqs / QI8_1);
  3090. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3091. const uint8_t * scales = bq2_K->scales + scale_offset;
  3092. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  3093. int u[QR2_K];
  3094. float d8[QR2_K];
  3095. #pragma unroll
  3096. for (int i = 0; i < QR2_K; ++ i) {
  3097. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3098. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3099. }
  3100. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  3101. }
  3102. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3103. (void)x_qh;
  3104. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3105. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  3106. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3107. *x_ql = tile_x_ql;
  3108. *x_dm = tile_x_dm;
  3109. *x_sc = tile_x_sc;
  3110. }
  3111. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  3112. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3113. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3114. (void)x_qh;
  3115. GGML_CUDA_ASSUME(i_offset >= 0);
  3116. GGML_CUDA_ASSUME(i_offset < nwarps);
  3117. GGML_CUDA_ASSUME(k >= 0);
  3118. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3119. const int kbx = k / QI2_K;
  3120. const int kqsx = k % QI2_K;
  3121. const block_q2_K * bx0 = (const block_q2_K *) vx;
  3122. #pragma unroll
  3123. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3124. int i = i0 + i_offset;
  3125. if (need_check) {
  3126. i = min(i, i_max);
  3127. }
  3128. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  3129. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3130. }
  3131. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  3132. const int kbxd = k % blocks_per_tile_x_row;
  3133. #pragma unroll
  3134. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  3135. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  3136. if (need_check) {
  3137. i = min(i, i_max);
  3138. }
  3139. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3140. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  3141. }
  3142. #pragma unroll
  3143. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3144. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3145. if (need_check) {
  3146. i = min(i, i_max);
  3147. }
  3148. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  3149. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  3150. }
  3151. }
  3152. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  3153. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3154. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3155. (void)x_qh;
  3156. const int kbx = k / QI2_K;
  3157. const int ky = (k % QI2_K) * QR2_K;
  3158. const float * y_df = (const float *) y_ds;
  3159. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  3160. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  3161. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  3162. #pragma unroll
  3163. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  3164. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3165. }
  3166. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  3167. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  3168. 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]);
  3169. }
  3170. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  3171. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3172. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  3173. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  3174. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3175. const float d = bq3_K->d;
  3176. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  3177. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3178. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  3179. int u[QR3_K];
  3180. float d8[QR3_K];
  3181. #pragma unroll
  3182. for (int i = 0; i < QR3_K; ++i) {
  3183. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3184. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3185. }
  3186. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  3187. }
  3188. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3189. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3190. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  3191. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  3192. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3193. *x_ql = tile_x_ql;
  3194. *x_dm = tile_x_dm;
  3195. *x_qh = tile_x_qh;
  3196. *x_sc = tile_x_sc;
  3197. }
  3198. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  3199. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3200. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3201. GGML_CUDA_ASSUME(i_offset >= 0);
  3202. GGML_CUDA_ASSUME(i_offset < nwarps);
  3203. GGML_CUDA_ASSUME(k >= 0);
  3204. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3205. const int kbx = k / QI3_K;
  3206. const int kqsx = k % QI3_K;
  3207. const block_q3_K * bx0 = (const block_q3_K *) vx;
  3208. #pragma unroll
  3209. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3210. int i = i0 + i_offset;
  3211. if (need_check) {
  3212. i = min(i, i_max);
  3213. }
  3214. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  3215. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3216. }
  3217. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  3218. const int kbxd = k % blocks_per_tile_x_row;
  3219. float * x_dmf = (float *) x_dm;
  3220. #pragma unroll
  3221. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  3222. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  3223. if (need_check) {
  3224. i = min(i, i_max);
  3225. }
  3226. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3227. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  3228. }
  3229. #pragma unroll
  3230. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  3231. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  3232. if (need_check) {
  3233. i = min(i, i_max);
  3234. }
  3235. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  3236. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3237. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  3238. }
  3239. #pragma unroll
  3240. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3241. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3242. if (need_check) {
  3243. i = min(i, i_max);
  3244. }
  3245. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  3246. const int ksc = k % (QI3_K/4);
  3247. const int ksc_low = ksc % (QI3_K/8);
  3248. const int shift_low = 4 * (ksc / (QI3_K/8));
  3249. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3250. const int ksc_high = QI3_K/8;
  3251. const int shift_high = 2 * ksc;
  3252. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3253. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3254. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3255. }
  3256. }
  3257. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3258. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3259. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3260. const int kbx = k / QI3_K;
  3261. const int ky = (k % QI3_K) * QR3_K;
  3262. const float * x_dmf = (const float *) x_dm;
  3263. const float * y_df = (const float *) y_ds;
  3264. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3265. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3266. #pragma unroll
  3267. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3268. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3269. const int shift = 2 * ((ky % 32) / 8);
  3270. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3271. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3272. const int vlh = (vh << 2) & 0x04040404;
  3273. v[l] = __vsubss4(vll, vlh);
  3274. }
  3275. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3276. 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]);
  3277. }
  3278. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3279. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3280. #ifndef GGML_QKK_64
  3281. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3282. int v[2];
  3283. int u[2*QR4_K];
  3284. float d8[QR4_K];
  3285. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3286. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3287. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3288. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3289. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3290. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3291. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3292. v[0] = q4[0];
  3293. v[1] = q4[4];
  3294. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3295. uint16_t aux[2];
  3296. const int j = bq8_offset/2;
  3297. if (j < 2) {
  3298. aux[0] = scales[j+0] & 0x3f3f;
  3299. aux[1] = scales[j+2] & 0x3f3f;
  3300. } else {
  3301. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3302. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3303. }
  3304. const uint8_t * sc = (const uint8_t *)aux;
  3305. const uint8_t * m = sc + 2;
  3306. for (int i = 0; i < QR4_K; ++i) {
  3307. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3308. d8[i] = __low2half(bq8i->ds);
  3309. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3310. u[2*i+0] = q8[0];
  3311. u[2*i+1] = q8[4];
  3312. }
  3313. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3314. #else
  3315. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3316. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3317. float sumf_d = 0.0f;
  3318. float sumf_m = 0.0f;
  3319. uint16_t aux16[2];
  3320. const uint8_t * s = (const uint8_t *)aux16;
  3321. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3322. aux16[0] = a[0] & 0x0f0f;
  3323. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3324. const float dall = bq4_K->dm[0];
  3325. const float dmin = bq4_K->dm[1];
  3326. const float d8_1 = __low2float(bq8_1[0].ds);
  3327. const float d8_2 = __low2float(bq8_1[1].ds);
  3328. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3329. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3330. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3331. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3332. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3333. const int v1 = q4[0];
  3334. const int v2 = q4[4];
  3335. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3336. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3337. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3338. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3339. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3340. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3341. return dall * sumf_d - dmin * sumf_m;
  3342. #else
  3343. NO_DEVICE_CODE;
  3344. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3345. #endif
  3346. }
  3347. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3348. (void)x_qh;
  3349. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3350. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3351. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3352. *x_ql = tile_x_ql;
  3353. *x_dm = tile_x_dm;
  3354. *x_sc = tile_x_sc;
  3355. }
  3356. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3357. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3358. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3359. (void)x_qh;
  3360. GGML_CUDA_ASSUME(i_offset >= 0);
  3361. GGML_CUDA_ASSUME(i_offset < nwarps);
  3362. GGML_CUDA_ASSUME(k >= 0);
  3363. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3364. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3365. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3366. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3367. #pragma unroll
  3368. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3369. int i = i0 + i_offset;
  3370. if (need_check) {
  3371. i = min(i, i_max);
  3372. }
  3373. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3374. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3375. }
  3376. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3377. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3378. #pragma unroll
  3379. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3380. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3381. if (need_check) {
  3382. i = min(i, i_max);
  3383. }
  3384. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3385. #if QK_K == 256
  3386. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3387. #else
  3388. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3389. #endif
  3390. }
  3391. #pragma unroll
  3392. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3393. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3394. if (need_check) {
  3395. i = min(i, i_max);
  3396. }
  3397. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3398. const int * scales = (const int *) bxi->scales;
  3399. const int ksc = k % (WARP_SIZE/8);
  3400. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3401. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3402. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3403. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3404. }
  3405. }
  3406. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3407. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3408. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3409. (void)x_qh;
  3410. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3411. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3412. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3413. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3414. }
  3415. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3416. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3417. #ifndef GGML_QKK_64
  3418. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3419. int vl[2];
  3420. int vh[2];
  3421. int u[2*QR5_K];
  3422. float d8[QR5_K];
  3423. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3424. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3425. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3426. vl[0] = ql[0];
  3427. vl[1] = ql[4];
  3428. vh[0] = qh[0] >> bq8_offset;
  3429. vh[1] = qh[4] >> bq8_offset;
  3430. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3431. uint16_t aux[2];
  3432. const int j = bq8_offset/2;
  3433. if (j < 2) {
  3434. aux[0] = scales[j+0] & 0x3f3f;
  3435. aux[1] = scales[j+2] & 0x3f3f;
  3436. } else {
  3437. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3438. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3439. }
  3440. const uint8_t * sc = (const uint8_t *)aux;
  3441. const uint8_t * m = sc + 2;
  3442. #pragma unroll
  3443. for (int i = 0; i < QR5_K; ++i) {
  3444. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3445. d8[i] = __low2float(bq8i->ds);
  3446. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3447. u[2*i+0] = q8[0];
  3448. u[2*i+1] = q8[4];
  3449. }
  3450. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3451. #else
  3452. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3453. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3454. const int8_t * s = bq5_K->scales;
  3455. const float d = bq5_K->d;
  3456. const float d8_1 = __low2half(bq8_1[0].ds);
  3457. const float d8_2 = __low2half(bq8_1[1].ds);
  3458. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3459. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3460. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3461. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3462. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3463. const int vl1 = ql[0];
  3464. const int vl2 = ql[4];
  3465. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3466. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3467. const int in = step%8; // 0, 4, 0, 4
  3468. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3469. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3470. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3471. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3472. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3473. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3474. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3475. return d * sumf_d;
  3476. #else
  3477. NO_DEVICE_CODE;
  3478. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3479. #endif
  3480. }
  3481. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3482. (void)x_qh;
  3483. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3484. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3485. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3486. *x_ql = tile_x_ql;
  3487. *x_dm = tile_x_dm;
  3488. *x_sc = tile_x_sc;
  3489. }
  3490. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3491. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3492. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3493. (void)x_qh;
  3494. GGML_CUDA_ASSUME(i_offset >= 0);
  3495. GGML_CUDA_ASSUME(i_offset < nwarps);
  3496. GGML_CUDA_ASSUME(k >= 0);
  3497. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3498. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3499. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3500. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3501. #pragma unroll
  3502. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3503. int i = i0 + i_offset;
  3504. if (need_check) {
  3505. i = min(i, i_max);
  3506. }
  3507. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3508. const int ky = QR5_K*kqsx;
  3509. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3510. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3511. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3512. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3513. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3514. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3515. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3516. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3517. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3518. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3519. }
  3520. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3521. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3522. #pragma unroll
  3523. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3524. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3525. if (need_check) {
  3526. i = min(i, i_max);
  3527. }
  3528. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3529. #if QK_K == 256
  3530. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3531. #endif
  3532. }
  3533. #pragma unroll
  3534. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3535. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3536. if (need_check) {
  3537. i = min(i, i_max);
  3538. }
  3539. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3540. const int * scales = (const int *) bxi->scales;
  3541. const int ksc = k % (WARP_SIZE/8);
  3542. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3543. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3544. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3545. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3546. }
  3547. }
  3548. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3549. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3550. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3551. (void)x_qh;
  3552. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3553. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3554. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3555. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3556. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3557. }
  3558. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3559. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3560. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3561. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3562. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3563. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3564. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3565. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3566. const int8_t * scales = bq6_K->scales + scale_offset;
  3567. int u[QR6_K];
  3568. float d8[QR6_K];
  3569. #pragma unroll
  3570. for (int i = 0; i < QR6_K; ++i) {
  3571. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3572. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3573. }
  3574. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3575. }
  3576. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3577. (void)x_qh;
  3578. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3579. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3580. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3581. *x_ql = tile_x_ql;
  3582. *x_dm = tile_x_dm;
  3583. *x_sc = tile_x_sc;
  3584. }
  3585. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3586. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3587. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3588. (void)x_qh;
  3589. GGML_CUDA_ASSUME(i_offset >= 0);
  3590. GGML_CUDA_ASSUME(i_offset < nwarps);
  3591. GGML_CUDA_ASSUME(k >= 0);
  3592. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3593. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3594. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3595. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3596. #pragma unroll
  3597. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3598. int i = i0 + i_offset;
  3599. if (need_check) {
  3600. i = min(i, i_max);
  3601. }
  3602. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3603. const int ky = QR6_K*kqsx;
  3604. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3605. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3606. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3607. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3608. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3609. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3610. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3611. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3612. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3613. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3614. }
  3615. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3616. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3617. float * x_dmf = (float *) x_dm;
  3618. #pragma unroll
  3619. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3620. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3621. if (need_check) {
  3622. i = min(i, i_max);
  3623. }
  3624. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3625. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3626. }
  3627. #pragma unroll
  3628. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3629. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3630. if (need_check) {
  3631. i = min(i, i_max);
  3632. }
  3633. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3634. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3635. }
  3636. }
  3637. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3638. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3639. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3640. (void)x_qh;
  3641. const float * x_dmf = (const float *) x_dm;
  3642. const float * y_df = (const float *) y_ds;
  3643. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3644. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3645. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3646. 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]);
  3647. }
  3648. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3649. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3650. #if QK_K == 256
  3651. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3652. #if QR2_XXS == 8
  3653. const int ib32 = iqs;
  3654. const uint16_t * q2 = bq2->qs + 4*ib32;
  3655. const uint8_t * aux8 = (const uint8_t *)q2;
  3656. const int8_t * q8 = bq8_1[ib32].qs;
  3657. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3658. int sumi = 0;
  3659. for (int l = 0; l < 4; ++l) {
  3660. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3661. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3662. for (int j = 0; j < 8; ++j) {
  3663. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3664. }
  3665. q8 += 8;
  3666. aux32 >>= 7;
  3667. }
  3668. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3669. return d * sumi;
  3670. #else
  3671. // iqs is 0...15
  3672. const int ib32 = iqs/2;
  3673. const int il = iqs%2;
  3674. const uint16_t * q2 = bq2->qs + 4*ib32;
  3675. const uint8_t * aux8 = (const uint8_t *)q2;
  3676. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  3677. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  3678. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3679. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3680. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3681. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3682. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3683. int sumi1 = 0, sumi2 = 0;
  3684. for (int j = 0; j < 8; ++j) {
  3685. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3686. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3687. }
  3688. return d * (sumi1 + sumi2);
  3689. #endif
  3690. #else
  3691. assert(false);
  3692. return 0.f;
  3693. #endif
  3694. }
  3695. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  3696. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3697. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3698. #if QK_K == 256
  3699. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  3700. const int ib32 = iqs;
  3701. const uint16_t * q2 = bq2->qs + 4*ib32;
  3702. const int8_t * q8 = bq8_1[ib32].qs;
  3703. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3704. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3705. int sumi1 = 0;
  3706. for (int l = 0; l < 2; ++l) {
  3707. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3708. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3709. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3710. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3711. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3712. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3713. q8 += 8;
  3714. }
  3715. int sumi2 = 0;
  3716. for (int l = 2; l < 4; ++l) {
  3717. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3718. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3719. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3720. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3721. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3722. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3723. q8 += 8;
  3724. }
  3725. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3726. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3727. #else
  3728. (void) ksigns64;
  3729. assert(false);
  3730. return 0.f;
  3731. #endif
  3732. #else
  3733. (void) ksigns64;
  3734. assert(false);
  3735. return 0.f;
  3736. #endif
  3737. }
  3738. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  3739. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3740. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3741. #if QK_K == 256
  3742. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  3743. const int ib32 = iqs;
  3744. const uint8_t * q3 = bq2->qs + 8*ib32;
  3745. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  3746. const int8_t * q8 = bq8_1[ib32].qs;
  3747. uint32_t aux32 = gas[0] | (gas[1] << 16);
  3748. int sumi = 0;
  3749. for (int l = 0; l < 4; ++l) {
  3750. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  3751. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  3752. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  3753. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  3754. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  3755. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3756. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3757. q8 += 8;
  3758. aux32 >>= 7;
  3759. }
  3760. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  3761. return d * sumi;
  3762. #else
  3763. assert(false);
  3764. return 0.f;
  3765. #endif
  3766. #else
  3767. assert(false);
  3768. return 0.f;
  3769. #endif
  3770. }
  3771. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  3772. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3773. #if QK_K == 256
  3774. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  3775. const int ib32 = iqs;
  3776. int sumi1 = 0, sumi2 = 0, sumi3 = 0, sumi4 = 0;
  3777. const uint8_t h1 = bq1->scales[2*ib32+0];
  3778. const uint8_t h2 = bq1->scales[2*ib32+1];
  3779. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3780. const int * q8 = (const int *)bq8_1[ib32].qs;
  3781. const int * grid1 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3782. const int * grid2 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3783. const int * grid3 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3784. const int * grid4 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3785. for (int j = 0; j < 2; ++j) {
  3786. sumi1 = __dp4a(q8[j+0], grid1[j], sumi1);
  3787. sumi2 = __dp4a(q8[j+2], grid2[j], sumi2);
  3788. sumi3 = __dp4a(q8[j+4], grid3[j], sumi3);
  3789. sumi4 = __dp4a(q8[j+6], grid4[j], sumi4);
  3790. }
  3791. #else
  3792. const int8_t * q8 = bq8_1[ib32].qs;
  3793. const int8_t * grid1 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3794. const int8_t * grid2 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3795. const int8_t * grid3 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3796. const int8_t * grid4 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3797. for (int j = 0; j < 8; ++j) {
  3798. sumi1 += q8[j+ 0] * grid1[j];
  3799. sumi2 += q8[j+ 8] * grid2[j];
  3800. sumi3 += q8[j+16] * grid3[j];
  3801. sumi4 += q8[j+24] * grid4[j];
  3802. }
  3803. #endif
  3804. const float d = (float)bq1->d * __low2float(bq8_1[ib32].ds);
  3805. return d * (sumi1 * (2*(h1 & 7) + 1) + sumi2 * (2*((h1 >> 4) & 7) + 1) +
  3806. sumi3 * (2*(h2 & 7) + 1) + sumi4 * (2*((h2 >> 4) & 7) + 1));
  3807. #else
  3808. assert(false);
  3809. return 0.f;
  3810. #endif
  3811. }
  3812. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3813. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3814. static __device__ __forceinline__ void mul_mat_q(
  3815. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3816. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3817. const block_q_t * x = (const block_q_t *) vx;
  3818. const block_q8_1 * y = (const block_q8_1 *) vy;
  3819. const int blocks_per_row_x = ncols_x / qk;
  3820. const int blocks_per_col_y = nrows_y / QK8_1;
  3821. const int blocks_per_warp = WARP_SIZE / qi;
  3822. const int & ncols_dst = ncols_y;
  3823. const int row_dst_0 = blockIdx.x*mmq_y;
  3824. const int & row_x_0 = row_dst_0;
  3825. const int col_dst_0 = blockIdx.y*mmq_x;
  3826. const int & col_y_0 = col_dst_0;
  3827. int * tile_x_ql = nullptr;
  3828. half2 * tile_x_dm = nullptr;
  3829. int * tile_x_qh = nullptr;
  3830. int * tile_x_sc = nullptr;
  3831. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3832. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3833. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3834. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3835. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3836. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3837. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3838. #pragma unroll
  3839. for (int ir = 0; ir < qr; ++ir) {
  3840. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3841. const int kbxd = kqs / QI8_1;
  3842. #pragma unroll
  3843. for (int i = 0; i < mmq_x; i += nwarps) {
  3844. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3845. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3846. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3847. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3848. }
  3849. #pragma unroll
  3850. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3851. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3852. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3853. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3854. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3855. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3856. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3857. if (need_sum) {
  3858. *dsi_dst = *dsi_src;
  3859. } else {
  3860. float * dfi_dst = (float *) dsi_dst;
  3861. *dfi_dst = __low2half(*dsi_src);
  3862. }
  3863. }
  3864. __syncthreads();
  3865. // #pragma unroll // unrolling this loop causes too much register pressure
  3866. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3867. #pragma unroll
  3868. for (int j = 0; j < mmq_x; j += nwarps) {
  3869. #pragma unroll
  3870. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3871. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3872. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3873. threadIdx.x + i, threadIdx.y + j, k);
  3874. }
  3875. }
  3876. }
  3877. __syncthreads();
  3878. }
  3879. }
  3880. #pragma unroll
  3881. for (int j = 0; j < mmq_x; j += nwarps) {
  3882. const int col_dst = col_dst_0 + j + threadIdx.y;
  3883. if (col_dst >= ncols_dst) {
  3884. return;
  3885. }
  3886. #pragma unroll
  3887. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3888. const int row_dst = row_dst_0 + threadIdx.x + i;
  3889. if (row_dst >= nrows_dst) {
  3890. continue;
  3891. }
  3892. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3893. }
  3894. }
  3895. }
  3896. #define MMQ_X_Q4_0_RDNA2 64
  3897. #define MMQ_Y_Q4_0_RDNA2 128
  3898. #define NWARPS_Q4_0_RDNA2 8
  3899. #define MMQ_X_Q4_0_RDNA1 64
  3900. #define MMQ_Y_Q4_0_RDNA1 64
  3901. #define NWARPS_Q4_0_RDNA1 8
  3902. #if defined(CUDA_USE_TENSOR_CORES)
  3903. #define MMQ_X_Q4_0_AMPERE 4
  3904. #define MMQ_Y_Q4_0_AMPERE 32
  3905. #define NWARPS_Q4_0_AMPERE 4
  3906. #else
  3907. #define MMQ_X_Q4_0_AMPERE 64
  3908. #define MMQ_Y_Q4_0_AMPERE 128
  3909. #define NWARPS_Q4_0_AMPERE 4
  3910. #endif
  3911. #define MMQ_X_Q4_0_PASCAL 64
  3912. #define MMQ_Y_Q4_0_PASCAL 64
  3913. #define NWARPS_Q4_0_PASCAL 8
  3914. template <bool need_check> static __global__ void
  3915. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3916. #if defined(RDNA3) || defined(RDNA2)
  3917. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3918. #endif // defined(RDNA3) || defined(RDNA2)
  3919. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3920. mul_mat_q4_0(
  3921. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3922. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3923. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3924. #if defined(RDNA3) || defined(RDNA2)
  3925. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3926. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3927. const int nwarps = NWARPS_Q4_0_RDNA2;
  3928. #else
  3929. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3930. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3931. const int nwarps = NWARPS_Q4_0_RDNA1;
  3932. #endif // defined(RDNA3) || defined(RDNA2)
  3933. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3934. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3935. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3936. #elif __CUDA_ARCH__ >= CC_VOLTA
  3937. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3938. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3939. const int nwarps = NWARPS_Q4_0_AMPERE;
  3940. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3941. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3942. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3943. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3944. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3945. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3946. const int nwarps = NWARPS_Q4_0_PASCAL;
  3947. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3948. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3949. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3950. #else
  3951. (void) vec_dot_q4_0_q8_1_mul_mat;
  3952. NO_DEVICE_CODE;
  3953. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3954. }
  3955. #define MMQ_X_Q4_1_RDNA2 64
  3956. #define MMQ_Y_Q4_1_RDNA2 128
  3957. #define NWARPS_Q4_1_RDNA2 8
  3958. #define MMQ_X_Q4_1_RDNA1 64
  3959. #define MMQ_Y_Q4_1_RDNA1 64
  3960. #define NWARPS_Q4_1_RDNA1 8
  3961. #if defined(CUDA_USE_TENSOR_CORES)
  3962. #define MMQ_X_Q4_1_AMPERE 4
  3963. #define MMQ_Y_Q4_1_AMPERE 32
  3964. #define NWARPS_Q4_1_AMPERE 4
  3965. #else
  3966. #define MMQ_X_Q4_1_AMPERE 64
  3967. #define MMQ_Y_Q4_1_AMPERE 128
  3968. #define NWARPS_Q4_1_AMPERE 4
  3969. #endif
  3970. #define MMQ_X_Q4_1_PASCAL 64
  3971. #define MMQ_Y_Q4_1_PASCAL 64
  3972. #define NWARPS_Q4_1_PASCAL 8
  3973. template <bool need_check> static __global__ void
  3974. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3975. #if defined(RDNA3) || defined(RDNA2)
  3976. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3977. #endif // defined(RDNA3) || defined(RDNA2)
  3978. #elif __CUDA_ARCH__ < CC_VOLTA
  3979. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3980. #endif // __CUDA_ARCH__ < CC_VOLTA
  3981. mul_mat_q4_1(
  3982. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3983. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3984. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3985. #if defined(RDNA3) || defined(RDNA2)
  3986. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3987. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3988. const int nwarps = NWARPS_Q4_1_RDNA2;
  3989. #else
  3990. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3991. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3992. const int nwarps = NWARPS_Q4_1_RDNA1;
  3993. #endif // defined(RDNA3) || defined(RDNA2)
  3994. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3995. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3996. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3997. #elif __CUDA_ARCH__ >= CC_VOLTA
  3998. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3999. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  4000. const int nwarps = NWARPS_Q4_1_AMPERE;
  4001. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4002. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4003. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4004. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4005. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  4006. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  4007. const int nwarps = NWARPS_Q4_1_PASCAL;
  4008. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4009. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4010. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4011. #else
  4012. (void) vec_dot_q4_1_q8_1_mul_mat;
  4013. NO_DEVICE_CODE;
  4014. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4015. }
  4016. #define MMQ_X_Q5_0_RDNA2 64
  4017. #define MMQ_Y_Q5_0_RDNA2 128
  4018. #define NWARPS_Q5_0_RDNA2 8
  4019. #define MMQ_X_Q5_0_RDNA1 64
  4020. #define MMQ_Y_Q5_0_RDNA1 64
  4021. #define NWARPS_Q5_0_RDNA1 8
  4022. #if defined(CUDA_USE_TENSOR_CORES)
  4023. #define MMQ_X_Q5_0_AMPERE 4
  4024. #define MMQ_Y_Q5_0_AMPERE 32
  4025. #define NWARPS_Q5_0_AMPERE 4
  4026. #else
  4027. #define MMQ_X_Q5_0_AMPERE 128
  4028. #define MMQ_Y_Q5_0_AMPERE 64
  4029. #define NWARPS_Q5_0_AMPERE 4
  4030. #endif
  4031. #define MMQ_X_Q5_0_PASCAL 64
  4032. #define MMQ_Y_Q5_0_PASCAL 64
  4033. #define NWARPS_Q5_0_PASCAL 8
  4034. template <bool need_check> static __global__ void
  4035. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4036. #if defined(RDNA3) || defined(RDNA2)
  4037. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  4038. #endif // defined(RDNA3) || defined(RDNA2)
  4039. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4040. mul_mat_q5_0(
  4041. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4042. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4043. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4044. #if defined(RDNA3) || defined(RDNA2)
  4045. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  4046. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  4047. const int nwarps = NWARPS_Q5_0_RDNA2;
  4048. #else
  4049. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  4050. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  4051. const int nwarps = NWARPS_Q5_0_RDNA1;
  4052. #endif // defined(RDNA3) || defined(RDNA2)
  4053. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4054. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4055. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4056. #elif __CUDA_ARCH__ >= CC_VOLTA
  4057. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  4058. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  4059. const int nwarps = NWARPS_Q5_0_AMPERE;
  4060. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4061. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4062. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4063. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4064. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  4065. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  4066. const int nwarps = NWARPS_Q5_0_PASCAL;
  4067. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4068. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4069. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4070. #else
  4071. (void) vec_dot_q5_0_q8_1_mul_mat;
  4072. NO_DEVICE_CODE;
  4073. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4074. }
  4075. #define MMQ_X_Q5_1_RDNA2 64
  4076. #define MMQ_Y_Q5_1_RDNA2 128
  4077. #define NWARPS_Q5_1_RDNA2 8
  4078. #define MMQ_X_Q5_1_RDNA1 64
  4079. #define MMQ_Y_Q5_1_RDNA1 64
  4080. #define NWARPS_Q5_1_RDNA1 8
  4081. #if defined(CUDA_USE_TENSOR_CORES)
  4082. #define MMQ_X_Q5_1_AMPERE 4
  4083. #define MMQ_Y_Q5_1_AMPERE 32
  4084. #define NWARPS_Q5_1_AMPERE 4
  4085. #else
  4086. #define MMQ_X_Q5_1_AMPERE 128
  4087. #define MMQ_Y_Q5_1_AMPERE 64
  4088. #define NWARPS_Q5_1_AMPERE 4
  4089. #endif
  4090. #define MMQ_X_Q5_1_PASCAL 64
  4091. #define MMQ_Y_Q5_1_PASCAL 64
  4092. #define NWARPS_Q5_1_PASCAL 8
  4093. template <bool need_check> static __global__ void
  4094. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4095. #if defined(RDNA3) || defined(RDNA2)
  4096. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4097. #endif // defined(RDNA3) || defined(RDNA2)
  4098. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4099. mul_mat_q5_1(
  4100. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4101. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4102. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4103. #if defined(RDNA3) || defined(RDNA2)
  4104. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4105. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4106. const int nwarps = NWARPS_Q5_1_RDNA2;
  4107. #else
  4108. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4109. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4110. const int nwarps = NWARPS_Q5_1_RDNA1;
  4111. #endif // defined(RDNA3) || defined(RDNA2)
  4112. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4113. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4114. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4115. #elif __CUDA_ARCH__ >= CC_VOLTA
  4116. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4117. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4118. const int nwarps = NWARPS_Q5_1_AMPERE;
  4119. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4120. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4121. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4122. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4123. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4124. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4125. const int nwarps = NWARPS_Q5_1_PASCAL;
  4126. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4127. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4128. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4129. #else
  4130. (void) vec_dot_q5_1_q8_1_mul_mat;
  4131. NO_DEVICE_CODE;
  4132. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4133. }
  4134. #define MMQ_X_Q8_0_RDNA2 64
  4135. #define MMQ_Y_Q8_0_RDNA2 128
  4136. #define NWARPS_Q8_0_RDNA2 8
  4137. #define MMQ_X_Q8_0_RDNA1 64
  4138. #define MMQ_Y_Q8_0_RDNA1 64
  4139. #define NWARPS_Q8_0_RDNA1 8
  4140. #if defined(CUDA_USE_TENSOR_CORES)
  4141. #define MMQ_X_Q8_0_AMPERE 4
  4142. #define MMQ_Y_Q8_0_AMPERE 32
  4143. #define NWARPS_Q8_0_AMPERE 4
  4144. #else
  4145. #define MMQ_X_Q8_0_AMPERE 128
  4146. #define MMQ_Y_Q8_0_AMPERE 64
  4147. #define NWARPS_Q8_0_AMPERE 4
  4148. #endif
  4149. #define MMQ_X_Q8_0_PASCAL 64
  4150. #define MMQ_Y_Q8_0_PASCAL 64
  4151. #define NWARPS_Q8_0_PASCAL 8
  4152. template <bool need_check> static __global__ void
  4153. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4154. #if defined(RDNA3) || defined(RDNA2)
  4155. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4156. #endif // defined(RDNA3) || defined(RDNA2)
  4157. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4158. mul_mat_q8_0(
  4159. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4160. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4161. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4162. #if defined(RDNA3) || defined(RDNA2)
  4163. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4164. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4165. const int nwarps = NWARPS_Q8_0_RDNA2;
  4166. #else
  4167. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4168. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4169. const int nwarps = NWARPS_Q8_0_RDNA1;
  4170. #endif // defined(RDNA3) || defined(RDNA2)
  4171. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4172. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4173. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4174. #elif __CUDA_ARCH__ >= CC_VOLTA
  4175. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4176. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4177. const int nwarps = NWARPS_Q8_0_AMPERE;
  4178. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4179. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4180. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4181. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4182. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4183. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4184. const int nwarps = NWARPS_Q8_0_PASCAL;
  4185. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4186. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4187. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4188. #else
  4189. (void) vec_dot_q8_0_q8_1_mul_mat;
  4190. NO_DEVICE_CODE;
  4191. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4192. }
  4193. #define MMQ_X_Q2_K_RDNA2 64
  4194. #define MMQ_Y_Q2_K_RDNA2 128
  4195. #define NWARPS_Q2_K_RDNA2 8
  4196. #define MMQ_X_Q2_K_RDNA1 128
  4197. #define MMQ_Y_Q2_K_RDNA1 32
  4198. #define NWARPS_Q2_K_RDNA1 8
  4199. #if defined(CUDA_USE_TENSOR_CORES)
  4200. #define MMQ_X_Q2_K_AMPERE 4
  4201. #define MMQ_Y_Q2_K_AMPERE 32
  4202. #define NWARPS_Q2_K_AMPERE 4
  4203. #else
  4204. #define MMQ_X_Q2_K_AMPERE 64
  4205. #define MMQ_Y_Q2_K_AMPERE 128
  4206. #define NWARPS_Q2_K_AMPERE 4
  4207. #endif
  4208. #define MMQ_X_Q2_K_PASCAL 64
  4209. #define MMQ_Y_Q2_K_PASCAL 64
  4210. #define NWARPS_Q2_K_PASCAL 8
  4211. template <bool need_check> static __global__ void
  4212. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4213. #if defined(RDNA3) || defined(RDNA2)
  4214. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4215. #endif // defined(RDNA3) || defined(RDNA2)
  4216. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4217. mul_mat_q2_K(
  4218. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4219. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4220. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4221. #if defined(RDNA3) || defined(RDNA2)
  4222. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4223. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4224. const int nwarps = NWARPS_Q2_K_RDNA2;
  4225. #else
  4226. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4227. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4228. const int nwarps = NWARPS_Q2_K_RDNA1;
  4229. #endif // defined(RDNA3) || defined(RDNA2)
  4230. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4231. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4232. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4233. #elif __CUDA_ARCH__ >= CC_VOLTA
  4234. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4235. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4236. const int nwarps = NWARPS_Q2_K_AMPERE;
  4237. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4238. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4239. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4240. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4241. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4242. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4243. const int nwarps = NWARPS_Q2_K_PASCAL;
  4244. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4245. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4246. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4247. #else
  4248. (void) vec_dot_q2_K_q8_1_mul_mat;
  4249. NO_DEVICE_CODE;
  4250. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4251. }
  4252. #define MMQ_X_Q3_K_RDNA2 128
  4253. #define MMQ_Y_Q3_K_RDNA2 64
  4254. #define NWARPS_Q3_K_RDNA2 8
  4255. #define MMQ_X_Q3_K_RDNA1 32
  4256. #define MMQ_Y_Q3_K_RDNA1 128
  4257. #define NWARPS_Q3_K_RDNA1 8
  4258. #if defined(CUDA_USE_TENSOR_CORES)
  4259. #define MMQ_X_Q3_K_AMPERE 4
  4260. #define MMQ_Y_Q3_K_AMPERE 32
  4261. #define NWARPS_Q3_K_AMPERE 4
  4262. #else
  4263. #define MMQ_X_Q3_K_AMPERE 128
  4264. #define MMQ_Y_Q3_K_AMPERE 128
  4265. #define NWARPS_Q3_K_AMPERE 4
  4266. #endif
  4267. #define MMQ_X_Q3_K_PASCAL 64
  4268. #define MMQ_Y_Q3_K_PASCAL 64
  4269. #define NWARPS_Q3_K_PASCAL 8
  4270. template <bool need_check> static __global__ void
  4271. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4272. #if defined(RDNA3) || defined(RDNA2)
  4273. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4274. #endif // defined(RDNA3) || defined(RDNA2)
  4275. #elif __CUDA_ARCH__ < CC_VOLTA
  4276. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4277. #endif // __CUDA_ARCH__ < CC_VOLTA
  4278. mul_mat_q3_K(
  4279. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4280. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4281. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4282. #if defined(RDNA3) || defined(RDNA2)
  4283. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4284. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4285. const int nwarps = NWARPS_Q3_K_RDNA2;
  4286. #else
  4287. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4288. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4289. const int nwarps = NWARPS_Q3_K_RDNA1;
  4290. #endif // defined(RDNA3) || defined(RDNA2)
  4291. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4292. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4293. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4294. #elif __CUDA_ARCH__ >= CC_VOLTA
  4295. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4296. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4297. const int nwarps = NWARPS_Q3_K_AMPERE;
  4298. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4299. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4300. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4301. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4302. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4303. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4304. const int nwarps = NWARPS_Q3_K_PASCAL;
  4305. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4306. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4307. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4308. #else
  4309. (void) vec_dot_q3_K_q8_1_mul_mat;
  4310. NO_DEVICE_CODE;
  4311. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4312. }
  4313. #define MMQ_X_Q4_K_RDNA2 64
  4314. #define MMQ_Y_Q4_K_RDNA2 128
  4315. #define NWARPS_Q4_K_RDNA2 8
  4316. #define MMQ_X_Q4_K_RDNA1 32
  4317. #define MMQ_Y_Q4_K_RDNA1 64
  4318. #define NWARPS_Q4_K_RDNA1 8
  4319. #if defined(CUDA_USE_TENSOR_CORES)
  4320. #define MMQ_X_Q4_K_AMPERE 4
  4321. #define MMQ_Y_Q4_K_AMPERE 32
  4322. #define NWARPS_Q4_K_AMPERE 4
  4323. #else
  4324. #define MMQ_X_Q4_K_AMPERE 64
  4325. #define MMQ_Y_Q4_K_AMPERE 128
  4326. #define NWARPS_Q4_K_AMPERE 4
  4327. #endif
  4328. #define MMQ_X_Q4_K_PASCAL 64
  4329. #define MMQ_Y_Q4_K_PASCAL 64
  4330. #define NWARPS_Q4_K_PASCAL 8
  4331. template <bool need_check> static __global__ void
  4332. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4333. #if defined(RDNA3) || defined(RDNA2)
  4334. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4335. #endif // defined(RDNA3) || defined(RDNA2)
  4336. #elif __CUDA_ARCH__ < CC_VOLTA
  4337. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4338. #endif // __CUDA_ARCH__ < CC_VOLTA
  4339. mul_mat_q4_K(
  4340. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4341. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4342. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4343. #if defined(RDNA3) || defined(RDNA2)
  4344. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4345. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4346. const int nwarps = NWARPS_Q4_K_RDNA2;
  4347. #else
  4348. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4349. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4350. const int nwarps = NWARPS_Q4_K_RDNA1;
  4351. #endif // defined(RDNA3) || defined(RDNA2)
  4352. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4353. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4354. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4355. #elif __CUDA_ARCH__ >= CC_VOLTA
  4356. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4357. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4358. const int nwarps = NWARPS_Q4_K_AMPERE;
  4359. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4360. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4361. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4362. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4363. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4364. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4365. const int nwarps = NWARPS_Q4_K_PASCAL;
  4366. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4367. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4368. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4369. #else
  4370. (void) vec_dot_q4_K_q8_1_mul_mat;
  4371. NO_DEVICE_CODE;
  4372. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4373. }
  4374. #define MMQ_X_Q5_K_RDNA2 64
  4375. #define MMQ_Y_Q5_K_RDNA2 128
  4376. #define NWARPS_Q5_K_RDNA2 8
  4377. #define MMQ_X_Q5_K_RDNA1 32
  4378. #define MMQ_Y_Q5_K_RDNA1 64
  4379. #define NWARPS_Q5_K_RDNA1 8
  4380. #if defined(CUDA_USE_TENSOR_CORES)
  4381. #define MMQ_X_Q5_K_AMPERE 4
  4382. #define MMQ_Y_Q5_K_AMPERE 32
  4383. #define NWARPS_Q5_K_AMPERE 4
  4384. #else
  4385. #define MMQ_X_Q5_K_AMPERE 64
  4386. #define MMQ_Y_Q5_K_AMPERE 128
  4387. #define NWARPS_Q5_K_AMPERE 4
  4388. #endif
  4389. #define MMQ_X_Q5_K_PASCAL 64
  4390. #define MMQ_Y_Q5_K_PASCAL 64
  4391. #define NWARPS_Q5_K_PASCAL 8
  4392. template <bool need_check> static __global__ void
  4393. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4394. #if defined(RDNA3) || defined(RDNA2)
  4395. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  4396. #endif // defined(RDNA3) || defined(RDNA2)
  4397. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4398. mul_mat_q5_K(
  4399. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4400. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4401. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4402. #if defined(RDNA3) || defined(RDNA2)
  4403. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  4404. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  4405. const int nwarps = NWARPS_Q5_K_RDNA2;
  4406. #else
  4407. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  4408. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  4409. const int nwarps = NWARPS_Q5_K_RDNA1;
  4410. #endif // defined(RDNA3) || defined(RDNA2)
  4411. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4412. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4413. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4414. #elif __CUDA_ARCH__ >= CC_VOLTA
  4415. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  4416. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  4417. const int nwarps = NWARPS_Q5_K_AMPERE;
  4418. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4419. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4420. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4421. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4422. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  4423. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  4424. const int nwarps = NWARPS_Q5_K_PASCAL;
  4425. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4426. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4427. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4428. #else
  4429. (void) vec_dot_q5_K_q8_1_mul_mat;
  4430. NO_DEVICE_CODE;
  4431. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4432. }
  4433. #define MMQ_X_Q6_K_RDNA2 64
  4434. #define MMQ_Y_Q6_K_RDNA2 128
  4435. #define NWARPS_Q6_K_RDNA2 8
  4436. #define MMQ_X_Q6_K_RDNA1 32
  4437. #define MMQ_Y_Q6_K_RDNA1 64
  4438. #define NWARPS_Q6_K_RDNA1 8
  4439. #if defined(CUDA_USE_TENSOR_CORES)
  4440. #define MMQ_X_Q6_K_AMPERE 4
  4441. #define MMQ_Y_Q6_K_AMPERE 32
  4442. #define NWARPS_Q6_K_AMPERE 4
  4443. #else
  4444. #define MMQ_X_Q6_K_AMPERE 64
  4445. #define MMQ_Y_Q6_K_AMPERE 64
  4446. #define NWARPS_Q6_K_AMPERE 4
  4447. #endif
  4448. #define MMQ_X_Q6_K_PASCAL 64
  4449. #define MMQ_Y_Q6_K_PASCAL 64
  4450. #define NWARPS_Q6_K_PASCAL 8
  4451. template <bool need_check> static __global__ void
  4452. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4453. #if defined(RDNA3) || defined(RDNA2)
  4454. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4455. #endif // defined(RDNA3) || defined(RDNA2)
  4456. #elif __CUDA_ARCH__ < CC_VOLTA
  4457. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4458. #endif // __CUDA_ARCH__ < CC_VOLTA
  4459. mul_mat_q6_K(
  4460. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4461. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4462. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4463. #if defined(RDNA3) || defined(RDNA2)
  4464. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  4465. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  4466. const int nwarps = NWARPS_Q6_K_RDNA2;
  4467. #else
  4468. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  4469. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  4470. const int nwarps = NWARPS_Q6_K_RDNA1;
  4471. #endif // defined(RDNA3) || defined(RDNA2)
  4472. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4473. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4474. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4475. #elif __CUDA_ARCH__ >= CC_VOLTA
  4476. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  4477. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  4478. const int nwarps = NWARPS_Q6_K_AMPERE;
  4479. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4480. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4481. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4482. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4483. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  4484. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  4485. const int nwarps = NWARPS_Q6_K_PASCAL;
  4486. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4487. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4488. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4489. #else
  4490. (void) vec_dot_q6_K_q8_1_mul_mat;
  4491. NO_DEVICE_CODE;
  4492. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4493. }
  4494. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  4495. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4496. // tell the compiler to use as many registers as it wants, see nwarps definition below
  4497. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  4498. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4499. static __global__ void mul_mat_vec_q(
  4500. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4501. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  4502. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  4503. constexpr int nwarps = 1;
  4504. constexpr int rows_per_cuda_block = 1;
  4505. #else
  4506. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  4507. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  4508. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  4509. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  4510. const int row0 = rows_per_cuda_block*blockIdx.x;
  4511. const int blocks_per_row_x = ncols_x / qk;
  4512. const int blocks_per_col_y = nrows_y / QK8_1;
  4513. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  4514. // partial sum for each thread
  4515. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  4516. const block_q_t * x = (const block_q_t *) vx;
  4517. const block_q8_1 * y = (const block_q8_1 *) vy;
  4518. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  4519. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  4520. // x block quant index when casting the quants to int
  4521. const int kqs = vdr * (tid % (qi/vdr));
  4522. #pragma unroll
  4523. for (int j = 0; j < ncols_y; ++j) {
  4524. #pragma unroll
  4525. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4526. tmp[j][i] += vec_dot_q_cuda(
  4527. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  4528. }
  4529. }
  4530. }
  4531. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  4532. if (threadIdx.y > 0) {
  4533. #pragma unroll
  4534. for (int j = 0; j < ncols_y; ++j) {
  4535. #pragma unroll
  4536. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4537. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  4538. }
  4539. }
  4540. }
  4541. __syncthreads();
  4542. if (threadIdx.y > 0) {
  4543. return;
  4544. }
  4545. // sum up partial sums and write back result
  4546. #pragma unroll
  4547. for (int j = 0; j < ncols_y; ++j) {
  4548. #pragma unroll
  4549. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4550. #pragma unroll
  4551. for (int l = 0; l < nwarps-1; ++l) {
  4552. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  4553. }
  4554. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  4555. }
  4556. if (threadIdx.x < rows_per_cuda_block) {
  4557. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  4558. }
  4559. }
  4560. }
  4561. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  4562. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  4563. // qk = quantized weights per x block
  4564. // qr = number of quantized weights per data value in x block
  4565. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4566. if (row >= nrows) {
  4567. return;
  4568. }
  4569. const int tid = threadIdx.x;
  4570. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  4571. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  4572. const int y_offset = qr == 1 ? 1 : qk/2;
  4573. // partial sum for each thread
  4574. #ifdef GGML_CUDA_F16
  4575. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  4576. #else
  4577. float tmp = 0.0f;
  4578. #endif // GGML_CUDA_F16
  4579. for (int i = 0; i < ncols; i += iter_stride) {
  4580. const int col = i + vals_per_iter*tid;
  4581. const int ib = (row*ncols + col)/qk; // x block index
  4582. const int iqs = (col%qk)/qr; // x quant index
  4583. const int iybs = col - col%qk; // y block start index
  4584. // processing >2 values per i iter is faster for fast GPUs
  4585. #pragma unroll
  4586. for (int j = 0; j < vals_per_iter; j += 2) {
  4587. // process 2 vals per j iter
  4588. // dequantize
  4589. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  4590. dfloat2 v;
  4591. dequantize_kernel(vx, ib, iqs + j/qr, v);
  4592. // matrix multiplication
  4593. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  4594. #ifdef GGML_CUDA_F16
  4595. tmp += __hmul2(v, {
  4596. y[iybs + iqs + j/qr + 0],
  4597. y[iybs + iqs + j/qr + y_offset]
  4598. });
  4599. #else
  4600. tmp += v.x * y[iybs + iqs + j/qr + 0];
  4601. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  4602. #endif // GGML_CUDA_F16
  4603. }
  4604. }
  4605. // sum up partial sums and write back result
  4606. #pragma unroll
  4607. for (int mask = 16; mask > 0; mask >>= 1) {
  4608. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4609. }
  4610. if (tid == 0) {
  4611. #ifdef GGML_CUDA_F16
  4612. dst[row] = tmp.x + tmp.y;
  4613. #else
  4614. dst[row] = tmp;
  4615. #endif // GGML_CUDA_F16
  4616. }
  4617. }
  4618. static __global__ void mul_mat_p021_f16_f32(
  4619. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  4620. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  4621. const half * x = (const half *) vx;
  4622. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4623. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4624. const int channel_x = channel / (nchannels_y / nchannels_x);
  4625. const int nrows_y = ncols_x;
  4626. const int nrows_dst = nrows_x;
  4627. const int row_dst = row_x;
  4628. float tmp = 0.0f;
  4629. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4630. const int col_x = col_x0 + threadIdx.x;
  4631. if (col_x >= ncols_x) {
  4632. break;
  4633. }
  4634. // x is transposed and permuted
  4635. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  4636. const float xi = __half2float(x[ix]);
  4637. const int row_y = col_x;
  4638. // y is not transposed but permuted
  4639. const int iy = channel*nrows_y + row_y;
  4640. tmp += xi * y[iy];
  4641. }
  4642. // dst is not transposed and not permuted
  4643. const int idst = channel*nrows_dst + row_dst;
  4644. // sum up partial sums and write back result
  4645. #pragma unroll
  4646. for (int mask = 16; mask > 0; mask >>= 1) {
  4647. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4648. }
  4649. if (threadIdx.x == 0) {
  4650. dst[idst] = tmp;
  4651. }
  4652. }
  4653. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  4654. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  4655. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  4656. const half * x = (const half *) vx;
  4657. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4658. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4659. const int channel_x = channel / channel_x_divisor;
  4660. const int nrows_y = ncols_x;
  4661. const int nrows_dst = nrows_x;
  4662. const int row_dst = row_x;
  4663. const int idst = channel*nrows_dst + row_dst;
  4664. float tmp = 0.0f;
  4665. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4666. const int col_x = col_x0 + threadIdx.x;
  4667. if (col_x >= ncols_x) {
  4668. break;
  4669. }
  4670. const int row_y = col_x;
  4671. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  4672. const int iy = channel*nrows_y + row_y;
  4673. const float xi = __half2float(x[ix]);
  4674. tmp += xi * y[iy];
  4675. }
  4676. // sum up partial sums and write back result
  4677. #pragma unroll
  4678. for (int mask = 16; mask > 0; mask >>= 1) {
  4679. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4680. }
  4681. if (threadIdx.x == 0) {
  4682. dst[idst] = tmp;
  4683. }
  4684. }
  4685. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4686. const float * xi = (const float *) cxi;
  4687. float * dsti = (float *) cdsti;
  4688. *dsti = *xi;
  4689. }
  4690. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4691. const float * xi = (const float *) cxi;
  4692. half * dsti = (half *) cdsti;
  4693. *dsti = __float2half(*xi);
  4694. }
  4695. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4696. const half * xi = (const half *) cxi;
  4697. half * dsti = (half *) cdsti;
  4698. *dsti = *xi;
  4699. }
  4700. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  4701. const half * xi = (const half *) cxi;
  4702. float * dsti = (float *) cdsti;
  4703. *dsti = *xi;
  4704. }
  4705. template <cpy_kernel_t cpy_1>
  4706. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4707. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4708. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4709. const int nb12, const int nb13) {
  4710. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4711. if (i >= ne) {
  4712. return;
  4713. }
  4714. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4715. // then combine those indices with the corresponding byte offsets to get the total offsets
  4716. const int i03 = i/(ne00 * ne01 * ne02);
  4717. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4718. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4719. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4720. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4721. const int i13 = i/(ne10 * ne11 * ne12);
  4722. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4723. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4724. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4725. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  4726. cpy_1(cx + x_offset, cdst + dst_offset);
  4727. }
  4728. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4729. const float * xi = (const float *) cxi;
  4730. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4731. float amax = 0.0f; // absolute max
  4732. for (int j = 0; j < QK8_0; j++) {
  4733. const float v = xi[j];
  4734. amax = fmaxf(amax, fabsf(v));
  4735. }
  4736. const float d = amax / ((1 << 7) - 1);
  4737. const float id = d ? 1.0f/d : 0.0f;
  4738. dsti->d = d;
  4739. for (int j = 0; j < QK8_0; ++j) {
  4740. const float x0 = xi[j]*id;
  4741. dsti->qs[j] = roundf(x0);
  4742. }
  4743. }
  4744. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4745. const float * xi = (const float *) cxi;
  4746. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4747. float amax = 0.0f;
  4748. float vmax = 0.0f;
  4749. for (int j = 0; j < QK4_0; ++j) {
  4750. const float v = xi[j];
  4751. if (amax < fabsf(v)) {
  4752. amax = fabsf(v);
  4753. vmax = v;
  4754. }
  4755. }
  4756. const float d = vmax / -8;
  4757. const float id = d ? 1.0f/d : 0.0f;
  4758. dsti->d = d;
  4759. for (int j = 0; j < QK4_0/2; ++j) {
  4760. const float x0 = xi[0 + j]*id;
  4761. const float x1 = xi[QK4_0/2 + j]*id;
  4762. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4763. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4764. dsti->qs[j] = xi0;
  4765. dsti->qs[j] |= xi1 << 4;
  4766. }
  4767. }
  4768. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4769. const float * xi = (const float *) cxi;
  4770. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4771. float vmin = FLT_MAX;
  4772. float vmax = -FLT_MAX;
  4773. for (int j = 0; j < QK4_1; ++j) {
  4774. const float v = xi[j];
  4775. if (v < vmin) vmin = v;
  4776. if (v > vmax) vmax = v;
  4777. }
  4778. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4779. const float id = d ? 1.0f/d : 0.0f;
  4780. dsti->dm.x = d;
  4781. dsti->dm.y = vmin;
  4782. for (int j = 0; j < QK4_1/2; ++j) {
  4783. const float x0 = (xi[0 + j] - vmin)*id;
  4784. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4785. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4786. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4787. dsti->qs[j] = xi0;
  4788. dsti->qs[j] |= xi1 << 4;
  4789. }
  4790. }
  4791. template <cpy_kernel_t cpy_blck, int qk>
  4792. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4793. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4794. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4795. const int nb12, const int nb13) {
  4796. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4797. if (i >= ne) {
  4798. return;
  4799. }
  4800. const int i03 = i/(ne00 * ne01 * ne02);
  4801. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4802. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4803. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4804. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4805. const int i13 = i/(ne10 * ne11 * ne12);
  4806. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4807. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4808. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4809. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  4810. cpy_blck(cx + x_offset, cdst + dst_offset);
  4811. }
  4812. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4813. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4814. return 1.0f - min(1.0f, max(0.0f, y));
  4815. }
  4816. struct rope_corr_dims {
  4817. float v[4];
  4818. };
  4819. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4820. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4821. static __device__ void rope_yarn(
  4822. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4823. float * cos_theta, float * sin_theta
  4824. ) {
  4825. // Get n-d rotational scaling corrected for extrapolation
  4826. float theta_interp = freq_scale * theta_extrap;
  4827. float theta = theta_interp;
  4828. if (ext_factor != 0.0f) {
  4829. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4830. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4831. // Get n-d magnitude scaling corrected for interpolation
  4832. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4833. }
  4834. *cos_theta = cosf(theta) * mscale;
  4835. *sin_theta = sinf(theta) * mscale;
  4836. }
  4837. // rope == RoPE == rotary positional embedding
  4838. template<typename T, bool has_pos>
  4839. static __global__ void rope(
  4840. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4841. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4842. ) {
  4843. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4844. if (col >= ncols) {
  4845. return;
  4846. }
  4847. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4848. const int i = row*ncols + col;
  4849. const int i2 = row/p_delta_rows;
  4850. const int p = has_pos ? pos[i2] : 0;
  4851. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4852. float cos_theta, sin_theta;
  4853. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4854. const float x0 = x[i + 0];
  4855. const float x1 = x[i + 1];
  4856. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4857. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4858. }
  4859. template<typename T, bool has_pos>
  4860. static __global__ void rope_neox(
  4861. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4862. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4863. ) {
  4864. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4865. if (col >= ncols) {
  4866. return;
  4867. }
  4868. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4869. const int ib = col / n_dims;
  4870. const int ic = col % n_dims;
  4871. if (ib > 0) {
  4872. const int i = row*ncols + ib*n_dims + ic;
  4873. dst[i + 0] = x[i + 0];
  4874. dst[i + 1] = x[i + 1];
  4875. return;
  4876. }
  4877. const int i = row*ncols + ib*n_dims + ic/2;
  4878. const int i2 = row/p_delta_rows;
  4879. float cur_rot = inv_ndims * ic - ib;
  4880. const int p = has_pos ? pos[i2] : 0;
  4881. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4882. float cos_theta, sin_theta;
  4883. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4884. const float x0 = x[i + 0];
  4885. const float x1 = x[i + n_dims/2];
  4886. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4887. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4888. }
  4889. static __global__ void rope_glm_f32(
  4890. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4891. int n_ctx
  4892. ) {
  4893. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4894. const int half_n_dims = ncols/4;
  4895. if (col >= half_n_dims) {
  4896. return;
  4897. }
  4898. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4899. const int i = row*ncols + col;
  4900. const int i2 = row/p_delta_rows;
  4901. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4902. // FIXME: this is likely wrong
  4903. const int p = pos != nullptr ? pos[i2] : 0;
  4904. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4905. const float sin_theta = sinf(theta);
  4906. const float cos_theta = cosf(theta);
  4907. const float x0 = x[i + 0];
  4908. const float x1 = x[i + half_n_dims];
  4909. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4910. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4911. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4912. const float sin_block_theta = sinf(block_theta);
  4913. const float cos_block_theta = cosf(block_theta);
  4914. const float x2 = x[i + half_n_dims * 2];
  4915. const float x3 = x[i + half_n_dims * 3];
  4916. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4917. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4918. }
  4919. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4920. const int n_heads_log2_floor, const float m0, const float m1) {
  4921. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4922. if (col >= ncols) {
  4923. return;
  4924. }
  4925. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4926. const int i = row*ncols + col;
  4927. const int k = row/k_rows;
  4928. float m_k;
  4929. if (k < n_heads_log2_floor) {
  4930. m_k = powf(m0, k + 1);
  4931. } else {
  4932. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4933. }
  4934. dst[i] = col * m_k + x[i];
  4935. }
  4936. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4937. const int row = blockIdx.x;
  4938. const int col = threadIdx.x;
  4939. float sum = 0.0f;
  4940. for (int i = col; i < ncols; i += blockDim.x) {
  4941. sum += x[row * ncols + i];
  4942. }
  4943. sum = warp_reduce_sum(sum);
  4944. if (col == 0) {
  4945. dst[row] = sum;
  4946. }
  4947. }
  4948. template<typename T>
  4949. static inline __device__ void swap(T & a, T & b) {
  4950. T tmp = a;
  4951. a = b;
  4952. b = tmp;
  4953. }
  4954. template<ggml_sort_order order>
  4955. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4956. // bitonic sort
  4957. int col = threadIdx.x;
  4958. int row = blockIdx.y;
  4959. if (col >= ncols) return;
  4960. const float * x_row = x + row * ncols;
  4961. int * dst_row = dst + row * ncols;
  4962. // initialize indices
  4963. if (col < ncols) {
  4964. dst_row[col] = col;
  4965. }
  4966. __syncthreads();
  4967. for (int k = 2; k <= ncols; k *= 2) {
  4968. for (int j = k / 2; j > 0; j /= 2) {
  4969. int ixj = col ^ j;
  4970. if (ixj > col) {
  4971. if ((col & k) == 0) {
  4972. 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]]) {
  4973. swap(dst_row[col], dst_row[ixj]);
  4974. }
  4975. } else {
  4976. 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]]) {
  4977. swap(dst_row[col], dst_row[ixj]);
  4978. }
  4979. }
  4980. }
  4981. __syncthreads();
  4982. }
  4983. }
  4984. }
  4985. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4986. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4987. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4988. if (col >= ncols) {
  4989. return;
  4990. }
  4991. const int i = row*ncols + col;
  4992. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4993. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4994. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4995. }
  4996. template <bool vals_smem, int ncols_template, int block_size_template>
  4997. static __global__ void soft_max_f32(const float * x, const float * mask, const float * pos, float * dst, const int ncols_par, const int nrows_y, const float scale, const float max_bias, const float m0, const float m1, uint32_t n_head_log2) {
  4998. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  4999. const int tid = threadIdx.x;
  5000. const int rowx = blockIdx.x;
  5001. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  5002. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  5003. const int warp_id = threadIdx.x / WARP_SIZE;
  5004. const int lane_id = threadIdx.x % WARP_SIZE;
  5005. float slope = 0.0f;
  5006. // ALiBi
  5007. if (max_bias > 0.0f) {
  5008. const int h = rowx/nrows_y; // head index
  5009. const float base = h < n_head_log2 ? m0 : m1;
  5010. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  5011. slope = powf(base, exp);
  5012. }
  5013. extern __shared__ float data_soft_max_f32[];
  5014. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  5015. // shared memory buffer to cache values between iterations:
  5016. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  5017. float max_val = -INFINITY;
  5018. #pragma unroll
  5019. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5020. const int col = col0 + tid;
  5021. if (ncols_template == 0 && col >= ncols) {
  5022. break;
  5023. }
  5024. const int ix = rowx*ncols + col;
  5025. const int iy = rowy*ncols + col;
  5026. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  5027. vals[col] = val;
  5028. max_val = max(max_val, val);
  5029. }
  5030. // find the max value in the block
  5031. max_val = warp_reduce_max(max_val);
  5032. if (block_size > WARP_SIZE) {
  5033. if (warp_id == 0) {
  5034. buf_iw[lane_id] = -INFINITY;
  5035. }
  5036. __syncthreads();
  5037. if (lane_id == 0) {
  5038. buf_iw[warp_id] = max_val;
  5039. }
  5040. __syncthreads();
  5041. max_val = buf_iw[lane_id];
  5042. max_val = warp_reduce_max(max_val);
  5043. }
  5044. float tmp = 0.0f; // partial sum
  5045. #pragma unroll
  5046. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5047. const int col = col0 + tid;
  5048. if (ncols_template == 0 && col >= ncols) {
  5049. break;
  5050. }
  5051. const float val = expf(vals[col] - max_val);
  5052. tmp += val;
  5053. vals[col] = val;
  5054. }
  5055. // find the sum of exps in the block
  5056. tmp = warp_reduce_sum(tmp);
  5057. if (block_size > WARP_SIZE) {
  5058. if (warp_id == 0) {
  5059. buf_iw[lane_id] = 0.0f;
  5060. }
  5061. __syncthreads();
  5062. if (lane_id == 0) {
  5063. buf_iw[warp_id] = tmp;
  5064. }
  5065. __syncthreads();
  5066. tmp = buf_iw[lane_id];
  5067. tmp = warp_reduce_sum(tmp);
  5068. }
  5069. const float inv_sum = 1.0f / tmp;
  5070. #pragma unroll
  5071. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5072. const int col = col0 + tid;
  5073. if (ncols_template == 0 && col >= ncols) {
  5074. return;
  5075. }
  5076. const int idst = rowx*ncols + col;
  5077. dst[idst] = vals[col] * inv_sum;
  5078. }
  5079. }
  5080. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5081. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5082. if (i >= k) {
  5083. return;
  5084. }
  5085. dst[i] = scale * x[i];
  5086. }
  5087. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5088. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5089. if (i >= k) {
  5090. return;
  5091. }
  5092. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5093. }
  5094. template <typename T>
  5095. static __global__ void im2col_kernel(
  5096. const float * x, T * dst, int batch_offset,
  5097. int offset_delta, int IC, int IW, int IH, int OH, int OW, int KW, int KH, int pelements, int CHW,
  5098. int s0, int s1, int p0, int p1, int d0, int d1) {
  5099. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  5100. if (i >= pelements) {
  5101. return;
  5102. }
  5103. const int ksize = OW * (KH > 1 ? KW : 1);
  5104. const int kx = i / ksize;
  5105. const int kd = kx * ksize;
  5106. const int ky = (i - kd) / OW;
  5107. const int ix = i % OW;
  5108. const int oh = blockIdx.y;
  5109. const int batch = blockIdx.z / IC;
  5110. const int ic = blockIdx.z % IC;
  5111. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5112. const int64_t iih = oh * s1 + ky * d1 - p1;
  5113. const int64_t offset_dst =
  5114. ((batch * OH + oh) * OW + ix) * CHW +
  5115. (ic * (KW * KH) + ky * KW + kx);
  5116. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5117. dst[offset_dst] = 0.0f;
  5118. } else {
  5119. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5120. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5121. }
  5122. }
  5123. template <typename Ti, typename To>
  5124. static __global__ void pool2d_nchw_kernel(
  5125. const int ih, const int iw, const int oh, const int ow,
  5126. const int kh, const int kw, const int sh, const int sw,
  5127. const int ph, const int pw, const int parallel_elements,
  5128. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5129. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5130. if (idx >= parallel_elements) {
  5131. return;
  5132. }
  5133. const int I_HW = ih * iw;
  5134. const int O_HW = oh * ow;
  5135. const int nc = idx / O_HW;
  5136. const int cur_oh = idx % O_HW / ow;
  5137. const int cur_ow = idx % O_HW % ow;
  5138. const Ti* i_ptr = src + nc * I_HW;
  5139. To* o_ptr = dst + nc * O_HW;
  5140. const int start_h = cur_oh * sh - ph;
  5141. const int bh = max(0, start_h);
  5142. const int eh = min(ih, start_h + kh);
  5143. const int start_w = cur_ow * sw - pw;
  5144. const int bw = max(0, start_w);
  5145. const int ew = min(iw, start_w + kw);
  5146. const To scale = 1. / (kh * kw);
  5147. To res = 0;
  5148. switch (op) {
  5149. case GGML_OP_POOL_AVG: res = 0; break;
  5150. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5151. }
  5152. for (int i = bh; i < eh; i += 1) {
  5153. for (int j = bw; j < ew; j += 1) {
  5154. #if __CUDA_ARCH__ >= 350
  5155. Ti cur = __ldg(i_ptr + i * iw + j);
  5156. #else
  5157. Ti cur = i_ptr[i * iw + j];
  5158. #endif
  5159. switch (op) {
  5160. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5161. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5162. }
  5163. }
  5164. }
  5165. o_ptr[cur_oh * ow + cur_ow] = res;
  5166. }
  5167. template<int qk, int qr, dequantize_kernel_t dq>
  5168. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5169. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5170. GGML_TENSOR_BINARY_OP_LOCALS
  5171. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5172. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5173. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5174. // strides in elements
  5175. //const size_t s0 = nb0 / ggml_element_size(dst);
  5176. const size_t s1 = nb1 / ggml_element_size(dst);
  5177. const size_t s2 = nb2 / ggml_element_size(dst);
  5178. const size_t s3 = nb3 / ggml_element_size(dst);
  5179. const size_t s10 = nb10 / ggml_element_size(src1);
  5180. const size_t s11 = nb11 / ggml_element_size(src1);
  5181. const size_t s12 = nb12 / ggml_element_size(src1);
  5182. //const size_t s13 = nb13 / ggml_element_size(src1);
  5183. GGML_ASSERT(ne00 % 2 == 0);
  5184. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5185. src0_dd, src1_dd, dst_dd,
  5186. ne00, /*ne01, ne02, ne03,*/
  5187. /*ne10, ne11,*/ ne12, /*ne13,*/
  5188. /* s0,*/ s1, s2, s3,
  5189. /* nb00,*/ nb01, nb02, nb03,
  5190. s10, s11, s12/*, s13*/);
  5191. (void) dst;
  5192. }
  5193. template<typename src0_t>
  5194. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5195. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5196. GGML_TENSOR_BINARY_OP_LOCALS
  5197. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5198. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5199. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5200. // strides in elements
  5201. //const size_t s0 = nb0 / ggml_element_size(dst);
  5202. const size_t s1 = nb1 / ggml_element_size(dst);
  5203. const size_t s2 = nb2 / ggml_element_size(dst);
  5204. const size_t s3 = nb3 / ggml_element_size(dst);
  5205. const size_t s10 = nb10 / ggml_element_size(src1);
  5206. const size_t s11 = nb11 / ggml_element_size(src1);
  5207. const size_t s12 = nb12 / ggml_element_size(src1);
  5208. //const size_t s13 = nb13 / ggml_element_size(src1);
  5209. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5210. src0_dd, src1_dd, dst_dd,
  5211. ne00, /*ne01, ne02, ne03,*/
  5212. /*ne10, ne11,*/ ne12, /*ne13,*/
  5213. /* s0,*/ s1, s2, s3,
  5214. /* nb00,*/ nb01, nb02, nb03,
  5215. s10, s11, s12/*, s13*/);
  5216. (void) dst;
  5217. }
  5218. template<float (*bin_op)(const float, const float)>
  5219. struct bin_bcast_cuda {
  5220. template<typename src0_t, typename src1_t, typename dst_t>
  5221. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5222. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5223. cudaStream_t stream) {
  5224. GGML_TENSOR_BINARY_OP_LOCALS
  5225. int nr0 = ne10/ne0;
  5226. int nr1 = ne11/ne1;
  5227. int nr2 = ne12/ne2;
  5228. int nr3 = ne13/ne3;
  5229. int nr[4] = { nr0, nr1, nr2, nr3 };
  5230. // collapse dimensions until first broadcast dimension
  5231. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5232. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5233. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5234. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5235. auto collapse = [](int64_t cne[]) {
  5236. cne[0] *= cne[1];
  5237. cne[1] = cne[2];
  5238. cne[2] = cne[3];
  5239. cne[3] = 1;
  5240. };
  5241. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5242. cnb[1] *= cne[1];
  5243. cnb[2] *= cne[2];
  5244. cnb[3] *= cne[3];
  5245. };
  5246. for (int i = 0; i < 4; i++) {
  5247. if (nr[i] != 1) {
  5248. break;
  5249. }
  5250. if (i > 0) {
  5251. collapse_nb(cnb0, cne0);
  5252. collapse_nb(cnb1, cne1);
  5253. collapse(cne0);
  5254. collapse(cne1);
  5255. }
  5256. }
  5257. {
  5258. int64_t ne0 = cne0[0];
  5259. int64_t ne1 = cne0[1];
  5260. int64_t ne2 = cne0[2];
  5261. int64_t ne3 = cne0[3];
  5262. int64_t ne10 = cne1[0];
  5263. int64_t ne11 = cne1[1];
  5264. int64_t ne12 = cne1[2];
  5265. int64_t ne13 = cne1[3];
  5266. size_t nb0 = cnb0[0];
  5267. size_t nb1 = cnb0[1];
  5268. size_t nb2 = cnb0[2];
  5269. size_t nb3 = cnb0[3];
  5270. size_t nb10 = cnb1[0];
  5271. size_t nb11 = cnb1[1];
  5272. size_t nb12 = cnb1[2];
  5273. size_t nb13 = cnb1[3];
  5274. size_t s0 = nb0 / sizeof(dst_t);
  5275. size_t s1 = nb1 / sizeof(dst_t);
  5276. size_t s2 = nb2 / sizeof(dst_t);
  5277. size_t s3 = nb3 / sizeof(dst_t);
  5278. size_t s10 = nb10 / sizeof(src1_t);
  5279. size_t s11 = nb11 / sizeof(src1_t);
  5280. size_t s12 = nb12 / sizeof(src1_t);
  5281. size_t s13 = nb13 / sizeof(src1_t);
  5282. GGML_ASSERT(s0 == 1);
  5283. GGML_ASSERT(s10 == 1);
  5284. const int block_size = 128;
  5285. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5286. dim3 block_dims;
  5287. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5288. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5289. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5290. dim3 block_nums(
  5291. (hne0 + block_dims.x - 1) / block_dims.x,
  5292. (ne1 + block_dims.y - 1) / block_dims.y,
  5293. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5294. );
  5295. if (block_nums.z > 65535) {
  5296. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5297. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5298. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5299. src0_dd, src1_dd, dst_dd,
  5300. ne0, ne1, ne2, ne3,
  5301. ne10, ne11, ne12, ne13,
  5302. /* s0, */ s1, s2, s3,
  5303. /* s10, */ s11, s12, s13);
  5304. } else {
  5305. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5306. src0_dd, src1_dd, dst_dd,
  5307. ne0, ne1, ne2, ne3,
  5308. ne10, ne11, ne12, ne13,
  5309. /* s0, */ s1, s2, s3,
  5310. /* s10, */ s11, s12, s13);
  5311. }
  5312. }
  5313. }
  5314. };
  5315. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5316. const int ne10, const int ne11, const int ne12,
  5317. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5318. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5319. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5320. }
  5321. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5322. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5323. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5324. }
  5325. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5326. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5327. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5328. }
  5329. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5330. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5331. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5332. }
  5333. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5334. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5335. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5336. }
  5337. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5338. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5339. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5340. }
  5341. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5342. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5343. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5344. }
  5345. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5346. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5347. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5348. }
  5349. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5350. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5351. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5352. }
  5353. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5354. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5355. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5356. }
  5357. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5358. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5359. if (ncols < 1024) {
  5360. const dim3 block_dims(WARP_SIZE, 1, 1);
  5361. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5362. } else {
  5363. const dim3 block_dims(1024, 1, 1);
  5364. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5365. }
  5366. }
  5367. 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) {
  5368. static const float eps = 1e-6f;
  5369. if (group_size < 1024) {
  5370. const dim3 block_dims(WARP_SIZE, 1, 1);
  5371. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5372. } else {
  5373. const dim3 block_dims(1024, 1, 1);
  5374. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5375. }
  5376. }
  5377. 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) {
  5378. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5379. dim3 gridDim(num_blocks, ne1, ne2);
  5380. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5381. }
  5382. 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) {
  5383. int ne0 = (ne00 * scale_factor);
  5384. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5385. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  5386. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  5387. }
  5388. static void pad_f32_cuda(const float * x, float * dst,
  5389. const int ne00, const int ne01, const int ne02,
  5390. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  5391. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  5392. dim3 gridDim(num_blocks, ne1, ne2);
  5393. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  5394. }
  5395. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5396. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5397. if (ncols < 1024) {
  5398. const dim3 block_dims(WARP_SIZE, 1, 1);
  5399. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5400. } else {
  5401. const dim3 block_dims(1024, 1, 1);
  5402. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5403. }
  5404. }
  5405. 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) {
  5406. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  5407. const dim3 num_blocks(block_num_x, ky, 1);
  5408. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  5409. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  5410. }
  5411. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5412. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5413. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  5414. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5415. }
  5416. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  5417. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  5418. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  5419. const bool need_check = false;
  5420. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5421. } else {
  5422. const bool need_check = true;
  5423. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5424. }
  5425. }
  5426. template<typename dst_t>
  5427. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5428. const int nb = k / QK_K;
  5429. #if QK_K == 256
  5430. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  5431. #else
  5432. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  5433. #endif
  5434. }
  5435. template<typename dst_t>
  5436. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5437. const int nb = k / QK_K;
  5438. #if QK_K == 256
  5439. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  5440. #else
  5441. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  5442. #endif
  5443. }
  5444. template<typename dst_t>
  5445. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5446. const int nb32 = k / 32;
  5447. const int nb = (k + 255) / 256;
  5448. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5449. }
  5450. template<typename dst_t>
  5451. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5452. const int nb32 = k / 32;
  5453. const int nb = (k + 255) / 256;
  5454. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5455. }
  5456. template<typename dst_t>
  5457. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5458. const int nb = k / QK_K;
  5459. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  5460. }
  5461. template<typename dst_t>
  5462. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5463. const int nb = k / QK_K;
  5464. #if QK_K == 256
  5465. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  5466. #else
  5467. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  5468. #endif
  5469. }
  5470. template<typename dst_t>
  5471. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5472. const int nb = k / QK_K;
  5473. #if QK_K == 256
  5474. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  5475. #else
  5476. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  5477. #endif
  5478. }
  5479. template<typename dst_t>
  5480. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5481. const int nb = k / QK_K;
  5482. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5483. }
  5484. template<typename dst_t>
  5485. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5486. const int nb = k / QK_K;
  5487. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  5488. }
  5489. template<typename dst_t>
  5490. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5491. const int nb = k / QK_K;
  5492. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5493. }
  5494. template<typename dst_t>
  5495. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5496. const int nb = k / QK_K;
  5497. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  5498. }
  5499. template <typename src_t, typename dst_t>
  5500. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5501. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  5502. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5503. }
  5504. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  5505. int id;
  5506. switch (type) {
  5507. case GGML_TYPE_Q4_0:
  5508. return dequantize_row_q4_0_cuda;
  5509. case GGML_TYPE_Q4_1:
  5510. return dequantize_row_q4_1_cuda;
  5511. case GGML_TYPE_Q5_0:
  5512. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5513. case GGML_TYPE_Q5_1:
  5514. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5515. case GGML_TYPE_Q8_0:
  5516. CUDA_CHECK(cudaGetDevice(&id));
  5517. if (g_device_caps[id].cc >= CC_PASCAL) {
  5518. return dequantize_block_q8_0_f16_cuda;
  5519. }
  5520. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5521. case GGML_TYPE_Q2_K:
  5522. return dequantize_row_q2_K_cuda;
  5523. case GGML_TYPE_Q3_K:
  5524. return dequantize_row_q3_K_cuda;
  5525. case GGML_TYPE_Q4_K:
  5526. return dequantize_row_q4_K_cuda;
  5527. case GGML_TYPE_Q5_K:
  5528. return dequantize_row_q5_K_cuda;
  5529. case GGML_TYPE_Q6_K:
  5530. return dequantize_row_q6_K_cuda;
  5531. case GGML_TYPE_IQ2_XXS:
  5532. return dequantize_row_iq2_xxs_cuda;
  5533. case GGML_TYPE_IQ2_XS:
  5534. return dequantize_row_iq2_xs_cuda;
  5535. case GGML_TYPE_IQ3_XXS:
  5536. return dequantize_row_iq3_xxs_cuda;
  5537. case GGML_TYPE_IQ1_S:
  5538. return dequantize_row_iq1_s_cuda;
  5539. case GGML_TYPE_F32:
  5540. return convert_unary_cuda<float>;
  5541. default:
  5542. return nullptr;
  5543. }
  5544. }
  5545. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  5546. switch (type) {
  5547. case GGML_TYPE_Q4_0:
  5548. return dequantize_row_q4_0_cuda;
  5549. case GGML_TYPE_Q4_1:
  5550. return dequantize_row_q4_1_cuda;
  5551. case GGML_TYPE_Q5_0:
  5552. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5553. case GGML_TYPE_Q5_1:
  5554. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5555. case GGML_TYPE_Q8_0:
  5556. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5557. case GGML_TYPE_Q2_K:
  5558. return dequantize_row_q2_K_cuda;
  5559. case GGML_TYPE_Q3_K:
  5560. return dequantize_row_q3_K_cuda;
  5561. case GGML_TYPE_Q4_K:
  5562. return dequantize_row_q4_K_cuda;
  5563. case GGML_TYPE_Q5_K:
  5564. return dequantize_row_q5_K_cuda;
  5565. case GGML_TYPE_Q6_K:
  5566. return dequantize_row_q6_K_cuda;
  5567. case GGML_TYPE_IQ2_XXS:
  5568. return dequantize_row_iq2_xxs_cuda;
  5569. case GGML_TYPE_IQ2_XS:
  5570. return dequantize_row_iq2_xs_cuda;
  5571. case GGML_TYPE_IQ3_XXS:
  5572. return dequantize_row_iq3_xxs_cuda;
  5573. case GGML_TYPE_IQ1_S:
  5574. return dequantize_row_iq1_s_cuda;
  5575. case GGML_TYPE_F16:
  5576. return convert_unary_cuda<half>;
  5577. default:
  5578. return nullptr;
  5579. }
  5580. }
  5581. 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) {
  5582. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5583. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5584. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  5585. const dim3 block_nums(block_num_y, 1, 1);
  5586. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5587. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  5588. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5589. }
  5590. 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) {
  5591. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5592. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5593. const dim3 block_nums(block_num_y, 1, 1);
  5594. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5595. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  5596. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5597. }
  5598. 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) {
  5599. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5600. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5601. const dim3 block_nums(block_num_y, 1, 1);
  5602. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5603. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  5604. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5605. }
  5606. 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) {
  5607. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5608. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5609. const dim3 block_nums(block_num_y, 1, 1);
  5610. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5611. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  5612. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5613. }
  5614. 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) {
  5615. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5616. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5617. const dim3 block_nums(block_num_y, 1, 1);
  5618. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5619. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  5620. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5621. }
  5622. 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) {
  5623. GGML_ASSERT(ncols % QK_K == 0);
  5624. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  5625. const int block_num_y = (nrows + ny - 1) / ny;
  5626. const dim3 block_nums(block_num_y, 1, 1);
  5627. const dim3 block_dims(32, ny, 1);
  5628. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5629. }
  5630. 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) {
  5631. GGML_ASSERT(ncols % QK_K == 0);
  5632. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5633. const int block_num_y = (nrows + ny - 1) / ny;
  5634. const dim3 block_nums(block_num_y, 1, 1);
  5635. const dim3 block_dims(32, ny, 1);
  5636. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5637. }
  5638. 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) {
  5639. GGML_ASSERT(ncols % QK_K == 0);
  5640. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5641. const int block_num_y = (nrows + ny - 1) / ny;
  5642. const dim3 block_nums(block_num_y, 1, 1);
  5643. const dim3 block_dims(32, ny, 1);
  5644. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5645. }
  5646. 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) {
  5647. GGML_ASSERT(ncols % QK_K == 0);
  5648. const dim3 block_dims(32, 1, 1);
  5649. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  5650. }
  5651. 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) {
  5652. GGML_ASSERT(ncols % QK_K == 0);
  5653. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5654. const int block_num_y = (nrows + ny - 1) / ny;
  5655. const dim3 block_nums(block_num_y, 1, 1);
  5656. const dim3 block_dims(32, ny, 1);
  5657. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5658. }
  5659. 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) {
  5660. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5661. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5662. const dim3 block_nums(block_num_y, 1, 1);
  5663. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5664. dequantize_mul_mat_vec<1, 1, convert_f16>
  5665. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5666. }
  5667. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  5668. static void mul_mat_vec_q_cuda(
  5669. const void * vx, const void * vy, float * dst,
  5670. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  5671. GGML_ASSERT(ncols_x % qk == 0);
  5672. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  5673. int id;
  5674. CUDA_CHECK(cudaGetDevice(&id));
  5675. int64_t nwarps = 1;
  5676. int64_t rows_per_cuda_block = 1;
  5677. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  5678. switch(ncols_y) {
  5679. case 1:
  5680. nwarps = 4;
  5681. rows_per_cuda_block = 1;
  5682. break;
  5683. case 2:
  5684. case 3:
  5685. case 4:
  5686. nwarps = 4;
  5687. rows_per_cuda_block = 2;
  5688. break;
  5689. case 5:
  5690. case 6:
  5691. case 7:
  5692. case 8:
  5693. nwarps = 2;
  5694. rows_per_cuda_block = 2;
  5695. break;
  5696. default:
  5697. GGML_ASSERT(false);
  5698. break;
  5699. }
  5700. }
  5701. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  5702. const dim3 block_nums(nblocks, 1, 1);
  5703. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5704. switch (ncols_y) {
  5705. case 1:
  5706. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  5707. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5708. break;
  5709. case 2:
  5710. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  5711. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5712. break;
  5713. case 3:
  5714. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  5715. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5716. break;
  5717. case 4:
  5718. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  5719. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5720. break;
  5721. case 5:
  5722. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  5723. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5724. break;
  5725. case 6:
  5726. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  5727. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5728. break;
  5729. case 7:
  5730. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  5731. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5732. break;
  5733. case 8:
  5734. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  5735. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5736. break;
  5737. default:
  5738. GGML_ASSERT(false);
  5739. break;
  5740. }
  5741. }
  5742. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5743. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5744. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5745. int id;
  5746. CUDA_CHECK(cudaGetDevice(&id));
  5747. const int compute_capability = g_device_caps[id].cc;
  5748. int mmq_x, mmq_y, nwarps;
  5749. if (compute_capability >= CC_RDNA2) {
  5750. mmq_x = MMQ_X_Q4_0_RDNA2;
  5751. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5752. nwarps = NWARPS_Q4_0_RDNA2;
  5753. } else if (compute_capability >= CC_OFFSET_AMD) {
  5754. mmq_x = MMQ_X_Q4_0_RDNA1;
  5755. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5756. nwarps = NWARPS_Q4_0_RDNA1;
  5757. } else if (compute_capability >= CC_VOLTA) {
  5758. mmq_x = MMQ_X_Q4_0_AMPERE;
  5759. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5760. nwarps = NWARPS_Q4_0_AMPERE;
  5761. } else if (compute_capability >= MIN_CC_DP4A) {
  5762. mmq_x = MMQ_X_Q4_0_PASCAL;
  5763. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5764. nwarps = NWARPS_Q4_0_PASCAL;
  5765. } else {
  5766. GGML_ASSERT(false);
  5767. }
  5768. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5769. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5770. const dim3 block_nums(block_num_x, block_num_y, 1);
  5771. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5772. if (nrows_x % mmq_y == 0) {
  5773. const bool need_check = false;
  5774. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5775. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5776. } else {
  5777. const bool need_check = true;
  5778. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5779. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5780. }
  5781. }
  5782. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5783. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5784. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5785. int id;
  5786. CUDA_CHECK(cudaGetDevice(&id));
  5787. const int compute_capability = g_device_caps[id].cc;
  5788. int mmq_x, mmq_y, nwarps;
  5789. if (compute_capability >= CC_RDNA2) {
  5790. mmq_x = MMQ_X_Q4_1_RDNA2;
  5791. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5792. nwarps = NWARPS_Q4_1_RDNA2;
  5793. } else if (compute_capability >= CC_OFFSET_AMD) {
  5794. mmq_x = MMQ_X_Q4_1_RDNA1;
  5795. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5796. nwarps = NWARPS_Q4_1_RDNA1;
  5797. } else if (compute_capability >= CC_VOLTA) {
  5798. mmq_x = MMQ_X_Q4_1_AMPERE;
  5799. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5800. nwarps = NWARPS_Q4_1_AMPERE;
  5801. } else if (compute_capability >= MIN_CC_DP4A) {
  5802. mmq_x = MMQ_X_Q4_1_PASCAL;
  5803. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5804. nwarps = NWARPS_Q4_1_PASCAL;
  5805. } else {
  5806. GGML_ASSERT(false);
  5807. }
  5808. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5809. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5810. const dim3 block_nums(block_num_x, block_num_y, 1);
  5811. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5812. if (nrows_x % mmq_y == 0) {
  5813. const bool need_check = false;
  5814. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5815. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5816. } else {
  5817. const bool need_check = true;
  5818. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5819. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5820. }
  5821. }
  5822. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5823. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5824. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5825. int id;
  5826. CUDA_CHECK(cudaGetDevice(&id));
  5827. const int compute_capability = g_device_caps[id].cc;
  5828. int mmq_x, mmq_y, nwarps;
  5829. if (compute_capability >= CC_RDNA2) {
  5830. mmq_x = MMQ_X_Q5_0_RDNA2;
  5831. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5832. nwarps = NWARPS_Q5_0_RDNA2;
  5833. } else if (compute_capability >= CC_OFFSET_AMD) {
  5834. mmq_x = MMQ_X_Q5_0_RDNA1;
  5835. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5836. nwarps = NWARPS_Q5_0_RDNA1;
  5837. } else if (compute_capability >= CC_VOLTA) {
  5838. mmq_x = MMQ_X_Q5_0_AMPERE;
  5839. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5840. nwarps = NWARPS_Q5_0_AMPERE;
  5841. } else if (compute_capability >= MIN_CC_DP4A) {
  5842. mmq_x = MMQ_X_Q5_0_PASCAL;
  5843. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5844. nwarps = NWARPS_Q5_0_PASCAL;
  5845. } else {
  5846. GGML_ASSERT(false);
  5847. }
  5848. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5849. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5850. const dim3 block_nums(block_num_x, block_num_y, 1);
  5851. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5852. if (nrows_x % mmq_y == 0) {
  5853. const bool need_check = false;
  5854. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5855. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5856. } else {
  5857. const bool need_check = true;
  5858. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5859. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5860. }
  5861. }
  5862. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5863. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5864. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5865. int id;
  5866. CUDA_CHECK(cudaGetDevice(&id));
  5867. const int compute_capability = g_device_caps[id].cc;
  5868. int mmq_x, mmq_y, nwarps;
  5869. if (compute_capability >= CC_RDNA2) {
  5870. mmq_x = MMQ_X_Q5_1_RDNA2;
  5871. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5872. nwarps = NWARPS_Q5_1_RDNA2;
  5873. } else if (compute_capability >= CC_OFFSET_AMD) {
  5874. mmq_x = MMQ_X_Q5_1_RDNA1;
  5875. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5876. nwarps = NWARPS_Q5_1_RDNA1;
  5877. } else if (compute_capability >= CC_VOLTA) {
  5878. mmq_x = MMQ_X_Q5_1_AMPERE;
  5879. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5880. nwarps = NWARPS_Q5_1_AMPERE;
  5881. } else if (compute_capability >= MIN_CC_DP4A) {
  5882. mmq_x = MMQ_X_Q5_1_PASCAL;
  5883. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5884. nwarps = NWARPS_Q5_1_PASCAL;
  5885. } else {
  5886. GGML_ASSERT(false);
  5887. }
  5888. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5889. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5890. const dim3 block_nums(block_num_x, block_num_y, 1);
  5891. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5892. if (nrows_x % mmq_y == 0) {
  5893. const bool need_check = false;
  5894. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5895. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5896. } else {
  5897. const bool need_check = true;
  5898. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5899. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5900. }
  5901. }
  5902. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5903. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5904. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5905. int id;
  5906. CUDA_CHECK(cudaGetDevice(&id));
  5907. const int compute_capability = g_device_caps[id].cc;
  5908. int mmq_x, mmq_y, nwarps;
  5909. if (compute_capability >= CC_RDNA2) {
  5910. mmq_x = MMQ_X_Q8_0_RDNA2;
  5911. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5912. nwarps = NWARPS_Q8_0_RDNA2;
  5913. } else if (compute_capability >= CC_OFFSET_AMD) {
  5914. mmq_x = MMQ_X_Q8_0_RDNA1;
  5915. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5916. nwarps = NWARPS_Q8_0_RDNA1;
  5917. } else if (compute_capability >= CC_VOLTA) {
  5918. mmq_x = MMQ_X_Q8_0_AMPERE;
  5919. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5920. nwarps = NWARPS_Q8_0_AMPERE;
  5921. } else if (compute_capability >= MIN_CC_DP4A) {
  5922. mmq_x = MMQ_X_Q8_0_PASCAL;
  5923. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5924. nwarps = NWARPS_Q8_0_PASCAL;
  5925. } else {
  5926. GGML_ASSERT(false);
  5927. }
  5928. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5929. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5930. const dim3 block_nums(block_num_x, block_num_y, 1);
  5931. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5932. if (nrows_x % mmq_y == 0) {
  5933. const bool need_check = false;
  5934. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5935. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5936. } else {
  5937. const bool need_check = true;
  5938. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5939. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5940. }
  5941. }
  5942. static void ggml_mul_mat_q2_K_q8_1_cuda(
  5943. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5944. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5945. int id;
  5946. CUDA_CHECK(cudaGetDevice(&id));
  5947. const int compute_capability = g_device_caps[id].cc;
  5948. int mmq_x, mmq_y, nwarps;
  5949. if (compute_capability >= CC_RDNA2) {
  5950. mmq_x = MMQ_X_Q2_K_RDNA2;
  5951. mmq_y = MMQ_Y_Q2_K_RDNA2;
  5952. nwarps = NWARPS_Q2_K_RDNA2;
  5953. } else if (compute_capability >= CC_OFFSET_AMD) {
  5954. mmq_x = MMQ_X_Q2_K_RDNA1;
  5955. mmq_y = MMQ_Y_Q2_K_RDNA1;
  5956. nwarps = NWARPS_Q2_K_RDNA1;
  5957. } else if (compute_capability >= CC_VOLTA) {
  5958. mmq_x = MMQ_X_Q2_K_AMPERE;
  5959. mmq_y = MMQ_Y_Q2_K_AMPERE;
  5960. nwarps = NWARPS_Q2_K_AMPERE;
  5961. } else if (compute_capability >= MIN_CC_DP4A) {
  5962. mmq_x = MMQ_X_Q2_K_PASCAL;
  5963. mmq_y = MMQ_Y_Q2_K_PASCAL;
  5964. nwarps = NWARPS_Q2_K_PASCAL;
  5965. } else {
  5966. GGML_ASSERT(false);
  5967. }
  5968. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5969. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5970. const dim3 block_nums(block_num_x, block_num_y, 1);
  5971. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5972. if (nrows_x % mmq_y == 0) {
  5973. const bool need_check = false;
  5974. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5975. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5976. } else {
  5977. const bool need_check = true;
  5978. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5979. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5980. }
  5981. }
  5982. static void ggml_mul_mat_q3_K_q8_1_cuda(
  5983. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5984. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5985. #if QK_K == 256
  5986. int id;
  5987. CUDA_CHECK(cudaGetDevice(&id));
  5988. const int compute_capability = g_device_caps[id].cc;
  5989. int mmq_x, mmq_y, nwarps;
  5990. if (compute_capability >= CC_RDNA2) {
  5991. mmq_x = MMQ_X_Q3_K_RDNA2;
  5992. mmq_y = MMQ_Y_Q3_K_RDNA2;
  5993. nwarps = NWARPS_Q3_K_RDNA2;
  5994. } else if (compute_capability >= CC_OFFSET_AMD) {
  5995. mmq_x = MMQ_X_Q3_K_RDNA1;
  5996. mmq_y = MMQ_Y_Q3_K_RDNA1;
  5997. nwarps = NWARPS_Q3_K_RDNA1;
  5998. } else if (compute_capability >= CC_VOLTA) {
  5999. mmq_x = MMQ_X_Q3_K_AMPERE;
  6000. mmq_y = MMQ_Y_Q3_K_AMPERE;
  6001. nwarps = NWARPS_Q3_K_AMPERE;
  6002. } else if (compute_capability >= MIN_CC_DP4A) {
  6003. mmq_x = MMQ_X_Q3_K_PASCAL;
  6004. mmq_y = MMQ_Y_Q3_K_PASCAL;
  6005. nwarps = NWARPS_Q3_K_PASCAL;
  6006. } else {
  6007. GGML_ASSERT(false);
  6008. }
  6009. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6010. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6011. const dim3 block_nums(block_num_x, block_num_y, 1);
  6012. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6013. if (nrows_x % mmq_y == 0) {
  6014. const bool need_check = false;
  6015. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6016. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6017. } else {
  6018. const bool need_check = true;
  6019. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6020. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6021. }
  6022. #endif
  6023. }
  6024. static void ggml_mul_mat_q4_K_q8_1_cuda(
  6025. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6026. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6027. int id;
  6028. CUDA_CHECK(cudaGetDevice(&id));
  6029. const int compute_capability = g_device_caps[id].cc;
  6030. int mmq_x, mmq_y, nwarps;
  6031. if (compute_capability >= CC_RDNA2) {
  6032. mmq_x = MMQ_X_Q4_K_RDNA2;
  6033. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6034. nwarps = NWARPS_Q4_K_RDNA2;
  6035. } else if (compute_capability >= CC_OFFSET_AMD) {
  6036. mmq_x = MMQ_X_Q4_K_RDNA1;
  6037. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6038. nwarps = NWARPS_Q4_K_RDNA1;
  6039. } else if (compute_capability >= CC_VOLTA) {
  6040. mmq_x = MMQ_X_Q4_K_AMPERE;
  6041. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6042. nwarps = NWARPS_Q4_K_AMPERE;
  6043. } else if (compute_capability >= MIN_CC_DP4A) {
  6044. mmq_x = MMQ_X_Q4_K_PASCAL;
  6045. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6046. nwarps = NWARPS_Q4_K_PASCAL;
  6047. } else {
  6048. GGML_ASSERT(false);
  6049. }
  6050. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6051. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6052. const dim3 block_nums(block_num_x, block_num_y, 1);
  6053. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6054. if (nrows_x % mmq_y == 0) {
  6055. const bool need_check = false;
  6056. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6057. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6058. } else {
  6059. const bool need_check = true;
  6060. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6061. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6062. }
  6063. }
  6064. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6065. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6066. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6067. int id;
  6068. CUDA_CHECK(cudaGetDevice(&id));
  6069. const int compute_capability = g_device_caps[id].cc;
  6070. int mmq_x, mmq_y, nwarps;
  6071. if (compute_capability >= CC_RDNA2) {
  6072. mmq_x = MMQ_X_Q5_K_RDNA2;
  6073. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6074. nwarps = NWARPS_Q5_K_RDNA2;
  6075. } else if (compute_capability >= CC_OFFSET_AMD) {
  6076. mmq_x = MMQ_X_Q5_K_RDNA1;
  6077. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6078. nwarps = NWARPS_Q5_K_RDNA1;
  6079. } else if (compute_capability >= CC_VOLTA) {
  6080. mmq_x = MMQ_X_Q5_K_AMPERE;
  6081. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6082. nwarps = NWARPS_Q5_K_AMPERE;
  6083. } else if (compute_capability >= MIN_CC_DP4A) {
  6084. mmq_x = MMQ_X_Q5_K_PASCAL;
  6085. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6086. nwarps = NWARPS_Q5_K_PASCAL;
  6087. } else {
  6088. GGML_ASSERT(false);
  6089. }
  6090. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6091. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6092. const dim3 block_nums(block_num_x, block_num_y, 1);
  6093. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6094. if (nrows_x % mmq_y == 0) {
  6095. const bool need_check = false;
  6096. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6097. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6098. } else {
  6099. const bool need_check = true;
  6100. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6101. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6102. }
  6103. }
  6104. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6105. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6106. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6107. int id;
  6108. CUDA_CHECK(cudaGetDevice(&id));
  6109. const int compute_capability = g_device_caps[id].cc;
  6110. int mmq_x, mmq_y, nwarps;
  6111. if (compute_capability >= CC_RDNA2) {
  6112. mmq_x = MMQ_X_Q6_K_RDNA2;
  6113. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6114. nwarps = NWARPS_Q6_K_RDNA2;
  6115. } else if (compute_capability >= CC_OFFSET_AMD) {
  6116. mmq_x = MMQ_X_Q6_K_RDNA1;
  6117. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6118. nwarps = NWARPS_Q6_K_RDNA1;
  6119. } else if (compute_capability >= CC_VOLTA) {
  6120. mmq_x = MMQ_X_Q6_K_AMPERE;
  6121. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6122. nwarps = NWARPS_Q6_K_AMPERE;
  6123. } else if (compute_capability >= MIN_CC_DP4A) {
  6124. mmq_x = MMQ_X_Q6_K_PASCAL;
  6125. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6126. nwarps = NWARPS_Q6_K_PASCAL;
  6127. } else {
  6128. GGML_ASSERT(false);
  6129. }
  6130. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6131. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6132. const dim3 block_nums(block_num_x, block_num_y, 1);
  6133. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6134. if (nrows_x % mmq_y == 0) {
  6135. const bool need_check = false;
  6136. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6137. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6138. } else {
  6139. const bool need_check = true;
  6140. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6141. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6142. }
  6143. }
  6144. static void ggml_mul_mat_p021_f16_f32_cuda(
  6145. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6146. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6147. const dim3 block_nums(1, nrows_x, nchannels_y);
  6148. const dim3 block_dims(WARP_SIZE, 1, 1);
  6149. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6150. }
  6151. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6152. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6153. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6154. const dim3 block_nums(1, nrows_x, nchannels_y);
  6155. const dim3 block_dims(WARP_SIZE, 1, 1);
  6156. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6157. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6158. }
  6159. static void ggml_cpy_f16_f32_cuda(
  6160. const char * cx, char * cdst, const int ne,
  6161. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6162. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6163. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6164. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6165. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6166. }
  6167. static void ggml_cpy_f32_f32_cuda(
  6168. const char * cx, char * cdst, const int ne,
  6169. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6170. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6171. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6172. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6173. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6174. }
  6175. static void ggml_cpy_f32_f16_cuda(
  6176. const char * cx, char * cdst, const int ne,
  6177. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6178. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6179. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6180. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6181. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6182. }
  6183. static void ggml_cpy_f32_q8_0_cuda(
  6184. const char * cx, char * cdst, const int ne,
  6185. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6186. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6187. GGML_ASSERT(ne % QK8_0 == 0);
  6188. const int num_blocks = ne / QK8_0;
  6189. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6190. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6191. }
  6192. static void ggml_cpy_f32_q4_0_cuda(
  6193. const char * cx, char * cdst, const int ne,
  6194. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6195. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6196. GGML_ASSERT(ne % QK4_0 == 0);
  6197. const int num_blocks = ne / QK4_0;
  6198. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6199. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6200. }
  6201. static void ggml_cpy_f32_q4_1_cuda(
  6202. const char * cx, char * cdst, const int ne,
  6203. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6204. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6205. GGML_ASSERT(ne % QK4_1 == 0);
  6206. const int num_blocks = ne / QK4_1;
  6207. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6208. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6209. }
  6210. static void ggml_cpy_f16_f16_cuda(
  6211. const char * cx, char * cdst, const int ne,
  6212. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6213. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6214. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6215. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6216. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6217. }
  6218. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  6219. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  6220. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  6221. }
  6222. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  6223. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  6224. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  6225. }
  6226. template<typename T>
  6227. static void rope_cuda(
  6228. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6229. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6230. ) {
  6231. GGML_ASSERT(ncols % 2 == 0);
  6232. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6233. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6234. const dim3 block_nums(nrows, num_blocks_x, 1);
  6235. if (pos == nullptr) {
  6236. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6237. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6238. );
  6239. } else {
  6240. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6241. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6242. );
  6243. }
  6244. }
  6245. template<typename T>
  6246. static void rope_neox_cuda(
  6247. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6248. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6249. ) {
  6250. GGML_ASSERT(ncols % 2 == 0);
  6251. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6252. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6253. const dim3 block_nums(nrows, num_blocks_x, 1);
  6254. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6255. const float inv_ndims = -1.0f / n_dims;
  6256. if (pos == nullptr) {
  6257. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6258. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6259. theta_scale, inv_ndims
  6260. );
  6261. } else {
  6262. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6263. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6264. theta_scale, inv_ndims
  6265. );
  6266. }
  6267. }
  6268. static void rope_glm_f32_cuda(
  6269. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6270. float freq_base, int n_ctx, cudaStream_t stream
  6271. ) {
  6272. GGML_ASSERT(ncols % 4 == 0);
  6273. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6274. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6275. const dim3 block_nums(num_blocks_x, nrows, 1);
  6276. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6277. }
  6278. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6279. const int k_rows, const int n_heads_log2_floor, const float m0,
  6280. const float m1, cudaStream_t stream) {
  6281. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6282. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6283. const dim3 block_nums(num_blocks_x, nrows, 1);
  6284. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6285. }
  6286. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6287. const dim3 block_dims(WARP_SIZE, 1, 1);
  6288. const dim3 block_nums(nrows, 1, 1);
  6289. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6290. }
  6291. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6292. // bitonic sort requires ncols to be power of 2
  6293. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6294. const dim3 block_dims(ncols, 1, 1);
  6295. const dim3 block_nums(1, nrows, 1);
  6296. if (order == GGML_SORT_ASC) {
  6297. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6298. } else if (order == GGML_SORT_DESC) {
  6299. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6300. } else {
  6301. GGML_ASSERT(false);
  6302. }
  6303. }
  6304. 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) {
  6305. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6306. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6307. const dim3 block_nums(nrows_x, block_num_x, 1);
  6308. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6309. }
  6310. static void soft_max_f32_cuda(const float * x, const float * mask, const float * pos, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, const float max_bias, cudaStream_t stream) {
  6311. int nth = WARP_SIZE;
  6312. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6313. const dim3 block_dims(nth, 1, 1);
  6314. const dim3 block_nums(nrows_x, 1, 1);
  6315. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6316. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6317. const uint32_t n_head_kv = nrows_x/nrows_y;
  6318. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6319. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6320. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6321. if (shmem < g_device_caps[g_main_device].smpb) {
  6322. switch (ncols_x) {
  6323. case 32:
  6324. soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6325. break;
  6326. case 64:
  6327. soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6328. break;
  6329. case 128:
  6330. soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6331. break;
  6332. case 256:
  6333. soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6334. break;
  6335. case 512:
  6336. soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6337. break;
  6338. case 1024:
  6339. soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6340. break;
  6341. case 2048:
  6342. soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6343. break;
  6344. case 4096:
  6345. soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6346. break;
  6347. default:
  6348. soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6349. break;
  6350. }
  6351. } else {
  6352. const size_t shmem_low = WARP_SIZE*sizeof(float);
  6353. soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6354. }
  6355. }
  6356. template <typename T>
  6357. static void im2col_cuda(const float* x, T* dst,
  6358. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  6359. int batch, int batch_offset, int offset_delta,
  6360. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  6361. const int parallel_elements = OW * KW * KH;
  6362. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  6363. dim3 block_nums(num_blocks, OH, batch * IC);
  6364. im2col_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  6365. }
  6366. // buffer pool for cuda
  6367. #define MAX_CUDA_BUFFERS 256
  6368. struct scoped_spin_lock {
  6369. std::atomic_flag& lock;
  6370. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  6371. while (lock.test_and_set(std::memory_order_acquire)) {
  6372. ; // spin
  6373. }
  6374. }
  6375. ~scoped_spin_lock() {
  6376. lock.clear(std::memory_order_release);
  6377. }
  6378. scoped_spin_lock(const scoped_spin_lock&) = delete;
  6379. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  6380. };
  6381. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  6382. // #define DEBUG_CUDA_MALLOC
  6383. struct ggml_cuda_buffer {
  6384. void * ptr = nullptr;
  6385. size_t size = 0;
  6386. };
  6387. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  6388. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  6389. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  6390. scoped_spin_lock lock(g_cuda_pool_lock);
  6391. #ifdef DEBUG_CUDA_MALLOC
  6392. int nnz = 0;
  6393. size_t max_size = 0;
  6394. #endif
  6395. size_t best_diff = 1ull << 36;
  6396. int ibest = -1;
  6397. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6398. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6399. if (b.ptr != nullptr) {
  6400. #ifdef DEBUG_CUDA_MALLOC
  6401. ++nnz;
  6402. if (b.size > max_size) max_size = b.size;
  6403. #endif
  6404. if (b.size >= size) {
  6405. size_t diff = b.size - size;
  6406. if (diff < best_diff) {
  6407. best_diff = diff;
  6408. ibest = i;
  6409. if (!best_diff) {
  6410. void * ptr = b.ptr;
  6411. *actual_size = b.size;
  6412. b.ptr = nullptr;
  6413. b.size = 0;
  6414. return ptr;
  6415. }
  6416. }
  6417. }
  6418. }
  6419. }
  6420. if (ibest >= 0) {
  6421. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  6422. void * ptr = b.ptr;
  6423. *actual_size = b.size;
  6424. b.ptr = nullptr;
  6425. b.size = 0;
  6426. return ptr;
  6427. }
  6428. void * ptr;
  6429. size_t look_ahead_size = (size_t) (1.05 * size);
  6430. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  6431. ggml_cuda_set_device(device);
  6432. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  6433. *actual_size = look_ahead_size;
  6434. g_cuda_pool_size[device] += look_ahead_size;
  6435. #ifdef DEBUG_CUDA_MALLOC
  6436. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  6437. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  6438. #endif
  6439. return ptr;
  6440. }
  6441. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  6442. scoped_spin_lock lock(g_cuda_pool_lock);
  6443. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6444. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6445. if (b.ptr == nullptr) {
  6446. b.ptr = ptr;
  6447. b.size = size;
  6448. return;
  6449. }
  6450. }
  6451. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  6452. ggml_cuda_set_device(device);
  6453. CUDA_CHECK(cudaFree(ptr));
  6454. g_cuda_pool_size[device] -= size;
  6455. }
  6456. #if !defined(GGML_USE_HIPBLAS)
  6457. // pool with virtual memory
  6458. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  6459. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  6460. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  6461. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  6462. scoped_spin_lock lock(g_cuda_pool_lock);
  6463. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  6464. const size_t alignment = 128;
  6465. size = alignment * ((size + alignment - 1) / alignment);
  6466. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  6467. if (size > avail) {
  6468. // round up to the next multiple of the granularity
  6469. size_t reserve_size = size - avail;
  6470. const size_t granularity = g_device_caps[device].vmm_granularity;
  6471. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  6472. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  6473. // allocate more physical memory
  6474. CUmemAllocationProp prop = {};
  6475. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6476. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6477. prop.location.id = device;
  6478. CUmemGenericAllocationHandle handle;
  6479. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  6480. // reserve virtual address space (if not already reserved)
  6481. if (g_cuda_pool_addr[device] == 0) {
  6482. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  6483. }
  6484. // map at the end of the pool
  6485. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  6486. // the memory allocation handle is no longer needed after mapping
  6487. CU_CHECK(cuMemRelease(handle));
  6488. // set access
  6489. CUmemAccessDesc access = {};
  6490. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6491. access.location.id = device;
  6492. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  6493. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  6494. // add to the pool
  6495. g_cuda_pool_size[device] += reserve_size;
  6496. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  6497. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  6498. // (unsigned long long) (reserve_size/1024/1024));
  6499. }
  6500. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  6501. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  6502. *actual_size = size;
  6503. g_cuda_pool_used[device] += size;
  6504. #ifdef DEBUG_CUDA_MALLOC
  6505. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  6506. #endif
  6507. return ptr;
  6508. }
  6509. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  6510. scoped_spin_lock lock(g_cuda_pool_lock);
  6511. #ifdef DEBUG_CUDA_MALLOC
  6512. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  6513. #endif
  6514. g_cuda_pool_used[device] -= size;
  6515. // all deallocations must be in reverse order of the allocations
  6516. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  6517. }
  6518. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  6519. if (g_device_caps[device].vmm) {
  6520. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  6521. } else {
  6522. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  6523. }
  6524. }
  6525. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  6526. if (g_device_caps[device].vmm) {
  6527. ggml_cuda_pool_free_vmm(device, ptr, size);
  6528. } else {
  6529. ggml_cuda_pool_free_leg(device, ptr, size);
  6530. }
  6531. }
  6532. #else
  6533. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  6534. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  6535. #endif // !defined(GGML_USE_HIPBLAS)
  6536. template<typename T>
  6537. struct cuda_pool_alloc {
  6538. int device = -1;
  6539. T * ptr = nullptr;
  6540. size_t actual_size = 0;
  6541. // size is in number of elements
  6542. T * alloc(size_t size) {
  6543. GGML_ASSERT(ptr == nullptr);
  6544. CUDA_CHECK(cudaGetDevice(&device));
  6545. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  6546. return ptr;
  6547. }
  6548. cuda_pool_alloc(size_t size) {
  6549. alloc(size);
  6550. }
  6551. ~cuda_pool_alloc() {
  6552. if (ptr != nullptr) {
  6553. ggml_cuda_pool_free(device, ptr, actual_size);
  6554. }
  6555. }
  6556. T * get() {
  6557. return ptr;
  6558. }
  6559. cuda_pool_alloc() = default;
  6560. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  6561. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  6562. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  6563. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  6564. };
  6565. static bool g_cublas_loaded = false;
  6566. GGML_CALL bool ggml_cublas_loaded(void) {
  6567. return g_cublas_loaded;
  6568. }
  6569. GGML_CALL void ggml_init_cublas() {
  6570. static bool initialized = false;
  6571. if (!initialized) {
  6572. #ifdef __HIP_PLATFORM_AMD__
  6573. // Workaround for a rocBLAS bug when using multiple graphics cards:
  6574. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  6575. rocblas_initialize();
  6576. CUDA_CHECK(cudaDeviceSynchronize());
  6577. #endif
  6578. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  6579. initialized = true;
  6580. g_cublas_loaded = false;
  6581. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  6582. return;
  6583. }
  6584. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  6585. int64_t total_vram = 0;
  6586. #if defined(GGML_CUDA_FORCE_MMQ)
  6587. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  6588. #else
  6589. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  6590. #endif
  6591. #if defined(CUDA_USE_TENSOR_CORES)
  6592. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  6593. #else
  6594. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  6595. #endif
  6596. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  6597. for (int id = 0; id < g_device_count; ++id) {
  6598. int device_vmm = 0;
  6599. #if !defined(GGML_USE_HIPBLAS)
  6600. CUdevice device;
  6601. CU_CHECK(cuDeviceGet(&device, id));
  6602. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  6603. if (device_vmm) {
  6604. CUmemAllocationProp alloc_prop = {};
  6605. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6606. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6607. alloc_prop.location.id = id;
  6608. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  6609. }
  6610. #endif // !defined(GGML_USE_HIPBLAS)
  6611. g_device_caps[id].vmm = !!device_vmm;
  6612. cudaDeviceProp prop;
  6613. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  6614. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  6615. g_default_tensor_split[id] = total_vram;
  6616. total_vram += prop.totalGlobalMem;
  6617. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6618. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  6619. #else
  6620. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  6621. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6622. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  6623. }
  6624. for (int id = 0; id < g_device_count; ++id) {
  6625. g_default_tensor_split[id] /= total_vram;
  6626. }
  6627. for (int id = 0; id < g_device_count; ++id) {
  6628. ggml_cuda_set_device(id);
  6629. // create cuda streams
  6630. for (int is = 0; is < MAX_STREAMS; ++is) {
  6631. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  6632. }
  6633. // create cublas handle
  6634. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  6635. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  6636. }
  6637. // configure logging to stdout
  6638. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  6639. initialized = true;
  6640. g_cublas_loaded = true;
  6641. }
  6642. }
  6643. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  6644. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6645. return nullptr;
  6646. }
  6647. void * ptr = nullptr;
  6648. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6649. if (err != cudaSuccess) {
  6650. // clear the error
  6651. cudaGetLastError();
  6652. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6653. size/1024.0/1024.0, cudaGetErrorString(err));
  6654. return nullptr;
  6655. }
  6656. return ptr;
  6657. }
  6658. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  6659. CUDA_CHECK(cudaFreeHost(ptr));
  6660. }
  6661. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6662. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6663. cudaMemcpyKind kind;
  6664. char * src_ptr;
  6665. if (src->backend == GGML_BACKEND_CPU) {
  6666. kind = cudaMemcpyHostToDevice;
  6667. src_ptr = (char *) src->data;
  6668. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  6669. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6670. kind = cudaMemcpyDeviceToDevice;
  6671. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6672. int id;
  6673. CUDA_CHECK(cudaGetDevice(&id));
  6674. src_ptr = (char *) extra->data_device[id];
  6675. } else {
  6676. GGML_ASSERT(false);
  6677. }
  6678. char * dst_ptr = (char *) dst;
  6679. const int64_t ne0 = src->ne[0];
  6680. const int64_t nb0 = src->nb[0];
  6681. const int64_t nb1 = src->nb[1];
  6682. const int64_t nb2 = src->nb[2];
  6683. const int64_t nb3 = src->nb[3];
  6684. const enum ggml_type type = src->type;
  6685. const int64_t ts = ggml_type_size(type);
  6686. const int64_t bs = ggml_blck_size(type);
  6687. int64_t i1_diff = i1_high - i1_low;
  6688. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6689. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6690. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6691. } else if (nb0 == ts) {
  6692. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6693. } else {
  6694. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6695. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6696. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6697. // pretend the row is a matrix with cols=1
  6698. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6699. if (r != cudaSuccess) return r;
  6700. }
  6701. return cudaSuccess;
  6702. }
  6703. }
  6704. static void ggml_cuda_op_get_rows(
  6705. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6706. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6707. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6708. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6709. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6710. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6711. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6712. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6713. switch (src0->type) {
  6714. case GGML_TYPE_F16:
  6715. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6716. break;
  6717. case GGML_TYPE_F32:
  6718. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6719. break;
  6720. case GGML_TYPE_Q4_0:
  6721. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6722. break;
  6723. case GGML_TYPE_Q4_1:
  6724. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6725. break;
  6726. case GGML_TYPE_Q5_0:
  6727. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6728. break;
  6729. case GGML_TYPE_Q5_1:
  6730. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6731. break;
  6732. case GGML_TYPE_Q8_0:
  6733. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6734. break;
  6735. default:
  6736. // TODO: k-quants
  6737. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6738. GGML_ASSERT(false);
  6739. break;
  6740. }
  6741. }
  6742. template<class op>
  6743. static void ggml_cuda_op_bin_bcast(
  6744. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6745. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6746. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6747. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6748. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6749. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6750. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6751. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6752. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6753. } else {
  6754. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6755. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6756. GGML_ASSERT(false);
  6757. }
  6758. }
  6759. static void ggml_cuda_op_repeat(
  6760. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6761. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6762. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6763. (void) src1;
  6764. (void) src1_d;
  6765. }
  6766. static void ggml_cuda_op_add(
  6767. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6768. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6769. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6770. }
  6771. static void ggml_cuda_op_acc(
  6772. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6773. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6774. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6775. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6776. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6777. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6778. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6779. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6780. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6781. int offset = dst->op_params[3] / 4; // offset in bytes
  6782. 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);
  6783. (void) dst;
  6784. }
  6785. static void ggml_cuda_op_mul(
  6786. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6787. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6788. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6789. }
  6790. static void ggml_cuda_op_div(
  6791. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6792. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6793. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6794. }
  6795. static void ggml_cuda_op_gelu(
  6796. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6797. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6798. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6799. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6800. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6801. (void) src1;
  6802. (void) dst;
  6803. (void) src1_dd;
  6804. }
  6805. static void ggml_cuda_op_silu(
  6806. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6807. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6808. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6809. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6810. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6811. (void) src1;
  6812. (void) dst;
  6813. (void) src1_dd;
  6814. }
  6815. static void ggml_cuda_op_gelu_quick(
  6816. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6817. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6818. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6819. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6820. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6821. (void) src1;
  6822. (void) dst;
  6823. (void) src1_dd;
  6824. }
  6825. static void ggml_cuda_op_tanh(
  6826. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6827. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6828. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6829. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6830. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6831. (void) src1;
  6832. (void) dst;
  6833. (void) src1_dd;
  6834. }
  6835. static void ggml_cuda_op_relu(
  6836. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6837. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6838. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6839. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6840. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6841. (void) src1;
  6842. (void) dst;
  6843. (void) src1_dd;
  6844. }
  6845. static void ggml_cuda_op_hardsigmoid(
  6846. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6847. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6848. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6849. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6850. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6851. (void) src1;
  6852. (void) dst;
  6853. (void) src1_dd;
  6854. }
  6855. static void ggml_cuda_op_hardswish(
  6856. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6857. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6858. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6859. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6860. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6861. (void) src1;
  6862. (void) dst;
  6863. (void) src1_dd;
  6864. }
  6865. static void ggml_cuda_op_leaky_relu(
  6866. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6867. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6868. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6869. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6870. float negative_slope;
  6871. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6872. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6873. (void) src1;
  6874. (void) dst;
  6875. (void) src1_dd;
  6876. }
  6877. static void ggml_cuda_op_sqr(
  6878. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6879. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6880. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6881. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6882. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6883. (void) src1;
  6884. (void) dst;
  6885. (void) src1_dd;
  6886. }
  6887. static void ggml_cuda_op_norm(
  6888. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6889. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6890. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6891. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6892. const int64_t ne00 = src0->ne[0];
  6893. const int64_t nrows = ggml_nrows(src0);
  6894. float eps;
  6895. memcpy(&eps, dst->op_params, sizeof(float));
  6896. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6897. (void) src1;
  6898. (void) dst;
  6899. (void) src1_dd;
  6900. }
  6901. static void ggml_cuda_op_group_norm(
  6902. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6903. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6904. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6905. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6906. int num_groups = dst->op_params[0];
  6907. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6908. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  6909. (void) src1;
  6910. (void) dst;
  6911. (void) src1_dd;
  6912. }
  6913. static void ggml_cuda_op_concat(
  6914. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6915. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6916. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6917. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6918. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6919. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6920. 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);
  6921. }
  6922. (void) src1;
  6923. (void) dst;
  6924. }
  6925. static void ggml_cuda_op_upscale(
  6926. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6927. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6928. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6929. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6930. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6931. const int scale_factor = dst->op_params[0];
  6932. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  6933. (void) src1;
  6934. (void) dst;
  6935. (void) src1_dd;
  6936. }
  6937. static void ggml_cuda_op_pad(
  6938. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6939. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6940. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6941. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6942. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6943. pad_f32_cuda(src0_dd, dst_dd,
  6944. src0->ne[0], src0->ne[1], src0->ne[2],
  6945. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  6946. (void) src1;
  6947. (void) dst;
  6948. (void) src1_dd;
  6949. }
  6950. static void ggml_cuda_op_rms_norm(
  6951. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6952. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6953. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6954. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6955. const int64_t ne00 = src0->ne[0];
  6956. const int64_t nrows = ggml_nrows(src0);
  6957. float eps;
  6958. memcpy(&eps, dst->op_params, sizeof(float));
  6959. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6960. (void) src1;
  6961. (void) dst;
  6962. (void) src1_dd;
  6963. }
  6964. static void ggml_cuda_op_mul_mat_q(
  6965. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6966. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6967. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6968. const int64_t ne00 = src0->ne[0];
  6969. const int64_t ne10 = src1->ne[0];
  6970. GGML_ASSERT(ne10 % QK8_1 == 0);
  6971. const int64_t ne0 = dst->ne[0];
  6972. const int64_t row_diff = row_high - row_low;
  6973. int id;
  6974. CUDA_CHECK(cudaGetDevice(&id));
  6975. // the main device has a larger memory buffer to hold the results from all GPUs
  6976. // nrows_dst == nrows of the matrix that the kernel writes into
  6977. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6978. switch (src0->type) {
  6979. case GGML_TYPE_Q4_0:
  6980. 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);
  6981. break;
  6982. case GGML_TYPE_Q4_1:
  6983. 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);
  6984. break;
  6985. case GGML_TYPE_Q5_0:
  6986. 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);
  6987. break;
  6988. case GGML_TYPE_Q5_1:
  6989. 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);
  6990. break;
  6991. case GGML_TYPE_Q8_0:
  6992. 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);
  6993. break;
  6994. case GGML_TYPE_Q2_K:
  6995. 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);
  6996. break;
  6997. case GGML_TYPE_Q3_K:
  6998. 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);
  6999. break;
  7000. case GGML_TYPE_Q4_K:
  7001. 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);
  7002. break;
  7003. case GGML_TYPE_Q5_K:
  7004. 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);
  7005. break;
  7006. case GGML_TYPE_Q6_K:
  7007. 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);
  7008. break;
  7009. default:
  7010. GGML_ASSERT(false);
  7011. break;
  7012. }
  7013. (void) src1;
  7014. (void) dst;
  7015. (void) src1_ddf_i;
  7016. }
  7017. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  7018. int64_t min_compute_capability = INT_MAX;
  7019. int64_t max_compute_capability = INT_MIN;
  7020. for (int id = 0; id < g_device_count; ++id) {
  7021. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  7022. if (min_compute_capability > g_device_caps[id].cc) {
  7023. min_compute_capability = g_device_caps[id].cc;
  7024. }
  7025. if (max_compute_capability < g_device_caps[id].cc) {
  7026. max_compute_capability = g_device_caps[id].cc;
  7027. }
  7028. }
  7029. }
  7030. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7031. switch(type) {
  7032. case GGML_TYPE_Q4_0:
  7033. case GGML_TYPE_Q4_1:
  7034. case GGML_TYPE_Q5_0:
  7035. case GGML_TYPE_Q5_1:
  7036. case GGML_TYPE_Q8_0:
  7037. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7038. case GGML_TYPE_F16:
  7039. case GGML_TYPE_F32:
  7040. return 1;
  7041. case GGML_TYPE_Q2_K:
  7042. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  7043. case GGML_TYPE_Q3_K:
  7044. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  7045. case GGML_TYPE_Q4_K:
  7046. case GGML_TYPE_Q5_K:
  7047. case GGML_TYPE_Q6_K:
  7048. case GGML_TYPE_IQ2_XXS:
  7049. case GGML_TYPE_IQ2_XS:
  7050. case GGML_TYPE_IQ3_XXS:
  7051. case GGML_TYPE_IQ1_S:
  7052. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7053. default:
  7054. GGML_ASSERT(false);
  7055. }
  7056. #else
  7057. switch(type) {
  7058. case GGML_TYPE_Q4_0:
  7059. case GGML_TYPE_Q4_1:
  7060. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7061. case GGML_TYPE_Q5_0:
  7062. case GGML_TYPE_Q5_1:
  7063. case GGML_TYPE_Q8_0:
  7064. return 64;
  7065. case GGML_TYPE_F16:
  7066. case GGML_TYPE_F32:
  7067. return 1;
  7068. case GGML_TYPE_Q2_K:
  7069. case GGML_TYPE_Q3_K:
  7070. case GGML_TYPE_Q4_K:
  7071. case GGML_TYPE_Q5_K:
  7072. case GGML_TYPE_IQ2_XXS:
  7073. case GGML_TYPE_IQ2_XS:
  7074. case GGML_TYPE_IQ3_XXS:
  7075. case GGML_TYPE_IQ1_S:
  7076. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7077. case GGML_TYPE_Q6_K:
  7078. return 64;
  7079. default:
  7080. GGML_ASSERT(false);
  7081. }
  7082. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7083. }
  7084. static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split, int id) {
  7085. const int64_t nrows = ggml_nrows(tensor);
  7086. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  7087. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  7088. *row_low -= *row_low % rounding;
  7089. if (id == g_device_count - 1) {
  7090. *row_high = nrows;
  7091. } else {
  7092. *row_high = nrows*tensor_split[id + 1];
  7093. *row_high -= *row_high % rounding;
  7094. }
  7095. }
  7096. static void ggml_cuda_op_mul_mat_vec_q(
  7097. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7098. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7099. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7100. const int64_t ne00 = src0->ne[0];
  7101. const int64_t row_diff = row_high - row_low;
  7102. const int64_t ne10 = src1->ne[0];
  7103. GGML_ASSERT(ne10 % QK8_1 == 0);
  7104. const int64_t ne0 = dst->ne[0];
  7105. int id;
  7106. CUDA_CHECK(cudaGetDevice(&id));
  7107. // the main device has a larger memory buffer to hold the results from all GPUs
  7108. // nrows_dst == nrows of the matrix that the kernel writes into
  7109. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7110. switch (src0->type) {
  7111. case GGML_TYPE_Q4_0:
  7112. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7113. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7114. break;
  7115. case GGML_TYPE_Q4_1:
  7116. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7117. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7118. break;
  7119. case GGML_TYPE_Q5_0:
  7120. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7121. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7122. break;
  7123. case GGML_TYPE_Q5_1:
  7124. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7125. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7126. break;
  7127. case GGML_TYPE_Q8_0:
  7128. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7129. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7130. break;
  7131. case GGML_TYPE_Q2_K:
  7132. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7133. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7134. break;
  7135. case GGML_TYPE_Q3_K:
  7136. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7137. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7138. break;
  7139. case GGML_TYPE_Q4_K:
  7140. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7141. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7142. break;
  7143. case GGML_TYPE_Q5_K:
  7144. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7145. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7146. break;
  7147. case GGML_TYPE_Q6_K:
  7148. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7149. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7150. break;
  7151. case GGML_TYPE_IQ2_XXS:
  7152. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7153. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7154. break;
  7155. case GGML_TYPE_IQ2_XS:
  7156. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7157. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7158. break;
  7159. case GGML_TYPE_IQ3_XXS:
  7160. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7161. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7162. break;
  7163. case GGML_TYPE_IQ1_S:
  7164. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7165. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7166. break;
  7167. default:
  7168. GGML_ASSERT(false);
  7169. break;
  7170. }
  7171. (void) src1;
  7172. (void) dst;
  7173. (void) src1_ddf_i;
  7174. (void) src1_ncols;
  7175. (void) src1_padded_row_size;
  7176. }
  7177. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7178. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7179. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7180. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7181. const int64_t ne00 = src0->ne[0];
  7182. const int64_t row_diff = row_high - row_low;
  7183. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7184. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7185. #ifdef GGML_CUDA_F16
  7186. cuda_pool_alloc<half> src1_dfloat_a;
  7187. half * src1_dfloat = nullptr; // dfloat == half
  7188. bool src1_convert_f16 =
  7189. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7190. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7191. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7192. if (src1_convert_f16) {
  7193. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7194. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7195. GGML_ASSERT(to_fp16_cuda != nullptr);
  7196. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7197. }
  7198. #else
  7199. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7200. #endif // GGML_CUDA_F16
  7201. switch (src0->type) {
  7202. case GGML_TYPE_Q4_0:
  7203. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7204. break;
  7205. case GGML_TYPE_Q4_1:
  7206. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7207. break;
  7208. case GGML_TYPE_Q5_0:
  7209. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7210. break;
  7211. case GGML_TYPE_Q5_1:
  7212. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7213. break;
  7214. case GGML_TYPE_Q8_0:
  7215. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7216. break;
  7217. case GGML_TYPE_Q2_K:
  7218. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7219. break;
  7220. case GGML_TYPE_Q3_K:
  7221. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7222. break;
  7223. case GGML_TYPE_Q4_K:
  7224. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7225. break;
  7226. case GGML_TYPE_Q5_K:
  7227. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7228. break;
  7229. case GGML_TYPE_Q6_K:
  7230. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7231. break;
  7232. case GGML_TYPE_F16:
  7233. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7234. break;
  7235. default:
  7236. GGML_ASSERT(false);
  7237. break;
  7238. }
  7239. (void) src1;
  7240. (void) dst;
  7241. (void) src1_ddq_i;
  7242. (void) src1_ncols;
  7243. (void) src1_padded_row_size;
  7244. }
  7245. static void ggml_cuda_op_mul_mat_cublas(
  7246. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7247. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7248. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7249. GGML_ASSERT(src0_dd_i != nullptr);
  7250. GGML_ASSERT(src1_ddf_i != nullptr);
  7251. GGML_ASSERT(dst_dd_i != nullptr);
  7252. const int64_t ne00 = src0->ne[0];
  7253. const int64_t ne10 = src1->ne[0];
  7254. const int64_t ne0 = dst->ne[0];
  7255. const int64_t row_diff = row_high - row_low;
  7256. int id;
  7257. CUDA_CHECK(cudaGetDevice(&id));
  7258. // the main device has a larger memory buffer to hold the results from all GPUs
  7259. // ldc == nrows of the matrix that cuBLAS writes into
  7260. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7261. const int compute_capability = g_device_caps[id].cc;
  7262. 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) {
  7263. //printf("this branch\n");
  7264. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7265. cuda_pool_alloc<half> src0_as_f16;
  7266. if (src0->type != GGML_TYPE_F16) {
  7267. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7268. GGML_ASSERT(to_fp16_cuda != nullptr);
  7269. size_t ne = row_diff*ne00;
  7270. src0_as_f16.alloc(ne);
  7271. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7272. }
  7273. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7274. cuda_pool_alloc<half> src1_as_f16;
  7275. if (src1->type != GGML_TYPE_F16) {
  7276. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7277. GGML_ASSERT(to_fp16_cuda != nullptr);
  7278. size_t ne = src1_ncols*ne10;
  7279. src1_as_f16.alloc(ne);
  7280. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7281. }
  7282. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7283. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7284. const half alpha_f16 = 1.0f;
  7285. const half beta_f16 = 0.0f;
  7286. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7287. CUBLAS_CHECK(
  7288. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7289. row_diff, src1_ncols, ne10,
  7290. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7291. src1_ptr, CUDA_R_16F, ne10,
  7292. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7293. CUBLAS_COMPUTE_16F,
  7294. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7295. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7296. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7297. } else {
  7298. cuda_pool_alloc<float> src0_ddq_as_f32;
  7299. cuda_pool_alloc<float> src1_ddq_as_f32;
  7300. if (src0->type != GGML_TYPE_F32) {
  7301. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7302. GGML_ASSERT(to_fp32_cuda != nullptr);
  7303. src0_ddq_as_f32.alloc(row_diff*ne00);
  7304. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7305. }
  7306. if (src1->type != GGML_TYPE_F32) {
  7307. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7308. GGML_ASSERT(to_fp32_cuda != nullptr);
  7309. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7310. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7311. }
  7312. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7313. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7314. const float alpha = 1.0f;
  7315. const float beta = 0.0f;
  7316. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7317. CUBLAS_CHECK(
  7318. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7319. row_diff, src1_ncols, ne10,
  7320. &alpha, src0_ddf_i, ne00,
  7321. src1_ddf1_i, ne10,
  7322. &beta, dst_dd_i, ldc));
  7323. }
  7324. (void) dst;
  7325. (void) src1_ddq_i;
  7326. (void) src1_padded_row_size;
  7327. }
  7328. static void ggml_cuda_op_rope(
  7329. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7330. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7331. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7332. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7333. GGML_ASSERT(src0->type == dst->type);
  7334. const int64_t ne00 = src0->ne[0];
  7335. const int64_t ne01 = src0->ne[1];
  7336. const int64_t ne2 = dst->ne[2];
  7337. const int64_t nrows = ggml_nrows(src0);
  7338. //const int n_past = ((int32_t *) dst->op_params)[0];
  7339. const int n_dims = ((int32_t *) dst->op_params)[1];
  7340. const int mode = ((int32_t *) dst->op_params)[2];
  7341. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7342. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7343. // RoPE alteration for extended context
  7344. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7345. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7346. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7347. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7348. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7349. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7350. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7351. const int32_t * pos = nullptr;
  7352. if ((mode & 1) == 0) {
  7353. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7354. GGML_ASSERT(src1->ne[0] == ne2);
  7355. pos = (const int32_t *) src1_dd;
  7356. }
  7357. const bool is_neox = mode & 2;
  7358. const bool is_glm = mode & 4;
  7359. rope_corr_dims corr_dims;
  7360. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7361. // compute
  7362. if (is_glm) {
  7363. GGML_ASSERT(false);
  7364. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7365. } else if (is_neox) {
  7366. if (src0->type == GGML_TYPE_F32) {
  7367. rope_neox_cuda(
  7368. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7369. attn_factor, corr_dims, main_stream
  7370. );
  7371. } else if (src0->type == GGML_TYPE_F16) {
  7372. rope_neox_cuda(
  7373. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7374. attn_factor, corr_dims, main_stream
  7375. );
  7376. } else {
  7377. GGML_ASSERT(false);
  7378. }
  7379. } else {
  7380. if (src0->type == GGML_TYPE_F32) {
  7381. rope_cuda(
  7382. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7383. attn_factor, corr_dims, main_stream
  7384. );
  7385. } else if (src0->type == GGML_TYPE_F16) {
  7386. rope_cuda(
  7387. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7388. attn_factor, corr_dims, main_stream
  7389. );
  7390. } else {
  7391. GGML_ASSERT(false);
  7392. }
  7393. }
  7394. (void) src1;
  7395. (void) dst;
  7396. (void) src1_dd;
  7397. }
  7398. static void ggml_cuda_op_alibi(
  7399. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7400. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7401. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7402. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7403. const int64_t ne00 = src0->ne[0];
  7404. const int64_t ne01 = src0->ne[1];
  7405. const int64_t ne02 = src0->ne[2];
  7406. const int64_t nrows = ggml_nrows(src0);
  7407. //const int n_past = ((int32_t *) dst->op_params)[0];
  7408. const int n_head = ((int32_t *) dst->op_params)[1];
  7409. float max_bias;
  7410. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7411. //GGML_ASSERT(ne01 + n_past == ne00);
  7412. GGML_ASSERT(n_head == ne02);
  7413. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7414. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7415. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7416. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7417. (void) src1;
  7418. (void) src1_dd;
  7419. }
  7420. static void ggml_cuda_op_pool2d(
  7421. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7422. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7423. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7424. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7425. const int32_t * opts = (const int32_t *)dst->op_params;
  7426. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  7427. const int k0 = opts[1];
  7428. const int k1 = opts[2];
  7429. const int s0 = opts[3];
  7430. const int s1 = opts[4];
  7431. const int p0 = opts[5];
  7432. const int p1 = opts[6];
  7433. const int64_t IH = src0->ne[1];
  7434. const int64_t IW = src0->ne[0];
  7435. const int64_t N = dst->ne[3];
  7436. const int64_t OC = dst->ne[2];
  7437. const int64_t OH = dst->ne[1];
  7438. const int64_t OW = dst->ne[0];
  7439. const int parallel_elements = N * OC * OH * OW;
  7440. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  7441. dim3 block_nums(num_blocks);
  7442. pool2d_nchw_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, main_stream>>>(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0, parallel_elements, src0_dd, dst_dd, op);
  7443. (void) src1;
  7444. (void) src1_dd;
  7445. }
  7446. static void ggml_cuda_op_im2col(
  7447. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7448. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7449. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7450. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7451. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  7452. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7453. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  7454. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  7455. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  7456. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  7457. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  7458. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  7459. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  7460. const int64_t IH = is_2D ? src1->ne[1] : 1;
  7461. const int64_t IW = src1->ne[0];
  7462. const int64_t KH = is_2D ? src0->ne[1] : 1;
  7463. const int64_t KW = src0->ne[0];
  7464. const int64_t OH = is_2D ? dst->ne[2] : 1;
  7465. const int64_t OW = dst->ne[1];
  7466. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7467. const int64_t batch = src1->ne[3];
  7468. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  7469. if(dst->type == GGML_TYPE_F16) {
  7470. im2col_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7471. } else {
  7472. im2col_cuda(src1_dd, (float*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7473. }
  7474. (void) src0;
  7475. (void) src0_dd;
  7476. }
  7477. static void ggml_cuda_op_sum_rows(
  7478. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7479. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7480. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7481. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7482. const int64_t ncols = src0->ne[0];
  7483. const int64_t nrows = ggml_nrows(src0);
  7484. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  7485. (void) src1;
  7486. (void) dst;
  7487. (void) src1_dd;
  7488. }
  7489. static void ggml_cuda_op_argsort(
  7490. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7491. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7492. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7493. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  7494. const int64_t ncols = src0->ne[0];
  7495. const int64_t nrows = ggml_nrows(src0);
  7496. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  7497. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  7498. (void) src1;
  7499. (void) dst;
  7500. (void) src1_dd;
  7501. }
  7502. static void ggml_cuda_op_diag_mask_inf(
  7503. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7504. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7505. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7506. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7507. const int64_t ne00 = src0->ne[0];
  7508. const int64_t ne01 = src0->ne[1];
  7509. const int nrows0 = ggml_nrows(src0);
  7510. const int n_past = ((int32_t *) dst->op_params)[0];
  7511. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  7512. (void) src1;
  7513. (void) dst;
  7514. (void) src1_dd;
  7515. }
  7516. static void ggml_cuda_op_soft_max(
  7517. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7518. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7519. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7520. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7521. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  7522. const int64_t ne00 = src0->ne[0];
  7523. const int64_t nrows_x = ggml_nrows(src0);
  7524. const int64_t nrows_y = src0->ne[1];
  7525. float scale = 1.0f;
  7526. float max_bias = 0.0f;
  7527. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7528. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7529. // positions tensor
  7530. float * src2_dd = nullptr;
  7531. cuda_pool_alloc<float> src2_f;
  7532. ggml_tensor * src2 = dst->src[2];
  7533. const bool use_src2 = src2 != nullptr;
  7534. if (use_src2) {
  7535. const bool src2_on_device = src2->backend == GGML_BACKEND_GPU;
  7536. if (src2_on_device) {
  7537. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  7538. src2_dd = (float *) src2_extra->data_device[g_main_device];
  7539. } else {
  7540. src2_dd = src2_f.alloc(ggml_nelements(src2));
  7541. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  7542. }
  7543. }
  7544. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  7545. }
  7546. static void ggml_cuda_op_scale(
  7547. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7548. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7549. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7550. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7551. float scale;
  7552. memcpy(&scale, dst->op_params, sizeof(float));
  7553. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  7554. CUDA_CHECK(cudaGetLastError());
  7555. (void) src1;
  7556. (void) dst;
  7557. (void) src1_dd;
  7558. }
  7559. static void ggml_cuda_op_clamp(
  7560. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7561. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7562. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7563. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7564. float min;
  7565. float max;
  7566. memcpy(&min, dst->op_params, sizeof(float));
  7567. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7568. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  7569. CUDA_CHECK(cudaGetLastError());
  7570. (void) src1;
  7571. (void) dst;
  7572. (void) src1_dd;
  7573. }
  7574. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  7575. const int64_t nrows0 = ggml_nrows(src0);
  7576. const bool use_src1 = src1 != nullptr;
  7577. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  7578. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  7579. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  7580. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7581. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  7582. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7583. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7584. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  7585. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  7586. // dd = data device
  7587. float * src0_ddf = nullptr;
  7588. float * src1_ddf = nullptr;
  7589. float * dst_ddf = nullptr;
  7590. cuda_pool_alloc<float> src0_f;
  7591. cuda_pool_alloc<float> src1_f;
  7592. cuda_pool_alloc<float> dst_f;
  7593. ggml_cuda_set_device(g_main_device);
  7594. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7595. if (src0_on_device) {
  7596. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  7597. } else {
  7598. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  7599. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  7600. }
  7601. if (use_src1) {
  7602. if (src1_on_device) {
  7603. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7604. } else {
  7605. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  7606. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  7607. }
  7608. }
  7609. if (dst_on_device) {
  7610. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7611. } else {
  7612. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7613. }
  7614. // do the computation
  7615. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7616. CUDA_CHECK(cudaGetLastError());
  7617. // copy dst to host if necessary
  7618. if (!dst_on_device) {
  7619. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7620. }
  7621. if (dst->backend == GGML_BACKEND_CPU) {
  7622. CUDA_CHECK(cudaDeviceSynchronize());
  7623. }
  7624. }
  7625. static void ggml_cuda_set_peer_access(const int n_tokens) {
  7626. static bool peer_access_enabled = false;
  7627. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  7628. if (peer_access_enabled == enable_peer_access) {
  7629. return;
  7630. }
  7631. #ifdef NDEBUG
  7632. for (int id = 0; id < g_device_count; ++id) {
  7633. ggml_cuda_set_device(id);
  7634. CUDA_CHECK(cudaDeviceSynchronize());
  7635. }
  7636. for (int id = 0; id < g_device_count; ++id) {
  7637. ggml_cuda_set_device(id);
  7638. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  7639. if (id == id_other) {
  7640. continue;
  7641. }
  7642. if (id != g_main_device && id_other != g_main_device) {
  7643. continue;
  7644. }
  7645. int can_access_peer;
  7646. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  7647. if (can_access_peer) {
  7648. if (enable_peer_access) {
  7649. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  7650. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  7651. CUDA_CHECK(err);
  7652. }
  7653. } else {
  7654. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  7655. if (err != cudaErrorPeerAccessNotEnabled) {
  7656. CUDA_CHECK(err);
  7657. }
  7658. }
  7659. }
  7660. }
  7661. }
  7662. #endif // NDEBUG
  7663. peer_access_enabled = enable_peer_access;
  7664. }
  7665. // FIXME: move this somewhere else
  7666. struct ggml_backend_cuda_split_buffer_type_context {
  7667. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7668. };
  7669. static void ggml_cuda_op_mul_mat(
  7670. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  7671. const bool convert_src1_to_q8_1) {
  7672. const int64_t ne00 = src0->ne[0];
  7673. const int64_t ne01 = src0->ne[1];
  7674. const int64_t ne02 = src0->ne[2];
  7675. const int64_t ne03 = src0->ne[3];
  7676. const int64_t ne10 = src1->ne[0];
  7677. const int64_t ne11 = src1->ne[1];
  7678. const int64_t ne12 = src1->ne[2];
  7679. const int64_t ne13 = src1->ne[3];
  7680. const int64_t nrows1 = ggml_nrows(src1);
  7681. GGML_ASSERT(ne03 == ne13);
  7682. const int64_t ne0 = dst->ne[0];
  7683. const int64_t ne1 = dst->ne[1];
  7684. const int nb2 = dst->nb[2];
  7685. const int nb3 = dst->nb[3];
  7686. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  7687. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  7688. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  7689. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  7690. const int64_t i02_divisor = ne12 / ne02;
  7691. const size_t src0_ts = ggml_type_size(src0->type);
  7692. const size_t src0_bs = ggml_blck_size(src0->type);
  7693. const size_t q8_1_ts = sizeof(block_q8_1);
  7694. const size_t q8_1_bs = QK8_1;
  7695. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7696. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7697. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7698. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7699. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  7700. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  7701. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  7702. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  7703. GGML_ASSERT(!(split && ne02 > 1));
  7704. GGML_ASSERT(!(split && ne03 > 1));
  7705. GGML_ASSERT(!(split && ne02 < ne12));
  7706. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7707. if (split) {
  7708. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_GPU_SPLIT check
  7709. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  7710. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  7711. tensor_split = buft_ctx->tensor_split;
  7712. }
  7713. struct dev_data {
  7714. cuda_pool_alloc<char> src0_dd_alloc;
  7715. cuda_pool_alloc<float> src1_ddf_alloc;
  7716. cuda_pool_alloc<char> src1_ddq_alloc;
  7717. cuda_pool_alloc<float> dst_dd_alloc;
  7718. char * src0_dd = nullptr;
  7719. float * src1_ddf = nullptr; // float
  7720. char * src1_ddq = nullptr; // q8_1
  7721. float * dst_dd = nullptr;
  7722. int64_t row_low;
  7723. int64_t row_high;
  7724. };
  7725. dev_data dev[GGML_CUDA_MAX_DEVICES];
  7726. int used_devices = 0;
  7727. for (int id = 0; id < g_device_count; ++id) {
  7728. // by default, use all rows
  7729. dev[id].row_low = 0;
  7730. dev[id].row_high = ne01;
  7731. // for multi GPU, get the row boundaries from tensor split
  7732. // and round to mul_mat_q tile sizes
  7733. if (split) {
  7734. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  7735. if (id != 0) {
  7736. dev[id].row_low = ne01*tensor_split[id];
  7737. if (dev[id].row_low < ne01) {
  7738. dev[id].row_low -= dev[id].row_low % rounding;
  7739. }
  7740. }
  7741. if (id != g_device_count - 1) {
  7742. dev[id].row_high = ne01*tensor_split[id + 1];
  7743. if (dev[id].row_high < ne01) {
  7744. dev[id].row_high -= dev[id].row_high % rounding;
  7745. }
  7746. }
  7747. }
  7748. }
  7749. for (int id = 0; id < g_device_count; ++id) {
  7750. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7751. continue;
  7752. }
  7753. used_devices++;
  7754. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7755. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7756. ggml_cuda_set_device(id);
  7757. cudaStream_t stream = g_cudaStreams[id][0];
  7758. if (src0_on_device && src0_is_contiguous) {
  7759. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  7760. } else {
  7761. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  7762. }
  7763. if (src1_on_device && src1_is_contiguous) {
  7764. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7765. } else {
  7766. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7767. }
  7768. if (convert_src1_to_q8_1) {
  7769. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7770. if (src1_on_device && src1_is_contiguous) {
  7771. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7772. CUDA_CHECK(cudaGetLastError());
  7773. }
  7774. }
  7775. if (dst_on_device) {
  7776. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7777. } else {
  7778. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7779. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7780. }
  7781. }
  7782. // if multiple devices are used they need to wait for the main device
  7783. // here an event is recorded that signals that the main device has finished calculating the input data
  7784. if (split && used_devices > 1) {
  7785. ggml_cuda_set_device(g_main_device);
  7786. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7787. }
  7788. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7789. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7790. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7791. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7792. for (int id = 0; id < g_device_count; ++id) {
  7793. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7794. continue;
  7795. }
  7796. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7797. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7798. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7799. ggml_cuda_set_device(id);
  7800. cudaStream_t stream = g_cudaStreams[id][is];
  7801. // wait for main GPU data if necessary
  7802. if (split && (id != g_main_device || is != 0)) {
  7803. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7804. }
  7805. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7806. const int64_t i03 = i0 / ne12;
  7807. const int64_t i02 = i0 % ne12;
  7808. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7809. // for split tensors the data begins at i0 == i0_offset_low
  7810. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7811. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7812. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7813. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7814. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7815. // in that case an offset on dst_ddf_i is needed
  7816. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  7817. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7818. }
  7819. // copy src0, src1 to device if necessary
  7820. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  7821. if (id != g_main_device) {
  7822. if (convert_src1_to_q8_1) {
  7823. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7824. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7825. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7826. } else {
  7827. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7828. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7829. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7830. src1_ncols*ne10*sizeof(float), stream));
  7831. }
  7832. }
  7833. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  7834. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7835. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7836. } else {
  7837. GGML_ASSERT(false);
  7838. }
  7839. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  7840. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7841. CUDA_CHECK(cudaGetLastError());
  7842. }
  7843. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7844. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7845. }
  7846. // do the computation
  7847. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7848. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7849. CUDA_CHECK(cudaGetLastError());
  7850. // copy dst to host or other device if necessary
  7851. if (!dst_on_device) {
  7852. void * dst_off_device;
  7853. cudaMemcpyKind kind;
  7854. if (dst->backend == GGML_BACKEND_CPU) {
  7855. dst_off_device = dst->data;
  7856. kind = cudaMemcpyDeviceToHost;
  7857. } else if (dst->backend == GGML_BACKEND_GPU) {
  7858. dst_off_device = dst_extra->data_device[g_main_device];
  7859. kind = cudaMemcpyDeviceToDevice;
  7860. } else {
  7861. GGML_ASSERT(false);
  7862. }
  7863. if (split) {
  7864. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7865. // dst is NOT transposed.
  7866. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7867. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7868. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7869. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7870. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7871. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7872. #if !defined(GGML_USE_HIPBLAS)
  7873. if (kind == cudaMemcpyDeviceToDevice) {
  7874. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7875. cudaMemcpy3DPeerParms p = {};
  7876. p.dstDevice = g_main_device;
  7877. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7878. p.srcDevice = id;
  7879. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7880. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7881. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7882. } else
  7883. #endif
  7884. {
  7885. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7886. dst_dd_i, row_diff*sizeof(float),
  7887. row_diff*sizeof(float), src1_ncols,
  7888. kind, stream));
  7889. }
  7890. } else {
  7891. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7892. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7893. dhf_dst_i += src1_col_0*ne0;
  7894. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7895. }
  7896. }
  7897. // add event for the main device to wait on until other device is done
  7898. if (split && (id != g_main_device || is != 0)) {
  7899. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7900. }
  7901. }
  7902. }
  7903. }
  7904. // main device waits for all other devices to be finished
  7905. if (split && g_device_count > 1) {
  7906. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7907. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7908. ggml_cuda_set_device(g_main_device);
  7909. for (int id = 0; id < g_device_count; ++id) {
  7910. if (dev[id].row_low == dev[id].row_high) {
  7911. continue;
  7912. }
  7913. for (int64_t is = 0; is < is_max; ++is) {
  7914. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7915. }
  7916. }
  7917. }
  7918. if (dst->backend == GGML_BACKEND_CPU) {
  7919. ggml_cuda_set_device(g_main_device);
  7920. CUDA_CHECK(cudaDeviceSynchronize());
  7921. }
  7922. }
  7923. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7924. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  7925. }
  7926. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7927. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  7928. }
  7929. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7930. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  7931. }
  7932. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7933. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  7934. }
  7935. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7936. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  7937. }
  7938. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7939. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  7940. }
  7941. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7942. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  7943. }
  7944. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7945. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  7946. }
  7947. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7948. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  7949. }
  7950. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7951. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  7952. }
  7953. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7954. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  7955. }
  7956. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7957. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  7958. }
  7959. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7960. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  7961. }
  7962. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7963. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  7964. }
  7965. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7966. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  7967. }
  7968. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7969. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  7970. }
  7971. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7972. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  7973. }
  7974. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7975. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  7976. }
  7977. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7978. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  7979. }
  7980. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7981. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  7982. }
  7983. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7984. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  7985. }
  7986. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  7987. if (!g_cublas_loaded) return false;
  7988. const int64_t ne10 = src1->ne[0];
  7989. const int64_t ne0 = dst->ne[0];
  7990. const int64_t ne1 = dst->ne[1];
  7991. // TODO: find the optimal values for these
  7992. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  7993. src1->type == GGML_TYPE_F32 &&
  7994. dst->type == GGML_TYPE_F32 &&
  7995. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  7996. }
  7997. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7998. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  7999. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8000. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  8001. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  8002. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8003. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8004. const int64_t ne00 = src0->ne[0];
  8005. const int64_t ne01 = src0->ne[1];
  8006. const int64_t ne02 = src0->ne[2];
  8007. const int64_t ne12 = src1->ne[2];
  8008. ggml_cuda_set_device(g_main_device);
  8009. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8010. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8011. void * src0_ddq = src0_extra->data_device[g_main_device];
  8012. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8013. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8014. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8015. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8016. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8017. }
  8018. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8019. GGML_ASSERT(!ggml_is_transposed(src0));
  8020. GGML_ASSERT(!ggml_is_transposed(src1));
  8021. GGML_ASSERT(!ggml_is_permuted(src0));
  8022. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8023. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8024. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8025. const int64_t ne00 = src0->ne[0];
  8026. const int64_t ne01 = src0->ne[1];
  8027. const int64_t ne02 = src0->ne[2];
  8028. const int64_t nb01 = src0->nb[1];
  8029. const int64_t nb02 = src0->nb[2];
  8030. const int64_t ne12 = src1->ne[2];
  8031. ggml_cuda_set_device(g_main_device);
  8032. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8033. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8034. void * src0_ddq = src0_extra->data_device[g_main_device];
  8035. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8036. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8037. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8038. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8039. const int64_t row_stride_x = nb01 / sizeof(half);
  8040. const int64_t channel_stride_x = nb02 / sizeof(half);
  8041. 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);
  8042. }
  8043. static __global__ void k_compute_batched_ptrs(
  8044. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8045. const void ** ptrs_src, void ** ptrs_dst,
  8046. int64_t ne12, int64_t ne13,
  8047. int64_t ne23,
  8048. size_t nb02, size_t nb03,
  8049. size_t nb12, size_t nb13,
  8050. size_t nbd2, size_t nbd3,
  8051. int64_t r2, int64_t r3) {
  8052. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8053. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8054. if (i13 >= ne13 || i12 >= ne12) {
  8055. return;
  8056. }
  8057. int64_t i03 = i13 / r3;
  8058. int64_t i02 = i12 / r2;
  8059. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8060. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8061. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8062. }
  8063. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8064. GGML_ASSERT(!ggml_is_transposed(src0));
  8065. GGML_ASSERT(!ggml_is_transposed(src1));
  8066. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8067. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8068. GGML_TENSOR_BINARY_OP_LOCALS
  8069. const int64_t ne_dst = ggml_nelements(dst);
  8070. ggml_cuda_set_device(g_main_device);
  8071. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8072. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8073. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8074. void * src0_ddq = src0_extra->data_device[g_main_device];
  8075. half * src0_f16 = (half *) src0_ddq;
  8076. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8077. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8078. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8079. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8080. // convert src1 to fp16
  8081. cuda_pool_alloc<half> src1_f16_alloc;
  8082. if (src1->type != GGML_TYPE_F16) {
  8083. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8084. const int64_t ne_src1 = ggml_nelements(src1);
  8085. src1_f16_alloc.alloc(ne_src1);
  8086. GGML_ASSERT(to_fp16_cuda != nullptr);
  8087. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8088. }
  8089. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8090. cuda_pool_alloc<half> dst_f16;
  8091. char * dst_t;
  8092. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8093. cudaDataType_t cu_data_type = CUDA_R_16F;
  8094. // dst strides
  8095. size_t nbd2 = dst->nb[2];
  8096. size_t nbd3 = dst->nb[3];
  8097. const half alpha_f16 = 1.0f;
  8098. const half beta_f16 = 0.0f;
  8099. const float alpha_f32 = 1.0f;
  8100. const float beta_f32 = 0.0f;
  8101. const void * alpha = &alpha_f16;
  8102. const void * beta = &beta_f16;
  8103. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8104. dst_t = (char *) dst_f16.alloc(ne_dst);
  8105. nbd2 /= sizeof(float) / sizeof(half);
  8106. nbd3 /= sizeof(float) / sizeof(half);
  8107. } else {
  8108. dst_t = (char *) dst_ddf;
  8109. cu_compute_type = CUBLAS_COMPUTE_32F;
  8110. cu_data_type = CUDA_R_32F;
  8111. alpha = &alpha_f32;
  8112. beta = &beta_f32;
  8113. }
  8114. GGML_ASSERT(ne12 % ne02 == 0);
  8115. GGML_ASSERT(ne13 % ne03 == 0);
  8116. // broadcast factors
  8117. const int64_t r2 = ne12/ne02;
  8118. const int64_t r3 = ne13/ne03;
  8119. #if 0
  8120. // use cublasGemmEx
  8121. {
  8122. for (int i13 = 0; i13 < ne13; ++i13) {
  8123. for (int i12 = 0; i12 < ne12; ++i12) {
  8124. int i03 = i13 / r3;
  8125. int i02 = i12 / r2;
  8126. CUBLAS_CHECK(
  8127. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8128. ne01, ne11, ne10,
  8129. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8130. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8131. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8132. cu_compute_type,
  8133. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8134. }
  8135. }
  8136. }
  8137. #else
  8138. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8139. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8140. // use cublasGemmStridedBatchedEx
  8141. CUBLAS_CHECK(
  8142. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8143. ne01, ne11, ne10,
  8144. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8145. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8146. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8147. ne12*ne13,
  8148. cu_compute_type,
  8149. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8150. } else {
  8151. // use cublasGemmBatchedEx
  8152. const int ne23 = ne12*ne13;
  8153. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8154. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8155. dim3 block_dims(ne13, ne12);
  8156. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8157. src0_f16, src1_f16, dst_t,
  8158. ptrs_src.get(), ptrs_dst.get(),
  8159. ne12, ne13,
  8160. ne23,
  8161. nb02, nb03,
  8162. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8163. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8164. nbd2, nbd3,
  8165. r2, r3);
  8166. CUDA_CHECK(cudaGetLastError());
  8167. CUBLAS_CHECK(
  8168. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8169. ne01, ne11, ne10,
  8170. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8171. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8172. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8173. ne23,
  8174. cu_compute_type,
  8175. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8176. }
  8177. #endif
  8178. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8179. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8180. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8181. }
  8182. }
  8183. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8184. const bool all_on_device =
  8185. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  8186. (src1->backend == GGML_BACKEND_GPU) &&
  8187. ( dst->backend == GGML_BACKEND_GPU);
  8188. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  8189. int64_t min_compute_capability = INT_MAX;
  8190. bool any_pascal_with_slow_fp16 = false;
  8191. if (split) {
  8192. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8193. auto & tensor_split = buft_ctx->tensor_split;
  8194. for (int id = 0; id < g_device_count; ++id) {
  8195. // skip devices that are not going to do any work:
  8196. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8197. continue;
  8198. }
  8199. if (min_compute_capability > g_device_caps[id].cc) {
  8200. min_compute_capability = g_device_caps[id].cc;
  8201. }
  8202. if (g_device_caps[id].cc == 610) {
  8203. any_pascal_with_slow_fp16 = true;
  8204. }
  8205. }
  8206. } else {
  8207. min_compute_capability = g_device_caps[g_main_device].cc;
  8208. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8209. }
  8210. // check data types and tensor shapes for custom matrix multiplication kernels:
  8211. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8212. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8213. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8214. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8215. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8216. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8217. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8218. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8219. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8220. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8221. #ifdef CUDA_USE_TENSOR_CORES
  8222. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8223. #endif // CUDA_USE_TENSOR_CORES
  8224. #else
  8225. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8226. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8227. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8228. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8229. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8230. #ifdef CUDA_USE_TENSOR_CORES
  8231. // when tensor cores are available, use them for large batch size
  8232. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8233. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8234. #endif // CUDA_USE_TENSOR_CORES
  8235. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8236. // if mmvq is available it's a better choice than dmmv:
  8237. #ifndef GGML_CUDA_FORCE_DMMV
  8238. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8239. #endif // GGML_CUDA_FORCE_DMMV
  8240. // debug helpers
  8241. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8242. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8243. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8244. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8245. //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);
  8246. //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);
  8247. 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) {
  8248. // KQ single-batch
  8249. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8250. } 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) {
  8251. // KQV single-batch
  8252. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8253. } else if (!split && all_on_device && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  8254. // KQ + KQV multi-batch
  8255. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8256. } else if (use_dequantize_mul_mat_vec) {
  8257. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8258. } else if (use_mul_mat_vec_q) {
  8259. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8260. } else if (use_mul_mat_q) {
  8261. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8262. } else {
  8263. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8264. }
  8265. }
  8266. #if 0
  8267. template<typename ... Srcs>
  8268. static __global__ void k_compute_batched_ptrs_id(
  8269. const void ** ptrs_src, void ** ptrs_dst,
  8270. int ne12, int ne13,
  8271. int ne23,
  8272. int nb02, int nb03,
  8273. int nb12, int nb13,
  8274. int nb2, int nb3,
  8275. int r2, int r3,
  8276. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8277. const half * src1_f16, half * dst_f16,
  8278. const int32_t * ids, const int id,
  8279. Srcs... src0s) {
  8280. int i = ids[id];
  8281. half * src0_f16;
  8282. const void * srcs_ar[] = { (const half *) src0s... };
  8283. if (src0_type == GGML_TYPE_F16) {
  8284. src0_f16 = (half *) srcs_ar[i];
  8285. } else {
  8286. src0_f16 = src0_as_f16;
  8287. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8288. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8289. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8290. }
  8291. }
  8292. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8293. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8294. if (i13 >= ne13 || i12 >= ne12) {
  8295. return;
  8296. }
  8297. int i03 = i13 / r3;
  8298. int i02 = i12 / r2;
  8299. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8300. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8301. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8302. }
  8303. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8304. const struct ggml_tensor * ids = dst->src[0];
  8305. const struct ggml_tensor * src1 = dst->src[1];
  8306. const struct ggml_tensor * src00 = dst->src[2];
  8307. const int id = dst->op_params[0];
  8308. GGML_ASSERT(!ggml_is_transposed(src00));
  8309. GGML_ASSERT(!ggml_is_transposed(src1));
  8310. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  8311. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8312. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8313. const int64_t ne01 = src00->ne[1];
  8314. const int64_t ne02 = src00->ne[2];
  8315. const int64_t ne03 = src00->ne[3];
  8316. //const int64_t nb01 = src00->nb[1];
  8317. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8318. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8319. const int64_t ne10 = src1->ne[0];
  8320. const int64_t ne11 = src1->ne[1];
  8321. const int64_t ne12 = src1->ne[2];
  8322. const int64_t ne13 = src1->ne[3];
  8323. //const int64_t nb11 = src1->nb[1];
  8324. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8325. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8326. const int64_t ne1 = ggml_nelements(src1);
  8327. const int64_t ne = ggml_nelements(dst);
  8328. ggml_cuda_set_device(g_main_device);
  8329. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8330. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8331. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8332. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8333. //half * src0_as_f16 = (half *) src0_ddq;
  8334. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8335. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8336. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8337. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8338. // convert src1 to fp16
  8339. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8340. GGML_ASSERT(to_fp16_cuda != nullptr);
  8341. size_t src1_as = 0;
  8342. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8343. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8344. size_t dst_as = 0;
  8345. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8346. GGML_ASSERT(ne12 % ne02 == 0);
  8347. GGML_ASSERT(ne13 % ne03 == 0);
  8348. // broadcast factors
  8349. const int64_t r2 = ne12/ne02;
  8350. const int64_t r3 = ne13/ne03;
  8351. const half alpha_f16 = 1.0f;
  8352. const half beta_f16 = 0.0f;
  8353. // use cublasGemmBatchedEx
  8354. const int ne23 = ne12*ne13;
  8355. const void ** ptrs_src = nullptr;
  8356. void ** ptrs_dst = nullptr;
  8357. size_t ptrs_src_s = 0;
  8358. size_t ptrs_dst_s = 0;
  8359. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8360. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8361. int64_t src0_ne = ggml_nelements(src00);
  8362. half * src0_as_f16 = nullptr;
  8363. size_t src0_as = 0;
  8364. if (src00->type != GGML_TYPE_F16) {
  8365. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8366. }
  8367. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8368. dim3 block_dims(ne13, ne12);
  8369. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8370. ptrs_src, ptrs_dst,
  8371. ne12, ne13,
  8372. ne23,
  8373. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8374. nb12, nb13,
  8375. dst->nb[2], dst->nb[3],
  8376. r2, r3,
  8377. src00->type, src0_as_f16, src0_ne,
  8378. src1_as_f16, dst_f16,
  8379. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8380. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8381. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8382. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8383. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8384. );
  8385. CUDA_CHECK(cudaGetLastError());
  8386. CUBLAS_CHECK(
  8387. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8388. ne01, ne11, ne10,
  8389. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8390. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8391. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8392. ne23,
  8393. CUBLAS_COMPUTE_16F,
  8394. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8395. if (src0_as != 0) {
  8396. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8397. }
  8398. if (ptrs_src_s != 0) {
  8399. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8400. }
  8401. if (ptrs_dst_s != 0) {
  8402. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8403. }
  8404. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8405. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8406. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8407. ggml_cuda_pool_free(dst_f16, dst_as);
  8408. }
  8409. #endif
  8410. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8411. #if 0
  8412. ggml_cuda_mul_mat_id_cublas(dst);
  8413. // TODO: mmq/mmv support
  8414. #endif
  8415. const size_t nb11 = src1->nb[1];
  8416. const size_t nb1 = dst->nb[1];
  8417. const struct ggml_tensor * ids = src0;
  8418. const int32_t id = ((int32_t *) dst->op_params)[0];
  8419. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  8420. std::vector<char> ids_host(ggml_nbytes(ids));
  8421. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  8422. if (ids->backend == GGML_BACKEND_GPU) {
  8423. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  8424. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  8425. CUDA_CHECK(cudaStreamSynchronize(stream));
  8426. } else {
  8427. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  8428. }
  8429. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  8430. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  8431. ggml_tensor_extra_gpu src1_row_extra;
  8432. ggml_tensor_extra_gpu dst_row_extra;
  8433. ggml_tensor src1_row = *src1;
  8434. ggml_tensor dst_row = *dst;
  8435. src1_row.backend = GGML_BACKEND_GPU;
  8436. dst_row.backend = GGML_BACKEND_GPU;
  8437. src1_row.extra = &src1_row_extra;
  8438. dst_row.extra = &dst_row_extra;
  8439. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  8440. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  8441. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  8442. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  8443. if (src1->ne[1] == 1) {
  8444. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8445. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  8446. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8447. //int32_t row_id;
  8448. //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]));
  8449. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  8450. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8451. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8452. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8453. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  8454. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  8455. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  8456. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  8457. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8458. }
  8459. } else {
  8460. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  8461. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  8462. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  8463. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  8464. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  8465. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  8466. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  8467. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  8468. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  8469. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8470. int64_t num_src1_rows = 0;
  8471. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8472. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8473. if (row_id_i != row_id) {
  8474. continue;
  8475. }
  8476. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8477. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  8478. nb11, src1_kind, stream));
  8479. num_src1_rows++;
  8480. }
  8481. if (num_src1_rows == 0) {
  8482. continue;
  8483. }
  8484. src1_row.ne[1] = num_src1_rows;
  8485. dst_row.ne[1] = num_src1_rows;
  8486. src1_row.nb[1] = nb11;
  8487. src1_row.nb[2] = num_src1_rows*nb11;
  8488. src1_row.nb[3] = num_src1_rows*nb11;
  8489. dst_row.nb[1] = nb1;
  8490. dst_row.nb[2] = num_src1_rows*nb1;
  8491. dst_row.nb[3] = num_src1_rows*nb1;
  8492. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8493. num_src1_rows = 0;
  8494. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8495. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8496. if (row_id_i != row_id) {
  8497. continue;
  8498. }
  8499. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8500. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  8501. nb1, dst_kind, stream));
  8502. num_src1_rows++;
  8503. }
  8504. }
  8505. }
  8506. if (dst->backend == GGML_BACKEND_CPU) {
  8507. CUDA_CHECK(cudaStreamSynchronize(stream));
  8508. }
  8509. }
  8510. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8511. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  8512. }
  8513. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8514. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  8515. }
  8516. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8517. const int64_t ne = ggml_nelements(src0);
  8518. GGML_ASSERT(ne == ggml_nelements(src1));
  8519. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  8520. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8521. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  8522. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  8523. const int64_t ne00 = src0->ne[0];
  8524. const int64_t ne01 = src0->ne[1];
  8525. const int64_t ne02 = src0->ne[2];
  8526. //GGML_ASSERT(src0->ne[3] == 1);
  8527. const int64_t nb00 = src0->nb[0];
  8528. const int64_t nb01 = src0->nb[1];
  8529. const int64_t nb02 = src0->nb[2];
  8530. const int64_t nb03 = src0->nb[3];
  8531. const int64_t ne10 = src1->ne[0];
  8532. const int64_t ne11 = src1->ne[1];
  8533. const int64_t ne12 = src1->ne[2];
  8534. //GGML_ASSERT(src1->ne[3] == 1);
  8535. const int64_t nb10 = src1->nb[0];
  8536. const int64_t nb11 = src1->nb[1];
  8537. const int64_t nb12 = src1->nb[2];
  8538. const int64_t nb13 = src1->nb[3];
  8539. ggml_cuda_set_device(g_main_device);
  8540. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8541. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8542. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8543. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8544. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  8545. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  8546. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8547. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  8548. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8549. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  8550. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8551. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  8552. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8553. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  8554. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8555. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  8556. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8557. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  8558. ggml_cpy_f16_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8559. } else {
  8560. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  8561. ggml_type_name(src0->type), ggml_type_name(src1->type));
  8562. GGML_ASSERT(false);
  8563. }
  8564. (void) dst;
  8565. }
  8566. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8567. // TODO: why do we pass dst as src1 here?
  8568. ggml_cuda_cpy(src0, dst, nullptr);
  8569. (void) src1;
  8570. }
  8571. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8572. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  8573. }
  8574. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8575. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  8576. }
  8577. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8578. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8579. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  8580. }
  8581. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8582. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  8583. }
  8584. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8585. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  8586. }
  8587. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8588. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  8589. }
  8590. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8591. GGML_ASSERT(ggml_is_contiguous(src0));
  8592. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  8593. }
  8594. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8595. GGML_ASSERT(ggml_is_contiguous(src0));
  8596. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  8597. }
  8598. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8599. (void) src0;
  8600. (void) src1;
  8601. (void) dst;
  8602. }
  8603. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  8604. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  8605. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  8606. }
  8607. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  8608. if (main_device >= g_device_count) {
  8609. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8610. main_device, g_device_count, g_main_device);
  8611. return;
  8612. }
  8613. if (g_main_device != main_device && g_device_count > 1) {
  8614. g_main_device = main_device;
  8615. //cudaDeviceProp prop;
  8616. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8617. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8618. }
  8619. }
  8620. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8621. if (!g_cublas_loaded) return false;
  8622. ggml_cuda_func_t func;
  8623. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  8624. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  8625. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  8626. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8627. return false;
  8628. }
  8629. if (tensor->op == GGML_OP_MUL_MAT) {
  8630. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8631. #ifndef NDEBUG
  8632. 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]);
  8633. #endif
  8634. return false;
  8635. }
  8636. }
  8637. switch (tensor->op) {
  8638. case GGML_OP_REPEAT:
  8639. func = ggml_cuda_repeat;
  8640. break;
  8641. case GGML_OP_GET_ROWS:
  8642. func = ggml_cuda_get_rows;
  8643. break;
  8644. case GGML_OP_DUP:
  8645. func = ggml_cuda_dup;
  8646. break;
  8647. case GGML_OP_ADD:
  8648. func = ggml_cuda_add;
  8649. break;
  8650. case GGML_OP_ACC:
  8651. func = ggml_cuda_acc;
  8652. break;
  8653. case GGML_OP_MUL:
  8654. func = ggml_cuda_mul;
  8655. break;
  8656. case GGML_OP_DIV:
  8657. func = ggml_cuda_div;
  8658. break;
  8659. case GGML_OP_UNARY:
  8660. switch (ggml_get_unary_op(tensor)) {
  8661. case GGML_UNARY_OP_GELU:
  8662. func = ggml_cuda_gelu;
  8663. break;
  8664. case GGML_UNARY_OP_SILU:
  8665. func = ggml_cuda_silu;
  8666. break;
  8667. case GGML_UNARY_OP_GELU_QUICK:
  8668. func = ggml_cuda_gelu_quick;
  8669. break;
  8670. case GGML_UNARY_OP_TANH:
  8671. func = ggml_cuda_tanh;
  8672. break;
  8673. case GGML_UNARY_OP_RELU:
  8674. func = ggml_cuda_relu;
  8675. break;
  8676. case GGML_UNARY_OP_HARDSIGMOID:
  8677. func = ggml_cuda_hardsigmoid;
  8678. break;
  8679. case GGML_UNARY_OP_HARDSWISH:
  8680. func = ggml_cuda_hardswish;
  8681. break;
  8682. default:
  8683. return false;
  8684. }
  8685. break;
  8686. case GGML_OP_NORM:
  8687. func = ggml_cuda_norm;
  8688. break;
  8689. case GGML_OP_GROUP_NORM:
  8690. func = ggml_cuda_group_norm;
  8691. break;
  8692. case GGML_OP_CONCAT:
  8693. func = ggml_cuda_concat;
  8694. break;
  8695. case GGML_OP_UPSCALE:
  8696. func = ggml_cuda_upscale;
  8697. break;
  8698. case GGML_OP_PAD:
  8699. func = ggml_cuda_pad;
  8700. break;
  8701. case GGML_OP_LEAKY_RELU:
  8702. func = ggml_cuda_leaky_relu;
  8703. break;
  8704. case GGML_OP_RMS_NORM:
  8705. func = ggml_cuda_rms_norm;
  8706. break;
  8707. case GGML_OP_MUL_MAT:
  8708. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8709. return false;
  8710. }
  8711. func = ggml_cuda_mul_mat;
  8712. break;
  8713. case GGML_OP_MUL_MAT_ID:
  8714. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8715. return false;
  8716. }
  8717. func = ggml_cuda_mul_mat_id;
  8718. break;
  8719. case GGML_OP_SCALE:
  8720. func = ggml_cuda_scale;
  8721. break;
  8722. case GGML_OP_SQR:
  8723. func = ggml_cuda_sqr;
  8724. break;
  8725. case GGML_OP_CLAMP:
  8726. func = ggml_cuda_clamp;
  8727. break;
  8728. case GGML_OP_CPY:
  8729. func = ggml_cuda_cpy;
  8730. break;
  8731. case GGML_OP_CONT:
  8732. func = ggml_cuda_dup;
  8733. break;
  8734. case GGML_OP_NONE:
  8735. case GGML_OP_RESHAPE:
  8736. case GGML_OP_VIEW:
  8737. case GGML_OP_PERMUTE:
  8738. case GGML_OP_TRANSPOSE:
  8739. func = ggml_cuda_nop;
  8740. break;
  8741. case GGML_OP_DIAG_MASK_INF:
  8742. func = ggml_cuda_diag_mask_inf;
  8743. break;
  8744. case GGML_OP_SOFT_MAX:
  8745. func = ggml_cuda_soft_max;
  8746. break;
  8747. case GGML_OP_ROPE:
  8748. func = ggml_cuda_rope;
  8749. break;
  8750. case GGML_OP_ALIBI:
  8751. func = ggml_cuda_alibi;
  8752. break;
  8753. case GGML_OP_IM2COL:
  8754. func = ggml_cuda_im2col;
  8755. break;
  8756. case GGML_OP_POOL_2D:
  8757. func = ggml_cuda_pool2d;
  8758. break;
  8759. case GGML_OP_SUM_ROWS:
  8760. func = ggml_cuda_sum_rows;
  8761. break;
  8762. case GGML_OP_ARGSORT:
  8763. func = ggml_cuda_argsort;
  8764. break;
  8765. default:
  8766. return false;
  8767. }
  8768. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  8769. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8770. }
  8771. if (params->ith != 0) {
  8772. return true;
  8773. }
  8774. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  8775. return true;
  8776. }
  8777. func(tensor->src[0], tensor->src[1], tensor);
  8778. return true;
  8779. }
  8780. GGML_CALL int ggml_cuda_get_device_count() {
  8781. int device_count;
  8782. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8783. return 0;
  8784. }
  8785. return device_count;
  8786. }
  8787. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8788. cudaDeviceProp prop;
  8789. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8790. snprintf(description, description_size, "%s", prop.name);
  8791. }
  8792. ////////////////////////////////////////////////////////////////////////////////
  8793. // backend interface
  8794. #define UNUSED GGML_UNUSED
  8795. struct ggml_backend_cuda_context {
  8796. int device;
  8797. std::string name;
  8798. };
  8799. // cuda buffer
  8800. struct ggml_backend_cuda_buffer_context {
  8801. int device;
  8802. void * dev_ptr = nullptr;
  8803. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8804. size_t temp_tensor_extra_index = 0;
  8805. std::string name;
  8806. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  8807. device(device), dev_ptr(dev_ptr),
  8808. name(GGML_CUDA_NAME + std::to_string(device)) {
  8809. }
  8810. ~ggml_backend_cuda_buffer_context() {
  8811. delete[] temp_tensor_extras;
  8812. }
  8813. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8814. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  8815. if (temp_tensor_extras == nullptr) {
  8816. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8817. }
  8818. size_t alloc_index = temp_tensor_extra_index;
  8819. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8820. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8821. memset(extra, 0, sizeof(*extra));
  8822. return extra;
  8823. }
  8824. };
  8825. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  8826. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8827. return ctx->name.c_str();
  8828. }
  8829. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  8830. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  8831. }
  8832. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8833. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8834. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8835. delete ctx;
  8836. }
  8837. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8838. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8839. return ctx->dev_ptr;
  8840. }
  8841. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8842. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8843. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8844. assert(tensor->view_src->buffer->buft == buffer->buft);
  8845. tensor->backend = tensor->view_src->backend;
  8846. tensor->extra = tensor->view_src->extra;
  8847. return;
  8848. }
  8849. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8850. extra->data_device[ctx->device] = tensor->data;
  8851. tensor->backend = GGML_BACKEND_GPU;
  8852. tensor->extra = extra;
  8853. if (ggml_is_quantized(tensor->type)) {
  8854. // initialize padding to 0 to avoid possible NaN values
  8855. size_t original_size = ggml_nbytes(tensor);
  8856. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8857. if (padded_size > original_size && tensor->view_src == nullptr) {
  8858. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  8859. }
  8860. }
  8861. }
  8862. GGML_CALL 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) {
  8863. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8864. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8865. ggml_cuda_set_device(ctx->device);
  8866. CUDA_CHECK(cudaDeviceSynchronize());
  8867. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8868. CUDA_CHECK(cudaDeviceSynchronize());
  8869. }
  8870. GGML_CALL 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) {
  8871. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8872. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8873. ggml_cuda_set_device(ctx->device);
  8874. CUDA_CHECK(cudaDeviceSynchronize());
  8875. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8876. CUDA_CHECK(cudaDeviceSynchronize());
  8877. }
  8878. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8879. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  8880. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  8881. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8882. ggml_cuda_set_device(src_ctx->device);
  8883. CUDA_CHECK(cudaDeviceSynchronize());
  8884. ggml_cuda_set_device(dst_ctx->device);
  8885. CUDA_CHECK(cudaDeviceSynchronize());
  8886. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  8887. CUDA_CHECK(cudaDeviceSynchronize());
  8888. return true;
  8889. }
  8890. return false;
  8891. }
  8892. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8893. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8894. ggml_cuda_set_device(ctx->device);
  8895. CUDA_CHECK(cudaDeviceSynchronize());
  8896. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8897. CUDA_CHECK(cudaDeviceSynchronize());
  8898. }
  8899. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  8900. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  8901. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8902. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8903. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8904. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8905. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8906. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  8907. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8908. /* .reset = */ NULL,
  8909. };
  8910. // cuda buffer type
  8911. struct ggml_backend_cuda_buffer_type_context {
  8912. int device;
  8913. std::string name;
  8914. };
  8915. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8916. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8917. return ctx->name.c_str();
  8918. }
  8919. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8920. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8921. ggml_cuda_set_device(buft_ctx->device);
  8922. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  8923. void * dev_ptr;
  8924. cudaError_t err = cudaMalloc(&dev_ptr, size);
  8925. if (err != cudaSuccess) {
  8926. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  8927. return nullptr;
  8928. }
  8929. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  8930. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  8931. }
  8932. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8933. return 128;
  8934. UNUSED(buft);
  8935. }
  8936. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8937. size_t size = ggml_nbytes(tensor);
  8938. int64_t ne0 = tensor->ne[0];
  8939. if (ggml_is_quantized(tensor->type)) {
  8940. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8941. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8942. }
  8943. }
  8944. return size;
  8945. UNUSED(buft);
  8946. }
  8947. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  8948. if (!ggml_backend_is_cuda(backend)) {
  8949. return false;
  8950. }
  8951. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8952. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  8953. return buft_ctx->device == cuda_ctx->device;
  8954. }
  8955. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  8956. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  8957. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  8958. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  8959. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  8960. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  8961. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  8962. /* .is_host = */ NULL,
  8963. };
  8964. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  8965. // FIXME: this is not thread safe
  8966. if (device >= ggml_backend_cuda_get_device_count()) {
  8967. return nullptr;
  8968. }
  8969. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  8970. static bool ggml_backend_cuda_buffer_type_initialized = false;
  8971. if (!ggml_backend_cuda_buffer_type_initialized) {
  8972. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  8973. ggml_backend_cuda_buffer_types[i] = {
  8974. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  8975. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  8976. };
  8977. }
  8978. ggml_backend_cuda_buffer_type_initialized = true;
  8979. }
  8980. return &ggml_backend_cuda_buffer_types[device];
  8981. }
  8982. // cuda split buffer
  8983. struct ggml_backend_cuda_split_buffer_context {
  8984. ~ggml_backend_cuda_split_buffer_context() {
  8985. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  8986. for (int id = 0; id < g_device_count; ++id) {
  8987. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  8988. if (extra->events[id][is] != nullptr) {
  8989. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  8990. }
  8991. }
  8992. if (extra->data_device[id] != nullptr) {
  8993. CUDA_CHECK(cudaFree(extra->data_device[id]));
  8994. }
  8995. }
  8996. delete extra;
  8997. }
  8998. }
  8999. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  9000. };
  9001. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  9002. return GGML_CUDA_NAME "_Split";
  9003. UNUSED(buffer);
  9004. }
  9005. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  9006. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  9007. UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  9008. }
  9009. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9010. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9011. delete ctx;
  9012. }
  9013. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  9014. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  9015. return (void *)0x1000;
  9016. UNUSED(buffer);
  9017. }
  9018. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9019. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  9020. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9021. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9022. const int64_t ne0 = tensor->ne[0];
  9023. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  9024. ctx->tensor_extras.push_back(extra);
  9025. for (int id = 0; id < g_device_count; ++id) {
  9026. int64_t row_low, row_high;
  9027. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9028. int64_t nrows_split = row_high - row_low;
  9029. if (nrows_split == 0) {
  9030. continue;
  9031. }
  9032. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9033. const size_t original_size = size;
  9034. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9035. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9036. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9037. }
  9038. // FIXME: do not crash if cudaMalloc fails
  9039. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  9040. ggml_cuda_set_device(id);
  9041. char * buf;
  9042. CUDA_CHECK(cudaMalloc(&buf, size));
  9043. // set padding to 0 to avoid possible NaN values
  9044. if (size > original_size) {
  9045. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  9046. }
  9047. extra->data_device[id] = buf;
  9048. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9049. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  9050. }
  9051. }
  9052. tensor->backend = GGML_BACKEND_GPU_SPLIT;
  9053. tensor->extra = extra;
  9054. }
  9055. GGML_CALL static void ggml_backend_cuda_split_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9056. // split tensors must always be set in their entirety at once
  9057. GGML_ASSERT(offset == 0);
  9058. GGML_ASSERT(size == ggml_nbytes(tensor));
  9059. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9060. const int64_t ne0 = tensor->ne[0];
  9061. const size_t nb1 = tensor->nb[1];
  9062. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9063. for (int id = 0; id < g_device_count; ++id) {
  9064. int64_t row_low, row_high;
  9065. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9066. int64_t nrows_split = row_high - row_low;
  9067. if (nrows_split == 0) {
  9068. continue;
  9069. }
  9070. const size_t offset_split = row_low*nb1;
  9071. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9072. const size_t original_size = size;
  9073. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9074. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9075. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9076. }
  9077. const char * buf_host = (const char *)data + offset_split;
  9078. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  9079. }
  9080. }
  9081. GGML_CALL static void ggml_backend_cuda_split_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9082. // split tensors must always be set in their entirety at once
  9083. GGML_ASSERT(offset == 0);
  9084. GGML_ASSERT(size == ggml_nbytes(tensor));
  9085. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9086. const int64_t ne0 = tensor->ne[0];
  9087. const size_t nb1 = tensor->nb[1];
  9088. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9089. for (int id = 0; id < g_device_count; ++id) {
  9090. int64_t row_low, row_high;
  9091. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9092. int64_t nrows_split = row_high - row_low;
  9093. if (nrows_split == 0) {
  9094. continue;
  9095. }
  9096. const size_t offset_split = row_low*nb1;
  9097. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9098. const size_t original_size = size;
  9099. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9100. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9101. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9102. }
  9103. char * buf_host = (char *)data + offset_split;
  9104. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  9105. }
  9106. }
  9107. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9108. UNUSED(buffer);
  9109. UNUSED(value);
  9110. }
  9111. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  9112. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  9113. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  9114. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  9115. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  9116. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  9117. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  9118. /* .cpy_tensor = */ NULL,
  9119. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  9120. /* .reset = */ NULL,
  9121. };
  9122. // cuda split buffer type
  9123. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9124. return GGML_CUDA_NAME "_Split";
  9125. UNUSED(buft);
  9126. }
  9127. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9128. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  9129. // instead, we allocate them for each tensor separately in init_tensor
  9130. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  9131. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  9132. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  9133. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  9134. }
  9135. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9136. return 128;
  9137. UNUSED(buft);
  9138. }
  9139. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9140. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9141. size_t total_size = 0;
  9142. const int64_t ne0 = tensor->ne[0];
  9143. for (int id = 0; id < g_device_count; ++id) {
  9144. int64_t row_low, row_high;
  9145. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9146. int64_t nrows_split = row_high - row_low;
  9147. if (nrows_split == 0) {
  9148. continue;
  9149. }
  9150. total_size += ggml_nbytes_split(tensor, nrows_split);
  9151. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9152. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9153. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9154. }
  9155. }
  9156. return total_size;
  9157. }
  9158. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9159. return ggml_backend_is_cuda(backend);
  9160. UNUSED(buft);
  9161. }
  9162. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9163. return false;
  9164. UNUSED(buft);
  9165. }
  9166. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9167. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9168. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9169. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9170. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9171. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9172. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9173. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9174. };
  9175. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9176. // FIXME: this is not thread safe
  9177. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9178. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9179. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9180. if (all_zero) {
  9181. tensor_split_arr = g_default_tensor_split;
  9182. } else {
  9183. float split_sum = 0.0f;
  9184. for (int i = 0; i < g_device_count; ++i) {
  9185. tensor_split_arr[i] = split_sum;
  9186. split_sum += tensor_split[i];
  9187. }
  9188. for (int i = 0; i < g_device_count; ++i) {
  9189. tensor_split_arr[i] /= split_sum;
  9190. }
  9191. }
  9192. auto it = buft_map.find(tensor_split_arr);
  9193. if (it != buft_map.end()) {
  9194. return &it->second;
  9195. }
  9196. struct ggml_backend_buffer_type buft {
  9197. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9198. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9199. };
  9200. auto result = buft_map.emplace(tensor_split_arr, buft);
  9201. return &result.first->second;
  9202. }
  9203. // host buffer type
  9204. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9205. return GGML_CUDA_NAME "_Host";
  9206. UNUSED(buft);
  9207. }
  9208. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9209. return GGML_CUDA_NAME "_Host";
  9210. UNUSED(buffer);
  9211. }
  9212. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9213. ggml_cuda_host_free(buffer->context);
  9214. }
  9215. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9216. void * ptr = ggml_cuda_host_malloc(size);
  9217. if (ptr == nullptr) {
  9218. // fallback to cpu buffer
  9219. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9220. }
  9221. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9222. buffer->buft = buft;
  9223. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9224. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9225. return buffer;
  9226. }
  9227. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9228. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9229. /* .iface = */ {
  9230. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9231. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9232. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9233. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9234. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9235. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9236. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9237. },
  9238. /* .context = */ nullptr,
  9239. };
  9240. return &ggml_backend_cuda_buffer_type_host;
  9241. }
  9242. // backend
  9243. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9244. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9245. return cuda_ctx->name.c_str();
  9246. }
  9247. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9248. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9249. delete cuda_ctx;
  9250. delete backend;
  9251. }
  9252. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9253. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9254. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9255. }
  9256. GGML_CALL static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9257. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9258. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9259. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9260. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9261. }
  9262. GGML_CALL static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9263. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9264. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9265. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9266. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9267. }
  9268. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9269. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9270. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9271. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9272. return true;
  9273. }
  9274. return false;
  9275. }
  9276. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9277. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9278. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9279. UNUSED(backend);
  9280. }
  9281. GGML_CALL static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9282. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9283. ggml_cuda_set_main_device(cuda_ctx->device);
  9284. ggml_compute_params params = {};
  9285. params.type = GGML_TASK_COMPUTE;
  9286. params.ith = 0;
  9287. for (int i = 0; i < cgraph->n_nodes; i++) {
  9288. ggml_tensor * node = cgraph->nodes[i];
  9289. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  9290. continue;
  9291. }
  9292. #ifndef NDEBUG
  9293. assert(node->backend == GGML_BACKEND_GPU || node->backend == GGML_BACKEND_GPU_SPLIT);
  9294. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9295. assert(node->extra != nullptr);
  9296. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9297. if (node->src[j] != nullptr) {
  9298. assert(node->src[j]->backend == GGML_BACKEND_GPU || node->src[j]->backend == GGML_BACKEND_GPU_SPLIT);
  9299. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9300. assert(node->src[j]->extra != nullptr);
  9301. }
  9302. }
  9303. #endif
  9304. bool ok = ggml_cuda_compute_forward(&params, node);
  9305. if (!ok) {
  9306. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9307. }
  9308. GGML_ASSERT(ok);
  9309. }
  9310. return true;
  9311. }
  9312. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9313. switch (op->op) {
  9314. case GGML_OP_UNARY:
  9315. switch (ggml_get_unary_op(op)) {
  9316. case GGML_UNARY_OP_GELU:
  9317. case GGML_UNARY_OP_SILU:
  9318. case GGML_UNARY_OP_RELU:
  9319. case GGML_UNARY_OP_HARDSIGMOID:
  9320. case GGML_UNARY_OP_HARDSWISH:
  9321. case GGML_UNARY_OP_GELU_QUICK:
  9322. case GGML_UNARY_OP_TANH:
  9323. return true;
  9324. default:
  9325. return false;
  9326. }
  9327. break;
  9328. case GGML_OP_MUL_MAT:
  9329. case GGML_OP_MUL_MAT_ID:
  9330. {
  9331. struct ggml_tensor * a;
  9332. struct ggml_tensor * b;
  9333. if (op->op == GGML_OP_MUL_MAT) {
  9334. a = op->src[0];
  9335. b = op->src[1];
  9336. } else {
  9337. a = op->src[2];
  9338. b = op->src[1];
  9339. }
  9340. if (a->ne[3] != b->ne[3]) {
  9341. return false;
  9342. }
  9343. ggml_type a_type = a->type;
  9344. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ1_S) {
  9345. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9346. return false;
  9347. }
  9348. }
  9349. return true;
  9350. } break;
  9351. case GGML_OP_GET_ROWS:
  9352. {
  9353. switch (op->src[0]->type) {
  9354. case GGML_TYPE_F16:
  9355. case GGML_TYPE_F32:
  9356. case GGML_TYPE_Q4_0:
  9357. case GGML_TYPE_Q4_1:
  9358. case GGML_TYPE_Q5_0:
  9359. case GGML_TYPE_Q5_1:
  9360. case GGML_TYPE_Q8_0:
  9361. return true;
  9362. default:
  9363. return false;
  9364. }
  9365. } break;
  9366. case GGML_OP_CPY:
  9367. {
  9368. ggml_type src0_type = op->src[0]->type;
  9369. ggml_type src1_type = op->src[1]->type;
  9370. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9371. return true;
  9372. }
  9373. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9374. return true;
  9375. }
  9376. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9377. return true;
  9378. }
  9379. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9380. return true;
  9381. }
  9382. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9383. return true;
  9384. }
  9385. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9386. return true;
  9387. }
  9388. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9389. return true;
  9390. }
  9391. return false;
  9392. } break;
  9393. case GGML_OP_DUP:
  9394. case GGML_OP_REPEAT:
  9395. case GGML_OP_CONCAT:
  9396. {
  9397. ggml_type src0_type = op->src[0]->type;
  9398. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9399. } break;
  9400. case GGML_OP_NONE:
  9401. case GGML_OP_RESHAPE:
  9402. case GGML_OP_VIEW:
  9403. case GGML_OP_PERMUTE:
  9404. case GGML_OP_TRANSPOSE:
  9405. case GGML_OP_NORM:
  9406. case GGML_OP_ADD:
  9407. case GGML_OP_MUL:
  9408. case GGML_OP_DIV:
  9409. case GGML_OP_RMS_NORM:
  9410. case GGML_OP_SCALE:
  9411. case GGML_OP_SQR:
  9412. case GGML_OP_CLAMP:
  9413. case GGML_OP_CONT:
  9414. case GGML_OP_DIAG_MASK_INF:
  9415. case GGML_OP_SOFT_MAX:
  9416. case GGML_OP_ROPE:
  9417. case GGML_OP_ALIBI:
  9418. case GGML_OP_IM2COL:
  9419. case GGML_OP_POOL_2D:
  9420. case GGML_OP_SUM_ROWS:
  9421. case GGML_OP_ARGSORT:
  9422. case GGML_OP_ACC:
  9423. case GGML_OP_GROUP_NORM:
  9424. case GGML_OP_UPSCALE:
  9425. case GGML_OP_PAD:
  9426. case GGML_OP_LEAKY_RELU:
  9427. return true;
  9428. default:
  9429. return false;
  9430. }
  9431. UNUSED(backend);
  9432. }
  9433. static ggml_backend_i ggml_backend_cuda_interface = {
  9434. /* .get_name = */ ggml_backend_cuda_name,
  9435. /* .free = */ ggml_backend_cuda_free,
  9436. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  9437. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  9438. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  9439. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  9440. /* .synchronize = */ ggml_backend_cuda_synchronize,
  9441. /* .graph_plan_create = */ NULL,
  9442. /* .graph_plan_free = */ NULL,
  9443. /* .graph_plan_compute = */ NULL,
  9444. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  9445. /* .supports_op = */ ggml_backend_cuda_supports_op,
  9446. };
  9447. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  9448. ggml_init_cublas(); // TODO: remove from ggml.c
  9449. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  9450. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  9451. return nullptr;
  9452. }
  9453. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  9454. ggml_cuda_set_main_device(device);
  9455. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  9456. /* .device = */ device,
  9457. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  9458. };
  9459. ggml_backend_t cuda_backend = new ggml_backend {
  9460. /* .interface = */ ggml_backend_cuda_interface,
  9461. /* .context = */ ctx
  9462. };
  9463. return cuda_backend;
  9464. }
  9465. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  9466. return backend && backend->iface.get_name == ggml_backend_cuda_name;
  9467. }
  9468. GGML_CALL int ggml_backend_cuda_get_device_count() {
  9469. return ggml_cuda_get_device_count();
  9470. }
  9471. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  9472. ggml_cuda_get_device_description(device, description, description_size);
  9473. }
  9474. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  9475. ggml_cuda_set_device(device);
  9476. CUDA_CHECK(cudaMemGetInfo(free, total));
  9477. }
  9478. // backend registry
  9479. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  9480. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  9481. return cuda_backend;
  9482. UNUSED(params);
  9483. }
  9484. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  9485. GGML_CALL int ggml_backend_cuda_reg_devices() {
  9486. int device_count = ggml_cuda_get_device_count();
  9487. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  9488. for (int i = 0; i < device_count; i++) {
  9489. char name[128];
  9490. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  9491. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  9492. }
  9493. return device_count;
  9494. }