ggml-cuda.cu 340 KB

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