ggml-cuda.cu 365 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008
  1. #include <algorithm>
  2. #include <assert.h>
  3. #include <atomic>
  4. #include <cinttypes>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <float.h>
  8. #include <limits>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <vector>
  12. #if defined(GGML_USE_HIPBLAS)
  13. #include <hip/hip_runtime.h>
  14. #include <hipblas/hipblas.h>
  15. #include <hip/hip_fp16.h>
  16. #ifdef __HIP_PLATFORM_AMD__
  17. // for rocblas_initialize()
  18. #include "rocblas/rocblas.h"
  19. #endif // __HIP_PLATFORM_AMD__
  20. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  21. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  22. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  23. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  24. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  25. #define CUBLAS_OP_N HIPBLAS_OP_N
  26. #define CUBLAS_OP_T HIPBLAS_OP_T
  27. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  28. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  29. #define CUDA_R_16F HIPBLAS_R_16F
  30. #define CUDA_R_32F HIPBLAS_R_32F
  31. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  32. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  33. #define cublasCreate hipblasCreate
  34. #define cublasGemmEx hipblasGemmEx
  35. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  36. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  37. #define cublasHandle_t hipblasHandle_t
  38. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  39. #define cublasSetStream hipblasSetStream
  40. #define cublasSgemm hipblasSgemm
  41. #define cublasStatus_t hipblasStatus_t
  42. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  43. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  44. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  45. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  46. #define cudaDeviceProp hipDeviceProp_t
  47. #define cudaDeviceSynchronize hipDeviceSynchronize
  48. #define cudaError_t hipError_t
  49. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  50. #define cudaEventDisableTiming hipEventDisableTiming
  51. #define cudaEventRecord hipEventRecord
  52. #define cudaEvent_t hipEvent_t
  53. #define cudaEventDestroy hipEventDestroy
  54. #define cudaFree hipFree
  55. #define cudaFreeHost hipHostFree
  56. #define cudaGetDevice hipGetDevice
  57. #define cudaGetDeviceCount hipGetDeviceCount
  58. #define cudaGetDeviceProperties hipGetDeviceProperties
  59. #define cudaGetErrorString hipGetErrorString
  60. #define cudaGetLastError hipGetLastError
  61. #ifdef GGML_HIP_UMA
  62. #define cudaMalloc hipMallocManaged
  63. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  64. #else
  65. #define cudaMalloc hipMalloc
  66. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  67. #endif
  68. #define cudaMemcpy hipMemcpy
  69. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  70. #define cudaMemcpyAsync hipMemcpyAsync
  71. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  72. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  73. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  74. #define cudaMemcpyKind hipMemcpyKind
  75. #define cudaMemset hipMemset
  76. #define cudaMemsetAsync hipMemsetAsync
  77. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  78. #define cudaSetDevice hipSetDevice
  79. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  80. #define cudaStreamFireAndForget hipStreamFireAndForget
  81. #define cudaStreamNonBlocking hipStreamNonBlocking
  82. #define cudaStreamSynchronize hipStreamSynchronize
  83. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  84. #define cudaStream_t hipStream_t
  85. #define cudaSuccess hipSuccess
  86. #define __trap abort
  87. #else
  88. #include <cuda_runtime.h>
  89. #include <cublas_v2.h>
  90. #include <cuda_fp16.h>
  91. // CUDA 10.2 does not have these macro definitions.
  92. #ifndef CUBLAS_TF32_TENSOR_OP_MATH
  93. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  94. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  95. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  96. #define cublasComputeType_t cudaDataType_t
  97. #endif
  98. #endif // defined(GGML_USE_HIPBLAS)
  99. #include "ggml-cuda.h"
  100. #include "ggml.h"
  101. #include "ggml-backend-impl.h"
  102. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  103. #define CC_VOLTA 700
  104. #define CC_OFFSET_AMD 1000000
  105. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  106. #define GGML_CUDA_MAX_NODES 8192
  107. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  108. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  109. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  110. // - 7B quantum model: +100-200 MB
  111. // - 13B quantum model: +200-400 MB
  112. //
  113. //#define GGML_CUDA_FORCE_MMQ
  114. // TODO: improve this to be correct for more hardware
  115. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  116. // probably other such cases, and not sure what happens on AMD hardware
  117. #if !defined(GGML_CUDA_FORCE_MMQ)
  118. #define CUDA_USE_TENSOR_CORES
  119. #endif
  120. // max batch size to use MMQ kernels when tensor cores are available
  121. #define MMQ_MAX_BATCH_SIZE 32
  122. #if defined(GGML_USE_HIPBLAS)
  123. #define __CUDA_ARCH__ 1300
  124. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  125. defined(__gfx1150__) || defined(__gfx1151__)
  126. #define RDNA3
  127. #endif
  128. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  129. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  130. #define RDNA2
  131. #endif
  132. #ifndef __has_builtin
  133. #define __has_builtin(x) 0
  134. #endif
  135. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  136. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  137. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  138. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  139. #if __has_builtin(__builtin_elementwise_sub_sat)
  140. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  141. return reinterpret_cast<const int&>(c);
  142. #else
  143. int8x4_t c;
  144. int16_t tmp;
  145. #pragma unroll
  146. for (int i = 0; i < 4; i++) {
  147. tmp = va[i] - vb[i];
  148. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  149. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  150. c[i] = tmp;
  151. }
  152. return reinterpret_cast<int&>(c);
  153. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  154. }
  155. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  156. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  157. c = __builtin_amdgcn_sdot4(a, b, c, false);
  158. #elif defined(__gfx1100__)
  159. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  160. #elif defined(__gfx1010__) || defined(__gfx900__)
  161. int tmp1;
  162. int tmp2;
  163. asm("\n \
  164. 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 \
  165. 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 \
  166. v_add3_u32 %0, %1, %2, %0 \n \
  167. 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 \
  168. 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 \
  169. v_add3_u32 %0, %1, %2, %0 \n \
  170. "
  171. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  172. : "v"(a), "v"(b)
  173. );
  174. #else
  175. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  176. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  177. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  178. #endif
  179. return c;
  180. }
  181. #endif // defined(GGML_USE_HIPBLAS)
  182. #if defined(_MSC_VER)
  183. #pragma warning(disable: 4244 4267) // possible loss of data
  184. #endif
  185. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  186. #define CUDA_CHECK(err) \
  187. do { \
  188. cudaError_t err_ = (err); \
  189. if (err_ != cudaSuccess) { \
  190. int id; \
  191. cudaGetDevice(&id); \
  192. fprintf(stderr, "\nCUDA error %d at %s:%d: %s\n", err_, __FILE__, __LINE__, \
  193. cudaGetErrorString(err_)); \
  194. fprintf(stderr, "current device: %d\n", id); \
  195. GGML_ASSERT(!"CUDA error"); \
  196. } \
  197. } while (0)
  198. #if CUDART_VERSION >= 12000
  199. #define CUBLAS_CHECK(err) \
  200. do { \
  201. cublasStatus_t err_ = (err); \
  202. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  203. int id; \
  204. cudaGetDevice(&id); \
  205. fprintf(stderr, "\ncuBLAS error %d at %s:%d: %s\n", \
  206. err_, __FILE__, __LINE__, cublasGetStatusString(err_)); \
  207. fprintf(stderr, "current device: %d\n", id); \
  208. GGML_ASSERT(!"cuBLAS error"); \
  209. } \
  210. } while (0)
  211. #else
  212. #define CUBLAS_CHECK(err) \
  213. do { \
  214. cublasStatus_t err_ = (err); \
  215. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  216. int id; \
  217. cudaGetDevice(&id); \
  218. fprintf(stderr, "\ncuBLAS error %d at %s:%d\n", err_, __FILE__, __LINE__); \
  219. fprintf(stderr, "current device: %d\n", id); \
  220. GGML_ASSERT(!"cuBLAS error"); \
  221. } \
  222. } while (0)
  223. #endif // CUDART_VERSION >= 11
  224. #if CUDART_VERSION >= 11100
  225. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  226. #else
  227. #define GGML_CUDA_ASSUME(x)
  228. #endif // CUDART_VERSION >= 11100
  229. #ifdef GGML_CUDA_F16
  230. typedef half dfloat; // dequantize float
  231. typedef half2 dfloat2;
  232. #else
  233. typedef float dfloat; // dequantize float
  234. typedef float2 dfloat2;
  235. #endif //GGML_CUDA_F16
  236. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  237. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  238. int x32 = 0;
  239. x32 |= x16[0] << 0;
  240. x32 |= x16[1] << 16;
  241. return x32;
  242. }
  243. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  244. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  245. int x32 = 0;
  246. x32 |= x16[0] << 0;
  247. x32 |= x16[1] << 16;
  248. return x32;
  249. }
  250. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  251. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  252. }
  253. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  254. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  255. }
  256. template<typename T>
  257. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  258. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  259. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  260. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  261. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  262. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  263. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  264. typedef void (*ggml_cuda_op_mul_mat_t)(
  265. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  266. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  267. const int64_t src1_padded_row_size, const cudaStream_t & stream);
  268. typedef void (*ggml_cuda_op_flatten_t)(
  269. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  270. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream);
  271. // QK = number of values after dequantization
  272. // QR = QK / number of values before dequantization
  273. // QI = number of 32 bit integers before dequantization
  274. #define QK4_0 32
  275. #define QR4_0 2
  276. #define QI4_0 (QK4_0 / (4 * QR4_0))
  277. typedef struct {
  278. half d; // delta
  279. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  280. } block_q4_0;
  281. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  282. #define QK4_1 32
  283. #define QR4_1 2
  284. #define QI4_1 (QK4_1 / (4 * QR4_1))
  285. typedef struct {
  286. half2 dm; // dm.x = delta, dm.y = min
  287. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  288. } block_q4_1;
  289. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  290. #define QK5_0 32
  291. #define QR5_0 2
  292. #define QI5_0 (QK5_0 / (4 * QR5_0))
  293. typedef struct {
  294. half d; // delta
  295. uint8_t qh[4]; // 5-th bit of quants
  296. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  297. } block_q5_0;
  298. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  299. #define QK5_1 32
  300. #define QR5_1 2
  301. #define QI5_1 (QK5_1 / (4 * QR5_1))
  302. typedef struct {
  303. half2 dm; // dm.x = delta, dm.y = min
  304. uint8_t qh[4]; // 5-th bit of quants
  305. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  306. } block_q5_1;
  307. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  308. #define QK8_0 32
  309. #define QR8_0 1
  310. #define QI8_0 (QK8_0 / (4 * QR8_0))
  311. typedef struct {
  312. half d; // delta
  313. int8_t qs[QK8_0]; // quants
  314. } block_q8_0;
  315. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  316. #define QK8_1 32
  317. #define QR8_1 1
  318. #define QI8_1 (QK8_1 / (4 * QR8_1))
  319. typedef struct {
  320. half2 ds; // ds.x = delta, ds.y = sum
  321. int8_t qs[QK8_0]; // quants
  322. } block_q8_1;
  323. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  324. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  325. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  326. typedef void (*load_tiles_cuda_t)(
  327. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  328. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  329. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  330. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  331. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  332. //================================= k-quants
  333. #ifdef GGML_QKK_64
  334. #define QK_K 64
  335. #define K_SCALE_SIZE 4
  336. #else
  337. #define QK_K 256
  338. #define K_SCALE_SIZE 12
  339. #endif
  340. #define QR2_K 4
  341. #define QI2_K (QK_K / (4*QR2_K))
  342. typedef struct {
  343. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  344. uint8_t qs[QK_K/4]; // quants
  345. half2 dm; // super-block scale for quantized scales/mins
  346. } block_q2_K;
  347. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  348. #define QR3_K 4
  349. #define QI3_K (QK_K / (4*QR3_K))
  350. typedef struct {
  351. uint8_t hmask[QK_K/8]; // quants - high bit
  352. uint8_t qs[QK_K/4]; // quants - low 2 bits
  353. #ifdef GGML_QKK_64
  354. uint8_t scales[2]; // scales, quantized with 8 bits
  355. #else
  356. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  357. #endif
  358. half d; // super-block scale
  359. } block_q3_K;
  360. //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");
  361. #define QR4_K 2
  362. #define QI4_K (QK_K / (4*QR4_K))
  363. #ifdef GGML_QKK_64
  364. typedef struct {
  365. half dm[2]; // super-block scales/mins
  366. uint8_t scales[2]; // 4-bit block scales/mins
  367. uint8_t qs[QK_K/2]; // 4--bit quants
  368. } block_q4_K;
  369. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  370. #else
  371. typedef struct {
  372. half2 dm; // super-block scale for quantized scales/mins
  373. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  374. uint8_t qs[QK_K/2]; // 4--bit quants
  375. } block_q4_K;
  376. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  377. #endif
  378. #define QR5_K 2
  379. #define QI5_K (QK_K / (4*QR5_K))
  380. #ifdef GGML_QKK_64
  381. typedef struct {
  382. half d; // super-block scale
  383. int8_t scales[QK_K/16]; // block scales
  384. uint8_t qh[QK_K/8]; // quants, high bit
  385. uint8_t qs[QK_K/2]; // quants, low 4 bits
  386. } block_q5_K;
  387. 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");
  388. #else
  389. typedef struct {
  390. half2 dm; // super-block scale for quantized scales/mins
  391. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  392. uint8_t qh[QK_K/8]; // quants, high bit
  393. uint8_t qs[QK_K/2]; // quants, low 4 bits
  394. } block_q5_K;
  395. 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");
  396. #endif
  397. #define QR6_K 2
  398. #define QI6_K (QK_K / (4*QR6_K))
  399. typedef struct {
  400. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  401. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  402. int8_t scales[QK_K/16]; // scales
  403. half d; // delta
  404. } block_q6_K;
  405. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  406. #define WARP_SIZE 32
  407. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  408. #define CUDA_GELU_BLOCK_SIZE 256
  409. #define CUDA_SILU_BLOCK_SIZE 256
  410. #define CUDA_TANH_BLOCK_SIZE 256
  411. #define CUDA_RELU_BLOCK_SIZE 256
  412. #define CUDA_SQR_BLOCK_SIZE 256
  413. #define CUDA_CPY_BLOCK_SIZE 32
  414. #define CUDA_SCALE_BLOCK_SIZE 256
  415. #define CUDA_CLAMP_BLOCK_SIZE 256
  416. #define CUDA_ROPE_BLOCK_SIZE 256
  417. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  418. #define CUDA_ALIBI_BLOCK_SIZE 32
  419. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  420. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  421. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  422. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  423. #define CUDA_UPSCALE_BLOCK_SIZE 256
  424. #define CUDA_CONCAT_BLOCK_SIZE 256
  425. #define CUDA_PAD_BLOCK_SIZE 256
  426. #define CUDA_ACC_BLOCK_SIZE 256
  427. #define CUDA_IM2COL_BLOCK_SIZE 256
  428. // dmmv = dequantize_mul_mat_vec
  429. #ifndef GGML_CUDA_DMMV_X
  430. #define GGML_CUDA_DMMV_X 32
  431. #endif
  432. #ifndef GGML_CUDA_MMV_Y
  433. #define GGML_CUDA_MMV_Y 1
  434. #endif
  435. #ifndef K_QUANTS_PER_ITERATION
  436. #define K_QUANTS_PER_ITERATION 2
  437. #else
  438. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  439. #endif
  440. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  441. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  442. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  443. #define MUL_MAT_SRC1_COL_STRIDE 128
  444. #define MAX_STREAMS 8
  445. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  446. struct ggml_tensor_extra_gpu {
  447. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  448. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  449. };
  450. // this is faster on Windows
  451. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  452. inline cudaError_t ggml_cuda_set_device(const int device) {
  453. int current_device;
  454. CUDA_CHECK(cudaGetDevice(&current_device));
  455. if (device == current_device) {
  456. return cudaSuccess;
  457. }
  458. return cudaSetDevice(device);
  459. }
  460. static int g_device_count = -1;
  461. static int g_main_device = 0;
  462. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  463. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  464. static void * g_scratch_buffer = nullptr;
  465. static size_t g_scratch_size = 0; // disabled by default
  466. static size_t g_scratch_offset = 0;
  467. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  468. [[noreturn]]
  469. static __device__ void bad_arch() {
  470. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  471. __trap();
  472. (void) bad_arch; // suppress unused function warning
  473. }
  474. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  475. #pragma unroll
  476. for (int mask = 16; mask > 0; mask >>= 1) {
  477. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  478. }
  479. return x;
  480. }
  481. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  482. #pragma unroll
  483. for (int mask = 16; mask > 0; mask >>= 1) {
  484. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  485. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  486. }
  487. return a;
  488. }
  489. static __device__ __forceinline__ float warp_reduce_max(float x) {
  490. #pragma unroll
  491. for (int mask = 16; mask > 0; mask >>= 1) {
  492. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  493. }
  494. return x;
  495. }
  496. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  497. return b;
  498. }
  499. static __device__ __forceinline__ float op_add(const float a, const float b) {
  500. return a + b;
  501. }
  502. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  503. return a * b;
  504. }
  505. static __device__ __forceinline__ float op_div(const float a, const float b) {
  506. return a / b;
  507. }
  508. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  509. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  510. int ne0, int ne1, int ne2, int ne3,
  511. int ne10, int ne11, int ne12, int ne13,
  512. /*int s0, */ int s1, int s2, int s3,
  513. /*int s10,*/ int s11, int s12, int s13) {
  514. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  515. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  516. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  517. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  518. if (i0s >= 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. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  531. const int i10 = i0 % ne10;
  532. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  533. }
  534. }
  535. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  536. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  537. int ne0, int ne1, int ne2, int ne3,
  538. int ne10, int ne11, int ne12, int ne13,
  539. /*int s0, */ int s1, int s2, int s3,
  540. /*int s10,*/ int s11, int s12, int s13) {
  541. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  542. const int i3 = i/(ne2*ne1*ne0);
  543. const int i2 = (i/(ne1*ne0)) % ne2;
  544. const int i1 = (i/ne0) % ne1;
  545. const int i0 = i % ne0;
  546. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  547. return;
  548. }
  549. const int i11 = i1 % ne11;
  550. const int i12 = i2 % ne12;
  551. const int i13 = i3 % ne13;
  552. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  553. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  554. const size_t i_dst = i_src0;
  555. const src0_t * src0_row = src0 + i_src0;
  556. const src1_t * src1_row = src1 + i_src1;
  557. dst_t * dst_row = dst + i_dst;
  558. const int i10 = i0 % ne10;
  559. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  560. }
  561. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  562. const int ne10, const int ne11, const int ne12,
  563. const int nb1, const int nb2, int offset) {
  564. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  565. if (i >= ne) {
  566. return;
  567. }
  568. int src1_idx = i - offset;
  569. int oz = src1_idx / nb2;
  570. int oy = (src1_idx - (oz * nb2)) / nb1;
  571. int ox = src1_idx % nb1;
  572. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  573. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  574. } else {
  575. dst[i] = x[i];
  576. }
  577. }
  578. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  579. const float GELU_COEF_A = 0.044715f;
  580. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  581. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  582. if (i >= k) {
  583. return;
  584. }
  585. float xi = x[i];
  586. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  587. }
  588. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  589. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  590. if (i >= k) {
  591. return;
  592. }
  593. dst[i] = x[i] / (1.0f + expf(-x[i]));
  594. }
  595. static __global__ void gelu_quick_f32(const float *x, float *dst, int k) {
  596. const float GELU_QUICK_COEF = -1.702f;
  597. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  598. if (i >= k) {
  599. return;
  600. }
  601. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  602. }
  603. static __global__ void tanh_f32(const float *x, float *dst, int k) {
  604. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  605. if (i >= k) {
  606. return;
  607. }
  608. dst[i] = tanhf(x[i]);
  609. }
  610. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  611. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  612. if (i >= k) {
  613. return;
  614. }
  615. dst[i] = fmaxf(x[i], 0);
  616. }
  617. static __global__ void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope) {
  618. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  619. if (i >= k) {
  620. return;
  621. }
  622. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  623. }
  624. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  625. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  626. if (i >= k) {
  627. return;
  628. }
  629. dst[i] = x[i] * x[i];
  630. }
  631. template <int block_size>
  632. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  633. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  634. const int tid = threadIdx.x;
  635. float2 mean_var = make_float2(0.f, 0.f);
  636. for (int col = tid; col < ncols; col += block_size) {
  637. const float xi = x[row*ncols + col];
  638. mean_var.x += xi;
  639. mean_var.y += xi * xi;
  640. }
  641. // sum up partial sums
  642. mean_var = warp_reduce_sum(mean_var);
  643. if (block_size > WARP_SIZE) {
  644. __shared__ float2 s_sum[32];
  645. int warp_id = threadIdx.x / WARP_SIZE;
  646. int lane_id = threadIdx.x % WARP_SIZE;
  647. if (lane_id == 0) {
  648. s_sum[warp_id] = mean_var;
  649. }
  650. __syncthreads();
  651. mean_var = s_sum[lane_id];
  652. mean_var = warp_reduce_sum(mean_var);
  653. }
  654. const float mean = mean_var.x / ncols;
  655. const float var = mean_var.y / ncols - mean * mean;
  656. const float inv_std = rsqrtf(var + eps);
  657. for (int col = tid; col < ncols; col += block_size) {
  658. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  659. }
  660. }
  661. static __global__ void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02) {
  662. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  663. if (nidx >= ne0) {
  664. return;
  665. }
  666. // operation
  667. int offset_dst =
  668. nidx +
  669. blockIdx.y * ne0 +
  670. blockIdx.z * ne0 * gridDim.y;
  671. if (blockIdx.z < ne02) { // src0
  672. int offset_src =
  673. nidx +
  674. blockIdx.y * ne0 +
  675. blockIdx.z * ne0 * gridDim.y;
  676. dst[offset_dst] = x[offset_src];
  677. } else {
  678. int offset_src =
  679. nidx +
  680. blockIdx.y * ne0 +
  681. (blockIdx.z - ne02) * ne0 * gridDim.y;
  682. dst[offset_dst] = y[offset_src];
  683. }
  684. }
  685. static __global__ void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor) {
  686. int ne0 = ne00 * scale_factor;
  687. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  688. if (nidx >= ne0) {
  689. return;
  690. }
  691. // operation
  692. int i00 = nidx / scale_factor;
  693. int i01 = blockIdx.y / scale_factor;
  694. int offset_src =
  695. i00 +
  696. i01 * ne00 +
  697. blockIdx.z * nb02;
  698. int offset_dst =
  699. nidx +
  700. blockIdx.y * ne0 +
  701. blockIdx.z * ne0 * gridDim.y;
  702. dst[offset_dst] = x[offset_src];
  703. }
  704. static __global__ void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  705. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  706. if (nidx >= ne0) {
  707. return;
  708. }
  709. // operation
  710. int offset_dst =
  711. nidx +
  712. blockIdx.y * ne0 +
  713. blockIdx.z * ne0 * gridDim.y;
  714. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  715. int offset_src =
  716. nidx +
  717. blockIdx.y * ne00 +
  718. blockIdx.z * ne00 * ne01;
  719. dst[offset_dst] = x[offset_src];
  720. } else {
  721. dst[offset_dst] = 0.0f;
  722. }
  723. }
  724. template <int block_size>
  725. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  726. int start = blockIdx.x * group_size;
  727. int end = start + group_size;
  728. start += threadIdx.x;
  729. if (end >= ne_elements) {
  730. end = ne_elements;
  731. }
  732. float tmp = 0.0f; // partial sum for thread in warp
  733. for (int j = start; j < end; j += block_size) {
  734. tmp += x[j];
  735. }
  736. tmp = warp_reduce_sum(tmp);
  737. if (block_size > WARP_SIZE) {
  738. __shared__ float s_sum[32];
  739. int warp_id = threadIdx.x / WARP_SIZE;
  740. int lane_id = threadIdx.x % WARP_SIZE;
  741. if (lane_id == 0) {
  742. s_sum[warp_id] = tmp;
  743. }
  744. __syncthreads();
  745. tmp = s_sum[lane_id];
  746. tmp = warp_reduce_sum(tmp);
  747. }
  748. float mean = tmp / group_size;
  749. tmp = 0.0f;
  750. for (int j = start; j < end; j += block_size) {
  751. float xi = x[j] - mean;
  752. dst[j] = xi;
  753. tmp += xi * xi;
  754. }
  755. tmp = warp_reduce_sum(tmp);
  756. if (block_size > WARP_SIZE) {
  757. __shared__ float s_sum[32];
  758. int warp_id = threadIdx.x / WARP_SIZE;
  759. int lane_id = threadIdx.x % WARP_SIZE;
  760. if (lane_id == 0) {
  761. s_sum[warp_id] = tmp;
  762. }
  763. __syncthreads();
  764. tmp = s_sum[lane_id];
  765. tmp = warp_reduce_sum(tmp);
  766. }
  767. float variance = tmp / group_size;
  768. float scale = rsqrtf(variance + eps);
  769. for (int j = start; j < end; j += block_size) {
  770. dst[j] *= scale;
  771. }
  772. }
  773. template <int block_size>
  774. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  775. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  776. const int tid = threadIdx.x;
  777. float tmp = 0.0f; // partial sum for thread in warp
  778. for (int col = tid; col < ncols; col += block_size) {
  779. const float xi = x[row*ncols + col];
  780. tmp += xi * xi;
  781. }
  782. // sum up partial sums
  783. tmp = warp_reduce_sum(tmp);
  784. if (block_size > WARP_SIZE) {
  785. __shared__ float s_sum[32];
  786. int warp_id = threadIdx.x / WARP_SIZE;
  787. int lane_id = threadIdx.x % WARP_SIZE;
  788. if (lane_id == 0) {
  789. s_sum[warp_id] = tmp;
  790. }
  791. __syncthreads();
  792. tmp = s_sum[lane_id];
  793. tmp = warp_reduce_sum(tmp);
  794. }
  795. const float mean = tmp / ncols;
  796. const float scale = rsqrtf(mean + eps);
  797. for (int col = tid; col < ncols; col += block_size) {
  798. dst[row*ncols + col] = scale * x[row*ncols + col];
  799. }
  800. }
  801. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  802. const block_q4_0 * x = (const block_q4_0 *) vx;
  803. const dfloat d = x[ib].d;
  804. const int vui = x[ib].qs[iqs];
  805. v.x = vui & 0xF;
  806. v.y = vui >> 4;
  807. #ifdef GGML_CUDA_F16
  808. v = __hsub2(v, {8.0f, 8.0f});
  809. v = __hmul2(v, {d, d});
  810. #else
  811. v.x = (v.x - 8.0f) * d;
  812. v.y = (v.y - 8.0f) * d;
  813. #endif // GGML_CUDA_F16
  814. }
  815. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  816. const block_q4_1 * x = (const block_q4_1 *) vx;
  817. const dfloat d = __low2half(x[ib].dm);
  818. const dfloat m = __high2half(x[ib].dm);
  819. const int vui = x[ib].qs[iqs];
  820. v.x = vui & 0xF;
  821. v.y = vui >> 4;
  822. #ifdef GGML_CUDA_F16
  823. v = __hmul2(v, {d, d});
  824. v = __hadd2(v, {m, m});
  825. #else
  826. v.x = (v.x * d) + m;
  827. v.y = (v.y * d) + m;
  828. #endif // GGML_CUDA_F16
  829. }
  830. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  831. const block_q5_0 * x = (const block_q5_0 *) vx;
  832. const dfloat d = x[ib].d;
  833. uint32_t qh;
  834. memcpy(&qh, x[ib].qh, sizeof(qh));
  835. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  836. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  837. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  838. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  839. #ifdef GGML_CUDA_F16
  840. v = __hsub2(v, {16.0f, 16.0f});
  841. v = __hmul2(v, {d, d});
  842. #else
  843. v.x = (v.x - 16.0f) * d;
  844. v.y = (v.y - 16.0f) * d;
  845. #endif // GGML_CUDA_F16
  846. }
  847. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  848. const block_q5_1 * x = (const block_q5_1 *) vx;
  849. const dfloat d = __low2half(x[ib].dm);
  850. const dfloat m = __high2half(x[ib].dm);
  851. uint32_t qh;
  852. memcpy(&qh, x[ib].qh, sizeof(qh));
  853. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  854. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  855. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  856. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  857. #ifdef GGML_CUDA_F16
  858. v = __hmul2(v, {d, d});
  859. v = __hadd2(v, {m, m});
  860. #else
  861. v.x = (v.x * d) + m;
  862. v.y = (v.y * d) + m;
  863. #endif // GGML_CUDA_F16
  864. }
  865. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  866. const block_q8_0 * x = (const block_q8_0 *) vx;
  867. const dfloat d = x[ib].d;
  868. v.x = x[ib].qs[iqs + 0];
  869. v.y = x[ib].qs[iqs + 1];
  870. #ifdef GGML_CUDA_F16
  871. v = __hmul2(v, {d, d});
  872. #else
  873. v.x *= d;
  874. v.y *= d;
  875. #endif // GGML_CUDA_F16
  876. }
  877. //================================== k-quants
  878. template<typename dst_t>
  879. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  880. const int i = blockIdx.x;
  881. const block_q2_K * x = (const block_q2_K *) vx;
  882. const int tid = threadIdx.x;
  883. #if QK_K == 256
  884. const int n = tid/32;
  885. const int l = tid - 32*n;
  886. const int is = 8*n + l/16;
  887. const uint8_t q = x[i].qs[32*n + l];
  888. dst_t * y = yy + i*QK_K + 128*n;
  889. float dall = __low2half(x[i].dm);
  890. float dmin = __high2half(x[i].dm);
  891. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  892. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  893. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  894. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  895. #else
  896. const int is = tid/16; // 0 or 1
  897. const int il = tid%16; // 0...15
  898. const uint8_t q = x[i].qs[il] >> (2*is);
  899. dst_t * y = yy + i*QK_K + 16*is + il;
  900. float dall = __low2half(x[i].dm);
  901. float dmin = __high2half(x[i].dm);
  902. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  903. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  904. #endif
  905. }
  906. template<typename dst_t>
  907. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  908. const int i = blockIdx.x;
  909. const block_q3_K * x = (const block_q3_K *) vx;
  910. #if QK_K == 256
  911. const int r = threadIdx.x/4;
  912. const int tid = r/2;
  913. const int is0 = r%2;
  914. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  915. const int n = tid / 4;
  916. const int j = tid - 4*n;
  917. uint8_t m = 1 << (4*n + j);
  918. int is = 8*n + 2*j + is0;
  919. int shift = 2*j;
  920. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  921. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  922. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  923. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  924. float d_all = x[i].d;
  925. float dl = d_all * (us - 32);
  926. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  927. const uint8_t * q = x[i].qs + 32*n;
  928. const uint8_t * hm = x[i].hmask;
  929. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  930. #else
  931. const int tid = threadIdx.x;
  932. const int is = tid/16; // 0 or 1
  933. const int il = tid%16; // 0...15
  934. const int im = il/8; // 0...1
  935. const int in = il%8; // 0...7
  936. dst_t * y = yy + i*QK_K + 16*is + il;
  937. const uint8_t q = x[i].qs[il] >> (2*is);
  938. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  939. const float d = (float)x[i].d;
  940. if (is == 0) {
  941. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  942. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  943. } else {
  944. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  945. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  946. }
  947. #endif
  948. }
  949. #if QK_K == 256
  950. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  951. if (j < 4) {
  952. d = q[j] & 63; m = q[j + 4] & 63;
  953. } else {
  954. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  955. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  956. }
  957. }
  958. #endif
  959. template<typename dst_t>
  960. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  961. const block_q4_K * x = (const block_q4_K *) vx;
  962. const int i = blockIdx.x;
  963. #if QK_K == 256
  964. // assume 32 threads
  965. const int tid = threadIdx.x;
  966. const int il = tid/8;
  967. const int ir = tid%8;
  968. const int is = 2*il;
  969. const int n = 4;
  970. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  971. const float dall = __low2half(x[i].dm);
  972. const float dmin = __high2half(x[i].dm);
  973. const uint8_t * q = x[i].qs + 32*il + n*ir;
  974. uint8_t sc, m;
  975. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  976. const float d1 = dall * sc; const float m1 = dmin * m;
  977. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  978. const float d2 = dall * sc; const float m2 = dmin * m;
  979. for (int l = 0; l < n; ++l) {
  980. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  981. y[l +32] = d2 * (q[l] >> 4) - m2;
  982. }
  983. #else
  984. const int tid = threadIdx.x;
  985. const uint8_t * q = x[i].qs;
  986. dst_t * y = yy + i*QK_K;
  987. const float d = (float)x[i].dm[0];
  988. const float m = (float)x[i].dm[1];
  989. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  990. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  991. #endif
  992. }
  993. template<typename dst_t>
  994. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  995. const block_q5_K * x = (const block_q5_K *) vx;
  996. const int i = blockIdx.x;
  997. #if QK_K == 256
  998. // assume 64 threads - this is very slightly better than the one below
  999. const int tid = threadIdx.x;
  1000. const int il = tid/16; // il is in 0...3
  1001. const int ir = tid%16; // ir is in 0...15
  1002. const int is = 2*il; // is is in 0...6
  1003. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1004. const float dall = __low2half(x[i].dm);
  1005. const float dmin = __high2half(x[i].dm);
  1006. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1007. const uint8_t * qh = x[i].qh + 2*ir;
  1008. uint8_t sc, m;
  1009. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1010. const float d1 = dall * sc; const float m1 = dmin * m;
  1011. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1012. const float d2 = dall * sc; const float m2 = dmin * m;
  1013. uint8_t hm = 1 << (2*il);
  1014. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1015. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1016. hm <<= 1;
  1017. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1018. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1019. #else
  1020. const int tid = threadIdx.x;
  1021. const uint8_t q = x[i].qs[tid];
  1022. const int im = tid/8; // 0...3
  1023. const int in = tid%8; // 0...7
  1024. const int is = tid/16; // 0 or 1
  1025. const uint8_t h = x[i].qh[in] >> im;
  1026. const float d = x[i].d;
  1027. dst_t * y = yy + i*QK_K + tid;
  1028. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1029. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1030. #endif
  1031. }
  1032. template<typename dst_t>
  1033. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1034. const block_q6_K * x = (const block_q6_K *) vx;
  1035. const int i = blockIdx.x;
  1036. #if QK_K == 256
  1037. // assume 64 threads - this is very slightly better than the one below
  1038. const int tid = threadIdx.x;
  1039. const int ip = tid/32; // ip is 0 or 1
  1040. const int il = tid - 32*ip; // 0...32
  1041. const int is = 8*ip + il/16;
  1042. dst_t * y = yy + i*QK_K + 128*ip + il;
  1043. const float d = x[i].d;
  1044. const uint8_t * ql = x[i].ql + 64*ip + il;
  1045. const uint8_t qh = x[i].qh[32*ip + il];
  1046. const int8_t * sc = x[i].scales + is;
  1047. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1048. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1049. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1050. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1051. #else
  1052. // assume 32 threads
  1053. const int tid = threadIdx.x;
  1054. const int ip = tid/16; // 0 or 1
  1055. const int il = tid - 16*ip; // 0...15
  1056. dst_t * y = yy + i*QK_K + 16*ip + il;
  1057. const float d = x[i].d;
  1058. const uint8_t ql = x[i].ql[16*ip + il];
  1059. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1060. const int8_t * sc = x[i].scales;
  1061. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1062. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1063. #endif
  1064. }
  1065. 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) {
  1066. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1067. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1068. if (row > nrows) return;
  1069. const int num_blocks_per_row = ncols / QK_K;
  1070. const int ib0 = row*num_blocks_per_row;
  1071. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1072. float tmp = 0; // partial sum for thread in warp
  1073. #if QK_K == 256
  1074. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1075. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1076. const int step = 16/K_QUANTS_PER_ITERATION;
  1077. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1078. const int in = tid - step*im; // 0...15 or 0...7
  1079. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1080. const int q_offset = 32*im + l0;
  1081. const int s_offset = 8*im;
  1082. const int y_offset = 128*im + l0;
  1083. uint32_t aux[4];
  1084. const uint8_t * d = (const uint8_t *)aux;
  1085. const uint8_t * m = (const uint8_t *)(aux + 2);
  1086. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1087. const float * y = yy + i * QK_K + y_offset;
  1088. const uint8_t * q = x[i].qs + q_offset;
  1089. const float dall = __low2half(x[i].dm);
  1090. const float dmin = __high2half(x[i].dm);
  1091. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1092. aux[0] = a[0] & 0x0f0f0f0f;
  1093. aux[1] = a[1] & 0x0f0f0f0f;
  1094. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1095. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1096. float sum1 = 0, sum2 = 0;
  1097. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1098. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1099. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1100. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1101. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1102. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1103. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1104. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1105. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1106. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1107. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1108. }
  1109. tmp += dall * sum1 - dmin * sum2;
  1110. }
  1111. #else
  1112. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1113. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1114. const int offset = tid * K_QUANTS_PER_ITERATION;
  1115. uint32_t uaux[2];
  1116. const uint8_t * d = (const uint8_t *)uaux;
  1117. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1118. const float * y = yy + i * QK_K + offset;
  1119. const uint8_t * q = x[i].qs + offset;
  1120. const uint32_t * s = (const uint32_t *)x[i].scales;
  1121. uaux[0] = s[0] & 0x0f0f0f0f;
  1122. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1123. const float2 dall = __half22float2(x[i].dm);
  1124. float sum1 = 0, sum2 = 0;
  1125. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1126. const uint8_t ql = q[l];
  1127. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1128. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1129. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1130. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1131. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1132. }
  1133. tmp += dall.x * sum1 - dall.y * sum2;
  1134. }
  1135. #endif
  1136. // sum up partial sums and write back result
  1137. #pragma unroll
  1138. for (int mask = 16; mask > 0; mask >>= 1) {
  1139. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1140. }
  1141. if (threadIdx.x == 0) {
  1142. dst[row] = tmp;
  1143. }
  1144. }
  1145. 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) {
  1146. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1147. if (row > nrows) return;
  1148. const int num_blocks_per_row = ncols / QK_K;
  1149. const int ib0 = row*num_blocks_per_row;
  1150. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1151. float tmp = 0; // partial sum for thread in warp
  1152. #if QK_K == 256
  1153. const uint16_t kmask1 = 0x0303;
  1154. const uint16_t kmask2 = 0x0f0f;
  1155. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1156. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1157. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1158. const int step = 16/K_QUANTS_PER_ITERATION;
  1159. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1160. const int in = tid - step*im; // 0....15 or 0...7
  1161. const uint8_t m = 1 << (4*im);
  1162. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1163. const int q_offset = 32*im + l0;
  1164. const int y_offset = 128*im + l0;
  1165. uint16_t utmp[4];
  1166. const int8_t * s = (const int8_t *)utmp;
  1167. const uint16_t s_shift = 4*im;
  1168. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1169. const float * y = yy + i * QK_K + y_offset;
  1170. const uint8_t * q = x[i].qs + q_offset;
  1171. const uint8_t * h = x[i].hmask + l0;
  1172. const uint16_t * a = (const uint16_t *)x[i].scales;
  1173. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1174. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1175. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1176. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1177. const float d = x[i].d;
  1178. float sum = 0;
  1179. for (int l = 0; l < n; ++l) {
  1180. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1181. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1182. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1183. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1184. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1185. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1186. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1187. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1188. }
  1189. tmp += d * sum;
  1190. }
  1191. #else
  1192. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1193. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1194. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1195. const int in = offset/8; // 0 or 1
  1196. const int im = offset%8; // 0...7
  1197. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1198. const float * y = yy + i * QK_K + offset;
  1199. const uint8_t * q = x[i].qs + offset;
  1200. const uint8_t * s = x[i].scales;
  1201. const float dall = (float)x[i].d;
  1202. float sum = 0;
  1203. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1204. const uint8_t hl = x[i].hmask[im+l] >> in;
  1205. const uint8_t ql = q[l];
  1206. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1207. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1208. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1209. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1210. }
  1211. tmp += sum;
  1212. }
  1213. #endif
  1214. // sum up partial sums and write back result
  1215. #pragma unroll
  1216. for (int mask = 16; mask > 0; mask >>= 1) {
  1217. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1218. }
  1219. if (threadIdx.x == 0) {
  1220. dst[row] = tmp;
  1221. }
  1222. }
  1223. 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) {
  1224. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1225. if (row > nrows) return;
  1226. const int num_blocks_per_row = ncols / QK_K;
  1227. const int ib0 = row*num_blocks_per_row;
  1228. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1229. #if QK_K == 256
  1230. const uint16_t kmask1 = 0x3f3f;
  1231. const uint16_t kmask2 = 0x0f0f;
  1232. const uint16_t kmask3 = 0xc0c0;
  1233. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1234. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1235. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1236. const int il = tid/step; // 0...3
  1237. const int ir = tid - step*il; // 0...7 or 0...3
  1238. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1239. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1240. const int in = il%2;
  1241. const int l0 = n*(2*ir + in);
  1242. const int q_offset = 32*im + l0;
  1243. const int y_offset = 64*im + l0;
  1244. uint16_t aux[4];
  1245. const uint8_t * sc = (const uint8_t *)aux;
  1246. #if K_QUANTS_PER_ITERATION == 2
  1247. uint32_t q32[4];
  1248. const uint8_t * q4 = (const uint8_t *)q32;
  1249. #else
  1250. uint16_t q16[4];
  1251. const uint8_t * q4 = (const uint8_t *)q16;
  1252. #endif
  1253. float tmp = 0; // partial sum for thread in warp
  1254. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1255. const float * y1 = yy + i*QK_K + y_offset;
  1256. const float * y2 = y1 + 128;
  1257. const float dall = __low2half(x[i].dm);
  1258. const float dmin = __high2half(x[i].dm);
  1259. const uint16_t * a = (const uint16_t *)x[i].scales;
  1260. aux[0] = a[im+0] & kmask1;
  1261. aux[1] = a[im+2] & kmask1;
  1262. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1263. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1264. #if K_QUANTS_PER_ITERATION == 2
  1265. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1266. const uint32_t * q2 = q1 + 16;
  1267. q32[0] = q1[0] & 0x0f0f0f0f;
  1268. q32[1] = q1[0] & 0xf0f0f0f0;
  1269. q32[2] = q2[0] & 0x0f0f0f0f;
  1270. q32[3] = q2[0] & 0xf0f0f0f0;
  1271. float4 s = {0.f, 0.f, 0.f, 0.f};
  1272. float smin = 0;
  1273. for (int l = 0; l < 4; ++l) {
  1274. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1275. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1276. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1277. }
  1278. 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;
  1279. #else
  1280. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1281. const uint16_t * q2 = q1 + 32;
  1282. q16[0] = q1[0] & 0x0f0f;
  1283. q16[1] = q1[0] & 0xf0f0;
  1284. q16[2] = q2[0] & 0x0f0f;
  1285. q16[3] = q2[0] & 0xf0f0;
  1286. float4 s = {0.f, 0.f, 0.f, 0.f};
  1287. float smin = 0;
  1288. for (int l = 0; l < 2; ++l) {
  1289. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1290. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1291. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1292. }
  1293. 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;
  1294. #endif
  1295. }
  1296. #else
  1297. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1298. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1299. const int step = tid * K_QUANTS_PER_ITERATION;
  1300. uint16_t aux16[2];
  1301. const uint8_t * s = (const uint8_t *)aux16;
  1302. float tmp = 0;
  1303. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1304. const uint8_t * q = x[i].qs + step;
  1305. const float * y = yy + i*QK_K + step;
  1306. const uint16_t * a = (const uint16_t *)x[i].scales;
  1307. aux16[0] = a[0] & 0x0f0f;
  1308. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1309. const float d = (float)x[i].dm[0];
  1310. const float m = (float)x[i].dm[1];
  1311. float sum = 0.f;
  1312. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1313. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1314. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1315. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1316. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1317. }
  1318. tmp += sum;
  1319. }
  1320. #endif
  1321. // sum up partial sums and write back result
  1322. #pragma unroll
  1323. for (int mask = 16; mask > 0; mask >>= 1) {
  1324. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1325. }
  1326. if (tid == 0) {
  1327. dst[row] = tmp;
  1328. }
  1329. }
  1330. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1331. const int row = blockIdx.x;
  1332. const int num_blocks_per_row = ncols / QK_K;
  1333. const int ib0 = row*num_blocks_per_row;
  1334. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1335. float tmp = 0; // partial sum for thread in warp
  1336. #if QK_K == 256
  1337. const uint16_t kmask1 = 0x3f3f;
  1338. const uint16_t kmask2 = 0x0f0f;
  1339. const uint16_t kmask3 = 0xc0c0;
  1340. const int tid = threadIdx.x/2; // 0...15
  1341. const int ix = threadIdx.x%2;
  1342. const int il = tid/4; // 0...3
  1343. const int ir = tid - 4*il;// 0...3
  1344. const int n = 2;
  1345. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1346. const int in = il%2;
  1347. const int l0 = n*(2*ir + in);
  1348. const int q_offset = 32*im + l0;
  1349. const int y_offset = 64*im + l0;
  1350. const uint8_t hm1 = 1 << (2*im);
  1351. const uint8_t hm2 = hm1 << 4;
  1352. uint16_t aux[4];
  1353. const uint8_t * sc = (const uint8_t *)aux;
  1354. uint16_t q16[8];
  1355. const uint8_t * q4 = (const uint8_t *)q16;
  1356. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1357. const uint8_t * ql1 = x[i].qs + q_offset;
  1358. const uint8_t * qh = x[i].qh + l0;
  1359. const float * y1 = yy + i*QK_K + y_offset;
  1360. const float * y2 = y1 + 128;
  1361. const float dall = __low2half(x[i].dm);
  1362. const float dmin = __high2half(x[i].dm);
  1363. const uint16_t * a = (const uint16_t *)x[i].scales;
  1364. aux[0] = a[im+0] & kmask1;
  1365. aux[1] = a[im+2] & kmask1;
  1366. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1367. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1368. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1369. float smin = 0;
  1370. const uint16_t * q1 = (const uint16_t *)ql1;
  1371. const uint16_t * q2 = q1 + 32;
  1372. q16[0] = q1[0] & 0x0f0f;
  1373. q16[1] = q1[8] & 0x0f0f;
  1374. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1375. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1376. q16[4] = q2[0] & 0x0f0f;
  1377. q16[5] = q2[8] & 0x0f0f;
  1378. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1379. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1380. for (int l = 0; l < n; ++l) {
  1381. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1382. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1383. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1384. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1385. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1386. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1387. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1388. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1389. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1390. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1391. }
  1392. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1393. }
  1394. #else
  1395. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1396. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1397. const int step = tid * K_QUANTS_PER_ITERATION;
  1398. const int im = step/8;
  1399. const int in = step%8;
  1400. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1401. const uint8_t * q = x[i].qs + step;
  1402. const int8_t * s = x[i].scales;
  1403. const float * y = yy + i*QK_K + step;
  1404. const float d = x[i].d;
  1405. float sum = 0.f;
  1406. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1407. const uint8_t h = x[i].qh[in+j] >> im;
  1408. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1409. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1410. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1411. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1412. }
  1413. tmp += sum;
  1414. }
  1415. #endif
  1416. // sum up partial sums and write back result
  1417. #pragma unroll
  1418. for (int mask = 16; mask > 0; mask >>= 1) {
  1419. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1420. }
  1421. if (threadIdx.x == 0) {
  1422. dst[row] = tmp;
  1423. }
  1424. }
  1425. 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) {
  1426. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1427. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1428. if (row > nrows) return;
  1429. const int num_blocks_per_row = ncols / QK_K;
  1430. const int ib0 = row*num_blocks_per_row;
  1431. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1432. #if QK_K == 256
  1433. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1434. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1435. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1436. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1437. const int in = tid - step*im; // 0...15 or 0...7
  1438. #if K_QUANTS_PER_ITERATION == 1
  1439. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1440. const int is = 0;
  1441. #else
  1442. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1443. const int is = in / 4;
  1444. #endif
  1445. const int ql_offset = 64*im + l0;
  1446. const int qh_offset = 32*im + l0;
  1447. const int s_offset = 8*im + is;
  1448. const int y_offset = 128*im + l0;
  1449. float tmp = 0; // partial sum for thread in warp
  1450. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1451. const float * y = yy + i * QK_K + y_offset;
  1452. const uint8_t * ql = x[i].ql + ql_offset;
  1453. const uint8_t * qh = x[i].qh + qh_offset;
  1454. const int8_t * s = x[i].scales + s_offset;
  1455. const float d = x[i].d;
  1456. #if K_QUANTS_PER_ITERATION == 1
  1457. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1458. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1459. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1460. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1461. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1462. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1463. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1464. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1465. tmp += sum;
  1466. #else
  1467. float sum = 0;
  1468. for (int l = 0; l < 4; ++l) {
  1469. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1470. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1471. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1472. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1473. }
  1474. tmp += sum;
  1475. #endif
  1476. }
  1477. #else
  1478. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1479. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1480. const int step = tid * K_QUANTS_PER_ITERATION;
  1481. float tmp = 0; // partial sum for thread in warp
  1482. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1483. const float * y = yy + i * QK_K + step;
  1484. const uint8_t * ql = x[i].ql + step;
  1485. const uint8_t * qh = x[i].qh + step;
  1486. const int8_t * s = x[i].scales;
  1487. const float d = x[i+0].d;
  1488. float sum = 0;
  1489. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1490. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1491. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1492. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1493. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1494. }
  1495. tmp += sum;
  1496. }
  1497. #endif
  1498. // sum up partial sums and write back result
  1499. #pragma unroll
  1500. for (int mask = 16; mask > 0; mask >>= 1) {
  1501. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1502. }
  1503. if (tid == 0) {
  1504. dst[row] = tmp;
  1505. }
  1506. }
  1507. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1508. const half * x = (const half *) vx;
  1509. // automatic half -> float type cast if dfloat == float
  1510. v.x = x[ib + iqs + 0];
  1511. v.y = x[ib + iqs + 1];
  1512. }
  1513. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1514. const float * x = (const float *) vx;
  1515. // automatic half -> float type cast if dfloat == float
  1516. v.x = x[ib + iqs + 0];
  1517. v.y = x[ib + iqs + 1];
  1518. }
  1519. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1520. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1521. if (ix >= kx_padded) {
  1522. return;
  1523. }
  1524. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1525. const int i_padded = iy*kx_padded + ix;
  1526. block_q8_1 * y = (block_q8_1 *) vy;
  1527. const int ib = i_padded / QK8_1; // block index
  1528. const int iqs = i_padded % QK8_1; // quant index
  1529. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1530. float amax = fabsf(xi);
  1531. float sum = xi;
  1532. #pragma unroll
  1533. for (int mask = 16; mask > 0; mask >>= 1) {
  1534. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1535. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1536. }
  1537. const float d = amax / 127;
  1538. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1539. y[ib].qs[iqs] = q;
  1540. if (iqs > 0) {
  1541. return;
  1542. }
  1543. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1544. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1545. }
  1546. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1547. static __global__ void k_get_rows(
  1548. const void * src0, const int32_t * src1, dst_t * dst,
  1549. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1550. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1551. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1552. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1553. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1554. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1555. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1556. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1557. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1558. if (i00 >= ne00) {
  1559. return;
  1560. }
  1561. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1562. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1563. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1564. const int ib = i00/qk; // block index
  1565. const int iqs = (i00%qk)/qr; // quant index
  1566. const int iybs = i00 - i00%qk; // dst block start index
  1567. const int y_offset = qr == 1 ? 1 : qk/2;
  1568. // dequantize
  1569. dfloat2 v;
  1570. dequantize_kernel(src0_row, ib, iqs, v);
  1571. dst_row[iybs + iqs + 0] = v.x;
  1572. dst_row[iybs + iqs + y_offset] = v.y;
  1573. }
  1574. template<typename src0_t, typename dst_t>
  1575. static __global__ void k_get_rows_float(
  1576. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1577. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1578. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1579. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1580. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1581. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1582. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1583. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1584. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1585. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1586. if (i00 >= ne00) {
  1587. return;
  1588. }
  1589. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1590. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1591. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1592. dst_row[i00] = src0_row[i00];
  1593. }
  1594. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1595. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1596. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1597. if (i >= k) {
  1598. return;
  1599. }
  1600. const int ib = i/qk; // block index
  1601. const int iqs = (i%qk)/qr; // quant index
  1602. const int iybs = i - i%qk; // y block start index
  1603. const int y_offset = qr == 1 ? 1 : qk/2;
  1604. // dequantize
  1605. dfloat2 v;
  1606. dequantize_kernel(vx, ib, iqs, v);
  1607. y[iybs + iqs + 0] = v.x;
  1608. y[iybs + iqs + y_offset] = v.y;
  1609. }
  1610. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1611. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1612. #define VDR_Q4_0_Q8_1_MMVQ 2
  1613. #define VDR_Q4_0_Q8_1_MMQ 4
  1614. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1615. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1616. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1617. int sumi = 0;
  1618. #pragma unroll
  1619. for (int i = 0; i < vdr; ++i) {
  1620. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1621. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1622. // SIMD dot product of quantized values
  1623. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1624. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1625. }
  1626. const float2 ds8f = __half22float2(ds8);
  1627. // second part effectively subtracts 8 from each quant value
  1628. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1629. #else
  1630. bad_arch();
  1631. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1632. }
  1633. #define VDR_Q4_1_Q8_1_MMVQ 2
  1634. #define VDR_Q4_1_Q8_1_MMQ 4
  1635. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1636. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1637. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1638. int sumi = 0;
  1639. #pragma unroll
  1640. for (int i = 0; i < vdr; ++i) {
  1641. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1642. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1643. // SIMD dot product of quantized values
  1644. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1645. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1646. }
  1647. #ifdef GGML_CUDA_F16
  1648. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1649. const float d4d8 = tmp.x;
  1650. const float m4s8 = tmp.y;
  1651. #else
  1652. const float2 dm4f = __half22float2(dm4);
  1653. const float2 ds8f = __half22float2(ds8);
  1654. const float d4d8 = dm4f.x * ds8f.x;
  1655. const float m4s8 = dm4f.y * ds8f.y;
  1656. #endif // GGML_CUDA_F16
  1657. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1658. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1659. #else
  1660. bad_arch();
  1661. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1662. }
  1663. #define VDR_Q5_0_Q8_1_MMVQ 2
  1664. #define VDR_Q5_0_Q8_1_MMQ 4
  1665. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1666. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1667. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1668. int sumi = 0;
  1669. #pragma unroll
  1670. for (int i = 0; i < vdr; ++i) {
  1671. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1672. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1673. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1674. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1675. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1676. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1677. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1678. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1679. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1680. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1681. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1682. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1683. }
  1684. const float2 ds8f = __half22float2(ds8);
  1685. // second part effectively subtracts 16 from each quant value
  1686. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1687. #else
  1688. bad_arch();
  1689. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1690. }
  1691. #define VDR_Q5_1_Q8_1_MMVQ 2
  1692. #define VDR_Q5_1_Q8_1_MMQ 4
  1693. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1694. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1695. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1696. int sumi = 0;
  1697. #pragma unroll
  1698. for (int i = 0; i < vdr; ++i) {
  1699. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1700. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1701. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1702. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1703. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1704. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1705. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1706. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1707. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1708. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1709. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1710. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1711. }
  1712. #ifdef GGML_CUDA_F16
  1713. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1714. const float d5d8 = tmp.x;
  1715. const float m5s8 = tmp.y;
  1716. #else
  1717. const float2 dm5f = __half22float2(dm5);
  1718. const float2 ds8f = __half22float2(ds8);
  1719. const float d5d8 = dm5f.x * ds8f.x;
  1720. const float m5s8 = dm5f.y * ds8f.y;
  1721. #endif // GGML_CUDA_F16
  1722. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1723. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1724. #else
  1725. bad_arch();
  1726. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1727. }
  1728. #define VDR_Q8_0_Q8_1_MMVQ 2
  1729. #define VDR_Q8_0_Q8_1_MMQ 8
  1730. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1731. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1732. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1733. int sumi = 0;
  1734. #pragma unroll
  1735. for (int i = 0; i < vdr; ++i) {
  1736. // SIMD dot product of quantized values
  1737. sumi = __dp4a(v[i], u[i], sumi);
  1738. }
  1739. return d8_0*d8_1 * sumi;
  1740. #else
  1741. bad_arch();
  1742. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1743. }
  1744. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1745. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1746. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1747. int sumi = 0;
  1748. #pragma unroll
  1749. for (int i = 0; i < vdr; ++i) {
  1750. // SIMD dot product of quantized values
  1751. sumi = __dp4a(v[i], u[i], sumi);
  1752. }
  1753. #ifdef GGML_CUDA_F16
  1754. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1755. const float d8d8 = tmp.x;
  1756. const float m8s8 = tmp.y;
  1757. #else
  1758. const float2 dm8f = __half22float2(dm8);
  1759. const float2 ds8f = __half22float2(ds8);
  1760. const float d8d8 = dm8f.x * ds8f.x;
  1761. const float m8s8 = dm8f.y * ds8f.y;
  1762. #endif // GGML_CUDA_F16
  1763. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1764. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1765. #else
  1766. bad_arch();
  1767. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1768. }
  1769. #define VDR_Q2_K_Q8_1_MMVQ 1
  1770. #define VDR_Q2_K_Q8_1_MMQ 2
  1771. // contiguous v/x values
  1772. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1773. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1774. const half2 & dm2, const float * __restrict__ d8) {
  1775. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1776. float sumf_d = 0.0f;
  1777. float sumf_m = 0.0f;
  1778. #pragma unroll
  1779. for (int i = 0; i < QR2_K; ++i) {
  1780. const int sc = scales[2*i];
  1781. const int vi = (v >> (2*i)) & 0x03030303;
  1782. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1783. // fill int with 4x m
  1784. int m = sc >> 4;
  1785. m |= m << 8;
  1786. m |= m << 16;
  1787. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1788. }
  1789. const float2 dm2f = __half22float2(dm2);
  1790. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1791. #else
  1792. bad_arch();
  1793. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1794. }
  1795. // contiguous u/y values
  1796. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1797. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1798. const half2 & dm2, const float & d8) {
  1799. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1800. int sumi_d = 0;
  1801. int sumi_m = 0;
  1802. #pragma unroll
  1803. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1804. int sumi_d_sc = 0;
  1805. const int sc = scales[i0 / (QI8_1/2)];
  1806. // fill int with 4x m
  1807. int m = sc >> 4;
  1808. m |= m << 8;
  1809. m |= m << 16;
  1810. #pragma unroll
  1811. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1812. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1813. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1814. }
  1815. sumi_d += sumi_d_sc * (sc & 0xF);
  1816. }
  1817. const float2 dm2f = __half22float2(dm2);
  1818. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1819. #else
  1820. bad_arch();
  1821. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1822. }
  1823. #define VDR_Q3_K_Q8_1_MMVQ 1
  1824. #define VDR_Q3_K_Q8_1_MMQ 2
  1825. // contiguous v/x values
  1826. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1827. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1828. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1829. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1830. float sumf = 0.0f;
  1831. #pragma unroll
  1832. for (int i = 0; i < QR3_K; ++i) {
  1833. const int isc = scale_offset + 2*i;
  1834. const int isc_low = isc % (QK_K/32);
  1835. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1836. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1837. const int isc_high = isc % (QK_K/64);
  1838. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1839. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1840. const int sc = (sc_low | sc_high) - 32;
  1841. const int vil = (vl >> (2*i)) & 0x03030303;
  1842. const int vih = ((vh >> i) << 2) & 0x04040404;
  1843. const int vi = __vsubss4(vil, vih);
  1844. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1845. }
  1846. return d3 * sumf;
  1847. #else
  1848. bad_arch();
  1849. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1850. }
  1851. // contiguous u/y values
  1852. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1853. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1854. const float & d3, const float & d8) {
  1855. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1856. int sumi = 0;
  1857. #pragma unroll
  1858. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1859. int sumi_sc = 0;
  1860. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1861. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1862. }
  1863. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1864. }
  1865. return d3*d8 * sumi;
  1866. #else
  1867. bad_arch();
  1868. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1869. }
  1870. #define VDR_Q4_K_Q8_1_MMVQ 2
  1871. #define VDR_Q4_K_Q8_1_MMQ 8
  1872. // contiguous v/x values
  1873. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1874. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1875. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1876. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1877. float sumf_d = 0.0f;
  1878. float sumf_m = 0.0f;
  1879. #pragma unroll
  1880. for (int i = 0; i < QR4_K; ++i) {
  1881. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1882. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1883. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1884. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1885. sumf_d += d8[i] * (dot1 * sc[i]);
  1886. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1887. }
  1888. const float2 dm4f = __half22float2(dm4);
  1889. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1890. #else
  1891. bad_arch();
  1892. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1893. }
  1894. // contiguous u/y values
  1895. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1896. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1897. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1898. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1899. float sumf_d = 0.0f;
  1900. float sumf_m = 0.0f;
  1901. #pragma unroll
  1902. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1903. int sumi_d = 0;
  1904. #pragma unroll
  1905. for (int j = 0; j < QI8_1; ++j) {
  1906. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1907. }
  1908. const float2 ds8f = __half22float2(ds8[i]);
  1909. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1910. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1911. }
  1912. const float2 dm4f = __half22float2(dm4);
  1913. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1914. #else
  1915. bad_arch();
  1916. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1917. }
  1918. #define VDR_Q5_K_Q8_1_MMVQ 2
  1919. #define VDR_Q5_K_Q8_1_MMQ 8
  1920. // contiguous v/x values
  1921. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1922. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1923. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1924. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1925. float sumf_d = 0.0f;
  1926. float sumf_m = 0.0f;
  1927. #pragma unroll
  1928. for (int i = 0; i < QR5_K; ++i) {
  1929. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1930. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1931. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1932. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1933. const int v0i = vl0i | vh0i;
  1934. const int v1i = vl1i | vh1i;
  1935. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1936. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1937. sumf_d += d8[i] * (dot1 * sc[i]);
  1938. sumf_m += d8[i] * (dot2 * m[i]);
  1939. }
  1940. const float2 dm5f = __half22float2(dm5);
  1941. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1942. #else
  1943. bad_arch();
  1944. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1945. }
  1946. // contiguous u/y values
  1947. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1948. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1949. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1950. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1951. float sumf_d = 0.0f;
  1952. float sumf_m = 0.0f;
  1953. #pragma unroll
  1954. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1955. int sumi_d = 0;
  1956. #pragma unroll
  1957. for (int j = 0; j < QI8_1; ++j) {
  1958. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1959. }
  1960. const float2 ds8f = __half22float2(ds8[i]);
  1961. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1962. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1963. }
  1964. const float2 dm4f = __half22float2(dm4);
  1965. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1966. #else
  1967. bad_arch();
  1968. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1969. }
  1970. #define VDR_Q6_K_Q8_1_MMVQ 1
  1971. #define VDR_Q6_K_Q8_1_MMQ 8
  1972. // contiguous v/x values
  1973. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1974. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1975. const float & d, const float * __restrict__ d8) {
  1976. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1977. float sumf = 0.0f;
  1978. #pragma unroll
  1979. for (int i = 0; i < QR6_K; ++i) {
  1980. const int sc = scales[4*i];
  1981. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1982. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1983. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1984. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1985. }
  1986. return d*sumf;
  1987. #else
  1988. bad_arch();
  1989. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1990. }
  1991. // contiguous u/y values
  1992. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  1993. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  1994. const float & d6, const float * __restrict__ d8) {
  1995. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1996. float sumf_d = 0.0f;
  1997. #pragma unroll
  1998. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  1999. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2000. #pragma unroll
  2001. for (int i = i0; i < i0 + 2; ++i) {
  2002. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2003. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2004. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2005. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2006. }
  2007. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2008. }
  2009. return d6 * sumf_d;
  2010. #else
  2011. bad_arch();
  2012. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2013. }
  2014. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2015. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2016. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2017. int v[VDR_Q4_0_Q8_1_MMVQ];
  2018. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2019. #pragma unroll
  2020. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2021. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2022. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2023. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2024. }
  2025. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2026. }
  2027. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2028. (void)x_qh; (void)x_sc;
  2029. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2030. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2031. *x_ql = tile_x_qs;
  2032. *x_dm = (half2 *) tile_x_d;
  2033. }
  2034. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2035. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2036. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2037. (void)x_qh; (void)x_sc;
  2038. GGML_CUDA_ASSUME(i_offset >= 0);
  2039. GGML_CUDA_ASSUME(i_offset < nwarps);
  2040. GGML_CUDA_ASSUME(k >= 0);
  2041. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2042. const int kbx = k / QI4_0;
  2043. const int kqsx = k % QI4_0;
  2044. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2045. float * x_dmf = (float *) x_dm;
  2046. #pragma unroll
  2047. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2048. int i = i0 + i_offset;
  2049. if (need_check) {
  2050. i = min(i, i_max);
  2051. }
  2052. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2053. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2054. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2055. }
  2056. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2057. const int kbxd = k % blocks_per_tile_x_row;
  2058. #pragma unroll
  2059. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2060. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2061. if (need_check) {
  2062. i = min(i, i_max);
  2063. }
  2064. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2065. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2066. }
  2067. }
  2068. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2069. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2070. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2071. (void)x_qh; (void)x_sc;
  2072. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2073. const float * x_dmf = (const float *) x_dm;
  2074. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2075. #pragma unroll
  2076. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2077. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2078. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2079. }
  2080. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2081. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2082. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2083. }
  2084. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2085. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2086. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2087. int v[VDR_Q4_1_Q8_1_MMVQ];
  2088. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2089. #pragma unroll
  2090. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2091. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2092. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2093. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2094. }
  2095. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2096. }
  2097. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2098. (void)x_qh; (void)x_sc;
  2099. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2100. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2101. *x_ql = tile_x_qs;
  2102. *x_dm = tile_x_dm;
  2103. }
  2104. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2105. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2106. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2107. (void)x_qh; (void)x_sc;
  2108. GGML_CUDA_ASSUME(i_offset >= 0);
  2109. GGML_CUDA_ASSUME(i_offset < nwarps);
  2110. GGML_CUDA_ASSUME(k >= 0);
  2111. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2112. const int kbx = k / QI4_1;
  2113. const int kqsx = k % QI4_1;
  2114. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2115. #pragma unroll
  2116. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2117. int i = i0 + i_offset;
  2118. if (need_check) {
  2119. i = min(i, i_max);
  2120. }
  2121. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2122. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2123. }
  2124. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2125. const int kbxd = k % blocks_per_tile_x_row;
  2126. #pragma unroll
  2127. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2128. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2129. if (need_check) {
  2130. i = min(i, i_max);
  2131. }
  2132. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2133. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2134. }
  2135. }
  2136. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2137. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2138. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2139. (void)x_qh; (void)x_sc;
  2140. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2141. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2142. #pragma unroll
  2143. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2144. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2145. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2146. }
  2147. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2148. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2149. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2150. }
  2151. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2152. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2153. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2154. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2155. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2156. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2157. #pragma unroll
  2158. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2159. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2160. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2161. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2162. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2163. }
  2164. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2165. }
  2166. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2167. (void)x_qh; (void)x_sc;
  2168. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2169. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2170. *x_ql = tile_x_ql;
  2171. *x_dm = (half2 *) tile_x_d;
  2172. }
  2173. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2174. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2175. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2176. (void)x_qh; (void)x_sc;
  2177. GGML_CUDA_ASSUME(i_offset >= 0);
  2178. GGML_CUDA_ASSUME(i_offset < nwarps);
  2179. GGML_CUDA_ASSUME(k >= 0);
  2180. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2181. const int kbx = k / QI5_0;
  2182. const int kqsx = k % QI5_0;
  2183. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2184. #pragma unroll
  2185. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2186. int i = i0 + i_offset;
  2187. if (need_check) {
  2188. i = min(i, i_max);
  2189. }
  2190. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2191. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2192. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2193. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2194. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2195. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2196. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2197. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2198. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2199. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2200. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2201. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2202. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2203. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2204. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2205. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2206. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2207. }
  2208. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2209. const int kbxd = k % blocks_per_tile_x_row;
  2210. float * x_dmf = (float *) x_dm;
  2211. #pragma unroll
  2212. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2213. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2214. if (need_check) {
  2215. i = min(i, i_max);
  2216. }
  2217. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2218. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2219. }
  2220. }
  2221. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2222. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2223. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2224. (void)x_qh; (void)x_sc;
  2225. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2226. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2227. const float * x_dmf = (const float *) x_dm;
  2228. const float * y_df = (const float *) y_ds;
  2229. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2230. #pragma unroll
  2231. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2232. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2233. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2234. }
  2235. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2236. (&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)]);
  2237. }
  2238. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2239. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2240. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2241. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2242. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2243. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2244. #pragma unroll
  2245. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2246. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2247. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2248. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2249. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2250. }
  2251. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2252. }
  2253. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2254. (void)x_qh; (void)x_sc;
  2255. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2256. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2257. *x_ql = tile_x_ql;
  2258. *x_dm = tile_x_dm;
  2259. }
  2260. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2261. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2262. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2263. (void)x_qh; (void)x_sc;
  2264. GGML_CUDA_ASSUME(i_offset >= 0);
  2265. GGML_CUDA_ASSUME(i_offset < nwarps);
  2266. GGML_CUDA_ASSUME(k >= 0);
  2267. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2268. const int kbx = k / QI5_1;
  2269. const int kqsx = k % QI5_1;
  2270. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2271. #pragma unroll
  2272. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2273. int i = i0 + i_offset;
  2274. if (need_check) {
  2275. i = min(i, i_max);
  2276. }
  2277. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2278. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2279. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2280. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2281. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2282. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2283. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2284. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2285. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2286. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2287. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2288. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2289. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2290. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2291. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2292. }
  2293. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2294. const int kbxd = k % blocks_per_tile_x_row;
  2295. #pragma unroll
  2296. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2297. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2298. if (need_check) {
  2299. i = min(i, i_max);
  2300. }
  2301. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2302. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2303. }
  2304. }
  2305. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2306. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2307. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2308. (void)x_qh; (void)x_sc;
  2309. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2310. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2311. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2312. #pragma unroll
  2313. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2314. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2315. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2316. }
  2317. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2318. (&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)]);
  2319. }
  2320. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2321. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2322. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2323. int v[VDR_Q8_0_Q8_1_MMVQ];
  2324. int u[VDR_Q8_0_Q8_1_MMVQ];
  2325. #pragma unroll
  2326. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2327. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2328. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2329. }
  2330. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2331. }
  2332. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2333. (void)x_qh; (void)x_sc;
  2334. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2335. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2336. *x_ql = tile_x_qs;
  2337. *x_dm = (half2 *) tile_x_d;
  2338. }
  2339. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2340. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2341. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2342. (void)x_qh; (void)x_sc;
  2343. GGML_CUDA_ASSUME(i_offset >= 0);
  2344. GGML_CUDA_ASSUME(i_offset < nwarps);
  2345. GGML_CUDA_ASSUME(k >= 0);
  2346. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2347. const int kbx = k / QI8_0;
  2348. const int kqsx = k % QI8_0;
  2349. float * x_dmf = (float *) x_dm;
  2350. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2351. #pragma unroll
  2352. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2353. int i = i0 + i_offset;
  2354. if (need_check) {
  2355. i = min(i, i_max);
  2356. }
  2357. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2358. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2359. }
  2360. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2361. const int kbxd = k % blocks_per_tile_x_row;
  2362. #pragma unroll
  2363. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2364. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2365. if (need_check) {
  2366. i = min(i, i_max);
  2367. }
  2368. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2369. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2370. }
  2371. }
  2372. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2373. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2374. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2375. (void)x_qh; (void)x_sc;
  2376. const float * x_dmf = (const float *) x_dm;
  2377. const float * y_df = (const float *) y_ds;
  2378. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2379. (&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],
  2380. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2381. }
  2382. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2383. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2384. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2385. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2386. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2387. const uint8_t * scales = bq2_K->scales + scale_offset;
  2388. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2389. int u[QR2_K];
  2390. float d8[QR2_K];
  2391. #pragma unroll
  2392. for (int i = 0; i < QR2_K; ++ i) {
  2393. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2394. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2395. }
  2396. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2397. }
  2398. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2399. (void)x_qh;
  2400. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2401. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2402. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2403. *x_ql = tile_x_ql;
  2404. *x_dm = tile_x_dm;
  2405. *x_sc = tile_x_sc;
  2406. }
  2407. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2408. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2409. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2410. (void)x_qh;
  2411. GGML_CUDA_ASSUME(i_offset >= 0);
  2412. GGML_CUDA_ASSUME(i_offset < nwarps);
  2413. GGML_CUDA_ASSUME(k >= 0);
  2414. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2415. const int kbx = k / QI2_K;
  2416. const int kqsx = k % QI2_K;
  2417. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2418. #pragma unroll
  2419. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2420. int i = i0 + i_offset;
  2421. if (need_check) {
  2422. i = min(i, i_max);
  2423. }
  2424. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2425. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2426. }
  2427. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2428. const int kbxd = k % blocks_per_tile_x_row;
  2429. #pragma unroll
  2430. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2431. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2432. if (need_check) {
  2433. i = min(i, i_max);
  2434. }
  2435. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2436. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2437. }
  2438. #pragma unroll
  2439. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2440. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2441. if (need_check) {
  2442. i = min(i, i_max);
  2443. }
  2444. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2445. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2446. }
  2447. }
  2448. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2449. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2450. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2451. (void)x_qh;
  2452. const int kbx = k / QI2_K;
  2453. const int ky = (k % QI2_K) * QR2_K;
  2454. const float * y_df = (const float *) y_ds;
  2455. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2456. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2457. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2458. #pragma unroll
  2459. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2460. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2461. }
  2462. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2463. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2464. 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]);
  2465. }
  2466. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2467. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2468. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2469. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2470. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2471. const float d = bq3_K->d;
  2472. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2473. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2474. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2475. int u[QR3_K];
  2476. float d8[QR3_K];
  2477. #pragma unroll
  2478. for (int i = 0; i < QR3_K; ++i) {
  2479. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2480. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2481. }
  2482. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2483. }
  2484. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2485. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2486. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2487. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2488. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2489. *x_ql = tile_x_ql;
  2490. *x_dm = tile_x_dm;
  2491. *x_qh = tile_x_qh;
  2492. *x_sc = tile_x_sc;
  2493. }
  2494. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2495. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2496. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2497. GGML_CUDA_ASSUME(i_offset >= 0);
  2498. GGML_CUDA_ASSUME(i_offset < nwarps);
  2499. GGML_CUDA_ASSUME(k >= 0);
  2500. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2501. const int kbx = k / QI3_K;
  2502. const int kqsx = k % QI3_K;
  2503. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2504. #pragma unroll
  2505. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2506. int i = i0 + i_offset;
  2507. if (need_check) {
  2508. i = min(i, i_max);
  2509. }
  2510. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2511. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2512. }
  2513. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2514. const int kbxd = k % blocks_per_tile_x_row;
  2515. float * x_dmf = (float *) x_dm;
  2516. #pragma unroll
  2517. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2518. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2519. if (need_check) {
  2520. i = min(i, i_max);
  2521. }
  2522. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2523. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2524. }
  2525. #pragma unroll
  2526. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2527. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2528. if (need_check) {
  2529. i = min(i, i_max);
  2530. }
  2531. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2532. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2533. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2534. }
  2535. #pragma unroll
  2536. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2537. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2538. if (need_check) {
  2539. i = min(i, i_max);
  2540. }
  2541. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2542. const int ksc = k % (QI3_K/4);
  2543. const int ksc_low = ksc % (QI3_K/8);
  2544. const int shift_low = 4 * (ksc / (QI3_K/8));
  2545. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2546. const int ksc_high = QI3_K/8;
  2547. const int shift_high = 2 * ksc;
  2548. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2549. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2550. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2551. }
  2552. }
  2553. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2554. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2555. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2556. const int kbx = k / QI3_K;
  2557. const int ky = (k % QI3_K) * QR3_K;
  2558. const float * x_dmf = (const float *) x_dm;
  2559. const float * y_df = (const float *) y_ds;
  2560. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2561. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2562. #pragma unroll
  2563. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2564. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2565. const int shift = 2 * ((ky % 32) / 8);
  2566. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2567. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2568. const int vlh = (vh << 2) & 0x04040404;
  2569. v[l] = __vsubss4(vll, vlh);
  2570. }
  2571. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2572. 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]);
  2573. }
  2574. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2575. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2576. #ifndef GGML_QKK_64
  2577. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2578. int v[2];
  2579. int u[2*QR4_K];
  2580. float d8[QR4_K];
  2581. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2582. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2583. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2584. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2585. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2586. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2587. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2588. v[0] = q4[0];
  2589. v[1] = q4[4];
  2590. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2591. uint16_t aux[2];
  2592. const int j = bq8_offset/2;
  2593. if (j < 2) {
  2594. aux[0] = scales[j+0] & 0x3f3f;
  2595. aux[1] = scales[j+2] & 0x3f3f;
  2596. } else {
  2597. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2598. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2599. }
  2600. const uint8_t * sc = (const uint8_t *)aux;
  2601. const uint8_t * m = sc + 2;
  2602. for (int i = 0; i < QR4_K; ++i) {
  2603. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2604. d8[i] = __low2half(bq8i->ds);
  2605. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2606. u[2*i+0] = q8[0];
  2607. u[2*i+1] = q8[4];
  2608. }
  2609. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2610. #else
  2611. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2612. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2613. float sumf_d = 0.0f;
  2614. float sumf_m = 0.0f;
  2615. uint16_t aux16[2];
  2616. const uint8_t * s = (const uint8_t *)aux16;
  2617. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2618. aux16[0] = a[0] & 0x0f0f;
  2619. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2620. const float dall = bq4_K->dm[0];
  2621. const float dmin = bq4_K->dm[1];
  2622. const float d8_1 = __low2float(bq8_1[0].ds);
  2623. const float d8_2 = __low2float(bq8_1[1].ds);
  2624. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2625. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2626. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2627. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2628. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2629. const int v1 = q4[0];
  2630. const int v2 = q4[4];
  2631. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2632. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2633. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2634. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2635. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2636. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2637. return dall * sumf_d - dmin * sumf_m;
  2638. #else
  2639. bad_arch();
  2640. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2641. #endif
  2642. }
  2643. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2644. (void)x_qh;
  2645. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2646. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2647. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2648. *x_ql = tile_x_ql;
  2649. *x_dm = tile_x_dm;
  2650. *x_sc = tile_x_sc;
  2651. }
  2652. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2653. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2654. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2655. (void)x_qh;
  2656. GGML_CUDA_ASSUME(i_offset >= 0);
  2657. GGML_CUDA_ASSUME(i_offset < nwarps);
  2658. GGML_CUDA_ASSUME(k >= 0);
  2659. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2660. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2661. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2662. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2663. #pragma unroll
  2664. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2665. int i = i0 + i_offset;
  2666. if (need_check) {
  2667. i = min(i, i_max);
  2668. }
  2669. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2670. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2671. }
  2672. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2673. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2674. #pragma unroll
  2675. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2676. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2677. if (need_check) {
  2678. i = min(i, i_max);
  2679. }
  2680. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2681. #if QK_K == 256
  2682. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2683. #else
  2684. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2685. #endif
  2686. }
  2687. #pragma unroll
  2688. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2689. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2690. if (need_check) {
  2691. i = min(i, i_max);
  2692. }
  2693. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2694. const int * scales = (const int *) bxi->scales;
  2695. const int ksc = k % (WARP_SIZE/8);
  2696. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2697. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2698. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2699. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2700. }
  2701. }
  2702. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2703. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2704. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2705. (void)x_qh;
  2706. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2707. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2708. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2709. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2710. }
  2711. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2712. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2713. #ifndef GGML_QKK_64
  2714. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2715. int vl[2];
  2716. int vh[2];
  2717. int u[2*QR5_K];
  2718. float d8[QR5_K];
  2719. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2720. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2721. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2722. vl[0] = ql[0];
  2723. vl[1] = ql[4];
  2724. vh[0] = qh[0] >> bq8_offset;
  2725. vh[1] = qh[4] >> bq8_offset;
  2726. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2727. uint16_t aux[2];
  2728. const int j = bq8_offset/2;
  2729. if (j < 2) {
  2730. aux[0] = scales[j+0] & 0x3f3f;
  2731. aux[1] = scales[j+2] & 0x3f3f;
  2732. } else {
  2733. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2734. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2735. }
  2736. const uint8_t * sc = (const uint8_t *)aux;
  2737. const uint8_t * m = sc + 2;
  2738. #pragma unroll
  2739. for (int i = 0; i < QR5_K; ++i) {
  2740. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2741. d8[i] = __low2float(bq8i->ds);
  2742. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2743. u[2*i+0] = q8[0];
  2744. u[2*i+1] = q8[4];
  2745. }
  2746. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2747. #else
  2748. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2749. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2750. const int8_t * s = bq5_K->scales;
  2751. const float d = bq5_K->d;
  2752. const float d8_1 = __low2half(bq8_1[0].ds);
  2753. const float d8_2 = __low2half(bq8_1[1].ds);
  2754. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2755. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2756. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2757. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2758. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2759. const int vl1 = ql[0];
  2760. const int vl2 = ql[4];
  2761. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2762. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2763. const int in = step%8; // 0, 4, 0, 4
  2764. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2765. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2766. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2767. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2768. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2769. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2770. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2771. return d * sumf_d;
  2772. #else
  2773. bad_arch();
  2774. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2775. #endif
  2776. }
  2777. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2778. (void)x_qh;
  2779. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2780. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2781. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2782. *x_ql = tile_x_ql;
  2783. *x_dm = tile_x_dm;
  2784. *x_sc = tile_x_sc;
  2785. }
  2786. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2787. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2788. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2789. (void)x_qh;
  2790. GGML_CUDA_ASSUME(i_offset >= 0);
  2791. GGML_CUDA_ASSUME(i_offset < nwarps);
  2792. GGML_CUDA_ASSUME(k >= 0);
  2793. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2794. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2795. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2796. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2797. #pragma unroll
  2798. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2799. int i = i0 + i_offset;
  2800. if (need_check) {
  2801. i = min(i, i_max);
  2802. }
  2803. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2804. const int ky = QR5_K*kqsx;
  2805. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2806. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2807. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2808. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2809. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2810. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2811. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2812. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2813. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2814. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2815. }
  2816. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2817. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2818. #pragma unroll
  2819. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2820. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2821. if (need_check) {
  2822. i = min(i, i_max);
  2823. }
  2824. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2825. #if QK_K == 256
  2826. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2827. #endif
  2828. }
  2829. #pragma unroll
  2830. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2831. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2832. if (need_check) {
  2833. i = min(i, i_max);
  2834. }
  2835. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2836. const int * scales = (const int *) bxi->scales;
  2837. const int ksc = k % (WARP_SIZE/8);
  2838. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2839. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2840. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2841. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2842. }
  2843. }
  2844. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2845. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2846. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2847. (void)x_qh;
  2848. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2849. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2850. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2851. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2852. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2853. }
  2854. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2855. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2856. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2857. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2858. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2859. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2860. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2861. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2862. const int8_t * scales = bq6_K->scales + scale_offset;
  2863. int u[QR6_K];
  2864. float d8[QR6_K];
  2865. #pragma unroll
  2866. for (int i = 0; i < QR6_K; ++i) {
  2867. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2868. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2869. }
  2870. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2871. }
  2872. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2873. (void)x_qh;
  2874. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2875. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2876. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2877. *x_ql = tile_x_ql;
  2878. *x_dm = tile_x_dm;
  2879. *x_sc = tile_x_sc;
  2880. }
  2881. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2882. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2883. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2884. (void)x_qh;
  2885. GGML_CUDA_ASSUME(i_offset >= 0);
  2886. GGML_CUDA_ASSUME(i_offset < nwarps);
  2887. GGML_CUDA_ASSUME(k >= 0);
  2888. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2889. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2890. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2891. const block_q6_K * bx0 = (const block_q6_K *) vx;
  2892. #pragma unroll
  2893. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2894. int i = i0 + i_offset;
  2895. if (need_check) {
  2896. i = min(i, i_max);
  2897. }
  2898. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2899. const int ky = QR6_K*kqsx;
  2900. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2901. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2902. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2903. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2904. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2905. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2906. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2907. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2908. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2909. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2910. }
  2911. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2912. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2913. float * x_dmf = (float *) x_dm;
  2914. #pragma unroll
  2915. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2916. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2917. if (need_check) {
  2918. i = min(i, i_max);
  2919. }
  2920. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2921. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2922. }
  2923. #pragma unroll
  2924. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2925. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2926. if (need_check) {
  2927. i = min(i, i_max);
  2928. }
  2929. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2930. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2931. }
  2932. }
  2933. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2934. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2935. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2936. (void)x_qh;
  2937. const float * x_dmf = (const float *) x_dm;
  2938. const float * y_df = (const float *) y_ds;
  2939. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2940. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2941. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2942. 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]);
  2943. }
  2944. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2945. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2946. static __device__ __forceinline__ void mul_mat_q(
  2947. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2948. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2949. const block_q_t * x = (const block_q_t *) vx;
  2950. const block_q8_1 * y = (const block_q8_1 *) vy;
  2951. const int blocks_per_row_x = ncols_x / qk;
  2952. const int blocks_per_col_y = nrows_y / QK8_1;
  2953. const int blocks_per_warp = WARP_SIZE / qi;
  2954. const int & ncols_dst = ncols_y;
  2955. const int row_dst_0 = blockIdx.x*mmq_y;
  2956. const int & row_x_0 = row_dst_0;
  2957. const int col_dst_0 = blockIdx.y*mmq_x;
  2958. const int & col_y_0 = col_dst_0;
  2959. int * tile_x_ql = nullptr;
  2960. half2 * tile_x_dm = nullptr;
  2961. int * tile_x_qh = nullptr;
  2962. int * tile_x_sc = nullptr;
  2963. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2964. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2965. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2966. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  2967. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2968. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2969. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2970. #pragma unroll
  2971. for (int ir = 0; ir < qr; ++ir) {
  2972. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2973. const int kbxd = kqs / QI8_1;
  2974. #pragma unroll
  2975. for (int i = 0; i < mmq_x; i += nwarps) {
  2976. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2977. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2978. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2979. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2980. }
  2981. #pragma unroll
  2982. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2983. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2984. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2985. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2986. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2987. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  2988. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  2989. if (need_sum) {
  2990. *dsi_dst = *dsi_src;
  2991. } else {
  2992. float * dfi_dst = (float *) dsi_dst;
  2993. *dfi_dst = __low2half(*dsi_src);
  2994. }
  2995. }
  2996. __syncthreads();
  2997. // #pragma unroll // unrolling this loop causes too much register pressure
  2998. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  2999. #pragma unroll
  3000. for (int j = 0; j < mmq_x; j += nwarps) {
  3001. #pragma unroll
  3002. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3003. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3004. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3005. threadIdx.x + i, threadIdx.y + j, k);
  3006. }
  3007. }
  3008. }
  3009. __syncthreads();
  3010. }
  3011. }
  3012. #pragma unroll
  3013. for (int j = 0; j < mmq_x; j += nwarps) {
  3014. const int col_dst = col_dst_0 + j + threadIdx.y;
  3015. if (col_dst >= ncols_dst) {
  3016. return;
  3017. }
  3018. #pragma unroll
  3019. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3020. const int row_dst = row_dst_0 + threadIdx.x + i;
  3021. if (row_dst >= nrows_dst) {
  3022. continue;
  3023. }
  3024. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3025. }
  3026. }
  3027. }
  3028. #define MMQ_X_Q4_0_RDNA2 64
  3029. #define MMQ_Y_Q4_0_RDNA2 128
  3030. #define NWARPS_Q4_0_RDNA2 8
  3031. #define MMQ_X_Q4_0_RDNA1 64
  3032. #define MMQ_Y_Q4_0_RDNA1 64
  3033. #define NWARPS_Q4_0_RDNA1 8
  3034. #if defined(CUDA_USE_TENSOR_CORES)
  3035. #define MMQ_X_Q4_0_AMPERE 4
  3036. #define MMQ_Y_Q4_0_AMPERE 32
  3037. #define NWARPS_Q4_0_AMPERE 4
  3038. #else
  3039. #define MMQ_X_Q4_0_AMPERE 64
  3040. #define MMQ_Y_Q4_0_AMPERE 128
  3041. #define NWARPS_Q4_0_AMPERE 4
  3042. #endif
  3043. #define MMQ_X_Q4_0_PASCAL 64
  3044. #define MMQ_Y_Q4_0_PASCAL 64
  3045. #define NWARPS_Q4_0_PASCAL 8
  3046. template <bool need_check> static __global__ void
  3047. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3048. #if defined(RDNA3) || defined(RDNA2)
  3049. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3050. #endif // defined(RDNA3) || defined(RDNA2)
  3051. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3052. mul_mat_q4_0(
  3053. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3054. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3055. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3056. #if defined(RDNA3) || defined(RDNA2)
  3057. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3058. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3059. const int nwarps = NWARPS_Q4_0_RDNA2;
  3060. #else
  3061. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3062. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3063. const int nwarps = NWARPS_Q4_0_RDNA1;
  3064. #endif // defined(RDNA3) || defined(RDNA2)
  3065. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3066. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3067. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3068. #elif __CUDA_ARCH__ >= CC_VOLTA
  3069. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3070. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3071. const int nwarps = NWARPS_Q4_0_AMPERE;
  3072. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3073. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3074. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3075. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3076. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3077. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3078. const int nwarps = NWARPS_Q4_0_PASCAL;
  3079. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3080. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3081. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3082. #else
  3083. (void) vec_dot_q4_0_q8_1_mul_mat;
  3084. bad_arch();
  3085. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3086. }
  3087. #define MMQ_X_Q4_1_RDNA2 64
  3088. #define MMQ_Y_Q4_1_RDNA2 128
  3089. #define NWARPS_Q4_1_RDNA2 8
  3090. #define MMQ_X_Q4_1_RDNA1 64
  3091. #define MMQ_Y_Q4_1_RDNA1 64
  3092. #define NWARPS_Q4_1_RDNA1 8
  3093. #if defined(CUDA_USE_TENSOR_CORES)
  3094. #define MMQ_X_Q4_1_AMPERE 4
  3095. #define MMQ_Y_Q4_1_AMPERE 32
  3096. #define NWARPS_Q4_1_AMPERE 4
  3097. #else
  3098. #define MMQ_X_Q4_1_AMPERE 64
  3099. #define MMQ_Y_Q4_1_AMPERE 128
  3100. #define NWARPS_Q4_1_AMPERE 4
  3101. #endif
  3102. #define MMQ_X_Q4_1_PASCAL 64
  3103. #define MMQ_Y_Q4_1_PASCAL 64
  3104. #define NWARPS_Q4_1_PASCAL 8
  3105. template <bool need_check> static __global__ void
  3106. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3107. #if defined(RDNA3) || defined(RDNA2)
  3108. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3109. #endif // defined(RDNA3) || defined(RDNA2)
  3110. #elif __CUDA_ARCH__ < CC_VOLTA
  3111. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3112. #endif // __CUDA_ARCH__ < CC_VOLTA
  3113. mul_mat_q4_1(
  3114. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3115. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3116. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3117. #if defined(RDNA3) || defined(RDNA2)
  3118. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3119. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3120. const int nwarps = NWARPS_Q4_1_RDNA2;
  3121. #else
  3122. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3123. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3124. const int nwarps = NWARPS_Q4_1_RDNA1;
  3125. #endif // defined(RDNA3) || defined(RDNA2)
  3126. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3127. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3128. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3129. #elif __CUDA_ARCH__ >= CC_VOLTA
  3130. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3131. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3132. const int nwarps = NWARPS_Q4_1_AMPERE;
  3133. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3134. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3135. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3136. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3137. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3138. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3139. const int nwarps = NWARPS_Q4_1_PASCAL;
  3140. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3141. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3142. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3143. #else
  3144. (void) vec_dot_q4_1_q8_1_mul_mat;
  3145. bad_arch();
  3146. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3147. }
  3148. #define MMQ_X_Q5_0_RDNA2 64
  3149. #define MMQ_Y_Q5_0_RDNA2 128
  3150. #define NWARPS_Q5_0_RDNA2 8
  3151. #define MMQ_X_Q5_0_RDNA1 64
  3152. #define MMQ_Y_Q5_0_RDNA1 64
  3153. #define NWARPS_Q5_0_RDNA1 8
  3154. #if defined(CUDA_USE_TENSOR_CORES)
  3155. #define MMQ_X_Q5_0_AMPERE 4
  3156. #define MMQ_Y_Q5_0_AMPERE 32
  3157. #define NWARPS_Q5_0_AMPERE 4
  3158. #else
  3159. #define MMQ_X_Q5_0_AMPERE 128
  3160. #define MMQ_Y_Q5_0_AMPERE 64
  3161. #define NWARPS_Q5_0_AMPERE 4
  3162. #endif
  3163. #define MMQ_X_Q5_0_PASCAL 64
  3164. #define MMQ_Y_Q5_0_PASCAL 64
  3165. #define NWARPS_Q5_0_PASCAL 8
  3166. template <bool need_check> static __global__ void
  3167. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3168. #if defined(RDNA3) || defined(RDNA2)
  3169. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3170. #endif // defined(RDNA3) || defined(RDNA2)
  3171. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3172. mul_mat_q5_0(
  3173. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3174. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3175. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3176. #if defined(RDNA3) || defined(RDNA2)
  3177. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3178. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3179. const int nwarps = NWARPS_Q5_0_RDNA2;
  3180. #else
  3181. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3182. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3183. const int nwarps = NWARPS_Q5_0_RDNA1;
  3184. #endif // defined(RDNA3) || defined(RDNA2)
  3185. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3186. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3187. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3188. #elif __CUDA_ARCH__ >= CC_VOLTA
  3189. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3190. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3191. const int nwarps = NWARPS_Q5_0_AMPERE;
  3192. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3193. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3194. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3195. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3196. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3197. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3198. const int nwarps = NWARPS_Q5_0_PASCAL;
  3199. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3200. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3201. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3202. #else
  3203. (void) vec_dot_q5_0_q8_1_mul_mat;
  3204. bad_arch();
  3205. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3206. }
  3207. #define MMQ_X_Q5_1_RDNA2 64
  3208. #define MMQ_Y_Q5_1_RDNA2 128
  3209. #define NWARPS_Q5_1_RDNA2 8
  3210. #define MMQ_X_Q5_1_RDNA1 64
  3211. #define MMQ_Y_Q5_1_RDNA1 64
  3212. #define NWARPS_Q5_1_RDNA1 8
  3213. #if defined(CUDA_USE_TENSOR_CORES)
  3214. #define MMQ_X_Q5_1_AMPERE 4
  3215. #define MMQ_Y_Q5_1_AMPERE 32
  3216. #define NWARPS_Q5_1_AMPERE 4
  3217. #else
  3218. #define MMQ_X_Q5_1_AMPERE 128
  3219. #define MMQ_Y_Q5_1_AMPERE 64
  3220. #define NWARPS_Q5_1_AMPERE 4
  3221. #endif
  3222. #define MMQ_X_Q5_1_PASCAL 64
  3223. #define MMQ_Y_Q5_1_PASCAL 64
  3224. #define NWARPS_Q5_1_PASCAL 8
  3225. template <bool need_check> static __global__ void
  3226. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3227. #if defined(RDNA3) || defined(RDNA2)
  3228. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3229. #endif // defined(RDNA3) || defined(RDNA2)
  3230. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3231. mul_mat_q5_1(
  3232. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3233. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3234. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3235. #if defined(RDNA3) || defined(RDNA2)
  3236. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3237. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3238. const int nwarps = NWARPS_Q5_1_RDNA2;
  3239. #else
  3240. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3241. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3242. const int nwarps = NWARPS_Q5_1_RDNA1;
  3243. #endif // defined(RDNA3) || defined(RDNA2)
  3244. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3245. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3246. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3247. #elif __CUDA_ARCH__ >= CC_VOLTA
  3248. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3249. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3250. const int nwarps = NWARPS_Q5_1_AMPERE;
  3251. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3252. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3253. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3254. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3255. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3256. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3257. const int nwarps = NWARPS_Q5_1_PASCAL;
  3258. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3259. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3260. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3261. #else
  3262. (void) vec_dot_q5_1_q8_1_mul_mat;
  3263. bad_arch();
  3264. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3265. }
  3266. #define MMQ_X_Q8_0_RDNA2 64
  3267. #define MMQ_Y_Q8_0_RDNA2 128
  3268. #define NWARPS_Q8_0_RDNA2 8
  3269. #define MMQ_X_Q8_0_RDNA1 64
  3270. #define MMQ_Y_Q8_0_RDNA1 64
  3271. #define NWARPS_Q8_0_RDNA1 8
  3272. #if defined(CUDA_USE_TENSOR_CORES)
  3273. #define MMQ_X_Q8_0_AMPERE 4
  3274. #define MMQ_Y_Q8_0_AMPERE 32
  3275. #define NWARPS_Q8_0_AMPERE 4
  3276. #else
  3277. #define MMQ_X_Q8_0_AMPERE 128
  3278. #define MMQ_Y_Q8_0_AMPERE 64
  3279. #define NWARPS_Q8_0_AMPERE 4
  3280. #endif
  3281. #define MMQ_X_Q8_0_PASCAL 64
  3282. #define MMQ_Y_Q8_0_PASCAL 64
  3283. #define NWARPS_Q8_0_PASCAL 8
  3284. template <bool need_check> static __global__ void
  3285. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3286. #if defined(RDNA3) || defined(RDNA2)
  3287. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3288. #endif // defined(RDNA3) || defined(RDNA2)
  3289. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3290. mul_mat_q8_0(
  3291. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3292. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3293. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3294. #if defined(RDNA3) || defined(RDNA2)
  3295. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3296. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3297. const int nwarps = NWARPS_Q8_0_RDNA2;
  3298. #else
  3299. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3300. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3301. const int nwarps = NWARPS_Q8_0_RDNA1;
  3302. #endif // defined(RDNA3) || defined(RDNA2)
  3303. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3304. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3305. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3306. #elif __CUDA_ARCH__ >= CC_VOLTA
  3307. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3308. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3309. const int nwarps = NWARPS_Q8_0_AMPERE;
  3310. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3311. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3312. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3313. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3314. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3315. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3316. const int nwarps = NWARPS_Q8_0_PASCAL;
  3317. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3318. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3319. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3320. #else
  3321. (void) vec_dot_q8_0_q8_1_mul_mat;
  3322. bad_arch();
  3323. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3324. }
  3325. #define MMQ_X_Q2_K_RDNA2 64
  3326. #define MMQ_Y_Q2_K_RDNA2 128
  3327. #define NWARPS_Q2_K_RDNA2 8
  3328. #define MMQ_X_Q2_K_RDNA1 128
  3329. #define MMQ_Y_Q2_K_RDNA1 32
  3330. #define NWARPS_Q2_K_RDNA1 8
  3331. #if defined(CUDA_USE_TENSOR_CORES)
  3332. #define MMQ_X_Q2_K_AMPERE 4
  3333. #define MMQ_Y_Q2_K_AMPERE 32
  3334. #define NWARPS_Q2_K_AMPERE 4
  3335. #else
  3336. #define MMQ_X_Q2_K_AMPERE 64
  3337. #define MMQ_Y_Q2_K_AMPERE 128
  3338. #define NWARPS_Q2_K_AMPERE 4
  3339. #endif
  3340. #define MMQ_X_Q2_K_PASCAL 64
  3341. #define MMQ_Y_Q2_K_PASCAL 64
  3342. #define NWARPS_Q2_K_PASCAL 8
  3343. template <bool need_check> static __global__ void
  3344. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3345. #if defined(RDNA3) || defined(RDNA2)
  3346. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3347. #endif // defined(RDNA3) || defined(RDNA2)
  3348. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3349. mul_mat_q2_K(
  3350. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3351. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3352. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3353. #if defined(RDNA3) || defined(RDNA2)
  3354. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3355. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3356. const int nwarps = NWARPS_Q2_K_RDNA2;
  3357. #else
  3358. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3359. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3360. const int nwarps = NWARPS_Q2_K_RDNA1;
  3361. #endif // defined(RDNA3) || defined(RDNA2)
  3362. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3363. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3364. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3365. #elif __CUDA_ARCH__ >= CC_VOLTA
  3366. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3367. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3368. const int nwarps = NWARPS_Q2_K_AMPERE;
  3369. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3370. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3371. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3372. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3373. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3374. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3375. const int nwarps = NWARPS_Q2_K_PASCAL;
  3376. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3377. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3378. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3379. #else
  3380. (void) vec_dot_q2_K_q8_1_mul_mat;
  3381. bad_arch();
  3382. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3383. }
  3384. #define MMQ_X_Q3_K_RDNA2 128
  3385. #define MMQ_Y_Q3_K_RDNA2 64
  3386. #define NWARPS_Q3_K_RDNA2 8
  3387. #define MMQ_X_Q3_K_RDNA1 32
  3388. #define MMQ_Y_Q3_K_RDNA1 128
  3389. #define NWARPS_Q3_K_RDNA1 8
  3390. #if defined(CUDA_USE_TENSOR_CORES)
  3391. #define MMQ_X_Q3_K_AMPERE 4
  3392. #define MMQ_Y_Q3_K_AMPERE 32
  3393. #define NWARPS_Q3_K_AMPERE 4
  3394. #else
  3395. #define MMQ_X_Q3_K_AMPERE 128
  3396. #define MMQ_Y_Q3_K_AMPERE 128
  3397. #define NWARPS_Q3_K_AMPERE 4
  3398. #endif
  3399. #define MMQ_X_Q3_K_PASCAL 64
  3400. #define MMQ_Y_Q3_K_PASCAL 64
  3401. #define NWARPS_Q3_K_PASCAL 8
  3402. template <bool need_check> static __global__ void
  3403. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3404. #if defined(RDNA3) || defined(RDNA2)
  3405. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3406. #endif // defined(RDNA3) || defined(RDNA2)
  3407. #elif __CUDA_ARCH__ < CC_VOLTA
  3408. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3409. #endif // __CUDA_ARCH__ < CC_VOLTA
  3410. mul_mat_q3_K(
  3411. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3412. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3413. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3414. #if defined(RDNA3) || defined(RDNA2)
  3415. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3416. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3417. const int nwarps = NWARPS_Q3_K_RDNA2;
  3418. #else
  3419. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3420. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3421. const int nwarps = NWARPS_Q3_K_RDNA1;
  3422. #endif // defined(RDNA3) || defined(RDNA2)
  3423. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3424. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3425. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3426. #elif __CUDA_ARCH__ >= CC_VOLTA
  3427. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3428. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3429. const int nwarps = NWARPS_Q3_K_AMPERE;
  3430. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3431. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3432. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3433. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3434. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3435. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3436. const int nwarps = NWARPS_Q3_K_PASCAL;
  3437. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3438. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3439. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3440. #else
  3441. (void) vec_dot_q3_K_q8_1_mul_mat;
  3442. bad_arch();
  3443. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3444. }
  3445. #define MMQ_X_Q4_K_RDNA2 64
  3446. #define MMQ_Y_Q4_K_RDNA2 128
  3447. #define NWARPS_Q4_K_RDNA2 8
  3448. #define MMQ_X_Q4_K_RDNA1 32
  3449. #define MMQ_Y_Q4_K_RDNA1 64
  3450. #define NWARPS_Q4_K_RDNA1 8
  3451. #if defined(CUDA_USE_TENSOR_CORES)
  3452. #define MMQ_X_Q4_K_AMPERE 4
  3453. #define MMQ_Y_Q4_K_AMPERE 32
  3454. #define NWARPS_Q4_K_AMPERE 4
  3455. #else
  3456. #define MMQ_X_Q4_K_AMPERE 64
  3457. #define MMQ_Y_Q4_K_AMPERE 128
  3458. #define NWARPS_Q4_K_AMPERE 4
  3459. #endif
  3460. #define MMQ_X_Q4_K_PASCAL 64
  3461. #define MMQ_Y_Q4_K_PASCAL 64
  3462. #define NWARPS_Q4_K_PASCAL 8
  3463. template <bool need_check> static __global__ void
  3464. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3465. #if defined(RDNA3) || defined(RDNA2)
  3466. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3467. #endif // defined(RDNA3) || defined(RDNA2)
  3468. #elif __CUDA_ARCH__ < CC_VOLTA
  3469. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3470. #endif // __CUDA_ARCH__ < CC_VOLTA
  3471. mul_mat_q4_K(
  3472. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3473. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3474. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3475. #if defined(RDNA3) || defined(RDNA2)
  3476. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3477. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3478. const int nwarps = NWARPS_Q4_K_RDNA2;
  3479. #else
  3480. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3481. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3482. const int nwarps = NWARPS_Q4_K_RDNA1;
  3483. #endif // defined(RDNA3) || defined(RDNA2)
  3484. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3485. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3486. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3487. #elif __CUDA_ARCH__ >= CC_VOLTA
  3488. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3489. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3490. const int nwarps = NWARPS_Q4_K_AMPERE;
  3491. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3492. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3493. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3494. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3495. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3496. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3497. const int nwarps = NWARPS_Q4_K_PASCAL;
  3498. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3499. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3500. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3501. #else
  3502. (void) vec_dot_q4_K_q8_1_mul_mat;
  3503. bad_arch();
  3504. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3505. }
  3506. #define MMQ_X_Q5_K_RDNA2 64
  3507. #define MMQ_Y_Q5_K_RDNA2 128
  3508. #define NWARPS_Q5_K_RDNA2 8
  3509. #define MMQ_X_Q5_K_RDNA1 32
  3510. #define MMQ_Y_Q5_K_RDNA1 64
  3511. #define NWARPS_Q5_K_RDNA1 8
  3512. #if defined(CUDA_USE_TENSOR_CORES)
  3513. #define MMQ_X_Q5_K_AMPERE 4
  3514. #define MMQ_Y_Q5_K_AMPERE 32
  3515. #define NWARPS_Q5_K_AMPERE 4
  3516. #else
  3517. #define MMQ_X_Q5_K_AMPERE 64
  3518. #define MMQ_Y_Q5_K_AMPERE 128
  3519. #define NWARPS_Q5_K_AMPERE 4
  3520. #endif
  3521. #define MMQ_X_Q5_K_PASCAL 64
  3522. #define MMQ_Y_Q5_K_PASCAL 64
  3523. #define NWARPS_Q5_K_PASCAL 8
  3524. template <bool need_check> static __global__ void
  3525. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3526. #if defined(RDNA3) || defined(RDNA2)
  3527. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3528. #endif // defined(RDNA3) || defined(RDNA2)
  3529. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3530. mul_mat_q5_K(
  3531. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3532. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3533. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3534. #if defined(RDNA3) || defined(RDNA2)
  3535. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3536. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3537. const int nwarps = NWARPS_Q5_K_RDNA2;
  3538. #else
  3539. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3540. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3541. const int nwarps = NWARPS_Q5_K_RDNA1;
  3542. #endif // defined(RDNA3) || defined(RDNA2)
  3543. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3544. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3545. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3546. #elif __CUDA_ARCH__ >= CC_VOLTA
  3547. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3548. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3549. const int nwarps = NWARPS_Q5_K_AMPERE;
  3550. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3551. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3552. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3553. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3554. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3555. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3556. const int nwarps = NWARPS_Q5_K_PASCAL;
  3557. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3558. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3559. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3560. #else
  3561. (void) vec_dot_q5_K_q8_1_mul_mat;
  3562. bad_arch();
  3563. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3564. }
  3565. #define MMQ_X_Q6_K_RDNA2 64
  3566. #define MMQ_Y_Q6_K_RDNA2 128
  3567. #define NWARPS_Q6_K_RDNA2 8
  3568. #define MMQ_X_Q6_K_RDNA1 32
  3569. #define MMQ_Y_Q6_K_RDNA1 64
  3570. #define NWARPS_Q6_K_RDNA1 8
  3571. #if defined(CUDA_USE_TENSOR_CORES)
  3572. #define MMQ_X_Q6_K_AMPERE 4
  3573. #define MMQ_Y_Q6_K_AMPERE 32
  3574. #define NWARPS_Q6_K_AMPERE 4
  3575. #else
  3576. #define MMQ_X_Q6_K_AMPERE 64
  3577. #define MMQ_Y_Q6_K_AMPERE 64
  3578. #define NWARPS_Q6_K_AMPERE 4
  3579. #endif
  3580. #define MMQ_X_Q6_K_PASCAL 64
  3581. #define MMQ_Y_Q6_K_PASCAL 64
  3582. #define NWARPS_Q6_K_PASCAL 8
  3583. template <bool need_check> static __global__ void
  3584. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3585. #if defined(RDNA3) || defined(RDNA2)
  3586. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3587. #endif // defined(RDNA3) || defined(RDNA2)
  3588. #elif __CUDA_ARCH__ < CC_VOLTA
  3589. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3590. #endif // __CUDA_ARCH__ < CC_VOLTA
  3591. mul_mat_q6_K(
  3592. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3593. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3594. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3595. #if defined(RDNA3) || defined(RDNA2)
  3596. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3597. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3598. const int nwarps = NWARPS_Q6_K_RDNA2;
  3599. #else
  3600. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3601. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3602. const int nwarps = NWARPS_Q6_K_RDNA1;
  3603. #endif // defined(RDNA3) || defined(RDNA2)
  3604. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3605. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3606. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3607. #elif __CUDA_ARCH__ >= CC_VOLTA
  3608. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3609. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3610. const int nwarps = NWARPS_Q6_K_AMPERE;
  3611. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3612. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3613. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3614. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3615. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3616. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3617. const int nwarps = NWARPS_Q6_K_PASCAL;
  3618. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3619. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3620. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3621. #else
  3622. (void) vec_dot_q6_K_q8_1_mul_mat;
  3623. bad_arch();
  3624. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3625. }
  3626. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3627. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3628. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3629. if (row >= nrows) {
  3630. return;
  3631. }
  3632. const int blocks_per_row = ncols / qk;
  3633. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3634. // partial sum for each thread
  3635. float tmp = 0.0f;
  3636. const block_q_t * x = (const block_q_t *) vx;
  3637. const block_q8_1 * y = (const block_q8_1 *) vy;
  3638. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3639. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3640. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3641. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3642. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3643. }
  3644. // sum up partial sums and write back result
  3645. #pragma unroll
  3646. for (int mask = 16; mask > 0; mask >>= 1) {
  3647. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3648. }
  3649. if (threadIdx.x == 0) {
  3650. dst[row] = tmp;
  3651. }
  3652. }
  3653. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3654. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3655. // qk = quantized weights per x block
  3656. // qr = number of quantized weights per data value in x block
  3657. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3658. if (row >= nrows) {
  3659. return;
  3660. }
  3661. const int tid = threadIdx.x;
  3662. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3663. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3664. const int y_offset = qr == 1 ? 1 : qk/2;
  3665. // partial sum for each thread
  3666. #ifdef GGML_CUDA_F16
  3667. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3668. #else
  3669. float tmp = 0.0f;
  3670. #endif // GGML_CUDA_F16
  3671. for (int i = 0; i < ncols; i += iter_stride) {
  3672. const int col = i + vals_per_iter*tid;
  3673. const int ib = (row*ncols + col)/qk; // x block index
  3674. const int iqs = (col%qk)/qr; // x quant index
  3675. const int iybs = col - col%qk; // y block start index
  3676. // processing >2 values per i iter is faster for fast GPUs
  3677. #pragma unroll
  3678. for (int j = 0; j < vals_per_iter; j += 2) {
  3679. // process 2 vals per j iter
  3680. // dequantize
  3681. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3682. dfloat2 v;
  3683. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3684. // matrix multiplication
  3685. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3686. #ifdef GGML_CUDA_F16
  3687. tmp += __hmul2(v, {
  3688. y[iybs + iqs + j/qr + 0],
  3689. y[iybs + iqs + j/qr + y_offset]
  3690. });
  3691. #else
  3692. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3693. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3694. #endif // GGML_CUDA_F16
  3695. }
  3696. }
  3697. // sum up partial sums and write back result
  3698. #pragma unroll
  3699. for (int mask = 16; mask > 0; mask >>= 1) {
  3700. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3701. }
  3702. if (tid == 0) {
  3703. #ifdef GGML_CUDA_F16
  3704. dst[row] = tmp.x + tmp.y;
  3705. #else
  3706. dst[row] = tmp;
  3707. #endif // GGML_CUDA_F16
  3708. }
  3709. }
  3710. static __global__ void mul_mat_p021_f16_f32(
  3711. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3712. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3713. const half * x = (const half *) vx;
  3714. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3715. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3716. const int channel_x = channel / (nchannels_y / nchannels_x);
  3717. const int nrows_y = ncols_x;
  3718. const int nrows_dst = nrows_x;
  3719. const int row_dst = row_x;
  3720. float tmp = 0.0f;
  3721. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3722. const int col_x = col_x0 + threadIdx.x;
  3723. if (col_x >= ncols_x) {
  3724. break;
  3725. }
  3726. // x is transposed and permuted
  3727. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3728. const float xi = __half2float(x[ix]);
  3729. const int row_y = col_x;
  3730. // y is not transposed but permuted
  3731. const int iy = channel*nrows_y + row_y;
  3732. tmp += xi * y[iy];
  3733. }
  3734. // dst is not transposed and not permuted
  3735. const int idst = channel*nrows_dst + row_dst;
  3736. // sum up partial sums and write back result
  3737. #pragma unroll
  3738. for (int mask = 16; mask > 0; mask >>= 1) {
  3739. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3740. }
  3741. if (threadIdx.x == 0) {
  3742. dst[idst] = tmp;
  3743. }
  3744. }
  3745. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3746. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3747. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3748. const half * x = (const half *) vx;
  3749. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3750. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3751. const int channel_x = channel / channel_x_divisor;
  3752. const int nrows_y = ncols_x;
  3753. const int nrows_dst = nrows_x;
  3754. const int row_dst = row_x;
  3755. const int idst = channel*nrows_dst + row_dst;
  3756. float tmp = 0.0f;
  3757. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3758. const int col_x = col_x0 + threadIdx.x;
  3759. if (col_x >= ncols_x) {
  3760. break;
  3761. }
  3762. const int row_y = col_x;
  3763. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3764. const int iy = channel*nrows_y + row_y;
  3765. const float xi = __half2float(x[ix]);
  3766. tmp += xi * y[iy];
  3767. }
  3768. // sum up partial sums and write back result
  3769. #pragma unroll
  3770. for (int mask = 16; mask > 0; mask >>= 1) {
  3771. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3772. }
  3773. if (threadIdx.x == 0) {
  3774. dst[idst] = tmp;
  3775. }
  3776. }
  3777. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3778. const float * xi = (const float *) cxi;
  3779. float * dsti = (float *) cdsti;
  3780. *dsti = *xi;
  3781. }
  3782. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3783. const float * xi = (const float *) cxi;
  3784. half * dsti = (half *) cdsti;
  3785. *dsti = __float2half(*xi);
  3786. }
  3787. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3788. const half * xi = (const half *) cxi;
  3789. half * dsti = (half *) cdsti;
  3790. *dsti = *xi;
  3791. }
  3792. template <cpy_kernel_t cpy_1>
  3793. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3794. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3795. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3796. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3797. if (i >= ne) {
  3798. return;
  3799. }
  3800. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3801. // then combine those indices with the corresponding byte offsets to get the total offsets
  3802. const int i02 = i / (ne00*ne01);
  3803. const int i01 = (i - i02*ne01*ne00) / ne00;
  3804. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3805. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3806. const int i12 = i / (ne10*ne11);
  3807. const int i11 = (i - i12*ne10*ne11) / ne10;
  3808. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3809. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3810. cpy_1(cx + x_offset, cdst + dst_offset);
  3811. }
  3812. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  3813. const float * xi = (const float *) cxi;
  3814. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  3815. float amax = 0.0f; // absolute max
  3816. for (int j = 0; j < QK8_0; j++) {
  3817. const float v = xi[j];
  3818. amax = fmaxf(amax, fabsf(v));
  3819. }
  3820. const float d = amax / ((1 << 7) - 1);
  3821. const float id = d ? 1.0f/d : 0.0f;
  3822. dsti->d = d;
  3823. for (int j = 0; j < QK8_0; ++j) {
  3824. const float x0 = xi[j]*id;
  3825. dsti->qs[j] = roundf(x0);
  3826. }
  3827. }
  3828. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  3829. const float * xi = (const float *) cxi;
  3830. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  3831. float amax = 0.0f;
  3832. float vmax = 0.0f;
  3833. for (int j = 0; j < QK4_0; ++j) {
  3834. const float v = xi[j];
  3835. if (amax < fabsf(v)) {
  3836. amax = fabsf(v);
  3837. vmax = v;
  3838. }
  3839. }
  3840. const float d = vmax / -8;
  3841. const float id = d ? 1.0f/d : 0.0f;
  3842. dsti->d = d;
  3843. for (int j = 0; j < QK4_0/2; ++j) {
  3844. const float x0 = xi[0 + j]*id;
  3845. const float x1 = xi[QK4_0/2 + j]*id;
  3846. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  3847. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  3848. dsti->qs[j] = xi0;
  3849. dsti->qs[j] |= xi1 << 4;
  3850. }
  3851. }
  3852. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  3853. const float * xi = (const float *) cxi;
  3854. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  3855. float vmin = FLT_MAX;
  3856. float vmax = -FLT_MAX;
  3857. for (int j = 0; j < QK4_1; ++j) {
  3858. const float v = xi[j];
  3859. if (v < vmin) vmin = v;
  3860. if (v > vmax) vmax = v;
  3861. }
  3862. const float d = (vmax - vmin) / ((1 << 4) - 1);
  3863. const float id = d ? 1.0f/d : 0.0f;
  3864. dsti->dm.x = d;
  3865. dsti->dm.y = vmin;
  3866. for (int j = 0; j < QK4_1/2; ++j) {
  3867. const float x0 = (xi[0 + j] - vmin)*id;
  3868. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  3869. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  3870. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  3871. dsti->qs[j] = xi0;
  3872. dsti->qs[j] |= xi1 << 4;
  3873. }
  3874. }
  3875. template <cpy_kernel_t cpy_blck, int qk>
  3876. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  3877. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3878. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3879. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  3880. if (i >= ne) {
  3881. return;
  3882. }
  3883. const int i02 = i / (ne00*ne01);
  3884. const int i01 = (i - i02*ne01*ne00) / ne00;
  3885. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  3886. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3887. const int i12 = i / (ne10*ne11);
  3888. const int i11 = (i - i12*ne10*ne11) / ne10;
  3889. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  3890. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3891. cpy_blck(cx + x_offset, cdst + dst_offset);
  3892. }
  3893. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3894. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3895. return 1.0f - min(1.0f, max(0.0f, y));
  3896. }
  3897. struct rope_corr_dims {
  3898. float v[4];
  3899. };
  3900. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3901. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3902. static __device__ void rope_yarn(
  3903. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3904. float * cos_theta, float * sin_theta
  3905. ) {
  3906. // Get n-d rotational scaling corrected for extrapolation
  3907. float theta_interp = freq_scale * theta_extrap;
  3908. float theta = theta_interp;
  3909. if (ext_factor != 0.0f) {
  3910. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3911. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3912. // Get n-d magnitude scaling corrected for interpolation
  3913. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3914. }
  3915. *cos_theta = cosf(theta) * mscale;
  3916. *sin_theta = sinf(theta) * mscale;
  3917. }
  3918. // rope == RoPE == rotary positional embedding
  3919. template<typename T, bool has_pos>
  3920. static __global__ void rope(
  3921. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3922. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3923. ) {
  3924. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3925. if (col >= ncols) {
  3926. return;
  3927. }
  3928. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3929. const int i = row*ncols + col;
  3930. const int i2 = row/p_delta_rows;
  3931. const int p = has_pos ? pos[i2] : 0;
  3932. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3933. float cos_theta, sin_theta;
  3934. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3935. const float x0 = x[i + 0];
  3936. const float x1 = x[i + 1];
  3937. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3938. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3939. }
  3940. template<typename T, bool has_pos>
  3941. static __global__ void rope_neox(
  3942. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  3943. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  3944. ) {
  3945. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3946. if (col >= ncols) {
  3947. return;
  3948. }
  3949. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3950. const int ib = col / n_dims;
  3951. const int ic = col % n_dims;
  3952. if (ib > 0) {
  3953. const int i = row*ncols + ib*n_dims + ic;
  3954. dst[i + 0] = x[i + 0];
  3955. dst[i + 1] = x[i + 1];
  3956. return;
  3957. }
  3958. const int i = row*ncols + ib*n_dims + ic/2;
  3959. const int i2 = row/p_delta_rows;
  3960. float cur_rot = inv_ndims * ic - ib;
  3961. const int p = has_pos ? pos[i2] : 0;
  3962. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  3963. float cos_theta, sin_theta;
  3964. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3965. const float x0 = x[i + 0];
  3966. const float x1 = x[i + n_dims/2];
  3967. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3968. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  3969. }
  3970. static __global__ void rope_glm_f32(
  3971. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3972. int n_ctx
  3973. ) {
  3974. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3975. const int half_n_dims = ncols/4;
  3976. if (col >= half_n_dims) {
  3977. return;
  3978. }
  3979. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3980. const int i = row*ncols + col;
  3981. const int i2 = row/p_delta_rows;
  3982. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  3983. // FIXME: this is likely wrong
  3984. const int p = pos != nullptr ? pos[i2] : 0;
  3985. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  3986. const float sin_theta = sinf(theta);
  3987. const float cos_theta = cosf(theta);
  3988. const float x0 = x[i + 0];
  3989. const float x1 = x[i + half_n_dims];
  3990. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3991. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  3992. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  3993. const float sin_block_theta = sinf(block_theta);
  3994. const float cos_block_theta = cosf(block_theta);
  3995. const float x2 = x[i + half_n_dims * 2];
  3996. const float x3 = x[i + half_n_dims * 3];
  3997. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  3998. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  3999. }
  4000. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4001. const int n_heads_log2_floor, const float m0, const float m1) {
  4002. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4003. if (col >= ncols) {
  4004. return;
  4005. }
  4006. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4007. const int i = row*ncols + col;
  4008. const int k = row/k_rows;
  4009. float m_k;
  4010. if (k < n_heads_log2_floor) {
  4011. m_k = powf(m0, k + 1);
  4012. } else {
  4013. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4014. }
  4015. dst[i] = col * m_k + x[i];
  4016. }
  4017. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4018. const int row = blockIdx.y;
  4019. const int col = threadIdx.x;
  4020. float sum = 0.0f;
  4021. for (int i = col; i < ncols; i += blockDim.x) {
  4022. sum += x[row * ncols + i];
  4023. }
  4024. sum = warp_reduce_sum(sum);
  4025. if (col == 0) {
  4026. dst[row] = sum;
  4027. }
  4028. }
  4029. template<typename T>
  4030. static inline __device__ void swap(T & a, T & b) {
  4031. T tmp = a;
  4032. a = b;
  4033. b = tmp;
  4034. }
  4035. template<ggml_sort_order order>
  4036. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4037. // bitonic sort
  4038. int col = threadIdx.x;
  4039. int row = blockIdx.y;
  4040. if (col >= ncols) return;
  4041. const float * x_row = x + row * ncols;
  4042. int * dst_row = dst + row * ncols;
  4043. // initialize indices
  4044. if (col < ncols) {
  4045. dst_row[col] = col;
  4046. }
  4047. __syncthreads();
  4048. for (int k = 2; k <= ncols; k *= 2) {
  4049. for (int j = k / 2; j > 0; j /= 2) {
  4050. int ixj = col ^ j;
  4051. if (ixj > col) {
  4052. if ((col & k) == 0) {
  4053. 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]]) {
  4054. swap(dst_row[col], dst_row[ixj]);
  4055. }
  4056. } else {
  4057. 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]]) {
  4058. swap(dst_row[col], dst_row[ixj]);
  4059. }
  4060. }
  4061. }
  4062. __syncthreads();
  4063. }
  4064. }
  4065. }
  4066. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4067. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4068. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4069. if (col >= ncols) {
  4070. return;
  4071. }
  4072. const int i = row*ncols + col;
  4073. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4074. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4075. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4076. }
  4077. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {
  4078. const int tid = threadIdx.x;
  4079. const int rowx = blockIdx.x;
  4080. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4081. const int block_size = blockDim.x;
  4082. const int warp_id = threadIdx.x / WARP_SIZE;
  4083. const int lane_id = threadIdx.x % WARP_SIZE;
  4084. __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];
  4085. float max_val = -INFINITY;
  4086. for (int col = tid; col < ncols; col += block_size) {
  4087. const int ix = rowx*ncols + col;
  4088. const int iy = rowy*ncols + col;
  4089. max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));
  4090. }
  4091. // find the max value in the block
  4092. max_val = warp_reduce_max(max_val);
  4093. if (block_size > WARP_SIZE) {
  4094. if (warp_id == 0) {
  4095. buf[lane_id] = -INFINITY;
  4096. }
  4097. __syncthreads();
  4098. if (lane_id == 0) {
  4099. buf[warp_id] = max_val;
  4100. }
  4101. __syncthreads();
  4102. max_val = buf[lane_id];
  4103. max_val = warp_reduce_max(max_val);
  4104. }
  4105. float tmp = 0.f;
  4106. for (int col = tid; col < ncols; col += block_size) {
  4107. const int ix = rowx*ncols + col;
  4108. const int iy = rowy*ncols + col;
  4109. const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);
  4110. tmp += val;
  4111. dst[ix] = val;
  4112. }
  4113. // find the sum of exps in the block
  4114. tmp = warp_reduce_sum(tmp);
  4115. if (block_size > WARP_SIZE) {
  4116. if (warp_id == 0) {
  4117. buf[lane_id] = 0.f;
  4118. }
  4119. __syncthreads();
  4120. if (lane_id == 0) {
  4121. buf[warp_id] = tmp;
  4122. }
  4123. __syncthreads();
  4124. tmp = buf[lane_id];
  4125. tmp = warp_reduce_sum(tmp);
  4126. }
  4127. const float inv_tmp = 1.f / tmp;
  4128. for (int col = tid; col < ncols; col += block_size) {
  4129. const int i = rowx*ncols + col;
  4130. dst[i] *= inv_tmp;
  4131. }
  4132. }
  4133. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4134. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4135. if (i >= k) {
  4136. return;
  4137. }
  4138. dst[i] = scale * x[i];
  4139. }
  4140. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4141. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4142. if (i >= k) {
  4143. return;
  4144. }
  4145. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4146. }
  4147. static __global__ void im2col_f32_f16(
  4148. const float * x, half * dst,
  4149. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4150. int s0, int s1, int p0, int p1, int d0, int d1) {
  4151. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4152. if (i >= pelements) {
  4153. return;
  4154. }
  4155. const int ksize = OW * (KH > 1 ? KW : 1);
  4156. const int kx = i / ksize;
  4157. const int kd = kx * ksize;
  4158. const int ky = (i - kd) / OW;
  4159. const int ix = i % OW;
  4160. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4161. const int64_t iih = blockIdx.y * s1 + ky * d1 - p1;
  4162. const int64_t offset_dst =
  4163. (blockIdx.y * OW + ix) * CHW +
  4164. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4165. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4166. dst[offset_dst] = __float2half(0.0f);
  4167. } else {
  4168. const int64_t offset_src = blockIdx.z * offset_delta;
  4169. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4170. }
  4171. }
  4172. template<int qk, int qr, dequantize_kernel_t dq>
  4173. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4174. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4175. GGML_TENSOR_BINARY_OP_LOCALS
  4176. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4177. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4178. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4179. // strides in elements
  4180. //const size_t s0 = nb0 / ggml_element_size(dst);
  4181. const size_t s1 = nb1 / ggml_element_size(dst);
  4182. const size_t s2 = nb2 / ggml_element_size(dst);
  4183. const size_t s3 = nb3 / ggml_element_size(dst);
  4184. const size_t s10 = nb10 / ggml_element_size(src1);
  4185. const size_t s11 = nb11 / ggml_element_size(src1);
  4186. const size_t s12 = nb12 / ggml_element_size(src1);
  4187. //const size_t s13 = nb13 / ggml_element_size(src1);
  4188. GGML_ASSERT(ne00 % 2 == 0);
  4189. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4190. src0_dd, src1_dd, dst_dd,
  4191. ne00, /*ne01, ne02, ne03,*/
  4192. /*ne10, ne11,*/ ne12, /*ne13,*/
  4193. /* s0,*/ s1, s2, s3,
  4194. /* nb00,*/ nb01, nb02, nb03,
  4195. s10, s11, s12/*, s13*/);
  4196. (void) dst;
  4197. }
  4198. template<typename src0_t>
  4199. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4200. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4201. GGML_TENSOR_BINARY_OP_LOCALS
  4202. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4203. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4204. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4205. // strides in elements
  4206. //const size_t s0 = nb0 / ggml_element_size(dst);
  4207. const size_t s1 = nb1 / ggml_element_size(dst);
  4208. const size_t s2 = nb2 / ggml_element_size(dst);
  4209. const size_t s3 = nb3 / ggml_element_size(dst);
  4210. const size_t s10 = nb10 / ggml_element_size(src1);
  4211. const size_t s11 = nb11 / ggml_element_size(src1);
  4212. const size_t s12 = nb12 / ggml_element_size(src1);
  4213. //const size_t s13 = nb13 / ggml_element_size(src1);
  4214. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4215. src0_dd, src1_dd, dst_dd,
  4216. ne00, /*ne01, ne02, ne03,*/
  4217. /*ne10, ne11,*/ ne12, /*ne13,*/
  4218. /* s0,*/ s1, s2, s3,
  4219. /* nb00,*/ nb01, nb02, nb03,
  4220. s10, s11, s12/*, s13*/);
  4221. (void) dst;
  4222. }
  4223. template<float (*bin_op)(const float, const float)>
  4224. struct bin_bcast_cuda {
  4225. template<typename src0_t, typename src1_t, typename dst_t>
  4226. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4227. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4228. cudaStream_t stream) {
  4229. GGML_TENSOR_BINARY_OP_LOCALS
  4230. int nr0 = ne10/ne0;
  4231. int nr1 = ne11/ne1;
  4232. int nr2 = ne12/ne2;
  4233. int nr3 = ne13/ne3;
  4234. int nr[4] = { nr0, nr1, nr2, nr3 };
  4235. // collapse dimensions until first broadcast dimension
  4236. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4237. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4238. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4239. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4240. auto collapse = [](int64_t cne[]) {
  4241. cne[0] *= cne[1];
  4242. cne[1] = cne[2];
  4243. cne[2] = cne[3];
  4244. cne[3] = 1;
  4245. };
  4246. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  4247. cnb[1] *= cne[1];
  4248. cnb[2] *= cne[2];
  4249. cnb[3] *= cne[3];
  4250. };
  4251. for (int i = 0; i < 4; i++) {
  4252. if (nr[i] != 1) {
  4253. break;
  4254. }
  4255. if (i > 0) {
  4256. collapse_nb(cnb0, cne0);
  4257. collapse_nb(cnb1, cne1);
  4258. collapse(cne0);
  4259. collapse(cne1);
  4260. }
  4261. }
  4262. {
  4263. int64_t ne0 = cne0[0];
  4264. int64_t ne1 = cne0[1];
  4265. int64_t ne2 = cne0[2];
  4266. int64_t ne3 = cne0[3];
  4267. int64_t ne10 = cne1[0];
  4268. int64_t ne11 = cne1[1];
  4269. int64_t ne12 = cne1[2];
  4270. int64_t ne13 = cne1[3];
  4271. size_t nb0 = cnb0[0];
  4272. size_t nb1 = cnb0[1];
  4273. size_t nb2 = cnb0[2];
  4274. size_t nb3 = cnb0[3];
  4275. size_t nb10 = cnb1[0];
  4276. size_t nb11 = cnb1[1];
  4277. size_t nb12 = cnb1[2];
  4278. size_t nb13 = cnb1[3];
  4279. size_t s0 = nb0 / sizeof(dst_t);
  4280. size_t s1 = nb1 / sizeof(dst_t);
  4281. size_t s2 = nb2 / sizeof(dst_t);
  4282. size_t s3 = nb3 / sizeof(dst_t);
  4283. size_t s10 = nb10 / sizeof(src1_t);
  4284. size_t s11 = nb11 / sizeof(src1_t);
  4285. size_t s12 = nb12 / sizeof(src1_t);
  4286. size_t s13 = nb13 / sizeof(src1_t);
  4287. GGML_ASSERT(s0 == 1);
  4288. GGML_ASSERT(s10 == 1);
  4289. const int block_size = 128;
  4290. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4291. dim3 block_dims;
  4292. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4293. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4294. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4295. dim3 block_nums(
  4296. (hne0 + block_dims.x - 1) / block_dims.x,
  4297. (ne1 + block_dims.y - 1) / block_dims.y,
  4298. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4299. );
  4300. if (block_nums.z > 65535) {
  4301. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4302. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4303. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4304. src0_dd, src1_dd, dst_dd,
  4305. ne0, ne1, ne2, ne3,
  4306. ne10, ne11, ne12, ne13,
  4307. /* s0, */ s1, s2, s3,
  4308. /* s10, */ s11, s12, s13);
  4309. } else {
  4310. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4311. src0_dd, src1_dd, dst_dd,
  4312. ne0, ne1, ne2, ne3,
  4313. ne10, ne11, ne12, ne13,
  4314. /* s0, */ s1, s2, s3,
  4315. /* s10, */ s11, s12, s13);
  4316. }
  4317. }
  4318. }
  4319. };
  4320. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4321. const int ne10, const int ne11, const int ne12,
  4322. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4323. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4324. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4325. }
  4326. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4327. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4328. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4329. }
  4330. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4331. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4332. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4333. }
  4334. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4335. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4336. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4337. }
  4338. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4339. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4340. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4341. }
  4342. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4343. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4344. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4345. }
  4346. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4347. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4348. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4349. }
  4350. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4351. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4352. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4353. }
  4354. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4355. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4356. if (ncols < 1024) {
  4357. const dim3 block_dims(WARP_SIZE, 1, 1);
  4358. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4359. } else {
  4360. const dim3 block_dims(1024, 1, 1);
  4361. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4362. }
  4363. }
  4364. static void group_norm_f32_cuda(const float * x, float * dst, const int num_groups, const int group_size, const int ne_elements, cudaStream_t stream) {
  4365. static const float eps = 1e-6f;
  4366. if (group_size < 1024) {
  4367. const dim3 block_dims(WARP_SIZE, 1, 1);
  4368. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4369. } else {
  4370. const dim3 block_dims(1024, 1, 1);
  4371. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4372. }
  4373. }
  4374. static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
  4375. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4376. dim3 gridDim(num_blocks, ne1, ne2);
  4377. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4378. }
  4379. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int scale_factor, cudaStream_t stream) {
  4380. int ne0 = (ne00 * scale_factor);
  4381. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4382. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4383. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4384. }
  4385. static void pad_f32_cuda(const float * x, float * dst,
  4386. const int ne00, const int ne01, const int ne02,
  4387. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4388. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4389. dim3 gridDim(num_blocks, ne1, ne2);
  4390. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4391. }
  4392. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4393. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4394. if (ncols < 1024) {
  4395. const dim3 block_dims(WARP_SIZE, 1, 1);
  4396. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4397. } else {
  4398. const dim3 block_dims(1024, 1, 1);
  4399. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4400. }
  4401. }
  4402. 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) {
  4403. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4404. const dim3 num_blocks(block_num_x, ky, 1);
  4405. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4406. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4407. }
  4408. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4409. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4410. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4411. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4412. }
  4413. template<typename dst_t>
  4414. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4415. const int nb = k / QK_K;
  4416. #if QK_K == 256
  4417. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4418. #else
  4419. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4420. #endif
  4421. }
  4422. template<typename dst_t>
  4423. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4424. const int nb = k / QK_K;
  4425. #if QK_K == 256
  4426. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4427. #else
  4428. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4429. #endif
  4430. }
  4431. template<typename dst_t>
  4432. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4433. const int nb = k / QK_K;
  4434. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4435. }
  4436. template<typename dst_t>
  4437. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4438. const int nb = k / QK_K;
  4439. #if QK_K == 256
  4440. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4441. #else
  4442. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4443. #endif
  4444. }
  4445. template<typename dst_t>
  4446. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4447. const int nb = k / QK_K;
  4448. #if QK_K == 256
  4449. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4450. #else
  4451. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4452. #endif
  4453. }
  4454. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4455. switch (type) {
  4456. case GGML_TYPE_Q4_0:
  4457. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4458. case GGML_TYPE_Q4_1:
  4459. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4460. case GGML_TYPE_Q5_0:
  4461. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4462. case GGML_TYPE_Q5_1:
  4463. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4464. case GGML_TYPE_Q8_0:
  4465. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4466. case GGML_TYPE_Q2_K:
  4467. return dequantize_row_q2_K_cuda;
  4468. case GGML_TYPE_Q3_K:
  4469. return dequantize_row_q3_K_cuda;
  4470. case GGML_TYPE_Q4_K:
  4471. return dequantize_row_q4_K_cuda;
  4472. case GGML_TYPE_Q5_K:
  4473. return dequantize_row_q5_K_cuda;
  4474. case GGML_TYPE_Q6_K:
  4475. return dequantize_row_q6_K_cuda;
  4476. case GGML_TYPE_F32:
  4477. return dequantize_block_cuda<1, 1, convert_f32>;
  4478. default:
  4479. return nullptr;
  4480. }
  4481. }
  4482. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4483. switch (type) {
  4484. case GGML_TYPE_Q4_0:
  4485. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4486. case GGML_TYPE_Q4_1:
  4487. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4488. case GGML_TYPE_Q5_0:
  4489. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4490. case GGML_TYPE_Q5_1:
  4491. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4492. case GGML_TYPE_Q8_0:
  4493. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4494. case GGML_TYPE_Q2_K:
  4495. return dequantize_row_q2_K_cuda;
  4496. case GGML_TYPE_Q3_K:
  4497. return dequantize_row_q3_K_cuda;
  4498. case GGML_TYPE_Q4_K:
  4499. return dequantize_row_q4_K_cuda;
  4500. case GGML_TYPE_Q5_K:
  4501. return dequantize_row_q5_K_cuda;
  4502. case GGML_TYPE_Q6_K:
  4503. return dequantize_row_q6_K_cuda;
  4504. case GGML_TYPE_F16:
  4505. return dequantize_block_cuda<1, 1, convert_f16>;
  4506. default:
  4507. return nullptr;
  4508. }
  4509. }
  4510. 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) {
  4511. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4512. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4513. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4514. const dim3 block_nums(block_num_y, 1, 1);
  4515. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4516. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4517. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4518. }
  4519. 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) {
  4520. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4521. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4522. const dim3 block_nums(block_num_y, 1, 1);
  4523. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4524. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4525. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4526. }
  4527. 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) {
  4528. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4529. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4530. const dim3 block_nums(block_num_y, 1, 1);
  4531. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4532. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4533. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4534. }
  4535. 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) {
  4536. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4537. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4538. const dim3 block_nums(block_num_y, 1, 1);
  4539. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4540. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4541. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4542. }
  4543. 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) {
  4544. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4545. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4546. const dim3 block_nums(block_num_y, 1, 1);
  4547. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4548. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4549. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4550. }
  4551. 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) {
  4552. GGML_ASSERT(ncols % QK_K == 0);
  4553. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4554. const int block_num_y = (nrows + ny - 1) / ny;
  4555. const dim3 block_nums(block_num_y, 1, 1);
  4556. const dim3 block_dims(32, ny, 1);
  4557. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4558. }
  4559. 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) {
  4560. GGML_ASSERT(ncols % QK_K == 0);
  4561. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4562. const int block_num_y = (nrows + ny - 1) / ny;
  4563. const dim3 block_nums(block_num_y, 1, 1);
  4564. const dim3 block_dims(32, ny, 1);
  4565. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4566. }
  4567. 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) {
  4568. GGML_ASSERT(ncols % QK_K == 0);
  4569. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4570. const int block_num_y = (nrows + ny - 1) / ny;
  4571. const dim3 block_nums(block_num_y, 1, 1);
  4572. const dim3 block_dims(32, ny, 1);
  4573. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4574. }
  4575. 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) {
  4576. GGML_ASSERT(ncols % QK_K == 0);
  4577. const dim3 block_dims(32, 1, 1);
  4578. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4579. }
  4580. 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) {
  4581. GGML_ASSERT(ncols % QK_K == 0);
  4582. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4583. const int block_num_y = (nrows + ny - 1) / ny;
  4584. const dim3 block_nums(block_num_y, 1, 1);
  4585. const dim3 block_dims(32, ny, 1);
  4586. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4587. }
  4588. 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) {
  4589. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4590. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4591. const dim3 block_nums(block_num_y, 1, 1);
  4592. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4593. dequantize_mul_mat_vec<1, 1, convert_f16>
  4594. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4595. }
  4596. 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) {
  4597. GGML_ASSERT(ncols % QK4_0 == 0);
  4598. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4599. const dim3 block_nums(block_num_y, 1, 1);
  4600. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4601. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4602. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4603. }
  4604. 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) {
  4605. GGML_ASSERT(ncols % QK4_1 == 0);
  4606. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4607. const dim3 block_nums(block_num_y, 1, 1);
  4608. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4609. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4610. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4611. }
  4612. 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) {
  4613. GGML_ASSERT(ncols % QK5_0 == 0);
  4614. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4615. const dim3 block_nums(block_num_y, 1, 1);
  4616. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4617. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4618. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4619. }
  4620. 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) {
  4621. GGML_ASSERT(ncols % QK5_1 == 0);
  4622. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4623. const dim3 block_nums(block_num_y, 1, 1);
  4624. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4625. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4626. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4627. }
  4628. 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) {
  4629. GGML_ASSERT(ncols % QK8_0 == 0);
  4630. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4631. const dim3 block_nums(block_num_y, 1, 1);
  4632. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4633. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4634. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4635. }
  4636. 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) {
  4637. GGML_ASSERT(ncols % QK_K == 0);
  4638. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4639. const dim3 block_nums(block_num_y, 1, 1);
  4640. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4641. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  4642. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4643. }
  4644. 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) {
  4645. GGML_ASSERT(ncols % QK_K == 0);
  4646. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4647. const dim3 block_nums(block_num_y, 1, 1);
  4648. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4649. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4650. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4651. }
  4652. 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) {
  4653. GGML_ASSERT(ncols % QK_K == 0);
  4654. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4655. const dim3 block_nums(block_num_y, 1, 1);
  4656. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4657. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4658. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4659. }
  4660. 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) {
  4661. GGML_ASSERT(ncols % QK_K == 0);
  4662. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4663. const dim3 block_nums(block_num_y, 1, 1);
  4664. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4665. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4666. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4667. }
  4668. 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) {
  4669. GGML_ASSERT(ncols % QK_K == 0);
  4670. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4671. const dim3 block_nums(block_num_y, 1, 1);
  4672. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4673. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4674. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4675. }
  4676. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4677. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4678. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4679. int id;
  4680. CUDA_CHECK(cudaGetDevice(&id));
  4681. const int compute_capability = g_compute_capabilities[id];
  4682. int mmq_x, mmq_y, nwarps;
  4683. if (compute_capability >= CC_RDNA2) {
  4684. mmq_x = MMQ_X_Q4_0_RDNA2;
  4685. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4686. nwarps = NWARPS_Q4_0_RDNA2;
  4687. } else if (compute_capability >= CC_OFFSET_AMD) {
  4688. mmq_x = MMQ_X_Q4_0_RDNA1;
  4689. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4690. nwarps = NWARPS_Q4_0_RDNA1;
  4691. } else if (compute_capability >= CC_VOLTA) {
  4692. mmq_x = MMQ_X_Q4_0_AMPERE;
  4693. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4694. nwarps = NWARPS_Q4_0_AMPERE;
  4695. } else if (compute_capability >= MIN_CC_DP4A) {
  4696. mmq_x = MMQ_X_Q4_0_PASCAL;
  4697. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4698. nwarps = NWARPS_Q4_0_PASCAL;
  4699. } else {
  4700. GGML_ASSERT(false);
  4701. }
  4702. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4703. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4704. const dim3 block_nums(block_num_x, block_num_y, 1);
  4705. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4706. if (nrows_x % mmq_y == 0) {
  4707. const bool need_check = false;
  4708. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4709. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4710. } else {
  4711. const bool need_check = true;
  4712. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4713. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4714. }
  4715. }
  4716. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4717. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4718. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4719. int id;
  4720. CUDA_CHECK(cudaGetDevice(&id));
  4721. const int compute_capability = g_compute_capabilities[id];
  4722. int mmq_x, mmq_y, nwarps;
  4723. if (compute_capability >= CC_RDNA2) {
  4724. mmq_x = MMQ_X_Q4_1_RDNA2;
  4725. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4726. nwarps = NWARPS_Q4_1_RDNA2;
  4727. } else if (compute_capability >= CC_OFFSET_AMD) {
  4728. mmq_x = MMQ_X_Q4_1_RDNA1;
  4729. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4730. nwarps = NWARPS_Q4_1_RDNA1;
  4731. } else if (compute_capability >= CC_VOLTA) {
  4732. mmq_x = MMQ_X_Q4_1_AMPERE;
  4733. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4734. nwarps = NWARPS_Q4_1_AMPERE;
  4735. } else if (compute_capability >= MIN_CC_DP4A) {
  4736. mmq_x = MMQ_X_Q4_1_PASCAL;
  4737. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4738. nwarps = NWARPS_Q4_1_PASCAL;
  4739. } else {
  4740. GGML_ASSERT(false);
  4741. }
  4742. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4743. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4744. const dim3 block_nums(block_num_x, block_num_y, 1);
  4745. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4746. if (nrows_x % mmq_y == 0) {
  4747. const bool need_check = false;
  4748. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4749. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4750. } else {
  4751. const bool need_check = true;
  4752. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4753. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4754. }
  4755. }
  4756. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4757. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4758. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4759. int id;
  4760. CUDA_CHECK(cudaGetDevice(&id));
  4761. const int compute_capability = g_compute_capabilities[id];
  4762. int mmq_x, mmq_y, nwarps;
  4763. if (compute_capability >= CC_RDNA2) {
  4764. mmq_x = MMQ_X_Q5_0_RDNA2;
  4765. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4766. nwarps = NWARPS_Q5_0_RDNA2;
  4767. } else if (compute_capability >= CC_OFFSET_AMD) {
  4768. mmq_x = MMQ_X_Q5_0_RDNA1;
  4769. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4770. nwarps = NWARPS_Q5_0_RDNA1;
  4771. } else if (compute_capability >= CC_VOLTA) {
  4772. mmq_x = MMQ_X_Q5_0_AMPERE;
  4773. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4774. nwarps = NWARPS_Q5_0_AMPERE;
  4775. } else if (compute_capability >= MIN_CC_DP4A) {
  4776. mmq_x = MMQ_X_Q5_0_PASCAL;
  4777. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4778. nwarps = NWARPS_Q5_0_PASCAL;
  4779. } else {
  4780. GGML_ASSERT(false);
  4781. }
  4782. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4783. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4784. const dim3 block_nums(block_num_x, block_num_y, 1);
  4785. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4786. if (nrows_x % mmq_y == 0) {
  4787. const bool need_check = false;
  4788. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4789. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4790. } else {
  4791. const bool need_check = true;
  4792. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4793. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4794. }
  4795. }
  4796. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4797. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4798. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4799. int id;
  4800. CUDA_CHECK(cudaGetDevice(&id));
  4801. const int compute_capability = g_compute_capabilities[id];
  4802. int mmq_x, mmq_y, nwarps;
  4803. if (compute_capability >= CC_RDNA2) {
  4804. mmq_x = MMQ_X_Q5_1_RDNA2;
  4805. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4806. nwarps = NWARPS_Q5_1_RDNA2;
  4807. } else if (compute_capability >= CC_OFFSET_AMD) {
  4808. mmq_x = MMQ_X_Q5_1_RDNA1;
  4809. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4810. nwarps = NWARPS_Q5_1_RDNA1;
  4811. } else if (compute_capability >= CC_VOLTA) {
  4812. mmq_x = MMQ_X_Q5_1_AMPERE;
  4813. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4814. nwarps = NWARPS_Q5_1_AMPERE;
  4815. } else if (compute_capability >= MIN_CC_DP4A) {
  4816. mmq_x = MMQ_X_Q5_1_PASCAL;
  4817. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4818. nwarps = NWARPS_Q5_1_PASCAL;
  4819. } else {
  4820. GGML_ASSERT(false);
  4821. }
  4822. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4823. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4824. const dim3 block_nums(block_num_x, block_num_y, 1);
  4825. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4826. if (nrows_x % mmq_y == 0) {
  4827. const bool need_check = false;
  4828. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4829. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4830. } else {
  4831. const bool need_check = true;
  4832. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4833. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4834. }
  4835. }
  4836. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4837. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4838. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4839. int id;
  4840. CUDA_CHECK(cudaGetDevice(&id));
  4841. const int compute_capability = g_compute_capabilities[id];
  4842. int mmq_x, mmq_y, nwarps;
  4843. if (compute_capability >= CC_RDNA2) {
  4844. mmq_x = MMQ_X_Q8_0_RDNA2;
  4845. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4846. nwarps = NWARPS_Q8_0_RDNA2;
  4847. } else if (compute_capability >= CC_OFFSET_AMD) {
  4848. mmq_x = MMQ_X_Q8_0_RDNA1;
  4849. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4850. nwarps = NWARPS_Q8_0_RDNA1;
  4851. } else if (compute_capability >= CC_VOLTA) {
  4852. mmq_x = MMQ_X_Q8_0_AMPERE;
  4853. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4854. nwarps = NWARPS_Q8_0_AMPERE;
  4855. } else if (compute_capability >= MIN_CC_DP4A) {
  4856. mmq_x = MMQ_X_Q8_0_PASCAL;
  4857. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4858. nwarps = NWARPS_Q8_0_PASCAL;
  4859. } else {
  4860. GGML_ASSERT(false);
  4861. }
  4862. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4863. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4864. const dim3 block_nums(block_num_x, block_num_y, 1);
  4865. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4866. if (nrows_x % mmq_y == 0) {
  4867. const bool need_check = false;
  4868. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4869. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4870. } else {
  4871. const bool need_check = true;
  4872. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4873. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4874. }
  4875. }
  4876. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4877. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4878. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4879. int id;
  4880. CUDA_CHECK(cudaGetDevice(&id));
  4881. const int compute_capability = g_compute_capabilities[id];
  4882. int mmq_x, mmq_y, nwarps;
  4883. if (compute_capability >= CC_RDNA2) {
  4884. mmq_x = MMQ_X_Q2_K_RDNA2;
  4885. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4886. nwarps = NWARPS_Q2_K_RDNA2;
  4887. } else if (compute_capability >= CC_OFFSET_AMD) {
  4888. mmq_x = MMQ_X_Q2_K_RDNA1;
  4889. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4890. nwarps = NWARPS_Q2_K_RDNA1;
  4891. } else if (compute_capability >= CC_VOLTA) {
  4892. mmq_x = MMQ_X_Q2_K_AMPERE;
  4893. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4894. nwarps = NWARPS_Q2_K_AMPERE;
  4895. } else if (compute_capability >= MIN_CC_DP4A) {
  4896. mmq_x = MMQ_X_Q2_K_PASCAL;
  4897. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4898. nwarps = NWARPS_Q2_K_PASCAL;
  4899. } else {
  4900. GGML_ASSERT(false);
  4901. }
  4902. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4903. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4904. const dim3 block_nums(block_num_x, block_num_y, 1);
  4905. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4906. if (nrows_x % mmq_y == 0) {
  4907. const bool need_check = false;
  4908. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4909. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4910. } else {
  4911. const bool need_check = true;
  4912. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4913. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4914. }
  4915. }
  4916. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4917. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4918. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4919. #if QK_K == 256
  4920. int id;
  4921. CUDA_CHECK(cudaGetDevice(&id));
  4922. const int compute_capability = g_compute_capabilities[id];
  4923. int mmq_x, mmq_y, nwarps;
  4924. if (compute_capability >= CC_RDNA2) {
  4925. mmq_x = MMQ_X_Q3_K_RDNA2;
  4926. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4927. nwarps = NWARPS_Q3_K_RDNA2;
  4928. } else if (compute_capability >= CC_OFFSET_AMD) {
  4929. mmq_x = MMQ_X_Q3_K_RDNA1;
  4930. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4931. nwarps = NWARPS_Q3_K_RDNA1;
  4932. } else if (compute_capability >= CC_VOLTA) {
  4933. mmq_x = MMQ_X_Q3_K_AMPERE;
  4934. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4935. nwarps = NWARPS_Q3_K_AMPERE;
  4936. } else if (compute_capability >= MIN_CC_DP4A) {
  4937. mmq_x = MMQ_X_Q3_K_PASCAL;
  4938. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4939. nwarps = NWARPS_Q3_K_PASCAL;
  4940. } else {
  4941. GGML_ASSERT(false);
  4942. }
  4943. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4944. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4945. const dim3 block_nums(block_num_x, block_num_y, 1);
  4946. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4947. if (nrows_x % mmq_y == 0) {
  4948. const bool need_check = false;
  4949. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4950. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4951. } else {
  4952. const bool need_check = true;
  4953. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4954. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4955. }
  4956. #endif
  4957. }
  4958. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4959. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4960. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4961. int id;
  4962. CUDA_CHECK(cudaGetDevice(&id));
  4963. const int compute_capability = g_compute_capabilities[id];
  4964. int mmq_x, mmq_y, nwarps;
  4965. if (compute_capability >= CC_RDNA2) {
  4966. mmq_x = MMQ_X_Q4_K_RDNA2;
  4967. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4968. nwarps = NWARPS_Q4_K_RDNA2;
  4969. } else if (compute_capability >= CC_OFFSET_AMD) {
  4970. mmq_x = MMQ_X_Q4_K_RDNA1;
  4971. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4972. nwarps = NWARPS_Q4_K_RDNA1;
  4973. } else if (compute_capability >= CC_VOLTA) {
  4974. mmq_x = MMQ_X_Q4_K_AMPERE;
  4975. mmq_y = MMQ_Y_Q4_K_AMPERE;
  4976. nwarps = NWARPS_Q4_K_AMPERE;
  4977. } else if (compute_capability >= MIN_CC_DP4A) {
  4978. mmq_x = MMQ_X_Q4_K_PASCAL;
  4979. mmq_y = MMQ_Y_Q4_K_PASCAL;
  4980. nwarps = NWARPS_Q4_K_PASCAL;
  4981. } else {
  4982. GGML_ASSERT(false);
  4983. }
  4984. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4985. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4986. const dim3 block_nums(block_num_x, block_num_y, 1);
  4987. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4988. if (nrows_x % mmq_y == 0) {
  4989. const bool need_check = false;
  4990. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4991. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4992. } else {
  4993. const bool need_check = true;
  4994. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4995. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4996. }
  4997. }
  4998. static void ggml_mul_mat_q5_K_q8_1_cuda(
  4999. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5000. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5001. int id;
  5002. CUDA_CHECK(cudaGetDevice(&id));
  5003. const int compute_capability = g_compute_capabilities[id];
  5004. int mmq_x, mmq_y, nwarps;
  5005. if (compute_capability >= CC_RDNA2) {
  5006. mmq_x = MMQ_X_Q5_K_RDNA2;
  5007. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5008. nwarps = NWARPS_Q5_K_RDNA2;
  5009. } else if (compute_capability >= CC_OFFSET_AMD) {
  5010. mmq_x = MMQ_X_Q5_K_RDNA1;
  5011. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5012. nwarps = NWARPS_Q5_K_RDNA1;
  5013. } else if (compute_capability >= CC_VOLTA) {
  5014. mmq_x = MMQ_X_Q5_K_AMPERE;
  5015. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5016. nwarps = NWARPS_Q5_K_AMPERE;
  5017. } else if (compute_capability >= MIN_CC_DP4A) {
  5018. mmq_x = MMQ_X_Q5_K_PASCAL;
  5019. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5020. nwarps = NWARPS_Q5_K_PASCAL;
  5021. } else {
  5022. GGML_ASSERT(false);
  5023. }
  5024. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5025. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5026. const dim3 block_nums(block_num_x, block_num_y, 1);
  5027. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5028. if (nrows_x % mmq_y == 0) {
  5029. const bool need_check = false;
  5030. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5031. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5032. } else {
  5033. const bool need_check = true;
  5034. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5035. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5036. }
  5037. }
  5038. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5039. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5040. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5041. int id;
  5042. CUDA_CHECK(cudaGetDevice(&id));
  5043. const int compute_capability = g_compute_capabilities[id];
  5044. int mmq_x, mmq_y, nwarps;
  5045. if (compute_capability >= CC_RDNA2) {
  5046. mmq_x = MMQ_X_Q6_K_RDNA2;
  5047. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5048. nwarps = NWARPS_Q6_K_RDNA2;
  5049. } else if (compute_capability >= CC_OFFSET_AMD) {
  5050. mmq_x = MMQ_X_Q6_K_RDNA1;
  5051. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5052. nwarps = NWARPS_Q6_K_RDNA1;
  5053. } else if (compute_capability >= CC_VOLTA) {
  5054. mmq_x = MMQ_X_Q6_K_AMPERE;
  5055. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5056. nwarps = NWARPS_Q6_K_AMPERE;
  5057. } else if (compute_capability >= MIN_CC_DP4A) {
  5058. mmq_x = MMQ_X_Q6_K_PASCAL;
  5059. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5060. nwarps = NWARPS_Q6_K_PASCAL;
  5061. } else {
  5062. GGML_ASSERT(false);
  5063. }
  5064. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5065. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5066. const dim3 block_nums(block_num_x, block_num_y, 1);
  5067. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5068. if (nrows_x % mmq_y == 0) {
  5069. const bool need_check = false;
  5070. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5071. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5072. } else {
  5073. const bool need_check = true;
  5074. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5075. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5076. }
  5077. }
  5078. static void ggml_mul_mat_p021_f16_f32_cuda(
  5079. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5080. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5081. const dim3 block_nums(1, nrows_x, nchannels_y);
  5082. const dim3 block_dims(WARP_SIZE, 1, 1);
  5083. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5084. }
  5085. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5086. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5087. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5088. const dim3 block_nums(1, nrows_x, nchannels_y);
  5089. const dim3 block_dims(WARP_SIZE, 1, 1);
  5090. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5091. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5092. }
  5093. static void ggml_cpy_f32_f32_cuda(
  5094. const char * cx, char * cdst, const int ne,
  5095. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5096. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5097. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5098. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5099. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5100. }
  5101. static void ggml_cpy_f32_f16_cuda(
  5102. const char * cx, char * cdst, const int ne,
  5103. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5104. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5105. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5106. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5107. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5108. }
  5109. static void ggml_cpy_f32_q8_0_cuda(
  5110. const char * cx, char * cdst, const int ne,
  5111. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5112. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5113. GGML_ASSERT(ne % QK8_0 == 0);
  5114. const int num_blocks = ne / QK8_0;
  5115. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5116. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5117. }
  5118. static void ggml_cpy_f32_q4_0_cuda(
  5119. const char * cx, char * cdst, const int ne,
  5120. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5121. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5122. GGML_ASSERT(ne % QK4_0 == 0);
  5123. const int num_blocks = ne / QK4_0;
  5124. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5125. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5126. }
  5127. static void ggml_cpy_f32_q4_1_cuda(
  5128. const char * cx, char * cdst, const int ne,
  5129. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5130. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5131. GGML_ASSERT(ne % QK4_1 == 0);
  5132. const int num_blocks = ne / QK4_1;
  5133. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5134. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5135. }
  5136. static void ggml_cpy_f16_f16_cuda(
  5137. const char * cx, char * cdst, const int ne,
  5138. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5139. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5140. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5141. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5142. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5143. }
  5144. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5145. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5146. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5147. }
  5148. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5149. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5150. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5151. }
  5152. template<typename T>
  5153. static void rope_cuda(
  5154. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5155. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5156. ) {
  5157. GGML_ASSERT(ncols % 2 == 0);
  5158. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5159. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5160. const dim3 block_nums(nrows, num_blocks_x, 1);
  5161. if (pos == nullptr) {
  5162. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5163. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5164. );
  5165. } else {
  5166. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5167. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5168. );
  5169. }
  5170. }
  5171. template<typename T>
  5172. static void rope_neox_cuda(
  5173. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5174. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5175. ) {
  5176. GGML_ASSERT(ncols % 2 == 0);
  5177. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5178. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5179. const dim3 block_nums(nrows, num_blocks_x, 1);
  5180. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5181. const float inv_ndims = -1.0f / n_dims;
  5182. if (pos == nullptr) {
  5183. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5184. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5185. theta_scale, inv_ndims
  5186. );
  5187. } else {
  5188. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5189. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5190. theta_scale, inv_ndims
  5191. );
  5192. }
  5193. }
  5194. static void rope_glm_f32_cuda(
  5195. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5196. float freq_base, int n_ctx, cudaStream_t stream
  5197. ) {
  5198. GGML_ASSERT(ncols % 4 == 0);
  5199. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5200. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5201. const dim3 block_nums(num_blocks_x, nrows, 1);
  5202. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5203. }
  5204. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5205. const int k_rows, const int n_heads_log2_floor, const float m0,
  5206. const float m1, cudaStream_t stream) {
  5207. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5208. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5209. const dim3 block_nums(num_blocks_x, nrows, 1);
  5210. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5211. }
  5212. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5213. const dim3 block_dims(WARP_SIZE, 1, 1);
  5214. const dim3 block_nums(1, nrows, 1);
  5215. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5216. }
  5217. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5218. // bitonic sort requires ncols to be power of 2
  5219. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5220. const dim3 block_dims(ncols, 1, 1);
  5221. const dim3 block_nums(1, nrows, 1);
  5222. if (order == GGML_SORT_ASC) {
  5223. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5224. } else if (order == GGML_SORT_DESC) {
  5225. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5226. } else {
  5227. GGML_ASSERT(false);
  5228. }
  5229. }
  5230. 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) {
  5231. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5232. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5233. const dim3 block_nums(nrows_x, block_num_x, 1);
  5234. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5235. }
  5236. 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) {
  5237. int nth = WARP_SIZE;
  5238. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5239. const dim3 block_dims(nth, 1, 1);
  5240. const dim3 block_nums(nrows_x, 1, 1);
  5241. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5242. }
  5243. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5244. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5245. int offset_delta,
  5246. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5247. const int parallel_elements = OW * KW * KH;
  5248. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5249. dim3 block_nums(num_blocks, OH, IC);
  5250. im2col_f32_f16<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, offset_delta, IW, IH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  5251. }
  5252. // buffer pool for cuda
  5253. #define MAX_CUDA_BUFFERS 256
  5254. struct scoped_spin_lock {
  5255. std::atomic_flag& lock;
  5256. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5257. while (lock.test_and_set(std::memory_order_acquire)) {
  5258. ; // spin
  5259. }
  5260. }
  5261. ~scoped_spin_lock() {
  5262. lock.clear(std::memory_order_release);
  5263. }
  5264. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5265. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5266. };
  5267. struct cuda_buffer {
  5268. void * ptr = nullptr;
  5269. size_t size = 0;
  5270. };
  5271. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5272. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5273. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  5274. scoped_spin_lock lock(g_cuda_pool_lock);
  5275. int id;
  5276. CUDA_CHECK(cudaGetDevice(&id));
  5277. #ifdef DEBUG_CUDA_MALLOC
  5278. int nnz = 0;
  5279. size_t max_size = 0, tot_size = 0;
  5280. #endif
  5281. size_t best_diff = 1ull << 36;
  5282. int ibest = -1;
  5283. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5284. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5285. if (b.ptr != nullptr) {
  5286. #ifdef DEBUG_CUDA_MALLOC
  5287. ++nnz;
  5288. tot_size += b.size;
  5289. if (b.size > max_size) max_size = b.size;
  5290. #endif
  5291. if (b.size >= size) {
  5292. size_t diff = b.size - size;
  5293. if (diff < best_diff) {
  5294. best_diff = diff;
  5295. ibest = i;
  5296. if (!best_diff) {
  5297. void * ptr = b.ptr;
  5298. *actual_size = b.size;
  5299. b.ptr = nullptr;
  5300. b.size = 0;
  5301. return ptr;
  5302. }
  5303. }
  5304. }
  5305. }
  5306. }
  5307. if (ibest >= 0) {
  5308. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  5309. void * ptr = b.ptr;
  5310. *actual_size = b.size;
  5311. b.ptr = nullptr;
  5312. b.size = 0;
  5313. return ptr;
  5314. }
  5315. #ifdef DEBUG_CUDA_MALLOC
  5316. fprintf(stderr, "%s: %d buffers, max_size = %u MB, tot_size = %u MB, requested %u MB\n", __func__, nnz,
  5317. (uint32_t)(max_size/1024/1024), (uint32_t)(tot_size/1024/1024), (uint32_t)(size/1024/1024));
  5318. #endif
  5319. void * ptr;
  5320. size_t look_ahead_size = (size_t) (1.05 * size);
  5321. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5322. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5323. *actual_size = look_ahead_size;
  5324. return ptr;
  5325. }
  5326. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  5327. scoped_spin_lock lock(g_cuda_pool_lock);
  5328. int id;
  5329. CUDA_CHECK(cudaGetDevice(&id));
  5330. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5331. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5332. if (b.ptr == nullptr) {
  5333. b.ptr = ptr;
  5334. b.size = size;
  5335. return;
  5336. }
  5337. }
  5338. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5339. CUDA_CHECK(cudaFree(ptr));
  5340. }
  5341. static bool g_cublas_loaded = false;
  5342. bool ggml_cublas_loaded(void) {
  5343. return g_cublas_loaded;
  5344. }
  5345. void ggml_init_cublas() {
  5346. static bool initialized = false;
  5347. if (!initialized) {
  5348. #ifdef __HIP_PLATFORM_AMD__
  5349. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5350. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5351. rocblas_initialize();
  5352. CUDA_CHECK(cudaDeviceSynchronize());
  5353. #endif
  5354. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5355. initialized = true;
  5356. g_cublas_loaded = false;
  5357. return;
  5358. }
  5359. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5360. int64_t total_vram = 0;
  5361. #if defined(GGML_CUDA_FORCE_MMQ)
  5362. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5363. #else
  5364. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5365. #endif
  5366. #if defined(CUDA_USE_TENSOR_CORES)
  5367. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5368. #else
  5369. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5370. #endif
  5371. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5372. for (int id = 0; id < g_device_count; ++id) {
  5373. cudaDeviceProp prop;
  5374. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5375. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  5376. g_tensor_split[id] = total_vram;
  5377. total_vram += prop.totalGlobalMem;
  5378. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5379. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5380. #else
  5381. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  5382. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5383. }
  5384. for (int id = 0; id < g_device_count; ++id) {
  5385. g_tensor_split[id] /= total_vram;
  5386. }
  5387. for (int id = 0; id < g_device_count; ++id) {
  5388. CUDA_CHECK(ggml_cuda_set_device(id));
  5389. // create cuda streams
  5390. for (int is = 0; is < MAX_STREAMS; ++is) {
  5391. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5392. }
  5393. // create cublas handle
  5394. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5395. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5396. }
  5397. // configure logging to stdout
  5398. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5399. initialized = true;
  5400. g_cublas_loaded = true;
  5401. }
  5402. }
  5403. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5404. if (tensor_split == nullptr) {
  5405. return;
  5406. }
  5407. bool all_zero = true;
  5408. for (int i = 0; i < g_device_count; ++i) {
  5409. if (tensor_split[i] != 0.0f) {
  5410. all_zero = false;
  5411. break;
  5412. }
  5413. }
  5414. if (all_zero) {
  5415. return;
  5416. }
  5417. float split_sum = 0.0f;
  5418. for (int i = 0; i < g_device_count; ++i) {
  5419. g_tensor_split[i] = split_sum;
  5420. split_sum += tensor_split[i];
  5421. }
  5422. for (int i = 0; i < g_device_count; ++i) {
  5423. g_tensor_split[i] /= split_sum;
  5424. }
  5425. }
  5426. void * ggml_cuda_host_malloc(size_t size) {
  5427. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  5428. return nullptr;
  5429. }
  5430. void * ptr = nullptr;
  5431. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  5432. if (err != cudaSuccess) {
  5433. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  5434. // This can fixed the OOM error in WSL.
  5435. cudaGetLastError();
  5436. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  5437. size/1024.0/1024.0, cudaGetErrorString(err));
  5438. return nullptr;
  5439. }
  5440. return ptr;
  5441. }
  5442. void ggml_cuda_host_free(void * ptr) {
  5443. CUDA_CHECK(cudaFreeHost(ptr));
  5444. }
  5445. static cudaError_t ggml_cuda_cpy_tensor_2d(
  5446. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  5447. cudaMemcpyKind kind;
  5448. char * src_ptr;
  5449. if (src->backend == GGML_BACKEND_CPU) {
  5450. kind = cudaMemcpyHostToDevice;
  5451. src_ptr = (char *) src->data;
  5452. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  5453. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  5454. kind = cudaMemcpyDeviceToDevice;
  5455. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  5456. int id;
  5457. CUDA_CHECK(cudaGetDevice(&id));
  5458. src_ptr = (char *) extra->data_device[id];
  5459. } else {
  5460. GGML_ASSERT(false);
  5461. }
  5462. char * dst_ptr = (char *) dst;
  5463. const int64_t ne0 = src->ne[0];
  5464. const int64_t nb0 = src->nb[0];
  5465. const int64_t nb1 = src->nb[1];
  5466. const int64_t nb2 = src->nb[2];
  5467. const int64_t nb3 = src->nb[3];
  5468. const enum ggml_type type = src->type;
  5469. const int64_t ts = ggml_type_size(type);
  5470. const int64_t bs = ggml_blck_size(type);
  5471. int64_t i1_diff = i1_high - i1_low;
  5472. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  5473. if (nb0 == ts && nb1 == ts*ne0/bs) {
  5474. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  5475. } else if (nb0 == ts) {
  5476. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  5477. } else {
  5478. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  5479. const void * rx = (const void *) ((const char *) x + i1*nb1);
  5480. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  5481. // pretend the row is a matrix with cols=1
  5482. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  5483. if (r != cudaSuccess) return r;
  5484. }
  5485. return cudaSuccess;
  5486. }
  5487. }
  5488. static void ggml_cuda_op_get_rows(
  5489. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5490. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & stream) {
  5491. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5492. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5493. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  5494. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  5495. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  5496. const int32_t * src1_i32 = (const int32_t *) src1_d;
  5497. switch (src0->type) {
  5498. case GGML_TYPE_F16:
  5499. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  5500. break;
  5501. case GGML_TYPE_F32:
  5502. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5503. break;
  5504. case GGML_TYPE_Q4_0:
  5505. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5506. break;
  5507. case GGML_TYPE_Q4_1:
  5508. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5509. break;
  5510. case GGML_TYPE_Q5_0:
  5511. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5512. break;
  5513. case GGML_TYPE_Q5_1:
  5514. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5515. break;
  5516. case GGML_TYPE_Q8_0:
  5517. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5518. break;
  5519. default:
  5520. // TODO: k-quants
  5521. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  5522. GGML_ASSERT(false);
  5523. break;
  5524. }
  5525. }
  5526. template<class op>
  5527. inline void ggml_cuda_op_bin_bcast(
  5528. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5529. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5530. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5531. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5532. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5533. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5534. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  5535. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5536. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  5537. } else {
  5538. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  5539. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  5540. GGML_ASSERT(false);
  5541. }
  5542. }
  5543. static void ggml_cuda_op_repeat(
  5544. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5545. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & main_stream) {
  5546. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  5547. (void) src1;
  5548. (void) src1_d;
  5549. }
  5550. inline void ggml_cuda_op_add(
  5551. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5552. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5553. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5554. }
  5555. inline void ggml_cuda_op_acc(
  5556. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5557. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5558. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5559. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5560. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5561. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  5562. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5563. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5564. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5565. int offset = dst->op_params[3] / 4; // offset in bytes
  5566. acc_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  5567. (void) dst;
  5568. }
  5569. inline void ggml_cuda_op_mul(
  5570. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5571. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5572. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5573. }
  5574. inline void ggml_cuda_op_div(
  5575. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5576. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5577. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5578. }
  5579. inline void ggml_cuda_op_gelu(
  5580. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5581. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5582. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5583. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5584. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5585. (void) src1;
  5586. (void) dst;
  5587. (void) src1_dd;
  5588. }
  5589. inline void ggml_cuda_op_silu(
  5590. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5591. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5592. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5593. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5594. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5595. (void) src1;
  5596. (void) dst;
  5597. (void) src1_dd;
  5598. }
  5599. inline void ggml_cuda_op_gelu_quick(
  5600. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5601. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5602. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5603. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5604. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5605. (void) src1;
  5606. (void) dst;
  5607. (void) src1_dd;
  5608. }
  5609. inline void ggml_cuda_op_tanh(
  5610. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5611. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5612. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5613. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5614. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5615. (void) src1;
  5616. (void) dst;
  5617. (void) src1_dd;
  5618. }
  5619. inline void ggml_cuda_op_relu(
  5620. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5621. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5622. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5623. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5624. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5625. (void) src1;
  5626. (void) dst;
  5627. (void) src1_dd;
  5628. }
  5629. inline void ggml_cuda_op_leaky_relu(
  5630. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5631. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5632. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5633. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5634. float negative_slope;
  5635. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5636. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  5637. (void) src1;
  5638. (void) dst;
  5639. (void) src1_dd;
  5640. }
  5641. inline void ggml_cuda_op_sqr(
  5642. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5643. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5644. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5645. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5646. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5647. (void) src1;
  5648. (void) dst;
  5649. (void) src1_dd;
  5650. }
  5651. inline void ggml_cuda_op_norm(
  5652. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5653. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5654. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5655. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5656. const int64_t ne00 = src0->ne[0];
  5657. const int64_t nrows = ggml_nrows(src0);
  5658. float eps;
  5659. memcpy(&eps, dst->op_params, sizeof(float));
  5660. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5661. (void) src1;
  5662. (void) dst;
  5663. (void) src1_dd;
  5664. }
  5665. inline void ggml_cuda_op_group_norm(
  5666. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5667. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5668. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5669. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5670. int num_groups = dst->op_params[0];
  5671. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5672. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  5673. (void) src1;
  5674. (void) dst;
  5675. (void) src1_dd;
  5676. }
  5677. inline void ggml_cuda_op_concat(
  5678. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5679. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5680. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5681. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5682. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5683. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  5684. concat_f32_cuda(src0_dd + i3 * (src0->nb[3] / 4), src1_dd + i3 * (src1->nb[3] / 4), dst_dd + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], main_stream);
  5685. }
  5686. (void) src1;
  5687. (void) dst;
  5688. }
  5689. inline void ggml_cuda_op_upscale(
  5690. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5691. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5692. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5693. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5694. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5695. const int scale_factor = dst->op_params[0];
  5696. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  5697. (void) src1;
  5698. (void) dst;
  5699. (void) src1_dd;
  5700. }
  5701. inline void ggml_cuda_op_pad(
  5702. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5703. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5704. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5705. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5706. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5707. pad_f32_cuda(src0_dd, dst_dd,
  5708. src0->ne[0], src0->ne[1], src0->ne[2],
  5709. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  5710. (void) src1;
  5711. (void) dst;
  5712. (void) src1_dd;
  5713. }
  5714. inline void ggml_cuda_op_rms_norm(
  5715. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5716. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5717. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5718. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5719. const int64_t ne00 = src0->ne[0];
  5720. const int64_t nrows = ggml_nrows(src0);
  5721. float eps;
  5722. memcpy(&eps, dst->op_params, sizeof(float));
  5723. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5724. (void) src1;
  5725. (void) dst;
  5726. (void) src1_dd;
  5727. }
  5728. inline void ggml_cuda_op_mul_mat_q(
  5729. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5730. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5731. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5732. const int64_t ne00 = src0->ne[0];
  5733. const int64_t ne10 = src1->ne[0];
  5734. GGML_ASSERT(ne10 % QK8_1 == 0);
  5735. const int64_t ne0 = dst->ne[0];
  5736. const int64_t row_diff = row_high - row_low;
  5737. int id;
  5738. CUDA_CHECK(cudaGetDevice(&id));
  5739. // the main device has a larger memory buffer to hold the results from all GPUs
  5740. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5741. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5742. switch (src0->type) {
  5743. case GGML_TYPE_Q4_0:
  5744. 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);
  5745. break;
  5746. case GGML_TYPE_Q4_1:
  5747. 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);
  5748. break;
  5749. case GGML_TYPE_Q5_0:
  5750. 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);
  5751. break;
  5752. case GGML_TYPE_Q5_1:
  5753. 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);
  5754. break;
  5755. case GGML_TYPE_Q8_0:
  5756. 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);
  5757. break;
  5758. case GGML_TYPE_Q2_K:
  5759. 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);
  5760. break;
  5761. case GGML_TYPE_Q3_K:
  5762. 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);
  5763. break;
  5764. case GGML_TYPE_Q4_K:
  5765. 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);
  5766. break;
  5767. case GGML_TYPE_Q5_K:
  5768. 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);
  5769. break;
  5770. case GGML_TYPE_Q6_K:
  5771. 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);
  5772. break;
  5773. default:
  5774. GGML_ASSERT(false);
  5775. break;
  5776. }
  5777. (void) src1;
  5778. (void) dst;
  5779. (void) src1_ddf_i;
  5780. }
  5781. static int64_t get_row_rounding(ggml_type type) {
  5782. int64_t min_compute_capability = INT_MAX;
  5783. int64_t max_compute_capability = INT_MIN;
  5784. for (int64_t id = 0; id < g_device_count; ++id) {
  5785. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5786. if (min_compute_capability > g_compute_capabilities[id]) {
  5787. min_compute_capability = g_compute_capabilities[id];
  5788. }
  5789. if (max_compute_capability < g_compute_capabilities[id]) {
  5790. max_compute_capability = g_compute_capabilities[id];
  5791. }
  5792. }
  5793. }
  5794. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5795. switch(type) {
  5796. case GGML_TYPE_Q4_0:
  5797. case GGML_TYPE_Q4_1:
  5798. case GGML_TYPE_Q5_0:
  5799. case GGML_TYPE_Q5_1:
  5800. case GGML_TYPE_Q8_0:
  5801. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5802. case GGML_TYPE_F16:
  5803. case GGML_TYPE_F32:
  5804. return 1;
  5805. case GGML_TYPE_Q2_K:
  5806. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5807. case GGML_TYPE_Q3_K:
  5808. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5809. case GGML_TYPE_Q4_K:
  5810. case GGML_TYPE_Q5_K:
  5811. case GGML_TYPE_Q6_K:
  5812. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5813. default:
  5814. GGML_ASSERT(false);
  5815. }
  5816. #else
  5817. switch(type) {
  5818. case GGML_TYPE_Q4_0:
  5819. case GGML_TYPE_Q4_1:
  5820. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5821. case GGML_TYPE_Q5_0:
  5822. case GGML_TYPE_Q5_1:
  5823. case GGML_TYPE_Q8_0:
  5824. return 64;
  5825. case GGML_TYPE_F16:
  5826. case GGML_TYPE_F32:
  5827. return 1;
  5828. case GGML_TYPE_Q2_K:
  5829. case GGML_TYPE_Q3_K:
  5830. case GGML_TYPE_Q4_K:
  5831. case GGML_TYPE_Q5_K:
  5832. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5833. case GGML_TYPE_Q6_K:
  5834. return 64;
  5835. default:
  5836. GGML_ASSERT(false);
  5837. }
  5838. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5839. }
  5840. inline void ggml_cuda_op_mul_mat_vec_q(
  5841. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5842. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5843. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5844. GGML_ASSERT(ggml_nrows(src1) == 1);
  5845. const int64_t ne00 = src0->ne[0];
  5846. const int64_t row_diff = row_high - row_low;
  5847. switch (src0->type) {
  5848. case GGML_TYPE_Q4_0:
  5849. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5850. break;
  5851. case GGML_TYPE_Q4_1:
  5852. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5853. break;
  5854. case GGML_TYPE_Q5_0:
  5855. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5856. break;
  5857. case GGML_TYPE_Q5_1:
  5858. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5859. break;
  5860. case GGML_TYPE_Q8_0:
  5861. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5862. break;
  5863. case GGML_TYPE_Q2_K:
  5864. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5865. break;
  5866. case GGML_TYPE_Q3_K:
  5867. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5868. break;
  5869. case GGML_TYPE_Q4_K:
  5870. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5871. break;
  5872. case GGML_TYPE_Q5_K:
  5873. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5874. break;
  5875. case GGML_TYPE_Q6_K:
  5876. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5877. break;
  5878. default:
  5879. GGML_ASSERT(false);
  5880. break;
  5881. }
  5882. (void) src1;
  5883. (void) dst;
  5884. (void) src1_ddf_i;
  5885. (void) src1_ncols;
  5886. (void) src1_padded_row_size;
  5887. }
  5888. inline void ggml_cuda_op_dequantize_mul_mat_vec(
  5889. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5890. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5891. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5892. const int64_t ne00 = src0->ne[0];
  5893. const int64_t row_diff = row_high - row_low;
  5894. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  5895. #ifdef GGML_CUDA_F16
  5896. size_t ash;
  5897. dfloat * src1_dfloat = nullptr; // dfloat == half
  5898. bool src1_convert_f16 =
  5899. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  5900. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  5901. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  5902. if (src1_convert_f16) {
  5903. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  5904. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  5905. ne00, 1, sizeof(float), 0, 0,
  5906. ne00, 1, sizeof(half), 0, 0, stream);
  5907. }
  5908. #else
  5909. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  5910. #endif // GGML_CUDA_F16
  5911. switch (src0->type) {
  5912. case GGML_TYPE_Q4_0:
  5913. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5914. break;
  5915. case GGML_TYPE_Q4_1:
  5916. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5917. break;
  5918. case GGML_TYPE_Q5_0:
  5919. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5920. break;
  5921. case GGML_TYPE_Q5_1:
  5922. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5923. break;
  5924. case GGML_TYPE_Q8_0:
  5925. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5926. break;
  5927. case GGML_TYPE_Q2_K:
  5928. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5929. break;
  5930. case GGML_TYPE_Q3_K:
  5931. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5932. break;
  5933. case GGML_TYPE_Q4_K:
  5934. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5935. break;
  5936. case GGML_TYPE_Q5_K:
  5937. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5938. break;
  5939. case GGML_TYPE_Q6_K:
  5940. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5941. break;
  5942. case GGML_TYPE_F16:
  5943. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5944. break;
  5945. default:
  5946. GGML_ASSERT(false);
  5947. break;
  5948. }
  5949. #ifdef GGML_CUDA_F16
  5950. if (src1_convert_f16) {
  5951. ggml_cuda_pool_free(src1_dfloat, ash);
  5952. }
  5953. #endif // GGML_CUDA_F16
  5954. (void) src1;
  5955. (void) dst;
  5956. (void) src1_ddq_i;
  5957. (void) src1_ncols;
  5958. (void) src1_padded_row_size;
  5959. }
  5960. inline void ggml_cuda_op_mul_mat_cublas(
  5961. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5962. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5963. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5964. GGML_ASSERT(src0_dd_i != nullptr);
  5965. GGML_ASSERT(src1_ddf_i != nullptr);
  5966. GGML_ASSERT(dst_dd_i != nullptr);
  5967. const int64_t ne00 = src0->ne[0];
  5968. const int64_t ne10 = src1->ne[0];
  5969. const int64_t ne0 = dst->ne[0];
  5970. const int64_t row_diff = row_high - row_low;
  5971. int id;
  5972. CUDA_CHECK(cudaGetDevice(&id));
  5973. // the main device has a larger memory buffer to hold the results from all GPUs
  5974. // ldc == nrows of the matrix that cuBLAS writes into
  5975. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5976. const int compute_capability = g_compute_capabilities[id];
  5977. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  5978. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  5979. half * src0_as_f16 = nullptr;
  5980. size_t src0_as = 0;
  5981. if (src0->type != GGML_TYPE_F16) {
  5982. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  5983. GGML_ASSERT(to_fp16_cuda != nullptr);
  5984. size_t ne = row_diff*ne00;
  5985. src0_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src0_as);
  5986. to_fp16_cuda(src0_dd_i, src0_as_f16, ne, stream);
  5987. }
  5988. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16;
  5989. half * src1_as_f16 = nullptr;
  5990. size_t src1_as = 0;
  5991. if (src1->type != GGML_TYPE_F16) {
  5992. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  5993. GGML_ASSERT(to_fp16_cuda != nullptr);
  5994. size_t ne = src1_ncols*ne10;
  5995. src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src1_as);
  5996. to_fp16_cuda(src1_ddf_i, src1_as_f16, ne, stream);
  5997. }
  5998. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16;
  5999. size_t dst_as = 0;
  6000. half * dst_f16 = (half *) ggml_cuda_pool_malloc(row_diff*src1_ncols * sizeof(half), &dst_as);
  6001. const half alpha_f16 = 1.0f;
  6002. const half beta_f16 = 0.0f;
  6003. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6004. CUBLAS_CHECK(
  6005. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6006. row_diff, src1_ncols, ne10,
  6007. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6008. src1_ptr, CUDA_R_16F, ne10,
  6009. &beta_f16, dst_f16, CUDA_R_16F, ldc,
  6010. CUBLAS_COMPUTE_16F,
  6011. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6012. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6013. to_fp32_cuda(dst_f16, dst_dd_i, row_diff*src1_ncols, stream);
  6014. ggml_cuda_pool_free(dst_f16, dst_as);
  6015. if (src0_as != 0) {
  6016. ggml_cuda_pool_free(src0_as_f16, src0_as);
  6017. }
  6018. if (src1_as != 0) {
  6019. ggml_cuda_pool_free(src1_as_f16, src1_as);
  6020. }
  6021. }
  6022. else {
  6023. float * src0_ddq_as_f32 = nullptr;
  6024. size_t src0_as = 0;
  6025. if (src0->type != GGML_TYPE_F32) {
  6026. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6027. GGML_ASSERT(to_fp32_cuda != nullptr);
  6028. src0_ddq_as_f32 = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_as); // NOLINT
  6029. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32, row_diff*ne00, stream);
  6030. }
  6031. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32;
  6032. const float alpha = 1.0f;
  6033. const float beta = 0.0f;
  6034. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6035. CUBLAS_CHECK(
  6036. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6037. row_diff, src1_ncols, ne10,
  6038. &alpha, src0_ddf_i, ne00,
  6039. src1_ddf_i, ne10,
  6040. &beta, dst_dd_i, ldc));
  6041. if (src0_as != 0) {
  6042. ggml_cuda_pool_free(src0_ddq_as_f32, src0_as);
  6043. }
  6044. }
  6045. (void) dst;
  6046. (void) src1_ddq_i;
  6047. (void) src1_padded_row_size;
  6048. }
  6049. inline void ggml_cuda_op_rope(
  6050. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6051. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6052. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6053. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6054. GGML_ASSERT(src0->type == dst->type);
  6055. const int64_t ne00 = src0->ne[0];
  6056. const int64_t ne01 = src0->ne[1];
  6057. const int64_t ne2 = dst->ne[2];
  6058. const int64_t nrows = ggml_nrows(src0);
  6059. //const int n_past = ((int32_t *) dst->op_params)[0];
  6060. const int n_dims = ((int32_t *) dst->op_params)[1];
  6061. const int mode = ((int32_t *) dst->op_params)[2];
  6062. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6063. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6064. // RoPE alteration for extended context
  6065. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6066. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6067. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6068. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6069. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6070. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6071. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6072. const int32_t * pos = nullptr;
  6073. if ((mode & 1) == 0) {
  6074. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6075. GGML_ASSERT(src1->ne[0] == ne2);
  6076. pos = (const int32_t *) src1_dd;
  6077. }
  6078. const bool is_neox = mode & 2;
  6079. const bool is_glm = mode & 4;
  6080. rope_corr_dims corr_dims;
  6081. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6082. // compute
  6083. if (is_glm) {
  6084. GGML_ASSERT(false);
  6085. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6086. } else if (is_neox) {
  6087. if (src0->type == GGML_TYPE_F32) {
  6088. rope_neox_cuda(
  6089. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6090. attn_factor, corr_dims, main_stream
  6091. );
  6092. } else if (src0->type == GGML_TYPE_F16) {
  6093. rope_neox_cuda(
  6094. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6095. attn_factor, corr_dims, main_stream
  6096. );
  6097. } else {
  6098. GGML_ASSERT(false);
  6099. }
  6100. } else {
  6101. if (src0->type == GGML_TYPE_F32) {
  6102. rope_cuda(
  6103. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6104. attn_factor, corr_dims, main_stream
  6105. );
  6106. } else if (src0->type == GGML_TYPE_F16) {
  6107. rope_cuda(
  6108. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6109. attn_factor, corr_dims, main_stream
  6110. );
  6111. } else {
  6112. GGML_ASSERT(false);
  6113. }
  6114. }
  6115. (void) src1;
  6116. (void) dst;
  6117. (void) src1_dd;
  6118. }
  6119. inline void ggml_cuda_op_alibi(
  6120. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6121. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6122. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6123. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6124. const int64_t ne00 = src0->ne[0];
  6125. const int64_t ne01 = src0->ne[1];
  6126. const int64_t ne02 = src0->ne[2];
  6127. const int64_t nrows = ggml_nrows(src0);
  6128. //const int n_past = ((int32_t *) dst->op_params)[0];
  6129. const int n_head = ((int32_t *) dst->op_params)[1];
  6130. float max_bias;
  6131. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6132. //GGML_ASSERT(ne01 + n_past == ne00);
  6133. GGML_ASSERT(n_head == ne02);
  6134. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6135. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6136. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6137. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6138. (void) src1;
  6139. (void) src1_dd;
  6140. }
  6141. inline void ggml_cuda_op_im2col(
  6142. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6143. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6144. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6145. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6146. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6147. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6148. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6149. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6150. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6151. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6152. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6153. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6154. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6155. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6156. const int64_t IW = src1->ne[0];
  6157. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6158. const int64_t KW = src0->ne[0];
  6159. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6160. const int64_t OW = dst->ne[1];
  6161. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6162. im2col_f32_f16_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  6163. (void) src0;
  6164. (void) src0_dd;
  6165. }
  6166. inline void ggml_cuda_op_sum_rows(
  6167. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6168. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6169. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6170. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6171. const int64_t ncols = src0->ne[0];
  6172. const int64_t nrows = ggml_nrows(src0);
  6173. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6174. (void) src1;
  6175. (void) dst;
  6176. (void) src1_dd;
  6177. }
  6178. inline void ggml_cuda_op_argsort(
  6179. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6180. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6181. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6182. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6183. const int64_t ncols = src0->ne[0];
  6184. const int64_t nrows = ggml_nrows(src0);
  6185. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6186. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6187. (void) src1;
  6188. (void) dst;
  6189. (void) src1_dd;
  6190. }
  6191. inline void ggml_cuda_op_diag_mask_inf(
  6192. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6193. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6194. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6195. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6196. const int64_t ne00 = src0->ne[0];
  6197. const int64_t ne01 = src0->ne[1];
  6198. const int nrows0 = ggml_nrows(src0);
  6199. const int n_past = ((int32_t *) dst->op_params)[0];
  6200. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6201. (void) src1;
  6202. (void) dst;
  6203. (void) src1_dd;
  6204. }
  6205. inline void ggml_cuda_op_soft_max(
  6206. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6207. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6208. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6209. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6210. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6211. const int64_t ne00 = src0->ne[0];
  6212. const int64_t nrows_x = ggml_nrows(src0);
  6213. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6214. float scale = 1.0f;
  6215. memcpy(&scale, dst->op_params, sizeof(float));
  6216. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6217. (void) dst;
  6218. }
  6219. inline void ggml_cuda_op_scale(
  6220. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6221. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6222. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6223. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6224. float scale;
  6225. memcpy(&scale, dst->op_params, sizeof(float));
  6226. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6227. CUDA_CHECK(cudaGetLastError());
  6228. (void) src1;
  6229. (void) dst;
  6230. (void) src1_dd;
  6231. }
  6232. inline void ggml_cuda_op_clamp(
  6233. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6234. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6235. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6236. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6237. float min;
  6238. float max;
  6239. memcpy(&min, dst->op_params, sizeof(float));
  6240. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6241. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6242. CUDA_CHECK(cudaGetLastError());
  6243. (void) src1;
  6244. (void) dst;
  6245. (void) src1_dd;
  6246. }
  6247. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6248. const int64_t nrows0 = ggml_nrows(src0);
  6249. const bool use_src1 = src1 != nullptr;
  6250. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6251. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6252. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6253. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6254. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6255. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6256. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6257. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6258. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6259. // dd = data device
  6260. float * src0_ddf = nullptr;
  6261. float * src1_ddf = nullptr;
  6262. float * dst_ddf = nullptr;
  6263. // as = actual size
  6264. size_t src0_asf = 0;
  6265. size_t src1_asf = 0;
  6266. size_t dst_asf = 0;
  6267. ggml_cuda_set_device(g_main_device);
  6268. const cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6269. if (src0_on_device) {
  6270. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6271. } else {
  6272. src0_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_asf);
  6273. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6274. }
  6275. if (use_src1) {
  6276. if (src1_on_device) {
  6277. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6278. } else {
  6279. src1_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf);
  6280. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6281. }
  6282. }
  6283. if (dst_on_device) {
  6284. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6285. } else {
  6286. dst_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(dst), &dst_asf);
  6287. }
  6288. // do the computation
  6289. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6290. CUDA_CHECK(cudaGetLastError());
  6291. // copy dst to host if necessary
  6292. if (!dst_on_device) {
  6293. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6294. }
  6295. if (src0_asf > 0) {
  6296. ggml_cuda_pool_free(src0_ddf, src0_asf);
  6297. }
  6298. if (src1_asf > 0) {
  6299. ggml_cuda_pool_free(src1_ddf, src1_asf);
  6300. }
  6301. if (dst_asf > 0) {
  6302. ggml_cuda_pool_free(dst_ddf, dst_asf);
  6303. }
  6304. if (dst->backend == GGML_BACKEND_CPU) {
  6305. CUDA_CHECK(cudaDeviceSynchronize());
  6306. }
  6307. }
  6308. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6309. static bool peer_access_enabled = false;
  6310. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6311. if (peer_access_enabled == enable_peer_access) {
  6312. return;
  6313. }
  6314. #ifdef NDEBUG
  6315. for (int id = 0; id < g_device_count; ++id) {
  6316. CUDA_CHECK(ggml_cuda_set_device(id));
  6317. CUDA_CHECK(cudaDeviceSynchronize());
  6318. }
  6319. for (int id = 0; id < g_device_count; ++id) {
  6320. CUDA_CHECK(ggml_cuda_set_device(id));
  6321. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6322. if (id == id_other) {
  6323. continue;
  6324. }
  6325. if (id != g_main_device && id_other != g_main_device) {
  6326. continue;
  6327. }
  6328. int can_access_peer;
  6329. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6330. if (can_access_peer) {
  6331. if (enable_peer_access) {
  6332. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6333. } else {
  6334. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6335. }
  6336. }
  6337. }
  6338. }
  6339. #endif // NDEBUG
  6340. peer_access_enabled = enable_peer_access;
  6341. }
  6342. static void ggml_cuda_op_mul_mat(
  6343. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6344. const bool convert_src1_to_q8_1) {
  6345. const int64_t ne00 = src0->ne[0];
  6346. const int64_t ne01 = src0->ne[1];
  6347. const int64_t ne02 = src0->ne[2];
  6348. const int64_t ne03 = src0->ne[3];
  6349. const int64_t nrows0 = ggml_nrows(src0);
  6350. const int64_t ne10 = src1->ne[0];
  6351. const int64_t ne11 = src1->ne[1];
  6352. const int64_t ne12 = src1->ne[2];
  6353. const int64_t ne13 = src1->ne[3];
  6354. const int64_t nrows1 = ggml_nrows(src1);
  6355. GGML_ASSERT(ne03 == ne13);
  6356. const int64_t ne0 = dst->ne[0];
  6357. const int64_t ne1 = dst->ne[1];
  6358. const int nb2 = dst->nb[2];
  6359. const int nb3 = dst->nb[3];
  6360. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6361. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6362. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6363. const int64_t i02_divisor = ne12 / ne02;
  6364. const size_t src0_ts = ggml_type_size(src0->type);
  6365. const size_t src0_bs = ggml_blck_size(src0->type);
  6366. const size_t q8_1_ts = sizeof(block_q8_1);
  6367. const size_t q8_1_bs = QK8_1;
  6368. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6369. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6370. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6371. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6372. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6373. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6374. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6375. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6376. GGML_ASSERT(!(split && ne02 > 1));
  6377. GGML_ASSERT(!(split && ne03 > 1));
  6378. GGML_ASSERT(!(split && ne02 < ne12));
  6379. // dd = data device
  6380. char * src0_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6381. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  6382. char * src1_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // q8_1
  6383. float * dst_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6384. // as = actual size
  6385. size_t src0_as[GGML_CUDA_MAX_DEVICES] = {0};
  6386. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  6387. size_t src1_asq[GGML_CUDA_MAX_DEVICES] = {0};
  6388. size_t dst_as[GGML_CUDA_MAX_DEVICES] = {0};
  6389. int64_t row_low[GGML_CUDA_MAX_DEVICES];
  6390. int64_t row_high[GGML_CUDA_MAX_DEVICES];
  6391. int used_devices = 0;
  6392. for (int64_t id = 0; id < g_device_count; ++id) {
  6393. // by default, use all rows
  6394. row_low[id] = 0;
  6395. row_high[id] = ne01;
  6396. // for multi GPU, get the row boundaries from tensor split
  6397. // and round to mul_mat_q tile sizes
  6398. if (split) {
  6399. const int64_t rounding = get_row_rounding(src0->type);
  6400. if (id != 0) {
  6401. row_low[id] = ne01*g_tensor_split[id];
  6402. if (row_low[id] < ne01) {
  6403. row_low[id] -= row_low[id] % rounding;
  6404. }
  6405. }
  6406. if (id != g_device_count - 1) {
  6407. row_high[id] = ne01*g_tensor_split[id + 1];
  6408. if (row_high[id] < ne01) {
  6409. row_high[id] -= row_high[id] % rounding;
  6410. }
  6411. }
  6412. }
  6413. }
  6414. for (int64_t id = 0; id < g_device_count; ++id) {
  6415. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6416. continue;
  6417. }
  6418. used_devices++;
  6419. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6420. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6421. ggml_cuda_set_device(id);
  6422. const cudaStream_t stream = g_cudaStreams[id][0];
  6423. if (src0_on_device && src0_is_contiguous) {
  6424. src0_dd[id] = (char *) src0_extra->data_device[id];
  6425. } else {
  6426. // const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  6427. src0_dd[id] = (char *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  6428. }
  6429. if (src1_on_device && src1_is_contiguous) {
  6430. src1_ddf[id] = (float *) src1_extra->data_device[id];
  6431. } else {
  6432. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  6433. }
  6434. if (convert_src1_to_q8_1) {
  6435. src1_ddq[id] = (char *) ggml_cuda_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  6436. if (src1_on_device && src1_is_contiguous) {
  6437. quantize_row_q8_1_cuda(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  6438. CUDA_CHECK(cudaGetLastError());
  6439. }
  6440. }
  6441. if (dst_on_device) {
  6442. dst_dd[id] = (float *) dst_extra->data_device[id];
  6443. } else {
  6444. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  6445. dst_dd[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_as[id]);
  6446. }
  6447. }
  6448. // if multiple devices are used they need to wait for the main device
  6449. // here an event is recorded that signals that the main device has finished calculating the input data
  6450. if (split && used_devices > 1) {
  6451. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6452. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  6453. }
  6454. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  6455. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  6456. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  6457. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  6458. for (int64_t id = 0; id < g_device_count; ++id) {
  6459. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6460. continue;
  6461. }
  6462. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6463. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6464. const int64_t row_diff = row_high[id] - row_low[id];
  6465. ggml_cuda_set_device(id);
  6466. const cudaStream_t stream = g_cudaStreams[id][is];
  6467. // wait for main GPU data if necessary
  6468. if (split && (id != g_main_device || is != 0)) {
  6469. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  6470. }
  6471. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  6472. const int64_t i03 = i0 / ne12;
  6473. const int64_t i02 = i0 % ne12;
  6474. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  6475. // for split tensors the data begins at i0 == i0_offset_low
  6476. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  6477. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  6478. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  6479. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  6480. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  6481. // in that case an offset on dst_ddf_i is needed
  6482. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  6483. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  6484. }
  6485. // copy src0, src1 to device if necessary
  6486. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  6487. if (id != g_main_device) {
  6488. if (convert_src1_to_q8_1) {
  6489. char * src1_ddq_i_source = src1_ddq[g_main_device] + src1_ddq_i_offset;
  6490. CUDA_CHECK(cudaMemcpyAsync(src1_ddq_i, src1_ddq_i_source, src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs,
  6491. cudaMemcpyDeviceToDevice, stream));
  6492. } else {
  6493. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  6494. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  6495. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_ncols*ne10*sizeof(float),
  6496. cudaMemcpyDeviceToDevice, stream));
  6497. }
  6498. }
  6499. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  6500. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  6501. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  6502. } else {
  6503. GGML_ASSERT(false);
  6504. }
  6505. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  6506. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  6507. CUDA_CHECK(cudaGetLastError());
  6508. }
  6509. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  6510. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  6511. }
  6512. // do the computation
  6513. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  6514. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  6515. CUDA_CHECK(cudaGetLastError());
  6516. // copy dst to host or other device if necessary
  6517. if (!dst_on_device) {
  6518. void * dst_off_device;
  6519. cudaMemcpyKind kind;
  6520. if (dst->backend == GGML_BACKEND_CPU) {
  6521. dst_off_device = dst->data;
  6522. kind = cudaMemcpyDeviceToHost;
  6523. } else if (dst->backend == GGML_BACKEND_GPU) {
  6524. dst_off_device = dst_extra->data_device[g_main_device];
  6525. kind = cudaMemcpyDeviceToDevice;
  6526. } else {
  6527. GGML_ASSERT(false);
  6528. }
  6529. if (split) {
  6530. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  6531. // dst is NOT transposed.
  6532. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  6533. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  6534. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  6535. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6536. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6537. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  6538. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_dd_i, row_diff*sizeof(float),
  6539. row_diff*sizeof(float), src1_ncols, kind, stream));
  6540. } else {
  6541. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6542. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6543. dhf_dst_i += src1_col_0*ne0;
  6544. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  6545. }
  6546. }
  6547. // add event for the main device to wait on until other device is done
  6548. if (split && (id != g_main_device || is != 0)) {
  6549. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  6550. }
  6551. }
  6552. }
  6553. }
  6554. for (int64_t id = 0; id < g_device_count; ++id) {
  6555. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6556. continue;
  6557. }
  6558. CUDA_CHECK(ggml_cuda_set_device(id));
  6559. // free buffers again when done
  6560. if (src0_as[id] > 0) {
  6561. ggml_cuda_pool_free(src0_dd[id], src0_as[id]);
  6562. }
  6563. if (src1_asf[id] > 0) {
  6564. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  6565. }
  6566. if (src1_asq[id] > 0) {
  6567. ggml_cuda_pool_free(src1_ddq[id], src1_asq[id]);
  6568. }
  6569. if (dst_as[id] > 0) {
  6570. ggml_cuda_pool_free(dst_dd[id], dst_as[id]);
  6571. }
  6572. }
  6573. // main device waits for all other devices to be finished
  6574. if (split && g_device_count > 1) {
  6575. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  6576. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  6577. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6578. for (int64_t id = 0; id < g_device_count; ++id) {
  6579. if (row_low[id] == row_high[id]) {
  6580. continue;
  6581. }
  6582. for (int64_t is = 0; is < is_max; ++is) {
  6583. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  6584. }
  6585. }
  6586. }
  6587. if (dst->backend == GGML_BACKEND_CPU) {
  6588. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6589. CUDA_CHECK(cudaDeviceSynchronize());
  6590. }
  6591. }
  6592. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6593. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  6594. }
  6595. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6596. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  6597. }
  6598. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6599. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  6600. }
  6601. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6602. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  6603. }
  6604. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6605. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  6606. }
  6607. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6608. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  6609. }
  6610. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6611. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  6612. }
  6613. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6614. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  6615. }
  6616. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6617. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  6618. }
  6619. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6620. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  6621. }
  6622. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6623. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  6624. }
  6625. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6626. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  6627. }
  6628. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6629. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  6630. }
  6631. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6632. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  6633. }
  6634. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6635. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  6636. }
  6637. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6638. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  6639. }
  6640. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6641. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  6642. }
  6643. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6644. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  6645. }
  6646. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6647. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  6648. }
  6649. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  6650. if (!g_cublas_loaded) return false;
  6651. const int64_t ne10 = src1->ne[0];
  6652. const int64_t ne0 = dst->ne[0];
  6653. const int64_t ne1 = dst->ne[1];
  6654. // TODO: find the optimal values for these
  6655. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  6656. src1->type == GGML_TYPE_F32 &&
  6657. dst->type == GGML_TYPE_F32 &&
  6658. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  6659. }
  6660. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6661. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  6662. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6663. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  6664. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  6665. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6666. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6667. const int64_t ne00 = src0->ne[0];
  6668. const int64_t ne01 = src0->ne[1];
  6669. const int64_t ne02 = src0->ne[2];
  6670. const int64_t ne12 = src1->ne[2];
  6671. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6672. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6673. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6674. void * src0_ddq = src0_extra->data_device[g_main_device];
  6675. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6676. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6677. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6678. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6679. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  6680. }
  6681. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6682. GGML_ASSERT(!ggml_is_transposed(src0));
  6683. GGML_ASSERT(!ggml_is_transposed(src1));
  6684. GGML_ASSERT(!ggml_is_permuted(src0));
  6685. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6686. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6687. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6688. const int64_t ne00 = src0->ne[0];
  6689. const int64_t ne01 = src0->ne[1];
  6690. const int64_t ne02 = src0->ne[2];
  6691. const int64_t nb01 = src0->nb[1];
  6692. const int64_t nb02 = src0->nb[2];
  6693. const int64_t ne12 = src1->ne[2];
  6694. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6695. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6696. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6697. void * src0_ddq = src0_extra->data_device[g_main_device];
  6698. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6699. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6700. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6701. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6702. const int64_t row_stride_x = nb01 / sizeof(half);
  6703. const int64_t channel_stride_x = nb02 / sizeof(half);
  6704. 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);
  6705. }
  6706. static __global__ void k_compute_batched_ptrs(
  6707. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  6708. const void ** ptrs_src, void ** ptrs_dst,
  6709. int64_t ne12, int64_t ne13,
  6710. int64_t ne23,
  6711. size_t nb02, size_t nb03,
  6712. size_t nb12, size_t nb13,
  6713. size_t nbd2, size_t nbd3,
  6714. int64_t r2, int64_t r3) {
  6715. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6716. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6717. if (i13 >= ne13 || i12 >= ne12) {
  6718. return;
  6719. }
  6720. int64_t i03 = i13 / r3;
  6721. int64_t i02 = i12 / r2;
  6722. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  6723. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  6724. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  6725. }
  6726. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6727. GGML_ASSERT(!ggml_is_transposed(src0));
  6728. GGML_ASSERT(!ggml_is_transposed(src1));
  6729. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6730. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6731. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6732. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  6733. const int64_t ne01 = src0->ne[1];
  6734. const int64_t ne02 = src0->ne[2];
  6735. const int64_t ne03 = src0->ne[3];
  6736. const int64_t nb01 = src0->nb[1];
  6737. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  6738. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  6739. const int64_t ne10 = src1->ne[0];
  6740. const int64_t ne11 = src1->ne[1];
  6741. const int64_t ne12 = src1->ne[2];
  6742. const int64_t ne13 = src1->ne[3];
  6743. const int64_t nb11 = src1->nb[1];
  6744. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6745. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6746. const int64_t ne1 = ggml_nelements(src1);
  6747. const int64_t ne = ggml_nelements(dst);
  6748. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6749. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6750. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6751. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6752. void * src0_ddq = src0_extra->data_device[g_main_device];
  6753. half * src0_as_f16 = (half *) src0_ddq;
  6754. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6755. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6756. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6757. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6758. // convert src1 to fp16
  6759. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6760. GGML_ASSERT(to_fp16_cuda != nullptr);
  6761. size_t src1_as = 0;
  6762. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  6763. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  6764. size_t dst_as = 0;
  6765. half * dst_f16 = nullptr;
  6766. char * dst_t = nullptr;
  6767. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  6768. cudaDataType_t cu_data_type = CUDA_R_16F;
  6769. // dst strides
  6770. size_t nbd2 = dst->nb[2];
  6771. size_t nbd3 = dst->nb[3];
  6772. const half alpha_f16 = 1.0f;
  6773. const half beta_f16 = 0.0f;
  6774. const float alpha_f32 = 1.0f;
  6775. const float beta_f32 = 0.0f;
  6776. const void * alpha = &alpha_f16;
  6777. const void * beta = &beta_f16;
  6778. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6779. dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  6780. dst_t = (char *) dst_f16;
  6781. nbd2 /= sizeof(float) / sizeof(half);
  6782. nbd3 /= sizeof(float) / sizeof(half);
  6783. } else {
  6784. dst_t = (char *) dst_ddf;
  6785. cu_compute_type = CUBLAS_COMPUTE_32F;
  6786. cu_data_type = CUDA_R_32F;
  6787. alpha = &alpha_f32;
  6788. beta = &beta_f32;
  6789. }
  6790. GGML_ASSERT(ne12 % ne02 == 0);
  6791. GGML_ASSERT(ne13 % ne03 == 0);
  6792. // broadcast factors
  6793. const int64_t r2 = ne12/ne02;
  6794. const int64_t r3 = ne13/ne03;
  6795. #if 0
  6796. // use cublasGemmEx
  6797. {
  6798. for (int i13 = 0; i13 < ne13; ++i13) {
  6799. for (int i12 = 0; i12 < ne12; ++i12) {
  6800. int i03 = i13 / r3;
  6801. int i02 = i12 / r2;
  6802. CUBLAS_CHECK(
  6803. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6804. ne01, ne11, ne10,
  6805. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6806. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6807. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  6808. cu_compute_type,
  6809. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6810. }
  6811. }
  6812. }
  6813. #else
  6814. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6815. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6816. // use cublasGemmStridedBatchedEx
  6817. CUBLAS_CHECK(
  6818. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6819. ne01, ne11, ne10,
  6820. alpha, (const char *) src0_as_f16, CUDA_R_16F, nb01/sizeof(half), src0->nb[2]/sizeof(half), // strideA
  6821. (const char *) src1_as_f16, CUDA_R_16F, nb11/sizeof(float), src1->nb[2]/sizeof(float), // strideB
  6822. beta, ( char *) dst_t, cu_data_type, ne01, dst->nb[2]/sizeof(float), // strideC
  6823. ne12*ne13,
  6824. cu_compute_type,
  6825. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6826. } else {
  6827. // use cublasGemmBatchedEx
  6828. const int ne23 = ne12*ne13;
  6829. const void ** ptrs_src = nullptr;
  6830. void ** ptrs_dst = nullptr;
  6831. size_t ptrs_src_s = 0;
  6832. size_t ptrs_dst_s = 0;
  6833. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  6834. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  6835. dim3 block_dims(ne13, ne12);
  6836. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6837. src0_as_f16, src1_as_f16, dst_t,
  6838. ptrs_src, ptrs_dst,
  6839. ne12, ne13,
  6840. ne23,
  6841. nb02, nb03,
  6842. nb12, nb13,
  6843. nbd2, nbd3,
  6844. r2, r3);
  6845. CUDA_CHECK(cudaGetLastError());
  6846. CUBLAS_CHECK(
  6847. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6848. ne01, ne11, ne10,
  6849. alpha, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, nb01/sizeof(half),
  6850. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, nb11/sizeof(float),
  6851. beta, ( void **) (ptrs_dst + 0*ne23), cu_data_type, ne01,
  6852. ne23,
  6853. cu_compute_type,
  6854. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6855. if (ptrs_src_s != 0) {
  6856. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  6857. }
  6858. if (ptrs_dst_s != 0) {
  6859. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  6860. }
  6861. }
  6862. #endif
  6863. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6864. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6865. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  6866. ggml_cuda_pool_free(dst_f16, dst_as);
  6867. }
  6868. ggml_cuda_pool_free(src1_as_f16, src1_as);
  6869. }
  6870. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6871. const bool all_on_device =
  6872. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6873. (src1->backend == GGML_BACKEND_GPU) &&
  6874. ( dst->backend == GGML_BACKEND_GPU);
  6875. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6876. int64_t min_compute_capability = INT_MAX;
  6877. for (int64_t id = 0; id < g_device_count; ++id) {
  6878. if (min_compute_capability > g_compute_capabilities[id] && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6879. min_compute_capability = g_compute_capabilities[id];
  6880. }
  6881. }
  6882. #ifdef CUDA_USE_TENSOR_CORES
  6883. const bool use_tensor_cores = true;
  6884. #else
  6885. const bool use_tensor_cores = false;
  6886. #endif
  6887. // debug helpers
  6888. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  6889. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  6890. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  6891. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  6892. //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);
  6893. //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);
  6894. 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) {
  6895. // KQ single-batch
  6896. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  6897. } 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) {
  6898. // KQV single-batch
  6899. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  6900. } 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)) {
  6901. // KQ + KQV multi-batch
  6902. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  6903. } else if (src0->type == GGML_TYPE_F32) {
  6904. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6905. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  6906. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  6907. #ifdef GGML_CUDA_FORCE_DMMV
  6908. const bool use_mul_mat_vec_q = false;
  6909. #else
  6910. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  6911. #endif // GGML_CUDA_FORCE_DMMV
  6912. if (use_mul_mat_vec_q) {
  6913. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  6914. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  6915. } else {
  6916. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  6917. }
  6918. } else {
  6919. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  6920. // when tensor cores are available, use them for large batch size
  6921. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  6922. if (use_tensor_cores && min_compute_capability >= CC_VOLTA && src1->ne[1] > MMQ_MAX_BATCH_SIZE) {
  6923. use_mul_mat_q = false;
  6924. }
  6925. if (use_mul_mat_q) {
  6926. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  6927. } else {
  6928. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6929. }
  6930. }
  6931. } else {
  6932. GGML_ASSERT(false);
  6933. }
  6934. }
  6935. #if 0
  6936. template<typename ... Srcs>
  6937. static __global__ void k_compute_batched_ptrs_id(
  6938. const void ** ptrs_src, void ** ptrs_dst,
  6939. int ne12, int ne13,
  6940. int ne23,
  6941. int nb02, int nb03,
  6942. int nb12, int nb13,
  6943. int nb2, int nb3,
  6944. int r2, int r3,
  6945. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  6946. const half * src1_f16, half * dst_f16,
  6947. const int32_t * ids, const int id,
  6948. Srcs... src0s) {
  6949. int i = ids[id];
  6950. half * src0_f16;
  6951. const void * srcs_ar[] = { (const half *) src0s... };
  6952. if (src0_type == GGML_TYPE_F16) {
  6953. src0_f16 = (half *) srcs_ar[i];
  6954. } else {
  6955. src0_f16 = src0_as_f16;
  6956. if (threadIdx.x == 0 && threadIdx.y == 0) {
  6957. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  6958. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  6959. }
  6960. }
  6961. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6962. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6963. if (i13 >= ne13 || i12 >= ne12) {
  6964. return;
  6965. }
  6966. int i03 = i13 / r3;
  6967. int i02 = i12 / r2;
  6968. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  6969. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  6970. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  6971. }
  6972. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  6973. const struct ggml_tensor * ids = dst->src[0];
  6974. const struct ggml_tensor * src1 = dst->src[1];
  6975. const struct ggml_tensor * src00 = dst->src[2];
  6976. const int id = dst->op_params[0];
  6977. GGML_ASSERT(!ggml_is_transposed(src00));
  6978. GGML_ASSERT(!ggml_is_transposed(src1));
  6979. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  6980. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6981. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  6982. const int64_t ne01 = src00->ne[1];
  6983. const int64_t ne02 = src00->ne[2];
  6984. const int64_t ne03 = src00->ne[3];
  6985. //const int64_t nb01 = src00->nb[1];
  6986. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  6987. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  6988. const int64_t ne10 = src1->ne[0];
  6989. const int64_t ne11 = src1->ne[1];
  6990. const int64_t ne12 = src1->ne[2];
  6991. const int64_t ne13 = src1->ne[3];
  6992. //const int64_t nb11 = src1->nb[1];
  6993. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6994. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6995. const int64_t ne1 = ggml_nelements(src1);
  6996. const int64_t ne = ggml_nelements(dst);
  6997. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6998. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6999. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7000. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7001. //void * src0_ddq = src0_extra->data_device[g_main_device];
  7002. //half * src0_as_f16 = (half *) src0_ddq;
  7003. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7004. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7005. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7006. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7007. // convert src1 to fp16
  7008. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7009. GGML_ASSERT(to_fp16_cuda != nullptr);
  7010. size_t src1_as = 0;
  7011. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7012. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7013. size_t dst_as = 0;
  7014. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7015. GGML_ASSERT(ne12 % ne02 == 0);
  7016. GGML_ASSERT(ne13 % ne03 == 0);
  7017. // broadcast factors
  7018. const int64_t r2 = ne12/ne02;
  7019. const int64_t r3 = ne13/ne03;
  7020. const half alpha_f16 = 1.0f;
  7021. const half beta_f16 = 0.0f;
  7022. // use cublasGemmBatchedEx
  7023. const int ne23 = ne12*ne13;
  7024. const void ** ptrs_src = nullptr;
  7025. void ** ptrs_dst = nullptr;
  7026. size_t ptrs_src_s = 0;
  7027. size_t ptrs_dst_s = 0;
  7028. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7029. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7030. int64_t src0_ne = ggml_nelements(src00);
  7031. half * src0_as_f16 = nullptr;
  7032. size_t src0_as = 0;
  7033. if (src00->type != GGML_TYPE_F16) {
  7034. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7035. }
  7036. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7037. dim3 block_dims(ne13, ne12);
  7038. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7039. ptrs_src, ptrs_dst,
  7040. ne12, ne13,
  7041. ne23,
  7042. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7043. nb12, nb13,
  7044. dst->nb[2], dst->nb[3],
  7045. r2, r3,
  7046. src00->type, src0_as_f16, src0_ne,
  7047. src1_as_f16, dst_f16,
  7048. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7049. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7050. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7051. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7052. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7053. );
  7054. CUDA_CHECK(cudaGetLastError());
  7055. CUBLAS_CHECK(
  7056. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7057. ne01, ne11, ne10,
  7058. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7059. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7060. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7061. ne23,
  7062. CUBLAS_COMPUTE_16F,
  7063. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7064. if (src0_as != 0) {
  7065. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7066. }
  7067. if (ptrs_src_s != 0) {
  7068. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7069. }
  7070. if (ptrs_dst_s != 0) {
  7071. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7072. }
  7073. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7074. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7075. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7076. ggml_cuda_pool_free(dst_f16, dst_as);
  7077. }
  7078. #endif
  7079. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7080. #if 0
  7081. ggml_cuda_mul_mat_id_cublas(dst);
  7082. // TODO: mmq/mmv support
  7083. #endif
  7084. const int64_t nb11 = src1->nb[1];
  7085. const int64_t nb1 = dst->nb[1];
  7086. const struct ggml_tensor * ids = src0;
  7087. const int32_t id = ((int32_t *) dst->op_params)[0];
  7088. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7089. std::vector<char> ids_host(ggml_nbytes(ids));
  7090. const cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7091. if (ids->backend == GGML_BACKEND_GPU) {
  7092. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7093. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7094. CUDA_CHECK(cudaStreamSynchronize(stream));
  7095. } else {
  7096. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7097. }
  7098. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7099. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7100. ggml_tensor_extra_gpu src1_row_extra;
  7101. ggml_tensor_extra_gpu dst_row_extra;
  7102. ggml_tensor src1_row = *src1;
  7103. ggml_tensor dst_row = *dst;
  7104. src1_row.backend = GGML_BACKEND_GPU;
  7105. dst_row.backend = GGML_BACKEND_GPU;
  7106. src1_row.extra = &src1_row_extra;
  7107. dst_row.extra = &dst_row_extra;
  7108. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7109. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7110. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7111. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7112. if (src1->ne[1] == 1) {
  7113. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7114. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7115. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7116. //int32_t row_id;
  7117. //CUDA_CHECK(cudaMemcpyAsync(&row_id, ids_dev + i01*ids->nb[1] + id*ids->nb[0], sizeof(int32_t), cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  7118. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7119. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7120. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7121. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7122. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7123. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7124. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7125. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7126. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7127. }
  7128. } else {
  7129. size_t as_src1, as_dst;
  7130. char * src1_contiguous = (char *) ggml_cuda_pool_malloc(sizeof(float)*ggml_nelements(src1), &as_src1);
  7131. char * dst_contiguous = (char *) ggml_cuda_pool_malloc(sizeof(float)*ggml_nelements(dst), &as_dst);
  7132. src1_row_extra.data_device[g_main_device] = src1_contiguous;
  7133. dst_row_extra.data_device[g_main_device] = dst_contiguous;
  7134. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7135. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7136. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7137. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  7138. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7139. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7140. int64_t num_src1_rows = 0;
  7141. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7142. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7143. if (row_id_i != row_id) {
  7144. continue;
  7145. }
  7146. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7147. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous + num_src1_rows*nb11, src1_original + i01*nb11,
  7148. nb11, src1_kind, stream));
  7149. num_src1_rows++;
  7150. }
  7151. if (num_src1_rows == 0) {
  7152. continue;
  7153. }
  7154. src1_row.ne[1] = num_src1_rows;
  7155. dst_row.ne[1] = num_src1_rows;
  7156. src1_row.nb[1] = nb11;
  7157. src1_row.nb[2] = num_src1_rows*nb11;
  7158. src1_row.nb[3] = num_src1_rows*nb11;
  7159. dst_row.nb[1] = nb1;
  7160. dst_row.nb[2] = num_src1_rows*nb1;
  7161. dst_row.nb[3] = num_src1_rows*nb1;
  7162. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7163. num_src1_rows = 0;
  7164. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7165. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7166. if (row_id_i != row_id) {
  7167. continue;
  7168. }
  7169. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7170. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous + num_src1_rows*nb1,
  7171. nb1, dst_kind, stream));
  7172. num_src1_rows++;
  7173. }
  7174. }
  7175. ggml_cuda_pool_free(src1_contiguous, as_src1);
  7176. ggml_cuda_pool_free(dst_contiguous, as_dst);
  7177. }
  7178. if (dst->backend == GGML_BACKEND_CPU) {
  7179. CUDA_CHECK(cudaStreamSynchronize(stream));
  7180. }
  7181. }
  7182. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7183. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7184. }
  7185. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7186. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7187. }
  7188. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7189. const int64_t ne = ggml_nelements(src0);
  7190. GGML_ASSERT(ne == ggml_nelements(src1));
  7191. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7192. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7193. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7194. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7195. const int64_t ne00 = src0->ne[0];
  7196. const int64_t ne01 = src0->ne[1];
  7197. GGML_ASSERT(src0->ne[3] == 1);
  7198. const int64_t nb00 = src0->nb[0];
  7199. const int64_t nb01 = src0->nb[1];
  7200. const int64_t nb02 = src0->nb[2];
  7201. const int64_t ne10 = src1->ne[0];
  7202. const int64_t ne11 = src1->ne[1];
  7203. GGML_ASSERT(src1->ne[3] == 1);
  7204. const int64_t nb10 = src1->nb[0];
  7205. const int64_t nb11 = src1->nb[1];
  7206. const int64_t nb12 = src1->nb[2];
  7207. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7208. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7209. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7210. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7211. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7212. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7213. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7214. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7215. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7216. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7217. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7218. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7219. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7220. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7221. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7222. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7223. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7224. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7225. } else {
  7226. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7227. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7228. GGML_ASSERT(false);
  7229. }
  7230. (void) dst;
  7231. }
  7232. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7233. // TODO: why do we pass dst as src1 here?
  7234. ggml_cuda_cpy(src0, dst, nullptr);
  7235. (void) src1;
  7236. }
  7237. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7238. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7239. }
  7240. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7241. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7242. }
  7243. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7244. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7245. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7246. }
  7247. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7248. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7249. }
  7250. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7251. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7252. }
  7253. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7254. GGML_ASSERT(ggml_is_contiguous(src0));
  7255. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7256. }
  7257. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7258. GGML_ASSERT(ggml_is_contiguous(src0));
  7259. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7260. }
  7261. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7262. (void) src0;
  7263. (void) src1;
  7264. (void) dst;
  7265. }
  7266. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7267. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7268. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7269. }
  7270. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7271. const int64_t nrows = ggml_nrows(tensor);
  7272. const int64_t ne0 = tensor->ne[0];
  7273. const size_t nb1 = tensor->nb[1];
  7274. ggml_backend_type backend = tensor->backend;
  7275. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7276. memset(extra, 0, sizeof(*extra));
  7277. for (int64_t id = 0; id < g_device_count; ++id) {
  7278. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7279. continue;
  7280. }
  7281. ggml_cuda_set_device(id);
  7282. int64_t row_low, row_high;
  7283. if (backend == GGML_BACKEND_GPU) {
  7284. row_low = 0;
  7285. row_high = nrows;
  7286. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7287. const int64_t rounding = get_row_rounding(tensor->type);
  7288. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7289. row_low -= row_low % rounding;
  7290. if (id == g_device_count - 1) {
  7291. row_high = nrows;
  7292. } else {
  7293. row_high = nrows*g_tensor_split[id + 1];
  7294. row_high -= row_high % rounding;
  7295. }
  7296. } else {
  7297. GGML_ASSERT(false);
  7298. }
  7299. if (row_low == row_high) {
  7300. continue;
  7301. }
  7302. int64_t nrows_split = row_high - row_low;
  7303. const size_t offset_split = row_low*nb1;
  7304. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7305. const size_t original_size = size;
  7306. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7307. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7308. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7309. }
  7310. char * buf;
  7311. CUDA_CHECK(cudaMalloc(&buf, size));
  7312. char * buf_host = (char *)data + offset_split;
  7313. // set padding to 0 to avoid possible NaN values
  7314. if (size > original_size) {
  7315. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7316. }
  7317. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7318. extra->data_device[id] = buf;
  7319. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7320. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7321. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7322. }
  7323. }
  7324. }
  7325. tensor->extra = extra;
  7326. }
  7327. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7328. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7329. return;
  7330. }
  7331. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7332. for (int64_t id = 0; id < g_device_count; ++id) {
  7333. if (extra->data_device[id] != nullptr) {
  7334. CUDA_CHECK(ggml_cuda_set_device(id));
  7335. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7336. }
  7337. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7338. if (extra->events[id][is] != nullptr) {
  7339. CUDA_CHECK(ggml_cuda_set_device(id));
  7340. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7341. }
  7342. }
  7343. }
  7344. delete extra;
  7345. }
  7346. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7347. static size_t g_temp_tensor_extra_index = 0;
  7348. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7349. if (g_temp_tensor_extras == nullptr) {
  7350. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7351. }
  7352. size_t alloc_index = g_temp_tensor_extra_index;
  7353. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7354. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7355. memset(extra, 0, sizeof(*extra));
  7356. return extra;
  7357. }
  7358. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7359. if (scratch && g_scratch_size == 0) {
  7360. return;
  7361. }
  7362. tensor->backend = GGML_BACKEND_GPU;
  7363. // recursively assign CUDA buffers until a compute tensor is found
  7364. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7365. const ggml_op src0_op = tensor->src[0]->op;
  7366. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7367. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7368. }
  7369. }
  7370. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7371. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7372. }
  7373. if (scratch && no_alloc) {
  7374. return;
  7375. }
  7376. ggml_tensor_extra_gpu * extra;
  7377. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7378. tensor->op == GGML_OP_VIEW ||
  7379. force_inplace;
  7380. const size_t size = ggml_nbytes(tensor);
  7381. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7382. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7383. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7384. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7385. size_t offset = 0;
  7386. if (tensor->op == GGML_OP_VIEW) {
  7387. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7388. }
  7389. extra = ggml_cuda_alloc_temp_tensor_extra();
  7390. extra->data_device[g_main_device] = src0_ddc + offset;
  7391. } else if (tensor->op == GGML_OP_CPY) {
  7392. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7393. void * src1_ddv = src1_extra->data_device[g_main_device];
  7394. extra = ggml_cuda_alloc_temp_tensor_extra();
  7395. extra->data_device[g_main_device] = src1_ddv;
  7396. } else if (scratch) {
  7397. GGML_ASSERT(size <= g_scratch_size);
  7398. if (g_scratch_offset + size > g_scratch_size) {
  7399. g_scratch_offset = 0;
  7400. }
  7401. char * data = (char *) g_scratch_buffer;
  7402. if (data == nullptr) {
  7403. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7404. g_scratch_buffer = data;
  7405. }
  7406. extra = ggml_cuda_alloc_temp_tensor_extra();
  7407. extra->data_device[g_main_device] = data + g_scratch_offset;
  7408. g_scratch_offset += size;
  7409. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7410. } else { // allocate new buffers outside of scratch
  7411. void * data;
  7412. CUDA_CHECK(cudaMalloc(&data, size));
  7413. CUDA_CHECK(cudaMemset(data, 0, size));
  7414. extra = new ggml_tensor_extra_gpu;
  7415. memset(extra, 0, sizeof(*extra));
  7416. extra->data_device[g_main_device] = data;
  7417. }
  7418. tensor->extra = extra;
  7419. }
  7420. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7421. if (g_scratch_size == 0) {
  7422. return;
  7423. }
  7424. if (g_scratch_buffer == nullptr) {
  7425. ggml_cuda_set_device(g_main_device);
  7426. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7427. }
  7428. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7429. const bool inplace = tensor->view_src != nullptr;
  7430. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  7431. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  7432. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7433. size_t view_offset = 0;
  7434. if (tensor->op == GGML_OP_VIEW) {
  7435. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7436. }
  7437. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7438. } else {
  7439. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7440. }
  7441. tensor->extra = extra;
  7442. }
  7443. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7444. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7445. GGML_ASSERT(ggml_is_contiguous(tensor));
  7446. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7447. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7448. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7449. }
  7450. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7451. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7452. }
  7453. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7454. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7455. }
  7456. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  7457. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  7458. }
  7459. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  7460. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  7461. }
  7462. void ggml_cuda_set_main_device(const int main_device) {
  7463. if (main_device >= g_device_count) {
  7464. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  7465. main_device, g_device_count, g_main_device);
  7466. return;
  7467. }
  7468. if (g_main_device != main_device && g_device_count > 1) {
  7469. g_main_device = main_device;
  7470. cudaDeviceProp prop;
  7471. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  7472. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  7473. }
  7474. }
  7475. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  7476. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  7477. // it still won't always work as expected, but it's better than nothing
  7478. if (scratch_size > g_scratch_size) {
  7479. ggml_cuda_free_scratch();
  7480. }
  7481. g_scratch_size = std::max(g_scratch_size, scratch_size);
  7482. }
  7483. void ggml_cuda_free_scratch() {
  7484. if (g_scratch_buffer == nullptr) {
  7485. return;
  7486. }
  7487. CUDA_CHECK(cudaFree(g_scratch_buffer));
  7488. g_scratch_buffer = nullptr;
  7489. }
  7490. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  7491. if (!g_cublas_loaded) return false;
  7492. ggml_cuda_func_t func;
  7493. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  7494. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  7495. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  7496. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  7497. return false;
  7498. }
  7499. if (tensor->op == GGML_OP_MUL_MAT) {
  7500. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  7501. #ifndef NDEBUG
  7502. 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]);
  7503. #endif
  7504. return false;
  7505. }
  7506. }
  7507. switch (tensor->op) {
  7508. case GGML_OP_REPEAT:
  7509. func = ggml_cuda_repeat;
  7510. break;
  7511. case GGML_OP_GET_ROWS:
  7512. func = ggml_cuda_get_rows;
  7513. break;
  7514. case GGML_OP_DUP:
  7515. func = ggml_cuda_dup;
  7516. break;
  7517. case GGML_OP_ADD:
  7518. func = ggml_cuda_add;
  7519. break;
  7520. case GGML_OP_ACC:
  7521. func = ggml_cuda_acc;
  7522. break;
  7523. case GGML_OP_MUL:
  7524. func = ggml_cuda_mul;
  7525. break;
  7526. case GGML_OP_DIV:
  7527. func = ggml_cuda_div;
  7528. break;
  7529. case GGML_OP_UNARY:
  7530. switch (ggml_get_unary_op(tensor)) {
  7531. case GGML_UNARY_OP_GELU:
  7532. func = ggml_cuda_gelu;
  7533. break;
  7534. case GGML_UNARY_OP_SILU:
  7535. func = ggml_cuda_silu;
  7536. break;
  7537. case GGML_UNARY_OP_GELU_QUICK:
  7538. func = ggml_cuda_gelu_quick;
  7539. break;
  7540. case GGML_UNARY_OP_TANH:
  7541. func = ggml_cuda_tanh;
  7542. break;
  7543. case GGML_UNARY_OP_RELU:
  7544. func = ggml_cuda_relu;
  7545. break;
  7546. default:
  7547. return false;
  7548. }
  7549. break;
  7550. case GGML_OP_NORM:
  7551. func = ggml_cuda_norm;
  7552. break;
  7553. case GGML_OP_GROUP_NORM:
  7554. func = ggml_cuda_group_norm;
  7555. break;
  7556. case GGML_OP_CONCAT:
  7557. func = ggml_cuda_concat;
  7558. break;
  7559. case GGML_OP_UPSCALE:
  7560. func = ggml_cuda_upscale;
  7561. break;
  7562. case GGML_OP_PAD:
  7563. func = ggml_cuda_pad;
  7564. break;
  7565. case GGML_OP_LEAKY_RELU:
  7566. func = ggml_cuda_leaky_relu;
  7567. break;
  7568. case GGML_OP_RMS_NORM:
  7569. func = ggml_cuda_rms_norm;
  7570. break;
  7571. case GGML_OP_MUL_MAT:
  7572. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  7573. return false;
  7574. }
  7575. func = ggml_cuda_mul_mat;
  7576. break;
  7577. case GGML_OP_MUL_MAT_ID:
  7578. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  7579. return false;
  7580. }
  7581. func = ggml_cuda_mul_mat_id;
  7582. break;
  7583. case GGML_OP_SCALE:
  7584. func = ggml_cuda_scale;
  7585. break;
  7586. case GGML_OP_SQR:
  7587. func = ggml_cuda_sqr;
  7588. break;
  7589. case GGML_OP_CLAMP:
  7590. func = ggml_cuda_clamp;
  7591. break;
  7592. case GGML_OP_CPY:
  7593. func = ggml_cuda_cpy;
  7594. break;
  7595. case GGML_OP_CONT:
  7596. func = ggml_cuda_dup;
  7597. break;
  7598. case GGML_OP_NONE:
  7599. case GGML_OP_RESHAPE:
  7600. case GGML_OP_VIEW:
  7601. case GGML_OP_PERMUTE:
  7602. case GGML_OP_TRANSPOSE:
  7603. func = ggml_cuda_nop;
  7604. break;
  7605. case GGML_OP_DIAG_MASK_INF:
  7606. func = ggml_cuda_diag_mask_inf;
  7607. break;
  7608. case GGML_OP_SOFT_MAX:
  7609. func = ggml_cuda_soft_max;
  7610. break;
  7611. case GGML_OP_ROPE:
  7612. func = ggml_cuda_rope;
  7613. break;
  7614. case GGML_OP_ALIBI:
  7615. func = ggml_cuda_alibi;
  7616. break;
  7617. case GGML_OP_IM2COL:
  7618. func = ggml_cuda_im2col;
  7619. break;
  7620. case GGML_OP_SUM_ROWS:
  7621. func = ggml_cuda_sum_rows;
  7622. break;
  7623. case GGML_OP_ARGSORT:
  7624. func = ggml_cuda_argsort;
  7625. break;
  7626. default:
  7627. return false;
  7628. }
  7629. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  7630. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  7631. }
  7632. if (params->ith != 0) {
  7633. return true;
  7634. }
  7635. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  7636. return true;
  7637. }
  7638. func(tensor->src[0], tensor->src[1], tensor);
  7639. return true;
  7640. }
  7641. int ggml_cuda_get_device_count() {
  7642. int device_count;
  7643. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  7644. return 0;
  7645. }
  7646. return device_count;
  7647. }
  7648. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  7649. cudaDeviceProp prop;
  7650. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  7651. snprintf(description, description_size, "%s", prop.name);
  7652. }
  7653. ////////////////////////////////////////////////////////////////////////////////
  7654. // backend interface
  7655. #define UNUSED GGML_UNUSED
  7656. // cuda buffer
  7657. struct ggml_backend_buffer_context_cuda {
  7658. int device;
  7659. void * dev_ptr = nullptr;
  7660. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  7661. size_t temp_tensor_extra_index = 0;
  7662. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  7663. ~ggml_backend_buffer_context_cuda() {
  7664. delete[] temp_tensor_extras;
  7665. }
  7666. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7667. if (temp_tensor_extras == nullptr) {
  7668. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7669. }
  7670. size_t alloc_index = temp_tensor_extra_index;
  7671. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7672. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  7673. memset(extra, 0, sizeof(*extra));
  7674. return extra;
  7675. }
  7676. };
  7677. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7678. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7679. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  7680. delete ctx;
  7681. }
  7682. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  7683. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7684. return ctx->dev_ptr;
  7685. }
  7686. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7687. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7688. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  7689. assert(tensor->view_src->buffer->buft == buffer->buft);
  7690. tensor->backend = tensor->view_src->backend;
  7691. tensor->extra = tensor->view_src->extra;
  7692. return;
  7693. }
  7694. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  7695. extra->data_device[ctx->device] = tensor->data;
  7696. tensor->backend = GGML_BACKEND_GPU;
  7697. tensor->extra = extra;
  7698. if (ggml_is_quantized(tensor->type)) {
  7699. // initialize padding to 0 to avoid possible NaN values
  7700. int64_t row_low = 0;
  7701. int64_t row_high = ggml_nrows(tensor);
  7702. int64_t nrows_split = row_high - row_low;
  7703. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  7704. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  7705. if (padded_size > original_size && tensor->view_src == nullptr) {
  7706. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  7707. }
  7708. }
  7709. UNUSED(buffer);
  7710. }
  7711. 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) {
  7712. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7713. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7714. ggml_cuda_set_device(ctx->device);
  7715. CUDA_CHECK(cudaDeviceSynchronize());
  7716. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  7717. }
  7718. 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) {
  7719. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7720. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7721. ggml_cuda_set_device(ctx->device);
  7722. CUDA_CHECK(cudaDeviceSynchronize());
  7723. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  7724. }
  7725. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7726. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7727. ggml_cuda_set_device(ctx->device);
  7728. CUDA_CHECK(cudaDeviceSynchronize());
  7729. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  7730. }
  7731. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  7732. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  7733. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  7734. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  7735. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  7736. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  7737. /* .cpy_tensor_from = */ NULL,
  7738. /* .cpy_tensor_to = */ NULL,
  7739. /* .clear = */ ggml_backend_cuda_buffer_clear,
  7740. };
  7741. // cuda buffer type
  7742. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7743. int device = (int) (intptr_t) buft->context;
  7744. ggml_cuda_set_device(device);
  7745. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  7746. void * dev_ptr;
  7747. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  7748. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  7749. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  7750. }
  7751. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7752. return 128;
  7753. UNUSED(buft);
  7754. }
  7755. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  7756. int64_t row_low = 0;
  7757. int64_t row_high = ggml_nrows(tensor);
  7758. int64_t nrows_split = row_high - row_low;
  7759. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7760. int64_t ne0 = tensor->ne[0];
  7761. if (ggml_is_quantized(tensor->type)) {
  7762. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7763. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7764. }
  7765. }
  7766. return size;
  7767. UNUSED(buft);
  7768. }
  7769. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  7770. return ggml_backend_is_cuda(backend);
  7771. UNUSED(buft);
  7772. }
  7773. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  7774. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  7775. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  7776. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  7777. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  7778. /* .is_host = */ nullptr,
  7779. };
  7780. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  7781. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  7782. static bool ggml_backend_cuda_buffer_type_initialized = false;
  7783. if (!ggml_backend_cuda_buffer_type_initialized) {
  7784. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  7785. ggml_backend_cuda_buffer_types[i] = {
  7786. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  7787. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  7788. };
  7789. }
  7790. ggml_backend_cuda_buffer_type_initialized = true;
  7791. }
  7792. return &ggml_backend_cuda_buffer_types[device];
  7793. }
  7794. // host buffer type
  7795. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7796. CUDA_CHECK(cudaFreeHost(buffer->context));
  7797. }
  7798. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7799. void * ptr;
  7800. CUDA_CHECK(cudaMallocHost(&ptr, size));
  7801. // FIXME: this is a hack to avoid having to implement a new buffer type
  7802. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7803. buffer->buft = buft;
  7804. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  7805. return buffer;
  7806. }
  7807. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  7808. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  7809. /* .iface = */ {
  7810. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  7811. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  7812. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7813. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  7814. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7815. },
  7816. /* .context = */ nullptr,
  7817. };
  7818. return &ggml_backend_cuda_buffer_type_host;
  7819. }
  7820. // backend
  7821. struct ggml_backend_context_cuda {
  7822. int device;
  7823. };
  7824. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  7825. return GGML_CUDA_NAME;
  7826. UNUSED(backend);
  7827. }
  7828. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  7829. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7830. delete cuda_ctx;
  7831. delete backend;
  7832. }
  7833. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  7834. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7835. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  7836. }
  7837. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7838. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7839. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7840. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7841. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  7842. }
  7843. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7844. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7845. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7846. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7847. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  7848. }
  7849. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  7850. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7851. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  7852. UNUSED(backend);
  7853. }
  7854. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7855. GGML_ASSERT(!"not implemented");
  7856. return nullptr;
  7857. UNUSED(backend);
  7858. UNUSED(cgraph);
  7859. }
  7860. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7861. GGML_ASSERT(!"not implemented");
  7862. UNUSED(backend);
  7863. UNUSED(plan);
  7864. }
  7865. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7866. GGML_ASSERT(!"not implemented");
  7867. UNUSED(backend);
  7868. UNUSED(plan);
  7869. }
  7870. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7871. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7872. ggml_cuda_set_main_device(cuda_ctx->device);
  7873. ggml_compute_params params = {};
  7874. params.type = GGML_TASK_COMPUTE;
  7875. params.ith = 0;
  7876. for (int i = 0; i < cgraph->n_nodes; i++) {
  7877. ggml_tensor * node = cgraph->nodes[i];
  7878. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  7879. continue;
  7880. assert(node->backend == GGML_BACKEND_GPU);
  7881. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7882. assert(node->extra != nullptr);
  7883. for (int j = 0; j < GGML_MAX_SRC; j++) {
  7884. if (node->src[j] != nullptr) {
  7885. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  7886. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7887. assert(node->src[j]->extra != nullptr);
  7888. }
  7889. }
  7890. bool ok = ggml_cuda_compute_forward(&params, node);
  7891. if (!ok) {
  7892. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  7893. }
  7894. GGML_ASSERT(ok);
  7895. #if 0
  7896. if (node->type == GGML_TYPE_F32) {
  7897. cudaDeviceSynchronize();
  7898. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  7899. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  7900. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  7901. ggml_type_name(node->src[0]->type),
  7902. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  7903. node->src[0]->name,
  7904. node->src[1] ? node->src[1]->name : "none");
  7905. double sum = 0.0;
  7906. double sq_sum = 0.0;
  7907. for (int i = 0; i < ggml_nelements(node); i++) {
  7908. printf("%f ", tmp[i]);
  7909. sum += tmp[i];
  7910. sq_sum += tmp[i]*tmp[i];
  7911. }
  7912. printf("\n");
  7913. printf("sum: %f, ", sum);
  7914. printf("sq_sum: %f\n", sq_sum);
  7915. }
  7916. #endif
  7917. }
  7918. UNUSED(backend);
  7919. }
  7920. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  7921. switch (op->op) {
  7922. case GGML_OP_UNARY:
  7923. switch (ggml_get_unary_op(op)) {
  7924. case GGML_UNARY_OP_GELU:
  7925. case GGML_UNARY_OP_SILU:
  7926. case GGML_UNARY_OP_RELU:
  7927. case GGML_UNARY_OP_GELU_QUICK:
  7928. case GGML_UNARY_OP_TANH:
  7929. return true;
  7930. default:
  7931. return false;
  7932. }
  7933. break;
  7934. case GGML_OP_MUL_MAT:
  7935. case GGML_OP_MUL_MAT_ID:
  7936. {
  7937. struct ggml_tensor * a;
  7938. struct ggml_tensor * b;
  7939. if (op->op == GGML_OP_MUL_MAT) {
  7940. a = op->src[0];
  7941. b = op->src[1];
  7942. } else {
  7943. a = op->src[2];
  7944. b = op->src[1];
  7945. }
  7946. if (a->ne[3] != b->ne[3]) {
  7947. return false;
  7948. }
  7949. return true;
  7950. } break;
  7951. case GGML_OP_GET_ROWS:
  7952. {
  7953. switch (op->src[0]->type) {
  7954. case GGML_TYPE_F16:
  7955. case GGML_TYPE_F32:
  7956. case GGML_TYPE_Q4_0:
  7957. case GGML_TYPE_Q4_1:
  7958. case GGML_TYPE_Q5_0:
  7959. case GGML_TYPE_Q5_1:
  7960. case GGML_TYPE_Q8_0:
  7961. return true;
  7962. default:
  7963. return false;
  7964. }
  7965. } break;
  7966. case GGML_OP_CPY:
  7967. {
  7968. ggml_type src0_type = op->src[0]->type;
  7969. ggml_type src1_type = op->src[1]->type;
  7970. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  7971. return true;
  7972. }
  7973. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  7974. return true;
  7975. }
  7976. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  7977. return true;
  7978. }
  7979. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  7980. return true;
  7981. }
  7982. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  7983. return true;
  7984. }
  7985. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  7986. return true;
  7987. }
  7988. return false;
  7989. } break;
  7990. case GGML_OP_NONE:
  7991. case GGML_OP_RESHAPE:
  7992. case GGML_OP_VIEW:
  7993. case GGML_OP_PERMUTE:
  7994. case GGML_OP_TRANSPOSE:
  7995. case GGML_OP_NORM:
  7996. case GGML_OP_REPEAT:
  7997. case GGML_OP_DUP:
  7998. case GGML_OP_ADD:
  7999. case GGML_OP_MUL:
  8000. case GGML_OP_DIV:
  8001. case GGML_OP_RMS_NORM:
  8002. case GGML_OP_SCALE:
  8003. case GGML_OP_SQR:
  8004. case GGML_OP_CLAMP:
  8005. case GGML_OP_CONT:
  8006. case GGML_OP_DIAG_MASK_INF:
  8007. case GGML_OP_SOFT_MAX:
  8008. case GGML_OP_ROPE:
  8009. case GGML_OP_ALIBI:
  8010. case GGML_OP_IM2COL:
  8011. case GGML_OP_SUM_ROWS:
  8012. case GGML_OP_ARGSORT:
  8013. case GGML_OP_ACC:
  8014. case GGML_OP_CONCAT:
  8015. case GGML_OP_GROUP_NORM:
  8016. case GGML_OP_UPSCALE:
  8017. case GGML_OP_PAD:
  8018. case GGML_OP_LEAKY_RELU:
  8019. return true;
  8020. default:
  8021. return false;
  8022. }
  8023. UNUSED(backend);
  8024. }
  8025. static ggml_backend_i cuda_backend_i = {
  8026. /* .get_name = */ ggml_backend_cuda_name,
  8027. /* .free = */ ggml_backend_cuda_free,
  8028. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8029. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8030. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8031. /* .cpy_tensor_from_async = */ NULL,
  8032. /* .cpy_tensor_to_async = */ NULL,
  8033. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8034. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8035. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8036. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8037. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8038. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8039. };
  8040. ggml_backend_t ggml_backend_cuda_init(int device) {
  8041. ggml_init_cublas(); // TODO: remove from ggml.c
  8042. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8043. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8044. return nullptr;
  8045. }
  8046. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8047. ggml_cuda_set_main_device(device);
  8048. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8049. /* .device = */ device
  8050. };
  8051. ggml_backend_t cuda_backend = new ggml_backend {
  8052. /* .interface = */ cuda_backend_i,
  8053. /* .context = */ ctx
  8054. };
  8055. return cuda_backend;
  8056. }
  8057. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8058. return backend->iface.get_name == ggml_backend_cuda_name;
  8059. }
  8060. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8061. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8062. return cuda_backend;
  8063. UNUSED(params);
  8064. }
  8065. extern "C" int ggml_backend_cuda_reg_devices();
  8066. int ggml_backend_cuda_reg_devices() {
  8067. int device_count = ggml_cuda_get_device_count();
  8068. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8069. for (int i = 0; i < device_count; i++) {
  8070. char name[128];
  8071. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8072. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8073. }
  8074. return device_count;
  8075. }