ggml-cuda.cu 426 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529
  1. #include "ggml-cuda.h"
  2. #include "ggml.h"
  3. #include "ggml-backend-impl.h"
  4. #if defined(GGML_USE_HIPBLAS)
  5. #define GGML_COMMON_DECL_HIP
  6. #define GGML_COMMON_IMPL_HIP
  7. #else
  8. #define GGML_COMMON_DECL_CUDA
  9. #define GGML_COMMON_IMPL_CUDA
  10. #endif
  11. #include "ggml-common.h"
  12. #include <algorithm>
  13. #include <assert.h>
  14. #include <atomic>
  15. #include <cinttypes>
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include <float.h>
  19. #include <limits>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <string>
  23. #include <vector>
  24. #include <map>
  25. #include <array>
  26. // stringize macro for converting __CUDA_ARCH_LIST__ (list of integers) to string
  27. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  28. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  29. #if defined(GGML_USE_HIPBLAS)
  30. #include <hip/hip_runtime.h>
  31. #include <hipblas/hipblas.h>
  32. #include <hip/hip_fp16.h>
  33. #ifdef __HIP_PLATFORM_AMD__
  34. // for rocblas_initialize()
  35. #include "rocblas/rocblas.h"
  36. #endif // __HIP_PLATFORM_AMD__
  37. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  38. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  39. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  40. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  41. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  42. #define CUBLAS_OP_N HIPBLAS_OP_N
  43. #define CUBLAS_OP_T HIPBLAS_OP_T
  44. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  45. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  46. #define CUDA_R_16F HIPBLAS_R_16F
  47. #define CUDA_R_32F HIPBLAS_R_32F
  48. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  49. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  50. #define cublasCreate hipblasCreate
  51. #define cublasGemmEx hipblasGemmEx
  52. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  53. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  54. #define cublasHandle_t hipblasHandle_t
  55. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  56. #define cublasSetStream hipblasSetStream
  57. #define cublasSgemm hipblasSgemm
  58. #define cublasStatus_t hipblasStatus_t
  59. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  60. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  61. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  62. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  63. #define cudaDeviceProp hipDeviceProp_t
  64. #define cudaDeviceSynchronize hipDeviceSynchronize
  65. #define cudaError_t hipError_t
  66. #define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
  67. #define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
  68. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  69. #define cudaEventDisableTiming hipEventDisableTiming
  70. #define cudaEventRecord hipEventRecord
  71. #define cudaEvent_t hipEvent_t
  72. #define cudaEventDestroy hipEventDestroy
  73. #define cudaFree hipFree
  74. #define cudaFreeHost hipHostFree
  75. #define cudaGetDevice hipGetDevice
  76. #define cudaGetDeviceCount hipGetDeviceCount
  77. #define cudaGetDeviceProperties hipGetDeviceProperties
  78. #define cudaGetErrorString hipGetErrorString
  79. #define cudaGetLastError hipGetLastError
  80. #ifdef GGML_HIP_UMA
  81. #define cudaMalloc hipMallocManaged
  82. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  83. #else
  84. #define cudaMalloc hipMalloc
  85. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  86. #endif
  87. #define cudaMemcpy hipMemcpy
  88. #define cudaMemcpyAsync hipMemcpyAsync
  89. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  90. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  91. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  92. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  93. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  94. #define cudaMemcpyKind hipMemcpyKind
  95. #define cudaMemset hipMemset
  96. #define cudaMemsetAsync hipMemsetAsync
  97. #define cudaMemGetInfo hipMemGetInfo
  98. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  99. #define cudaSetDevice hipSetDevice
  100. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  101. #define cudaStreamFireAndForget hipStreamFireAndForget
  102. #define cudaStreamNonBlocking hipStreamNonBlocking
  103. #define cudaStreamSynchronize hipStreamSynchronize
  104. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  105. #define cudaStream_t hipStream_t
  106. #define cudaSuccess hipSuccess
  107. #define __trap abort
  108. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  109. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  110. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  111. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  112. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  113. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  114. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  115. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  116. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  117. #else
  118. #include <cuda_runtime.h>
  119. #include <cuda.h>
  120. #include <cublas_v2.h>
  121. #include <cuda_fp16.h>
  122. #if CUDART_VERSION < 11020
  123. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  124. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  125. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  126. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  127. #define cublasComputeType_t cudaDataType_t
  128. #endif // CUDART_VERSION < 11020
  129. #endif // defined(GGML_USE_HIPBLAS)
  130. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  131. #define CC_PASCAL 600
  132. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  133. #define CC_VOLTA 700
  134. #define CC_OFFSET_AMD 1000000
  135. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  136. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  137. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  138. #define GGML_CUDA_MAX_NODES 8192
  139. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  140. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  141. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  142. // - 7B quantum model: +100-200 MB
  143. // - 13B quantum model: +200-400 MB
  144. //
  145. //#define GGML_CUDA_FORCE_MMQ
  146. // TODO: improve this to be correct for more hardware
  147. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  148. #if !defined(GGML_CUDA_FORCE_MMQ)
  149. #define CUDA_USE_TENSOR_CORES
  150. #endif
  151. #define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
  152. #define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
  153. #if defined(GGML_USE_HIPBLAS)
  154. #define __CUDA_ARCH__ 1300
  155. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  156. defined(__gfx1150__) || defined(__gfx1151__)
  157. #define RDNA3
  158. #endif
  159. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  160. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  161. #define RDNA2
  162. #endif
  163. #ifndef __has_builtin
  164. #define __has_builtin(x) 0
  165. #endif
  166. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  167. typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4)));
  168. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  169. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  170. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  171. #if __has_builtin(__builtin_elementwise_sub_sat)
  172. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  173. return reinterpret_cast<const int &>(c);
  174. #else
  175. int8x4_t c;
  176. int16_t tmp;
  177. #pragma unroll
  178. for (int i = 0; i < 4; i++) {
  179. tmp = va[i] - vb[i];
  180. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  181. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  182. c[i] = tmp;
  183. }
  184. return reinterpret_cast<int &>(c);
  185. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  186. }
  187. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  188. return __vsubss4(a, b);
  189. }
  190. static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) {
  191. const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a);
  192. const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b);
  193. unsigned int c;
  194. uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c);
  195. #pragma unroll
  196. for (int i = 0; i < 4; ++i) {
  197. vc[i] = va[i] == vb[i] ? 0xff : 0x00;
  198. }
  199. return c;
  200. }
  201. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  202. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  203. c = __builtin_amdgcn_sdot4(a, b, c, false);
  204. #elif defined(RDNA3)
  205. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  206. #elif defined(__gfx1010__) || defined(__gfx900__)
  207. int tmp1;
  208. int tmp2;
  209. asm("\n \
  210. 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 \
  211. 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 \
  212. v_add3_u32 %0, %1, %2, %0 \n \
  213. 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 \
  214. 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 \
  215. v_add3_u32 %0, %1, %2, %0 \n \
  216. "
  217. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  218. : "v"(a), "v"(b)
  219. );
  220. #else
  221. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  222. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  223. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  224. #endif
  225. return c;
  226. }
  227. #endif // defined(GGML_USE_HIPBLAS)
  228. #if defined(_MSC_VER)
  229. #pragma warning(disable: 4244 4267) // possible loss of data
  230. #endif
  231. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  232. [[noreturn]]
  233. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  234. int id = -1; // in case cudaGetDevice fails
  235. cudaGetDevice(&id);
  236. fprintf(stderr, "CUDA error: %s\n", msg);
  237. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  238. fprintf(stderr, " %s\n", stmt);
  239. // abort with GGML_ASSERT to get a stack trace
  240. GGML_ASSERT(!"CUDA error");
  241. }
  242. #define CUDA_CHECK_GEN(err, success, error_fn) \
  243. do { \
  244. auto err_ = (err); \
  245. if (err_ != (success)) { \
  246. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  247. } \
  248. } while (0)
  249. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  250. #if CUDART_VERSION >= 12000
  251. static const char * cublas_get_error_str(const cublasStatus_t err) {
  252. return cublasGetStatusString(err);
  253. }
  254. #else
  255. static const char * cublas_get_error_str(const cublasStatus_t err) {
  256. switch (err) {
  257. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  258. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  259. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  260. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  261. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  262. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  263. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  264. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  265. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  266. default: return "unknown error";
  267. }
  268. }
  269. #endif // CUDART_VERSION >= 12000
  270. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  271. #if !defined(GGML_USE_HIPBLAS)
  272. static const char * cu_get_error_str(CUresult err) {
  273. const char * err_str;
  274. cuGetErrorString(err, &err_str);
  275. return err_str;
  276. }
  277. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  278. #endif
  279. #if CUDART_VERSION >= 11100
  280. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  281. #else
  282. #define GGML_CUDA_ASSUME(x)
  283. #endif // CUDART_VERSION >= 11100
  284. #ifdef GGML_CUDA_F16
  285. typedef half dfloat; // dequantize float
  286. typedef half2 dfloat2;
  287. #else
  288. typedef float dfloat; // dequantize float
  289. typedef float2 dfloat2;
  290. #endif //GGML_CUDA_F16
  291. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  292. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  293. int x32 = 0;
  294. x32 |= x16[0] << 0;
  295. x32 |= x16[1] << 16;
  296. return x32;
  297. }
  298. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  299. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  300. int x32 = 0;
  301. x32 |= x16[0] << 0;
  302. x32 |= x16[1] << 16;
  303. return x32;
  304. }
  305. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  306. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  307. }
  308. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  309. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  310. }
  311. template<typename T>
  312. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  313. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  314. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  315. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  316. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  317. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  318. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  319. typedef void (*ggml_cuda_op_mul_mat_t)(
  320. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  321. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  322. const int64_t src1_padded_row_size, cudaStream_t stream);
  323. typedef void (*ggml_cuda_op_flatten_t)(
  324. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  325. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  326. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  327. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  328. typedef void (*load_tiles_cuda_t)(
  329. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  330. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  331. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  332. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  333. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  334. #define WARP_SIZE 32
  335. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  336. #define CUDA_GELU_BLOCK_SIZE 256
  337. #define CUDA_SILU_BLOCK_SIZE 256
  338. #define CUDA_TANH_BLOCK_SIZE 256
  339. #define CUDA_RELU_BLOCK_SIZE 256
  340. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  341. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  342. #define CUDA_SQR_BLOCK_SIZE 256
  343. #define CUDA_CPY_BLOCK_SIZE 32
  344. #define CUDA_SCALE_BLOCK_SIZE 256
  345. #define CUDA_CLAMP_BLOCK_SIZE 256
  346. #define CUDA_ROPE_BLOCK_SIZE 256
  347. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  348. #define CUDA_ALIBI_BLOCK_SIZE 32
  349. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  350. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  351. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  352. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  353. #define CUDA_UPSCALE_BLOCK_SIZE 256
  354. #define CUDA_CONCAT_BLOCK_SIZE 256
  355. #define CUDA_PAD_BLOCK_SIZE 256
  356. #define CUDA_ARANGE_BLOCK_SIZE 256
  357. #define CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE 256
  358. #define CUDA_ACC_BLOCK_SIZE 256
  359. #define CUDA_IM2COL_BLOCK_SIZE 256
  360. #define CUDA_POOL2D_BLOCK_SIZE 256
  361. #define CUDA_Q8_0_NE_ALIGN 2048
  362. // dmmv = dequantize_mul_mat_vec
  363. #ifndef GGML_CUDA_DMMV_X
  364. #define GGML_CUDA_DMMV_X 32
  365. #endif
  366. #ifndef GGML_CUDA_MMV_Y
  367. #define GGML_CUDA_MMV_Y 1
  368. #endif
  369. #ifndef K_QUANTS_PER_ITERATION
  370. #define K_QUANTS_PER_ITERATION 2
  371. #else
  372. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  373. #endif
  374. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  375. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  376. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  377. #define MUL_MAT_SRC1_COL_STRIDE 128
  378. #define MAX_STREAMS 8
  379. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  380. struct ggml_tensor_extra_gpu {
  381. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  382. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  383. };
  384. // this is faster on Windows
  385. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  386. static void ggml_cuda_set_device(const int device) {
  387. int current_device;
  388. CUDA_CHECK(cudaGetDevice(&current_device));
  389. if (device == current_device) {
  390. return;
  391. }
  392. CUDA_CHECK(cudaSetDevice(device));
  393. }
  394. static int g_device_count = -1;
  395. static int g_main_device = 0;
  396. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  397. struct cuda_device_capabilities {
  398. int cc; // compute capability
  399. size_t smpb; // max. shared memory per block
  400. bool vmm; // virtual memory support
  401. size_t vmm_granularity; // granularity of virtual memory
  402. };
  403. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  404. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  405. [[noreturn]]
  406. static __device__ void no_device_code(
  407. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  408. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  409. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  410. file_name, line, function_name, arch);
  411. (void) arch_list;
  412. #else
  413. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  414. file_name, line, function_name, arch, arch_list);
  415. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  416. __trap();
  417. (void) no_device_code; // suppress unused function warning
  418. }
  419. #ifdef __CUDA_ARCH__
  420. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  421. #else
  422. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  423. #endif // __CUDA_ARCH__
  424. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  425. #pragma unroll
  426. for (int mask = 16; mask > 0; mask >>= 1) {
  427. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  428. }
  429. return x;
  430. }
  431. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  432. #pragma unroll
  433. for (int mask = 16; mask > 0; mask >>= 1) {
  434. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  435. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  436. }
  437. return a;
  438. }
  439. #ifdef GGML_CUDA_F16
  440. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  441. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  442. #pragma unroll
  443. for (int mask = 16; mask > 0; mask >>= 1) {
  444. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  445. }
  446. return a;
  447. #else
  448. (void) a;
  449. NO_DEVICE_CODE;
  450. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  451. }
  452. #endif // GGML_CUDA_F16
  453. static __device__ __forceinline__ float warp_reduce_max(float x) {
  454. #pragma unroll
  455. for (int mask = 16; mask > 0; mask >>= 1) {
  456. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  457. }
  458. return x;
  459. }
  460. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  461. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  462. //#pragma unroll
  463. // for (int mask = 16; mask > 0; mask >>= 1) {
  464. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  465. // }
  466. // return x;
  467. //#else
  468. // (void) x;
  469. // NO_DEVICE_CODE;
  470. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  471. //}
  472. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  473. return b;
  474. GGML_UNUSED(a);
  475. }
  476. static __device__ __forceinline__ float op_add(const float a, const float b) {
  477. return a + b;
  478. }
  479. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  480. return a * b;
  481. }
  482. static __device__ __forceinline__ float op_div(const float a, const float b) {
  483. return a / b;
  484. }
  485. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  486. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  487. int ne0, int ne1, int ne2, int ne3,
  488. int ne10, int ne11, int ne12, int ne13,
  489. /*int s0, */ int s1, int s2, int s3,
  490. /*int s10,*/ int s11, int s12, int s13) {
  491. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  492. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  493. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  494. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  495. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  496. return;
  497. }
  498. const int i11 = i1 % ne11;
  499. const int i12 = i2 % ne12;
  500. const int i13 = i3 % ne13;
  501. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  502. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  503. const size_t i_dst = i_src0;
  504. const src0_t * src0_row = src0 + i_src0;
  505. const src1_t * src1_row = src1 + i_src1;
  506. dst_t * dst_row = dst + i_dst;
  507. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  508. const int i10 = i0 % ne10;
  509. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  510. }
  511. }
  512. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  513. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  514. int ne0, int ne1, int ne2, int ne3,
  515. int ne10, int ne11, int ne12, int ne13,
  516. /*int s0, */ int s1, int s2, int s3,
  517. /*int s10,*/ int s11, int s12, int s13) {
  518. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  519. const int i3 = i/(ne2*ne1*ne0);
  520. const int i2 = (i/(ne1*ne0)) % ne2;
  521. const int i1 = (i/ne0) % ne1;
  522. const int i0 = i % ne0;
  523. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  524. return;
  525. }
  526. const int i11 = i1 % ne11;
  527. const int i12 = i2 % ne12;
  528. const int i13 = i3 % ne13;
  529. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  530. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  531. const size_t i_dst = i_src0;
  532. const src0_t * src0_row = src0 + i_src0;
  533. const src1_t * src1_row = src1 + i_src1;
  534. dst_t * dst_row = dst + i_dst;
  535. const int i10 = i0 % ne10;
  536. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  537. }
  538. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  539. const int ne10, const int ne11, const int ne12,
  540. const int nb1, const int nb2, int offset) {
  541. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  542. if (i >= ne) {
  543. return;
  544. }
  545. int src1_idx = i - offset;
  546. int oz = src1_idx / nb2;
  547. int oy = (src1_idx - (oz * nb2)) / nb1;
  548. int ox = src1_idx % nb1;
  549. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  550. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  551. } else {
  552. dst[i] = x[i];
  553. }
  554. }
  555. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  556. const float GELU_COEF_A = 0.044715f;
  557. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  558. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  559. if (i >= k) {
  560. return;
  561. }
  562. float xi = x[i];
  563. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  564. }
  565. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  566. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  567. if (i >= k) {
  568. return;
  569. }
  570. dst[i] = x[i] / (1.0f + expf(-x[i]));
  571. }
  572. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  573. const float GELU_QUICK_COEF = -1.702f;
  574. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  575. if (i >= k) {
  576. return;
  577. }
  578. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  579. }
  580. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  581. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  582. if (i >= k) {
  583. return;
  584. }
  585. dst[i] = tanhf(x[i]);
  586. }
  587. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  588. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  589. if (i >= k) {
  590. return;
  591. }
  592. dst[i] = fmaxf(x[i], 0);
  593. }
  594. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  595. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  596. if (i >= k) {
  597. return;
  598. }
  599. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  600. }
  601. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  602. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  603. if (i >= k) {
  604. return;
  605. }
  606. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  607. }
  608. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  609. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  610. if (i >= k) {
  611. return;
  612. }
  613. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  614. }
  615. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  616. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  617. if (i >= k) {
  618. return;
  619. }
  620. dst[i] = x[i] * x[i];
  621. }
  622. template <int block_size>
  623. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  624. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  625. const int tid = threadIdx.x;
  626. float2 mean_var = make_float2(0.f, 0.f);
  627. for (int col = tid; col < ncols; col += block_size) {
  628. const float xi = x[row*ncols + col];
  629. mean_var.x += xi;
  630. mean_var.y += xi * xi;
  631. }
  632. // sum up partial sums
  633. mean_var = warp_reduce_sum(mean_var);
  634. if (block_size > WARP_SIZE) {
  635. __shared__ float2 s_sum[32];
  636. int warp_id = threadIdx.x / WARP_SIZE;
  637. int lane_id = threadIdx.x % WARP_SIZE;
  638. if (lane_id == 0) {
  639. s_sum[warp_id] = mean_var;
  640. }
  641. __syncthreads();
  642. mean_var = s_sum[lane_id];
  643. mean_var = warp_reduce_sum(mean_var);
  644. }
  645. const float mean = mean_var.x / ncols;
  646. const float var = mean_var.y / ncols - mean * mean;
  647. const float inv_std = rsqrtf(var + eps);
  648. for (int col = tid; col < ncols; col += block_size) {
  649. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  650. }
  651. }
  652. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  653. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  654. if (nidx >= ne0) {
  655. return;
  656. }
  657. // operation
  658. int offset_dst =
  659. nidx +
  660. blockIdx.y * ne0 +
  661. blockIdx.z * ne0 * gridDim.y;
  662. if (blockIdx.z < ne02) { // src0
  663. int offset_src =
  664. nidx +
  665. blockIdx.y * ne0 +
  666. blockIdx.z * ne0 * gridDim.y;
  667. dst[offset_dst] = x[offset_src];
  668. } else {
  669. int offset_src =
  670. nidx +
  671. blockIdx.y * ne0 +
  672. (blockIdx.z - ne02) * ne0 * gridDim.y;
  673. dst[offset_dst] = y[offset_src];
  674. }
  675. }
  676. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int ne00xne01, const int scale_factor) {
  677. // blockIdx.z: idx of ne02*ne03
  678. // blockIdx.y: idx of ne01*scale_factor, aka ne1
  679. // blockIDx.x: idx of ne00*scale_factor / BLOCK_SIZE
  680. // ne00xne01: ne00 * ne01
  681. int ne0 = ne00 * scale_factor;
  682. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  683. if (nidx >= ne0) {
  684. return;
  685. }
  686. // operation
  687. int i00 = nidx / scale_factor;
  688. int i01 = blockIdx.y / scale_factor;
  689. int offset_src =
  690. i00 +
  691. i01 * ne00 +
  692. blockIdx.z * ne00xne01;
  693. int offset_dst =
  694. nidx +
  695. blockIdx.y * ne0 +
  696. blockIdx.z * ne0 * gridDim.y;
  697. dst[offset_dst] = x[offset_src];
  698. }
  699. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02, const int ne03) {
  700. // blockIdx.z: idx of ne2*ne3, aka ne02*ne03
  701. // blockIdx.y: idx of ne1
  702. // blockIDx.x: idx of ne0 / BLOCK_SIZE
  703. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  704. if (nidx >= ne0) {
  705. return;
  706. }
  707. // operation
  708. int offset_dst =
  709. nidx +
  710. blockIdx.y * ne0 +
  711. blockIdx.z * ne0 * gridDim.y;
  712. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02*ne03) {
  713. int offset_src =
  714. nidx +
  715. blockIdx.y * ne00 +
  716. blockIdx.z * ne00 * ne01;
  717. dst[offset_dst] = x[offset_src];
  718. } else {
  719. dst[offset_dst] = 0.0f;
  720. }
  721. }
  722. static __global__ void arange_f32(float * dst, const int ne0, const float start, const float step) {
  723. // blockIDx.x: idx of ne0 / BLOCK_SIZE
  724. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  725. if (nidx >= ne0) {
  726. return;
  727. }
  728. dst[nidx] = start + step * nidx;
  729. }
  730. static __global__ void timestep_embedding_f32(const float * timesteps, float * dst, const int nb1, const int dim, const int max_period) {
  731. // blockIDx.y: idx of timesteps->ne[0]
  732. // blockIDx.x: idx of ((dim + 1) / 2) / BLOCK_SIZE
  733. int i = blockIdx.y;
  734. int j = threadIdx.x + blockIdx.x * blockDim.x;
  735. float * embed_data = (float *)((char *)dst + i*nb1);
  736. if (dim % 2 != 0 && j == ((dim + 1) / 2)) {
  737. embed_data[dim] = 0.f;
  738. }
  739. int half = dim / 2;
  740. if (j >= half) {
  741. return;
  742. }
  743. float timestep = timesteps[i];
  744. float freq = (float)expf(-logf(max_period) * j / half);
  745. float arg = timestep * freq;
  746. embed_data[j] = cosf(arg);
  747. embed_data[j + half] = sinf(arg);
  748. }
  749. template <int block_size>
  750. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  751. // blockIdx.x: num_groups idx
  752. // threadIdx.x: block_size idx
  753. int start = blockIdx.x * group_size;
  754. int end = start + group_size;
  755. start += threadIdx.x;
  756. if (end >= ne_elements) {
  757. end = ne_elements;
  758. }
  759. float tmp = 0.0f; // partial sum for thread in warp
  760. for (int j = start; j < end; j += block_size) {
  761. tmp += x[j];
  762. }
  763. tmp = warp_reduce_sum(tmp);
  764. if (block_size > WARP_SIZE) {
  765. __shared__ float s_sum[32];
  766. int warp_id = threadIdx.x / WARP_SIZE;
  767. int lane_id = threadIdx.x % WARP_SIZE;
  768. if (lane_id == 0) {
  769. s_sum[warp_id] = tmp;
  770. }
  771. __syncthreads();
  772. tmp = s_sum[lane_id];
  773. tmp = warp_reduce_sum(tmp);
  774. }
  775. float mean = tmp / group_size;
  776. tmp = 0.0f;
  777. for (int j = start; j < end; j += block_size) {
  778. float xi = x[j] - mean;
  779. dst[j] = xi;
  780. tmp += xi * xi;
  781. }
  782. tmp = warp_reduce_sum(tmp);
  783. if (block_size > WARP_SIZE) {
  784. __shared__ float s_sum[32];
  785. int warp_id = threadIdx.x / WARP_SIZE;
  786. int lane_id = threadIdx.x % WARP_SIZE;
  787. if (lane_id == 0) {
  788. s_sum[warp_id] = tmp;
  789. }
  790. __syncthreads();
  791. tmp = s_sum[lane_id];
  792. tmp = warp_reduce_sum(tmp);
  793. }
  794. float variance = tmp / group_size;
  795. float scale = rsqrtf(variance + eps);
  796. for (int j = start; j < end; j += block_size) {
  797. dst[j] *= scale;
  798. }
  799. }
  800. template <int block_size>
  801. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  802. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  803. const int tid = threadIdx.x;
  804. float tmp = 0.0f; // partial sum for thread in warp
  805. for (int col = tid; col < ncols; col += block_size) {
  806. const float xi = x[row*ncols + col];
  807. tmp += xi * xi;
  808. }
  809. // sum up partial sums
  810. tmp = warp_reduce_sum(tmp);
  811. if (block_size > WARP_SIZE) {
  812. __shared__ float s_sum[32];
  813. int warp_id = threadIdx.x / WARP_SIZE;
  814. int lane_id = threadIdx.x % WARP_SIZE;
  815. if (lane_id == 0) {
  816. s_sum[warp_id] = tmp;
  817. }
  818. __syncthreads();
  819. tmp = s_sum[lane_id];
  820. tmp = warp_reduce_sum(tmp);
  821. }
  822. const float mean = tmp / ncols;
  823. const float scale = rsqrtf(mean + eps);
  824. for (int col = tid; col < ncols; col += block_size) {
  825. dst[row*ncols + col] = scale * x[row*ncols + col];
  826. }
  827. }
  828. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  829. const block_q4_0 * x = (const block_q4_0 *) vx;
  830. const dfloat d = x[ib].d;
  831. const int vui = x[ib].qs[iqs];
  832. v.x = vui & 0xF;
  833. v.y = vui >> 4;
  834. #ifdef GGML_CUDA_F16
  835. v = __hsub2(v, {8.0f, 8.0f});
  836. v = __hmul2(v, {d, d});
  837. #else
  838. v.x = (v.x - 8.0f) * d;
  839. v.y = (v.y - 8.0f) * d;
  840. #endif // GGML_CUDA_F16
  841. }
  842. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  843. const block_q4_1 * x = (const block_q4_1 *) vx;
  844. const dfloat d = __low2half(x[ib].dm);
  845. const dfloat m = __high2half(x[ib].dm);
  846. const int vui = x[ib].qs[iqs];
  847. v.x = vui & 0xF;
  848. v.y = vui >> 4;
  849. #ifdef GGML_CUDA_F16
  850. v = __hmul2(v, {d, d});
  851. v = __hadd2(v, {m, m});
  852. #else
  853. v.x = (v.x * d) + m;
  854. v.y = (v.y * d) + m;
  855. #endif // GGML_CUDA_F16
  856. }
  857. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  858. const block_q5_0 * x = (const block_q5_0 *) vx;
  859. const dfloat d = x[ib].d;
  860. uint32_t qh;
  861. memcpy(&qh, x[ib].qh, sizeof(qh));
  862. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  863. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  864. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  865. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  866. #ifdef GGML_CUDA_F16
  867. v = __hsub2(v, {16.0f, 16.0f});
  868. v = __hmul2(v, {d, d});
  869. #else
  870. v.x = (v.x - 16.0f) * d;
  871. v.y = (v.y - 16.0f) * d;
  872. #endif // GGML_CUDA_F16
  873. }
  874. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  875. const block_q5_1 * x = (const block_q5_1 *) vx;
  876. const dfloat d = __low2half(x[ib].dm);
  877. const dfloat m = __high2half(x[ib].dm);
  878. uint32_t qh;
  879. memcpy(&qh, x[ib].qh, sizeof(qh));
  880. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  881. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  882. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  883. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  884. #ifdef GGML_CUDA_F16
  885. v = __hmul2(v, {d, d});
  886. v = __hadd2(v, {m, m});
  887. #else
  888. v.x = (v.x * d) + m;
  889. v.y = (v.y * d) + m;
  890. #endif // GGML_CUDA_F16
  891. }
  892. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  893. const block_q8_0 * x = (const block_q8_0 *) vx;
  894. const dfloat d = x[ib].d;
  895. v.x = x[ib].qs[iqs + 0];
  896. v.y = x[ib].qs[iqs + 1];
  897. #ifdef GGML_CUDA_F16
  898. v = __hmul2(v, {d, d});
  899. #else
  900. v.x *= d;
  901. v.y *= d;
  902. #endif // GGML_CUDA_F16
  903. }
  904. template<typename dst_t>
  905. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  906. const int i = blockIdx.x;
  907. // assume 32 threads
  908. const int tid = threadIdx.x;
  909. const int il = tid/8;
  910. const int ir = tid%8;
  911. const int ib = 8*i + ir;
  912. if (ib >= nb32) {
  913. return;
  914. }
  915. dst_t * y = yy + 256*i + 32*ir + 4*il;
  916. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  917. const float d = __half2float(x->d);
  918. const float dm = -8*d;
  919. const uint8_t * q = x->qs + 4*il;
  920. for (int l = 0; l < 4; ++l) {
  921. y[l+ 0] = d * (q[l] & 0xF) + dm;
  922. y[l+16] = d * (q[l] >> 4) + dm;
  923. }
  924. }
  925. template<typename dst_t>
  926. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  927. const int i = blockIdx.x;
  928. // assume 32 threads
  929. const int tid = threadIdx.x;
  930. const int il = tid/8;
  931. const int ir = tid%8;
  932. const int ib = 8*i + ir;
  933. if (ib >= nb32) {
  934. return;
  935. }
  936. dst_t * y = yy + 256*i + 32*ir + 4*il;
  937. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  938. const float2 d = __half22float2(x->dm);
  939. const uint8_t * q = x->qs + 4*il;
  940. for (int l = 0; l < 4; ++l) {
  941. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  942. y[l+16] = d.x * (q[l] >> 4) + d.y;
  943. }
  944. }
  945. //================================== k-quants
  946. template<typename dst_t>
  947. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  948. const int i = blockIdx.x;
  949. const block_q2_K * x = (const block_q2_K *) vx;
  950. const int tid = threadIdx.x;
  951. #if QK_K == 256
  952. const int n = tid/32;
  953. const int l = tid - 32*n;
  954. const int is = 8*n + l/16;
  955. const uint8_t q = x[i].qs[32*n + l];
  956. dst_t * y = yy + i*QK_K + 128*n;
  957. float dall = __low2half(x[i].dm);
  958. float dmin = __high2half(x[i].dm);
  959. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  960. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  961. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  962. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  963. #else
  964. const int is = tid/16; // 0 or 1
  965. const int il = tid%16; // 0...15
  966. const uint8_t q = x[i].qs[il] >> (2*is);
  967. dst_t * y = yy + i*QK_K + 16*is + il;
  968. float dall = __low2half(x[i].dm);
  969. float dmin = __high2half(x[i].dm);
  970. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  971. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  972. #endif
  973. }
  974. template<typename dst_t>
  975. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  976. const int i = blockIdx.x;
  977. const block_q3_K * x = (const block_q3_K *) vx;
  978. #if QK_K == 256
  979. const int r = threadIdx.x/4;
  980. const int tid = r/2;
  981. const int is0 = r%2;
  982. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  983. const int n = tid / 4;
  984. const int j = tid - 4*n;
  985. uint8_t m = 1 << (4*n + j);
  986. int is = 8*n + 2*j + is0;
  987. int shift = 2*j;
  988. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  989. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  990. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  991. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  992. float d_all = x[i].d;
  993. float dl = d_all * (us - 32);
  994. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  995. const uint8_t * q = x[i].qs + 32*n;
  996. const uint8_t * hm = x[i].hmask;
  997. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  998. #else
  999. const int tid = threadIdx.x;
  1000. const int is = tid/16; // 0 or 1
  1001. const int il = tid%16; // 0...15
  1002. const int im = il/8; // 0...1
  1003. const int in = il%8; // 0...7
  1004. dst_t * y = yy + i*QK_K + 16*is + il;
  1005. const uint8_t q = x[i].qs[il] >> (2*is);
  1006. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1007. const float d = (float)x[i].d;
  1008. if (is == 0) {
  1009. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1010. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1011. } else {
  1012. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1013. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1014. }
  1015. #endif
  1016. }
  1017. #if QK_K == 256
  1018. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1019. if (j < 4) {
  1020. d = q[j] & 63; m = q[j + 4] & 63;
  1021. } else {
  1022. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1023. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1024. }
  1025. }
  1026. #endif
  1027. template<typename dst_t>
  1028. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1029. const block_q4_K * x = (const block_q4_K *) vx;
  1030. const int i = blockIdx.x;
  1031. #if QK_K == 256
  1032. // assume 32 threads
  1033. const int tid = threadIdx.x;
  1034. const int il = tid/8;
  1035. const int ir = tid%8;
  1036. const int is = 2*il;
  1037. const int n = 4;
  1038. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1039. const float dall = __low2half(x[i].dm);
  1040. const float dmin = __high2half(x[i].dm);
  1041. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1042. uint8_t sc, m;
  1043. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1044. const float d1 = dall * sc; const float m1 = dmin * m;
  1045. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1046. const float d2 = dall * sc; const float m2 = dmin * m;
  1047. for (int l = 0; l < n; ++l) {
  1048. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1049. y[l +32] = d2 * (q[l] >> 4) - m2;
  1050. }
  1051. #else
  1052. const int tid = threadIdx.x;
  1053. const uint8_t * q = x[i].qs;
  1054. dst_t * y = yy + i*QK_K;
  1055. const float d = (float)x[i].dm[0];
  1056. const float m = (float)x[i].dm[1];
  1057. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1058. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1059. #endif
  1060. }
  1061. template<typename dst_t>
  1062. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1063. const block_q5_K * x = (const block_q5_K *) vx;
  1064. const int i = blockIdx.x;
  1065. #if QK_K == 256
  1066. // assume 64 threads - this is very slightly better than the one below
  1067. const int tid = threadIdx.x;
  1068. const int il = tid/16; // il is in 0...3
  1069. const int ir = tid%16; // ir is in 0...15
  1070. const int is = 2*il; // is is in 0...6
  1071. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1072. const float dall = __low2half(x[i].dm);
  1073. const float dmin = __high2half(x[i].dm);
  1074. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1075. const uint8_t * qh = x[i].qh + 2*ir;
  1076. uint8_t sc, m;
  1077. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1078. const float d1 = dall * sc; const float m1 = dmin * m;
  1079. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1080. const float d2 = dall * sc; const float m2 = dmin * m;
  1081. uint8_t hm = 1 << (2*il);
  1082. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1083. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1084. hm <<= 1;
  1085. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1086. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1087. #else
  1088. const int tid = threadIdx.x;
  1089. const uint8_t q = x[i].qs[tid];
  1090. const int im = tid/8; // 0...3
  1091. const int in = tid%8; // 0...7
  1092. const int is = tid/16; // 0 or 1
  1093. const uint8_t h = x[i].qh[in] >> im;
  1094. const float d = x[i].d;
  1095. dst_t * y = yy + i*QK_K + tid;
  1096. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1097. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1098. #endif
  1099. }
  1100. template<typename dst_t>
  1101. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1102. const block_q6_K * x = (const block_q6_K *) vx;
  1103. const int i = blockIdx.x;
  1104. #if QK_K == 256
  1105. // assume 64 threads - this is very slightly better than the one below
  1106. const int tid = threadIdx.x;
  1107. const int ip = tid/32; // ip is 0 or 1
  1108. const int il = tid - 32*ip; // 0...32
  1109. const int is = 8*ip + il/16;
  1110. dst_t * y = yy + i*QK_K + 128*ip + il;
  1111. const float d = x[i].d;
  1112. const uint8_t * ql = x[i].ql + 64*ip + il;
  1113. const uint8_t qh = x[i].qh[32*ip + il];
  1114. const int8_t * sc = x[i].scales + is;
  1115. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1116. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1117. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1118. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1119. #else
  1120. // assume 32 threads
  1121. const int tid = threadIdx.x;
  1122. const int ip = tid/16; // 0 or 1
  1123. const int il = tid - 16*ip; // 0...15
  1124. dst_t * y = yy + i*QK_K + 16*ip + il;
  1125. const float d = x[i].d;
  1126. const uint8_t ql = x[i].ql[16*ip + il];
  1127. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1128. const int8_t * sc = x[i].scales;
  1129. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1130. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1131. #endif
  1132. }
  1133. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1134. switch (type) {
  1135. case GGML_TYPE_Q4_0:
  1136. case GGML_TYPE_Q4_1:
  1137. case GGML_TYPE_Q5_0:
  1138. case GGML_TYPE_Q5_1:
  1139. case GGML_TYPE_Q8_0:
  1140. case GGML_TYPE_Q2_K:
  1141. case GGML_TYPE_Q3_K:
  1142. case GGML_TYPE_Q4_K:
  1143. case GGML_TYPE_Q5_K:
  1144. case GGML_TYPE_Q6_K:
  1145. return true;
  1146. default:
  1147. return false;
  1148. }
  1149. }
  1150. template<typename dst_t>
  1151. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1152. const int i = blockIdx.x;
  1153. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1154. const int tid = threadIdx.x;
  1155. #if QK_K == 256
  1156. const int il = tid/8; // 0...3
  1157. const int ib = tid%8; // 0...7
  1158. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1159. const uint16_t * q2 = x[i].qs + 4*ib;
  1160. const uint8_t * aux8 = (const uint8_t *)q2;
  1161. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  1162. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1163. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1164. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1165. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1166. #else
  1167. assert(false);
  1168. #endif
  1169. }
  1170. template<typename dst_t>
  1171. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1172. const int i = blockIdx.x;
  1173. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  1174. const int tid = threadIdx.x;
  1175. #if QK_K == 256
  1176. const int il = tid/8; // 0...3
  1177. const int ib = tid%8; // 0...7
  1178. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1179. const uint16_t * q2 = x[i].qs + 4*ib;
  1180. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  1181. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1182. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  1183. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1184. #else
  1185. assert(false);
  1186. #endif
  1187. }
  1188. template<typename dst_t>
  1189. static __global__ void dequantize_block_iq2_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1190. const int i = blockIdx.x;
  1191. const block_iq2_s * x = (const block_iq2_s *) vx;
  1192. const int tid = threadIdx.x;
  1193. #if QK_K == 256
  1194. const int il = tid/8; // 0...3
  1195. const int ib = tid%8; // 0...7
  1196. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1197. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  1198. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1199. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  1200. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1201. #else
  1202. assert(false);
  1203. #endif
  1204. }
  1205. template<typename dst_t>
  1206. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1207. const int i = blockIdx.x;
  1208. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  1209. const int tid = threadIdx.x;
  1210. #if QK_K == 256
  1211. const int il = tid/8; // 0...3
  1212. const int ib = tid%8; // 0...7
  1213. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1214. const uint8_t * q3 = x[i].qs + 8*ib;
  1215. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  1216. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  1217. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  1218. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  1219. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  1220. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1221. for (int j = 0; j < 4; ++j) {
  1222. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1223. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1224. }
  1225. #else
  1226. assert(false);
  1227. #endif
  1228. }
  1229. template<typename dst_t>
  1230. static __global__ void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1231. const int i = blockIdx.x;
  1232. const block_iq3_s * x = (const block_iq3_s *) vx;
  1233. const int tid = threadIdx.x;
  1234. #if QK_K == 256
  1235. const int il = tid/8; // 0...3
  1236. const int ib = tid%8; // 0...7
  1237. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1238. const uint8_t * qs = x[i].qs + 8*ib;
  1239. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  1240. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  1241. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  1242. const uint8_t signs = x[i].signs[4*ib + il];
  1243. for (int j = 0; j < 4; ++j) {
  1244. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1245. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1246. }
  1247. #else
  1248. assert(false);
  1249. #endif
  1250. }
  1251. template<typename dst_t>
  1252. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1253. const int i = blockIdx.x;
  1254. const block_iq1_s * x = (const block_iq1_s *) vx;
  1255. const int tid = threadIdx.x;
  1256. #if QK_K == 256
  1257. const int il = tid/8; // 0...3
  1258. const int ib = tid%8; // 0...7
  1259. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1260. const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
  1261. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
  1262. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  1263. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
  1264. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  1265. grid32[0] &= 0x0f0f0f0f;
  1266. for (int j = 0; j < 8; ++j) {
  1267. y[j] = d * (q[j] + delta);
  1268. }
  1269. #else
  1270. assert(false);
  1271. #endif
  1272. }
  1273. static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  1274. template<typename dst_t>
  1275. static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1276. const int i = blockIdx.x;
  1277. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  1278. const int tid = threadIdx.x;
  1279. const int il = tid/8; // 0...3
  1280. const int ib = tid%8; // 0...7
  1281. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  1282. const uint8_t * q4 = x[ib].qs + 4*il;
  1283. const float d = (float)x[ib].d;
  1284. for (int j = 0; j < 4; ++j) {
  1285. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  1286. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  1287. }
  1288. }
  1289. #if QK_K != 64
  1290. template<typename dst_t>
  1291. static __global__ void dequantize_block_iq4_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1292. const int i = blockIdx.x;
  1293. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  1294. const int tid = threadIdx.x;
  1295. const int il = tid/8; // 0...3
  1296. const int ib = tid%8; // 0...7
  1297. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  1298. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  1299. const float d = (float)x[i].d * ((((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4)) - 32);
  1300. for (int j = 0; j < 4; ++j) {
  1301. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  1302. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  1303. }
  1304. }
  1305. #endif
  1306. 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) {
  1307. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1308. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1309. if (row > nrows) return;
  1310. const int num_blocks_per_row = ncols / QK_K;
  1311. const int ib0 = row*num_blocks_per_row;
  1312. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1313. float tmp = 0; // partial sum for thread in warp
  1314. #if QK_K == 256
  1315. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1316. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1317. const int step = 16/K_QUANTS_PER_ITERATION;
  1318. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1319. const int in = tid - step*im; // 0...15 or 0...7
  1320. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1321. const int q_offset = 32*im + l0;
  1322. const int s_offset = 8*im;
  1323. const int y_offset = 128*im + l0;
  1324. uint32_t aux[4];
  1325. const uint8_t * d = (const uint8_t *)aux;
  1326. const uint8_t * m = (const uint8_t *)(aux + 2);
  1327. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1328. const float * y = yy + i * QK_K + y_offset;
  1329. const uint8_t * q = x[i].qs + q_offset;
  1330. const float dall = __low2half(x[i].dm);
  1331. const float dmin = __high2half(x[i].dm);
  1332. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1333. aux[0] = a[0] & 0x0f0f0f0f;
  1334. aux[1] = a[1] & 0x0f0f0f0f;
  1335. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1336. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1337. float sum1 = 0, sum2 = 0;
  1338. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1339. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1340. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1341. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1342. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1343. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1344. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1345. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1346. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1347. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1348. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1349. }
  1350. tmp += dall * sum1 - dmin * sum2;
  1351. }
  1352. #else
  1353. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1354. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1355. const int offset = tid * K_QUANTS_PER_ITERATION;
  1356. uint32_t uaux[2];
  1357. const uint8_t * d = (const uint8_t *)uaux;
  1358. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1359. const float * y = yy + i * QK_K + offset;
  1360. const uint8_t * q = x[i].qs + offset;
  1361. const uint32_t * s = (const uint32_t *)x[i].scales;
  1362. uaux[0] = s[0] & 0x0f0f0f0f;
  1363. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1364. const float2 dall = __half22float2(x[i].dm);
  1365. float sum1 = 0, sum2 = 0;
  1366. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1367. const uint8_t ql = q[l];
  1368. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1369. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1370. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1371. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1372. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1373. }
  1374. tmp += dall.x * sum1 - dall.y * sum2;
  1375. }
  1376. #endif
  1377. // sum up partial sums and write back result
  1378. tmp = warp_reduce_sum(tmp);
  1379. if (threadIdx.x == 0) {
  1380. dst[row] = tmp;
  1381. }
  1382. }
  1383. 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) {
  1384. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1385. if (row > nrows) return;
  1386. const int num_blocks_per_row = ncols / QK_K;
  1387. const int ib0 = row*num_blocks_per_row;
  1388. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1389. float tmp = 0; // partial sum for thread in warp
  1390. #if QK_K == 256
  1391. const uint16_t kmask1 = 0x0303;
  1392. const uint16_t kmask2 = 0x0f0f;
  1393. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1394. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1395. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1396. const int step = 16/K_QUANTS_PER_ITERATION;
  1397. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1398. const int in = tid - step*im; // 0....15 or 0...7
  1399. const uint8_t m = 1 << (4*im);
  1400. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1401. const int q_offset = 32*im + l0;
  1402. const int y_offset = 128*im + l0;
  1403. uint16_t utmp[4];
  1404. const int8_t * s = (const int8_t *)utmp;
  1405. const uint16_t s_shift = 4*im;
  1406. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1407. const float * y = yy + i * QK_K + y_offset;
  1408. const uint8_t * q = x[i].qs + q_offset;
  1409. const uint8_t * h = x[i].hmask + l0;
  1410. const uint16_t * a = (const uint16_t *)x[i].scales;
  1411. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1412. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1413. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1414. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1415. const float d = x[i].d;
  1416. float sum = 0;
  1417. for (int l = 0; l < n; ++l) {
  1418. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1419. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1420. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1421. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1422. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1423. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1424. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1425. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1426. }
  1427. tmp += d * sum;
  1428. }
  1429. #else
  1430. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1431. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1432. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1433. const int in = offset/8; // 0 or 1
  1434. const int im = offset%8; // 0...7
  1435. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1436. const float * y = yy + i * QK_K + offset;
  1437. const uint8_t * q = x[i].qs + offset;
  1438. const uint8_t * s = x[i].scales;
  1439. const float dall = (float)x[i].d;
  1440. float sum = 0;
  1441. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1442. const uint8_t hl = x[i].hmask[im+l] >> in;
  1443. const uint8_t ql = q[l];
  1444. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1445. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1446. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1447. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1448. }
  1449. tmp += sum;
  1450. }
  1451. #endif
  1452. // sum up partial sums and write back result
  1453. tmp = warp_reduce_sum(tmp);
  1454. if (threadIdx.x == 0) {
  1455. dst[row] = tmp;
  1456. }
  1457. }
  1458. 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) {
  1459. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1460. if (row > nrows) return;
  1461. const int num_blocks_per_row = ncols / QK_K;
  1462. const int ib0 = row*num_blocks_per_row;
  1463. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1464. #if QK_K == 256
  1465. const uint16_t kmask1 = 0x3f3f;
  1466. const uint16_t kmask2 = 0x0f0f;
  1467. const uint16_t kmask3 = 0xc0c0;
  1468. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1469. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1470. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1471. const int il = tid/step; // 0...3
  1472. const int ir = tid - step*il; // 0...7 or 0...3
  1473. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1474. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1475. const int in = il%2;
  1476. const int l0 = n*(2*ir + in);
  1477. const int q_offset = 32*im + l0;
  1478. const int y_offset = 64*im + l0;
  1479. uint16_t aux[4];
  1480. const uint8_t * sc = (const uint8_t *)aux;
  1481. #if K_QUANTS_PER_ITERATION == 2
  1482. uint32_t q32[4];
  1483. const uint8_t * q4 = (const uint8_t *)q32;
  1484. #else
  1485. uint16_t q16[4];
  1486. const uint8_t * q4 = (const uint8_t *)q16;
  1487. #endif
  1488. float tmp = 0; // partial sum for thread in warp
  1489. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1490. const float * y1 = yy + i*QK_K + y_offset;
  1491. const float * y2 = y1 + 128;
  1492. const float dall = __low2half(x[i].dm);
  1493. const float dmin = __high2half(x[i].dm);
  1494. const uint16_t * a = (const uint16_t *)x[i].scales;
  1495. aux[0] = a[im+0] & kmask1;
  1496. aux[1] = a[im+2] & kmask1;
  1497. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1498. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1499. #if K_QUANTS_PER_ITERATION == 2
  1500. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1501. const uint32_t * q2 = q1 + 16;
  1502. q32[0] = q1[0] & 0x0f0f0f0f;
  1503. q32[1] = q1[0] & 0xf0f0f0f0;
  1504. q32[2] = q2[0] & 0x0f0f0f0f;
  1505. q32[3] = q2[0] & 0xf0f0f0f0;
  1506. float4 s = {0.f, 0.f, 0.f, 0.f};
  1507. float smin = 0;
  1508. for (int l = 0; l < 4; ++l) {
  1509. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1510. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1511. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1512. }
  1513. 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;
  1514. #else
  1515. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1516. const uint16_t * q2 = q1 + 32;
  1517. q16[0] = q1[0] & 0x0f0f;
  1518. q16[1] = q1[0] & 0xf0f0;
  1519. q16[2] = q2[0] & 0x0f0f;
  1520. q16[3] = q2[0] & 0xf0f0;
  1521. float4 s = {0.f, 0.f, 0.f, 0.f};
  1522. float smin = 0;
  1523. for (int l = 0; l < 2; ++l) {
  1524. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1525. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1526. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1527. }
  1528. 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;
  1529. #endif
  1530. }
  1531. #else
  1532. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1533. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1534. const int step = tid * K_QUANTS_PER_ITERATION;
  1535. uint16_t aux16[2];
  1536. const uint8_t * s = (const uint8_t *)aux16;
  1537. float tmp = 0;
  1538. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1539. const uint8_t * q = x[i].qs + step;
  1540. const float * y = yy + i*QK_K + step;
  1541. const uint16_t * a = (const uint16_t *)x[i].scales;
  1542. aux16[0] = a[0] & 0x0f0f;
  1543. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1544. const float d = (float)x[i].dm[0];
  1545. const float m = (float)x[i].dm[1];
  1546. float sum = 0.f;
  1547. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1548. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1549. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1550. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1551. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1552. }
  1553. tmp += sum;
  1554. }
  1555. #endif
  1556. // sum up partial sums and write back result
  1557. tmp = warp_reduce_sum(tmp);
  1558. if (tid == 0) {
  1559. dst[row] = tmp;
  1560. }
  1561. }
  1562. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1563. const int row = blockIdx.x;
  1564. const int num_blocks_per_row = ncols / QK_K;
  1565. const int ib0 = row*num_blocks_per_row;
  1566. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1567. float tmp = 0; // partial sum for thread in warp
  1568. #if QK_K == 256
  1569. const uint16_t kmask1 = 0x3f3f;
  1570. const uint16_t kmask2 = 0x0f0f;
  1571. const uint16_t kmask3 = 0xc0c0;
  1572. const int tid = threadIdx.x/2; // 0...15
  1573. const int ix = threadIdx.x%2;
  1574. const int il = tid/4; // 0...3
  1575. const int ir = tid - 4*il;// 0...3
  1576. const int n = 2;
  1577. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1578. const int in = il%2;
  1579. const int l0 = n*(2*ir + in);
  1580. const int q_offset = 32*im + l0;
  1581. const int y_offset = 64*im + l0;
  1582. const uint8_t hm1 = 1 << (2*im);
  1583. const uint8_t hm2 = hm1 << 4;
  1584. uint16_t aux[4];
  1585. const uint8_t * sc = (const uint8_t *)aux;
  1586. uint16_t q16[8];
  1587. const uint8_t * q4 = (const uint8_t *)q16;
  1588. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1589. const uint8_t * ql1 = x[i].qs + q_offset;
  1590. const uint8_t * qh = x[i].qh + l0;
  1591. const float * y1 = yy + i*QK_K + y_offset;
  1592. const float * y2 = y1 + 128;
  1593. const float dall = __low2half(x[i].dm);
  1594. const float dmin = __high2half(x[i].dm);
  1595. const uint16_t * a = (const uint16_t *)x[i].scales;
  1596. aux[0] = a[im+0] & kmask1;
  1597. aux[1] = a[im+2] & kmask1;
  1598. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1599. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1600. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1601. float smin = 0;
  1602. const uint16_t * q1 = (const uint16_t *)ql1;
  1603. const uint16_t * q2 = q1 + 32;
  1604. q16[0] = q1[0] & 0x0f0f;
  1605. q16[1] = q1[8] & 0x0f0f;
  1606. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1607. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1608. q16[4] = q2[0] & 0x0f0f;
  1609. q16[5] = q2[8] & 0x0f0f;
  1610. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1611. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1612. for (int l = 0; l < n; ++l) {
  1613. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1614. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1615. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1616. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1617. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1618. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1619. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1620. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1621. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1622. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1623. }
  1624. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1625. }
  1626. #else
  1627. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1628. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1629. const int step = tid * K_QUANTS_PER_ITERATION;
  1630. const int im = step/8;
  1631. const int in = step%8;
  1632. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1633. const uint8_t * q = x[i].qs + step;
  1634. const int8_t * s = x[i].scales;
  1635. const float * y = yy + i*QK_K + step;
  1636. const float d = x[i].d;
  1637. float sum = 0.f;
  1638. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1639. const uint8_t h = x[i].qh[in+j] >> im;
  1640. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1641. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1642. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1643. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1644. }
  1645. tmp += sum;
  1646. }
  1647. #endif
  1648. // sum up partial sums and write back result
  1649. tmp = warp_reduce_sum(tmp);
  1650. if (threadIdx.x == 0) {
  1651. dst[row] = tmp;
  1652. }
  1653. }
  1654. 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) {
  1655. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1656. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1657. if (row > nrows) return;
  1658. const int num_blocks_per_row = ncols / QK_K;
  1659. const int ib0 = row*num_blocks_per_row;
  1660. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1661. #if QK_K == 256
  1662. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1663. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1664. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1665. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1666. const int in = tid - step*im; // 0...15 or 0...7
  1667. #if K_QUANTS_PER_ITERATION == 1
  1668. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1669. const int is = 0;
  1670. #else
  1671. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1672. const int is = in / 4;
  1673. #endif
  1674. const int ql_offset = 64*im + l0;
  1675. const int qh_offset = 32*im + l0;
  1676. const int s_offset = 8*im + is;
  1677. const int y_offset = 128*im + l0;
  1678. float tmp = 0; // partial sum for thread in warp
  1679. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1680. const float * y = yy + i * QK_K + y_offset;
  1681. const uint8_t * ql = x[i].ql + ql_offset;
  1682. const uint8_t * qh = x[i].qh + qh_offset;
  1683. const int8_t * s = x[i].scales + s_offset;
  1684. const float d = x[i].d;
  1685. #if K_QUANTS_PER_ITERATION == 1
  1686. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1687. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1688. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1689. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1690. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1691. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1692. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1693. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1694. tmp += sum;
  1695. #else
  1696. float sum = 0;
  1697. for (int l = 0; l < 4; ++l) {
  1698. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1699. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1700. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1701. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1702. }
  1703. tmp += sum;
  1704. #endif
  1705. }
  1706. #else
  1707. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1708. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1709. const int step = tid * K_QUANTS_PER_ITERATION;
  1710. float tmp = 0; // partial sum for thread in warp
  1711. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1712. const float * y = yy + i * QK_K + step;
  1713. const uint8_t * ql = x[i].ql + step;
  1714. const uint8_t * qh = x[i].qh + step;
  1715. const int8_t * s = x[i].scales;
  1716. const float d = x[i+0].d;
  1717. float sum = 0;
  1718. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1719. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1720. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1721. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1722. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1723. }
  1724. tmp += sum;
  1725. }
  1726. #endif
  1727. // sum up partial sums and write back result
  1728. tmp = warp_reduce_sum(tmp);
  1729. if (tid == 0) {
  1730. dst[row] = tmp;
  1731. }
  1732. }
  1733. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1734. const half * x = (const half *) vx;
  1735. // automatic half -> float type cast if dfloat == float
  1736. v.x = x[ib + iqs + 0];
  1737. v.y = x[ib + iqs + 1];
  1738. }
  1739. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1740. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1741. if (ix >= kx_padded) {
  1742. return;
  1743. }
  1744. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1745. const int i_padded = iy*kx_padded + ix;
  1746. block_q8_1 * y = (block_q8_1 *) vy;
  1747. const int ib = i_padded / QK8_1; // block index
  1748. const int iqs = i_padded % QK8_1; // quant index
  1749. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1750. float amax = fabsf(xi);
  1751. float sum = xi;
  1752. amax = warp_reduce_max(amax);
  1753. sum = warp_reduce_sum(sum);
  1754. const float d = amax / 127;
  1755. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1756. y[ib].qs[iqs] = q;
  1757. if (iqs > 0) {
  1758. return;
  1759. }
  1760. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1761. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1762. }
  1763. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1764. static __global__ void k_get_rows(
  1765. const void * src0, const int32_t * src1, dst_t * dst,
  1766. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1767. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1768. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1769. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1770. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1771. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1772. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1773. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1774. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1775. if (i00 >= ne00) {
  1776. return;
  1777. }
  1778. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1779. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1780. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1781. const int ib = i00/qk; // block index
  1782. const int iqs = (i00%qk)/qr; // quant index
  1783. const int iybs = i00 - i00%qk; // dst block start index
  1784. const int y_offset = qr == 1 ? 1 : qk/2;
  1785. // dequantize
  1786. dfloat2 v;
  1787. dequantize_kernel(src0_row, ib, iqs, v);
  1788. dst_row[iybs + iqs + 0] = v.x;
  1789. dst_row[iybs + iqs + y_offset] = v.y;
  1790. }
  1791. template<typename src0_t, typename dst_t>
  1792. static __global__ void k_get_rows_float(
  1793. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1794. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1795. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1796. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1797. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1798. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1799. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1800. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1801. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1802. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1803. if (i00 >= ne00) {
  1804. return;
  1805. }
  1806. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1807. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1808. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1809. dst_row[i00] = src0_row[i00];
  1810. }
  1811. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1812. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1813. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  1814. if (i >= k) {
  1815. return;
  1816. }
  1817. const int ib = i/qk; // block index
  1818. const int iqs = (i%qk)/qr; // quant index
  1819. const int iybs = i - i%qk; // y block start index
  1820. const int y_offset = qr == 1 ? 1 : qk/2;
  1821. // dequantize
  1822. dfloat2 v;
  1823. dequantize_kernel(vx, ib, iqs, v);
  1824. y[iybs + iqs + 0] = v.x;
  1825. y[iybs + iqs + y_offset] = v.y;
  1826. }
  1827. template <typename src_t, typename dst_t>
  1828. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1829. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  1830. if (i >= k) {
  1831. return;
  1832. }
  1833. const src_t * x = (src_t *) vx;
  1834. y[i] = x[i];
  1835. }
  1836. template <bool need_check>
  1837. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  1838. #if __CUDA_ARCH__ >= CC_PASCAL
  1839. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  1840. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  1841. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  1842. half2 * y2 = (half2 *) (y + i0);
  1843. __shared__ int vals[nint];
  1844. #pragma unroll
  1845. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  1846. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  1847. break;
  1848. }
  1849. const int ix = ix0 + threadIdx.x;
  1850. vals[ix] = x0[ix];
  1851. }
  1852. #pragma unroll
  1853. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  1854. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  1855. return;
  1856. }
  1857. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  1858. const half d = *b0;
  1859. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  1860. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  1861. }
  1862. #else
  1863. (void) vx; (void) y; (void) k;
  1864. NO_DEVICE_CODE;
  1865. #endif // __CUDA_ARCH__ >= CC_PASCAL
  1866. }
  1867. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1868. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1869. #define VDR_Q4_0_Q8_1_MMVQ 2
  1870. #define VDR_Q4_0_Q8_1_MMQ 4
  1871. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1872. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1873. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1874. int sumi = 0;
  1875. #pragma unroll
  1876. for (int i = 0; i < vdr; ++i) {
  1877. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1878. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1879. // SIMD dot product of quantized values
  1880. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1881. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1882. }
  1883. const float2 ds8f = __half22float2(ds8);
  1884. // second part effectively subtracts 8 from each quant value
  1885. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1886. #else
  1887. NO_DEVICE_CODE;
  1888. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1889. }
  1890. #define VDR_Q4_1_Q8_1_MMVQ 2
  1891. #define VDR_Q4_1_Q8_1_MMQ 4
  1892. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1893. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1894. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1895. int sumi = 0;
  1896. #pragma unroll
  1897. for (int i = 0; i < vdr; ++i) {
  1898. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1899. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1900. // SIMD dot product of quantized values
  1901. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1902. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1903. }
  1904. #ifdef GGML_CUDA_F16
  1905. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1906. const float d4d8 = tmp.x;
  1907. const float m4s8 = tmp.y;
  1908. #else
  1909. const float2 dm4f = __half22float2(dm4);
  1910. const float2 ds8f = __half22float2(ds8);
  1911. const float d4d8 = dm4f.x * ds8f.x;
  1912. const float m4s8 = dm4f.y * ds8f.y;
  1913. #endif // GGML_CUDA_F16
  1914. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1915. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1916. #else
  1917. NO_DEVICE_CODE;
  1918. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1919. }
  1920. #define VDR_Q5_0_Q8_1_MMVQ 2
  1921. #define VDR_Q5_0_Q8_1_MMQ 4
  1922. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1923. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1924. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1925. int sumi = 0;
  1926. #pragma unroll
  1927. for (int i = 0; i < vdr; ++i) {
  1928. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1929. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1930. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1931. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1932. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1933. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1934. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1935. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1936. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1937. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1938. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1939. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1940. }
  1941. const float2 ds8f = __half22float2(ds8);
  1942. // second part effectively subtracts 16 from each quant value
  1943. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1944. #else
  1945. NO_DEVICE_CODE;
  1946. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1947. }
  1948. #define VDR_Q5_1_Q8_1_MMVQ 2
  1949. #define VDR_Q5_1_Q8_1_MMQ 4
  1950. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1951. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1952. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1953. int sumi = 0;
  1954. #pragma unroll
  1955. for (int i = 0; i < vdr; ++i) {
  1956. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1957. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1958. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1959. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1960. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1961. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1962. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1963. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1964. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1965. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1966. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1967. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1968. }
  1969. #ifdef GGML_CUDA_F16
  1970. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1971. const float d5d8 = tmp.x;
  1972. const float m5s8 = tmp.y;
  1973. #else
  1974. const float2 dm5f = __half22float2(dm5);
  1975. const float2 ds8f = __half22float2(ds8);
  1976. const float d5d8 = dm5f.x * ds8f.x;
  1977. const float m5s8 = dm5f.y * ds8f.y;
  1978. #endif // GGML_CUDA_F16
  1979. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1980. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1981. #else
  1982. NO_DEVICE_CODE;
  1983. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1984. }
  1985. #define VDR_Q8_0_Q8_1_MMVQ 2
  1986. #define VDR_Q8_0_Q8_1_MMQ 8
  1987. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1988. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1989. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1990. int sumi = 0;
  1991. #pragma unroll
  1992. for (int i = 0; i < vdr; ++i) {
  1993. // SIMD dot product of quantized values
  1994. sumi = __dp4a(v[i], u[i], sumi);
  1995. }
  1996. return d8_0*d8_1 * sumi;
  1997. #else
  1998. NO_DEVICE_CODE;
  1999. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2000. }
  2001. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2002. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2003. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2004. int sumi = 0;
  2005. #pragma unroll
  2006. for (int i = 0; i < vdr; ++i) {
  2007. // SIMD dot product of quantized values
  2008. sumi = __dp4a(v[i], u[i], sumi);
  2009. }
  2010. #ifdef GGML_CUDA_F16
  2011. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2012. const float d8d8 = tmp.x;
  2013. const float m8s8 = tmp.y;
  2014. #else
  2015. const float2 dm8f = __half22float2(dm8);
  2016. const float2 ds8f = __half22float2(ds8);
  2017. const float d8d8 = dm8f.x * ds8f.x;
  2018. const float m8s8 = dm8f.y * ds8f.y;
  2019. #endif // GGML_CUDA_F16
  2020. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2021. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2022. #else
  2023. NO_DEVICE_CODE;
  2024. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2025. }
  2026. #define VDR_Q2_K_Q8_1_MMVQ 1
  2027. #define VDR_Q2_K_Q8_1_MMQ 2
  2028. // contiguous v/x values
  2029. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2030. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2031. const half2 & dm2, const float * __restrict__ d8) {
  2032. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2033. float sumf_d = 0.0f;
  2034. float sumf_m = 0.0f;
  2035. #pragma unroll
  2036. for (int i = 0; i < QR2_K; ++i) {
  2037. const int sc = scales[2*i];
  2038. const int vi = (v >> (2*i)) & 0x03030303;
  2039. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2040. // fill int with 4x m
  2041. int m = sc >> 4;
  2042. m |= m << 8;
  2043. m |= m << 16;
  2044. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2045. }
  2046. const float2 dm2f = __half22float2(dm2);
  2047. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2048. #else
  2049. NO_DEVICE_CODE;
  2050. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2051. }
  2052. // contiguous u/y values
  2053. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2054. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2055. const half2 & dm2, const float & d8) {
  2056. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2057. int sumi_d = 0;
  2058. int sumi_m = 0;
  2059. #pragma unroll
  2060. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2061. int sumi_d_sc = 0;
  2062. const int sc = scales[i0 / (QI8_1/2)];
  2063. // fill int with 4x m
  2064. int m = sc >> 4;
  2065. m |= m << 8;
  2066. m |= m << 16;
  2067. #pragma unroll
  2068. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2069. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2070. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2071. }
  2072. sumi_d += sumi_d_sc * (sc & 0xF);
  2073. }
  2074. const float2 dm2f = __half22float2(dm2);
  2075. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2076. #else
  2077. NO_DEVICE_CODE;
  2078. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2079. }
  2080. #define VDR_Q3_K_Q8_1_MMVQ 1
  2081. #define VDR_Q3_K_Q8_1_MMQ 2
  2082. // contiguous v/x values
  2083. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2084. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2085. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2086. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2087. float sumf = 0.0f;
  2088. #pragma unroll
  2089. for (int i = 0; i < QR3_K; ++i) {
  2090. const int isc = scale_offset + 2*i;
  2091. const int isc_low = isc % (QK_K/32);
  2092. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2093. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2094. const int isc_high = isc % (QK_K/64);
  2095. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2096. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2097. const int sc = (sc_low | sc_high) - 32;
  2098. const int vil = (vl >> (2*i)) & 0x03030303;
  2099. const int vih = ((vh >> i) << 2) & 0x04040404;
  2100. const int vi = __vsubss4(vil, vih);
  2101. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2102. }
  2103. return d3 * sumf;
  2104. #else
  2105. NO_DEVICE_CODE;
  2106. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2107. }
  2108. // contiguous u/y values
  2109. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2110. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2111. const float & d3, const float & d8) {
  2112. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2113. int sumi = 0;
  2114. #pragma unroll
  2115. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2116. int sumi_sc = 0;
  2117. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2118. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2119. }
  2120. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2121. }
  2122. return d3*d8 * sumi;
  2123. #else
  2124. NO_DEVICE_CODE;
  2125. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2126. }
  2127. #define VDR_Q4_K_Q8_1_MMVQ 2
  2128. #define VDR_Q4_K_Q8_1_MMQ 8
  2129. // contiguous v/x values
  2130. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2131. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2132. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2133. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2134. float sumf_d = 0.0f;
  2135. float sumf_m = 0.0f;
  2136. #pragma unroll
  2137. for (int i = 0; i < QR4_K; ++i) {
  2138. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2139. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2140. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2141. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2142. sumf_d += d8[i] * (dot1 * sc[i]);
  2143. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2144. }
  2145. const float2 dm4f = __half22float2(dm4);
  2146. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2147. #else
  2148. NO_DEVICE_CODE;
  2149. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2150. }
  2151. // contiguous u/y values
  2152. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2153. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2154. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2155. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2156. float sumf_d = 0.0f;
  2157. float sumf_m = 0.0f;
  2158. #pragma unroll
  2159. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2160. int sumi_d = 0;
  2161. #pragma unroll
  2162. for (int j = 0; j < QI8_1; ++j) {
  2163. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2164. }
  2165. const float2 ds8f = __half22float2(ds8[i]);
  2166. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2167. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2168. }
  2169. const float2 dm4f = __half22float2(dm4);
  2170. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2171. #else
  2172. NO_DEVICE_CODE;
  2173. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2174. }
  2175. #define VDR_Q5_K_Q8_1_MMVQ 2
  2176. #define VDR_Q5_K_Q8_1_MMQ 8
  2177. // contiguous v/x values
  2178. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2179. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2180. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2181. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2182. float sumf_d = 0.0f;
  2183. float sumf_m = 0.0f;
  2184. #pragma unroll
  2185. for (int i = 0; i < QR5_K; ++i) {
  2186. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2187. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2188. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2189. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2190. const int v0i = vl0i | vh0i;
  2191. const int v1i = vl1i | vh1i;
  2192. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2193. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2194. sumf_d += d8[i] * (dot1 * sc[i]);
  2195. sumf_m += d8[i] * (dot2 * m[i]);
  2196. }
  2197. const float2 dm5f = __half22float2(dm5);
  2198. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2199. #else
  2200. NO_DEVICE_CODE;
  2201. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2202. }
  2203. // contiguous u/y values
  2204. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2205. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2206. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2207. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2208. float sumf_d = 0.0f;
  2209. float sumf_m = 0.0f;
  2210. #pragma unroll
  2211. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2212. int sumi_d = 0;
  2213. #pragma unroll
  2214. for (int j = 0; j < QI8_1; ++j) {
  2215. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2216. }
  2217. const float2 ds8f = __half22float2(ds8[i]);
  2218. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2219. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2220. }
  2221. const float2 dm4f = __half22float2(dm4);
  2222. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2223. #else
  2224. NO_DEVICE_CODE;
  2225. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2226. }
  2227. #define VDR_Q6_K_Q8_1_MMVQ 1
  2228. #define VDR_Q6_K_Q8_1_MMQ 8
  2229. // contiguous v/x values
  2230. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2231. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2232. const float & d, const float * __restrict__ d8) {
  2233. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2234. float sumf = 0.0f;
  2235. #pragma unroll
  2236. for (int i = 0; i < QR6_K; ++i) {
  2237. const int sc = scales[4*i];
  2238. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2239. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2240. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2241. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2242. }
  2243. return d*sumf;
  2244. #else
  2245. NO_DEVICE_CODE;
  2246. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2247. }
  2248. // contiguous u/y values
  2249. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2250. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2251. const float & d6, const float * __restrict__ d8) {
  2252. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2253. float sumf_d = 0.0f;
  2254. #pragma unroll
  2255. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2256. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2257. #pragma unroll
  2258. for (int i = i0; i < i0 + 2; ++i) {
  2259. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2260. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2261. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2262. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2263. }
  2264. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2265. }
  2266. return d6 * sumf_d;
  2267. #else
  2268. NO_DEVICE_CODE;
  2269. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2270. }
  2271. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2272. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2273. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2274. int v[VDR_Q4_0_Q8_1_MMVQ];
  2275. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2276. #pragma unroll
  2277. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2278. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2279. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2280. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2281. }
  2282. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2283. }
  2284. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2285. (void)x_qh; (void)x_sc;
  2286. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2287. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2288. *x_ql = tile_x_qs;
  2289. *x_dm = (half2 *) tile_x_d;
  2290. }
  2291. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2292. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2293. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2294. (void)x_qh; (void)x_sc;
  2295. GGML_CUDA_ASSUME(i_offset >= 0);
  2296. GGML_CUDA_ASSUME(i_offset < nwarps);
  2297. GGML_CUDA_ASSUME(k >= 0);
  2298. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2299. const int kbx = k / QI4_0;
  2300. const int kqsx = k % QI4_0;
  2301. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2302. float * x_dmf = (float *) x_dm;
  2303. #pragma unroll
  2304. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2305. int i = i0 + i_offset;
  2306. if (need_check) {
  2307. i = min(i, i_max);
  2308. }
  2309. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2310. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2311. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2312. }
  2313. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2314. const int kbxd = k % blocks_per_tile_x_row;
  2315. #pragma unroll
  2316. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2317. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2318. if (need_check) {
  2319. i = min(i, i_max);
  2320. }
  2321. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2322. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2323. }
  2324. }
  2325. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2326. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2327. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2328. (void)x_qh; (void)x_sc;
  2329. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2330. const float * x_dmf = (const float *) x_dm;
  2331. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2332. #pragma unroll
  2333. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2334. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2335. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2336. }
  2337. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2338. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2339. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2340. }
  2341. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2342. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2343. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2344. int v[VDR_Q4_1_Q8_1_MMVQ];
  2345. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2346. #pragma unroll
  2347. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2348. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2349. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2350. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2351. }
  2352. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2353. }
  2354. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2355. (void)x_qh; (void)x_sc;
  2356. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2357. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2358. *x_ql = tile_x_qs;
  2359. *x_dm = tile_x_dm;
  2360. }
  2361. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2362. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2363. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2364. (void)x_qh; (void)x_sc;
  2365. GGML_CUDA_ASSUME(i_offset >= 0);
  2366. GGML_CUDA_ASSUME(i_offset < nwarps);
  2367. GGML_CUDA_ASSUME(k >= 0);
  2368. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2369. const int kbx = k / QI4_1;
  2370. const int kqsx = k % QI4_1;
  2371. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2372. #pragma unroll
  2373. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2374. int i = i0 + i_offset;
  2375. if (need_check) {
  2376. i = min(i, i_max);
  2377. }
  2378. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2379. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2380. }
  2381. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2382. const int kbxd = k % blocks_per_tile_x_row;
  2383. #pragma unroll
  2384. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2385. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2386. if (need_check) {
  2387. i = min(i, i_max);
  2388. }
  2389. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2390. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2391. }
  2392. }
  2393. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2394. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2395. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2396. (void)x_qh; (void)x_sc;
  2397. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2398. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2399. #pragma unroll
  2400. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2401. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2402. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2403. }
  2404. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2405. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2406. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2407. }
  2408. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2409. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2410. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2411. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2412. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2413. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2414. #pragma unroll
  2415. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2416. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2417. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2418. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2419. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2420. }
  2421. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2422. }
  2423. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2424. (void)x_qh; (void)x_sc;
  2425. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2426. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2427. *x_ql = tile_x_ql;
  2428. *x_dm = (half2 *) tile_x_d;
  2429. }
  2430. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2431. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2432. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2433. (void)x_qh; (void)x_sc;
  2434. GGML_CUDA_ASSUME(i_offset >= 0);
  2435. GGML_CUDA_ASSUME(i_offset < nwarps);
  2436. GGML_CUDA_ASSUME(k >= 0);
  2437. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2438. const int kbx = k / QI5_0;
  2439. const int kqsx = k % QI5_0;
  2440. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2441. #pragma unroll
  2442. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2443. int i = i0 + i_offset;
  2444. if (need_check) {
  2445. i = min(i, i_max);
  2446. }
  2447. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2448. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2449. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2450. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2451. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2452. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2453. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2454. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2455. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2456. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2457. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2458. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2459. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2460. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2461. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2462. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2463. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2464. }
  2465. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2466. const int kbxd = k % blocks_per_tile_x_row;
  2467. float * x_dmf = (float *) x_dm;
  2468. #pragma unroll
  2469. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2470. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2471. if (need_check) {
  2472. i = min(i, i_max);
  2473. }
  2474. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2475. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2476. }
  2477. }
  2478. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2479. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2480. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2481. (void)x_qh; (void)x_sc;
  2482. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2483. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2484. const float * x_dmf = (const float *) x_dm;
  2485. const float * y_df = (const float *) y_ds;
  2486. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2487. #pragma unroll
  2488. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2489. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2490. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2491. }
  2492. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2493. (&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)]);
  2494. }
  2495. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2496. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2497. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2498. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2499. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2500. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2501. #pragma unroll
  2502. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2503. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2504. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2505. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2506. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2507. }
  2508. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2509. }
  2510. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2511. (void)x_qh; (void)x_sc;
  2512. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2513. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2514. *x_ql = tile_x_ql;
  2515. *x_dm = tile_x_dm;
  2516. }
  2517. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2518. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2519. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2520. (void)x_qh; (void)x_sc;
  2521. GGML_CUDA_ASSUME(i_offset >= 0);
  2522. GGML_CUDA_ASSUME(i_offset < nwarps);
  2523. GGML_CUDA_ASSUME(k >= 0);
  2524. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2525. const int kbx = k / QI5_1;
  2526. const int kqsx = k % QI5_1;
  2527. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2528. #pragma unroll
  2529. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2530. int i = i0 + i_offset;
  2531. if (need_check) {
  2532. i = min(i, i_max);
  2533. }
  2534. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2535. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2536. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2537. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2538. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2539. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2540. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2541. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2542. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2543. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2544. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2545. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2546. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2547. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2548. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2549. }
  2550. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2551. const int kbxd = k % blocks_per_tile_x_row;
  2552. #pragma unroll
  2553. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2554. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2555. if (need_check) {
  2556. i = min(i, i_max);
  2557. }
  2558. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2559. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2560. }
  2561. }
  2562. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2563. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2564. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2565. (void)x_qh; (void)x_sc;
  2566. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2567. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2568. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2569. #pragma unroll
  2570. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2571. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2572. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2573. }
  2574. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2575. (&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)]);
  2576. }
  2577. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2578. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2579. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2580. int v[VDR_Q8_0_Q8_1_MMVQ];
  2581. int u[VDR_Q8_0_Q8_1_MMVQ];
  2582. #pragma unroll
  2583. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2584. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2585. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2586. }
  2587. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2588. }
  2589. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2590. (void)x_qh; (void)x_sc;
  2591. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2592. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2593. *x_ql = tile_x_qs;
  2594. *x_dm = (half2 *) tile_x_d;
  2595. }
  2596. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2597. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2598. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2599. (void)x_qh; (void)x_sc;
  2600. GGML_CUDA_ASSUME(i_offset >= 0);
  2601. GGML_CUDA_ASSUME(i_offset < nwarps);
  2602. GGML_CUDA_ASSUME(k >= 0);
  2603. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2604. const int kbx = k / QI8_0;
  2605. const int kqsx = k % QI8_0;
  2606. float * x_dmf = (float *) x_dm;
  2607. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2608. #pragma unroll
  2609. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2610. int i = i0 + i_offset;
  2611. if (need_check) {
  2612. i = min(i, i_max);
  2613. }
  2614. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2615. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2616. }
  2617. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2618. const int kbxd = k % blocks_per_tile_x_row;
  2619. #pragma unroll
  2620. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2621. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2622. if (need_check) {
  2623. i = min(i, i_max);
  2624. }
  2625. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2626. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2627. }
  2628. }
  2629. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2630. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2631. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2632. (void)x_qh; (void)x_sc;
  2633. const float * x_dmf = (const float *) x_dm;
  2634. const float * y_df = (const float *) y_ds;
  2635. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2636. (&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],
  2637. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2638. }
  2639. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2640. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2641. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2642. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2643. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2644. const uint8_t * scales = bq2_K->scales + scale_offset;
  2645. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2646. int u[QR2_K];
  2647. float d8[QR2_K];
  2648. #pragma unroll
  2649. for (int i = 0; i < QR2_K; ++ i) {
  2650. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2651. d8[i] = __low2float(bq8_1[bq8_offset + i].ds);
  2652. }
  2653. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2654. }
  2655. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2656. (void)x_qh;
  2657. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2658. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2659. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2660. *x_ql = tile_x_ql;
  2661. *x_dm = tile_x_dm;
  2662. *x_sc = tile_x_sc;
  2663. }
  2664. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2665. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2666. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2667. (void)x_qh;
  2668. GGML_CUDA_ASSUME(i_offset >= 0);
  2669. GGML_CUDA_ASSUME(i_offset < nwarps);
  2670. GGML_CUDA_ASSUME(k >= 0);
  2671. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2672. const int kbx = k / QI2_K;
  2673. const int kqsx = k % QI2_K;
  2674. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2675. #pragma unroll
  2676. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2677. int i = i0 + i_offset;
  2678. if (need_check) {
  2679. i = min(i, i_max);
  2680. }
  2681. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2682. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2683. }
  2684. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2685. const int kbxd = k % blocks_per_tile_x_row;
  2686. #pragma unroll
  2687. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2688. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2689. if (need_check) {
  2690. i = min(i, i_max);
  2691. }
  2692. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2693. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2694. }
  2695. #pragma unroll
  2696. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2697. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2698. if (need_check) {
  2699. i = min(i, i_max);
  2700. }
  2701. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2702. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2703. }
  2704. }
  2705. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2706. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2707. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2708. (void)x_qh;
  2709. const int kbx = k / QI2_K;
  2710. const int ky = (k % QI2_K) * QR2_K;
  2711. const float * y_df = (const float *) y_ds;
  2712. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2713. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2714. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2715. #pragma unroll
  2716. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2717. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2718. }
  2719. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2720. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2721. 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]);
  2722. }
  2723. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2724. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2725. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2726. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2727. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2728. const float d = bq3_K->d;
  2729. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2730. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2731. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2732. int u[QR3_K];
  2733. float d8[QR3_K];
  2734. #pragma unroll
  2735. for (int i = 0; i < QR3_K; ++i) {
  2736. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2737. d8[i] = __low2float(bq8_1[bq8_offset + i].ds);
  2738. }
  2739. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2740. }
  2741. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2742. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2743. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2744. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2745. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2746. *x_ql = tile_x_ql;
  2747. *x_dm = tile_x_dm;
  2748. *x_qh = tile_x_qh;
  2749. *x_sc = tile_x_sc;
  2750. }
  2751. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2752. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2753. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2754. GGML_CUDA_ASSUME(i_offset >= 0);
  2755. GGML_CUDA_ASSUME(i_offset < nwarps);
  2756. GGML_CUDA_ASSUME(k >= 0);
  2757. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2758. const int kbx = k / QI3_K;
  2759. const int kqsx = k % QI3_K;
  2760. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2761. #pragma unroll
  2762. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2763. int i = i0 + i_offset;
  2764. if (need_check) {
  2765. i = min(i, i_max);
  2766. }
  2767. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2768. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2769. }
  2770. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2771. const int kbxd = k % blocks_per_tile_x_row;
  2772. float * x_dmf = (float *) x_dm;
  2773. #pragma unroll
  2774. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2775. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2776. if (need_check) {
  2777. i = min(i, i_max);
  2778. }
  2779. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2780. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2781. }
  2782. #pragma unroll
  2783. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2784. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2785. if (need_check) {
  2786. i = min(i, i_max);
  2787. }
  2788. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2789. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2790. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2791. }
  2792. #pragma unroll
  2793. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2794. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2795. if (need_check) {
  2796. i = min(i, i_max);
  2797. }
  2798. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2799. const int ksc = k % (QI3_K/4);
  2800. const int ksc_low = ksc % (QI3_K/8);
  2801. const int shift_low = 4 * (ksc / (QI3_K/8));
  2802. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2803. const int ksc_high = QI3_K/8;
  2804. const int shift_high = 2 * ksc;
  2805. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2806. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2807. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2808. }
  2809. }
  2810. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2811. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2812. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2813. const int kbx = k / QI3_K;
  2814. const int ky = (k % QI3_K) * QR3_K;
  2815. const float * x_dmf = (const float *) x_dm;
  2816. const float * y_df = (const float *) y_ds;
  2817. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2818. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2819. #pragma unroll
  2820. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2821. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2822. const int shift = 2 * ((ky % 32) / 8);
  2823. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2824. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2825. const int vlh = (vh << 2) & 0x04040404;
  2826. v[l] = __vsubss4(vll, vlh);
  2827. }
  2828. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2829. 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]);
  2830. }
  2831. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2832. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2833. #ifndef GGML_QKK_64
  2834. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2835. int v[2];
  2836. int u[2*QR4_K];
  2837. float d8[QR4_K];
  2838. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2839. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2840. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2841. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2842. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2843. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2844. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2845. v[0] = q4[0];
  2846. v[1] = q4[4];
  2847. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2848. uint16_t aux[2];
  2849. const int j = bq8_offset/2;
  2850. if (j < 2) {
  2851. aux[0] = scales[j+0] & 0x3f3f;
  2852. aux[1] = scales[j+2] & 0x3f3f;
  2853. } else {
  2854. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2855. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2856. }
  2857. const uint8_t * sc = (const uint8_t *)aux;
  2858. const uint8_t * m = sc + 2;
  2859. for (int i = 0; i < QR4_K; ++i) {
  2860. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2861. d8[i] = __low2float(bq8i->ds);
  2862. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2863. u[2*i+0] = q8[0];
  2864. u[2*i+1] = q8[4];
  2865. }
  2866. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2867. #else
  2868. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2869. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2870. float sumf_d = 0.0f;
  2871. float sumf_m = 0.0f;
  2872. uint16_t aux16[2];
  2873. const uint8_t * s = (const uint8_t *)aux16;
  2874. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2875. aux16[0] = a[0] & 0x0f0f;
  2876. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2877. const float dall = bq4_K->dm[0];
  2878. const float dmin = bq4_K->dm[1];
  2879. const float d8_1 = __low2float(bq8_1[0].ds);
  2880. const float d8_2 = __low2float(bq8_1[1].ds);
  2881. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2882. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2883. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2884. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2885. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2886. const int v1 = q4[0];
  2887. const int v2 = q4[4];
  2888. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2889. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2890. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2891. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2892. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2893. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2894. return dall * sumf_d - dmin * sumf_m;
  2895. #else
  2896. NO_DEVICE_CODE;
  2897. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2898. #endif
  2899. }
  2900. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2901. (void)x_qh;
  2902. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2903. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2904. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2905. *x_ql = tile_x_ql;
  2906. *x_dm = tile_x_dm;
  2907. *x_sc = tile_x_sc;
  2908. }
  2909. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2910. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2911. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2912. (void)x_qh;
  2913. GGML_CUDA_ASSUME(i_offset >= 0);
  2914. GGML_CUDA_ASSUME(i_offset < nwarps);
  2915. GGML_CUDA_ASSUME(k >= 0);
  2916. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2917. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2918. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2919. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2920. #pragma unroll
  2921. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2922. int i = i0 + i_offset;
  2923. if (need_check) {
  2924. i = min(i, i_max);
  2925. }
  2926. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2927. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2928. }
  2929. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2930. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2931. #pragma unroll
  2932. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2933. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2934. if (need_check) {
  2935. i = min(i, i_max);
  2936. }
  2937. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2938. #if QK_K == 256
  2939. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2940. #else
  2941. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2942. #endif
  2943. }
  2944. #pragma unroll
  2945. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2946. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2947. if (need_check) {
  2948. i = min(i, i_max);
  2949. }
  2950. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2951. const int * scales = (const int *) bxi->scales;
  2952. const int ksc = k % (WARP_SIZE/8);
  2953. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2954. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2955. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2956. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2957. }
  2958. }
  2959. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2960. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2961. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2962. (void)x_qh;
  2963. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2964. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2965. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2966. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2967. }
  2968. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2969. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2970. #ifndef GGML_QKK_64
  2971. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2972. int vl[2];
  2973. int vh[2];
  2974. int u[2*QR5_K];
  2975. float d8[QR5_K];
  2976. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2977. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2978. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2979. vl[0] = ql[0];
  2980. vl[1] = ql[4];
  2981. vh[0] = qh[0] >> bq8_offset;
  2982. vh[1] = qh[4] >> bq8_offset;
  2983. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2984. uint16_t aux[2];
  2985. const int j = bq8_offset/2;
  2986. if (j < 2) {
  2987. aux[0] = scales[j+0] & 0x3f3f;
  2988. aux[1] = scales[j+2] & 0x3f3f;
  2989. } else {
  2990. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2991. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2992. }
  2993. const uint8_t * sc = (const uint8_t *)aux;
  2994. const uint8_t * m = sc + 2;
  2995. #pragma unroll
  2996. for (int i = 0; i < QR5_K; ++i) {
  2997. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2998. d8[i] = __low2float(bq8i->ds);
  2999. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3000. u[2*i+0] = q8[0];
  3001. u[2*i+1] = q8[4];
  3002. }
  3003. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3004. #else
  3005. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3006. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3007. const int8_t * s = bq5_K->scales;
  3008. const float d = bq5_K->d;
  3009. const float d8_1 = __low2half(bq8_1[0].ds);
  3010. const float d8_2 = __low2half(bq8_1[1].ds);
  3011. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3012. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3013. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3014. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3015. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3016. const int vl1 = ql[0];
  3017. const int vl2 = ql[4];
  3018. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3019. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3020. const int in = step%8; // 0, 4, 0, 4
  3021. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3022. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3023. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3024. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3025. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3026. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3027. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3028. return d * sumf_d;
  3029. #else
  3030. NO_DEVICE_CODE;
  3031. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3032. #endif
  3033. }
  3034. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3035. (void)x_qh;
  3036. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3037. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3038. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3039. *x_ql = tile_x_ql;
  3040. *x_dm = tile_x_dm;
  3041. *x_sc = tile_x_sc;
  3042. }
  3043. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3044. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3045. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3046. (void)x_qh;
  3047. GGML_CUDA_ASSUME(i_offset >= 0);
  3048. GGML_CUDA_ASSUME(i_offset < nwarps);
  3049. GGML_CUDA_ASSUME(k >= 0);
  3050. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3051. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3052. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3053. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3054. #pragma unroll
  3055. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3056. int i = i0 + i_offset;
  3057. if (need_check) {
  3058. i = min(i, i_max);
  3059. }
  3060. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3061. const int ky = QR5_K*kqsx;
  3062. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3063. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3064. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3065. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3066. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3067. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3068. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3069. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3070. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3071. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3072. }
  3073. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3074. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3075. #pragma unroll
  3076. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3077. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3078. if (need_check) {
  3079. i = min(i, i_max);
  3080. }
  3081. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3082. #if QK_K == 256
  3083. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3084. #endif
  3085. }
  3086. #pragma unroll
  3087. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3088. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3089. if (need_check) {
  3090. i = min(i, i_max);
  3091. }
  3092. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3093. const int * scales = (const int *) bxi->scales;
  3094. const int ksc = k % (WARP_SIZE/8);
  3095. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3096. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3097. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3098. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3099. }
  3100. }
  3101. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3102. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3103. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3104. (void)x_qh;
  3105. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3106. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3107. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3108. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3109. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3110. }
  3111. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3112. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3113. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3114. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3115. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3116. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3117. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3118. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3119. const int8_t * scales = bq6_K->scales + scale_offset;
  3120. int u[QR6_K];
  3121. float d8[QR6_K];
  3122. #pragma unroll
  3123. for (int i = 0; i < QR6_K; ++i) {
  3124. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3125. d8[i] = __low2float(bq8_1[bq8_offset + 2*i].ds);
  3126. }
  3127. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3128. }
  3129. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3130. (void)x_qh;
  3131. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3132. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3133. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3134. *x_ql = tile_x_ql;
  3135. *x_dm = tile_x_dm;
  3136. *x_sc = tile_x_sc;
  3137. }
  3138. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3139. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3140. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3141. (void)x_qh;
  3142. GGML_CUDA_ASSUME(i_offset >= 0);
  3143. GGML_CUDA_ASSUME(i_offset < nwarps);
  3144. GGML_CUDA_ASSUME(k >= 0);
  3145. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3146. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3147. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3148. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3149. #pragma unroll
  3150. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3151. int i = i0 + i_offset;
  3152. if (need_check) {
  3153. i = min(i, i_max);
  3154. }
  3155. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3156. const int ky = QR6_K*kqsx;
  3157. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3158. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3159. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3160. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3161. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3162. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3163. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3164. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3165. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3166. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3167. }
  3168. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3169. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3170. float * x_dmf = (float *) x_dm;
  3171. #pragma unroll
  3172. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3173. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3174. if (need_check) {
  3175. i = min(i, i_max);
  3176. }
  3177. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3178. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3179. }
  3180. #pragma unroll
  3181. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3182. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3183. if (need_check) {
  3184. i = min(i, i_max);
  3185. }
  3186. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3187. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3188. }
  3189. }
  3190. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3191. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3192. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3193. (void)x_qh;
  3194. const float * x_dmf = (const float *) x_dm;
  3195. const float * y_df = (const float *) y_ds;
  3196. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3197. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3198. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3199. 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]);
  3200. }
  3201. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3202. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3203. #if QK_K == 256
  3204. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3205. #if QR2_XXS == 8
  3206. const int ib32 = iqs;
  3207. const uint16_t * q2 = bq2->qs + 4*ib32;
  3208. const uint8_t * aux8 = (const uint8_t *)q2;
  3209. const int8_t * q8 = bq8_1[ib32].qs;
  3210. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3211. int sumi = 0;
  3212. for (int l = 0; l < 4; ++l) {
  3213. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3214. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3215. for (int j = 0; j < 8; ++j) {
  3216. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3217. }
  3218. q8 += 8;
  3219. aux32 >>= 7;
  3220. }
  3221. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3222. return d * sumi;
  3223. #else
  3224. // iqs is 0...15
  3225. const int ib32 = iqs/2;
  3226. const int il = iqs%2;
  3227. const uint16_t * q2 = bq2->qs + 4*ib32;
  3228. const uint8_t * aux8 = (const uint8_t *)q2;
  3229. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  3230. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  3231. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3232. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3233. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3234. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3235. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3236. int sumi1 = 0, sumi2 = 0;
  3237. for (int j = 0; j < 8; ++j) {
  3238. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3239. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3240. }
  3241. return d * (sumi1 + sumi2);
  3242. #endif
  3243. #else
  3244. assert(false);
  3245. return 0.f;
  3246. #endif
  3247. }
  3248. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  3249. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3250. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3251. #if QK_K == 256
  3252. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  3253. const int ib32 = iqs;
  3254. const uint16_t * q2 = bq2->qs + 4*ib32;
  3255. const int8_t * q8 = bq8_1[ib32].qs;
  3256. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3257. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3258. int sumi1 = 0;
  3259. for (int l = 0; l < 2; ++l) {
  3260. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3261. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3262. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3263. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3264. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3265. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3266. q8 += 8;
  3267. }
  3268. int sumi2 = 0;
  3269. for (int l = 2; l < 4; ++l) {
  3270. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3271. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3272. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3273. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3274. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3275. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3276. q8 += 8;
  3277. }
  3278. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3279. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3280. #else
  3281. (void) ksigns64;
  3282. assert(false);
  3283. return 0.f;
  3284. #endif
  3285. #else
  3286. (void) ksigns64;
  3287. assert(false);
  3288. return 0.f;
  3289. #endif
  3290. }
  3291. // TODO
  3292. static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(
  3293. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3294. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3295. #if QK_K == 256
  3296. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  3297. const int ib32 = iqs;
  3298. const int8_t * q8 = bq8_1[ib32].qs;
  3299. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  3300. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3301. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3302. int sumi1 = 0;
  3303. for (int l = 0; l < 2; ++l) {
  3304. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  3305. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  3306. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  3307. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  3308. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  3309. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3310. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3311. q8 += 8;
  3312. }
  3313. int sumi2 = 0;
  3314. for (int l = 2; l < 4; ++l) {
  3315. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  3316. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  3317. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  3318. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  3319. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  3320. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3321. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3322. q8 += 8;
  3323. }
  3324. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3325. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3326. #else
  3327. (void) ksigns64;
  3328. assert(false);
  3329. return 0.f;
  3330. #endif
  3331. #else
  3332. (void) ksigns64;
  3333. assert(false);
  3334. return 0.f;
  3335. #endif
  3336. }
  3337. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  3338. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3339. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3340. #if QK_K == 256
  3341. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  3342. const int ib32 = iqs;
  3343. const uint8_t * q3 = bq2->qs + 8*ib32;
  3344. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  3345. const int8_t * q8 = bq8_1[ib32].qs;
  3346. uint32_t aux32 = gas[0] | (gas[1] << 16);
  3347. int sumi = 0;
  3348. for (int l = 0; l < 4; ++l) {
  3349. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  3350. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  3351. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  3352. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  3353. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  3354. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3355. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3356. q8 += 8;
  3357. aux32 >>= 7;
  3358. }
  3359. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  3360. return d * sumi;
  3361. #else
  3362. assert(false);
  3363. return 0.f;
  3364. #endif
  3365. #else
  3366. assert(false);
  3367. return 0.f;
  3368. #endif
  3369. }
  3370. // TODO: don't use lookup table for signs
  3371. static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(
  3372. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3373. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3374. #if QK_K == 256
  3375. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  3376. const int ib32 = iqs;
  3377. const uint8_t * qs = bq2->qs + 8*ib32;
  3378. const int8_t * q8 = bq8_1[ib32].qs;
  3379. int sumi = 0;
  3380. for (int l = 0; l < 4; ++l) {
  3381. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  3382. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  3383. uint32_t signs0 = __vcmpeq4(((bq2->signs[4*ib32+l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  3384. uint32_t signs1 = __vcmpeq4(((bq2->signs[4*ib32+l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  3385. const int grid_l = __vsub4(grid1[0] ^ signs0, signs0);
  3386. const int grid_h = __vsub4(grid2[0] ^ signs1, signs1);
  3387. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3388. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3389. q8 += 8;
  3390. }
  3391. const float d = (float)bq2->d * (1 + 2*((bq2->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * __low2float(bq8_1[ib32].ds);
  3392. return d * sumi;
  3393. #else
  3394. assert(false);
  3395. return 0.f;
  3396. #endif
  3397. #else
  3398. assert(false);
  3399. return 0.f;
  3400. #endif
  3401. }
  3402. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  3403. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3404. #if QK_K == 256
  3405. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  3406. const int ib32 = iqs;
  3407. int sumi = 0;
  3408. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3409. const int * q8 = (const int *)bq8_1[ib32].qs;
  3410. for (int l = 0; l < 4; ++l) {
  3411. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  3412. int grid0 = grid[0] & 0x0f0f0f0f;
  3413. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  3414. sumi = __dp4a(q8[2*l+1], grid1, __dp4a(q8[2*l+0], grid0, sumi));
  3415. }
  3416. #else
  3417. const int8_t * q8 = bq8_1[ib32].qs;
  3418. for (int l = 0; l < 4; ++l) {
  3419. const uint8_t * grid = (const uint8_t *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  3420. for (int j = 0; j < 4; ++j) {
  3421. sumi += q8[j] * (grid[j] & 0xf) + q8[j+4] * (grid[j] >> 4);
  3422. }
  3423. q8 += 8;
  3424. }
  3425. #endif
  3426. const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA;
  3427. const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1);
  3428. const float d = d1q * __low2float (bq8_1[ib32].ds);
  3429. const float m = d1q * __high2float(bq8_1[ib32].ds);
  3430. return d * sumi + m * delta;
  3431. #else
  3432. assert(false);
  3433. return 0.f;
  3434. #endif
  3435. }
  3436. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3437. static __device__ __forceinline__ void get_int_from_table_16(const uint32_t & q4, const uint8_t * values,
  3438. int & val1, int & val2) {
  3439. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  3440. aux32 = q4 & 0x0f0f0f0f;
  3441. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  3442. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  3443. val1 = v1 | (v2 << 16);
  3444. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  3445. v1 = values[q8[0]] | (values[q8[1]] << 8);
  3446. v2 = values[q8[2]] | (values[q8[3]] << 8);
  3447. val2 = v1 | (v2 << 16);
  3448. }
  3449. #endif
  3450. static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
  3451. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3452. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  3453. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3454. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  3455. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  3456. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  3457. int v1, v2;
  3458. int sumi1 = 0, sumi2 = 0;
  3459. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3460. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  3461. get_int_from_table_16(aux, values, v1, v2);
  3462. sumi1 = __dp4a(v1, q8[l+0], sumi1);
  3463. sumi2 = __dp4a(v2, q8[l+4], sumi2);
  3464. }
  3465. #else
  3466. const uint8_t * q4 = bq->qs + 4*iqs;
  3467. const int8_t * q8 = bq8_1->qs + 4*iqs;
  3468. int sumi1 = 0, sumi2 = 0;
  3469. for (int l = 0; l < 4*VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3470. sumi1 += q8[l+ 0] * kvalues_iq4nl[q4[l] & 0xf];
  3471. sumi2 += q8[l+16] * kvalues_iq4nl[q4[l] >> 4];
  3472. }
  3473. #endif
  3474. const float d = (float)bq->d * __low2float(bq8_1->ds);
  3475. return d * (sumi1 + sumi2);
  3476. }
  3477. static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(
  3478. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3479. #if QK_K == 256
  3480. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3481. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  3482. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  3483. //// iqs is 0...7
  3484. //const int ib64 = iqs/2;
  3485. //const int il = iqs%2;
  3486. //const int32_t * q8_1 = (const int *)bq8_1[2*ib64+0].qs + 2*il;
  3487. //const int32_t * q8_2 = (const int *)bq8_1[2*ib64+1].qs + 2*il;
  3488. //const uint32_t * q4_1 = (const uint32_t *)bq4->qs + 8*ib64 + 2*il;
  3489. //const uint32_t * q4_2 = q4_1 + 4;
  3490. //const int8_t ls1 = (bq4->scales_l[ib64] & 0xf) | (((bq4->scales_h >> (4*ib64+0)) & 3) << 4);
  3491. //const int8_t ls2 = (bq4->scales_l[ib64] >> 4) | (((bq4->scales_h >> (4*ib64+2)) & 3) << 4);
  3492. //const float d1 = (float)bq4->d * (ls1 - 32) * __low2float(bq8_1[2*ib64+0].ds);
  3493. //const float d2 = (float)bq4->d * (ls2 - 32) * __low2float(bq8_1[2*ib64+1].ds);
  3494. //int v1, v2;
  3495. //int sumi1 = 0, sumi2 = 0;
  3496. //for (int j = 0; j < 2; ++j) {
  3497. // get_int_from_table_16(q4_1[j], values, v1, v2);
  3498. // sumi1 = __dp4a(v2, q8_1[j+4], __dp4a(v1, q8_1[j+0], sumi1));
  3499. // get_int_from_table_16(q4_2[j], values, v1, v2);
  3500. // sumi2 = __dp4a(v2, q8_2[j+4], __dp4a(v1, q8_2[j+0], sumi2));
  3501. //}
  3502. //return d1 * sumi1 + d2 * sumi2;
  3503. // iqs is 0...7
  3504. const int ib32 = iqs;
  3505. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  3506. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  3507. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  3508. const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  3509. int v1, v2;
  3510. int sumi1 = 0, sumi2 = 0;
  3511. for (int j = 0; j < 4; ++j) {
  3512. get_int_from_table_16(q4[j], values, v1, v2);
  3513. sumi1 = __dp4a(v1, q8[j+0], sumi1);
  3514. sumi2 = __dp4a(v2, q8[j+4], sumi2);
  3515. }
  3516. return d * (sumi1 + sumi2);
  3517. //// iqs is 0...15
  3518. //const int ib32 = iqs/2;
  3519. //const int il = iqs%2;
  3520. //const int32_t * q8 = (const int *)bq8_1[ib32].qs + 2*il;
  3521. //const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32 + 2*il;
  3522. //const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  3523. //const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  3524. //int v1, v2;
  3525. //int sumi1 = 0, sumi2 = 0;
  3526. //for (int j = 0; j < 2; ++j) {
  3527. // get_int_from_table_16(q4[j], values, v1, v2);
  3528. // sumi1 = __dp4a(v1, q8[j+0], sumi1);
  3529. // sumi2 = __dp4a(v2, q8[j+4], sumi2);
  3530. //}
  3531. //return d * (sumi1 + sumi2);
  3532. #else
  3533. assert(false);
  3534. return 0.f;
  3535. #endif
  3536. #else
  3537. return vec_dot_iq4_xs_q8_1(vbq, bq8_1, iqs);
  3538. #endif
  3539. }
  3540. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3541. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3542. static __device__ __forceinline__ void mul_mat_q(
  3543. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3544. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3545. const block_q_t * x = (const block_q_t *) vx;
  3546. const block_q8_1 * y = (const block_q8_1 *) vy;
  3547. const int blocks_per_row_x = ncols_x / qk;
  3548. const int blocks_per_col_y = nrows_y / QK8_1;
  3549. const int blocks_per_warp = WARP_SIZE / qi;
  3550. const int & ncols_dst = ncols_y;
  3551. const int row_dst_0 = blockIdx.x*mmq_y;
  3552. const int & row_x_0 = row_dst_0;
  3553. const int col_dst_0 = blockIdx.y*mmq_x;
  3554. const int & col_y_0 = col_dst_0;
  3555. int * tile_x_ql = nullptr;
  3556. half2 * tile_x_dm = nullptr;
  3557. int * tile_x_qh = nullptr;
  3558. int * tile_x_sc = nullptr;
  3559. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3560. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3561. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3562. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3563. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3564. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3565. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3566. #pragma unroll
  3567. for (int ir = 0; ir < qr; ++ir) {
  3568. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3569. const int kbxd = kqs / QI8_1;
  3570. #pragma unroll
  3571. for (int i = 0; i < mmq_x; i += nwarps) {
  3572. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3573. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3574. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3575. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3576. }
  3577. #pragma unroll
  3578. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3579. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3580. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3581. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3582. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3583. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3584. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3585. if (need_sum) {
  3586. *dsi_dst = *dsi_src;
  3587. } else {
  3588. float * dfi_dst = (float *) dsi_dst;
  3589. *dfi_dst = __low2float(*dsi_src);
  3590. }
  3591. }
  3592. __syncthreads();
  3593. // #pragma unroll // unrolling this loop causes too much register pressure
  3594. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3595. #pragma unroll
  3596. for (int j = 0; j < mmq_x; j += nwarps) {
  3597. #pragma unroll
  3598. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3599. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3600. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3601. threadIdx.x + i, threadIdx.y + j, k);
  3602. }
  3603. }
  3604. }
  3605. __syncthreads();
  3606. }
  3607. }
  3608. #pragma unroll
  3609. for (int j = 0; j < mmq_x; j += nwarps) {
  3610. const int col_dst = col_dst_0 + j + threadIdx.y;
  3611. if (col_dst >= ncols_dst) {
  3612. return;
  3613. }
  3614. #pragma unroll
  3615. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3616. const int row_dst = row_dst_0 + threadIdx.x + i;
  3617. if (row_dst >= nrows_dst) {
  3618. continue;
  3619. }
  3620. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3621. }
  3622. }
  3623. }
  3624. #define MMQ_X_Q4_0_RDNA2 64
  3625. #define MMQ_Y_Q4_0_RDNA2 128
  3626. #define NWARPS_Q4_0_RDNA2 8
  3627. #define MMQ_X_Q4_0_RDNA1 64
  3628. #define MMQ_Y_Q4_0_RDNA1 64
  3629. #define NWARPS_Q4_0_RDNA1 8
  3630. #if defined(CUDA_USE_TENSOR_CORES)
  3631. #define MMQ_X_Q4_0_AMPERE 4
  3632. #define MMQ_Y_Q4_0_AMPERE 32
  3633. #define NWARPS_Q4_0_AMPERE 4
  3634. #else
  3635. #define MMQ_X_Q4_0_AMPERE 64
  3636. #define MMQ_Y_Q4_0_AMPERE 128
  3637. #define NWARPS_Q4_0_AMPERE 4
  3638. #endif
  3639. #define MMQ_X_Q4_0_PASCAL 64
  3640. #define MMQ_Y_Q4_0_PASCAL 64
  3641. #define NWARPS_Q4_0_PASCAL 8
  3642. template <bool need_check> static __global__ void
  3643. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3644. #if defined(RDNA3) || defined(RDNA2)
  3645. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3646. #endif // defined(RDNA3) || defined(RDNA2)
  3647. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3648. mul_mat_q4_0(
  3649. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3650. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3651. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3652. #if defined(RDNA3) || defined(RDNA2)
  3653. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3654. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3655. const int nwarps = NWARPS_Q4_0_RDNA2;
  3656. #else
  3657. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3658. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3659. const int nwarps = NWARPS_Q4_0_RDNA1;
  3660. #endif // defined(RDNA3) || defined(RDNA2)
  3661. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3662. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3663. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3664. #elif __CUDA_ARCH__ >= CC_VOLTA
  3665. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3666. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3667. const int nwarps = NWARPS_Q4_0_AMPERE;
  3668. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3669. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3670. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3671. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3672. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3673. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3674. const int nwarps = NWARPS_Q4_0_PASCAL;
  3675. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3676. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3677. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3678. #else
  3679. (void) vec_dot_q4_0_q8_1_mul_mat;
  3680. NO_DEVICE_CODE;
  3681. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3682. }
  3683. #define MMQ_X_Q4_1_RDNA2 64
  3684. #define MMQ_Y_Q4_1_RDNA2 128
  3685. #define NWARPS_Q4_1_RDNA2 8
  3686. #define MMQ_X_Q4_1_RDNA1 64
  3687. #define MMQ_Y_Q4_1_RDNA1 64
  3688. #define NWARPS_Q4_1_RDNA1 8
  3689. #if defined(CUDA_USE_TENSOR_CORES)
  3690. #define MMQ_X_Q4_1_AMPERE 4
  3691. #define MMQ_Y_Q4_1_AMPERE 32
  3692. #define NWARPS_Q4_1_AMPERE 4
  3693. #else
  3694. #define MMQ_X_Q4_1_AMPERE 64
  3695. #define MMQ_Y_Q4_1_AMPERE 128
  3696. #define NWARPS_Q4_1_AMPERE 4
  3697. #endif
  3698. #define MMQ_X_Q4_1_PASCAL 64
  3699. #define MMQ_Y_Q4_1_PASCAL 64
  3700. #define NWARPS_Q4_1_PASCAL 8
  3701. template <bool need_check> static __global__ void
  3702. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3703. #if defined(RDNA3) || defined(RDNA2)
  3704. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3705. #endif // defined(RDNA3) || defined(RDNA2)
  3706. #elif __CUDA_ARCH__ < CC_VOLTA
  3707. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3708. #endif // __CUDA_ARCH__ < CC_VOLTA
  3709. mul_mat_q4_1(
  3710. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3711. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3712. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3713. #if defined(RDNA3) || defined(RDNA2)
  3714. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3715. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3716. const int nwarps = NWARPS_Q4_1_RDNA2;
  3717. #else
  3718. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3719. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3720. const int nwarps = NWARPS_Q4_1_RDNA1;
  3721. #endif // defined(RDNA3) || defined(RDNA2)
  3722. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3723. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3724. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3725. #elif __CUDA_ARCH__ >= CC_VOLTA
  3726. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3727. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3728. const int nwarps = NWARPS_Q4_1_AMPERE;
  3729. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3730. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3731. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3732. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3733. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3734. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3735. const int nwarps = NWARPS_Q4_1_PASCAL;
  3736. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3737. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3738. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3739. #else
  3740. (void) vec_dot_q4_1_q8_1_mul_mat;
  3741. NO_DEVICE_CODE;
  3742. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3743. }
  3744. #define MMQ_X_Q5_0_RDNA2 64
  3745. #define MMQ_Y_Q5_0_RDNA2 128
  3746. #define NWARPS_Q5_0_RDNA2 8
  3747. #define MMQ_X_Q5_0_RDNA1 64
  3748. #define MMQ_Y_Q5_0_RDNA1 64
  3749. #define NWARPS_Q5_0_RDNA1 8
  3750. #if defined(CUDA_USE_TENSOR_CORES)
  3751. #define MMQ_X_Q5_0_AMPERE 4
  3752. #define MMQ_Y_Q5_0_AMPERE 32
  3753. #define NWARPS_Q5_0_AMPERE 4
  3754. #else
  3755. #define MMQ_X_Q5_0_AMPERE 128
  3756. #define MMQ_Y_Q5_0_AMPERE 64
  3757. #define NWARPS_Q5_0_AMPERE 4
  3758. #endif
  3759. #define MMQ_X_Q5_0_PASCAL 64
  3760. #define MMQ_Y_Q5_0_PASCAL 64
  3761. #define NWARPS_Q5_0_PASCAL 8
  3762. template <bool need_check> static __global__ void
  3763. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3764. #if defined(RDNA3) || defined(RDNA2)
  3765. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3766. #endif // defined(RDNA3) || defined(RDNA2)
  3767. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3768. mul_mat_q5_0(
  3769. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3770. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3771. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3772. #if defined(RDNA3) || defined(RDNA2)
  3773. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3774. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3775. const int nwarps = NWARPS_Q5_0_RDNA2;
  3776. #else
  3777. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3778. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3779. const int nwarps = NWARPS_Q5_0_RDNA1;
  3780. #endif // defined(RDNA3) || defined(RDNA2)
  3781. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3782. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3783. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3784. #elif __CUDA_ARCH__ >= CC_VOLTA
  3785. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3786. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3787. const int nwarps = NWARPS_Q5_0_AMPERE;
  3788. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3789. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3790. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3791. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3792. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3793. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3794. const int nwarps = NWARPS_Q5_0_PASCAL;
  3795. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3796. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3797. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3798. #else
  3799. (void) vec_dot_q5_0_q8_1_mul_mat;
  3800. NO_DEVICE_CODE;
  3801. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3802. }
  3803. #define MMQ_X_Q5_1_RDNA2 64
  3804. #define MMQ_Y_Q5_1_RDNA2 128
  3805. #define NWARPS_Q5_1_RDNA2 8
  3806. #define MMQ_X_Q5_1_RDNA1 64
  3807. #define MMQ_Y_Q5_1_RDNA1 64
  3808. #define NWARPS_Q5_1_RDNA1 8
  3809. #if defined(CUDA_USE_TENSOR_CORES)
  3810. #define MMQ_X_Q5_1_AMPERE 4
  3811. #define MMQ_Y_Q5_1_AMPERE 32
  3812. #define NWARPS_Q5_1_AMPERE 4
  3813. #else
  3814. #define MMQ_X_Q5_1_AMPERE 128
  3815. #define MMQ_Y_Q5_1_AMPERE 64
  3816. #define NWARPS_Q5_1_AMPERE 4
  3817. #endif
  3818. #define MMQ_X_Q5_1_PASCAL 64
  3819. #define MMQ_Y_Q5_1_PASCAL 64
  3820. #define NWARPS_Q5_1_PASCAL 8
  3821. template <bool need_check> static __global__ void
  3822. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3823. #if defined(RDNA3) || defined(RDNA2)
  3824. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3825. #endif // defined(RDNA3) || defined(RDNA2)
  3826. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3827. mul_mat_q5_1(
  3828. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3829. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3830. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3831. #if defined(RDNA3) || defined(RDNA2)
  3832. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3833. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3834. const int nwarps = NWARPS_Q5_1_RDNA2;
  3835. #else
  3836. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3837. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3838. const int nwarps = NWARPS_Q5_1_RDNA1;
  3839. #endif // defined(RDNA3) || defined(RDNA2)
  3840. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3841. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3842. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3843. #elif __CUDA_ARCH__ >= CC_VOLTA
  3844. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3845. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3846. const int nwarps = NWARPS_Q5_1_AMPERE;
  3847. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3848. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3849. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3850. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3851. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3852. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3853. const int nwarps = NWARPS_Q5_1_PASCAL;
  3854. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3855. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3856. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3857. #else
  3858. (void) vec_dot_q5_1_q8_1_mul_mat;
  3859. NO_DEVICE_CODE;
  3860. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3861. }
  3862. #define MMQ_X_Q8_0_RDNA2 64
  3863. #define MMQ_Y_Q8_0_RDNA2 128
  3864. #define NWARPS_Q8_0_RDNA2 8
  3865. #define MMQ_X_Q8_0_RDNA1 64
  3866. #define MMQ_Y_Q8_0_RDNA1 64
  3867. #define NWARPS_Q8_0_RDNA1 8
  3868. #if defined(CUDA_USE_TENSOR_CORES)
  3869. #define MMQ_X_Q8_0_AMPERE 4
  3870. #define MMQ_Y_Q8_0_AMPERE 32
  3871. #define NWARPS_Q8_0_AMPERE 4
  3872. #else
  3873. #define MMQ_X_Q8_0_AMPERE 128
  3874. #define MMQ_Y_Q8_0_AMPERE 64
  3875. #define NWARPS_Q8_0_AMPERE 4
  3876. #endif
  3877. #define MMQ_X_Q8_0_PASCAL 64
  3878. #define MMQ_Y_Q8_0_PASCAL 64
  3879. #define NWARPS_Q8_0_PASCAL 8
  3880. template <bool need_check> static __global__ void
  3881. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3882. #if defined(RDNA3) || defined(RDNA2)
  3883. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3884. #endif // defined(RDNA3) || defined(RDNA2)
  3885. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3886. mul_mat_q8_0(
  3887. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3888. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3889. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3890. #if defined(RDNA3) || defined(RDNA2)
  3891. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3892. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3893. const int nwarps = NWARPS_Q8_0_RDNA2;
  3894. #else
  3895. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3896. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3897. const int nwarps = NWARPS_Q8_0_RDNA1;
  3898. #endif // defined(RDNA3) || defined(RDNA2)
  3899. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3900. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3901. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3902. #elif __CUDA_ARCH__ >= CC_VOLTA
  3903. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3904. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3905. const int nwarps = NWARPS_Q8_0_AMPERE;
  3906. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3907. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3908. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3909. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3910. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3911. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3912. const int nwarps = NWARPS_Q8_0_PASCAL;
  3913. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3914. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3915. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3916. #else
  3917. (void) vec_dot_q8_0_q8_1_mul_mat;
  3918. NO_DEVICE_CODE;
  3919. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3920. }
  3921. #define MMQ_X_Q2_K_RDNA2 64
  3922. #define MMQ_Y_Q2_K_RDNA2 128
  3923. #define NWARPS_Q2_K_RDNA2 8
  3924. #define MMQ_X_Q2_K_RDNA1 128
  3925. #define MMQ_Y_Q2_K_RDNA1 32
  3926. #define NWARPS_Q2_K_RDNA1 8
  3927. #if defined(CUDA_USE_TENSOR_CORES)
  3928. #define MMQ_X_Q2_K_AMPERE 4
  3929. #define MMQ_Y_Q2_K_AMPERE 32
  3930. #define NWARPS_Q2_K_AMPERE 4
  3931. #else
  3932. #define MMQ_X_Q2_K_AMPERE 64
  3933. #define MMQ_Y_Q2_K_AMPERE 128
  3934. #define NWARPS_Q2_K_AMPERE 4
  3935. #endif
  3936. #define MMQ_X_Q2_K_PASCAL 64
  3937. #define MMQ_Y_Q2_K_PASCAL 64
  3938. #define NWARPS_Q2_K_PASCAL 8
  3939. template <bool need_check> static __global__ void
  3940. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3941. #if defined(RDNA3) || defined(RDNA2)
  3942. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3943. #endif // defined(RDNA3) || defined(RDNA2)
  3944. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3945. mul_mat_q2_K(
  3946. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3947. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3948. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3949. #if defined(RDNA3) || defined(RDNA2)
  3950. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3951. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3952. const int nwarps = NWARPS_Q2_K_RDNA2;
  3953. #else
  3954. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3955. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3956. const int nwarps = NWARPS_Q2_K_RDNA1;
  3957. #endif // defined(RDNA3) || defined(RDNA2)
  3958. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3959. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3960. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3961. #elif __CUDA_ARCH__ >= CC_VOLTA
  3962. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3963. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3964. const int nwarps = NWARPS_Q2_K_AMPERE;
  3965. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3966. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3967. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3968. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3969. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3970. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3971. const int nwarps = NWARPS_Q2_K_PASCAL;
  3972. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3973. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3974. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3975. #else
  3976. (void) vec_dot_q2_K_q8_1_mul_mat;
  3977. NO_DEVICE_CODE;
  3978. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3979. }
  3980. #define MMQ_X_Q3_K_RDNA2 128
  3981. #define MMQ_Y_Q3_K_RDNA2 64
  3982. #define NWARPS_Q3_K_RDNA2 8
  3983. #define MMQ_X_Q3_K_RDNA1 32
  3984. #define MMQ_Y_Q3_K_RDNA1 128
  3985. #define NWARPS_Q3_K_RDNA1 8
  3986. #if defined(CUDA_USE_TENSOR_CORES)
  3987. #define MMQ_X_Q3_K_AMPERE 4
  3988. #define MMQ_Y_Q3_K_AMPERE 32
  3989. #define NWARPS_Q3_K_AMPERE 4
  3990. #else
  3991. #define MMQ_X_Q3_K_AMPERE 128
  3992. #define MMQ_Y_Q3_K_AMPERE 128
  3993. #define NWARPS_Q3_K_AMPERE 4
  3994. #endif
  3995. #define MMQ_X_Q3_K_PASCAL 64
  3996. #define MMQ_Y_Q3_K_PASCAL 64
  3997. #define NWARPS_Q3_K_PASCAL 8
  3998. template <bool need_check> static __global__ void
  3999. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4000. #if defined(RDNA3) || defined(RDNA2)
  4001. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4002. #endif // defined(RDNA3) || defined(RDNA2)
  4003. #elif __CUDA_ARCH__ < CC_VOLTA
  4004. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4005. #endif // __CUDA_ARCH__ < CC_VOLTA
  4006. mul_mat_q3_K(
  4007. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4008. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4009. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4010. #if defined(RDNA3) || defined(RDNA2)
  4011. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4012. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4013. const int nwarps = NWARPS_Q3_K_RDNA2;
  4014. #else
  4015. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4016. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4017. const int nwarps = NWARPS_Q3_K_RDNA1;
  4018. #endif // defined(RDNA3) || defined(RDNA2)
  4019. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4020. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4021. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4022. #elif __CUDA_ARCH__ >= CC_VOLTA
  4023. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4024. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4025. const int nwarps = NWARPS_Q3_K_AMPERE;
  4026. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4027. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4028. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4029. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4030. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4031. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4032. const int nwarps = NWARPS_Q3_K_PASCAL;
  4033. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4034. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4035. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4036. #else
  4037. (void) vec_dot_q3_K_q8_1_mul_mat;
  4038. NO_DEVICE_CODE;
  4039. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4040. }
  4041. #define MMQ_X_Q4_K_RDNA2 64
  4042. #define MMQ_Y_Q4_K_RDNA2 128
  4043. #define NWARPS_Q4_K_RDNA2 8
  4044. #define MMQ_X_Q4_K_RDNA1 32
  4045. #define MMQ_Y_Q4_K_RDNA1 64
  4046. #define NWARPS_Q4_K_RDNA1 8
  4047. #if defined(CUDA_USE_TENSOR_CORES)
  4048. #define MMQ_X_Q4_K_AMPERE 4
  4049. #define MMQ_Y_Q4_K_AMPERE 32
  4050. #define NWARPS_Q4_K_AMPERE 4
  4051. #else
  4052. #define MMQ_X_Q4_K_AMPERE 64
  4053. #define MMQ_Y_Q4_K_AMPERE 128
  4054. #define NWARPS_Q4_K_AMPERE 4
  4055. #endif
  4056. #define MMQ_X_Q4_K_PASCAL 64
  4057. #define MMQ_Y_Q4_K_PASCAL 64
  4058. #define NWARPS_Q4_K_PASCAL 8
  4059. template <bool need_check> static __global__ void
  4060. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4061. #if defined(RDNA3) || defined(RDNA2)
  4062. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4063. #endif // defined(RDNA3) || defined(RDNA2)
  4064. #elif __CUDA_ARCH__ < CC_VOLTA
  4065. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4066. #endif // __CUDA_ARCH__ < CC_VOLTA
  4067. mul_mat_q4_K(
  4068. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4069. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4070. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4071. #if defined(RDNA3) || defined(RDNA2)
  4072. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4073. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4074. const int nwarps = NWARPS_Q4_K_RDNA2;
  4075. #else
  4076. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4077. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4078. const int nwarps = NWARPS_Q4_K_RDNA1;
  4079. #endif // defined(RDNA3) || defined(RDNA2)
  4080. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4081. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4082. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4083. #elif __CUDA_ARCH__ >= CC_VOLTA
  4084. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4085. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4086. const int nwarps = NWARPS_Q4_K_AMPERE;
  4087. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4088. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4089. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4090. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4091. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4092. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4093. const int nwarps = NWARPS_Q4_K_PASCAL;
  4094. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4095. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4096. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4097. #else
  4098. (void) vec_dot_q4_K_q8_1_mul_mat;
  4099. NO_DEVICE_CODE;
  4100. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4101. }
  4102. #define MMQ_X_Q5_K_RDNA2 64
  4103. #define MMQ_Y_Q5_K_RDNA2 128
  4104. #define NWARPS_Q5_K_RDNA2 8
  4105. #define MMQ_X_Q5_K_RDNA1 32
  4106. #define MMQ_Y_Q5_K_RDNA1 64
  4107. #define NWARPS_Q5_K_RDNA1 8
  4108. #if defined(CUDA_USE_TENSOR_CORES)
  4109. #define MMQ_X_Q5_K_AMPERE 4
  4110. #define MMQ_Y_Q5_K_AMPERE 32
  4111. #define NWARPS_Q5_K_AMPERE 4
  4112. #else
  4113. #define MMQ_X_Q5_K_AMPERE 64
  4114. #define MMQ_Y_Q5_K_AMPERE 128
  4115. #define NWARPS_Q5_K_AMPERE 4
  4116. #endif
  4117. #define MMQ_X_Q5_K_PASCAL 64
  4118. #define MMQ_Y_Q5_K_PASCAL 64
  4119. #define NWARPS_Q5_K_PASCAL 8
  4120. template <bool need_check> static __global__ void
  4121. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4122. #if defined(RDNA3) || defined(RDNA2)
  4123. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  4124. #endif // defined(RDNA3) || defined(RDNA2)
  4125. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4126. mul_mat_q5_K(
  4127. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4128. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4129. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4130. #if defined(RDNA3) || defined(RDNA2)
  4131. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  4132. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  4133. const int nwarps = NWARPS_Q5_K_RDNA2;
  4134. #else
  4135. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  4136. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  4137. const int nwarps = NWARPS_Q5_K_RDNA1;
  4138. #endif // defined(RDNA3) || defined(RDNA2)
  4139. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4140. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4141. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4142. #elif __CUDA_ARCH__ >= CC_VOLTA
  4143. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  4144. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  4145. const int nwarps = NWARPS_Q5_K_AMPERE;
  4146. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4147. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4148. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4149. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4150. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  4151. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  4152. const int nwarps = NWARPS_Q5_K_PASCAL;
  4153. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4154. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4155. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4156. #else
  4157. (void) vec_dot_q5_K_q8_1_mul_mat;
  4158. NO_DEVICE_CODE;
  4159. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4160. }
  4161. #define MMQ_X_Q6_K_RDNA2 64
  4162. #define MMQ_Y_Q6_K_RDNA2 128
  4163. #define NWARPS_Q6_K_RDNA2 8
  4164. #define MMQ_X_Q6_K_RDNA1 32
  4165. #define MMQ_Y_Q6_K_RDNA1 64
  4166. #define NWARPS_Q6_K_RDNA1 8
  4167. #if defined(CUDA_USE_TENSOR_CORES)
  4168. #define MMQ_X_Q6_K_AMPERE 4
  4169. #define MMQ_Y_Q6_K_AMPERE 32
  4170. #define NWARPS_Q6_K_AMPERE 4
  4171. #else
  4172. #define MMQ_X_Q6_K_AMPERE 64
  4173. #define MMQ_Y_Q6_K_AMPERE 64
  4174. #define NWARPS_Q6_K_AMPERE 4
  4175. #endif
  4176. #define MMQ_X_Q6_K_PASCAL 64
  4177. #define MMQ_Y_Q6_K_PASCAL 64
  4178. #define NWARPS_Q6_K_PASCAL 8
  4179. template <bool need_check> static __global__ void
  4180. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4181. #if defined(RDNA3) || defined(RDNA2)
  4182. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4183. #endif // defined(RDNA3) || defined(RDNA2)
  4184. #elif __CUDA_ARCH__ < CC_VOLTA
  4185. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4186. #endif // __CUDA_ARCH__ < CC_VOLTA
  4187. mul_mat_q6_K(
  4188. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4189. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4190. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4191. #if defined(RDNA3) || defined(RDNA2)
  4192. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  4193. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  4194. const int nwarps = NWARPS_Q6_K_RDNA2;
  4195. #else
  4196. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  4197. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  4198. const int nwarps = NWARPS_Q6_K_RDNA1;
  4199. #endif // defined(RDNA3) || defined(RDNA2)
  4200. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4201. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4202. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4203. #elif __CUDA_ARCH__ >= CC_VOLTA
  4204. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  4205. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  4206. const int nwarps = NWARPS_Q6_K_AMPERE;
  4207. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4208. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4209. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4210. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4211. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  4212. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  4213. const int nwarps = NWARPS_Q6_K_PASCAL;
  4214. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4215. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4216. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4217. #else
  4218. (void) vec_dot_q6_K_q8_1_mul_mat;
  4219. NO_DEVICE_CODE;
  4220. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4221. }
  4222. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  4223. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4224. // tell the compiler to use as many registers as it wants, see nwarps definition below
  4225. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  4226. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4227. static __global__ void mul_mat_vec_q(
  4228. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4229. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  4230. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  4231. constexpr int nwarps = 1;
  4232. constexpr int rows_per_cuda_block = 1;
  4233. #else
  4234. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  4235. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  4236. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  4237. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  4238. const int row0 = rows_per_cuda_block*blockIdx.x;
  4239. const int blocks_per_row_x = ncols_x / qk;
  4240. const int blocks_per_col_y = nrows_y / QK8_1;
  4241. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  4242. // partial sum for each thread
  4243. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  4244. const block_q_t * x = (const block_q_t *) vx;
  4245. const block_q8_1 * y = (const block_q8_1 *) vy;
  4246. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  4247. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  4248. // x block quant index when casting the quants to int
  4249. const int kqs = vdr * (tid % (qi/vdr));
  4250. #pragma unroll
  4251. for (int j = 0; j < ncols_y; ++j) {
  4252. #pragma unroll
  4253. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4254. tmp[j][i] += vec_dot_q_cuda(
  4255. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  4256. }
  4257. }
  4258. }
  4259. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  4260. if (threadIdx.y > 0) {
  4261. #pragma unroll
  4262. for (int j = 0; j < ncols_y; ++j) {
  4263. #pragma unroll
  4264. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4265. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  4266. }
  4267. }
  4268. }
  4269. __syncthreads();
  4270. if (threadIdx.y > 0) {
  4271. return;
  4272. }
  4273. // sum up partial sums and write back result
  4274. #pragma unroll
  4275. for (int j = 0; j < ncols_y; ++j) {
  4276. #pragma unroll
  4277. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4278. #pragma unroll
  4279. for (int l = 0; l < nwarps-1; ++l) {
  4280. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  4281. }
  4282. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  4283. }
  4284. if (threadIdx.x < rows_per_cuda_block) {
  4285. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  4286. }
  4287. }
  4288. }
  4289. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  4290. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  4291. // qk = quantized weights per x block
  4292. // qr = number of quantized weights per data value in x block
  4293. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4294. if (row >= nrows) {
  4295. return;
  4296. }
  4297. const int tid = threadIdx.x;
  4298. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  4299. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  4300. const int y_offset = qr == 1 ? 1 : qk/2;
  4301. // partial sum for each thread
  4302. #ifdef GGML_CUDA_F16
  4303. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  4304. #else
  4305. float tmp = 0.0f;
  4306. #endif // GGML_CUDA_F16
  4307. for (int i = 0; i < ncols; i += iter_stride) {
  4308. const int col = i + vals_per_iter*tid;
  4309. const int ib = (row*ncols + col)/qk; // x block index
  4310. const int iqs = (col%qk)/qr; // x quant index
  4311. const int iybs = col - col%qk; // y block start index
  4312. // processing >2 values per i iter is faster for fast GPUs
  4313. #pragma unroll
  4314. for (int j = 0; j < vals_per_iter; j += 2) {
  4315. // process 2 vals per j iter
  4316. // dequantize
  4317. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  4318. dfloat2 v;
  4319. dequantize_kernel(vx, ib, iqs + j/qr, v);
  4320. // matrix multiplication
  4321. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  4322. #ifdef GGML_CUDA_F16
  4323. tmp += __hmul2(v, {
  4324. y[iybs + iqs + j/qr + 0],
  4325. y[iybs + iqs + j/qr + y_offset]
  4326. });
  4327. #else
  4328. tmp += v.x * y[iybs + iqs + j/qr + 0];
  4329. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  4330. #endif // GGML_CUDA_F16
  4331. }
  4332. }
  4333. // sum up partial sums and write back result
  4334. tmp = warp_reduce_sum(tmp);
  4335. if (tid == 0) {
  4336. #ifdef GGML_CUDA_F16
  4337. dst[row] = tmp.x + tmp.y;
  4338. #else
  4339. dst[row] = tmp;
  4340. #endif // GGML_CUDA_F16
  4341. }
  4342. }
  4343. static __global__ void mul_mat_p021_f16_f32(
  4344. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  4345. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  4346. const half * x = (const half *) vx;
  4347. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4348. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4349. const int channel_x = channel / (nchannels_y / nchannels_x);
  4350. const int nrows_y = ncols_x;
  4351. const int nrows_dst = nrows_x;
  4352. const int row_dst = row_x;
  4353. float tmp = 0.0f;
  4354. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4355. const int col_x = col_x0 + threadIdx.x;
  4356. if (col_x >= ncols_x) {
  4357. break;
  4358. }
  4359. // x is transposed and permuted
  4360. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  4361. const float xi = __half2float(x[ix]);
  4362. const int row_y = col_x;
  4363. // y is not transposed but permuted
  4364. const int iy = channel*nrows_y + row_y;
  4365. tmp += xi * y[iy];
  4366. }
  4367. // dst is not transposed and not permuted
  4368. const int idst = channel*nrows_dst + row_dst;
  4369. // sum up partial sums and write back result
  4370. tmp = warp_reduce_sum(tmp);
  4371. if (threadIdx.x == 0) {
  4372. dst[idst] = tmp;
  4373. }
  4374. }
  4375. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  4376. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  4377. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  4378. const half * x = (const half *) vx;
  4379. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4380. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4381. const int channel_x = channel / channel_x_divisor;
  4382. const int nrows_y = ncols_x;
  4383. const int nrows_dst = nrows_x;
  4384. const int row_dst = row_x;
  4385. const int idst = channel*nrows_dst + row_dst;
  4386. float tmp = 0.0f;
  4387. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4388. const int col_x = col_x0 + threadIdx.x;
  4389. if (col_x >= ncols_x) {
  4390. break;
  4391. }
  4392. const int row_y = col_x;
  4393. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  4394. const int iy = channel*nrows_y + row_y;
  4395. const float xi = __half2float(x[ix]);
  4396. tmp += xi * y[iy];
  4397. }
  4398. // sum up partial sums and write back result
  4399. tmp = warp_reduce_sum(tmp);
  4400. if (threadIdx.x == 0) {
  4401. dst[idst] = tmp;
  4402. }
  4403. }
  4404. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4405. const float * xi = (const float *) cxi;
  4406. float * dsti = (float *) cdsti;
  4407. *dsti = *xi;
  4408. }
  4409. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4410. const float * xi = (const float *) cxi;
  4411. half * dsti = (half *) cdsti;
  4412. *dsti = __float2half(*xi);
  4413. }
  4414. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4415. const half * xi = (const half *) cxi;
  4416. half * dsti = (half *) cdsti;
  4417. *dsti = *xi;
  4418. }
  4419. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  4420. const half * xi = (const half *) cxi;
  4421. float * dsti = (float *) cdsti;
  4422. *dsti = *xi;
  4423. }
  4424. template <cpy_kernel_t cpy_1>
  4425. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4426. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4427. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4428. const int nb12, const int nb13) {
  4429. const int64_t i = blockDim.x*blockIdx.x + threadIdx.x;
  4430. if (i >= ne) {
  4431. return;
  4432. }
  4433. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4434. // then combine those indices with the corresponding byte offsets to get the total offsets
  4435. const int64_t i03 = i/(ne00 * ne01 * ne02);
  4436. const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4437. const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4438. const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4439. const int64_t x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4440. const int64_t i13 = i/(ne10 * ne11 * ne12);
  4441. const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4442. const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4443. const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4444. const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  4445. cpy_1(cx + x_offset, cdst + dst_offset);
  4446. }
  4447. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4448. const float * xi = (const float *) cxi;
  4449. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4450. float amax = 0.0f; // absolute max
  4451. for (int j = 0; j < QK8_0; j++) {
  4452. const float v = xi[j];
  4453. amax = fmaxf(amax, fabsf(v));
  4454. }
  4455. const float d = amax / ((1 << 7) - 1);
  4456. const float id = d ? 1.0f/d : 0.0f;
  4457. dsti->d = d;
  4458. for (int j = 0; j < QK8_0; ++j) {
  4459. const float x0 = xi[j]*id;
  4460. dsti->qs[j] = roundf(x0);
  4461. }
  4462. }
  4463. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4464. const float * xi = (const float *) cxi;
  4465. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4466. float amax = 0.0f;
  4467. float vmax = 0.0f;
  4468. for (int j = 0; j < QK4_0; ++j) {
  4469. const float v = xi[j];
  4470. if (amax < fabsf(v)) {
  4471. amax = fabsf(v);
  4472. vmax = v;
  4473. }
  4474. }
  4475. const float d = vmax / -8;
  4476. const float id = d ? 1.0f/d : 0.0f;
  4477. dsti->d = d;
  4478. for (int j = 0; j < QK4_0/2; ++j) {
  4479. const float x0 = xi[0 + j]*id;
  4480. const float x1 = xi[QK4_0/2 + j]*id;
  4481. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4482. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4483. dsti->qs[j] = xi0;
  4484. dsti->qs[j] |= xi1 << 4;
  4485. }
  4486. }
  4487. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4488. const float * xi = (const float *) cxi;
  4489. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4490. float vmin = FLT_MAX;
  4491. float vmax = -FLT_MAX;
  4492. for (int j = 0; j < QK4_1; ++j) {
  4493. const float v = xi[j];
  4494. if (v < vmin) vmin = v;
  4495. if (v > vmax) vmax = v;
  4496. }
  4497. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4498. const float id = d ? 1.0f/d : 0.0f;
  4499. dsti->dm.x = d;
  4500. dsti->dm.y = vmin;
  4501. for (int j = 0; j < QK4_1/2; ++j) {
  4502. const float x0 = (xi[0 + j] - vmin)*id;
  4503. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4504. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4505. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4506. dsti->qs[j] = xi0;
  4507. dsti->qs[j] |= xi1 << 4;
  4508. }
  4509. }
  4510. template <cpy_kernel_t cpy_blck, int qk>
  4511. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4512. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4513. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4514. const int nb12, const int nb13) {
  4515. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4516. if (i >= ne) {
  4517. return;
  4518. }
  4519. const int i03 = i/(ne00 * ne01 * ne02);
  4520. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4521. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4522. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4523. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4524. const int i13 = i/(ne10 * ne11 * ne12);
  4525. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4526. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4527. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4528. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  4529. cpy_blck(cx + x_offset, cdst + dst_offset);
  4530. }
  4531. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4532. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4533. return 1.0f - min(1.0f, max(0.0f, y));
  4534. }
  4535. struct rope_corr_dims {
  4536. float v[4];
  4537. };
  4538. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4539. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4540. static __device__ void rope_yarn(
  4541. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4542. float * cos_theta, float * sin_theta
  4543. ) {
  4544. // Get n-d rotational scaling corrected for extrapolation
  4545. float theta_interp = freq_scale * theta_extrap;
  4546. float theta = theta_interp;
  4547. if (ext_factor != 0.0f) {
  4548. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4549. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4550. // Get n-d magnitude scaling corrected for interpolation
  4551. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4552. }
  4553. *cos_theta = cosf(theta) * mscale;
  4554. *sin_theta = sinf(theta) * mscale;
  4555. }
  4556. // rope == RoPE == rotary positional embedding
  4557. template<typename T, bool has_pos>
  4558. static __global__ void rope(
  4559. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4560. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4561. ) {
  4562. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4563. if (col >= ncols) {
  4564. return;
  4565. }
  4566. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4567. const int i = row*ncols + col;
  4568. const int i2 = row/p_delta_rows;
  4569. const int p = has_pos ? pos[i2] : 0;
  4570. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4571. float cos_theta, sin_theta;
  4572. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4573. const float x0 = x[i + 0];
  4574. const float x1 = x[i + 1];
  4575. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4576. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4577. }
  4578. template<typename T, bool has_pos>
  4579. static __global__ void rope_neox(
  4580. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4581. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4582. ) {
  4583. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4584. if (col >= ncols) {
  4585. return;
  4586. }
  4587. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4588. const int ib = col / n_dims;
  4589. const int ic = col % n_dims;
  4590. if (ib > 0) {
  4591. const int i = row*ncols + ib*n_dims + ic;
  4592. dst[i + 0] = x[i + 0];
  4593. dst[i + 1] = x[i + 1];
  4594. return;
  4595. }
  4596. const int i = row*ncols + ib*n_dims + ic/2;
  4597. const int i2 = row/p_delta_rows;
  4598. float cur_rot = inv_ndims * ic - ib;
  4599. const int p = has_pos ? pos[i2] : 0;
  4600. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4601. float cos_theta, sin_theta;
  4602. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4603. const float x0 = x[i + 0];
  4604. const float x1 = x[i + n_dims/2];
  4605. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4606. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4607. }
  4608. static __global__ void rope_glm_f32(
  4609. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4610. int n_ctx
  4611. ) {
  4612. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4613. const int half_n_dims = ncols/4;
  4614. if (col >= half_n_dims) {
  4615. return;
  4616. }
  4617. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4618. const int i = row*ncols + col;
  4619. const int i2 = row/p_delta_rows;
  4620. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4621. // FIXME: this is likely wrong
  4622. const int p = pos != nullptr ? pos[i2] : 0;
  4623. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4624. const float sin_theta = sinf(theta);
  4625. const float cos_theta = cosf(theta);
  4626. const float x0 = x[i + 0];
  4627. const float x1 = x[i + half_n_dims];
  4628. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4629. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4630. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4631. const float sin_block_theta = sinf(block_theta);
  4632. const float cos_block_theta = cosf(block_theta);
  4633. const float x2 = x[i + half_n_dims * 2];
  4634. const float x3 = x[i + half_n_dims * 3];
  4635. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4636. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4637. }
  4638. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4639. const int n_heads_log2_floor, const float m0, const float m1) {
  4640. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4641. if (col >= ncols) {
  4642. return;
  4643. }
  4644. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4645. const int i = row*ncols + col;
  4646. const int k = row/k_rows;
  4647. float m_k;
  4648. if (k < n_heads_log2_floor) {
  4649. m_k = powf(m0, k + 1);
  4650. } else {
  4651. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4652. }
  4653. dst[i] = col * m_k + x[i];
  4654. }
  4655. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4656. const int row = blockIdx.x;
  4657. const int col = threadIdx.x;
  4658. float sum = 0.0f;
  4659. for (int i = col; i < ncols; i += blockDim.x) {
  4660. sum += x[row * ncols + i];
  4661. }
  4662. sum = warp_reduce_sum(sum);
  4663. if (col == 0) {
  4664. dst[row] = sum;
  4665. }
  4666. }
  4667. template<typename T>
  4668. static inline __device__ void swap(T & a, T & b) {
  4669. T tmp = a;
  4670. a = b;
  4671. b = tmp;
  4672. }
  4673. template<ggml_sort_order order>
  4674. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4675. // bitonic sort
  4676. int col = threadIdx.x;
  4677. int row = blockIdx.y;
  4678. if (col >= ncols) return;
  4679. const float * x_row = x + row * ncols;
  4680. int * dst_row = dst + row * ncols;
  4681. // initialize indices
  4682. if (col < ncols) {
  4683. dst_row[col] = col;
  4684. }
  4685. __syncthreads();
  4686. for (int k = 2; k <= ncols; k *= 2) {
  4687. for (int j = k / 2; j > 0; j /= 2) {
  4688. int ixj = col ^ j;
  4689. if (ixj > col) {
  4690. if ((col & k) == 0) {
  4691. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  4692. swap(dst_row[col], dst_row[ixj]);
  4693. }
  4694. } else {
  4695. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  4696. swap(dst_row[col], dst_row[ixj]);
  4697. }
  4698. }
  4699. }
  4700. __syncthreads();
  4701. }
  4702. }
  4703. }
  4704. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4705. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4706. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4707. if (col >= ncols) {
  4708. return;
  4709. }
  4710. const int i = row*ncols + col;
  4711. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4712. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4713. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4714. }
  4715. template <bool vals_smem, int ncols_template, int block_size_template>
  4716. static __global__ void soft_max_f32(const float * x, const float * mask, const float * pos, float * dst, const int ncols_par, const int nrows_y, const float scale, const float max_bias, const float m0, const float m1, uint32_t n_head_log2) {
  4717. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  4718. const int tid = threadIdx.x;
  4719. const int rowx = blockIdx.x;
  4720. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  4721. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  4722. const int warp_id = threadIdx.x / WARP_SIZE;
  4723. const int lane_id = threadIdx.x % WARP_SIZE;
  4724. float slope = 0.0f;
  4725. // ALiBi
  4726. if (max_bias > 0.0f) {
  4727. const int h = rowx/nrows_y; // head index
  4728. const float base = h < n_head_log2 ? m0 : m1;
  4729. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  4730. slope = powf(base, exp);
  4731. }
  4732. extern __shared__ float data_soft_max_f32[];
  4733. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  4734. // shared memory buffer to cache values between iterations:
  4735. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  4736. float max_val = -INFINITY;
  4737. #pragma unroll
  4738. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4739. const int col = col0 + tid;
  4740. if (ncols_template == 0 && col >= ncols) {
  4741. break;
  4742. }
  4743. const int ix = rowx*ncols + col;
  4744. const int iy = rowy*ncols + col;
  4745. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  4746. vals[col] = val;
  4747. max_val = max(max_val, val);
  4748. }
  4749. // find the max value in the block
  4750. max_val = warp_reduce_max(max_val);
  4751. if (block_size > WARP_SIZE) {
  4752. if (warp_id == 0) {
  4753. buf_iw[lane_id] = -INFINITY;
  4754. }
  4755. __syncthreads();
  4756. if (lane_id == 0) {
  4757. buf_iw[warp_id] = max_val;
  4758. }
  4759. __syncthreads();
  4760. max_val = buf_iw[lane_id];
  4761. max_val = warp_reduce_max(max_val);
  4762. }
  4763. float tmp = 0.0f; // partial sum
  4764. #pragma unroll
  4765. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4766. const int col = col0 + tid;
  4767. if (ncols_template == 0 && col >= ncols) {
  4768. break;
  4769. }
  4770. const float val = expf(vals[col] - max_val);
  4771. tmp += val;
  4772. vals[col] = val;
  4773. }
  4774. // find the sum of exps in the block
  4775. tmp = warp_reduce_sum(tmp);
  4776. if (block_size > WARP_SIZE) {
  4777. __syncthreads();
  4778. if (warp_id == 0) {
  4779. buf_iw[lane_id] = 0.0f;
  4780. }
  4781. __syncthreads();
  4782. if (lane_id == 0) {
  4783. buf_iw[warp_id] = tmp;
  4784. }
  4785. __syncthreads();
  4786. tmp = buf_iw[lane_id];
  4787. tmp = warp_reduce_sum(tmp);
  4788. }
  4789. const float inv_sum = 1.0f / tmp;
  4790. #pragma unroll
  4791. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4792. const int col = col0 + tid;
  4793. if (ncols_template == 0 && col >= ncols) {
  4794. return;
  4795. }
  4796. const int idst = rowx*ncols + col;
  4797. dst[idst] = vals[col] * inv_sum;
  4798. }
  4799. }
  4800. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4801. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4802. if (i >= k) {
  4803. return;
  4804. }
  4805. dst[i] = scale * x[i];
  4806. }
  4807. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4808. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4809. if (i >= k) {
  4810. return;
  4811. }
  4812. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4813. }
  4814. template <typename T>
  4815. static __global__ void im2col_kernel(
  4816. const float * x, T * dst, int64_t batch_offset,
  4817. int64_t offset_delta, int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH, int64_t pelements, int64_t CHW,
  4818. int s0, int s1, int p0, int p1, int d0, int d1) {
  4819. const int64_t i = threadIdx.x + blockIdx.x * blockDim.x;
  4820. if (i >= pelements) {
  4821. return;
  4822. }
  4823. const int64_t ksize = OW * (KH > 1 ? KW : 1);
  4824. const int64_t kx = i / ksize;
  4825. const int64_t kd = kx * ksize;
  4826. const int64_t ky = (i - kd) / OW;
  4827. const int64_t ix = i % OW;
  4828. const int64_t oh = blockIdx.y;
  4829. const int64_t batch = blockIdx.z / IC;
  4830. const int64_t ic = blockIdx.z % IC;
  4831. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4832. const int64_t iih = oh * s1 + ky * d1 - p1;
  4833. const int64_t offset_dst =
  4834. ((batch * OH + oh) * OW + ix) * CHW +
  4835. (ic * (KW * KH) + ky * KW + kx);
  4836. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4837. dst[offset_dst] = 0.0f;
  4838. } else {
  4839. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  4840. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  4841. }
  4842. }
  4843. template <typename Ti, typename To>
  4844. static __global__ void pool2d_nchw_kernel(
  4845. const int ih, const int iw, const int oh, const int ow,
  4846. const int kh, const int kw, const int sh, const int sw,
  4847. const int ph, const int pw, const int parallel_elements,
  4848. const Ti* src, To* dst, const enum ggml_op_pool op) {
  4849. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  4850. if (idx >= parallel_elements) {
  4851. return;
  4852. }
  4853. const int I_HW = ih * iw;
  4854. const int O_HW = oh * ow;
  4855. const int nc = idx / O_HW;
  4856. const int cur_oh = idx % O_HW / ow;
  4857. const int cur_ow = idx % O_HW % ow;
  4858. const Ti* i_ptr = src + nc * I_HW;
  4859. To* o_ptr = dst + nc * O_HW;
  4860. const int start_h = cur_oh * sh - ph;
  4861. const int bh = max(0, start_h);
  4862. const int eh = min(ih, start_h + kh);
  4863. const int start_w = cur_ow * sw - pw;
  4864. const int bw = max(0, start_w);
  4865. const int ew = min(iw, start_w + kw);
  4866. const To scale = 1. / (kh * kw);
  4867. To res = 0;
  4868. switch (op) {
  4869. case GGML_OP_POOL_AVG: res = 0; break;
  4870. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  4871. }
  4872. for (int i = bh; i < eh; i += 1) {
  4873. for (int j = bw; j < ew; j += 1) {
  4874. #if __CUDA_ARCH__ >= 350
  4875. Ti cur = __ldg(i_ptr + i * iw + j);
  4876. #else
  4877. Ti cur = i_ptr[i * iw + j];
  4878. #endif
  4879. switch (op) {
  4880. case GGML_OP_POOL_AVG: res += cur * scale; break;
  4881. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  4882. }
  4883. }
  4884. }
  4885. o_ptr[cur_oh * ow + cur_ow] = res;
  4886. }
  4887. template<int qk, int qr, dequantize_kernel_t dq>
  4888. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4889. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4890. GGML_TENSOR_BINARY_OP_LOCALS
  4891. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4892. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4893. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4894. // strides in elements
  4895. //const size_t s0 = nb0 / ggml_element_size(dst);
  4896. const size_t s1 = nb1 / ggml_element_size(dst);
  4897. const size_t s2 = nb2 / ggml_element_size(dst);
  4898. const size_t s3 = nb3 / ggml_element_size(dst);
  4899. const size_t s10 = nb10 / ggml_element_size(src1);
  4900. const size_t s11 = nb11 / ggml_element_size(src1);
  4901. const size_t s12 = nb12 / ggml_element_size(src1);
  4902. //const size_t s13 = nb13 / ggml_element_size(src1);
  4903. GGML_ASSERT(ne00 % 2 == 0);
  4904. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4905. src0_dd, src1_dd, dst_dd,
  4906. ne00, /*ne01, ne02, ne03,*/
  4907. /*ne10, ne11,*/ ne12, /*ne13,*/
  4908. /* s0,*/ s1, s2, s3,
  4909. /* nb00,*/ nb01, nb02, nb03,
  4910. s10, s11, s12/*, s13*/);
  4911. (void) dst;
  4912. }
  4913. template<typename src0_t>
  4914. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4915. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4916. GGML_TENSOR_BINARY_OP_LOCALS
  4917. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4918. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4919. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4920. // strides in elements
  4921. //const size_t s0 = nb0 / ggml_element_size(dst);
  4922. const size_t s1 = nb1 / ggml_element_size(dst);
  4923. const size_t s2 = nb2 / ggml_element_size(dst);
  4924. const size_t s3 = nb3 / ggml_element_size(dst);
  4925. const size_t s10 = nb10 / ggml_element_size(src1);
  4926. const size_t s11 = nb11 / ggml_element_size(src1);
  4927. const size_t s12 = nb12 / ggml_element_size(src1);
  4928. //const size_t s13 = nb13 / ggml_element_size(src1);
  4929. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4930. src0_dd, src1_dd, dst_dd,
  4931. ne00, /*ne01, ne02, ne03,*/
  4932. /*ne10, ne11,*/ ne12, /*ne13,*/
  4933. /* s0,*/ s1, s2, s3,
  4934. /* nb00,*/ nb01, nb02, nb03,
  4935. s10, s11, s12/*, s13*/);
  4936. (void) dst;
  4937. }
  4938. template<float (*bin_op)(const float, const float)>
  4939. struct bin_bcast_cuda {
  4940. template<typename src0_t, typename src1_t, typename dst_t>
  4941. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4942. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4943. cudaStream_t stream) {
  4944. GGML_TENSOR_BINARY_OP_LOCALS
  4945. int nr0 = ne10/ne0;
  4946. int nr1 = ne11/ne1;
  4947. int nr2 = ne12/ne2;
  4948. int nr3 = ne13/ne3;
  4949. int nr[4] = { nr0, nr1, nr2, nr3 };
  4950. // collapse dimensions until first broadcast dimension
  4951. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4952. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4953. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4954. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4955. auto collapse = [](int64_t cne[]) {
  4956. cne[0] *= cne[1];
  4957. cne[1] = cne[2];
  4958. cne[2] = cne[3];
  4959. cne[3] = 1;
  4960. };
  4961. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  4962. cnb[1] *= cne[1];
  4963. cnb[2] *= cne[2];
  4964. cnb[3] *= cne[3];
  4965. };
  4966. for (int i = 0; i < 4; i++) {
  4967. if (nr[i] != 1) {
  4968. break;
  4969. }
  4970. if (i > 0) {
  4971. collapse_nb(cnb0, cne0);
  4972. collapse_nb(cnb1, cne1);
  4973. collapse(cne0);
  4974. collapse(cne1);
  4975. }
  4976. }
  4977. {
  4978. int64_t ne0 = cne0[0];
  4979. int64_t ne1 = cne0[1];
  4980. int64_t ne2 = cne0[2];
  4981. int64_t ne3 = cne0[3];
  4982. int64_t ne10 = cne1[0];
  4983. int64_t ne11 = cne1[1];
  4984. int64_t ne12 = cne1[2];
  4985. int64_t ne13 = cne1[3];
  4986. size_t nb0 = cnb0[0];
  4987. size_t nb1 = cnb0[1];
  4988. size_t nb2 = cnb0[2];
  4989. size_t nb3 = cnb0[3];
  4990. size_t nb10 = cnb1[0];
  4991. size_t nb11 = cnb1[1];
  4992. size_t nb12 = cnb1[2];
  4993. size_t nb13 = cnb1[3];
  4994. size_t s0 = nb0 / sizeof(dst_t);
  4995. size_t s1 = nb1 / sizeof(dst_t);
  4996. size_t s2 = nb2 / sizeof(dst_t);
  4997. size_t s3 = nb3 / sizeof(dst_t);
  4998. size_t s10 = nb10 / sizeof(src1_t);
  4999. size_t s11 = nb11 / sizeof(src1_t);
  5000. size_t s12 = nb12 / sizeof(src1_t);
  5001. size_t s13 = nb13 / sizeof(src1_t);
  5002. GGML_ASSERT(s0 == 1);
  5003. GGML_ASSERT(s10 == 1);
  5004. const int block_size = 128;
  5005. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5006. dim3 block_dims;
  5007. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5008. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5009. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5010. dim3 block_nums(
  5011. (hne0 + block_dims.x - 1) / block_dims.x,
  5012. (ne1 + block_dims.y - 1) / block_dims.y,
  5013. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5014. );
  5015. if (block_nums.z > 65535) {
  5016. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5017. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5018. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5019. src0_dd, src1_dd, dst_dd,
  5020. ne0, ne1, ne2, ne3,
  5021. ne10, ne11, ne12, ne13,
  5022. /* s0, */ s1, s2, s3,
  5023. /* s10, */ s11, s12, s13);
  5024. } else {
  5025. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5026. src0_dd, src1_dd, dst_dd,
  5027. ne0, ne1, ne2, ne3,
  5028. ne10, ne11, ne12, ne13,
  5029. /* s0, */ s1, s2, s3,
  5030. /* s10, */ s11, s12, s13);
  5031. }
  5032. }
  5033. }
  5034. };
  5035. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5036. const int ne10, const int ne11, const int ne12,
  5037. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5038. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5039. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5040. }
  5041. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5042. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5043. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5044. }
  5045. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5046. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5047. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5048. }
  5049. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5050. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5051. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5052. }
  5053. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5054. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5055. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5056. }
  5057. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5058. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5059. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5060. }
  5061. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5062. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5063. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5064. }
  5065. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5066. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5067. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5068. }
  5069. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5070. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5071. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5072. }
  5073. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5074. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5075. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5076. }
  5077. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5078. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5079. if (ncols < 1024) {
  5080. const dim3 block_dims(WARP_SIZE, 1, 1);
  5081. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5082. } else {
  5083. const dim3 block_dims(1024, 1, 1);
  5084. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5085. }
  5086. }
  5087. 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) {
  5088. static const float eps = 1e-6f;
  5089. if (group_size < 1024) {
  5090. const dim3 block_dims(WARP_SIZE, 1, 1);
  5091. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5092. } else {
  5093. const dim3 block_dims(1024, 1, 1);
  5094. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5095. }
  5096. }
  5097. 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) {
  5098. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5099. dim3 gridDim(num_blocks, ne1, ne2);
  5100. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5101. }
  5102. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int ne03,
  5103. const int scale_factor, cudaStream_t stream) {
  5104. int ne0 = (ne00 * scale_factor);
  5105. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5106. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02*ne03);
  5107. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  5108. }
  5109. static void pad_f32_cuda(const float * x, float * dst,
  5110. const int ne00, const int ne01, const int ne02, const int ne03,
  5111. const int ne0, const int ne1, const int ne2, const int ne3, cudaStream_t stream) {
  5112. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  5113. dim3 gridDim(num_blocks, ne1, ne2*ne3);
  5114. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02, ne03);
  5115. }
  5116. static void arange_f32_cuda(float * dst, const int ne0, const float start, const float step, cudaStream_t stream) {
  5117. int num_blocks = (ne0 + CUDA_ARANGE_BLOCK_SIZE - 1) / CUDA_ARANGE_BLOCK_SIZE;
  5118. arange_f32<<<num_blocks, CUDA_ARANGE_BLOCK_SIZE, 0, stream>>>(dst, ne0, start, step);
  5119. }
  5120. static void timestep_embedding_f32_cuda(const float * x, float * dst, const int ne00, const int nb1,
  5121. const int dim, const int max_period, cudaStream_t stream) {
  5122. int half_ceil = (dim + 1) / 2;
  5123. int num_blocks = (half_ceil + CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE - 1) / CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE;
  5124. dim3 gridDim(num_blocks, ne00, 1);
  5125. timestep_embedding_f32<<<gridDim, CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE, 0, stream>>>(x, dst, nb1, dim, max_period);
  5126. }
  5127. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5128. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5129. if (ncols < 1024) {
  5130. const dim3 block_dims(WARP_SIZE, 1, 1);
  5131. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5132. } else {
  5133. const dim3 block_dims(1024, 1, 1);
  5134. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5135. }
  5136. }
  5137. 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) {
  5138. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  5139. const dim3 num_blocks(block_num_x, ky, 1);
  5140. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  5141. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  5142. }
  5143. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5144. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5145. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  5146. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5147. }
  5148. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  5149. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  5150. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  5151. const bool need_check = false;
  5152. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5153. } else {
  5154. const bool need_check = true;
  5155. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5156. }
  5157. }
  5158. template<typename dst_t>
  5159. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5160. const int nb = k / QK_K;
  5161. #if QK_K == 256
  5162. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  5163. #else
  5164. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  5165. #endif
  5166. }
  5167. template<typename dst_t>
  5168. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5169. const int nb = k / QK_K;
  5170. #if QK_K == 256
  5171. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  5172. #else
  5173. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  5174. #endif
  5175. }
  5176. template<typename dst_t>
  5177. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5178. const int nb32 = k / 32;
  5179. const int nb = (k + 255) / 256;
  5180. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5181. }
  5182. template<typename dst_t>
  5183. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5184. const int nb32 = k / 32;
  5185. const int nb = (k + 255) / 256;
  5186. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5187. }
  5188. template<typename dst_t>
  5189. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5190. const int nb = k / QK_K;
  5191. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  5192. }
  5193. template<typename dst_t>
  5194. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5195. const int nb = k / QK_K;
  5196. #if QK_K == 256
  5197. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  5198. #else
  5199. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  5200. #endif
  5201. }
  5202. template<typename dst_t>
  5203. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5204. const int nb = k / QK_K;
  5205. #if QK_K == 256
  5206. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  5207. #else
  5208. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  5209. #endif
  5210. }
  5211. template<typename dst_t>
  5212. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5213. const int nb = k / QK_K;
  5214. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5215. }
  5216. template<typename dst_t>
  5217. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5218. const int nb = k / QK_K;
  5219. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  5220. }
  5221. template<typename dst_t>
  5222. static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5223. const int nb = k / QK_K;
  5224. dequantize_block_iq2_s<<<nb, 32, 0, stream>>>(vx, y);
  5225. }
  5226. template<typename dst_t>
  5227. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5228. const int nb = k / QK_K;
  5229. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5230. }
  5231. template<typename dst_t>
  5232. static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5233. const int nb = k / QK_K;
  5234. dequantize_block_iq3_s<<<nb, 32, 0, stream>>>(vx, y);
  5235. }
  5236. template<typename dst_t>
  5237. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5238. const int nb = k / QK_K;
  5239. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  5240. }
  5241. template<typename dst_t>
  5242. static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5243. const int nb = (k + QK_K - 1) / QK_K;
  5244. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  5245. }
  5246. template<typename dst_t>
  5247. static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5248. const int nb = (k + QK_K - 1) / QK_K;
  5249. #if QK_K == 64
  5250. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  5251. #else
  5252. dequantize_block_iq4_xs<<<nb, 32, 0, stream>>>(vx, y);
  5253. #endif
  5254. }
  5255. template <typename src_t, typename dst_t>
  5256. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5257. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  5258. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5259. }
  5260. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  5261. int id;
  5262. switch (type) {
  5263. case GGML_TYPE_Q4_0:
  5264. return dequantize_row_q4_0_cuda;
  5265. case GGML_TYPE_Q4_1:
  5266. return dequantize_row_q4_1_cuda;
  5267. case GGML_TYPE_Q5_0:
  5268. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5269. case GGML_TYPE_Q5_1:
  5270. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5271. case GGML_TYPE_Q8_0:
  5272. CUDA_CHECK(cudaGetDevice(&id));
  5273. if (g_device_caps[id].cc >= CC_PASCAL) {
  5274. return dequantize_block_q8_0_f16_cuda;
  5275. }
  5276. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5277. case GGML_TYPE_Q2_K:
  5278. return dequantize_row_q2_K_cuda;
  5279. case GGML_TYPE_Q3_K:
  5280. return dequantize_row_q3_K_cuda;
  5281. case GGML_TYPE_Q4_K:
  5282. return dequantize_row_q4_K_cuda;
  5283. case GGML_TYPE_Q5_K:
  5284. return dequantize_row_q5_K_cuda;
  5285. case GGML_TYPE_Q6_K:
  5286. return dequantize_row_q6_K_cuda;
  5287. case GGML_TYPE_IQ2_XXS:
  5288. return dequantize_row_iq2_xxs_cuda;
  5289. case GGML_TYPE_IQ2_XS:
  5290. return dequantize_row_iq2_xs_cuda;
  5291. case GGML_TYPE_IQ2_S:
  5292. return dequantize_row_iq2_s_cuda;
  5293. case GGML_TYPE_IQ3_XXS:
  5294. return dequantize_row_iq3_xxs_cuda;
  5295. case GGML_TYPE_IQ1_S:
  5296. return dequantize_row_iq1_s_cuda;
  5297. case GGML_TYPE_IQ4_NL:
  5298. return dequantize_row_iq4_nl_cuda;
  5299. case GGML_TYPE_IQ4_XS:
  5300. return dequantize_row_iq4_xs_cuda;
  5301. case GGML_TYPE_IQ3_S:
  5302. return dequantize_row_iq3_s_cuda;
  5303. case GGML_TYPE_F32:
  5304. return convert_unary_cuda<float>;
  5305. default:
  5306. return nullptr;
  5307. }
  5308. }
  5309. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  5310. switch (type) {
  5311. case GGML_TYPE_Q4_0:
  5312. return dequantize_row_q4_0_cuda;
  5313. case GGML_TYPE_Q4_1:
  5314. return dequantize_row_q4_1_cuda;
  5315. case GGML_TYPE_Q5_0:
  5316. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5317. case GGML_TYPE_Q5_1:
  5318. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5319. case GGML_TYPE_Q8_0:
  5320. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5321. case GGML_TYPE_Q2_K:
  5322. return dequantize_row_q2_K_cuda;
  5323. case GGML_TYPE_Q3_K:
  5324. return dequantize_row_q3_K_cuda;
  5325. case GGML_TYPE_Q4_K:
  5326. return dequantize_row_q4_K_cuda;
  5327. case GGML_TYPE_Q5_K:
  5328. return dequantize_row_q5_K_cuda;
  5329. case GGML_TYPE_Q6_K:
  5330. return dequantize_row_q6_K_cuda;
  5331. case GGML_TYPE_IQ2_XXS:
  5332. return dequantize_row_iq2_xxs_cuda;
  5333. case GGML_TYPE_IQ2_XS:
  5334. return dequantize_row_iq2_xs_cuda;
  5335. case GGML_TYPE_IQ2_S:
  5336. return dequantize_row_iq2_s_cuda;
  5337. case GGML_TYPE_IQ3_XXS:
  5338. return dequantize_row_iq3_xxs_cuda;
  5339. case GGML_TYPE_IQ1_S:
  5340. return dequantize_row_iq1_s_cuda;
  5341. case GGML_TYPE_IQ4_NL:
  5342. return dequantize_row_iq4_nl_cuda;
  5343. case GGML_TYPE_IQ4_XS:
  5344. return dequantize_row_iq4_xs_cuda;
  5345. case GGML_TYPE_IQ3_S:
  5346. return dequantize_row_iq3_s_cuda;
  5347. case GGML_TYPE_F16:
  5348. return convert_unary_cuda<half>;
  5349. default:
  5350. return nullptr;
  5351. }
  5352. }
  5353. 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) {
  5354. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5355. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5356. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  5357. const dim3 block_nums(block_num_y, 1, 1);
  5358. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5359. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  5360. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5361. }
  5362. 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) {
  5363. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5364. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5365. const dim3 block_nums(block_num_y, 1, 1);
  5366. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5367. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  5368. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5369. }
  5370. 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) {
  5371. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5372. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5373. const dim3 block_nums(block_num_y, 1, 1);
  5374. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5375. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  5376. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5377. }
  5378. 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) {
  5379. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5380. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5381. const dim3 block_nums(block_num_y, 1, 1);
  5382. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5383. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  5384. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5385. }
  5386. 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) {
  5387. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5388. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5389. const dim3 block_nums(block_num_y, 1, 1);
  5390. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5391. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  5392. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5393. }
  5394. 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) {
  5395. GGML_ASSERT(ncols % QK_K == 0);
  5396. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  5397. const int block_num_y = (nrows + ny - 1) / ny;
  5398. const dim3 block_nums(block_num_y, 1, 1);
  5399. const dim3 block_dims(32, ny, 1);
  5400. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5401. }
  5402. 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) {
  5403. GGML_ASSERT(ncols % QK_K == 0);
  5404. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5405. const int block_num_y = (nrows + ny - 1) / ny;
  5406. const dim3 block_nums(block_num_y, 1, 1);
  5407. const dim3 block_dims(32, ny, 1);
  5408. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5409. }
  5410. 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) {
  5411. GGML_ASSERT(ncols % QK_K == 0);
  5412. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5413. const int block_num_y = (nrows + ny - 1) / ny;
  5414. const dim3 block_nums(block_num_y, 1, 1);
  5415. const dim3 block_dims(32, ny, 1);
  5416. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5417. }
  5418. 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) {
  5419. GGML_ASSERT(ncols % QK_K == 0);
  5420. const dim3 block_dims(32, 1, 1);
  5421. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  5422. }
  5423. 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) {
  5424. GGML_ASSERT(ncols % QK_K == 0);
  5425. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5426. const int block_num_y = (nrows + ny - 1) / ny;
  5427. const dim3 block_nums(block_num_y, 1, 1);
  5428. const dim3 block_dims(32, ny, 1);
  5429. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5430. }
  5431. 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) {
  5432. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5433. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5434. const dim3 block_nums(block_num_y, 1, 1);
  5435. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5436. dequantize_mul_mat_vec<1, 1, convert_f16>
  5437. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5438. }
  5439. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  5440. static void mul_mat_vec_q_cuda(
  5441. const void * vx, const void * vy, float * dst,
  5442. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  5443. GGML_ASSERT(ncols_x % qk == 0);
  5444. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  5445. int id;
  5446. CUDA_CHECK(cudaGetDevice(&id));
  5447. int64_t nwarps = 1;
  5448. int64_t rows_per_cuda_block = 1;
  5449. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  5450. switch(ncols_y) {
  5451. case 1:
  5452. nwarps = 4;
  5453. rows_per_cuda_block = 1;
  5454. break;
  5455. case 2:
  5456. case 3:
  5457. case 4:
  5458. nwarps = 4;
  5459. rows_per_cuda_block = 2;
  5460. break;
  5461. case 5:
  5462. case 6:
  5463. case 7:
  5464. case 8:
  5465. nwarps = 2;
  5466. rows_per_cuda_block = 2;
  5467. break;
  5468. default:
  5469. GGML_ASSERT(false);
  5470. break;
  5471. }
  5472. }
  5473. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  5474. const dim3 block_nums(nblocks, 1, 1);
  5475. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5476. switch (ncols_y) {
  5477. case 1:
  5478. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  5479. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5480. break;
  5481. case 2:
  5482. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  5483. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5484. break;
  5485. case 3:
  5486. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  5487. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5488. break;
  5489. case 4:
  5490. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  5491. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5492. break;
  5493. case 5:
  5494. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  5495. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5496. break;
  5497. case 6:
  5498. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  5499. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5500. break;
  5501. case 7:
  5502. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  5503. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5504. break;
  5505. case 8:
  5506. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  5507. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5508. break;
  5509. default:
  5510. GGML_ASSERT(false);
  5511. break;
  5512. }
  5513. }
  5514. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5515. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5516. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5517. int id;
  5518. CUDA_CHECK(cudaGetDevice(&id));
  5519. const int compute_capability = g_device_caps[id].cc;
  5520. int mmq_x, mmq_y, nwarps;
  5521. if (compute_capability >= CC_RDNA2) {
  5522. mmq_x = MMQ_X_Q4_0_RDNA2;
  5523. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5524. nwarps = NWARPS_Q4_0_RDNA2;
  5525. } else if (compute_capability >= CC_OFFSET_AMD) {
  5526. mmq_x = MMQ_X_Q4_0_RDNA1;
  5527. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5528. nwarps = NWARPS_Q4_0_RDNA1;
  5529. } else if (compute_capability >= CC_VOLTA) {
  5530. mmq_x = MMQ_X_Q4_0_AMPERE;
  5531. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5532. nwarps = NWARPS_Q4_0_AMPERE;
  5533. } else if (compute_capability >= MIN_CC_DP4A) {
  5534. mmq_x = MMQ_X_Q4_0_PASCAL;
  5535. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5536. nwarps = NWARPS_Q4_0_PASCAL;
  5537. } else {
  5538. GGML_ASSERT(false);
  5539. }
  5540. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5541. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5542. const dim3 block_nums(block_num_x, block_num_y, 1);
  5543. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5544. if (nrows_x % mmq_y == 0) {
  5545. const bool need_check = false;
  5546. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5547. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5548. } else {
  5549. const bool need_check = true;
  5550. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5551. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5552. }
  5553. }
  5554. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5555. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5556. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5557. int id;
  5558. CUDA_CHECK(cudaGetDevice(&id));
  5559. const int compute_capability = g_device_caps[id].cc;
  5560. int mmq_x, mmq_y, nwarps;
  5561. if (compute_capability >= CC_RDNA2) {
  5562. mmq_x = MMQ_X_Q4_1_RDNA2;
  5563. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5564. nwarps = NWARPS_Q4_1_RDNA2;
  5565. } else if (compute_capability >= CC_OFFSET_AMD) {
  5566. mmq_x = MMQ_X_Q4_1_RDNA1;
  5567. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5568. nwarps = NWARPS_Q4_1_RDNA1;
  5569. } else if (compute_capability >= CC_VOLTA) {
  5570. mmq_x = MMQ_X_Q4_1_AMPERE;
  5571. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5572. nwarps = NWARPS_Q4_1_AMPERE;
  5573. } else if (compute_capability >= MIN_CC_DP4A) {
  5574. mmq_x = MMQ_X_Q4_1_PASCAL;
  5575. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5576. nwarps = NWARPS_Q4_1_PASCAL;
  5577. } else {
  5578. GGML_ASSERT(false);
  5579. }
  5580. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5581. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5582. const dim3 block_nums(block_num_x, block_num_y, 1);
  5583. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5584. if (nrows_x % mmq_y == 0) {
  5585. const bool need_check = false;
  5586. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5587. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5588. } else {
  5589. const bool need_check = true;
  5590. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5591. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5592. }
  5593. }
  5594. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5595. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5596. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5597. int id;
  5598. CUDA_CHECK(cudaGetDevice(&id));
  5599. const int compute_capability = g_device_caps[id].cc;
  5600. int mmq_x, mmq_y, nwarps;
  5601. if (compute_capability >= CC_RDNA2) {
  5602. mmq_x = MMQ_X_Q5_0_RDNA2;
  5603. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5604. nwarps = NWARPS_Q5_0_RDNA2;
  5605. } else if (compute_capability >= CC_OFFSET_AMD) {
  5606. mmq_x = MMQ_X_Q5_0_RDNA1;
  5607. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5608. nwarps = NWARPS_Q5_0_RDNA1;
  5609. } else if (compute_capability >= CC_VOLTA) {
  5610. mmq_x = MMQ_X_Q5_0_AMPERE;
  5611. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5612. nwarps = NWARPS_Q5_0_AMPERE;
  5613. } else if (compute_capability >= MIN_CC_DP4A) {
  5614. mmq_x = MMQ_X_Q5_0_PASCAL;
  5615. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5616. nwarps = NWARPS_Q5_0_PASCAL;
  5617. } else {
  5618. GGML_ASSERT(false);
  5619. }
  5620. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5621. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5622. const dim3 block_nums(block_num_x, block_num_y, 1);
  5623. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5624. if (nrows_x % mmq_y == 0) {
  5625. const bool need_check = false;
  5626. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5627. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5628. } else {
  5629. const bool need_check = true;
  5630. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5631. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5632. }
  5633. }
  5634. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5635. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5636. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5637. int id;
  5638. CUDA_CHECK(cudaGetDevice(&id));
  5639. const int compute_capability = g_device_caps[id].cc;
  5640. int mmq_x, mmq_y, nwarps;
  5641. if (compute_capability >= CC_RDNA2) {
  5642. mmq_x = MMQ_X_Q5_1_RDNA2;
  5643. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5644. nwarps = NWARPS_Q5_1_RDNA2;
  5645. } else if (compute_capability >= CC_OFFSET_AMD) {
  5646. mmq_x = MMQ_X_Q5_1_RDNA1;
  5647. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5648. nwarps = NWARPS_Q5_1_RDNA1;
  5649. } else if (compute_capability >= CC_VOLTA) {
  5650. mmq_x = MMQ_X_Q5_1_AMPERE;
  5651. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5652. nwarps = NWARPS_Q5_1_AMPERE;
  5653. } else if (compute_capability >= MIN_CC_DP4A) {
  5654. mmq_x = MMQ_X_Q5_1_PASCAL;
  5655. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5656. nwarps = NWARPS_Q5_1_PASCAL;
  5657. } else {
  5658. GGML_ASSERT(false);
  5659. }
  5660. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5661. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5662. const dim3 block_nums(block_num_x, block_num_y, 1);
  5663. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5664. if (nrows_x % mmq_y == 0) {
  5665. const bool need_check = false;
  5666. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5667. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5668. } else {
  5669. const bool need_check = true;
  5670. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5671. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5672. }
  5673. }
  5674. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5675. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5676. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5677. int id;
  5678. CUDA_CHECK(cudaGetDevice(&id));
  5679. const int compute_capability = g_device_caps[id].cc;
  5680. int mmq_x, mmq_y, nwarps;
  5681. if (compute_capability >= CC_RDNA2) {
  5682. mmq_x = MMQ_X_Q8_0_RDNA2;
  5683. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5684. nwarps = NWARPS_Q8_0_RDNA2;
  5685. } else if (compute_capability >= CC_OFFSET_AMD) {
  5686. mmq_x = MMQ_X_Q8_0_RDNA1;
  5687. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5688. nwarps = NWARPS_Q8_0_RDNA1;
  5689. } else if (compute_capability >= CC_VOLTA) {
  5690. mmq_x = MMQ_X_Q8_0_AMPERE;
  5691. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5692. nwarps = NWARPS_Q8_0_AMPERE;
  5693. } else if (compute_capability >= MIN_CC_DP4A) {
  5694. mmq_x = MMQ_X_Q8_0_PASCAL;
  5695. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5696. nwarps = NWARPS_Q8_0_PASCAL;
  5697. } else {
  5698. GGML_ASSERT(false);
  5699. }
  5700. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5701. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5702. const dim3 block_nums(block_num_x, block_num_y, 1);
  5703. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5704. if (nrows_x % mmq_y == 0) {
  5705. const bool need_check = false;
  5706. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5707. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5708. } else {
  5709. const bool need_check = true;
  5710. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5711. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5712. }
  5713. }
  5714. static void ggml_mul_mat_q2_K_q8_1_cuda(
  5715. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5716. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5717. int id;
  5718. CUDA_CHECK(cudaGetDevice(&id));
  5719. const int compute_capability = g_device_caps[id].cc;
  5720. int mmq_x, mmq_y, nwarps;
  5721. if (compute_capability >= CC_RDNA2) {
  5722. mmq_x = MMQ_X_Q2_K_RDNA2;
  5723. mmq_y = MMQ_Y_Q2_K_RDNA2;
  5724. nwarps = NWARPS_Q2_K_RDNA2;
  5725. } else if (compute_capability >= CC_OFFSET_AMD) {
  5726. mmq_x = MMQ_X_Q2_K_RDNA1;
  5727. mmq_y = MMQ_Y_Q2_K_RDNA1;
  5728. nwarps = NWARPS_Q2_K_RDNA1;
  5729. } else if (compute_capability >= CC_VOLTA) {
  5730. mmq_x = MMQ_X_Q2_K_AMPERE;
  5731. mmq_y = MMQ_Y_Q2_K_AMPERE;
  5732. nwarps = NWARPS_Q2_K_AMPERE;
  5733. } else if (compute_capability >= MIN_CC_DP4A) {
  5734. mmq_x = MMQ_X_Q2_K_PASCAL;
  5735. mmq_y = MMQ_Y_Q2_K_PASCAL;
  5736. nwarps = NWARPS_Q2_K_PASCAL;
  5737. } else {
  5738. GGML_ASSERT(false);
  5739. }
  5740. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5741. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5742. const dim3 block_nums(block_num_x, block_num_y, 1);
  5743. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5744. if (nrows_x % mmq_y == 0) {
  5745. const bool need_check = false;
  5746. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5747. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5748. } else {
  5749. const bool need_check = true;
  5750. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5751. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5752. }
  5753. }
  5754. static void ggml_mul_mat_q3_K_q8_1_cuda(
  5755. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5756. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5757. #if QK_K == 256
  5758. int id;
  5759. CUDA_CHECK(cudaGetDevice(&id));
  5760. const int compute_capability = g_device_caps[id].cc;
  5761. int mmq_x, mmq_y, nwarps;
  5762. if (compute_capability >= CC_RDNA2) {
  5763. mmq_x = MMQ_X_Q3_K_RDNA2;
  5764. mmq_y = MMQ_Y_Q3_K_RDNA2;
  5765. nwarps = NWARPS_Q3_K_RDNA2;
  5766. } else if (compute_capability >= CC_OFFSET_AMD) {
  5767. mmq_x = MMQ_X_Q3_K_RDNA1;
  5768. mmq_y = MMQ_Y_Q3_K_RDNA1;
  5769. nwarps = NWARPS_Q3_K_RDNA1;
  5770. } else if (compute_capability >= CC_VOLTA) {
  5771. mmq_x = MMQ_X_Q3_K_AMPERE;
  5772. mmq_y = MMQ_Y_Q3_K_AMPERE;
  5773. nwarps = NWARPS_Q3_K_AMPERE;
  5774. } else if (compute_capability >= MIN_CC_DP4A) {
  5775. mmq_x = MMQ_X_Q3_K_PASCAL;
  5776. mmq_y = MMQ_Y_Q3_K_PASCAL;
  5777. nwarps = NWARPS_Q3_K_PASCAL;
  5778. } else {
  5779. GGML_ASSERT(false);
  5780. }
  5781. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5782. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5783. const dim3 block_nums(block_num_x, block_num_y, 1);
  5784. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5785. if (nrows_x % mmq_y == 0) {
  5786. const bool need_check = false;
  5787. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5788. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5789. } else {
  5790. const bool need_check = true;
  5791. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5792. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5793. }
  5794. #endif
  5795. }
  5796. static void ggml_mul_mat_q4_K_q8_1_cuda(
  5797. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5798. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5799. int id;
  5800. CUDA_CHECK(cudaGetDevice(&id));
  5801. const int compute_capability = g_device_caps[id].cc;
  5802. int mmq_x, mmq_y, nwarps;
  5803. if (compute_capability >= CC_RDNA2) {
  5804. mmq_x = MMQ_X_Q4_K_RDNA2;
  5805. mmq_y = MMQ_Y_Q4_K_RDNA2;
  5806. nwarps = NWARPS_Q4_K_RDNA2;
  5807. } else if (compute_capability >= CC_OFFSET_AMD) {
  5808. mmq_x = MMQ_X_Q4_K_RDNA1;
  5809. mmq_y = MMQ_Y_Q4_K_RDNA1;
  5810. nwarps = NWARPS_Q4_K_RDNA1;
  5811. } else if (compute_capability >= CC_VOLTA) {
  5812. mmq_x = MMQ_X_Q4_K_AMPERE;
  5813. mmq_y = MMQ_Y_Q4_K_AMPERE;
  5814. nwarps = NWARPS_Q4_K_AMPERE;
  5815. } else if (compute_capability >= MIN_CC_DP4A) {
  5816. mmq_x = MMQ_X_Q4_K_PASCAL;
  5817. mmq_y = MMQ_Y_Q4_K_PASCAL;
  5818. nwarps = NWARPS_Q4_K_PASCAL;
  5819. } else {
  5820. GGML_ASSERT(false);
  5821. }
  5822. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5823. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5824. const dim3 block_nums(block_num_x, block_num_y, 1);
  5825. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5826. if (nrows_x % mmq_y == 0) {
  5827. const bool need_check = false;
  5828. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5829. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5830. } else {
  5831. const bool need_check = true;
  5832. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5833. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5834. }
  5835. }
  5836. static void ggml_mul_mat_q5_K_q8_1_cuda(
  5837. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5838. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5839. int id;
  5840. CUDA_CHECK(cudaGetDevice(&id));
  5841. const int compute_capability = g_device_caps[id].cc;
  5842. int mmq_x, mmq_y, nwarps;
  5843. if (compute_capability >= CC_RDNA2) {
  5844. mmq_x = MMQ_X_Q5_K_RDNA2;
  5845. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5846. nwarps = NWARPS_Q5_K_RDNA2;
  5847. } else if (compute_capability >= CC_OFFSET_AMD) {
  5848. mmq_x = MMQ_X_Q5_K_RDNA1;
  5849. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5850. nwarps = NWARPS_Q5_K_RDNA1;
  5851. } else if (compute_capability >= CC_VOLTA) {
  5852. mmq_x = MMQ_X_Q5_K_AMPERE;
  5853. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5854. nwarps = NWARPS_Q5_K_AMPERE;
  5855. } else if (compute_capability >= MIN_CC_DP4A) {
  5856. mmq_x = MMQ_X_Q5_K_PASCAL;
  5857. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5858. nwarps = NWARPS_Q5_K_PASCAL;
  5859. } else {
  5860. GGML_ASSERT(false);
  5861. }
  5862. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5863. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5864. const dim3 block_nums(block_num_x, block_num_y, 1);
  5865. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5866. if (nrows_x % mmq_y == 0) {
  5867. const bool need_check = false;
  5868. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5869. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5870. } else {
  5871. const bool need_check = true;
  5872. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5873. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5874. }
  5875. }
  5876. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5877. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5878. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5879. int id;
  5880. CUDA_CHECK(cudaGetDevice(&id));
  5881. const int compute_capability = g_device_caps[id].cc;
  5882. int mmq_x, mmq_y, nwarps;
  5883. if (compute_capability >= CC_RDNA2) {
  5884. mmq_x = MMQ_X_Q6_K_RDNA2;
  5885. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5886. nwarps = NWARPS_Q6_K_RDNA2;
  5887. } else if (compute_capability >= CC_OFFSET_AMD) {
  5888. mmq_x = MMQ_X_Q6_K_RDNA1;
  5889. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5890. nwarps = NWARPS_Q6_K_RDNA1;
  5891. } else if (compute_capability >= CC_VOLTA) {
  5892. mmq_x = MMQ_X_Q6_K_AMPERE;
  5893. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5894. nwarps = NWARPS_Q6_K_AMPERE;
  5895. } else if (compute_capability >= MIN_CC_DP4A) {
  5896. mmq_x = MMQ_X_Q6_K_PASCAL;
  5897. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5898. nwarps = NWARPS_Q6_K_PASCAL;
  5899. } else {
  5900. GGML_ASSERT(false);
  5901. }
  5902. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5903. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5904. const dim3 block_nums(block_num_x, block_num_y, 1);
  5905. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5906. if (nrows_x % mmq_y == 0) {
  5907. const bool need_check = false;
  5908. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5909. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5910. } else {
  5911. const bool need_check = true;
  5912. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5913. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5914. }
  5915. }
  5916. static void ggml_mul_mat_p021_f16_f32_cuda(
  5917. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5918. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5919. const dim3 block_nums(1, nrows_x, nchannels_y);
  5920. const dim3 block_dims(WARP_SIZE, 1, 1);
  5921. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5922. }
  5923. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5924. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5925. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5926. const dim3 block_nums(1, nrows_x, nchannels_y);
  5927. const dim3 block_dims(WARP_SIZE, 1, 1);
  5928. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5929. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5930. }
  5931. static void ggml_cpy_f16_f32_cuda(
  5932. const char * cx, char * cdst, const int ne,
  5933. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5934. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  5935. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5936. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5937. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  5938. }
  5939. static void ggml_cpy_f32_f32_cuda(
  5940. const char * cx, char * cdst, const int ne,
  5941. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5942. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  5943. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5944. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5945. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  5946. }
  5947. static void ggml_cpy_f32_f16_cuda(
  5948. const char * cx, char * cdst, const int ne,
  5949. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5950. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  5951. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5952. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5953. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  5954. }
  5955. static void ggml_cpy_f32_q8_0_cuda(
  5956. const char * cx, char * cdst, const int ne,
  5957. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5958. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  5959. GGML_ASSERT(ne % QK8_0 == 0);
  5960. const int num_blocks = ne / QK8_0;
  5961. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5962. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  5963. }
  5964. static void ggml_cpy_f32_q4_0_cuda(
  5965. const char * cx, char * cdst, const int ne,
  5966. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5967. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  5968. GGML_ASSERT(ne % QK4_0 == 0);
  5969. const int num_blocks = ne / QK4_0;
  5970. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5971. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  5972. }
  5973. static void ggml_cpy_f32_q4_1_cuda(
  5974. const char * cx, char * cdst, const int ne,
  5975. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5976. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  5977. GGML_ASSERT(ne % QK4_1 == 0);
  5978. const int num_blocks = ne / QK4_1;
  5979. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5980. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  5981. }
  5982. static void ggml_cpy_f16_f16_cuda(
  5983. const char * cx, char * cdst, const int ne,
  5984. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  5985. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  5986. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5987. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5988. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  5989. }
  5990. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5991. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5992. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5993. }
  5994. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5995. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5996. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5997. }
  5998. template<typename T>
  5999. static void rope_cuda(
  6000. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6001. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6002. ) {
  6003. GGML_ASSERT(ncols % 2 == 0);
  6004. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6005. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6006. const dim3 block_nums(nrows, num_blocks_x, 1);
  6007. if (pos == nullptr) {
  6008. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6009. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6010. );
  6011. } else {
  6012. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6013. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6014. );
  6015. }
  6016. }
  6017. template<typename T>
  6018. static void rope_neox_cuda(
  6019. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6020. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6021. ) {
  6022. GGML_ASSERT(ncols % 2 == 0);
  6023. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6024. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6025. const dim3 block_nums(nrows, num_blocks_x, 1);
  6026. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6027. const float inv_ndims = -1.0f / n_dims;
  6028. if (pos == nullptr) {
  6029. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6030. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6031. theta_scale, inv_ndims
  6032. );
  6033. } else {
  6034. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6035. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6036. theta_scale, inv_ndims
  6037. );
  6038. }
  6039. }
  6040. static void rope_glm_f32_cuda(
  6041. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6042. float freq_base, int n_ctx, cudaStream_t stream
  6043. ) {
  6044. GGML_ASSERT(ncols % 4 == 0);
  6045. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6046. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6047. const dim3 block_nums(num_blocks_x, nrows, 1);
  6048. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6049. }
  6050. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6051. const int k_rows, const int n_heads_log2_floor, const float m0,
  6052. const float m1, cudaStream_t stream) {
  6053. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6054. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6055. const dim3 block_nums(num_blocks_x, nrows, 1);
  6056. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6057. }
  6058. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6059. const dim3 block_dims(WARP_SIZE, 1, 1);
  6060. const dim3 block_nums(nrows, 1, 1);
  6061. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6062. }
  6063. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6064. // bitonic sort requires ncols to be power of 2
  6065. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6066. const dim3 block_dims(ncols, 1, 1);
  6067. const dim3 block_nums(1, nrows, 1);
  6068. if (order == GGML_SORT_ORDER_ASC) {
  6069. k_argsort_f32_i32<GGML_SORT_ORDER_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6070. } else if (order == GGML_SORT_ORDER_DESC) {
  6071. k_argsort_f32_i32<GGML_SORT_ORDER_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6072. } else {
  6073. GGML_ASSERT(false);
  6074. }
  6075. }
  6076. 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) {
  6077. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6078. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6079. const dim3 block_nums(nrows_x, block_num_x, 1);
  6080. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6081. }
  6082. static void soft_max_f32_cuda(const float * x, const float * mask, const float * pos, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, const float max_bias, cudaStream_t stream) {
  6083. int nth = WARP_SIZE;
  6084. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6085. const dim3 block_dims(nth, 1, 1);
  6086. const dim3 block_nums(nrows_x, 1, 1);
  6087. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6088. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6089. const uint32_t n_head_kv = nrows_x/nrows_y;
  6090. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6091. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6092. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6093. if (shmem < g_device_caps[g_main_device].smpb) {
  6094. switch (ncols_x) {
  6095. case 32:
  6096. soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6097. break;
  6098. case 64:
  6099. soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6100. break;
  6101. case 128:
  6102. soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6103. break;
  6104. case 256:
  6105. soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6106. break;
  6107. case 512:
  6108. soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6109. break;
  6110. case 1024:
  6111. soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6112. break;
  6113. case 2048:
  6114. soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6115. break;
  6116. case 4096:
  6117. soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6118. break;
  6119. default:
  6120. soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6121. break;
  6122. }
  6123. } else {
  6124. const size_t shmem_low = WARP_SIZE*sizeof(float);
  6125. soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6126. }
  6127. }
  6128. template <typename T>
  6129. static void im2col_cuda(const float* x, T* dst,
  6130. int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
  6131. int64_t batch, int64_t batch_offset, int64_t offset_delta,
  6132. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  6133. const int parallel_elements = OW * KW * KH;
  6134. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  6135. dim3 block_nums(num_blocks, OH, batch * IC);
  6136. im2col_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  6137. }
  6138. // buffer pool for cuda
  6139. #define MAX_CUDA_BUFFERS 256
  6140. struct scoped_spin_lock {
  6141. std::atomic_flag& lock;
  6142. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  6143. while (lock.test_and_set(std::memory_order_acquire)) {
  6144. ; // spin
  6145. }
  6146. }
  6147. ~scoped_spin_lock() {
  6148. lock.clear(std::memory_order_release);
  6149. }
  6150. scoped_spin_lock(const scoped_spin_lock&) = delete;
  6151. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  6152. };
  6153. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  6154. // #define DEBUG_CUDA_MALLOC
  6155. struct ggml_cuda_buffer {
  6156. void * ptr = nullptr;
  6157. size_t size = 0;
  6158. };
  6159. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  6160. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  6161. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  6162. scoped_spin_lock lock(g_cuda_pool_lock);
  6163. #ifdef DEBUG_CUDA_MALLOC
  6164. int nnz = 0;
  6165. size_t max_size = 0;
  6166. #endif
  6167. size_t best_diff = 1ull << 36;
  6168. int ibest = -1;
  6169. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6170. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6171. if (b.ptr != nullptr) {
  6172. #ifdef DEBUG_CUDA_MALLOC
  6173. ++nnz;
  6174. if (b.size > max_size) max_size = b.size;
  6175. #endif
  6176. if (b.size >= size) {
  6177. size_t diff = b.size - size;
  6178. if (diff < best_diff) {
  6179. best_diff = diff;
  6180. ibest = i;
  6181. if (!best_diff) {
  6182. void * ptr = b.ptr;
  6183. *actual_size = b.size;
  6184. b.ptr = nullptr;
  6185. b.size = 0;
  6186. return ptr;
  6187. }
  6188. }
  6189. }
  6190. }
  6191. }
  6192. if (ibest >= 0) {
  6193. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  6194. void * ptr = b.ptr;
  6195. *actual_size = b.size;
  6196. b.ptr = nullptr;
  6197. b.size = 0;
  6198. return ptr;
  6199. }
  6200. void * ptr;
  6201. size_t look_ahead_size = (size_t) (1.05 * size);
  6202. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  6203. ggml_cuda_set_device(device);
  6204. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  6205. *actual_size = look_ahead_size;
  6206. g_cuda_pool_size[device] += look_ahead_size;
  6207. #ifdef DEBUG_CUDA_MALLOC
  6208. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, device, nnz,
  6209. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[device]/1024/1024), (uint32_t)(size/1024/1024));
  6210. #endif
  6211. return ptr;
  6212. }
  6213. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  6214. scoped_spin_lock lock(g_cuda_pool_lock);
  6215. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6216. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6217. if (b.ptr == nullptr) {
  6218. b.ptr = ptr;
  6219. b.size = size;
  6220. return;
  6221. }
  6222. }
  6223. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  6224. ggml_cuda_set_device(device);
  6225. CUDA_CHECK(cudaFree(ptr));
  6226. g_cuda_pool_size[device] -= size;
  6227. }
  6228. #if !defined(GGML_USE_HIPBLAS)
  6229. // pool with virtual memory
  6230. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  6231. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  6232. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  6233. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  6234. scoped_spin_lock lock(g_cuda_pool_lock);
  6235. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  6236. const size_t alignment = 128;
  6237. size = alignment * ((size + alignment - 1) / alignment);
  6238. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  6239. if (size > avail) {
  6240. // round up to the next multiple of the granularity
  6241. size_t reserve_size = size - avail;
  6242. const size_t granularity = g_device_caps[device].vmm_granularity;
  6243. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  6244. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  6245. // allocate more physical memory
  6246. CUmemAllocationProp prop = {};
  6247. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6248. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6249. prop.location.id = device;
  6250. CUmemGenericAllocationHandle handle;
  6251. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  6252. // reserve virtual address space (if not already reserved)
  6253. if (g_cuda_pool_addr[device] == 0) {
  6254. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  6255. }
  6256. // map at the end of the pool
  6257. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  6258. // the memory allocation handle is no longer needed after mapping
  6259. CU_CHECK(cuMemRelease(handle));
  6260. // set access
  6261. CUmemAccessDesc access = {};
  6262. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6263. access.location.id = device;
  6264. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  6265. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  6266. // add to the pool
  6267. g_cuda_pool_size[device] += reserve_size;
  6268. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  6269. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  6270. // (unsigned long long) (reserve_size/1024/1024));
  6271. }
  6272. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  6273. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  6274. *actual_size = size;
  6275. g_cuda_pool_used[device] += size;
  6276. #ifdef DEBUG_CUDA_MALLOC
  6277. printf("cuda pool[%d]: allocated %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  6278. #endif
  6279. return ptr;
  6280. }
  6281. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  6282. scoped_spin_lock lock(g_cuda_pool_lock);
  6283. #ifdef DEBUG_CUDA_MALLOC
  6284. printf("cuda pool[%d]: freed %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  6285. #endif
  6286. g_cuda_pool_used[device] -= size;
  6287. // all deallocations must be in reverse order of the allocations
  6288. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  6289. }
  6290. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  6291. if (g_device_caps[device].vmm) {
  6292. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  6293. } else {
  6294. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  6295. }
  6296. }
  6297. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  6298. if (g_device_caps[device].vmm) {
  6299. ggml_cuda_pool_free_vmm(device, ptr, size);
  6300. } else {
  6301. ggml_cuda_pool_free_leg(device, ptr, size);
  6302. }
  6303. }
  6304. #else
  6305. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  6306. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  6307. #endif // !defined(GGML_USE_HIPBLAS)
  6308. template<typename T>
  6309. struct cuda_pool_alloc {
  6310. int device = -1;
  6311. T * ptr = nullptr;
  6312. size_t actual_size = 0;
  6313. // size is in number of elements
  6314. T * alloc(size_t size) {
  6315. GGML_ASSERT(ptr == nullptr);
  6316. CUDA_CHECK(cudaGetDevice(&device));
  6317. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  6318. return ptr;
  6319. }
  6320. cuda_pool_alloc(size_t size) {
  6321. alloc(size);
  6322. }
  6323. ~cuda_pool_alloc() {
  6324. if (ptr != nullptr) {
  6325. ggml_cuda_pool_free(device, ptr, actual_size);
  6326. }
  6327. }
  6328. T * get() {
  6329. return ptr;
  6330. }
  6331. cuda_pool_alloc() = default;
  6332. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  6333. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  6334. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  6335. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  6336. };
  6337. static bool g_cublas_loaded = false;
  6338. GGML_CALL bool ggml_cublas_loaded(void) {
  6339. return g_cublas_loaded;
  6340. }
  6341. GGML_CALL void ggml_init_cublas() {
  6342. static bool initialized = false;
  6343. if (!initialized) {
  6344. #ifdef __HIP_PLATFORM_AMD__
  6345. // Workaround for a rocBLAS bug when using multiple graphics cards:
  6346. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  6347. rocblas_initialize();
  6348. CUDA_CHECK(cudaDeviceSynchronize());
  6349. #endif
  6350. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  6351. initialized = true;
  6352. g_cublas_loaded = false;
  6353. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  6354. return;
  6355. }
  6356. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  6357. int64_t total_vram = 0;
  6358. #if defined(GGML_CUDA_FORCE_MMQ)
  6359. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  6360. #else
  6361. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  6362. #endif
  6363. #if defined(CUDA_USE_TENSOR_CORES)
  6364. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  6365. #else
  6366. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  6367. #endif
  6368. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  6369. for (int id = 0; id < g_device_count; ++id) {
  6370. int device_vmm = 0;
  6371. #if !defined(GGML_USE_HIPBLAS)
  6372. CUdevice device;
  6373. CU_CHECK(cuDeviceGet(&device, id));
  6374. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  6375. if (device_vmm) {
  6376. CUmemAllocationProp alloc_prop = {};
  6377. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6378. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6379. alloc_prop.location.id = id;
  6380. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  6381. }
  6382. #endif // !defined(GGML_USE_HIPBLAS)
  6383. g_device_caps[id].vmm = !!device_vmm;
  6384. cudaDeviceProp prop;
  6385. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  6386. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  6387. g_default_tensor_split[id] = total_vram;
  6388. total_vram += prop.totalGlobalMem;
  6389. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6390. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  6391. #else
  6392. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  6393. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6394. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  6395. }
  6396. for (int id = 0; id < g_device_count; ++id) {
  6397. g_default_tensor_split[id] /= total_vram;
  6398. }
  6399. for (int id = 0; id < g_device_count; ++id) {
  6400. ggml_cuda_set_device(id);
  6401. // create cuda streams
  6402. for (int is = 0; is < MAX_STREAMS; ++is) {
  6403. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  6404. }
  6405. // create cublas handle
  6406. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  6407. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  6408. }
  6409. // configure logging to stdout
  6410. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  6411. initialized = true;
  6412. g_cublas_loaded = true;
  6413. }
  6414. }
  6415. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  6416. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6417. return nullptr;
  6418. }
  6419. void * ptr = nullptr;
  6420. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6421. if (err != cudaSuccess) {
  6422. // clear the error
  6423. cudaGetLastError();
  6424. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6425. size/1024.0/1024.0, cudaGetErrorString(err));
  6426. return nullptr;
  6427. }
  6428. return ptr;
  6429. }
  6430. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  6431. CUDA_CHECK(cudaFreeHost(ptr));
  6432. }
  6433. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6434. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6435. cudaMemcpyKind kind;
  6436. char * src_ptr;
  6437. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  6438. kind = cudaMemcpyHostToDevice;
  6439. src_ptr = (char *) src->data;
  6440. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  6441. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6442. kind = cudaMemcpyDeviceToDevice;
  6443. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6444. int id;
  6445. CUDA_CHECK(cudaGetDevice(&id));
  6446. src_ptr = (char *) extra->data_device[id];
  6447. } else {
  6448. GGML_ASSERT(false);
  6449. }
  6450. char * dst_ptr = (char *) dst;
  6451. const int64_t ne0 = src->ne[0];
  6452. const int64_t nb0 = src->nb[0];
  6453. const int64_t nb1 = src->nb[1];
  6454. const int64_t nb2 = src->nb[2];
  6455. const int64_t nb3 = src->nb[3];
  6456. const enum ggml_type type = src->type;
  6457. const int64_t ts = ggml_type_size(type);
  6458. const int64_t bs = ggml_blck_size(type);
  6459. int64_t i1_diff = i1_high - i1_low;
  6460. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6461. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6462. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6463. } else if (nb0 == ts) {
  6464. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6465. } else {
  6466. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6467. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6468. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6469. // pretend the row is a matrix with cols=1
  6470. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6471. if (r != cudaSuccess) return r;
  6472. }
  6473. return cudaSuccess;
  6474. }
  6475. }
  6476. static void ggml_cuda_op_get_rows(
  6477. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6478. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6479. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6480. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6481. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6482. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6483. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6484. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6485. switch (src0->type) {
  6486. case GGML_TYPE_F16:
  6487. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6488. break;
  6489. case GGML_TYPE_F32:
  6490. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6491. break;
  6492. case GGML_TYPE_Q4_0:
  6493. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6494. break;
  6495. case GGML_TYPE_Q4_1:
  6496. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6497. break;
  6498. case GGML_TYPE_Q5_0:
  6499. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6500. break;
  6501. case GGML_TYPE_Q5_1:
  6502. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6503. break;
  6504. case GGML_TYPE_Q8_0:
  6505. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6506. break;
  6507. default:
  6508. // TODO: k-quants
  6509. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6510. GGML_ASSERT(false);
  6511. break;
  6512. }
  6513. }
  6514. template<class op>
  6515. static void ggml_cuda_op_bin_bcast(
  6516. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6517. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6518. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6519. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6520. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6521. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6522. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6523. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6524. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6525. } else {
  6526. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6527. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6528. GGML_ASSERT(false);
  6529. }
  6530. }
  6531. static void ggml_cuda_op_repeat(
  6532. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6533. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6534. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6535. (void) src1;
  6536. (void) src1_d;
  6537. }
  6538. static void ggml_cuda_op_add(
  6539. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6540. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6541. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6542. }
  6543. static void ggml_cuda_op_acc(
  6544. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6545. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6546. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6547. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6548. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6549. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6550. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6551. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6552. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6553. int offset = dst->op_params[3] / 4; // offset in bytes
  6554. 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);
  6555. (void) dst;
  6556. }
  6557. static void ggml_cuda_op_mul(
  6558. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6559. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6560. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6561. }
  6562. static void ggml_cuda_op_div(
  6563. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6564. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6565. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6566. }
  6567. static void ggml_cuda_op_gelu(
  6568. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6569. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6570. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6571. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6572. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6573. (void) src1;
  6574. (void) dst;
  6575. (void) src1_dd;
  6576. }
  6577. static void ggml_cuda_op_silu(
  6578. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6579. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6580. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6581. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6582. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6583. (void) src1;
  6584. (void) dst;
  6585. (void) src1_dd;
  6586. }
  6587. static void ggml_cuda_op_gelu_quick(
  6588. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6589. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6590. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6591. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6592. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6593. (void) src1;
  6594. (void) dst;
  6595. (void) src1_dd;
  6596. }
  6597. static void ggml_cuda_op_tanh(
  6598. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6599. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6600. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6601. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6602. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6603. (void) src1;
  6604. (void) dst;
  6605. (void) src1_dd;
  6606. }
  6607. static void ggml_cuda_op_relu(
  6608. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6609. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6610. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6611. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6612. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6613. (void) src1;
  6614. (void) dst;
  6615. (void) src1_dd;
  6616. }
  6617. static void ggml_cuda_op_hardsigmoid(
  6618. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6619. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6620. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6621. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6622. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6623. (void) src1;
  6624. (void) dst;
  6625. (void) src1_dd;
  6626. }
  6627. static void ggml_cuda_op_hardswish(
  6628. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6629. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6630. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6631. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6632. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6633. (void) src1;
  6634. (void) dst;
  6635. (void) src1_dd;
  6636. }
  6637. static void ggml_cuda_op_leaky_relu(
  6638. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6639. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6640. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6641. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6642. float negative_slope;
  6643. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6644. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6645. (void) src1;
  6646. (void) dst;
  6647. (void) src1_dd;
  6648. }
  6649. static void ggml_cuda_op_sqr(
  6650. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6651. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6652. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6653. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6654. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6655. (void) src1;
  6656. (void) dst;
  6657. (void) src1_dd;
  6658. }
  6659. static void ggml_cuda_op_norm(
  6660. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6661. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6662. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6663. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6664. const int64_t ne00 = src0->ne[0];
  6665. const int64_t nrows = ggml_nrows(src0);
  6666. float eps;
  6667. memcpy(&eps, dst->op_params, sizeof(float));
  6668. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6669. (void) src1;
  6670. (void) dst;
  6671. (void) src1_dd;
  6672. }
  6673. static void ggml_cuda_op_group_norm(
  6674. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6675. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6676. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6677. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6678. int num_groups = dst->op_params[0];
  6679. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6680. group_norm_f32_cuda(src0_dd, dst_dd, num_groups * src0->ne[3], group_size, ggml_nelements(src0), main_stream);
  6681. (void) src1;
  6682. (void) dst;
  6683. (void) src1_dd;
  6684. }
  6685. static void ggml_cuda_op_concat(
  6686. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6687. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6688. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6689. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6690. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6691. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6692. 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);
  6693. }
  6694. (void) src1;
  6695. (void) dst;
  6696. }
  6697. static void ggml_cuda_op_upscale(
  6698. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6699. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6700. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6701. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6702. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6703. const int scale_factor = dst->op_params[0];
  6704. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], scale_factor, main_stream);
  6705. (void) src1;
  6706. (void) dst;
  6707. (void) src1_dd;
  6708. }
  6709. static void ggml_cuda_op_pad(
  6710. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6711. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6712. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6713. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6714. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6715. pad_f32_cuda(src0_dd, dst_dd,
  6716. src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
  6717. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], main_stream);
  6718. (void) src1;
  6719. (void) dst;
  6720. (void) src1_dd;
  6721. }
  6722. static void ggml_cuda_op_arange(
  6723. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6724. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6725. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6726. float start;
  6727. float stop;
  6728. float step;
  6729. memcpy(&start, (float *)dst->op_params + 0, sizeof(float));
  6730. memcpy(&stop, (float *)dst->op_params + 1, sizeof(float));
  6731. memcpy(&step, (float *)dst->op_params + 2, sizeof(float));
  6732. int64_t steps = (int64_t)ceil((stop - start) / step);
  6733. GGML_ASSERT(ggml_nelements(dst) == steps);
  6734. arange_f32_cuda(dst_dd, dst->ne[0], start, step, main_stream);
  6735. (void) src0;
  6736. (void) src1;
  6737. (void) src0_dd;
  6738. (void) src1_dd;
  6739. }
  6740. static void ggml_cuda_op_timestep_embedding(
  6741. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6742. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6743. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6744. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6745. const int dim = dst->op_params[0];
  6746. const int max_period = dst->op_params[1];
  6747. timestep_embedding_f32_cuda(src0_dd, dst_dd, src0->ne[0], dst->nb[1], dim, max_period, main_stream);
  6748. (void) src1;
  6749. (void) dst;
  6750. (void) src1_dd;
  6751. }
  6752. static void ggml_cuda_op_rms_norm(
  6753. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6754. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6755. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6756. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6757. const int64_t ne00 = src0->ne[0];
  6758. const int64_t nrows = ggml_nrows(src0);
  6759. float eps;
  6760. memcpy(&eps, dst->op_params, sizeof(float));
  6761. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6762. (void) src1;
  6763. (void) dst;
  6764. (void) src1_dd;
  6765. }
  6766. static void ggml_cuda_op_mul_mat_q(
  6767. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6768. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6769. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6770. const int64_t ne00 = src0->ne[0];
  6771. const int64_t ne10 = src1->ne[0];
  6772. GGML_ASSERT(ne10 % QK8_1 == 0);
  6773. const int64_t ne0 = dst->ne[0];
  6774. const int64_t row_diff = row_high - row_low;
  6775. int id;
  6776. CUDA_CHECK(cudaGetDevice(&id));
  6777. // the main device has a larger memory buffer to hold the results from all GPUs
  6778. // nrows_dst == nrows of the matrix that the kernel writes into
  6779. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  6780. switch (src0->type) {
  6781. case GGML_TYPE_Q4_0:
  6782. 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);
  6783. break;
  6784. case GGML_TYPE_Q4_1:
  6785. 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);
  6786. break;
  6787. case GGML_TYPE_Q5_0:
  6788. 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);
  6789. break;
  6790. case GGML_TYPE_Q5_1:
  6791. 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);
  6792. break;
  6793. case GGML_TYPE_Q8_0:
  6794. 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);
  6795. break;
  6796. case GGML_TYPE_Q2_K:
  6797. 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);
  6798. break;
  6799. case GGML_TYPE_Q3_K:
  6800. 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);
  6801. break;
  6802. case GGML_TYPE_Q4_K:
  6803. 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);
  6804. break;
  6805. case GGML_TYPE_Q5_K:
  6806. 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);
  6807. break;
  6808. case GGML_TYPE_Q6_K:
  6809. 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);
  6810. break;
  6811. default:
  6812. GGML_ASSERT(false);
  6813. break;
  6814. }
  6815. (void) src1;
  6816. (void) dst;
  6817. (void) src1_ddf_i;
  6818. }
  6819. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  6820. int64_t min_compute_capability = INT_MAX;
  6821. int64_t max_compute_capability = INT_MIN;
  6822. for (int id = 0; id < g_device_count; ++id) {
  6823. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  6824. if (min_compute_capability > g_device_caps[id].cc) {
  6825. min_compute_capability = g_device_caps[id].cc;
  6826. }
  6827. if (max_compute_capability < g_device_caps[id].cc) {
  6828. max_compute_capability = g_device_caps[id].cc;
  6829. }
  6830. }
  6831. }
  6832. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6833. switch(type) {
  6834. case GGML_TYPE_Q4_0:
  6835. case GGML_TYPE_Q4_1:
  6836. case GGML_TYPE_Q5_0:
  6837. case GGML_TYPE_Q5_1:
  6838. case GGML_TYPE_Q8_0:
  6839. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  6840. case GGML_TYPE_F16:
  6841. case GGML_TYPE_F32:
  6842. return 1;
  6843. case GGML_TYPE_Q2_K:
  6844. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  6845. case GGML_TYPE_Q3_K:
  6846. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  6847. case GGML_TYPE_Q4_K:
  6848. case GGML_TYPE_Q5_K:
  6849. case GGML_TYPE_Q6_K:
  6850. case GGML_TYPE_IQ2_XXS:
  6851. case GGML_TYPE_IQ2_XS:
  6852. case GGML_TYPE_IQ2_S:
  6853. case GGML_TYPE_IQ3_XXS:
  6854. case GGML_TYPE_IQ1_S:
  6855. case GGML_TYPE_IQ4_NL:
  6856. case GGML_TYPE_IQ4_XS:
  6857. case GGML_TYPE_IQ3_S:
  6858. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  6859. default:
  6860. GGML_ASSERT(false);
  6861. }
  6862. #else
  6863. switch(type) {
  6864. case GGML_TYPE_Q4_0:
  6865. case GGML_TYPE_Q4_1:
  6866. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  6867. case GGML_TYPE_Q5_0:
  6868. case GGML_TYPE_Q5_1:
  6869. case GGML_TYPE_Q8_0:
  6870. return 64;
  6871. case GGML_TYPE_F16:
  6872. case GGML_TYPE_F32:
  6873. return 1;
  6874. case GGML_TYPE_Q2_K:
  6875. case GGML_TYPE_Q3_K:
  6876. case GGML_TYPE_Q4_K:
  6877. case GGML_TYPE_Q5_K:
  6878. case GGML_TYPE_IQ2_XXS:
  6879. case GGML_TYPE_IQ2_XS:
  6880. case GGML_TYPE_IQ2_S:
  6881. case GGML_TYPE_IQ3_XXS:
  6882. case GGML_TYPE_IQ1_S:
  6883. case GGML_TYPE_IQ4_NL:
  6884. case GGML_TYPE_IQ4_XS:
  6885. case GGML_TYPE_IQ3_S:
  6886. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  6887. case GGML_TYPE_Q6_K:
  6888. return 64;
  6889. default:
  6890. GGML_ASSERT(false);
  6891. }
  6892. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6893. }
  6894. static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split, int id) {
  6895. const int64_t nrows = ggml_nrows(tensor);
  6896. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  6897. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  6898. *row_low -= *row_low % rounding;
  6899. if (id == g_device_count - 1) {
  6900. *row_high = nrows;
  6901. } else {
  6902. *row_high = nrows*tensor_split[id + 1];
  6903. *row_high -= *row_high % rounding;
  6904. }
  6905. }
  6906. static void ggml_cuda_op_mul_mat_vec_q(
  6907. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6908. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6909. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6910. const int64_t ne00 = src0->ne[0];
  6911. const int64_t row_diff = row_high - row_low;
  6912. const int64_t ne10 = src1->ne[0];
  6913. GGML_ASSERT(ne10 % QK8_1 == 0);
  6914. const int64_t ne0 = dst->ne[0];
  6915. int id;
  6916. CUDA_CHECK(cudaGetDevice(&id));
  6917. // the main device has a larger memory buffer to hold the results from all GPUs
  6918. // nrows_dst == nrows of the matrix that the kernel writes into
  6919. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  6920. switch (src0->type) {
  6921. case GGML_TYPE_Q4_0:
  6922. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  6923. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6924. break;
  6925. case GGML_TYPE_Q4_1:
  6926. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  6927. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6928. break;
  6929. case GGML_TYPE_Q5_0:
  6930. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  6931. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6932. break;
  6933. case GGML_TYPE_Q5_1:
  6934. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  6935. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6936. break;
  6937. case GGML_TYPE_Q8_0:
  6938. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  6939. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6940. break;
  6941. case GGML_TYPE_Q2_K:
  6942. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  6943. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6944. break;
  6945. case GGML_TYPE_Q3_K:
  6946. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  6947. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6948. break;
  6949. case GGML_TYPE_Q4_K:
  6950. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  6951. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6952. break;
  6953. case GGML_TYPE_Q5_K:
  6954. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  6955. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6956. break;
  6957. case GGML_TYPE_Q6_K:
  6958. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  6959. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6960. break;
  6961. case GGML_TYPE_IQ2_XXS:
  6962. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  6963. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6964. break;
  6965. case GGML_TYPE_IQ2_XS:
  6966. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  6967. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6968. break;
  6969. case GGML_TYPE_IQ2_S:
  6970. mul_mat_vec_q_cuda<QK_K, QI2_S, block_iq2_s, 1, vec_dot_iq2_s_q8_1>
  6971. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6972. break;
  6973. case GGML_TYPE_IQ3_XXS:
  6974. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  6975. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6976. break;
  6977. case GGML_TYPE_IQ1_S:
  6978. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  6979. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6980. break;
  6981. case GGML_TYPE_IQ4_NL:
  6982. mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
  6983. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6984. break;
  6985. case GGML_TYPE_IQ4_XS:
  6986. mul_mat_vec_q_cuda<QK_K, QI4_XS, block_iq4_xs, 1, vec_dot_iq4_xs_q8_1>
  6987. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6988. break;
  6989. case GGML_TYPE_IQ3_S:
  6990. mul_mat_vec_q_cuda<QK_K, QI3_XS, block_iq3_s, 1, vec_dot_iq3_s_q8_1>
  6991. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  6992. break;
  6993. default:
  6994. GGML_ASSERT(false);
  6995. break;
  6996. }
  6997. (void) src1;
  6998. (void) dst;
  6999. (void) src1_ddf_i;
  7000. (void) src1_ncols;
  7001. (void) src1_padded_row_size;
  7002. }
  7003. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7004. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7005. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7006. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7007. const int64_t ne00 = src0->ne[0];
  7008. const int64_t row_diff = row_high - row_low;
  7009. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7010. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7011. #ifdef GGML_CUDA_F16
  7012. cuda_pool_alloc<half> src1_dfloat_a;
  7013. half * src1_dfloat = nullptr; // dfloat == half
  7014. bool src1_convert_f16 =
  7015. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7016. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7017. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7018. if (src1_convert_f16) {
  7019. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7020. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7021. GGML_ASSERT(to_fp16_cuda != nullptr);
  7022. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7023. }
  7024. #else
  7025. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7026. #endif // GGML_CUDA_F16
  7027. switch (src0->type) {
  7028. case GGML_TYPE_Q4_0:
  7029. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7030. break;
  7031. case GGML_TYPE_Q4_1:
  7032. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7033. break;
  7034. case GGML_TYPE_Q5_0:
  7035. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7036. break;
  7037. case GGML_TYPE_Q5_1:
  7038. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7039. break;
  7040. case GGML_TYPE_Q8_0:
  7041. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7042. break;
  7043. case GGML_TYPE_Q2_K:
  7044. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7045. break;
  7046. case GGML_TYPE_Q3_K:
  7047. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7048. break;
  7049. case GGML_TYPE_Q4_K:
  7050. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7051. break;
  7052. case GGML_TYPE_Q5_K:
  7053. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7054. break;
  7055. case GGML_TYPE_Q6_K:
  7056. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7057. break;
  7058. case GGML_TYPE_F16:
  7059. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7060. break;
  7061. default:
  7062. GGML_ASSERT(false);
  7063. break;
  7064. }
  7065. (void) src1;
  7066. (void) dst;
  7067. (void) src1_ddq_i;
  7068. (void) src1_ncols;
  7069. (void) src1_padded_row_size;
  7070. }
  7071. static void ggml_cuda_op_mul_mat_cublas(
  7072. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7073. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7074. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7075. GGML_ASSERT(src0_dd_i != nullptr);
  7076. GGML_ASSERT(src1_ddf_i != nullptr);
  7077. GGML_ASSERT(dst_dd_i != nullptr);
  7078. const int64_t ne00 = src0->ne[0];
  7079. const int64_t ne10 = src1->ne[0];
  7080. const int64_t ne0 = dst->ne[0];
  7081. const int64_t row_diff = row_high - row_low;
  7082. int id;
  7083. CUDA_CHECK(cudaGetDevice(&id));
  7084. // the main device has a larger memory buffer to hold the results from all GPUs
  7085. // ldc == nrows of the matrix that cuBLAS writes into
  7086. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7087. const int compute_capability = g_device_caps[id].cc;
  7088. 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) {
  7089. //printf("this branch\n");
  7090. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7091. cuda_pool_alloc<half> src0_as_f16;
  7092. if (src0->type != GGML_TYPE_F16) {
  7093. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7094. GGML_ASSERT(to_fp16_cuda != nullptr);
  7095. size_t ne = row_diff*ne00;
  7096. src0_as_f16.alloc(ne);
  7097. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7098. }
  7099. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7100. cuda_pool_alloc<half> src1_as_f16;
  7101. if (src1->type != GGML_TYPE_F16) {
  7102. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7103. GGML_ASSERT(to_fp16_cuda != nullptr);
  7104. size_t ne = src1_ncols*ne10;
  7105. src1_as_f16.alloc(ne);
  7106. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7107. }
  7108. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7109. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7110. const half alpha_f16 = 1.0f;
  7111. const half beta_f16 = 0.0f;
  7112. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7113. CUBLAS_CHECK(
  7114. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7115. row_diff, src1_ncols, ne10,
  7116. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7117. src1_ptr, CUDA_R_16F, ne10,
  7118. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7119. CUBLAS_COMPUTE_16F,
  7120. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7121. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7122. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7123. } else {
  7124. cuda_pool_alloc<float> src0_ddq_as_f32;
  7125. cuda_pool_alloc<float> src1_ddq_as_f32;
  7126. if (src0->type != GGML_TYPE_F32) {
  7127. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7128. GGML_ASSERT(to_fp32_cuda != nullptr);
  7129. src0_ddq_as_f32.alloc(row_diff*ne00);
  7130. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7131. }
  7132. if (src1->type != GGML_TYPE_F32) {
  7133. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7134. GGML_ASSERT(to_fp32_cuda != nullptr);
  7135. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7136. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7137. }
  7138. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7139. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7140. const float alpha = 1.0f;
  7141. const float beta = 0.0f;
  7142. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7143. CUBLAS_CHECK(
  7144. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7145. row_diff, src1_ncols, ne10,
  7146. &alpha, src0_ddf_i, ne00,
  7147. src1_ddf1_i, ne10,
  7148. &beta, dst_dd_i, ldc));
  7149. }
  7150. (void) dst;
  7151. (void) src1_ddq_i;
  7152. (void) src1_padded_row_size;
  7153. }
  7154. static void ggml_cuda_op_rope(
  7155. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7156. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7157. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7158. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7159. GGML_ASSERT(src0->type == dst->type);
  7160. const int64_t ne00 = src0->ne[0];
  7161. const int64_t ne01 = src0->ne[1];
  7162. const int64_t ne2 = dst->ne[2];
  7163. const int64_t nrows = ggml_nrows(src0);
  7164. //const int n_past = ((int32_t *) dst->op_params)[0];
  7165. const int n_dims = ((int32_t *) dst->op_params)[1];
  7166. const int mode = ((int32_t *) dst->op_params)[2];
  7167. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7168. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7169. // RoPE alteration for extended context
  7170. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7171. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7172. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7173. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7174. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7175. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7176. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7177. const int32_t * pos = nullptr;
  7178. if ((mode & 1) == 0) {
  7179. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7180. GGML_ASSERT(src1->ne[0] == ne2);
  7181. pos = (const int32_t *) src1_dd;
  7182. }
  7183. const bool is_neox = mode & 2;
  7184. const bool is_glm = mode & 4;
  7185. rope_corr_dims corr_dims;
  7186. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7187. // compute
  7188. if (is_glm) {
  7189. GGML_ASSERT(false);
  7190. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7191. } else if (is_neox) {
  7192. if (src0->type == GGML_TYPE_F32) {
  7193. rope_neox_cuda(
  7194. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7195. attn_factor, corr_dims, main_stream
  7196. );
  7197. } else if (src0->type == GGML_TYPE_F16) {
  7198. rope_neox_cuda(
  7199. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7200. attn_factor, corr_dims, main_stream
  7201. );
  7202. } else {
  7203. GGML_ASSERT(false);
  7204. }
  7205. } else {
  7206. if (src0->type == GGML_TYPE_F32) {
  7207. rope_cuda(
  7208. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7209. attn_factor, corr_dims, main_stream
  7210. );
  7211. } else if (src0->type == GGML_TYPE_F16) {
  7212. rope_cuda(
  7213. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7214. attn_factor, corr_dims, main_stream
  7215. );
  7216. } else {
  7217. GGML_ASSERT(false);
  7218. }
  7219. }
  7220. (void) src1;
  7221. (void) dst;
  7222. (void) src1_dd;
  7223. }
  7224. static void ggml_cuda_op_alibi(
  7225. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7226. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7227. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7228. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7229. const int64_t ne00 = src0->ne[0];
  7230. const int64_t ne01 = src0->ne[1];
  7231. const int64_t ne02 = src0->ne[2];
  7232. const int64_t nrows = ggml_nrows(src0);
  7233. //const int n_past = ((int32_t *) dst->op_params)[0];
  7234. const int n_head = ((int32_t *) dst->op_params)[1];
  7235. float max_bias;
  7236. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7237. //GGML_ASSERT(ne01 + n_past == ne00);
  7238. GGML_ASSERT(n_head == ne02);
  7239. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7240. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7241. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7242. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7243. (void) src1;
  7244. (void) src1_dd;
  7245. }
  7246. static void ggml_cuda_op_pool2d(
  7247. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7248. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7249. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7250. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7251. const int32_t * opts = (const int32_t *)dst->op_params;
  7252. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  7253. const int k0 = opts[1];
  7254. const int k1 = opts[2];
  7255. const int s0 = opts[3];
  7256. const int s1 = opts[4];
  7257. const int p0 = opts[5];
  7258. const int p1 = opts[6];
  7259. const int64_t IH = src0->ne[1];
  7260. const int64_t IW = src0->ne[0];
  7261. const int64_t N = dst->ne[3];
  7262. const int64_t OC = dst->ne[2];
  7263. const int64_t OH = dst->ne[1];
  7264. const int64_t OW = dst->ne[0];
  7265. const int parallel_elements = N * OC * OH * OW;
  7266. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  7267. dim3 block_nums(num_blocks);
  7268. pool2d_nchw_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, main_stream>>>(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0, parallel_elements, src0_dd, dst_dd, op);
  7269. (void) src1;
  7270. (void) src1_dd;
  7271. }
  7272. static void ggml_cuda_op_im2col(
  7273. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7274. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7275. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7276. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7277. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  7278. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7279. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  7280. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  7281. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  7282. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  7283. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  7284. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  7285. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  7286. const int64_t IH = is_2D ? src1->ne[1] : 1;
  7287. const int64_t IW = src1->ne[0];
  7288. const int64_t KH = is_2D ? src0->ne[1] : 1;
  7289. const int64_t KW = src0->ne[0];
  7290. const int64_t OH = is_2D ? dst->ne[2] : 1;
  7291. const int64_t OW = dst->ne[1];
  7292. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7293. const int64_t batch = src1->ne[3];
  7294. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  7295. if(dst->type == GGML_TYPE_F16) {
  7296. im2col_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7297. } else {
  7298. im2col_cuda(src1_dd, (float*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7299. }
  7300. (void) src0;
  7301. (void) src0_dd;
  7302. }
  7303. static void ggml_cuda_op_sum_rows(
  7304. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7305. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7306. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7307. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7308. const int64_t ncols = src0->ne[0];
  7309. const int64_t nrows = ggml_nrows(src0);
  7310. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  7311. (void) src1;
  7312. (void) dst;
  7313. (void) src1_dd;
  7314. }
  7315. static void ggml_cuda_op_argsort(
  7316. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7317. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7318. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7319. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  7320. const int64_t ncols = src0->ne[0];
  7321. const int64_t nrows = ggml_nrows(src0);
  7322. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  7323. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  7324. (void) src1;
  7325. (void) dst;
  7326. (void) src1_dd;
  7327. }
  7328. static void ggml_cuda_op_diag_mask_inf(
  7329. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7330. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7331. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7332. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7333. const int64_t ne00 = src0->ne[0];
  7334. const int64_t ne01 = src0->ne[1];
  7335. const int nrows0 = ggml_nrows(src0);
  7336. const int n_past = ((int32_t *) dst->op_params)[0];
  7337. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  7338. (void) src1;
  7339. (void) dst;
  7340. (void) src1_dd;
  7341. }
  7342. static void ggml_cuda_op_soft_max(
  7343. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7344. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7345. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7346. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7347. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  7348. const int64_t ne00 = src0->ne[0];
  7349. const int64_t nrows_x = ggml_nrows(src0);
  7350. const int64_t nrows_y = src0->ne[1];
  7351. float scale = 1.0f;
  7352. float max_bias = 0.0f;
  7353. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7354. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7355. // positions tensor
  7356. float * src2_dd = nullptr;
  7357. cuda_pool_alloc<float> src2_f;
  7358. ggml_tensor * src2 = dst->src[2];
  7359. const bool use_src2 = src2 != nullptr;
  7360. if (use_src2) {
  7361. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  7362. if (src2_on_device) {
  7363. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  7364. src2_dd = (float *) src2_extra->data_device[g_main_device];
  7365. } else {
  7366. src2_dd = src2_f.alloc(ggml_nelements(src2));
  7367. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  7368. }
  7369. }
  7370. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  7371. }
  7372. static void ggml_cuda_op_scale(
  7373. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7374. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7375. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7376. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7377. float scale;
  7378. memcpy(&scale, dst->op_params, sizeof(float));
  7379. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  7380. CUDA_CHECK(cudaGetLastError());
  7381. (void) src1;
  7382. (void) dst;
  7383. (void) src1_dd;
  7384. }
  7385. static void ggml_cuda_op_clamp(
  7386. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7387. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7388. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7389. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7390. float min;
  7391. float max;
  7392. memcpy(&min, dst->op_params, sizeof(float));
  7393. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7394. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  7395. CUDA_CHECK(cudaGetLastError());
  7396. (void) src1;
  7397. (void) dst;
  7398. (void) src1_dd;
  7399. }
  7400. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  7401. const int64_t nrows0 = ggml_nrows(src0);
  7402. const bool use_src1 = src1 != nullptr;
  7403. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  7404. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7405. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7406. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7407. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  7408. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7409. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  7410. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  7411. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  7412. // dd = data device
  7413. float * src0_ddf = nullptr;
  7414. float * src1_ddf = nullptr;
  7415. float * dst_ddf = nullptr;
  7416. cuda_pool_alloc<float> src0_f;
  7417. cuda_pool_alloc<float> src1_f;
  7418. cuda_pool_alloc<float> dst_f;
  7419. ggml_cuda_set_device(g_main_device);
  7420. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7421. if (src0_on_device) {
  7422. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  7423. } else {
  7424. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  7425. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  7426. }
  7427. if (use_src1) {
  7428. if (src1_on_device) {
  7429. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7430. } else {
  7431. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  7432. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  7433. }
  7434. }
  7435. if (dst_on_device) {
  7436. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7437. } else {
  7438. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7439. }
  7440. // do the computation
  7441. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7442. CUDA_CHECK(cudaGetLastError());
  7443. // copy dst to host if necessary
  7444. if (!dst_on_device) {
  7445. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7446. }
  7447. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  7448. CUDA_CHECK(cudaDeviceSynchronize());
  7449. }
  7450. }
  7451. static void ggml_cuda_set_peer_access(const int n_tokens) {
  7452. static bool peer_access_enabled = false;
  7453. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  7454. if (peer_access_enabled == enable_peer_access) {
  7455. return;
  7456. }
  7457. #ifdef NDEBUG
  7458. for (int id = 0; id < g_device_count; ++id) {
  7459. ggml_cuda_set_device(id);
  7460. CUDA_CHECK(cudaDeviceSynchronize());
  7461. }
  7462. for (int id = 0; id < g_device_count; ++id) {
  7463. ggml_cuda_set_device(id);
  7464. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  7465. if (id == id_other) {
  7466. continue;
  7467. }
  7468. if (id != g_main_device && id_other != g_main_device) {
  7469. continue;
  7470. }
  7471. int can_access_peer;
  7472. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  7473. if (can_access_peer) {
  7474. if (enable_peer_access) {
  7475. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  7476. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  7477. CUDA_CHECK(err);
  7478. }
  7479. } else {
  7480. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  7481. if (err != cudaErrorPeerAccessNotEnabled) {
  7482. CUDA_CHECK(err);
  7483. }
  7484. }
  7485. }
  7486. }
  7487. }
  7488. #endif // NDEBUG
  7489. peer_access_enabled = enable_peer_access;
  7490. }
  7491. // FIXME: move this somewhere else
  7492. struct ggml_backend_cuda_split_buffer_type_context {
  7493. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7494. };
  7495. static void ggml_cuda_op_mul_mat(
  7496. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  7497. const bool convert_src1_to_q8_1) {
  7498. const int64_t ne00 = src0->ne[0];
  7499. const int64_t ne01 = src0->ne[1];
  7500. const int64_t ne02 = src0->ne[2];
  7501. const int64_t ne03 = src0->ne[3];
  7502. const int64_t ne10 = src1->ne[0];
  7503. const int64_t ne11 = src1->ne[1];
  7504. const int64_t ne12 = src1->ne[2];
  7505. const int64_t ne13 = src1->ne[3];
  7506. const int64_t nrows1 = ggml_nrows(src1);
  7507. GGML_ASSERT(ne03 == ne13);
  7508. const int64_t ne0 = dst->ne[0];
  7509. const int64_t ne1 = dst->ne[1];
  7510. const int nb2 = dst->nb[2];
  7511. const int nb3 = dst->nb[3];
  7512. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7513. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7514. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  7515. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  7516. const int64_t i02_divisor = ne12 / ne02;
  7517. const size_t src0_ts = ggml_type_size(src0->type);
  7518. const size_t src0_bs = ggml_blck_size(src0->type);
  7519. const size_t q8_1_ts = sizeof(block_q8_1);
  7520. const size_t q8_1_bs = QK8_1;
  7521. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7522. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7523. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7524. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  7525. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  7526. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  7527. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  7528. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  7529. GGML_ASSERT(!(split && ne02 > 1));
  7530. GGML_ASSERT(!(split && ne03 > 1));
  7531. GGML_ASSERT(!(split && ne02 < ne12));
  7532. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7533. if (split) {
  7534. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  7535. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  7536. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  7537. tensor_split = buft_ctx->tensor_split;
  7538. }
  7539. struct dev_data {
  7540. cuda_pool_alloc<char> src0_dd_alloc;
  7541. cuda_pool_alloc<float> src1_ddf_alloc;
  7542. cuda_pool_alloc<char> src1_ddq_alloc;
  7543. cuda_pool_alloc<float> dst_dd_alloc;
  7544. char * src0_dd = nullptr;
  7545. float * src1_ddf = nullptr; // float
  7546. char * src1_ddq = nullptr; // q8_1
  7547. float * dst_dd = nullptr;
  7548. int64_t row_low;
  7549. int64_t row_high;
  7550. };
  7551. dev_data dev[GGML_CUDA_MAX_DEVICES];
  7552. int used_devices = 0;
  7553. for (int id = 0; id < g_device_count; ++id) {
  7554. // by default, use all rows
  7555. dev[id].row_low = 0;
  7556. dev[id].row_high = ne01;
  7557. // for multi GPU, get the row boundaries from tensor split
  7558. // and round to mul_mat_q tile sizes
  7559. if (split) {
  7560. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  7561. if (id != 0) {
  7562. dev[id].row_low = ne01*tensor_split[id];
  7563. if (dev[id].row_low < ne01) {
  7564. dev[id].row_low -= dev[id].row_low % rounding;
  7565. }
  7566. }
  7567. if (id != g_device_count - 1) {
  7568. dev[id].row_high = ne01*tensor_split[id + 1];
  7569. if (dev[id].row_high < ne01) {
  7570. dev[id].row_high -= dev[id].row_high % rounding;
  7571. }
  7572. }
  7573. }
  7574. }
  7575. for (int id = 0; id < g_device_count; ++id) {
  7576. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7577. continue;
  7578. }
  7579. used_devices++;
  7580. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7581. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7582. ggml_cuda_set_device(id);
  7583. cudaStream_t stream = g_cudaStreams[id][0];
  7584. if (src0_on_device && src0_is_contiguous) {
  7585. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  7586. } else {
  7587. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  7588. }
  7589. if (src1_on_device && src1_is_contiguous) {
  7590. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7591. } else {
  7592. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7593. }
  7594. if (convert_src1_to_q8_1) {
  7595. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7596. if (src1_on_device && src1_is_contiguous) {
  7597. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7598. CUDA_CHECK(cudaGetLastError());
  7599. }
  7600. }
  7601. if (dst_on_device) {
  7602. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7603. } else {
  7604. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7605. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7606. }
  7607. }
  7608. // if multiple devices are used they need to wait for the main device
  7609. // here an event is recorded that signals that the main device has finished calculating the input data
  7610. if (split && used_devices > 1) {
  7611. ggml_cuda_set_device(g_main_device);
  7612. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7613. }
  7614. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7615. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7616. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7617. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7618. for (int id = 0; id < g_device_count; ++id) {
  7619. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7620. continue;
  7621. }
  7622. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7623. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7624. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7625. ggml_cuda_set_device(id);
  7626. cudaStream_t stream = g_cudaStreams[id][is];
  7627. // wait for main GPU data if necessary
  7628. if (split && (id != g_main_device || is != 0)) {
  7629. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7630. }
  7631. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7632. const int64_t i03 = i0 / ne12;
  7633. const int64_t i02 = i0 % ne12;
  7634. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7635. // for split tensors the data begins at i0 == i0_offset_low
  7636. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7637. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7638. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7639. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7640. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7641. // in that case an offset on dst_ddf_i is needed
  7642. if (dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device) {
  7643. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7644. }
  7645. // copy src0, src1 to device if necessary
  7646. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  7647. if (id != g_main_device) {
  7648. if (convert_src1_to_q8_1) {
  7649. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7650. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7651. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7652. } else {
  7653. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7654. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7655. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7656. src1_ncols*ne10*sizeof(float), stream));
  7657. }
  7658. }
  7659. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  7660. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7661. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7662. } else {
  7663. GGML_ASSERT(false);
  7664. }
  7665. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  7666. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7667. CUDA_CHECK(cudaGetLastError());
  7668. }
  7669. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7670. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7671. }
  7672. // do the computation
  7673. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7674. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7675. CUDA_CHECK(cudaGetLastError());
  7676. // copy dst to host or other device if necessary
  7677. if (!dst_on_device) {
  7678. void * dst_off_device;
  7679. cudaMemcpyKind kind;
  7680. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  7681. dst_off_device = dst->data;
  7682. kind = cudaMemcpyDeviceToHost;
  7683. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  7684. dst_off_device = dst_extra->data_device[g_main_device];
  7685. kind = cudaMemcpyDeviceToDevice;
  7686. } else {
  7687. GGML_ASSERT(false);
  7688. }
  7689. if (split) {
  7690. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7691. // dst is NOT transposed.
  7692. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7693. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7694. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7695. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7696. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7697. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7698. #if !defined(GGML_USE_HIPBLAS)
  7699. if (kind == cudaMemcpyDeviceToDevice) {
  7700. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7701. cudaMemcpy3DPeerParms p = {};
  7702. p.dstDevice = g_main_device;
  7703. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7704. p.srcDevice = id;
  7705. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7706. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7707. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7708. } else
  7709. #endif
  7710. {
  7711. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7712. dst_dd_i, row_diff*sizeof(float),
  7713. row_diff*sizeof(float), src1_ncols,
  7714. kind, stream));
  7715. }
  7716. } else {
  7717. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7718. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7719. dhf_dst_i += src1_col_0*ne0;
  7720. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7721. }
  7722. }
  7723. // add event for the main device to wait on until other device is done
  7724. if (split && (id != g_main_device || is != 0)) {
  7725. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7726. }
  7727. }
  7728. }
  7729. }
  7730. // main device waits for all other devices to be finished
  7731. if (split && g_device_count > 1) {
  7732. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7733. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7734. ggml_cuda_set_device(g_main_device);
  7735. for (int id = 0; id < g_device_count; ++id) {
  7736. if (dev[id].row_low == dev[id].row_high) {
  7737. continue;
  7738. }
  7739. for (int64_t is = 0; is < is_max; ++is) {
  7740. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7741. }
  7742. }
  7743. }
  7744. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  7745. ggml_cuda_set_device(g_main_device);
  7746. CUDA_CHECK(cudaDeviceSynchronize());
  7747. }
  7748. }
  7749. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7750. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  7751. }
  7752. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7753. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  7754. }
  7755. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7756. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  7757. }
  7758. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7759. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  7760. }
  7761. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7762. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  7763. }
  7764. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7765. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  7766. }
  7767. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7768. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  7769. }
  7770. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7771. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  7772. }
  7773. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7774. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  7775. }
  7776. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7777. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  7778. }
  7779. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7780. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  7781. }
  7782. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7783. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  7784. }
  7785. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7786. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  7787. }
  7788. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7789. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  7790. }
  7791. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7792. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  7793. }
  7794. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7795. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  7796. }
  7797. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7798. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  7799. }
  7800. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7801. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  7802. }
  7803. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7804. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  7805. }
  7806. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7807. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  7808. }
  7809. static void ggml_cuda_arange(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7810. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7811. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  7812. // dd = data device
  7813. float * src0_ddf = nullptr;
  7814. float * src1_ddf = nullptr;
  7815. float * dst_ddf = nullptr;
  7816. cuda_pool_alloc<float> dst_f;
  7817. ggml_cuda_set_device(g_main_device);
  7818. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7819. if (dst_on_device) {
  7820. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7821. } else {
  7822. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7823. }
  7824. // do the computation
  7825. ggml_cuda_op_arange(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7826. CUDA_CHECK(cudaGetLastError());
  7827. // copy dst to host if necessary
  7828. if (!dst_on_device) {
  7829. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7830. }
  7831. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  7832. CUDA_CHECK(cudaDeviceSynchronize());
  7833. }
  7834. }
  7835. static void ggml_cuda_timestep_embedding(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7836. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_timestep_embedding);
  7837. }
  7838. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7839. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  7840. }
  7841. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  7842. if (!g_cublas_loaded) return false;
  7843. const int64_t ne10 = src1->ne[0];
  7844. const int64_t ne0 = dst->ne[0];
  7845. const int64_t ne1 = dst->ne[1];
  7846. // TODO: find the optimal values for these
  7847. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  7848. src1->type == GGML_TYPE_F32 &&
  7849. dst->type == GGML_TYPE_F32 &&
  7850. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  7851. }
  7852. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7853. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  7854. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7855. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  7856. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  7857. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7858. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7859. const int64_t ne00 = src0->ne[0];
  7860. const int64_t ne01 = src0->ne[1];
  7861. const int64_t ne02 = src0->ne[2];
  7862. const int64_t ne12 = src1->ne[2];
  7863. ggml_cuda_set_device(g_main_device);
  7864. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7865. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7866. void * src0_ddq = src0_extra->data_device[g_main_device];
  7867. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7868. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7869. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7870. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7871. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  7872. }
  7873. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7874. GGML_ASSERT(!ggml_is_transposed(src0));
  7875. GGML_ASSERT(!ggml_is_transposed(src1));
  7876. GGML_ASSERT(!ggml_is_permuted(src0));
  7877. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7878. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7879. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7880. const int64_t ne00 = src0->ne[0];
  7881. const int64_t ne01 = src0->ne[1];
  7882. const int64_t ne02 = src0->ne[2];
  7883. const int64_t nb01 = src0->nb[1];
  7884. const int64_t nb02 = src0->nb[2];
  7885. const int64_t ne12 = src1->ne[2];
  7886. ggml_cuda_set_device(g_main_device);
  7887. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7888. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7889. void * src0_ddq = src0_extra->data_device[g_main_device];
  7890. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7891. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7892. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7893. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7894. const int64_t row_stride_x = nb01 / sizeof(half);
  7895. const int64_t channel_stride_x = nb02 / sizeof(half);
  7896. 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);
  7897. }
  7898. static __global__ void k_compute_batched_ptrs(
  7899. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  7900. const void ** ptrs_src, void ** ptrs_dst,
  7901. int64_t ne12, int64_t ne13,
  7902. int64_t ne23,
  7903. size_t nb02, size_t nb03,
  7904. size_t nb12, size_t nb13,
  7905. size_t nbd2, size_t nbd3,
  7906. int64_t r2, int64_t r3) {
  7907. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7908. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7909. if (i13 >= ne13 || i12 >= ne12) {
  7910. return;
  7911. }
  7912. int64_t i03 = i13 / r3;
  7913. int64_t i02 = i12 / r2;
  7914. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  7915. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  7916. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  7917. }
  7918. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7919. GGML_ASSERT(!ggml_is_transposed(src0));
  7920. GGML_ASSERT(!ggml_is_transposed(src1));
  7921. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7922. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7923. GGML_TENSOR_BINARY_OP_LOCALS
  7924. const int64_t ne_dst = ggml_nelements(dst);
  7925. ggml_cuda_set_device(g_main_device);
  7926. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7927. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7928. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7929. void * src0_ddq = src0_extra->data_device[g_main_device];
  7930. half * src0_f16 = (half *) src0_ddq;
  7931. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7932. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7933. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7934. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7935. // convert src1 to fp16
  7936. cuda_pool_alloc<half> src1_f16_alloc;
  7937. if (src1->type != GGML_TYPE_F16) {
  7938. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7939. const int64_t ne_src1 = ggml_nelements(src1);
  7940. src1_f16_alloc.alloc(ne_src1);
  7941. GGML_ASSERT(to_fp16_cuda != nullptr);
  7942. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  7943. }
  7944. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  7945. cuda_pool_alloc<half> dst_f16;
  7946. char * dst_t;
  7947. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  7948. cudaDataType_t cu_data_type = CUDA_R_16F;
  7949. // dst strides
  7950. size_t nbd2 = dst->nb[2];
  7951. size_t nbd3 = dst->nb[3];
  7952. const half alpha_f16 = 1.0f;
  7953. const half beta_f16 = 0.0f;
  7954. const float alpha_f32 = 1.0f;
  7955. const float beta_f32 = 0.0f;
  7956. const void * alpha = &alpha_f16;
  7957. const void * beta = &beta_f16;
  7958. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  7959. dst_t = (char *) dst_f16.alloc(ne_dst);
  7960. nbd2 /= sizeof(float) / sizeof(half);
  7961. nbd3 /= sizeof(float) / sizeof(half);
  7962. } else {
  7963. dst_t = (char *) dst_ddf;
  7964. cu_compute_type = CUBLAS_COMPUTE_32F;
  7965. cu_data_type = CUDA_R_32F;
  7966. alpha = &alpha_f32;
  7967. beta = &beta_f32;
  7968. }
  7969. GGML_ASSERT(ne12 % ne02 == 0);
  7970. GGML_ASSERT(ne13 % ne03 == 0);
  7971. // broadcast factors
  7972. const int64_t r2 = ne12/ne02;
  7973. const int64_t r3 = ne13/ne03;
  7974. #if 0
  7975. // use cublasGemmEx
  7976. {
  7977. for (int i13 = 0; i13 < ne13; ++i13) {
  7978. for (int i12 = 0; i12 < ne12; ++i12) {
  7979. int i03 = i13 / r3;
  7980. int i02 = i12 / r2;
  7981. CUBLAS_CHECK(
  7982. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7983. ne01, ne11, ne10,
  7984. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  7985. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  7986. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  7987. cu_compute_type,
  7988. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7989. }
  7990. }
  7991. }
  7992. #else
  7993. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  7994. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  7995. // use cublasGemmStridedBatchedEx
  7996. CUBLAS_CHECK(
  7997. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7998. ne01, ne11, ne10,
  7999. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8000. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8001. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8002. ne12*ne13,
  8003. cu_compute_type,
  8004. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8005. } else {
  8006. // use cublasGemmBatchedEx
  8007. const int ne23 = ne12*ne13;
  8008. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8009. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8010. dim3 block_dims(ne13, ne12);
  8011. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8012. src0_f16, src1_f16, dst_t,
  8013. ptrs_src.get(), ptrs_dst.get(),
  8014. ne12, ne13,
  8015. ne23,
  8016. nb02, nb03,
  8017. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8018. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8019. nbd2, nbd3,
  8020. r2, r3);
  8021. CUDA_CHECK(cudaGetLastError());
  8022. CUBLAS_CHECK(
  8023. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8024. ne01, ne11, ne10,
  8025. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8026. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8027. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8028. ne23,
  8029. cu_compute_type,
  8030. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8031. }
  8032. #endif
  8033. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8034. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8035. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8036. }
  8037. }
  8038. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8039. const bool all_on_device =
  8040. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  8041. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  8042. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  8043. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8044. int64_t min_compute_capability = INT_MAX;
  8045. bool any_pascal_with_slow_fp16 = false;
  8046. if (split) {
  8047. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8048. auto & tensor_split = buft_ctx->tensor_split;
  8049. for (int id = 0; id < g_device_count; ++id) {
  8050. // skip devices that are not going to do any work:
  8051. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8052. continue;
  8053. }
  8054. if (min_compute_capability > g_device_caps[id].cc) {
  8055. min_compute_capability = g_device_caps[id].cc;
  8056. }
  8057. if (g_device_caps[id].cc == 610) {
  8058. any_pascal_with_slow_fp16 = true;
  8059. }
  8060. }
  8061. } else {
  8062. min_compute_capability = g_device_caps[g_main_device].cc;
  8063. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8064. }
  8065. // check data types and tensor shapes for custom matrix multiplication kernels:
  8066. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8067. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8068. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8069. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8070. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8071. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8072. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8073. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8074. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8075. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8076. #ifdef CUDA_USE_TENSOR_CORES
  8077. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8078. #endif // CUDA_USE_TENSOR_CORES
  8079. #else
  8080. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8081. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8082. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8083. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8084. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8085. #ifdef CUDA_USE_TENSOR_CORES
  8086. // when tensor cores are available, use them for large batch size
  8087. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8088. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8089. #endif // CUDA_USE_TENSOR_CORES
  8090. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8091. // if mmvq is available it's a better choice than dmmv:
  8092. #ifndef GGML_CUDA_FORCE_DMMV
  8093. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8094. #endif // GGML_CUDA_FORCE_DMMV
  8095. // debug helpers
  8096. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8097. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8098. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8099. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8100. //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);
  8101. //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);
  8102. if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  8103. // KQ single-batch
  8104. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8105. } else if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  8106. // KQV single-batch
  8107. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8108. } else if (!split && all_on_device && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  8109. // KQ + KQV multi-batch
  8110. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8111. } else if (use_dequantize_mul_mat_vec) {
  8112. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8113. } else if (use_mul_mat_vec_q) {
  8114. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8115. } else if (use_mul_mat_q) {
  8116. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8117. } else {
  8118. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8119. }
  8120. }
  8121. #if 0
  8122. template<typename ... Srcs>
  8123. static __global__ void k_compute_batched_ptrs_id(
  8124. const void ** ptrs_src, void ** ptrs_dst,
  8125. int ne12, int ne13,
  8126. int ne23,
  8127. int nb02, int nb03,
  8128. int nb12, int nb13,
  8129. int nb2, int nb3,
  8130. int r2, int r3,
  8131. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8132. const half * src1_f16, half * dst_f16,
  8133. const int32_t * ids, const int id,
  8134. Srcs... src0s) {
  8135. int i = ids[id];
  8136. half * src0_f16;
  8137. const void * srcs_ar[] = { (const half *) src0s... };
  8138. if (src0_type == GGML_TYPE_F16) {
  8139. src0_f16 = (half *) srcs_ar[i];
  8140. } else {
  8141. src0_f16 = src0_as_f16;
  8142. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8143. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8144. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8145. }
  8146. }
  8147. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8148. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8149. if (i13 >= ne13 || i12 >= ne12) {
  8150. return;
  8151. }
  8152. int i03 = i13 / r3;
  8153. int i02 = i12 / r2;
  8154. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8155. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8156. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8157. }
  8158. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8159. const struct ggml_tensor * ids = dst->src[0];
  8160. const struct ggml_tensor * src1 = dst->src[1];
  8161. const struct ggml_tensor * src00 = dst->src[2];
  8162. const int id = dst->op_params[0];
  8163. GGML_ASSERT(!ggml_is_transposed(src00));
  8164. GGML_ASSERT(!ggml_is_transposed(src1));
  8165. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8166. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8167. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8168. const int64_t ne01 = src00->ne[1];
  8169. const int64_t ne02 = src00->ne[2];
  8170. const int64_t ne03 = src00->ne[3];
  8171. //const int64_t nb01 = src00->nb[1];
  8172. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8173. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8174. const int64_t ne10 = src1->ne[0];
  8175. const int64_t ne11 = src1->ne[1];
  8176. const int64_t ne12 = src1->ne[2];
  8177. const int64_t ne13 = src1->ne[3];
  8178. //const int64_t nb11 = src1->nb[1];
  8179. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8180. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8181. const int64_t ne1 = ggml_nelements(src1);
  8182. const int64_t ne = ggml_nelements(dst);
  8183. ggml_cuda_set_device(g_main_device);
  8184. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8185. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8186. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8187. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8188. //half * src0_as_f16 = (half *) src0_ddq;
  8189. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8190. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8191. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8192. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8193. // convert src1 to fp16
  8194. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8195. GGML_ASSERT(to_fp16_cuda != nullptr);
  8196. size_t src1_as = 0;
  8197. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8198. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8199. size_t dst_as = 0;
  8200. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8201. GGML_ASSERT(ne12 % ne02 == 0);
  8202. GGML_ASSERT(ne13 % ne03 == 0);
  8203. // broadcast factors
  8204. const int64_t r2 = ne12/ne02;
  8205. const int64_t r3 = ne13/ne03;
  8206. const half alpha_f16 = 1.0f;
  8207. const half beta_f16 = 0.0f;
  8208. // use cublasGemmBatchedEx
  8209. const int ne23 = ne12*ne13;
  8210. const void ** ptrs_src = nullptr;
  8211. void ** ptrs_dst = nullptr;
  8212. size_t ptrs_src_s = 0;
  8213. size_t ptrs_dst_s = 0;
  8214. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8215. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8216. int64_t src0_ne = ggml_nelements(src00);
  8217. half * src0_as_f16 = nullptr;
  8218. size_t src0_as = 0;
  8219. if (src00->type != GGML_TYPE_F16) {
  8220. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8221. }
  8222. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8223. dim3 block_dims(ne13, ne12);
  8224. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8225. ptrs_src, ptrs_dst,
  8226. ne12, ne13,
  8227. ne23,
  8228. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8229. nb12, nb13,
  8230. dst->nb[2], dst->nb[3],
  8231. r2, r3,
  8232. src00->type, src0_as_f16, src0_ne,
  8233. src1_as_f16, dst_f16,
  8234. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8235. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8236. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8237. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8238. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8239. );
  8240. CUDA_CHECK(cudaGetLastError());
  8241. CUBLAS_CHECK(
  8242. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8243. ne01, ne11, ne10,
  8244. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8245. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8246. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8247. ne23,
  8248. CUBLAS_COMPUTE_16F,
  8249. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8250. if (src0_as != 0) {
  8251. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8252. }
  8253. if (ptrs_src_s != 0) {
  8254. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8255. }
  8256. if (ptrs_dst_s != 0) {
  8257. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8258. }
  8259. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8260. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8261. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8262. ggml_cuda_pool_free(dst_f16, dst_as);
  8263. }
  8264. #endif
  8265. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8266. #if 0
  8267. ggml_cuda_mul_mat_id_cublas(dst);
  8268. // TODO: mmq/mmv support
  8269. #endif
  8270. const size_t nb11 = src1->nb[1];
  8271. const size_t nb1 = dst->nb[1];
  8272. const struct ggml_tensor * ids = src0;
  8273. const int32_t id = ((int32_t *) dst->op_params)[0];
  8274. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  8275. std::vector<char> ids_host(ggml_nbytes(ids));
  8276. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  8277. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  8278. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  8279. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  8280. CUDA_CHECK(cudaStreamSynchronize(stream));
  8281. } else {
  8282. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  8283. }
  8284. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  8285. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  8286. ggml_tensor_extra_gpu src1_row_extra;
  8287. ggml_tensor_extra_gpu dst_row_extra;
  8288. ggml_tensor src1_row = *src1;
  8289. ggml_tensor dst_row = *dst;
  8290. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  8291. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  8292. src1_row.extra = &src1_row_extra;
  8293. dst_row.extra = &dst_row_extra;
  8294. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  8295. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  8296. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  8297. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  8298. if (src1->ne[1] == 1) {
  8299. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  8300. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  8301. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8302. //int32_t row_id;
  8303. //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]));
  8304. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  8305. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8306. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8307. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8308. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  8309. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  8310. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  8311. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  8312. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8313. }
  8314. } else {
  8315. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  8316. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  8317. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  8318. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  8319. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_TYPE_CPU ?
  8320. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  8321. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_TYPE_CPU ?
  8322. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  8323. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  8324. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8325. int64_t num_src1_rows = 0;
  8326. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8327. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8328. if (row_id_i != row_id) {
  8329. continue;
  8330. }
  8331. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8332. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  8333. nb11, src1_kind, stream));
  8334. num_src1_rows++;
  8335. }
  8336. if (num_src1_rows == 0) {
  8337. continue;
  8338. }
  8339. src1_row.ne[1] = num_src1_rows;
  8340. dst_row.ne[1] = num_src1_rows;
  8341. src1_row.nb[1] = nb11;
  8342. src1_row.nb[2] = num_src1_rows*nb11;
  8343. src1_row.nb[3] = num_src1_rows*nb11;
  8344. dst_row.nb[1] = nb1;
  8345. dst_row.nb[2] = num_src1_rows*nb1;
  8346. dst_row.nb[3] = num_src1_rows*nb1;
  8347. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8348. num_src1_rows = 0;
  8349. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8350. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8351. if (row_id_i != row_id) {
  8352. continue;
  8353. }
  8354. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8355. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  8356. nb1, dst_kind, stream));
  8357. num_src1_rows++;
  8358. }
  8359. }
  8360. }
  8361. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8362. CUDA_CHECK(cudaStreamSynchronize(stream));
  8363. }
  8364. }
  8365. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8366. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  8367. }
  8368. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8369. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  8370. }
  8371. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8372. const int64_t ne = ggml_nelements(src0);
  8373. GGML_ASSERT(ne == ggml_nelements(src1));
  8374. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  8375. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  8376. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  8377. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  8378. const int64_t ne00 = src0->ne[0];
  8379. const int64_t ne01 = src0->ne[1];
  8380. const int64_t ne02 = src0->ne[2];
  8381. //GGML_ASSERT(src0->ne[3] == 1);
  8382. const int64_t nb00 = src0->nb[0];
  8383. const int64_t nb01 = src0->nb[1];
  8384. const int64_t nb02 = src0->nb[2];
  8385. const int64_t nb03 = src0->nb[3];
  8386. const int64_t ne10 = src1->ne[0];
  8387. const int64_t ne11 = src1->ne[1];
  8388. const int64_t ne12 = src1->ne[2];
  8389. //GGML_ASSERT(src1->ne[3] == 1);
  8390. const int64_t nb10 = src1->nb[0];
  8391. const int64_t nb11 = src1->nb[1];
  8392. const int64_t nb12 = src1->nb[2];
  8393. const int64_t nb13 = src1->nb[3];
  8394. ggml_cuda_set_device(g_main_device);
  8395. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8396. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8397. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8398. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8399. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  8400. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  8401. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8402. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  8403. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8404. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  8405. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8406. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  8407. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8408. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  8409. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8410. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  8411. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8412. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  8413. ggml_cpy_f16_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8414. } else {
  8415. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  8416. ggml_type_name(src0->type), ggml_type_name(src1->type));
  8417. GGML_ASSERT(false);
  8418. }
  8419. (void) dst;
  8420. }
  8421. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8422. // TODO: why do we pass dst as src1 here?
  8423. ggml_cuda_cpy(src0, dst, nullptr);
  8424. (void) src1;
  8425. }
  8426. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8427. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  8428. }
  8429. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8430. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  8431. }
  8432. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8433. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8434. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  8435. }
  8436. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8437. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  8438. }
  8439. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8440. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  8441. }
  8442. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8443. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  8444. }
  8445. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8446. GGML_ASSERT(ggml_is_contiguous(src0));
  8447. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  8448. }
  8449. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8450. GGML_ASSERT(ggml_is_contiguous(src0));
  8451. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  8452. }
  8453. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8454. (void) src0;
  8455. (void) src1;
  8456. (void) dst;
  8457. }
  8458. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  8459. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  8460. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  8461. }
  8462. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  8463. if (main_device >= g_device_count) {
  8464. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8465. main_device, g_device_count, g_main_device);
  8466. return;
  8467. }
  8468. if (g_main_device != main_device && g_device_count > 1) {
  8469. g_main_device = main_device;
  8470. //cudaDeviceProp prop;
  8471. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8472. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8473. }
  8474. }
  8475. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8476. if (!g_cublas_loaded) return false;
  8477. ggml_cuda_func_t func;
  8478. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  8479. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  8480. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  8481. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8482. return false;
  8483. }
  8484. if (tensor->op == GGML_OP_MUL_MAT) {
  8485. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8486. #ifndef NDEBUG
  8487. 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]);
  8488. #endif
  8489. return false;
  8490. }
  8491. }
  8492. switch (tensor->op) {
  8493. case GGML_OP_REPEAT:
  8494. func = ggml_cuda_repeat;
  8495. break;
  8496. case GGML_OP_GET_ROWS:
  8497. func = ggml_cuda_get_rows;
  8498. break;
  8499. case GGML_OP_DUP:
  8500. func = ggml_cuda_dup;
  8501. break;
  8502. case GGML_OP_ADD:
  8503. func = ggml_cuda_add;
  8504. break;
  8505. case GGML_OP_ACC:
  8506. func = ggml_cuda_acc;
  8507. break;
  8508. case GGML_OP_MUL:
  8509. func = ggml_cuda_mul;
  8510. break;
  8511. case GGML_OP_DIV:
  8512. func = ggml_cuda_div;
  8513. break;
  8514. case GGML_OP_UNARY:
  8515. switch (ggml_get_unary_op(tensor)) {
  8516. case GGML_UNARY_OP_GELU:
  8517. func = ggml_cuda_gelu;
  8518. break;
  8519. case GGML_UNARY_OP_SILU:
  8520. func = ggml_cuda_silu;
  8521. break;
  8522. case GGML_UNARY_OP_GELU_QUICK:
  8523. func = ggml_cuda_gelu_quick;
  8524. break;
  8525. case GGML_UNARY_OP_TANH:
  8526. func = ggml_cuda_tanh;
  8527. break;
  8528. case GGML_UNARY_OP_RELU:
  8529. func = ggml_cuda_relu;
  8530. break;
  8531. case GGML_UNARY_OP_HARDSIGMOID:
  8532. func = ggml_cuda_hardsigmoid;
  8533. break;
  8534. case GGML_UNARY_OP_HARDSWISH:
  8535. func = ggml_cuda_hardswish;
  8536. break;
  8537. default:
  8538. return false;
  8539. }
  8540. break;
  8541. case GGML_OP_NORM:
  8542. func = ggml_cuda_norm;
  8543. break;
  8544. case GGML_OP_GROUP_NORM:
  8545. func = ggml_cuda_group_norm;
  8546. break;
  8547. case GGML_OP_CONCAT:
  8548. func = ggml_cuda_concat;
  8549. break;
  8550. case GGML_OP_UPSCALE:
  8551. func = ggml_cuda_upscale;
  8552. break;
  8553. case GGML_OP_PAD:
  8554. func = ggml_cuda_pad;
  8555. break;
  8556. case GGML_OP_ARANGE:
  8557. func = ggml_cuda_arange;
  8558. break;
  8559. case GGML_OP_TIMESTEP_EMBEDDING:
  8560. func = ggml_cuda_timestep_embedding;
  8561. break;
  8562. case GGML_OP_LEAKY_RELU:
  8563. func = ggml_cuda_leaky_relu;
  8564. break;
  8565. case GGML_OP_RMS_NORM:
  8566. func = ggml_cuda_rms_norm;
  8567. break;
  8568. case GGML_OP_MUL_MAT:
  8569. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8570. return false;
  8571. }
  8572. func = ggml_cuda_mul_mat;
  8573. break;
  8574. case GGML_OP_MUL_MAT_ID:
  8575. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8576. return false;
  8577. }
  8578. func = ggml_cuda_mul_mat_id;
  8579. break;
  8580. case GGML_OP_SCALE:
  8581. func = ggml_cuda_scale;
  8582. break;
  8583. case GGML_OP_SQR:
  8584. func = ggml_cuda_sqr;
  8585. break;
  8586. case GGML_OP_CLAMP:
  8587. func = ggml_cuda_clamp;
  8588. break;
  8589. case GGML_OP_CPY:
  8590. func = ggml_cuda_cpy;
  8591. break;
  8592. case GGML_OP_CONT:
  8593. func = ggml_cuda_dup;
  8594. break;
  8595. case GGML_OP_NONE:
  8596. case GGML_OP_RESHAPE:
  8597. case GGML_OP_VIEW:
  8598. case GGML_OP_PERMUTE:
  8599. case GGML_OP_TRANSPOSE:
  8600. func = ggml_cuda_nop;
  8601. break;
  8602. case GGML_OP_DIAG_MASK_INF:
  8603. func = ggml_cuda_diag_mask_inf;
  8604. break;
  8605. case GGML_OP_SOFT_MAX:
  8606. func = ggml_cuda_soft_max;
  8607. break;
  8608. case GGML_OP_ROPE:
  8609. func = ggml_cuda_rope;
  8610. break;
  8611. case GGML_OP_ALIBI:
  8612. func = ggml_cuda_alibi;
  8613. break;
  8614. case GGML_OP_IM2COL:
  8615. func = ggml_cuda_im2col;
  8616. break;
  8617. case GGML_OP_POOL_2D:
  8618. func = ggml_cuda_pool2d;
  8619. break;
  8620. case GGML_OP_SUM_ROWS:
  8621. func = ggml_cuda_sum_rows;
  8622. break;
  8623. case GGML_OP_ARGSORT:
  8624. func = ggml_cuda_argsort;
  8625. break;
  8626. default:
  8627. return false;
  8628. }
  8629. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  8630. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8631. }
  8632. if (params->ith != 0) {
  8633. return true;
  8634. }
  8635. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  8636. return true;
  8637. }
  8638. func(tensor->src[0], tensor->src[1], tensor);
  8639. return true;
  8640. }
  8641. GGML_CALL int ggml_cuda_get_device_count() {
  8642. int device_count;
  8643. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8644. return 0;
  8645. }
  8646. return device_count;
  8647. }
  8648. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8649. cudaDeviceProp prop;
  8650. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8651. snprintf(description, description_size, "%s", prop.name);
  8652. }
  8653. ////////////////////////////////////////////////////////////////////////////////
  8654. // backend interface
  8655. #define UNUSED GGML_UNUSED
  8656. struct ggml_backend_cuda_context {
  8657. int device;
  8658. std::string name;
  8659. };
  8660. // cuda buffer
  8661. struct ggml_backend_cuda_buffer_context {
  8662. int device;
  8663. void * dev_ptr = nullptr;
  8664. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8665. size_t temp_tensor_extra_index = 0;
  8666. std::string name;
  8667. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  8668. device(device), dev_ptr(dev_ptr),
  8669. name(GGML_CUDA_NAME + std::to_string(device)) {
  8670. }
  8671. ~ggml_backend_cuda_buffer_context() {
  8672. delete[] temp_tensor_extras;
  8673. }
  8674. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8675. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  8676. if (temp_tensor_extras == nullptr) {
  8677. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8678. }
  8679. size_t alloc_index = temp_tensor_extra_index;
  8680. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8681. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8682. memset(extra, 0, sizeof(*extra));
  8683. return extra;
  8684. }
  8685. };
  8686. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  8687. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8688. return ctx->name.c_str();
  8689. }
  8690. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  8691. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  8692. }
  8693. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8694. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8695. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8696. delete ctx;
  8697. }
  8698. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8699. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8700. return ctx->dev_ptr;
  8701. }
  8702. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8703. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8704. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8705. assert(tensor->view_src->buffer->buft == buffer->buft);
  8706. tensor->backend = tensor->view_src->backend;
  8707. tensor->extra = tensor->view_src->extra;
  8708. return;
  8709. }
  8710. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8711. extra->data_device[ctx->device] = tensor->data;
  8712. tensor->backend = GGML_BACKEND_TYPE_GPU;
  8713. tensor->extra = extra;
  8714. if (ggml_is_quantized(tensor->type)) {
  8715. // initialize padding to 0 to avoid possible NaN values
  8716. size_t original_size = ggml_nbytes(tensor);
  8717. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8718. if (padded_size > original_size && tensor->view_src == nullptr) {
  8719. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  8720. }
  8721. }
  8722. }
  8723. GGML_CALL static void ggml_backend_cuda_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8724. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  8725. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8726. ggml_cuda_set_device(ctx->device);
  8727. CUDA_CHECK(cudaDeviceSynchronize());
  8728. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8729. CUDA_CHECK(cudaDeviceSynchronize());
  8730. }
  8731. GGML_CALL static void ggml_backend_cuda_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8732. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  8733. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8734. ggml_cuda_set_device(ctx->device);
  8735. CUDA_CHECK(cudaDeviceSynchronize());
  8736. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8737. CUDA_CHECK(cudaDeviceSynchronize());
  8738. }
  8739. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8740. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  8741. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  8742. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8743. ggml_cuda_set_device(src_ctx->device);
  8744. CUDA_CHECK(cudaDeviceSynchronize());
  8745. ggml_cuda_set_device(dst_ctx->device);
  8746. CUDA_CHECK(cudaDeviceSynchronize());
  8747. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  8748. CUDA_CHECK(cudaDeviceSynchronize());
  8749. return true;
  8750. }
  8751. return false;
  8752. }
  8753. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8754. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8755. ggml_cuda_set_device(ctx->device);
  8756. CUDA_CHECK(cudaDeviceSynchronize());
  8757. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8758. CUDA_CHECK(cudaDeviceSynchronize());
  8759. }
  8760. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  8761. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  8762. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8763. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8764. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8765. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8766. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8767. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  8768. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8769. /* .reset = */ NULL,
  8770. };
  8771. // cuda buffer type
  8772. struct ggml_backend_cuda_buffer_type_context {
  8773. int device;
  8774. std::string name;
  8775. };
  8776. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8777. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8778. return ctx->name.c_str();
  8779. }
  8780. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8781. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8782. ggml_cuda_set_device(buft_ctx->device);
  8783. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  8784. void * dev_ptr;
  8785. cudaError_t err = cudaMalloc(&dev_ptr, size);
  8786. if (err != cudaSuccess) {
  8787. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  8788. return nullptr;
  8789. }
  8790. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  8791. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  8792. }
  8793. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8794. return 128;
  8795. UNUSED(buft);
  8796. }
  8797. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8798. size_t size = ggml_nbytes(tensor);
  8799. int64_t ne0 = tensor->ne[0];
  8800. if (ggml_is_quantized(tensor->type)) {
  8801. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8802. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8803. }
  8804. }
  8805. return size;
  8806. UNUSED(buft);
  8807. }
  8808. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  8809. if (!ggml_backend_is_cuda(backend)) {
  8810. return false;
  8811. }
  8812. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8813. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  8814. return buft_ctx->device == cuda_ctx->device;
  8815. }
  8816. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  8817. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  8818. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  8819. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  8820. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  8821. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  8822. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  8823. /* .is_host = */ NULL,
  8824. };
  8825. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  8826. // FIXME: this is not thread safe
  8827. if (device >= ggml_backend_cuda_get_device_count()) {
  8828. return nullptr;
  8829. }
  8830. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  8831. static bool ggml_backend_cuda_buffer_type_initialized = false;
  8832. if (!ggml_backend_cuda_buffer_type_initialized) {
  8833. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  8834. ggml_backend_cuda_buffer_types[i] = {
  8835. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  8836. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  8837. };
  8838. }
  8839. ggml_backend_cuda_buffer_type_initialized = true;
  8840. }
  8841. return &ggml_backend_cuda_buffer_types[device];
  8842. }
  8843. // cuda split buffer
  8844. struct ggml_backend_cuda_split_buffer_context {
  8845. ~ggml_backend_cuda_split_buffer_context() {
  8846. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  8847. for (int id = 0; id < g_device_count; ++id) {
  8848. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  8849. if (extra->events[id][is] != nullptr) {
  8850. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  8851. }
  8852. }
  8853. if (extra->data_device[id] != nullptr) {
  8854. CUDA_CHECK(cudaFree(extra->data_device[id]));
  8855. }
  8856. }
  8857. delete extra;
  8858. }
  8859. }
  8860. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  8861. };
  8862. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  8863. return GGML_CUDA_NAME "_Split";
  8864. UNUSED(buffer);
  8865. }
  8866. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  8867. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  8868. UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  8869. }
  8870. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8871. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  8872. delete ctx;
  8873. }
  8874. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  8875. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  8876. return (void *)0x1000;
  8877. UNUSED(buffer);
  8878. }
  8879. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8880. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  8881. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  8882. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  8883. const int64_t ne0 = tensor->ne[0];
  8884. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  8885. ctx->tensor_extras.push_back(extra);
  8886. for (int id = 0; id < g_device_count; ++id) {
  8887. int64_t row_low, row_high;
  8888. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  8889. int64_t nrows_split = row_high - row_low;
  8890. if (nrows_split == 0) {
  8891. continue;
  8892. }
  8893. size_t size = ggml_nbytes_split(tensor, nrows_split);
  8894. const size_t original_size = size;
  8895. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  8896. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8897. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8898. }
  8899. // FIXME: do not crash if cudaMalloc fails
  8900. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  8901. ggml_cuda_set_device(id);
  8902. char * buf;
  8903. CUDA_CHECK(cudaMalloc(&buf, size));
  8904. // set padding to 0 to avoid possible NaN values
  8905. if (size > original_size) {
  8906. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  8907. }
  8908. extra->data_device[id] = buf;
  8909. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  8910. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  8911. }
  8912. }
  8913. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  8914. tensor->extra = extra;
  8915. }
  8916. GGML_CALL static void ggml_backend_cuda_split_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8917. // split tensors must always be set in their entirety at once
  8918. GGML_ASSERT(offset == 0);
  8919. GGML_ASSERT(size == ggml_nbytes(tensor));
  8920. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  8921. const int64_t ne0 = tensor->ne[0];
  8922. const size_t nb1 = tensor->nb[1];
  8923. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  8924. for (int id = 0; id < g_device_count; ++id) {
  8925. int64_t row_low, row_high;
  8926. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  8927. int64_t nrows_split = row_high - row_low;
  8928. if (nrows_split == 0) {
  8929. continue;
  8930. }
  8931. const size_t offset_split = row_low*nb1;
  8932. size_t size = ggml_nbytes_split(tensor, nrows_split);
  8933. const size_t original_size = size;
  8934. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  8935. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8936. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8937. }
  8938. const char * buf_host = (const char *)data + offset_split;
  8939. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  8940. }
  8941. }
  8942. GGML_CALL static void ggml_backend_cuda_split_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8943. // split tensors must always be set in their entirety at once
  8944. GGML_ASSERT(offset == 0);
  8945. GGML_ASSERT(size == ggml_nbytes(tensor));
  8946. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  8947. const int64_t ne0 = tensor->ne[0];
  8948. const size_t nb1 = tensor->nb[1];
  8949. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  8950. for (int id = 0; id < g_device_count; ++id) {
  8951. int64_t row_low, row_high;
  8952. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  8953. int64_t nrows_split = row_high - row_low;
  8954. if (nrows_split == 0) {
  8955. continue;
  8956. }
  8957. const size_t offset_split = row_low*nb1;
  8958. size_t size = ggml_nbytes_split(tensor, nrows_split);
  8959. const size_t original_size = size;
  8960. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  8961. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8962. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8963. }
  8964. char * buf_host = (char *)data + offset_split;
  8965. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  8966. }
  8967. }
  8968. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8969. UNUSED(buffer);
  8970. UNUSED(value);
  8971. }
  8972. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  8973. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  8974. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  8975. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  8976. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  8977. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  8978. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  8979. /* .cpy_tensor = */ NULL,
  8980. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  8981. /* .reset = */ NULL,
  8982. };
  8983. // cuda split buffer type
  8984. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8985. return GGML_CUDA_NAME "_Split";
  8986. UNUSED(buft);
  8987. }
  8988. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8989. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  8990. // instead, we allocate them for each tensor separately in init_tensor
  8991. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  8992. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  8993. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  8994. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  8995. }
  8996. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8997. return 128;
  8998. UNUSED(buft);
  8999. }
  9000. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9001. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9002. size_t total_size = 0;
  9003. const int64_t ne0 = tensor->ne[0];
  9004. for (int id = 0; id < g_device_count; ++id) {
  9005. int64_t row_low, row_high;
  9006. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9007. int64_t nrows_split = row_high - row_low;
  9008. if (nrows_split == 0) {
  9009. continue;
  9010. }
  9011. total_size += ggml_nbytes_split(tensor, nrows_split);
  9012. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9013. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9014. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9015. }
  9016. }
  9017. return total_size;
  9018. }
  9019. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9020. return ggml_backend_is_cuda(backend);
  9021. UNUSED(buft);
  9022. }
  9023. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9024. return false;
  9025. UNUSED(buft);
  9026. }
  9027. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9028. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9029. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9030. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9031. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9032. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9033. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9034. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9035. };
  9036. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9037. // FIXME: this is not thread safe
  9038. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9039. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9040. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9041. if (all_zero) {
  9042. tensor_split_arr = g_default_tensor_split;
  9043. } else {
  9044. float split_sum = 0.0f;
  9045. for (int i = 0; i < g_device_count; ++i) {
  9046. tensor_split_arr[i] = split_sum;
  9047. split_sum += tensor_split[i];
  9048. }
  9049. for (int i = 0; i < g_device_count; ++i) {
  9050. tensor_split_arr[i] /= split_sum;
  9051. }
  9052. }
  9053. auto it = buft_map.find(tensor_split_arr);
  9054. if (it != buft_map.end()) {
  9055. return &it->second;
  9056. }
  9057. struct ggml_backend_buffer_type buft {
  9058. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9059. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9060. };
  9061. auto result = buft_map.emplace(tensor_split_arr, buft);
  9062. return &result.first->second;
  9063. }
  9064. // host buffer type
  9065. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9066. return GGML_CUDA_NAME "_Host";
  9067. UNUSED(buft);
  9068. }
  9069. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9070. return GGML_CUDA_NAME "_Host";
  9071. UNUSED(buffer);
  9072. }
  9073. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9074. ggml_cuda_host_free(buffer->context);
  9075. }
  9076. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9077. void * ptr = ggml_cuda_host_malloc(size);
  9078. if (ptr == nullptr) {
  9079. // fallback to cpu buffer
  9080. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9081. }
  9082. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9083. buffer->buft = buft;
  9084. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9085. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9086. return buffer;
  9087. }
  9088. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9089. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9090. /* .iface = */ {
  9091. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9092. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9093. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9094. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9095. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9096. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9097. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9098. },
  9099. /* .context = */ nullptr,
  9100. };
  9101. return &ggml_backend_cuda_buffer_type_host;
  9102. }
  9103. // backend
  9104. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9105. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9106. return cuda_ctx->name.c_str();
  9107. }
  9108. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9109. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9110. delete cuda_ctx;
  9111. delete backend;
  9112. }
  9113. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9114. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9115. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9116. }
  9117. GGML_CALL static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9118. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9119. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9120. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9121. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9122. }
  9123. GGML_CALL static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9124. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9125. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9126. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9127. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9128. }
  9129. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9130. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9131. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9132. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9133. return true;
  9134. }
  9135. return false;
  9136. }
  9137. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9138. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9139. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9140. UNUSED(backend);
  9141. }
  9142. GGML_CALL static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9143. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9144. ggml_cuda_set_main_device(cuda_ctx->device);
  9145. ggml_compute_params params = {};
  9146. params.type = GGML_TASK_TYPE_COMPUTE;
  9147. params.ith = 0;
  9148. for (int i = 0; i < cgraph->n_nodes; i++) {
  9149. ggml_tensor * node = cgraph->nodes[i];
  9150. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  9151. continue;
  9152. }
  9153. #ifndef NDEBUG
  9154. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9155. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9156. assert(node->extra != nullptr);
  9157. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9158. if (node->src[j] != nullptr) {
  9159. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9160. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9161. assert(node->src[j]->extra != nullptr);
  9162. }
  9163. }
  9164. #endif
  9165. bool ok = ggml_cuda_compute_forward(&params, node);
  9166. if (!ok) {
  9167. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9168. }
  9169. GGML_ASSERT(ok);
  9170. }
  9171. return GGML_STATUS_SUCCESS;
  9172. }
  9173. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9174. switch (op->op) {
  9175. case GGML_OP_UNARY:
  9176. switch (ggml_get_unary_op(op)) {
  9177. case GGML_UNARY_OP_GELU:
  9178. case GGML_UNARY_OP_SILU:
  9179. case GGML_UNARY_OP_RELU:
  9180. case GGML_UNARY_OP_HARDSIGMOID:
  9181. case GGML_UNARY_OP_HARDSWISH:
  9182. case GGML_UNARY_OP_GELU_QUICK:
  9183. case GGML_UNARY_OP_TANH:
  9184. return true;
  9185. default:
  9186. return false;
  9187. }
  9188. break;
  9189. case GGML_OP_MUL_MAT:
  9190. case GGML_OP_MUL_MAT_ID:
  9191. {
  9192. struct ggml_tensor * a;
  9193. struct ggml_tensor * b;
  9194. if (op->op == GGML_OP_MUL_MAT) {
  9195. a = op->src[0];
  9196. b = op->src[1];
  9197. } else {
  9198. a = op->src[2];
  9199. b = op->src[1];
  9200. }
  9201. if (a->ne[3] != b->ne[3]) {
  9202. return false;
  9203. }
  9204. ggml_type a_type = a->type;
  9205. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  9206. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ3_S ||
  9207. a_type == GGML_TYPE_IQ2_S || a_type == GGML_TYPE_IQ4_XS) {
  9208. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9209. return false;
  9210. }
  9211. }
  9212. return true;
  9213. } break;
  9214. case GGML_OP_GET_ROWS:
  9215. {
  9216. switch (op->src[0]->type) {
  9217. case GGML_TYPE_F16:
  9218. case GGML_TYPE_F32:
  9219. case GGML_TYPE_Q4_0:
  9220. case GGML_TYPE_Q4_1:
  9221. case GGML_TYPE_Q5_0:
  9222. case GGML_TYPE_Q5_1:
  9223. case GGML_TYPE_Q8_0:
  9224. return true;
  9225. default:
  9226. return false;
  9227. }
  9228. } break;
  9229. case GGML_OP_CPY:
  9230. {
  9231. ggml_type src0_type = op->src[0]->type;
  9232. ggml_type src1_type = op->src[1]->type;
  9233. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9234. return true;
  9235. }
  9236. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9237. return true;
  9238. }
  9239. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9240. return true;
  9241. }
  9242. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9243. return true;
  9244. }
  9245. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9246. return true;
  9247. }
  9248. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9249. return true;
  9250. }
  9251. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9252. return true;
  9253. }
  9254. return false;
  9255. } break;
  9256. case GGML_OP_DUP:
  9257. case GGML_OP_REPEAT:
  9258. case GGML_OP_CONCAT:
  9259. {
  9260. ggml_type src0_type = op->src[0]->type;
  9261. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9262. } break;
  9263. case GGML_OP_NONE:
  9264. case GGML_OP_RESHAPE:
  9265. case GGML_OP_VIEW:
  9266. case GGML_OP_PERMUTE:
  9267. case GGML_OP_TRANSPOSE:
  9268. case GGML_OP_NORM:
  9269. case GGML_OP_ADD:
  9270. case GGML_OP_MUL:
  9271. case GGML_OP_DIV:
  9272. case GGML_OP_RMS_NORM:
  9273. case GGML_OP_SCALE:
  9274. case GGML_OP_SQR:
  9275. case GGML_OP_CLAMP:
  9276. case GGML_OP_CONT:
  9277. case GGML_OP_DIAG_MASK_INF:
  9278. case GGML_OP_SOFT_MAX:
  9279. case GGML_OP_ROPE:
  9280. case GGML_OP_ALIBI:
  9281. case GGML_OP_IM2COL:
  9282. case GGML_OP_POOL_2D:
  9283. case GGML_OP_SUM_ROWS:
  9284. case GGML_OP_ARGSORT:
  9285. case GGML_OP_ACC:
  9286. case GGML_OP_GROUP_NORM:
  9287. case GGML_OP_UPSCALE:
  9288. case GGML_OP_PAD:
  9289. case GGML_OP_ARANGE:
  9290. case GGML_OP_TIMESTEP_EMBEDDING:
  9291. case GGML_OP_LEAKY_RELU:
  9292. return true;
  9293. default:
  9294. return false;
  9295. }
  9296. UNUSED(backend);
  9297. }
  9298. static ggml_backend_i ggml_backend_cuda_interface = {
  9299. /* .get_name = */ ggml_backend_cuda_name,
  9300. /* .free = */ ggml_backend_cuda_free,
  9301. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  9302. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  9303. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  9304. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  9305. /* .synchronize = */ ggml_backend_cuda_synchronize,
  9306. /* .graph_plan_create = */ NULL,
  9307. /* .graph_plan_free = */ NULL,
  9308. /* .graph_plan_compute = */ NULL,
  9309. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  9310. /* .supports_op = */ ggml_backend_cuda_supports_op,
  9311. };
  9312. static ggml_guid_t ggml_backend_cuda_guid() {
  9313. static ggml_guid guid = { 0x2c, 0xdd, 0xe8, 0x1c, 0x65, 0xb3, 0x65, 0x73, 0x6a, 0x12, 0x88, 0x61, 0x1c, 0xc9, 0xdc, 0x25 };
  9314. return &guid;
  9315. }
  9316. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  9317. ggml_init_cublas(); // TODO: remove from ggml.c
  9318. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  9319. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  9320. return nullptr;
  9321. }
  9322. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  9323. ggml_cuda_set_main_device(device);
  9324. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  9325. /* .device = */ device,
  9326. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  9327. };
  9328. ggml_backend_t cuda_backend = new ggml_backend {
  9329. /* .guid = */ ggml_backend_cuda_guid(),
  9330. /* .interface = */ ggml_backend_cuda_interface,
  9331. /* .context = */ ctx
  9332. };
  9333. return cuda_backend;
  9334. }
  9335. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  9336. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cuda_guid());
  9337. }
  9338. GGML_CALL int ggml_backend_cuda_get_device_count() {
  9339. return ggml_cuda_get_device_count();
  9340. }
  9341. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  9342. ggml_cuda_get_device_description(device, description, description_size);
  9343. }
  9344. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  9345. ggml_cuda_set_device(device);
  9346. CUDA_CHECK(cudaMemGetInfo(free, total));
  9347. }
  9348. // backend registry
  9349. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  9350. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  9351. return cuda_backend;
  9352. UNUSED(params);
  9353. }
  9354. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  9355. GGML_CALL int ggml_backend_cuda_reg_devices() {
  9356. int device_count = ggml_cuda_get_device_count();
  9357. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  9358. for (int i = 0; i < device_count; i++) {
  9359. char name[128];
  9360. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  9361. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  9362. }
  9363. return device_count;
  9364. }