ggml-cuda.cu 318 KB

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