ggml-cuda.cu 434 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751
  1. #include "ggml-cuda.h"
  2. #include "ggml.h"
  3. #include "ggml-backend-impl.h"
  4. #define GGML_COMMON_IMPL_CUDA
  5. #include "ggml-common.h"
  6. #include <algorithm>
  7. #include <assert.h>
  8. #include <atomic>
  9. #include <cinttypes>
  10. #include <cstddef>
  11. #include <cstdint>
  12. #include <float.h>
  13. #include <limits>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string>
  17. #include <vector>
  18. #include <map>
  19. #include <array>
  20. // stringize macro for converting __CUDA_ARCH_LIST__ (list of integers) to string
  21. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  22. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  23. #if defined(GGML_USE_HIPBLAS)
  24. #include <hip/hip_runtime.h>
  25. #include <hipblas/hipblas.h>
  26. #include <hip/hip_fp16.h>
  27. #ifdef __HIP_PLATFORM_AMD__
  28. // for rocblas_initialize()
  29. #include "rocblas/rocblas.h"
  30. #endif // __HIP_PLATFORM_AMD__
  31. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  32. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  33. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  34. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  35. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  36. #define CUBLAS_OP_N HIPBLAS_OP_N
  37. #define CUBLAS_OP_T HIPBLAS_OP_T
  38. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  39. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  40. #define CUDA_R_16F HIPBLAS_R_16F
  41. #define CUDA_R_32F HIPBLAS_R_32F
  42. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  43. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  44. #define cublasCreate hipblasCreate
  45. #define cublasGemmEx hipblasGemmEx
  46. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  47. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  48. #define cublasHandle_t hipblasHandle_t
  49. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  50. #define cublasSetStream hipblasSetStream
  51. #define cublasSgemm hipblasSgemm
  52. #define cublasStatus_t hipblasStatus_t
  53. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  54. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  55. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  56. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  57. #define cudaDeviceProp hipDeviceProp_t
  58. #define cudaDeviceSynchronize hipDeviceSynchronize
  59. #define cudaError_t hipError_t
  60. #define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
  61. #define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
  62. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  63. #define cudaEventDisableTiming hipEventDisableTiming
  64. #define cudaEventRecord hipEventRecord
  65. #define cudaEvent_t hipEvent_t
  66. #define cudaEventDestroy hipEventDestroy
  67. #define cudaFree hipFree
  68. #define cudaFreeHost hipHostFree
  69. #define cudaGetDevice hipGetDevice
  70. #define cudaGetDeviceCount hipGetDeviceCount
  71. #define cudaGetDeviceProperties hipGetDeviceProperties
  72. #define cudaGetErrorString hipGetErrorString
  73. #define cudaGetLastError hipGetLastError
  74. #ifdef GGML_HIP_UMA
  75. #define cudaMalloc hipMallocManaged
  76. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  77. #else
  78. #define cudaMalloc hipMalloc
  79. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  80. #endif
  81. #define cudaMemcpy hipMemcpy
  82. #define cudaMemcpyAsync hipMemcpyAsync
  83. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  84. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  85. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  86. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  87. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  88. #define cudaMemcpyKind hipMemcpyKind
  89. #define cudaMemset hipMemset
  90. #define cudaMemsetAsync hipMemsetAsync
  91. #define cudaMemGetInfo hipMemGetInfo
  92. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  93. #define cudaSetDevice hipSetDevice
  94. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  95. #define cudaStreamFireAndForget hipStreamFireAndForget
  96. #define cudaStreamNonBlocking hipStreamNonBlocking
  97. #define cudaStreamSynchronize hipStreamSynchronize
  98. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  99. #define cudaStream_t hipStream_t
  100. #define cudaSuccess hipSuccess
  101. #define __trap abort
  102. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  103. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  104. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  105. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  106. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  107. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  108. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  109. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  110. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  111. #else
  112. #include <cuda_runtime.h>
  113. #include <cuda.h>
  114. #include <cublas_v2.h>
  115. #include <cuda_fp16.h>
  116. #if CUDART_VERSION < 11020
  117. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  118. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  119. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  120. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  121. #define cublasComputeType_t cudaDataType_t
  122. #endif // CUDART_VERSION < 11020
  123. #endif // defined(GGML_USE_HIPBLAS)
  124. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  125. #define CC_PASCAL 600
  126. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  127. #define CC_VOLTA 700
  128. #define CC_OFFSET_AMD 1000000
  129. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  130. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  131. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  132. #define GGML_CUDA_MAX_NODES 8192
  133. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  134. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  135. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  136. // - 7B quantum model: +100-200 MB
  137. // - 13B quantum model: +200-400 MB
  138. //
  139. //#define GGML_CUDA_FORCE_MMQ
  140. // TODO: improve this to be correct for more hardware
  141. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  142. #if !defined(GGML_CUDA_FORCE_MMQ)
  143. #define CUDA_USE_TENSOR_CORES
  144. #endif
  145. #define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
  146. #define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
  147. #if defined(GGML_USE_HIPBLAS)
  148. #define __CUDA_ARCH__ 1300
  149. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  150. defined(__gfx1150__) || defined(__gfx1151__)
  151. #define RDNA3
  152. #endif
  153. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  154. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  155. #define RDNA2
  156. #endif
  157. #ifndef __has_builtin
  158. #define __has_builtin(x) 0
  159. #endif
  160. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  161. typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4)));
  162. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  163. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  164. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  165. #if __has_builtin(__builtin_elementwise_sub_sat)
  166. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  167. return reinterpret_cast<const int &>(c);
  168. #else
  169. int8x4_t c;
  170. int16_t tmp;
  171. #pragma unroll
  172. for (int i = 0; i < 4; i++) {
  173. tmp = va[i] - vb[i];
  174. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  175. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  176. c[i] = tmp;
  177. }
  178. return reinterpret_cast<int &>(c);
  179. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  180. }
  181. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  182. return __vsubss4(a, b);
  183. }
  184. static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) {
  185. const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a);
  186. const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b);
  187. unsigned int c;
  188. uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c);
  189. #pragma unroll
  190. for (int i = 0; i < 4; ++i) {
  191. vc[i] = va[i] == vb[i] ? 0xff : 0x00;
  192. }
  193. return c;
  194. }
  195. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  196. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  197. c = __builtin_amdgcn_sdot4(a, b, c, false);
  198. #elif defined(RDNA3)
  199. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  200. #elif defined(__gfx1010__) || defined(__gfx900__)
  201. int tmp1;
  202. int tmp2;
  203. asm("\n \
  204. 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 \
  205. 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 \
  206. v_add3_u32 %0, %1, %2, %0 \n \
  207. 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 \
  208. 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 \
  209. v_add3_u32 %0, %1, %2, %0 \n \
  210. "
  211. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  212. : "v"(a), "v"(b)
  213. );
  214. #else
  215. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  216. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  217. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  218. #endif
  219. return c;
  220. }
  221. #endif // defined(GGML_USE_HIPBLAS)
  222. #if defined(_MSC_VER)
  223. #pragma warning(disable: 4244 4267) // possible loss of data
  224. #endif
  225. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  226. [[noreturn]]
  227. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  228. int id = -1; // in case cudaGetDevice fails
  229. cudaGetDevice(&id);
  230. fprintf(stderr, "CUDA error: %s\n", msg);
  231. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  232. fprintf(stderr, " %s\n", stmt);
  233. // abort with GGML_ASSERT to get a stack trace
  234. GGML_ASSERT(!"CUDA error");
  235. }
  236. #define CUDA_CHECK_GEN(err, success, error_fn) \
  237. do { \
  238. auto err_ = (err); \
  239. if (err_ != (success)) { \
  240. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  241. } \
  242. } while (0)
  243. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  244. #if CUDART_VERSION >= 12000
  245. static const char * cublas_get_error_str(const cublasStatus_t err) {
  246. return cublasGetStatusString(err);
  247. }
  248. #else
  249. static const char * cublas_get_error_str(const cublasStatus_t err) {
  250. switch (err) {
  251. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  252. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  253. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  254. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  255. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  256. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  257. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  258. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  259. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  260. default: return "unknown error";
  261. }
  262. }
  263. #endif // CUDART_VERSION >= 12000
  264. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  265. #if !defined(GGML_USE_HIPBLAS)
  266. static const char * cu_get_error_str(CUresult err) {
  267. const char * err_str;
  268. cuGetErrorString(err, &err_str);
  269. return err_str;
  270. }
  271. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  272. #endif
  273. #if CUDART_VERSION >= 11100
  274. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  275. #else
  276. #define GGML_CUDA_ASSUME(x)
  277. #endif // CUDART_VERSION >= 11100
  278. #ifdef GGML_CUDA_F16
  279. typedef half dfloat; // dequantize float
  280. typedef half2 dfloat2;
  281. #else
  282. typedef float dfloat; // dequantize float
  283. typedef float2 dfloat2;
  284. #endif //GGML_CUDA_F16
  285. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  286. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  287. int x32 = 0;
  288. x32 |= x16[0] << 0;
  289. x32 |= x16[1] << 16;
  290. return x32;
  291. }
  292. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  293. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  294. int x32 = 0;
  295. x32 |= x16[0] << 0;
  296. x32 |= x16[1] << 16;
  297. return x32;
  298. }
  299. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  300. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  301. }
  302. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  303. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  304. }
  305. template<typename T>
  306. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  307. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  308. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  309. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  310. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  311. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  312. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  313. typedef void (*ggml_cuda_op_mul_mat_t)(
  314. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  315. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  316. const int64_t src1_padded_row_size, cudaStream_t stream);
  317. typedef void (*ggml_cuda_op_flatten_t)(
  318. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  319. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  320. // QK = number of values after dequantization
  321. // QR = QK / number of values before dequantization
  322. // QI = number of 32 bit integers before dequantization
  323. #define QK4_0 32
  324. #define QR4_0 2
  325. #define QI4_0 (QK4_0 / (4 * QR4_0))
  326. typedef struct {
  327. half d; // delta
  328. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  329. } block_q4_0;
  330. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  331. #define QK4_1 32
  332. #define QR4_1 2
  333. #define QI4_1 (QK4_1 / (4 * QR4_1))
  334. typedef struct {
  335. half2 dm; // dm.x = delta, dm.y = min
  336. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  337. } block_q4_1;
  338. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  339. #define QK5_0 32
  340. #define QR5_0 2
  341. #define QI5_0 (QK5_0 / (4 * QR5_0))
  342. typedef struct {
  343. half d; // delta
  344. uint8_t qh[4]; // 5-th bit of quants
  345. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  346. } block_q5_0;
  347. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  348. #define QK5_1 32
  349. #define QR5_1 2
  350. #define QI5_1 (QK5_1 / (4 * QR5_1))
  351. typedef struct {
  352. half2 dm; // dm.x = delta, dm.y = min
  353. uint8_t qh[4]; // 5-th bit of quants
  354. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  355. } block_q5_1;
  356. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  357. #define QK8_0 32
  358. #define QR8_0 1
  359. #define QI8_0 (QK8_0 / (4 * QR8_0))
  360. typedef struct {
  361. half d; // delta
  362. int8_t qs[QK8_0]; // quants
  363. } block_q8_0;
  364. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  365. #define QK8_1 32
  366. #define QR8_1 1
  367. #define QI8_1 (QK8_1 / (4 * QR8_1))
  368. typedef struct {
  369. half2 ds; // ds.x = delta, ds.y = sum
  370. int8_t qs[QK8_0]; // quants
  371. } block_q8_1;
  372. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  373. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  374. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  375. typedef void (*load_tiles_cuda_t)(
  376. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  377. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  378. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  379. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  380. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  381. //================================= k-quants
  382. #ifdef GGML_QKK_64
  383. #define QK_K 64
  384. #define K_SCALE_SIZE 4
  385. #else
  386. #define QK_K 256
  387. #define K_SCALE_SIZE 12
  388. #endif
  389. #define QR2_K 4
  390. #define QI2_K (QK_K / (4*QR2_K))
  391. typedef struct {
  392. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  393. uint8_t qs[QK_K/4]; // quants
  394. half2 dm; // super-block scale for quantized scales/mins
  395. } block_q2_K;
  396. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  397. #define QR3_K 4
  398. #define QI3_K (QK_K / (4*QR3_K))
  399. typedef struct {
  400. uint8_t hmask[QK_K/8]; // quants - high bit
  401. uint8_t qs[QK_K/4]; // quants - low 2 bits
  402. #ifdef GGML_QKK_64
  403. uint8_t scales[2]; // scales, quantized with 8 bits
  404. #else
  405. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  406. #endif
  407. half d; // super-block scale
  408. } block_q3_K;
  409. //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");
  410. #define QR4_K 2
  411. #define QI4_K (QK_K / (4*QR4_K))
  412. #ifdef GGML_QKK_64
  413. typedef struct {
  414. half dm[2]; // super-block scales/mins
  415. uint8_t scales[2]; // 4-bit block scales/mins
  416. uint8_t qs[QK_K/2]; // 4--bit quants
  417. } block_q4_K;
  418. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  419. #else
  420. typedef struct {
  421. half2 dm; // super-block scale for quantized scales/mins
  422. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  423. uint8_t qs[QK_K/2]; // 4--bit quants
  424. } block_q4_K;
  425. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  426. #endif
  427. #define QR5_K 2
  428. #define QI5_K (QK_K / (4*QR5_K))
  429. #ifdef GGML_QKK_64
  430. typedef struct {
  431. half d; // super-block scale
  432. int8_t scales[QK_K/16]; // block scales
  433. uint8_t qh[QK_K/8]; // quants, high bit
  434. uint8_t qs[QK_K/2]; // quants, low 4 bits
  435. } block_q5_K;
  436. 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");
  437. #else
  438. typedef struct {
  439. half2 dm; // super-block scale for quantized scales/mins
  440. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  441. uint8_t qh[QK_K/8]; // quants, high bit
  442. uint8_t qs[QK_K/2]; // quants, low 4 bits
  443. } block_q5_K;
  444. 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");
  445. #endif
  446. #define QR6_K 2
  447. #define QI6_K (QK_K / (4*QR6_K))
  448. typedef struct {
  449. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  450. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  451. int8_t scales[QK_K/16]; // scales
  452. half d; // delta
  453. } block_q6_K;
  454. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  455. #define QR2_XXS 8
  456. #define QI2_XXS (QK_K / (4*QR2_XXS))
  457. typedef struct {
  458. half d;
  459. uint16_t qs[QK_K/8];
  460. } block_iq2_xxs;
  461. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  462. #define QR2_XS 8
  463. #define QI2_XS (QK_K / (4*QR2_XS))
  464. typedef struct {
  465. half d;
  466. uint16_t qs[QK_K/8];
  467. uint8_t scales[QK_K/32];
  468. } block_iq2_xs;
  469. static_assert(sizeof(block_iq2_xs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");
  470. // 2.5625 bpw quants
  471. #define QR2_S 8
  472. #define QI2_S (QK_K / (4*QR2_S))
  473. typedef struct {
  474. half d;
  475. uint8_t qs[QK_K/4];
  476. uint8_t qh[QK_K/32];
  477. uint8_t scales[QK_K/32];
  478. } block_iq2_s;
  479. static_assert(sizeof(block_iq2_s) == sizeof(ggml_fp16_t) + QK_K/4 + QK_K/16, "wrong iq2_s block size/padding");
  480. #define QR3_XXS 8
  481. #define QI3_XXS (QK_K / (4*QR3_XXS))
  482. typedef struct {
  483. half d;
  484. uint8_t qs[3*(QK_K/8)];
  485. } block_iq3_xxs;
  486. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_fp16_t) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  487. #define QR3_XS 8
  488. #define QI3_XS (QK_K / (4*QR3_XS))
  489. #if QK_K == 64
  490. #define IQ3S_N_SCALE 2
  491. #else
  492. #define IQ3S_N_SCALE QK_K/64
  493. #endif
  494. typedef struct {
  495. half d;
  496. uint8_t qs[QK_K/4];
  497. uint8_t qh[QK_K/32];
  498. uint8_t signs[QK_K/8];
  499. uint8_t scales[IQ3S_N_SCALE];
  500. } block_iq3_s;
  501. static_assert(sizeof(block_iq3_s) == sizeof(ggml_fp16_t) + 13*(QK_K/32) + IQ3S_N_SCALE, "wrong iq3_s block size/padding");
  502. #define QR1_S 8
  503. #define QI1_S (QK_K / (4*QR1_S))
  504. typedef struct {
  505. half d;
  506. uint8_t qs[QK_K/8];
  507. uint16_t qh[QK_K/32];
  508. } block_iq1_s;
  509. static_assert(sizeof(block_iq1_s) == sizeof(ggml_fp16_t) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  510. #define QK4_NL 32
  511. #define QR4_NL 2
  512. #define QI4_NL (QK4_NL / (4*QR4_NL))
  513. typedef struct {
  514. half d;
  515. uint8_t qs[QK4_NL/2];
  516. } block_iq4_nl;
  517. static_assert(sizeof(block_iq4_nl) == sizeof(ggml_fp16_t) + QK4_NL/2, "wrong iq4_nl block size/padding");
  518. #if QK_K == 64
  519. #define block_iq4_xs block_iq4_nl
  520. #define QR4_XS QR4_NL
  521. #define QI4_XS QI4_NL
  522. #else
  523. // QR4_XS = 8 is very slightly faster than QR4_XS = 4
  524. #define QR4_XS 8
  525. #define QI4_XS (QK_K / (4*QR4_XS))
  526. typedef struct {
  527. half d;
  528. uint16_t scales_h;
  529. uint8_t scales_l[QK_K/64];
  530. uint8_t qs[QK_K/2];
  531. } block_iq4_xs;
  532. static_assert(sizeof(block_iq4_xs) == sizeof(ggml_fp16_t) + sizeof(uint16_t) + QK_K/64 + QK_K/2, "wrong iq4_xs block size/padding");
  533. #endif
  534. #define WARP_SIZE 32
  535. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  536. #define CUDA_GELU_BLOCK_SIZE 256
  537. #define CUDA_SILU_BLOCK_SIZE 256
  538. #define CUDA_TANH_BLOCK_SIZE 256
  539. #define CUDA_RELU_BLOCK_SIZE 256
  540. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  541. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  542. #define CUDA_SQR_BLOCK_SIZE 256
  543. #define CUDA_CPY_BLOCK_SIZE 32
  544. #define CUDA_SCALE_BLOCK_SIZE 256
  545. #define CUDA_CLAMP_BLOCK_SIZE 256
  546. #define CUDA_ROPE_BLOCK_SIZE 256
  547. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  548. #define CUDA_ALIBI_BLOCK_SIZE 32
  549. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  550. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  551. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  552. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  553. #define CUDA_UPSCALE_BLOCK_SIZE 256
  554. #define CUDA_CONCAT_BLOCK_SIZE 256
  555. #define CUDA_PAD_BLOCK_SIZE 256
  556. #define CUDA_ARANGE_BLOCK_SIZE 256
  557. #define CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE 256
  558. #define CUDA_ACC_BLOCK_SIZE 256
  559. #define CUDA_IM2COL_BLOCK_SIZE 256
  560. #define CUDA_POOL2D_BLOCK_SIZE 256
  561. #define CUDA_Q8_0_NE_ALIGN 2048
  562. // dmmv = dequantize_mul_mat_vec
  563. #ifndef GGML_CUDA_DMMV_X
  564. #define GGML_CUDA_DMMV_X 32
  565. #endif
  566. #ifndef GGML_CUDA_MMV_Y
  567. #define GGML_CUDA_MMV_Y 1
  568. #endif
  569. #ifndef K_QUANTS_PER_ITERATION
  570. #define K_QUANTS_PER_ITERATION 2
  571. #else
  572. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  573. #endif
  574. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  575. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  576. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  577. #define MUL_MAT_SRC1_COL_STRIDE 128
  578. #define MAX_STREAMS 8
  579. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  580. struct ggml_tensor_extra_gpu {
  581. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  582. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  583. };
  584. // this is faster on Windows
  585. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  586. static void ggml_cuda_set_device(const int device) {
  587. int current_device;
  588. CUDA_CHECK(cudaGetDevice(&current_device));
  589. if (device == current_device) {
  590. return;
  591. }
  592. CUDA_CHECK(cudaSetDevice(device));
  593. }
  594. static int g_device_count = -1;
  595. static int g_main_device = 0;
  596. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  597. struct cuda_device_capabilities {
  598. int cc; // compute capability
  599. size_t smpb; // max. shared memory per block
  600. bool vmm; // virtual memory support
  601. size_t vmm_granularity; // granularity of virtual memory
  602. };
  603. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  604. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  605. [[noreturn]]
  606. static __device__ void no_device_code(
  607. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  608. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  609. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  610. file_name, line, function_name, arch);
  611. (void) arch_list;
  612. #else
  613. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  614. file_name, line, function_name, arch, arch_list);
  615. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  616. __trap();
  617. (void) no_device_code; // suppress unused function warning
  618. }
  619. #ifdef __CUDA_ARCH__
  620. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  621. #else
  622. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  623. #endif // __CUDA_ARCH__
  624. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  625. #pragma unroll
  626. for (int mask = 16; mask > 0; mask >>= 1) {
  627. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  628. }
  629. return x;
  630. }
  631. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  632. #pragma unroll
  633. for (int mask = 16; mask > 0; mask >>= 1) {
  634. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  635. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  636. }
  637. return a;
  638. }
  639. #ifdef GGML_CUDA_F16
  640. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  641. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  642. #pragma unroll
  643. for (int mask = 16; mask > 0; mask >>= 1) {
  644. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  645. }
  646. return a;
  647. #else
  648. (void) a;
  649. NO_DEVICE_CODE;
  650. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  651. }
  652. #endif // GGML_CUDA_F16
  653. static __device__ __forceinline__ float warp_reduce_max(float x) {
  654. #pragma unroll
  655. for (int mask = 16; mask > 0; mask >>= 1) {
  656. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  657. }
  658. return x;
  659. }
  660. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  661. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  662. //#pragma unroll
  663. // for (int mask = 16; mask > 0; mask >>= 1) {
  664. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  665. // }
  666. // return x;
  667. //#else
  668. // (void) x;
  669. // NO_DEVICE_CODE;
  670. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  671. //}
  672. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  673. return b;
  674. GGML_UNUSED(a);
  675. }
  676. static __device__ __forceinline__ float op_add(const float a, const float b) {
  677. return a + b;
  678. }
  679. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  680. return a * b;
  681. }
  682. static __device__ __forceinline__ float op_div(const float a, const float b) {
  683. return a / b;
  684. }
  685. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  686. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  687. int ne0, int ne1, int ne2, int ne3,
  688. int ne10, int ne11, int ne12, int ne13,
  689. /*int s0, */ int s1, int s2, int s3,
  690. /*int s10,*/ int s11, int s12, int s13) {
  691. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  692. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  693. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  694. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  695. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  696. return;
  697. }
  698. const int i11 = i1 % ne11;
  699. const int i12 = i2 % ne12;
  700. const int i13 = i3 % ne13;
  701. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  702. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  703. const size_t i_dst = i_src0;
  704. const src0_t * src0_row = src0 + i_src0;
  705. const src1_t * src1_row = src1 + i_src1;
  706. dst_t * dst_row = dst + i_dst;
  707. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  708. const int i10 = i0 % ne10;
  709. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  710. }
  711. }
  712. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  713. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  714. int ne0, int ne1, int ne2, int ne3,
  715. int ne10, int ne11, int ne12, int ne13,
  716. /*int s0, */ int s1, int s2, int s3,
  717. /*int s10,*/ int s11, int s12, int s13) {
  718. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  719. const int i3 = i/(ne2*ne1*ne0);
  720. const int i2 = (i/(ne1*ne0)) % ne2;
  721. const int i1 = (i/ne0) % ne1;
  722. const int i0 = i % ne0;
  723. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  724. return;
  725. }
  726. const int i11 = i1 % ne11;
  727. const int i12 = i2 % ne12;
  728. const int i13 = i3 % ne13;
  729. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  730. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  731. const size_t i_dst = i_src0;
  732. const src0_t * src0_row = src0 + i_src0;
  733. const src1_t * src1_row = src1 + i_src1;
  734. dst_t * dst_row = dst + i_dst;
  735. const int i10 = i0 % ne10;
  736. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  737. }
  738. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  739. const int ne10, const int ne11, const int ne12,
  740. const int nb1, const int nb2, int offset) {
  741. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  742. if (i >= ne) {
  743. return;
  744. }
  745. int src1_idx = i - offset;
  746. int oz = src1_idx / nb2;
  747. int oy = (src1_idx - (oz * nb2)) / nb1;
  748. int ox = src1_idx % nb1;
  749. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  750. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  751. } else {
  752. dst[i] = x[i];
  753. }
  754. }
  755. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  756. const float GELU_COEF_A = 0.044715f;
  757. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  758. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  759. if (i >= k) {
  760. return;
  761. }
  762. float xi = x[i];
  763. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  764. }
  765. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  766. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  767. if (i >= k) {
  768. return;
  769. }
  770. dst[i] = x[i] / (1.0f + expf(-x[i]));
  771. }
  772. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  773. const float GELU_QUICK_COEF = -1.702f;
  774. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  775. if (i >= k) {
  776. return;
  777. }
  778. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  779. }
  780. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  781. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  782. if (i >= k) {
  783. return;
  784. }
  785. dst[i] = tanhf(x[i]);
  786. }
  787. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  788. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  789. if (i >= k) {
  790. return;
  791. }
  792. dst[i] = fmaxf(x[i], 0);
  793. }
  794. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  795. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  796. if (i >= k) {
  797. return;
  798. }
  799. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  800. }
  801. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  802. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  803. if (i >= k) {
  804. return;
  805. }
  806. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  807. }
  808. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  809. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  810. if (i >= k) {
  811. return;
  812. }
  813. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  814. }
  815. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  816. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  817. if (i >= k) {
  818. return;
  819. }
  820. dst[i] = x[i] * x[i];
  821. }
  822. template <int block_size>
  823. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  824. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  825. const int tid = threadIdx.x;
  826. float2 mean_var = make_float2(0.f, 0.f);
  827. for (int col = tid; col < ncols; col += block_size) {
  828. const float xi = x[row*ncols + col];
  829. mean_var.x += xi;
  830. mean_var.y += xi * xi;
  831. }
  832. // sum up partial sums
  833. mean_var = warp_reduce_sum(mean_var);
  834. if (block_size > WARP_SIZE) {
  835. __shared__ float2 s_sum[32];
  836. int warp_id = threadIdx.x / WARP_SIZE;
  837. int lane_id = threadIdx.x % WARP_SIZE;
  838. if (lane_id == 0) {
  839. s_sum[warp_id] = mean_var;
  840. }
  841. __syncthreads();
  842. mean_var = s_sum[lane_id];
  843. mean_var = warp_reduce_sum(mean_var);
  844. }
  845. const float mean = mean_var.x / ncols;
  846. const float var = mean_var.y / ncols - mean * mean;
  847. const float inv_std = rsqrtf(var + eps);
  848. for (int col = tid; col < ncols; col += block_size) {
  849. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  850. }
  851. }
  852. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  853. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  854. if (nidx >= ne0) {
  855. return;
  856. }
  857. // operation
  858. int offset_dst =
  859. nidx +
  860. blockIdx.y * ne0 +
  861. blockIdx.z * ne0 * gridDim.y;
  862. if (blockIdx.z < ne02) { // src0
  863. int offset_src =
  864. nidx +
  865. blockIdx.y * ne0 +
  866. blockIdx.z * ne0 * gridDim.y;
  867. dst[offset_dst] = x[offset_src];
  868. } else {
  869. int offset_src =
  870. nidx +
  871. blockIdx.y * ne0 +
  872. (blockIdx.z - ne02) * ne0 * gridDim.y;
  873. dst[offset_dst] = y[offset_src];
  874. }
  875. }
  876. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int ne00xne01, const int scale_factor) {
  877. // blockIdx.z: idx of ne02*ne03
  878. // blockIdx.y: idx of ne01*scale_factor, aka ne1
  879. // blockIDx.x: idx of ne00*scale_factor / BLOCK_SIZE
  880. // ne00xne01: ne00 * ne01
  881. int ne0 = ne00 * scale_factor;
  882. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  883. if (nidx >= ne0) {
  884. return;
  885. }
  886. // operation
  887. int i00 = nidx / scale_factor;
  888. int i01 = blockIdx.y / scale_factor;
  889. int offset_src =
  890. i00 +
  891. i01 * ne00 +
  892. blockIdx.z * ne00xne01;
  893. int offset_dst =
  894. nidx +
  895. blockIdx.y * ne0 +
  896. blockIdx.z * ne0 * gridDim.y;
  897. dst[offset_dst] = x[offset_src];
  898. }
  899. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02, const int ne03) {
  900. // blockIdx.z: idx of ne2*ne3, aka ne02*ne03
  901. // blockIdx.y: idx of ne1
  902. // blockIDx.x: idx of ne0 / BLOCK_SIZE
  903. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  904. if (nidx >= ne0) {
  905. return;
  906. }
  907. // operation
  908. int offset_dst =
  909. nidx +
  910. blockIdx.y * ne0 +
  911. blockIdx.z * ne0 * gridDim.y;
  912. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02*ne03) {
  913. int offset_src =
  914. nidx +
  915. blockIdx.y * ne00 +
  916. blockIdx.z * ne00 * ne01;
  917. dst[offset_dst] = x[offset_src];
  918. } else {
  919. dst[offset_dst] = 0.0f;
  920. }
  921. }
  922. static __global__ void arange_f32(float * dst, const int ne0, const float start, const float step) {
  923. // blockIDx.x: idx of ne0 / BLOCK_SIZE
  924. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  925. if (nidx >= ne0) {
  926. return;
  927. }
  928. dst[nidx] = start + step * nidx;
  929. }
  930. static __global__ void timestep_embedding_f32(const float * timesteps, float * dst, const int nb1, const int dim, const int max_period) {
  931. // blockIDx.y: idx of timesteps->ne[0]
  932. // blockIDx.x: idx of ((dim + 1) / 2) / BLOCK_SIZE
  933. int i = blockIdx.y;
  934. int j = threadIdx.x + blockIdx.x * blockDim.x;
  935. float * embed_data = (float *)((char *)dst + i*nb1);
  936. if (dim % 2 != 0 && j == ((dim + 1) / 2)) {
  937. embed_data[dim] = 0.f;
  938. }
  939. int half = dim / 2;
  940. if (j >= half) {
  941. return;
  942. }
  943. float timestep = timesteps[i];
  944. float freq = (float)expf(-logf(max_period) * j / half);
  945. float arg = timestep * freq;
  946. embed_data[j] = cosf(arg);
  947. embed_data[j + half] = sinf(arg);
  948. }
  949. template <int block_size>
  950. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  951. // blockIdx.x: num_groups idx
  952. // threadIdx.x: block_size idx
  953. int start = blockIdx.x * group_size;
  954. int end = start + group_size;
  955. start += threadIdx.x;
  956. if (end >= ne_elements) {
  957. end = ne_elements;
  958. }
  959. float tmp = 0.0f; // partial sum for thread in warp
  960. for (int j = start; j < end; j += block_size) {
  961. tmp += x[j];
  962. }
  963. tmp = warp_reduce_sum(tmp);
  964. if (block_size > WARP_SIZE) {
  965. __shared__ float s_sum[32];
  966. int warp_id = threadIdx.x / WARP_SIZE;
  967. int lane_id = threadIdx.x % WARP_SIZE;
  968. if (lane_id == 0) {
  969. s_sum[warp_id] = tmp;
  970. }
  971. __syncthreads();
  972. tmp = s_sum[lane_id];
  973. tmp = warp_reduce_sum(tmp);
  974. }
  975. float mean = tmp / group_size;
  976. tmp = 0.0f;
  977. for (int j = start; j < end; j += block_size) {
  978. float xi = x[j] - mean;
  979. dst[j] = xi;
  980. tmp += xi * xi;
  981. }
  982. tmp = warp_reduce_sum(tmp);
  983. if (block_size > WARP_SIZE) {
  984. __shared__ float s_sum[32];
  985. int warp_id = threadIdx.x / WARP_SIZE;
  986. int lane_id = threadIdx.x % WARP_SIZE;
  987. if (lane_id == 0) {
  988. s_sum[warp_id] = tmp;
  989. }
  990. __syncthreads();
  991. tmp = s_sum[lane_id];
  992. tmp = warp_reduce_sum(tmp);
  993. }
  994. float variance = tmp / group_size;
  995. float scale = rsqrtf(variance + eps);
  996. for (int j = start; j < end; j += block_size) {
  997. dst[j] *= scale;
  998. }
  999. }
  1000. template <int block_size>
  1001. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  1002. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1003. const int tid = threadIdx.x;
  1004. float tmp = 0.0f; // partial sum for thread in warp
  1005. for (int col = tid; col < ncols; col += block_size) {
  1006. const float xi = x[row*ncols + col];
  1007. tmp += xi * xi;
  1008. }
  1009. // sum up partial sums
  1010. tmp = warp_reduce_sum(tmp);
  1011. if (block_size > WARP_SIZE) {
  1012. __shared__ float s_sum[32];
  1013. int warp_id = threadIdx.x / WARP_SIZE;
  1014. int lane_id = threadIdx.x % WARP_SIZE;
  1015. if (lane_id == 0) {
  1016. s_sum[warp_id] = tmp;
  1017. }
  1018. __syncthreads();
  1019. tmp = s_sum[lane_id];
  1020. tmp = warp_reduce_sum(tmp);
  1021. }
  1022. const float mean = tmp / ncols;
  1023. const float scale = rsqrtf(mean + eps);
  1024. for (int col = tid; col < ncols; col += block_size) {
  1025. dst[row*ncols + col] = scale * x[row*ncols + col];
  1026. }
  1027. }
  1028. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1029. const block_q4_0 * x = (const block_q4_0 *) vx;
  1030. const dfloat d = x[ib].d;
  1031. const int vui = x[ib].qs[iqs];
  1032. v.x = vui & 0xF;
  1033. v.y = vui >> 4;
  1034. #ifdef GGML_CUDA_F16
  1035. v = __hsub2(v, {8.0f, 8.0f});
  1036. v = __hmul2(v, {d, d});
  1037. #else
  1038. v.x = (v.x - 8.0f) * d;
  1039. v.y = (v.y - 8.0f) * d;
  1040. #endif // GGML_CUDA_F16
  1041. }
  1042. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1043. const block_q4_1 * x = (const block_q4_1 *) vx;
  1044. const dfloat d = __low2half(x[ib].dm);
  1045. const dfloat m = __high2half(x[ib].dm);
  1046. const int vui = x[ib].qs[iqs];
  1047. v.x = vui & 0xF;
  1048. v.y = vui >> 4;
  1049. #ifdef GGML_CUDA_F16
  1050. v = __hmul2(v, {d, d});
  1051. v = __hadd2(v, {m, m});
  1052. #else
  1053. v.x = (v.x * d) + m;
  1054. v.y = (v.y * d) + m;
  1055. #endif // GGML_CUDA_F16
  1056. }
  1057. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1058. const block_q5_0 * x = (const block_q5_0 *) vx;
  1059. const dfloat d = x[ib].d;
  1060. uint32_t qh;
  1061. memcpy(&qh, x[ib].qh, sizeof(qh));
  1062. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1063. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1064. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1065. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1066. #ifdef GGML_CUDA_F16
  1067. v = __hsub2(v, {16.0f, 16.0f});
  1068. v = __hmul2(v, {d, d});
  1069. #else
  1070. v.x = (v.x - 16.0f) * d;
  1071. v.y = (v.y - 16.0f) * d;
  1072. #endif // GGML_CUDA_F16
  1073. }
  1074. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1075. const block_q5_1 * x = (const block_q5_1 *) vx;
  1076. const dfloat d = __low2half(x[ib].dm);
  1077. const dfloat m = __high2half(x[ib].dm);
  1078. uint32_t qh;
  1079. memcpy(&qh, x[ib].qh, sizeof(qh));
  1080. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  1081. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  1082. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  1083. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  1084. #ifdef GGML_CUDA_F16
  1085. v = __hmul2(v, {d, d});
  1086. v = __hadd2(v, {m, m});
  1087. #else
  1088. v.x = (v.x * d) + m;
  1089. v.y = (v.y * d) + m;
  1090. #endif // GGML_CUDA_F16
  1091. }
  1092. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1093. const block_q8_0 * x = (const block_q8_0 *) vx;
  1094. const dfloat d = x[ib].d;
  1095. v.x = x[ib].qs[iqs + 0];
  1096. v.y = x[ib].qs[iqs + 1];
  1097. #ifdef GGML_CUDA_F16
  1098. v = __hmul2(v, {d, d});
  1099. #else
  1100. v.x *= d;
  1101. v.y *= d;
  1102. #endif // GGML_CUDA_F16
  1103. }
  1104. template<typename dst_t>
  1105. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1106. const int i = blockIdx.x;
  1107. // assume 32 threads
  1108. const int tid = threadIdx.x;
  1109. const int il = tid/8;
  1110. const int ir = tid%8;
  1111. const int ib = 8*i + ir;
  1112. if (ib >= nb32) {
  1113. return;
  1114. }
  1115. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1116. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1117. const float d = __half2float(x->d);
  1118. const float dm = -8*d;
  1119. const uint8_t * q = x->qs + 4*il;
  1120. for (int l = 0; l < 4; ++l) {
  1121. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1122. y[l+16] = d * (q[l] >> 4) + dm;
  1123. }
  1124. }
  1125. template<typename dst_t>
  1126. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1127. const int i = blockIdx.x;
  1128. // assume 32 threads
  1129. const int tid = threadIdx.x;
  1130. const int il = tid/8;
  1131. const int ir = tid%8;
  1132. const int ib = 8*i + ir;
  1133. if (ib >= nb32) {
  1134. return;
  1135. }
  1136. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1137. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1138. const float2 d = __half22float2(x->dm);
  1139. const uint8_t * q = x->qs + 4*il;
  1140. for (int l = 0; l < 4; ++l) {
  1141. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1142. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1143. }
  1144. }
  1145. //================================== k-quants
  1146. template<typename dst_t>
  1147. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1148. const int i = blockIdx.x;
  1149. const block_q2_K * x = (const block_q2_K *) vx;
  1150. const int tid = threadIdx.x;
  1151. #if QK_K == 256
  1152. const int n = tid/32;
  1153. const int l = tid - 32*n;
  1154. const int is = 8*n + l/16;
  1155. const uint8_t q = x[i].qs[32*n + l];
  1156. dst_t * y = yy + i*QK_K + 128*n;
  1157. float dall = __low2half(x[i].dm);
  1158. float dmin = __high2half(x[i].dm);
  1159. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1160. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1161. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1162. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1163. #else
  1164. const int is = tid/16; // 0 or 1
  1165. const int il = tid%16; // 0...15
  1166. const uint8_t q = x[i].qs[il] >> (2*is);
  1167. dst_t * y = yy + i*QK_K + 16*is + il;
  1168. float dall = __low2half(x[i].dm);
  1169. float dmin = __high2half(x[i].dm);
  1170. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1171. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1172. #endif
  1173. }
  1174. template<typename dst_t>
  1175. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1176. const int i = blockIdx.x;
  1177. const block_q3_K * x = (const block_q3_K *) vx;
  1178. #if QK_K == 256
  1179. const int r = threadIdx.x/4;
  1180. const int tid = r/2;
  1181. const int is0 = r%2;
  1182. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1183. const int n = tid / 4;
  1184. const int j = tid - 4*n;
  1185. uint8_t m = 1 << (4*n + j);
  1186. int is = 8*n + 2*j + is0;
  1187. int shift = 2*j;
  1188. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1189. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1190. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1191. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1192. float d_all = x[i].d;
  1193. float dl = d_all * (us - 32);
  1194. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1195. const uint8_t * q = x[i].qs + 32*n;
  1196. const uint8_t * hm = x[i].hmask;
  1197. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1198. #else
  1199. const int tid = threadIdx.x;
  1200. const int is = tid/16; // 0 or 1
  1201. const int il = tid%16; // 0...15
  1202. const int im = il/8; // 0...1
  1203. const int in = il%8; // 0...7
  1204. dst_t * y = yy + i*QK_K + 16*is + il;
  1205. const uint8_t q = x[i].qs[il] >> (2*is);
  1206. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1207. const float d = (float)x[i].d;
  1208. if (is == 0) {
  1209. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1210. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1211. } else {
  1212. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1213. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1214. }
  1215. #endif
  1216. }
  1217. #if QK_K == 256
  1218. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1219. if (j < 4) {
  1220. d = q[j] & 63; m = q[j + 4] & 63;
  1221. } else {
  1222. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1223. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1224. }
  1225. }
  1226. #endif
  1227. template<typename dst_t>
  1228. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1229. const block_q4_K * x = (const block_q4_K *) vx;
  1230. const int i = blockIdx.x;
  1231. #if QK_K == 256
  1232. // assume 32 threads
  1233. const int tid = threadIdx.x;
  1234. const int il = tid/8;
  1235. const int ir = tid%8;
  1236. const int is = 2*il;
  1237. const int n = 4;
  1238. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1239. const float dall = __low2half(x[i].dm);
  1240. const float dmin = __high2half(x[i].dm);
  1241. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1242. uint8_t sc, m;
  1243. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1244. const float d1 = dall * sc; const float m1 = dmin * m;
  1245. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1246. const float d2 = dall * sc; const float m2 = dmin * m;
  1247. for (int l = 0; l < n; ++l) {
  1248. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1249. y[l +32] = d2 * (q[l] >> 4) - m2;
  1250. }
  1251. #else
  1252. const int tid = threadIdx.x;
  1253. const uint8_t * q = x[i].qs;
  1254. dst_t * y = yy + i*QK_K;
  1255. const float d = (float)x[i].dm[0];
  1256. const float m = (float)x[i].dm[1];
  1257. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1258. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1259. #endif
  1260. }
  1261. template<typename dst_t>
  1262. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1263. const block_q5_K * x = (const block_q5_K *) vx;
  1264. const int i = blockIdx.x;
  1265. #if QK_K == 256
  1266. // assume 64 threads - this is very slightly better than the one below
  1267. const int tid = threadIdx.x;
  1268. const int il = tid/16; // il is in 0...3
  1269. const int ir = tid%16; // ir is in 0...15
  1270. const int is = 2*il; // is is in 0...6
  1271. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1272. const float dall = __low2half(x[i].dm);
  1273. const float dmin = __high2half(x[i].dm);
  1274. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1275. const uint8_t * qh = x[i].qh + 2*ir;
  1276. uint8_t sc, m;
  1277. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1278. const float d1 = dall * sc; const float m1 = dmin * m;
  1279. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1280. const float d2 = dall * sc; const float m2 = dmin * m;
  1281. uint8_t hm = 1 << (2*il);
  1282. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1283. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1284. hm <<= 1;
  1285. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1286. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1287. #else
  1288. const int tid = threadIdx.x;
  1289. const uint8_t q = x[i].qs[tid];
  1290. const int im = tid/8; // 0...3
  1291. const int in = tid%8; // 0...7
  1292. const int is = tid/16; // 0 or 1
  1293. const uint8_t h = x[i].qh[in] >> im;
  1294. const float d = x[i].d;
  1295. dst_t * y = yy + i*QK_K + tid;
  1296. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1297. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1298. #endif
  1299. }
  1300. template<typename dst_t>
  1301. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1302. const block_q6_K * x = (const block_q6_K *) vx;
  1303. const int i = blockIdx.x;
  1304. #if QK_K == 256
  1305. // assume 64 threads - this is very slightly better than the one below
  1306. const int tid = threadIdx.x;
  1307. const int ip = tid/32; // ip is 0 or 1
  1308. const int il = tid - 32*ip; // 0...32
  1309. const int is = 8*ip + il/16;
  1310. dst_t * y = yy + i*QK_K + 128*ip + il;
  1311. const float d = x[i].d;
  1312. const uint8_t * ql = x[i].ql + 64*ip + il;
  1313. const uint8_t qh = x[i].qh[32*ip + il];
  1314. const int8_t * sc = x[i].scales + is;
  1315. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1316. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1317. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1318. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1319. #else
  1320. // assume 32 threads
  1321. const int tid = threadIdx.x;
  1322. const int ip = tid/16; // 0 or 1
  1323. const int il = tid - 16*ip; // 0...15
  1324. dst_t * y = yy + i*QK_K + 16*ip + il;
  1325. const float d = x[i].d;
  1326. const uint8_t ql = x[i].ql[16*ip + il];
  1327. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1328. const int8_t * sc = x[i].scales;
  1329. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1330. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1331. #endif
  1332. }
  1333. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1334. switch (type) {
  1335. case GGML_TYPE_Q4_0:
  1336. case GGML_TYPE_Q4_1:
  1337. case GGML_TYPE_Q5_0:
  1338. case GGML_TYPE_Q5_1:
  1339. case GGML_TYPE_Q8_0:
  1340. case GGML_TYPE_Q2_K:
  1341. case GGML_TYPE_Q3_K:
  1342. case GGML_TYPE_Q4_K:
  1343. case GGML_TYPE_Q5_K:
  1344. case GGML_TYPE_Q6_K:
  1345. return true;
  1346. default:
  1347. return false;
  1348. }
  1349. }
  1350. template<typename dst_t>
  1351. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1352. const int i = blockIdx.x;
  1353. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1354. const int tid = threadIdx.x;
  1355. #if QK_K == 256
  1356. const int il = tid/8; // 0...3
  1357. const int ib = tid%8; // 0...7
  1358. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1359. const uint16_t * q2 = x[i].qs + 4*ib;
  1360. const uint8_t * aux8 = (const uint8_t *)q2;
  1361. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  1362. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1363. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1364. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1365. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1366. #else
  1367. assert(false);
  1368. #endif
  1369. }
  1370. template<typename dst_t>
  1371. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1372. const int i = blockIdx.x;
  1373. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  1374. const int tid = threadIdx.x;
  1375. #if QK_K == 256
  1376. const int il = tid/8; // 0...3
  1377. const int ib = tid%8; // 0...7
  1378. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1379. const uint16_t * q2 = x[i].qs + 4*ib;
  1380. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  1381. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1382. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  1383. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1384. #else
  1385. assert(false);
  1386. #endif
  1387. }
  1388. template<typename dst_t>
  1389. static __global__ void dequantize_block_iq2_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1390. const int i = blockIdx.x;
  1391. const block_iq2_s * x = (const block_iq2_s *) vx;
  1392. const int tid = threadIdx.x;
  1393. #if QK_K == 256
  1394. const int il = tid/8; // 0...3
  1395. const int ib = tid%8; // 0...7
  1396. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1397. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  1398. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1399. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  1400. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1401. #else
  1402. assert(false);
  1403. #endif
  1404. }
  1405. template<typename dst_t>
  1406. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1407. const int i = blockIdx.x;
  1408. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  1409. const int tid = threadIdx.x;
  1410. #if QK_K == 256
  1411. const int il = tid/8; // 0...3
  1412. const int ib = tid%8; // 0...7
  1413. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1414. const uint8_t * q3 = x[i].qs + 8*ib;
  1415. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  1416. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  1417. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  1418. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  1419. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  1420. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1421. for (int j = 0; j < 4; ++j) {
  1422. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1423. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1424. }
  1425. #else
  1426. assert(false);
  1427. #endif
  1428. }
  1429. template<typename dst_t>
  1430. static __global__ void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1431. const int i = blockIdx.x;
  1432. const block_iq3_s * x = (const block_iq3_s *) vx;
  1433. const int tid = threadIdx.x;
  1434. #if QK_K == 256
  1435. const int il = tid/8; // 0...3
  1436. const int ib = tid%8; // 0...7
  1437. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1438. const uint8_t * qs = x[i].qs + 8*ib;
  1439. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  1440. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  1441. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  1442. const uint8_t signs = x[i].signs[4*ib + il];
  1443. for (int j = 0; j < 4; ++j) {
  1444. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1445. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1446. }
  1447. #else
  1448. assert(false);
  1449. #endif
  1450. }
  1451. template<typename dst_t>
  1452. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1453. const int i = blockIdx.x;
  1454. const block_iq1_s * x = (const block_iq1_s *) vx;
  1455. const int tid = threadIdx.x;
  1456. #if QK_K == 256
  1457. const int il = tid/8; // 0...3
  1458. const int ib = tid%8; // 0...7
  1459. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1460. const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
  1461. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
  1462. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  1463. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
  1464. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  1465. grid32[0] &= 0x0f0f0f0f;
  1466. for (int j = 0; j < 8; ++j) {
  1467. y[j] = d * (q[j] + delta);
  1468. }
  1469. #else
  1470. assert(false);
  1471. #endif
  1472. }
  1473. static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  1474. template<typename dst_t>
  1475. static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1476. const int i = blockIdx.x;
  1477. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  1478. const int tid = threadIdx.x;
  1479. const int il = tid/8; // 0...3
  1480. const int ib = tid%8; // 0...7
  1481. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  1482. const uint8_t * q4 = x[ib].qs + 4*il;
  1483. const float d = (float)x[ib].d;
  1484. for (int j = 0; j < 4; ++j) {
  1485. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  1486. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  1487. }
  1488. }
  1489. #if QK_K != 64
  1490. template<typename dst_t>
  1491. static __global__ void dequantize_block_iq4_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1492. const int i = blockIdx.x;
  1493. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  1494. const int tid = threadIdx.x;
  1495. const int il = tid/8; // 0...3
  1496. const int ib = tid%8; // 0...7
  1497. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  1498. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  1499. const float d = (float)x[i].d * ((((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4)) - 32);
  1500. for (int j = 0; j < 4; ++j) {
  1501. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  1502. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  1503. }
  1504. }
  1505. #endif
  1506. 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) {
  1507. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1508. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1509. if (row > nrows) return;
  1510. const int num_blocks_per_row = ncols / QK_K;
  1511. const int ib0 = row*num_blocks_per_row;
  1512. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1513. float tmp = 0; // partial sum for thread in warp
  1514. #if QK_K == 256
  1515. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1516. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1517. const int step = 16/K_QUANTS_PER_ITERATION;
  1518. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1519. const int in = tid - step*im; // 0...15 or 0...7
  1520. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1521. const int q_offset = 32*im + l0;
  1522. const int s_offset = 8*im;
  1523. const int y_offset = 128*im + l0;
  1524. uint32_t aux[4];
  1525. const uint8_t * d = (const uint8_t *)aux;
  1526. const uint8_t * m = (const uint8_t *)(aux + 2);
  1527. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1528. const float * y = yy + i * QK_K + y_offset;
  1529. const uint8_t * q = x[i].qs + q_offset;
  1530. const float dall = __low2half(x[i].dm);
  1531. const float dmin = __high2half(x[i].dm);
  1532. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1533. aux[0] = a[0] & 0x0f0f0f0f;
  1534. aux[1] = a[1] & 0x0f0f0f0f;
  1535. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1536. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1537. float sum1 = 0, sum2 = 0;
  1538. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1539. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1540. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1541. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1542. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1543. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1544. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1545. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1546. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1547. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1548. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1549. }
  1550. tmp += dall * sum1 - dmin * sum2;
  1551. }
  1552. #else
  1553. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1554. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1555. const int offset = tid * K_QUANTS_PER_ITERATION;
  1556. uint32_t uaux[2];
  1557. const uint8_t * d = (const uint8_t *)uaux;
  1558. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1559. const float * y = yy + i * QK_K + offset;
  1560. const uint8_t * q = x[i].qs + offset;
  1561. const uint32_t * s = (const uint32_t *)x[i].scales;
  1562. uaux[0] = s[0] & 0x0f0f0f0f;
  1563. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1564. const float2 dall = __half22float2(x[i].dm);
  1565. float sum1 = 0, sum2 = 0;
  1566. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1567. const uint8_t ql = q[l];
  1568. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1569. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1570. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1571. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1572. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1573. }
  1574. tmp += dall.x * sum1 - dall.y * sum2;
  1575. }
  1576. #endif
  1577. // sum up partial sums and write back result
  1578. tmp = warp_reduce_sum(tmp);
  1579. if (threadIdx.x == 0) {
  1580. dst[row] = tmp;
  1581. }
  1582. }
  1583. 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) {
  1584. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1585. if (row > nrows) return;
  1586. const int num_blocks_per_row = ncols / QK_K;
  1587. const int ib0 = row*num_blocks_per_row;
  1588. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1589. float tmp = 0; // partial sum for thread in warp
  1590. #if QK_K == 256
  1591. const uint16_t kmask1 = 0x0303;
  1592. const uint16_t kmask2 = 0x0f0f;
  1593. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1594. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1595. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1596. const int step = 16/K_QUANTS_PER_ITERATION;
  1597. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1598. const int in = tid - step*im; // 0....15 or 0...7
  1599. const uint8_t m = 1 << (4*im);
  1600. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1601. const int q_offset = 32*im + l0;
  1602. const int y_offset = 128*im + l0;
  1603. uint16_t utmp[4];
  1604. const int8_t * s = (const int8_t *)utmp;
  1605. const uint16_t s_shift = 4*im;
  1606. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1607. const float * y = yy + i * QK_K + y_offset;
  1608. const uint8_t * q = x[i].qs + q_offset;
  1609. const uint8_t * h = x[i].hmask + l0;
  1610. const uint16_t * a = (const uint16_t *)x[i].scales;
  1611. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1612. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1613. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1614. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1615. const float d = x[i].d;
  1616. float sum = 0;
  1617. for (int l = 0; l < n; ++l) {
  1618. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1619. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1620. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1621. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1622. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1623. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1624. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1625. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1626. }
  1627. tmp += d * sum;
  1628. }
  1629. #else
  1630. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1631. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1632. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1633. const int in = offset/8; // 0 or 1
  1634. const int im = offset%8; // 0...7
  1635. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1636. const float * y = yy + i * QK_K + offset;
  1637. const uint8_t * q = x[i].qs + offset;
  1638. const uint8_t * s = x[i].scales;
  1639. const float dall = (float)x[i].d;
  1640. float sum = 0;
  1641. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1642. const uint8_t hl = x[i].hmask[im+l] >> in;
  1643. const uint8_t ql = q[l];
  1644. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1645. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1646. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1647. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1648. }
  1649. tmp += sum;
  1650. }
  1651. #endif
  1652. // sum up partial sums and write back result
  1653. tmp = warp_reduce_sum(tmp);
  1654. if (threadIdx.x == 0) {
  1655. dst[row] = tmp;
  1656. }
  1657. }
  1658. 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) {
  1659. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1660. if (row > nrows) return;
  1661. const int num_blocks_per_row = ncols / QK_K;
  1662. const int ib0 = row*num_blocks_per_row;
  1663. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1664. #if QK_K == 256
  1665. const uint16_t kmask1 = 0x3f3f;
  1666. const uint16_t kmask2 = 0x0f0f;
  1667. const uint16_t kmask3 = 0xc0c0;
  1668. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1669. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1670. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1671. const int il = tid/step; // 0...3
  1672. const int ir = tid - step*il; // 0...7 or 0...3
  1673. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1674. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1675. const int in = il%2;
  1676. const int l0 = n*(2*ir + in);
  1677. const int q_offset = 32*im + l0;
  1678. const int y_offset = 64*im + l0;
  1679. uint16_t aux[4];
  1680. const uint8_t * sc = (const uint8_t *)aux;
  1681. #if K_QUANTS_PER_ITERATION == 2
  1682. uint32_t q32[4];
  1683. const uint8_t * q4 = (const uint8_t *)q32;
  1684. #else
  1685. uint16_t q16[4];
  1686. const uint8_t * q4 = (const uint8_t *)q16;
  1687. #endif
  1688. float tmp = 0; // partial sum for thread in warp
  1689. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1690. const float * y1 = yy + i*QK_K + y_offset;
  1691. const float * y2 = y1 + 128;
  1692. const float dall = __low2half(x[i].dm);
  1693. const float dmin = __high2half(x[i].dm);
  1694. const uint16_t * a = (const uint16_t *)x[i].scales;
  1695. aux[0] = a[im+0] & kmask1;
  1696. aux[1] = a[im+2] & kmask1;
  1697. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1698. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1699. #if K_QUANTS_PER_ITERATION == 2
  1700. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1701. const uint32_t * q2 = q1 + 16;
  1702. q32[0] = q1[0] & 0x0f0f0f0f;
  1703. q32[1] = q1[0] & 0xf0f0f0f0;
  1704. q32[2] = q2[0] & 0x0f0f0f0f;
  1705. q32[3] = q2[0] & 0xf0f0f0f0;
  1706. float4 s = {0.f, 0.f, 0.f, 0.f};
  1707. float smin = 0;
  1708. for (int l = 0; l < 4; ++l) {
  1709. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1710. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1711. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1712. }
  1713. 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;
  1714. #else
  1715. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1716. const uint16_t * q2 = q1 + 32;
  1717. q16[0] = q1[0] & 0x0f0f;
  1718. q16[1] = q1[0] & 0xf0f0;
  1719. q16[2] = q2[0] & 0x0f0f;
  1720. q16[3] = q2[0] & 0xf0f0;
  1721. float4 s = {0.f, 0.f, 0.f, 0.f};
  1722. float smin = 0;
  1723. for (int l = 0; l < 2; ++l) {
  1724. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1725. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1726. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1727. }
  1728. 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;
  1729. #endif
  1730. }
  1731. #else
  1732. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1733. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1734. const int step = tid * K_QUANTS_PER_ITERATION;
  1735. uint16_t aux16[2];
  1736. const uint8_t * s = (const uint8_t *)aux16;
  1737. float tmp = 0;
  1738. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1739. const uint8_t * q = x[i].qs + step;
  1740. const float * y = yy + i*QK_K + step;
  1741. const uint16_t * a = (const uint16_t *)x[i].scales;
  1742. aux16[0] = a[0] & 0x0f0f;
  1743. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1744. const float d = (float)x[i].dm[0];
  1745. const float m = (float)x[i].dm[1];
  1746. float sum = 0.f;
  1747. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1748. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1749. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1750. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1751. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1752. }
  1753. tmp += sum;
  1754. }
  1755. #endif
  1756. // sum up partial sums and write back result
  1757. tmp = warp_reduce_sum(tmp);
  1758. if (tid == 0) {
  1759. dst[row] = tmp;
  1760. }
  1761. }
  1762. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1763. const int row = blockIdx.x;
  1764. const int num_blocks_per_row = ncols / QK_K;
  1765. const int ib0 = row*num_blocks_per_row;
  1766. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1767. float tmp = 0; // partial sum for thread in warp
  1768. #if QK_K == 256
  1769. const uint16_t kmask1 = 0x3f3f;
  1770. const uint16_t kmask2 = 0x0f0f;
  1771. const uint16_t kmask3 = 0xc0c0;
  1772. const int tid = threadIdx.x/2; // 0...15
  1773. const int ix = threadIdx.x%2;
  1774. const int il = tid/4; // 0...3
  1775. const int ir = tid - 4*il;// 0...3
  1776. const int n = 2;
  1777. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1778. const int in = il%2;
  1779. const int l0 = n*(2*ir + in);
  1780. const int q_offset = 32*im + l0;
  1781. const int y_offset = 64*im + l0;
  1782. const uint8_t hm1 = 1 << (2*im);
  1783. const uint8_t hm2 = hm1 << 4;
  1784. uint16_t aux[4];
  1785. const uint8_t * sc = (const uint8_t *)aux;
  1786. uint16_t q16[8];
  1787. const uint8_t * q4 = (const uint8_t *)q16;
  1788. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1789. const uint8_t * ql1 = x[i].qs + q_offset;
  1790. const uint8_t * qh = x[i].qh + l0;
  1791. const float * y1 = yy + i*QK_K + y_offset;
  1792. const float * y2 = y1 + 128;
  1793. const float dall = __low2half(x[i].dm);
  1794. const float dmin = __high2half(x[i].dm);
  1795. const uint16_t * a = (const uint16_t *)x[i].scales;
  1796. aux[0] = a[im+0] & kmask1;
  1797. aux[1] = a[im+2] & kmask1;
  1798. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1799. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1800. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1801. float smin = 0;
  1802. const uint16_t * q1 = (const uint16_t *)ql1;
  1803. const uint16_t * q2 = q1 + 32;
  1804. q16[0] = q1[0] & 0x0f0f;
  1805. q16[1] = q1[8] & 0x0f0f;
  1806. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1807. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1808. q16[4] = q2[0] & 0x0f0f;
  1809. q16[5] = q2[8] & 0x0f0f;
  1810. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1811. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1812. for (int l = 0; l < n; ++l) {
  1813. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1814. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1815. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1816. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1817. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1818. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1819. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1820. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1821. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1822. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1823. }
  1824. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1825. }
  1826. #else
  1827. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1828. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1829. const int step = tid * K_QUANTS_PER_ITERATION;
  1830. const int im = step/8;
  1831. const int in = step%8;
  1832. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1833. const uint8_t * q = x[i].qs + step;
  1834. const int8_t * s = x[i].scales;
  1835. const float * y = yy + i*QK_K + step;
  1836. const float d = x[i].d;
  1837. float sum = 0.f;
  1838. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1839. const uint8_t h = x[i].qh[in+j] >> im;
  1840. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1841. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1842. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1843. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1844. }
  1845. tmp += sum;
  1846. }
  1847. #endif
  1848. // sum up partial sums and write back result
  1849. tmp = warp_reduce_sum(tmp);
  1850. if (threadIdx.x == 0) {
  1851. dst[row] = tmp;
  1852. }
  1853. }
  1854. 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) {
  1855. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1856. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1857. if (row > nrows) return;
  1858. const int num_blocks_per_row = ncols / QK_K;
  1859. const int ib0 = row*num_blocks_per_row;
  1860. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1861. #if QK_K == 256
  1862. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1863. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1864. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1865. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1866. const int in = tid - step*im; // 0...15 or 0...7
  1867. #if K_QUANTS_PER_ITERATION == 1
  1868. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1869. const int is = 0;
  1870. #else
  1871. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1872. const int is = in / 4;
  1873. #endif
  1874. const int ql_offset = 64*im + l0;
  1875. const int qh_offset = 32*im + l0;
  1876. const int s_offset = 8*im + is;
  1877. const int y_offset = 128*im + l0;
  1878. float tmp = 0; // partial sum for thread in warp
  1879. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1880. const float * y = yy + i * QK_K + y_offset;
  1881. const uint8_t * ql = x[i].ql + ql_offset;
  1882. const uint8_t * qh = x[i].qh + qh_offset;
  1883. const int8_t * s = x[i].scales + s_offset;
  1884. const float d = x[i].d;
  1885. #if K_QUANTS_PER_ITERATION == 1
  1886. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1887. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1888. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1889. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1890. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1891. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1892. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1893. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1894. tmp += sum;
  1895. #else
  1896. float sum = 0;
  1897. for (int l = 0; l < 4; ++l) {
  1898. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1899. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1900. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1901. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1902. }
  1903. tmp += sum;
  1904. #endif
  1905. }
  1906. #else
  1907. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1908. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1909. const int step = tid * K_QUANTS_PER_ITERATION;
  1910. float tmp = 0; // partial sum for thread in warp
  1911. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1912. const float * y = yy + i * QK_K + step;
  1913. const uint8_t * ql = x[i].ql + step;
  1914. const uint8_t * qh = x[i].qh + step;
  1915. const int8_t * s = x[i].scales;
  1916. const float d = x[i+0].d;
  1917. float sum = 0;
  1918. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1919. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1920. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1921. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1922. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1923. }
  1924. tmp += sum;
  1925. }
  1926. #endif
  1927. // sum up partial sums and write back result
  1928. tmp = warp_reduce_sum(tmp);
  1929. if (tid == 0) {
  1930. dst[row] = tmp;
  1931. }
  1932. }
  1933. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1934. const half * x = (const half *) vx;
  1935. // automatic half -> float type cast if dfloat == float
  1936. v.x = x[ib + iqs + 0];
  1937. v.y = x[ib + iqs + 1];
  1938. }
  1939. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1940. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1941. if (ix >= kx_padded) {
  1942. return;
  1943. }
  1944. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1945. const int i_padded = iy*kx_padded + ix;
  1946. block_q8_1 * y = (block_q8_1 *) vy;
  1947. const int ib = i_padded / QK8_1; // block index
  1948. const int iqs = i_padded % QK8_1; // quant index
  1949. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1950. float amax = fabsf(xi);
  1951. float sum = xi;
  1952. amax = warp_reduce_max(amax);
  1953. sum = warp_reduce_sum(sum);
  1954. const float d = amax / 127;
  1955. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1956. y[ib].qs[iqs] = q;
  1957. if (iqs > 0) {
  1958. return;
  1959. }
  1960. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1961. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1962. }
  1963. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1964. static __global__ void k_get_rows(
  1965. const void * src0, const int32_t * src1, dst_t * dst,
  1966. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1967. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1968. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1969. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1970. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1971. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1972. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1973. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1974. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1975. if (i00 >= ne00) {
  1976. return;
  1977. }
  1978. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1979. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1980. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1981. const int ib = i00/qk; // block index
  1982. const int iqs = (i00%qk)/qr; // quant index
  1983. const int iybs = i00 - i00%qk; // dst block start index
  1984. const int y_offset = qr == 1 ? 1 : qk/2;
  1985. // dequantize
  1986. dfloat2 v;
  1987. dequantize_kernel(src0_row, ib, iqs, v);
  1988. dst_row[iybs + iqs + 0] = v.x;
  1989. dst_row[iybs + iqs + y_offset] = v.y;
  1990. }
  1991. template<typename src0_t, typename dst_t>
  1992. static __global__ void k_get_rows_float(
  1993. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1994. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1995. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1996. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1997. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1998. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1999. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2000. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2001. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2002. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2003. if (i00 >= ne00) {
  2004. return;
  2005. }
  2006. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2007. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2008. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2009. dst_row[i00] = src0_row[i00];
  2010. }
  2011. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2012. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2013. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2014. if (i >= k) {
  2015. return;
  2016. }
  2017. const int ib = i/qk; // block index
  2018. const int iqs = (i%qk)/qr; // quant index
  2019. const int iybs = i - i%qk; // y block start index
  2020. const int y_offset = qr == 1 ? 1 : qk/2;
  2021. // dequantize
  2022. dfloat2 v;
  2023. dequantize_kernel(vx, ib, iqs, v);
  2024. y[iybs + iqs + 0] = v.x;
  2025. y[iybs + iqs + y_offset] = v.y;
  2026. }
  2027. template <typename src_t, typename dst_t>
  2028. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2029. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2030. if (i >= k) {
  2031. return;
  2032. }
  2033. const src_t * x = (src_t *) vx;
  2034. y[i] = x[i];
  2035. }
  2036. template <bool need_check>
  2037. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2038. #if __CUDA_ARCH__ >= CC_PASCAL
  2039. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2040. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2041. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2042. half2 * y2 = (half2 *) (y + i0);
  2043. __shared__ int vals[nint];
  2044. #pragma unroll
  2045. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2046. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2047. break;
  2048. }
  2049. const int ix = ix0 + threadIdx.x;
  2050. vals[ix] = x0[ix];
  2051. }
  2052. #pragma unroll
  2053. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2054. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2055. return;
  2056. }
  2057. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2058. const half d = *b0;
  2059. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2060. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2061. }
  2062. #else
  2063. (void) vx; (void) y; (void) k;
  2064. NO_DEVICE_CODE;
  2065. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2066. }
  2067. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2068. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2069. #define VDR_Q4_0_Q8_1_MMVQ 2
  2070. #define VDR_Q4_0_Q8_1_MMQ 4
  2071. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2072. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2073. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2074. int sumi = 0;
  2075. #pragma unroll
  2076. for (int i = 0; i < vdr; ++i) {
  2077. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2078. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2079. // SIMD dot product of quantized values
  2080. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2081. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2082. }
  2083. const float2 ds8f = __half22float2(ds8);
  2084. // second part effectively subtracts 8 from each quant value
  2085. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2086. #else
  2087. NO_DEVICE_CODE;
  2088. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2089. }
  2090. #define VDR_Q4_1_Q8_1_MMVQ 2
  2091. #define VDR_Q4_1_Q8_1_MMQ 4
  2092. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2093. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2094. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2095. int sumi = 0;
  2096. #pragma unroll
  2097. for (int i = 0; i < vdr; ++i) {
  2098. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2099. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2100. // SIMD dot product of quantized values
  2101. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2102. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2103. }
  2104. #ifdef GGML_CUDA_F16
  2105. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2106. const float d4d8 = tmp.x;
  2107. const float m4s8 = tmp.y;
  2108. #else
  2109. const float2 dm4f = __half22float2(dm4);
  2110. const float2 ds8f = __half22float2(ds8);
  2111. const float d4d8 = dm4f.x * ds8f.x;
  2112. const float m4s8 = dm4f.y * ds8f.y;
  2113. #endif // GGML_CUDA_F16
  2114. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2115. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2116. #else
  2117. NO_DEVICE_CODE;
  2118. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2119. }
  2120. #define VDR_Q5_0_Q8_1_MMVQ 2
  2121. #define VDR_Q5_0_Q8_1_MMQ 4
  2122. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2123. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2124. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2125. int sumi = 0;
  2126. #pragma unroll
  2127. for (int i = 0; i < vdr; ++i) {
  2128. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2129. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2130. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2131. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2132. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2133. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2134. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2135. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2136. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2137. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2138. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2139. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2140. }
  2141. const float2 ds8f = __half22float2(ds8);
  2142. // second part effectively subtracts 16 from each quant value
  2143. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2144. #else
  2145. NO_DEVICE_CODE;
  2146. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2147. }
  2148. #define VDR_Q5_1_Q8_1_MMVQ 2
  2149. #define VDR_Q5_1_Q8_1_MMQ 4
  2150. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2151. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2152. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2153. int sumi = 0;
  2154. #pragma unroll
  2155. for (int i = 0; i < vdr; ++i) {
  2156. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2157. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2158. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2159. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2160. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2161. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2162. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2163. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2164. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2165. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2166. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2167. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2168. }
  2169. #ifdef GGML_CUDA_F16
  2170. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2171. const float d5d8 = tmp.x;
  2172. const float m5s8 = tmp.y;
  2173. #else
  2174. const float2 dm5f = __half22float2(dm5);
  2175. const float2 ds8f = __half22float2(ds8);
  2176. const float d5d8 = dm5f.x * ds8f.x;
  2177. const float m5s8 = dm5f.y * ds8f.y;
  2178. #endif // GGML_CUDA_F16
  2179. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2180. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2181. #else
  2182. NO_DEVICE_CODE;
  2183. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2184. }
  2185. #define VDR_Q8_0_Q8_1_MMVQ 2
  2186. #define VDR_Q8_0_Q8_1_MMQ 8
  2187. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2188. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2189. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2190. int sumi = 0;
  2191. #pragma unroll
  2192. for (int i = 0; i < vdr; ++i) {
  2193. // SIMD dot product of quantized values
  2194. sumi = __dp4a(v[i], u[i], sumi);
  2195. }
  2196. return d8_0*d8_1 * sumi;
  2197. #else
  2198. NO_DEVICE_CODE;
  2199. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2200. }
  2201. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2202. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2203. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2204. int sumi = 0;
  2205. #pragma unroll
  2206. for (int i = 0; i < vdr; ++i) {
  2207. // SIMD dot product of quantized values
  2208. sumi = __dp4a(v[i], u[i], sumi);
  2209. }
  2210. #ifdef GGML_CUDA_F16
  2211. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2212. const float d8d8 = tmp.x;
  2213. const float m8s8 = tmp.y;
  2214. #else
  2215. const float2 dm8f = __half22float2(dm8);
  2216. const float2 ds8f = __half22float2(ds8);
  2217. const float d8d8 = dm8f.x * ds8f.x;
  2218. const float m8s8 = dm8f.y * ds8f.y;
  2219. #endif // GGML_CUDA_F16
  2220. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2221. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2222. #else
  2223. NO_DEVICE_CODE;
  2224. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2225. }
  2226. #define VDR_Q2_K_Q8_1_MMVQ 1
  2227. #define VDR_Q2_K_Q8_1_MMQ 2
  2228. // contiguous v/x values
  2229. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2230. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2231. const half2 & dm2, const float * __restrict__ d8) {
  2232. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2233. float sumf_d = 0.0f;
  2234. float sumf_m = 0.0f;
  2235. #pragma unroll
  2236. for (int i = 0; i < QR2_K; ++i) {
  2237. const int sc = scales[2*i];
  2238. const int vi = (v >> (2*i)) & 0x03030303;
  2239. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2240. // fill int with 4x m
  2241. int m = sc >> 4;
  2242. m |= m << 8;
  2243. m |= m << 16;
  2244. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2245. }
  2246. const float2 dm2f = __half22float2(dm2);
  2247. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2248. #else
  2249. NO_DEVICE_CODE;
  2250. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2251. }
  2252. // contiguous u/y values
  2253. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2254. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2255. const half2 & dm2, const float & d8) {
  2256. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2257. int sumi_d = 0;
  2258. int sumi_m = 0;
  2259. #pragma unroll
  2260. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2261. int sumi_d_sc = 0;
  2262. const int sc = scales[i0 / (QI8_1/2)];
  2263. // fill int with 4x m
  2264. int m = sc >> 4;
  2265. m |= m << 8;
  2266. m |= m << 16;
  2267. #pragma unroll
  2268. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2269. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2270. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2271. }
  2272. sumi_d += sumi_d_sc * (sc & 0xF);
  2273. }
  2274. const float2 dm2f = __half22float2(dm2);
  2275. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2276. #else
  2277. NO_DEVICE_CODE;
  2278. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2279. }
  2280. #define VDR_Q3_K_Q8_1_MMVQ 1
  2281. #define VDR_Q3_K_Q8_1_MMQ 2
  2282. // contiguous v/x values
  2283. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2284. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2285. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2286. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2287. float sumf = 0.0f;
  2288. #pragma unroll
  2289. for (int i = 0; i < QR3_K; ++i) {
  2290. const int isc = scale_offset + 2*i;
  2291. const int isc_low = isc % (QK_K/32);
  2292. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2293. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2294. const int isc_high = isc % (QK_K/64);
  2295. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2296. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2297. const int sc = (sc_low | sc_high) - 32;
  2298. const int vil = (vl >> (2*i)) & 0x03030303;
  2299. const int vih = ((vh >> i) << 2) & 0x04040404;
  2300. const int vi = __vsubss4(vil, vih);
  2301. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2302. }
  2303. return d3 * sumf;
  2304. #else
  2305. NO_DEVICE_CODE;
  2306. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2307. }
  2308. // contiguous u/y values
  2309. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2310. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2311. const float & d3, const float & d8) {
  2312. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2313. int sumi = 0;
  2314. #pragma unroll
  2315. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2316. int sumi_sc = 0;
  2317. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2318. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2319. }
  2320. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2321. }
  2322. return d3*d8 * sumi;
  2323. #else
  2324. NO_DEVICE_CODE;
  2325. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2326. }
  2327. #define VDR_Q4_K_Q8_1_MMVQ 2
  2328. #define VDR_Q4_K_Q8_1_MMQ 8
  2329. // contiguous v/x values
  2330. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2331. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2332. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2333. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2334. float sumf_d = 0.0f;
  2335. float sumf_m = 0.0f;
  2336. #pragma unroll
  2337. for (int i = 0; i < QR4_K; ++i) {
  2338. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2339. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2340. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2341. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2342. sumf_d += d8[i] * (dot1 * sc[i]);
  2343. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2344. }
  2345. const float2 dm4f = __half22float2(dm4);
  2346. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2347. #else
  2348. NO_DEVICE_CODE;
  2349. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2350. }
  2351. // contiguous u/y values
  2352. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2353. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2354. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2355. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2356. float sumf_d = 0.0f;
  2357. float sumf_m = 0.0f;
  2358. #pragma unroll
  2359. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2360. int sumi_d = 0;
  2361. #pragma unroll
  2362. for (int j = 0; j < QI8_1; ++j) {
  2363. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2364. }
  2365. const float2 ds8f = __half22float2(ds8[i]);
  2366. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2367. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2368. }
  2369. const float2 dm4f = __half22float2(dm4);
  2370. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2371. #else
  2372. NO_DEVICE_CODE;
  2373. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2374. }
  2375. #define VDR_Q5_K_Q8_1_MMVQ 2
  2376. #define VDR_Q5_K_Q8_1_MMQ 8
  2377. // contiguous v/x values
  2378. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2379. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2380. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2381. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2382. float sumf_d = 0.0f;
  2383. float sumf_m = 0.0f;
  2384. #pragma unroll
  2385. for (int i = 0; i < QR5_K; ++i) {
  2386. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2387. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2388. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2389. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2390. const int v0i = vl0i | vh0i;
  2391. const int v1i = vl1i | vh1i;
  2392. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2393. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2394. sumf_d += d8[i] * (dot1 * sc[i]);
  2395. sumf_m += d8[i] * (dot2 * m[i]);
  2396. }
  2397. const float2 dm5f = __half22float2(dm5);
  2398. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2399. #else
  2400. NO_DEVICE_CODE;
  2401. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2402. }
  2403. // contiguous u/y values
  2404. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2405. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2406. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2407. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2408. float sumf_d = 0.0f;
  2409. float sumf_m = 0.0f;
  2410. #pragma unroll
  2411. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2412. int sumi_d = 0;
  2413. #pragma unroll
  2414. for (int j = 0; j < QI8_1; ++j) {
  2415. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2416. }
  2417. const float2 ds8f = __half22float2(ds8[i]);
  2418. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2419. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2420. }
  2421. const float2 dm4f = __half22float2(dm4);
  2422. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2423. #else
  2424. NO_DEVICE_CODE;
  2425. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2426. }
  2427. #define VDR_Q6_K_Q8_1_MMVQ 1
  2428. #define VDR_Q6_K_Q8_1_MMQ 8
  2429. // contiguous v/x values
  2430. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2431. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2432. const float & d, const float * __restrict__ d8) {
  2433. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2434. float sumf = 0.0f;
  2435. #pragma unroll
  2436. for (int i = 0; i < QR6_K; ++i) {
  2437. const int sc = scales[4*i];
  2438. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2439. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2440. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2441. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2442. }
  2443. return d*sumf;
  2444. #else
  2445. NO_DEVICE_CODE;
  2446. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2447. }
  2448. // contiguous u/y values
  2449. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2450. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2451. const float & d6, const float * __restrict__ d8) {
  2452. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2453. float sumf_d = 0.0f;
  2454. #pragma unroll
  2455. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2456. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2457. #pragma unroll
  2458. for (int i = i0; i < i0 + 2; ++i) {
  2459. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2460. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2461. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2462. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2463. }
  2464. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2465. }
  2466. return d6 * sumf_d;
  2467. #else
  2468. NO_DEVICE_CODE;
  2469. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2470. }
  2471. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2472. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2473. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2474. int v[VDR_Q4_0_Q8_1_MMVQ];
  2475. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2476. #pragma unroll
  2477. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2478. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2479. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2480. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2481. }
  2482. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2483. }
  2484. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2485. (void)x_qh; (void)x_sc;
  2486. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2487. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2488. *x_ql = tile_x_qs;
  2489. *x_dm = (half2 *) tile_x_d;
  2490. }
  2491. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2492. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2493. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2494. (void)x_qh; (void)x_sc;
  2495. GGML_CUDA_ASSUME(i_offset >= 0);
  2496. GGML_CUDA_ASSUME(i_offset < nwarps);
  2497. GGML_CUDA_ASSUME(k >= 0);
  2498. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2499. const int kbx = k / QI4_0;
  2500. const int kqsx = k % QI4_0;
  2501. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2502. float * x_dmf = (float *) x_dm;
  2503. #pragma unroll
  2504. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2505. int i = i0 + i_offset;
  2506. if (need_check) {
  2507. i = min(i, i_max);
  2508. }
  2509. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2510. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2511. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2512. }
  2513. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2514. const int kbxd = k % blocks_per_tile_x_row;
  2515. #pragma unroll
  2516. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2517. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2518. if (need_check) {
  2519. i = min(i, i_max);
  2520. }
  2521. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2522. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2523. }
  2524. }
  2525. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2526. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2527. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2528. (void)x_qh; (void)x_sc;
  2529. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2530. const float * x_dmf = (const float *) x_dm;
  2531. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2532. #pragma unroll
  2533. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2534. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2535. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2536. }
  2537. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2538. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2539. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2540. }
  2541. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2542. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2543. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2544. int v[VDR_Q4_1_Q8_1_MMVQ];
  2545. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2546. #pragma unroll
  2547. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2548. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2549. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2550. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2551. }
  2552. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2553. }
  2554. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2555. (void)x_qh; (void)x_sc;
  2556. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2557. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2558. *x_ql = tile_x_qs;
  2559. *x_dm = tile_x_dm;
  2560. }
  2561. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2562. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2563. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2564. (void)x_qh; (void)x_sc;
  2565. GGML_CUDA_ASSUME(i_offset >= 0);
  2566. GGML_CUDA_ASSUME(i_offset < nwarps);
  2567. GGML_CUDA_ASSUME(k >= 0);
  2568. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2569. const int kbx = k / QI4_1;
  2570. const int kqsx = k % QI4_1;
  2571. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2572. #pragma unroll
  2573. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2574. int i = i0 + i_offset;
  2575. if (need_check) {
  2576. i = min(i, i_max);
  2577. }
  2578. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2579. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2580. }
  2581. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2582. const int kbxd = k % blocks_per_tile_x_row;
  2583. #pragma unroll
  2584. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2585. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2586. if (need_check) {
  2587. i = min(i, i_max);
  2588. }
  2589. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2590. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2591. }
  2592. }
  2593. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2594. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2595. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2596. (void)x_qh; (void)x_sc;
  2597. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2598. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2599. #pragma unroll
  2600. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2601. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2602. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2603. }
  2604. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2605. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2606. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2607. }
  2608. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2609. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2610. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2611. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2612. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2613. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2614. #pragma unroll
  2615. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2616. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2617. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2618. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2619. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2620. }
  2621. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2622. }
  2623. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2624. (void)x_qh; (void)x_sc;
  2625. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2626. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2627. *x_ql = tile_x_ql;
  2628. *x_dm = (half2 *) tile_x_d;
  2629. }
  2630. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2631. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2632. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2633. (void)x_qh; (void)x_sc;
  2634. GGML_CUDA_ASSUME(i_offset >= 0);
  2635. GGML_CUDA_ASSUME(i_offset < nwarps);
  2636. GGML_CUDA_ASSUME(k >= 0);
  2637. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2638. const int kbx = k / QI5_0;
  2639. const int kqsx = k % QI5_0;
  2640. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2641. #pragma unroll
  2642. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2643. int i = i0 + i_offset;
  2644. if (need_check) {
  2645. i = min(i, i_max);
  2646. }
  2647. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2648. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2649. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2650. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2651. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2652. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2653. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2654. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2655. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2656. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2657. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2658. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2659. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2660. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2661. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2662. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2663. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2664. }
  2665. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2666. const int kbxd = k % blocks_per_tile_x_row;
  2667. float * x_dmf = (float *) x_dm;
  2668. #pragma unroll
  2669. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2670. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2671. if (need_check) {
  2672. i = min(i, i_max);
  2673. }
  2674. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2675. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2676. }
  2677. }
  2678. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2679. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2680. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2681. (void)x_qh; (void)x_sc;
  2682. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2683. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2684. const float * x_dmf = (const float *) x_dm;
  2685. const float * y_df = (const float *) y_ds;
  2686. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2687. #pragma unroll
  2688. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2689. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2690. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2691. }
  2692. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2693. (&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)]);
  2694. }
  2695. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2696. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2697. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2698. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2699. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2700. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2701. #pragma unroll
  2702. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2703. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2704. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2705. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2706. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2707. }
  2708. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2709. }
  2710. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2711. (void)x_qh; (void)x_sc;
  2712. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2713. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2714. *x_ql = tile_x_ql;
  2715. *x_dm = tile_x_dm;
  2716. }
  2717. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2718. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2719. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2720. (void)x_qh; (void)x_sc;
  2721. GGML_CUDA_ASSUME(i_offset >= 0);
  2722. GGML_CUDA_ASSUME(i_offset < nwarps);
  2723. GGML_CUDA_ASSUME(k >= 0);
  2724. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2725. const int kbx = k / QI5_1;
  2726. const int kqsx = k % QI5_1;
  2727. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2728. #pragma unroll
  2729. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2730. int i = i0 + i_offset;
  2731. if (need_check) {
  2732. i = min(i, i_max);
  2733. }
  2734. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2735. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2736. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2737. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2738. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2739. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2740. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2741. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2742. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2743. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2744. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2745. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2746. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2747. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2748. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2749. }
  2750. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2751. const int kbxd = k % blocks_per_tile_x_row;
  2752. #pragma unroll
  2753. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2754. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2755. if (need_check) {
  2756. i = min(i, i_max);
  2757. }
  2758. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2759. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2760. }
  2761. }
  2762. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2763. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2764. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2765. (void)x_qh; (void)x_sc;
  2766. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2767. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2768. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2769. #pragma unroll
  2770. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2771. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2772. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2773. }
  2774. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2775. (&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)]);
  2776. }
  2777. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2778. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2779. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2780. int v[VDR_Q8_0_Q8_1_MMVQ];
  2781. int u[VDR_Q8_0_Q8_1_MMVQ];
  2782. #pragma unroll
  2783. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2784. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2785. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2786. }
  2787. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2788. }
  2789. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2790. (void)x_qh; (void)x_sc;
  2791. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2792. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2793. *x_ql = tile_x_qs;
  2794. *x_dm = (half2 *) tile_x_d;
  2795. }
  2796. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2797. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2798. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2799. (void)x_qh; (void)x_sc;
  2800. GGML_CUDA_ASSUME(i_offset >= 0);
  2801. GGML_CUDA_ASSUME(i_offset < nwarps);
  2802. GGML_CUDA_ASSUME(k >= 0);
  2803. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2804. const int kbx = k / QI8_0;
  2805. const int kqsx = k % QI8_0;
  2806. float * x_dmf = (float *) x_dm;
  2807. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2808. #pragma unroll
  2809. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2810. int i = i0 + i_offset;
  2811. if (need_check) {
  2812. i = min(i, i_max);
  2813. }
  2814. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2815. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2816. }
  2817. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2818. const int kbxd = k % blocks_per_tile_x_row;
  2819. #pragma unroll
  2820. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2821. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2822. if (need_check) {
  2823. i = min(i, i_max);
  2824. }
  2825. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2826. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2827. }
  2828. }
  2829. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2830. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2831. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2832. (void)x_qh; (void)x_sc;
  2833. const float * x_dmf = (const float *) x_dm;
  2834. const float * y_df = (const float *) y_ds;
  2835. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2836. (&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],
  2837. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2838. }
  2839. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2840. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2841. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2842. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2843. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2844. const uint8_t * scales = bq2_K->scales + scale_offset;
  2845. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2846. int u[QR2_K];
  2847. float d8[QR2_K];
  2848. #pragma unroll
  2849. for (int i = 0; i < QR2_K; ++ i) {
  2850. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2851. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2852. }
  2853. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2854. }
  2855. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2856. (void)x_qh;
  2857. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2858. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2859. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2860. *x_ql = tile_x_ql;
  2861. *x_dm = tile_x_dm;
  2862. *x_sc = tile_x_sc;
  2863. }
  2864. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2865. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2866. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2867. (void)x_qh;
  2868. GGML_CUDA_ASSUME(i_offset >= 0);
  2869. GGML_CUDA_ASSUME(i_offset < nwarps);
  2870. GGML_CUDA_ASSUME(k >= 0);
  2871. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2872. const int kbx = k / QI2_K;
  2873. const int kqsx = k % QI2_K;
  2874. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2875. #pragma unroll
  2876. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2877. int i = i0 + i_offset;
  2878. if (need_check) {
  2879. i = min(i, i_max);
  2880. }
  2881. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2882. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2883. }
  2884. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2885. const int kbxd = k % blocks_per_tile_x_row;
  2886. #pragma unroll
  2887. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2888. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2889. if (need_check) {
  2890. i = min(i, i_max);
  2891. }
  2892. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2893. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2894. }
  2895. #pragma unroll
  2896. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2897. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2898. if (need_check) {
  2899. i = min(i, i_max);
  2900. }
  2901. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2902. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2903. }
  2904. }
  2905. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2906. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2907. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2908. (void)x_qh;
  2909. const int kbx = k / QI2_K;
  2910. const int ky = (k % QI2_K) * QR2_K;
  2911. const float * y_df = (const float *) y_ds;
  2912. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2913. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2914. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2915. #pragma unroll
  2916. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2917. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2918. }
  2919. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2920. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2921. 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]);
  2922. }
  2923. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2924. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2925. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2926. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2927. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2928. const float d = bq3_K->d;
  2929. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2930. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2931. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2932. int u[QR3_K];
  2933. float d8[QR3_K];
  2934. #pragma unroll
  2935. for (int i = 0; i < QR3_K; ++i) {
  2936. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2937. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2938. }
  2939. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2940. }
  2941. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2942. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2943. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2944. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2945. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2946. *x_ql = tile_x_ql;
  2947. *x_dm = tile_x_dm;
  2948. *x_qh = tile_x_qh;
  2949. *x_sc = tile_x_sc;
  2950. }
  2951. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2952. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2953. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2954. GGML_CUDA_ASSUME(i_offset >= 0);
  2955. GGML_CUDA_ASSUME(i_offset < nwarps);
  2956. GGML_CUDA_ASSUME(k >= 0);
  2957. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2958. const int kbx = k / QI3_K;
  2959. const int kqsx = k % QI3_K;
  2960. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2961. #pragma unroll
  2962. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2963. int i = i0 + i_offset;
  2964. if (need_check) {
  2965. i = min(i, i_max);
  2966. }
  2967. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2968. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2969. }
  2970. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2971. const int kbxd = k % blocks_per_tile_x_row;
  2972. float * x_dmf = (float *) x_dm;
  2973. #pragma unroll
  2974. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2975. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2976. if (need_check) {
  2977. i = min(i, i_max);
  2978. }
  2979. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2980. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2981. }
  2982. #pragma unroll
  2983. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2984. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2985. if (need_check) {
  2986. i = min(i, i_max);
  2987. }
  2988. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2989. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2990. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2991. }
  2992. #pragma unroll
  2993. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2994. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2995. if (need_check) {
  2996. i = min(i, i_max);
  2997. }
  2998. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2999. const int ksc = k % (QI3_K/4);
  3000. const int ksc_low = ksc % (QI3_K/8);
  3001. const int shift_low = 4 * (ksc / (QI3_K/8));
  3002. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3003. const int ksc_high = QI3_K/8;
  3004. const int shift_high = 2 * ksc;
  3005. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3006. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3007. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3008. }
  3009. }
  3010. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3011. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3012. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3013. const int kbx = k / QI3_K;
  3014. const int ky = (k % QI3_K) * QR3_K;
  3015. const float * x_dmf = (const float *) x_dm;
  3016. const float * y_df = (const float *) y_ds;
  3017. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3018. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3019. #pragma unroll
  3020. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3021. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3022. const int shift = 2 * ((ky % 32) / 8);
  3023. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3024. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3025. const int vlh = (vh << 2) & 0x04040404;
  3026. v[l] = __vsubss4(vll, vlh);
  3027. }
  3028. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3029. 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]);
  3030. }
  3031. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3032. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3033. #ifndef GGML_QKK_64
  3034. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3035. int v[2];
  3036. int u[2*QR4_K];
  3037. float d8[QR4_K];
  3038. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3039. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3040. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3041. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3042. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3043. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3044. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3045. v[0] = q4[0];
  3046. v[1] = q4[4];
  3047. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3048. uint16_t aux[2];
  3049. const int j = bq8_offset/2;
  3050. if (j < 2) {
  3051. aux[0] = scales[j+0] & 0x3f3f;
  3052. aux[1] = scales[j+2] & 0x3f3f;
  3053. } else {
  3054. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3055. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3056. }
  3057. const uint8_t * sc = (const uint8_t *)aux;
  3058. const uint8_t * m = sc + 2;
  3059. for (int i = 0; i < QR4_K; ++i) {
  3060. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3061. d8[i] = __low2half(bq8i->ds);
  3062. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3063. u[2*i+0] = q8[0];
  3064. u[2*i+1] = q8[4];
  3065. }
  3066. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3067. #else
  3068. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3069. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3070. float sumf_d = 0.0f;
  3071. float sumf_m = 0.0f;
  3072. uint16_t aux16[2];
  3073. const uint8_t * s = (const uint8_t *)aux16;
  3074. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3075. aux16[0] = a[0] & 0x0f0f;
  3076. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3077. const float dall = bq4_K->dm[0];
  3078. const float dmin = bq4_K->dm[1];
  3079. const float d8_1 = __low2float(bq8_1[0].ds);
  3080. const float d8_2 = __low2float(bq8_1[1].ds);
  3081. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3082. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3083. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3084. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3085. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3086. const int v1 = q4[0];
  3087. const int v2 = q4[4];
  3088. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3089. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3090. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3091. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3092. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3093. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3094. return dall * sumf_d - dmin * sumf_m;
  3095. #else
  3096. NO_DEVICE_CODE;
  3097. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3098. #endif
  3099. }
  3100. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3101. (void)x_qh;
  3102. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3103. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3104. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3105. *x_ql = tile_x_ql;
  3106. *x_dm = tile_x_dm;
  3107. *x_sc = tile_x_sc;
  3108. }
  3109. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3110. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3111. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3112. (void)x_qh;
  3113. GGML_CUDA_ASSUME(i_offset >= 0);
  3114. GGML_CUDA_ASSUME(i_offset < nwarps);
  3115. GGML_CUDA_ASSUME(k >= 0);
  3116. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3117. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3118. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3119. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3120. #pragma unroll
  3121. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3122. int i = i0 + i_offset;
  3123. if (need_check) {
  3124. i = min(i, i_max);
  3125. }
  3126. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3127. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3128. }
  3129. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3130. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3131. #pragma unroll
  3132. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3133. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3134. if (need_check) {
  3135. i = min(i, i_max);
  3136. }
  3137. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3138. #if QK_K == 256
  3139. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3140. #else
  3141. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3142. #endif
  3143. }
  3144. #pragma unroll
  3145. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3146. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3147. if (need_check) {
  3148. i = min(i, i_max);
  3149. }
  3150. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3151. const int * scales = (const int *) bxi->scales;
  3152. const int ksc = k % (WARP_SIZE/8);
  3153. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3154. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3155. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3156. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3157. }
  3158. }
  3159. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3160. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3161. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3162. (void)x_qh;
  3163. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3164. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3165. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3166. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3167. }
  3168. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3169. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3170. #ifndef GGML_QKK_64
  3171. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3172. int vl[2];
  3173. int vh[2];
  3174. int u[2*QR5_K];
  3175. float d8[QR5_K];
  3176. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3177. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3178. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3179. vl[0] = ql[0];
  3180. vl[1] = ql[4];
  3181. vh[0] = qh[0] >> bq8_offset;
  3182. vh[1] = qh[4] >> bq8_offset;
  3183. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3184. uint16_t aux[2];
  3185. const int j = bq8_offset/2;
  3186. if (j < 2) {
  3187. aux[0] = scales[j+0] & 0x3f3f;
  3188. aux[1] = scales[j+2] & 0x3f3f;
  3189. } else {
  3190. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3191. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3192. }
  3193. const uint8_t * sc = (const uint8_t *)aux;
  3194. const uint8_t * m = sc + 2;
  3195. #pragma unroll
  3196. for (int i = 0; i < QR5_K; ++i) {
  3197. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3198. d8[i] = __low2float(bq8i->ds);
  3199. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3200. u[2*i+0] = q8[0];
  3201. u[2*i+1] = q8[4];
  3202. }
  3203. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3204. #else
  3205. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3206. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3207. const int8_t * s = bq5_K->scales;
  3208. const float d = bq5_K->d;
  3209. const float d8_1 = __low2half(bq8_1[0].ds);
  3210. const float d8_2 = __low2half(bq8_1[1].ds);
  3211. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3212. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3213. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3214. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3215. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3216. const int vl1 = ql[0];
  3217. const int vl2 = ql[4];
  3218. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3219. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3220. const int in = step%8; // 0, 4, 0, 4
  3221. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3222. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3223. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3224. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3225. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3226. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3227. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3228. return d * sumf_d;
  3229. #else
  3230. NO_DEVICE_CODE;
  3231. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3232. #endif
  3233. }
  3234. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3235. (void)x_qh;
  3236. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3237. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3238. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3239. *x_ql = tile_x_ql;
  3240. *x_dm = tile_x_dm;
  3241. *x_sc = tile_x_sc;
  3242. }
  3243. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3244. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3245. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3246. (void)x_qh;
  3247. GGML_CUDA_ASSUME(i_offset >= 0);
  3248. GGML_CUDA_ASSUME(i_offset < nwarps);
  3249. GGML_CUDA_ASSUME(k >= 0);
  3250. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3251. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3252. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3253. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3254. #pragma unroll
  3255. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3256. int i = i0 + i_offset;
  3257. if (need_check) {
  3258. i = min(i, i_max);
  3259. }
  3260. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3261. const int ky = QR5_K*kqsx;
  3262. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3263. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3264. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3265. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3266. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3267. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3268. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3269. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3270. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3271. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3272. }
  3273. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3274. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3275. #pragma unroll
  3276. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3277. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3278. if (need_check) {
  3279. i = min(i, i_max);
  3280. }
  3281. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3282. #if QK_K == 256
  3283. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3284. #endif
  3285. }
  3286. #pragma unroll
  3287. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3288. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3289. if (need_check) {
  3290. i = min(i, i_max);
  3291. }
  3292. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3293. const int * scales = (const int *) bxi->scales;
  3294. const int ksc = k % (WARP_SIZE/8);
  3295. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3296. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3297. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3298. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3299. }
  3300. }
  3301. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3302. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3303. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3304. (void)x_qh;
  3305. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3306. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3307. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3308. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3309. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3310. }
  3311. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3312. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3313. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3314. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3315. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3316. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3317. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3318. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3319. const int8_t * scales = bq6_K->scales + scale_offset;
  3320. int u[QR6_K];
  3321. float d8[QR6_K];
  3322. #pragma unroll
  3323. for (int i = 0; i < QR6_K; ++i) {
  3324. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3325. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3326. }
  3327. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3328. }
  3329. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3330. (void)x_qh;
  3331. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3332. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3333. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3334. *x_ql = tile_x_ql;
  3335. *x_dm = tile_x_dm;
  3336. *x_sc = tile_x_sc;
  3337. }
  3338. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3339. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3340. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3341. (void)x_qh;
  3342. GGML_CUDA_ASSUME(i_offset >= 0);
  3343. GGML_CUDA_ASSUME(i_offset < nwarps);
  3344. GGML_CUDA_ASSUME(k >= 0);
  3345. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3346. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3347. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3348. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3349. #pragma unroll
  3350. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3351. int i = i0 + i_offset;
  3352. if (need_check) {
  3353. i = min(i, i_max);
  3354. }
  3355. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3356. const int ky = QR6_K*kqsx;
  3357. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3358. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3359. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3360. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3361. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3362. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3363. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3364. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3365. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3366. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3367. }
  3368. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3369. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3370. float * x_dmf = (float *) x_dm;
  3371. #pragma unroll
  3372. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3373. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3374. if (need_check) {
  3375. i = min(i, i_max);
  3376. }
  3377. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3378. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3379. }
  3380. #pragma unroll
  3381. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3382. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3383. if (need_check) {
  3384. i = min(i, i_max);
  3385. }
  3386. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3387. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3388. }
  3389. }
  3390. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3391. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3392. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3393. (void)x_qh;
  3394. const float * x_dmf = (const float *) x_dm;
  3395. const float * y_df = (const float *) y_ds;
  3396. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3397. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3398. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3399. 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]);
  3400. }
  3401. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3402. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3403. #if QK_K == 256
  3404. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3405. #if QR2_XXS == 8
  3406. const int ib32 = iqs;
  3407. const uint16_t * q2 = bq2->qs + 4*ib32;
  3408. const uint8_t * aux8 = (const uint8_t *)q2;
  3409. const int8_t * q8 = bq8_1[ib32].qs;
  3410. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3411. int sumi = 0;
  3412. for (int l = 0; l < 4; ++l) {
  3413. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3414. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3415. for (int j = 0; j < 8; ++j) {
  3416. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3417. }
  3418. q8 += 8;
  3419. aux32 >>= 7;
  3420. }
  3421. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3422. return d * sumi;
  3423. #else
  3424. // iqs is 0...15
  3425. const int ib32 = iqs/2;
  3426. const int il = iqs%2;
  3427. const uint16_t * q2 = bq2->qs + 4*ib32;
  3428. const uint8_t * aux8 = (const uint8_t *)q2;
  3429. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  3430. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  3431. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3432. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3433. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3434. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3435. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3436. int sumi1 = 0, sumi2 = 0;
  3437. for (int j = 0; j < 8; ++j) {
  3438. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3439. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3440. }
  3441. return d * (sumi1 + sumi2);
  3442. #endif
  3443. #else
  3444. assert(false);
  3445. return 0.f;
  3446. #endif
  3447. }
  3448. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  3449. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3450. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3451. #if QK_K == 256
  3452. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  3453. const int ib32 = iqs;
  3454. const uint16_t * q2 = bq2->qs + 4*ib32;
  3455. const int8_t * q8 = bq8_1[ib32].qs;
  3456. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3457. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3458. int sumi1 = 0;
  3459. for (int l = 0; l < 2; ++l) {
  3460. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3461. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3462. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3463. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3464. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3465. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3466. q8 += 8;
  3467. }
  3468. int sumi2 = 0;
  3469. for (int l = 2; l < 4; ++l) {
  3470. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3471. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3472. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3473. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3474. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3475. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3476. q8 += 8;
  3477. }
  3478. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3479. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3480. #else
  3481. (void) ksigns64;
  3482. assert(false);
  3483. return 0.f;
  3484. #endif
  3485. #else
  3486. (void) ksigns64;
  3487. assert(false);
  3488. return 0.f;
  3489. #endif
  3490. }
  3491. // TODO
  3492. static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(
  3493. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3494. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3495. #if QK_K == 256
  3496. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  3497. const int ib32 = iqs;
  3498. const int8_t * q8 = bq8_1[ib32].qs;
  3499. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  3500. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3501. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3502. int sumi1 = 0;
  3503. for (int l = 0; l < 2; ++l) {
  3504. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  3505. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  3506. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  3507. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  3508. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  3509. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3510. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3511. q8 += 8;
  3512. }
  3513. int sumi2 = 0;
  3514. for (int l = 2; l < 4; ++l) {
  3515. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  3516. const uint32_t signs0 = __vcmpeq4(((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  3517. const uint32_t signs1 = __vcmpeq4(((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  3518. const int grid_l = __vsub4(grid[0] ^ signs0, signs0);
  3519. const int grid_h = __vsub4(grid[1] ^ signs1, signs1);
  3520. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3521. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3522. q8 += 8;
  3523. }
  3524. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3525. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3526. #else
  3527. (void) ksigns64;
  3528. assert(false);
  3529. return 0.f;
  3530. #endif
  3531. #else
  3532. (void) ksigns64;
  3533. assert(false);
  3534. return 0.f;
  3535. #endif
  3536. }
  3537. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  3538. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3539. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3540. #if QK_K == 256
  3541. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  3542. const int ib32 = iqs;
  3543. const uint8_t * q3 = bq2->qs + 8*ib32;
  3544. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  3545. const int8_t * q8 = bq8_1[ib32].qs;
  3546. uint32_t aux32 = gas[0] | (gas[1] << 16);
  3547. int sumi = 0;
  3548. for (int l = 0; l < 4; ++l) {
  3549. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  3550. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  3551. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  3552. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  3553. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  3554. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3555. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3556. q8 += 8;
  3557. aux32 >>= 7;
  3558. }
  3559. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  3560. return d * sumi;
  3561. #else
  3562. assert(false);
  3563. return 0.f;
  3564. #endif
  3565. #else
  3566. assert(false);
  3567. return 0.f;
  3568. #endif
  3569. }
  3570. // TODO: don't use lookup table for signs
  3571. static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(
  3572. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3573. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3574. #if QK_K == 256
  3575. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  3576. const int ib32 = iqs;
  3577. const uint8_t * qs = bq2->qs + 8*ib32;
  3578. const int8_t * q8 = bq8_1[ib32].qs;
  3579. int sumi = 0;
  3580. for (int l = 0; l < 4; ++l) {
  3581. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  3582. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  3583. uint32_t signs0 = __vcmpeq4(((bq2->signs[4*ib32+l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201);
  3584. uint32_t signs1 = __vcmpeq4(((bq2->signs[4*ib32+l] >> 4) * 0x01010101) & 0x08040201, 0x08040201);
  3585. const int grid_l = __vsub4(grid1[0] ^ signs0, signs0);
  3586. const int grid_h = __vsub4(grid2[0] ^ signs1, signs1);
  3587. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3588. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3589. q8 += 8;
  3590. }
  3591. const float d = (float)bq2->d * (1 + 2*((bq2->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * __low2float(bq8_1[ib32].ds);
  3592. return d * sumi;
  3593. #else
  3594. assert(false);
  3595. return 0.f;
  3596. #endif
  3597. #else
  3598. assert(false);
  3599. return 0.f;
  3600. #endif
  3601. }
  3602. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  3603. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3604. #if QK_K == 256
  3605. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  3606. const int ib32 = iqs;
  3607. int sumi = 0;
  3608. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3609. const int * q8 = (const int *)bq8_1[ib32].qs;
  3610. for (int l = 0; l < 4; ++l) {
  3611. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  3612. int grid0 = grid[0] & 0x0f0f0f0f;
  3613. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  3614. sumi = __dp4a(q8[2*l+1], grid1, __dp4a(q8[2*l+0], grid0, sumi));
  3615. }
  3616. #else
  3617. const int8_t * q8 = bq8_1[ib32].qs;
  3618. for (int l = 0; l < 4; ++l) {
  3619. const uint8_t * grid = (const uint8_t *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  3620. for (int j = 0; j < 4; ++j) {
  3621. sumi += q8[j] * (grid[j] & 0xf) + q8[j+4] * (grid[j] >> 4);
  3622. }
  3623. q8 += 8;
  3624. }
  3625. #endif
  3626. const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA;
  3627. const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1);
  3628. const float d = d1q * __low2float (bq8_1[ib32].ds);
  3629. const float m = d1q * __high2float(bq8_1[ib32].ds);
  3630. return d * sumi + m * delta;
  3631. #else
  3632. assert(false);
  3633. return 0.f;
  3634. #endif
  3635. }
  3636. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3637. static __device__ __forceinline__ void get_int_from_table_16(const uint32_t & q4, const uint8_t * values,
  3638. int & val1, int & val2) {
  3639. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  3640. aux32 = q4 & 0x0f0f0f0f;
  3641. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  3642. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  3643. val1 = v1 | (v2 << 16);
  3644. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  3645. v1 = values[q8[0]] | (values[q8[1]] << 8);
  3646. v2 = values[q8[2]] | (values[q8[3]] << 8);
  3647. val2 = v1 | (v2 << 16);
  3648. }
  3649. #endif
  3650. static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
  3651. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3652. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  3653. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3654. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  3655. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  3656. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  3657. int v1, v2;
  3658. int sumi1 = 0, sumi2 = 0;
  3659. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3660. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  3661. get_int_from_table_16(aux, values, v1, v2);
  3662. sumi1 = __dp4a(v1, q8[l+0], sumi1);
  3663. sumi2 = __dp4a(v2, q8[l+4], sumi2);
  3664. }
  3665. #else
  3666. const uint8_t * q4 = bq->qs + 4*iqs;
  3667. const int8_t * q8 = bq8_1->qs + 4*iqs;
  3668. int sumi1 = 0, sumi2 = 0;
  3669. for (int l = 0; l < 4*VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3670. sumi1 += q8[l+ 0] * kvalues_iq4nl[q4[l] & 0xf];
  3671. sumi2 += q8[l+16] * kvalues_iq4nl[q4[l] >> 4];
  3672. }
  3673. #endif
  3674. const float d = (float)bq->d * __low2float(bq8_1->ds);
  3675. return d * (sumi1 + sumi2);
  3676. }
  3677. static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(
  3678. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3679. #if QK_K == 256
  3680. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3681. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  3682. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  3683. //// iqs is 0...7
  3684. //const int ib64 = iqs/2;
  3685. //const int il = iqs%2;
  3686. //const int32_t * q8_1 = (const int *)bq8_1[2*ib64+0].qs + 2*il;
  3687. //const int32_t * q8_2 = (const int *)bq8_1[2*ib64+1].qs + 2*il;
  3688. //const uint32_t * q4_1 = (const uint32_t *)bq4->qs + 8*ib64 + 2*il;
  3689. //const uint32_t * q4_2 = q4_1 + 4;
  3690. //const int8_t ls1 = (bq4->scales_l[ib64] & 0xf) | (((bq4->scales_h >> (4*ib64+0)) & 3) << 4);
  3691. //const int8_t ls2 = (bq4->scales_l[ib64] >> 4) | (((bq4->scales_h >> (4*ib64+2)) & 3) << 4);
  3692. //const float d1 = (float)bq4->d * (ls1 - 32) * __low2float(bq8_1[2*ib64+0].ds);
  3693. //const float d2 = (float)bq4->d * (ls2 - 32) * __low2float(bq8_1[2*ib64+1].ds);
  3694. //int v1, v2;
  3695. //int sumi1 = 0, sumi2 = 0;
  3696. //for (int j = 0; j < 2; ++j) {
  3697. // get_int_from_table_16(q4_1[j], values, v1, v2);
  3698. // sumi1 = __dp4a(v2, q8_1[j+4], __dp4a(v1, q8_1[j+0], sumi1));
  3699. // get_int_from_table_16(q4_2[j], values, v1, v2);
  3700. // sumi2 = __dp4a(v2, q8_2[j+4], __dp4a(v1, q8_2[j+0], sumi2));
  3701. //}
  3702. //return d1 * sumi1 + d2 * sumi2;
  3703. // iqs is 0...7
  3704. const int ib32 = iqs;
  3705. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  3706. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  3707. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  3708. const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  3709. int v1, v2;
  3710. int sumi1 = 0, sumi2 = 0;
  3711. for (int j = 0; j < 4; ++j) {
  3712. get_int_from_table_16(q4[j], values, v1, v2);
  3713. sumi1 = __dp4a(v1, q8[j+0], sumi1);
  3714. sumi2 = __dp4a(v2, q8[j+4], sumi2);
  3715. }
  3716. return d * (sumi1 + sumi2);
  3717. //// iqs is 0...15
  3718. //const int ib32 = iqs/2;
  3719. //const int il = iqs%2;
  3720. //const int32_t * q8 = (const int *)bq8_1[ib32].qs + 2*il;
  3721. //const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32 + 2*il;
  3722. //const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  3723. //const float d = (float)bq4->d * (ls - 32) * __low2float(bq8_1[ib32].ds);
  3724. //int v1, v2;
  3725. //int sumi1 = 0, sumi2 = 0;
  3726. //for (int j = 0; j < 2; ++j) {
  3727. // get_int_from_table_16(q4[j], values, v1, v2);
  3728. // sumi1 = __dp4a(v1, q8[j+0], sumi1);
  3729. // sumi2 = __dp4a(v2, q8[j+4], sumi2);
  3730. //}
  3731. //return d * (sumi1 + sumi2);
  3732. #else
  3733. assert(false);
  3734. return 0.f;
  3735. #endif
  3736. #else
  3737. return vec_dot_iq4_xs_q8_1(vbq, bq8_1, iqs);
  3738. #endif
  3739. }
  3740. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3741. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3742. static __device__ __forceinline__ void mul_mat_q(
  3743. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3744. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3745. const block_q_t * x = (const block_q_t *) vx;
  3746. const block_q8_1 * y = (const block_q8_1 *) vy;
  3747. const int blocks_per_row_x = ncols_x / qk;
  3748. const int blocks_per_col_y = nrows_y / QK8_1;
  3749. const int blocks_per_warp = WARP_SIZE / qi;
  3750. const int & ncols_dst = ncols_y;
  3751. const int row_dst_0 = blockIdx.x*mmq_y;
  3752. const int & row_x_0 = row_dst_0;
  3753. const int col_dst_0 = blockIdx.y*mmq_x;
  3754. const int & col_y_0 = col_dst_0;
  3755. int * tile_x_ql = nullptr;
  3756. half2 * tile_x_dm = nullptr;
  3757. int * tile_x_qh = nullptr;
  3758. int * tile_x_sc = nullptr;
  3759. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3760. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3761. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3762. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3763. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3764. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3765. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3766. #pragma unroll
  3767. for (int ir = 0; ir < qr; ++ir) {
  3768. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3769. const int kbxd = kqs / QI8_1;
  3770. #pragma unroll
  3771. for (int i = 0; i < mmq_x; i += nwarps) {
  3772. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3773. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3774. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3775. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3776. }
  3777. #pragma unroll
  3778. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3779. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3780. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3781. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3782. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3783. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3784. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3785. if (need_sum) {
  3786. *dsi_dst = *dsi_src;
  3787. } else {
  3788. float * dfi_dst = (float *) dsi_dst;
  3789. *dfi_dst = __low2half(*dsi_src);
  3790. }
  3791. }
  3792. __syncthreads();
  3793. // #pragma unroll // unrolling this loop causes too much register pressure
  3794. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3795. #pragma unroll
  3796. for (int j = 0; j < mmq_x; j += nwarps) {
  3797. #pragma unroll
  3798. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3799. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3800. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3801. threadIdx.x + i, threadIdx.y + j, k);
  3802. }
  3803. }
  3804. }
  3805. __syncthreads();
  3806. }
  3807. }
  3808. #pragma unroll
  3809. for (int j = 0; j < mmq_x; j += nwarps) {
  3810. const int col_dst = col_dst_0 + j + threadIdx.y;
  3811. if (col_dst >= ncols_dst) {
  3812. return;
  3813. }
  3814. #pragma unroll
  3815. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3816. const int row_dst = row_dst_0 + threadIdx.x + i;
  3817. if (row_dst >= nrows_dst) {
  3818. continue;
  3819. }
  3820. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3821. }
  3822. }
  3823. }
  3824. #define MMQ_X_Q4_0_RDNA2 64
  3825. #define MMQ_Y_Q4_0_RDNA2 128
  3826. #define NWARPS_Q4_0_RDNA2 8
  3827. #define MMQ_X_Q4_0_RDNA1 64
  3828. #define MMQ_Y_Q4_0_RDNA1 64
  3829. #define NWARPS_Q4_0_RDNA1 8
  3830. #if defined(CUDA_USE_TENSOR_CORES)
  3831. #define MMQ_X_Q4_0_AMPERE 4
  3832. #define MMQ_Y_Q4_0_AMPERE 32
  3833. #define NWARPS_Q4_0_AMPERE 4
  3834. #else
  3835. #define MMQ_X_Q4_0_AMPERE 64
  3836. #define MMQ_Y_Q4_0_AMPERE 128
  3837. #define NWARPS_Q4_0_AMPERE 4
  3838. #endif
  3839. #define MMQ_X_Q4_0_PASCAL 64
  3840. #define MMQ_Y_Q4_0_PASCAL 64
  3841. #define NWARPS_Q4_0_PASCAL 8
  3842. template <bool need_check> static __global__ void
  3843. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3844. #if defined(RDNA3) || defined(RDNA2)
  3845. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3846. #endif // defined(RDNA3) || defined(RDNA2)
  3847. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3848. mul_mat_q4_0(
  3849. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3850. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3851. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3852. #if defined(RDNA3) || defined(RDNA2)
  3853. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3854. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3855. const int nwarps = NWARPS_Q4_0_RDNA2;
  3856. #else
  3857. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3858. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3859. const int nwarps = NWARPS_Q4_0_RDNA1;
  3860. #endif // defined(RDNA3) || defined(RDNA2)
  3861. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3862. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3863. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3864. #elif __CUDA_ARCH__ >= CC_VOLTA
  3865. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3866. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3867. const int nwarps = NWARPS_Q4_0_AMPERE;
  3868. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3869. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3870. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3871. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3872. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3873. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3874. const int nwarps = NWARPS_Q4_0_PASCAL;
  3875. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3876. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3877. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3878. #else
  3879. (void) vec_dot_q4_0_q8_1_mul_mat;
  3880. NO_DEVICE_CODE;
  3881. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3882. }
  3883. #define MMQ_X_Q4_1_RDNA2 64
  3884. #define MMQ_Y_Q4_1_RDNA2 128
  3885. #define NWARPS_Q4_1_RDNA2 8
  3886. #define MMQ_X_Q4_1_RDNA1 64
  3887. #define MMQ_Y_Q4_1_RDNA1 64
  3888. #define NWARPS_Q4_1_RDNA1 8
  3889. #if defined(CUDA_USE_TENSOR_CORES)
  3890. #define MMQ_X_Q4_1_AMPERE 4
  3891. #define MMQ_Y_Q4_1_AMPERE 32
  3892. #define NWARPS_Q4_1_AMPERE 4
  3893. #else
  3894. #define MMQ_X_Q4_1_AMPERE 64
  3895. #define MMQ_Y_Q4_1_AMPERE 128
  3896. #define NWARPS_Q4_1_AMPERE 4
  3897. #endif
  3898. #define MMQ_X_Q4_1_PASCAL 64
  3899. #define MMQ_Y_Q4_1_PASCAL 64
  3900. #define NWARPS_Q4_1_PASCAL 8
  3901. template <bool need_check> static __global__ void
  3902. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3903. #if defined(RDNA3) || defined(RDNA2)
  3904. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3905. #endif // defined(RDNA3) || defined(RDNA2)
  3906. #elif __CUDA_ARCH__ < CC_VOLTA
  3907. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3908. #endif // __CUDA_ARCH__ < CC_VOLTA
  3909. mul_mat_q4_1(
  3910. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3911. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3912. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3913. #if defined(RDNA3) || defined(RDNA2)
  3914. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3915. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3916. const int nwarps = NWARPS_Q4_1_RDNA2;
  3917. #else
  3918. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3919. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3920. const int nwarps = NWARPS_Q4_1_RDNA1;
  3921. #endif // defined(RDNA3) || defined(RDNA2)
  3922. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3923. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3924. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3925. #elif __CUDA_ARCH__ >= CC_VOLTA
  3926. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3927. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3928. const int nwarps = NWARPS_Q4_1_AMPERE;
  3929. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3930. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3931. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3932. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3933. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3934. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3935. const int nwarps = NWARPS_Q4_1_PASCAL;
  3936. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3937. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3938. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3939. #else
  3940. (void) vec_dot_q4_1_q8_1_mul_mat;
  3941. NO_DEVICE_CODE;
  3942. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3943. }
  3944. #define MMQ_X_Q5_0_RDNA2 64
  3945. #define MMQ_Y_Q5_0_RDNA2 128
  3946. #define NWARPS_Q5_0_RDNA2 8
  3947. #define MMQ_X_Q5_0_RDNA1 64
  3948. #define MMQ_Y_Q5_0_RDNA1 64
  3949. #define NWARPS_Q5_0_RDNA1 8
  3950. #if defined(CUDA_USE_TENSOR_CORES)
  3951. #define MMQ_X_Q5_0_AMPERE 4
  3952. #define MMQ_Y_Q5_0_AMPERE 32
  3953. #define NWARPS_Q5_0_AMPERE 4
  3954. #else
  3955. #define MMQ_X_Q5_0_AMPERE 128
  3956. #define MMQ_Y_Q5_0_AMPERE 64
  3957. #define NWARPS_Q5_0_AMPERE 4
  3958. #endif
  3959. #define MMQ_X_Q5_0_PASCAL 64
  3960. #define MMQ_Y_Q5_0_PASCAL 64
  3961. #define NWARPS_Q5_0_PASCAL 8
  3962. template <bool need_check> static __global__ void
  3963. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3964. #if defined(RDNA3) || defined(RDNA2)
  3965. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3966. #endif // defined(RDNA3) || defined(RDNA2)
  3967. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3968. mul_mat_q5_0(
  3969. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3970. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3971. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3972. #if defined(RDNA3) || defined(RDNA2)
  3973. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3974. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3975. const int nwarps = NWARPS_Q5_0_RDNA2;
  3976. #else
  3977. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3978. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3979. const int nwarps = NWARPS_Q5_0_RDNA1;
  3980. #endif // defined(RDNA3) || defined(RDNA2)
  3981. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3982. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3983. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3984. #elif __CUDA_ARCH__ >= CC_VOLTA
  3985. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3986. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3987. const int nwarps = NWARPS_Q5_0_AMPERE;
  3988. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3989. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3990. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3991. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3992. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3993. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3994. const int nwarps = NWARPS_Q5_0_PASCAL;
  3995. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3996. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3997. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3998. #else
  3999. (void) vec_dot_q5_0_q8_1_mul_mat;
  4000. NO_DEVICE_CODE;
  4001. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4002. }
  4003. #define MMQ_X_Q5_1_RDNA2 64
  4004. #define MMQ_Y_Q5_1_RDNA2 128
  4005. #define NWARPS_Q5_1_RDNA2 8
  4006. #define MMQ_X_Q5_1_RDNA1 64
  4007. #define MMQ_Y_Q5_1_RDNA1 64
  4008. #define NWARPS_Q5_1_RDNA1 8
  4009. #if defined(CUDA_USE_TENSOR_CORES)
  4010. #define MMQ_X_Q5_1_AMPERE 4
  4011. #define MMQ_Y_Q5_1_AMPERE 32
  4012. #define NWARPS_Q5_1_AMPERE 4
  4013. #else
  4014. #define MMQ_X_Q5_1_AMPERE 128
  4015. #define MMQ_Y_Q5_1_AMPERE 64
  4016. #define NWARPS_Q5_1_AMPERE 4
  4017. #endif
  4018. #define MMQ_X_Q5_1_PASCAL 64
  4019. #define MMQ_Y_Q5_1_PASCAL 64
  4020. #define NWARPS_Q5_1_PASCAL 8
  4021. template <bool need_check> static __global__ void
  4022. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4023. #if defined(RDNA3) || defined(RDNA2)
  4024. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4025. #endif // defined(RDNA3) || defined(RDNA2)
  4026. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4027. mul_mat_q5_1(
  4028. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4029. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4030. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4031. #if defined(RDNA3) || defined(RDNA2)
  4032. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4033. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4034. const int nwarps = NWARPS_Q5_1_RDNA2;
  4035. #else
  4036. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4037. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4038. const int nwarps = NWARPS_Q5_1_RDNA1;
  4039. #endif // defined(RDNA3) || defined(RDNA2)
  4040. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4041. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4042. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4043. #elif __CUDA_ARCH__ >= CC_VOLTA
  4044. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4045. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4046. const int nwarps = NWARPS_Q5_1_AMPERE;
  4047. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4048. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4049. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4050. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4051. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4052. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4053. const int nwarps = NWARPS_Q5_1_PASCAL;
  4054. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4055. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4056. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4057. #else
  4058. (void) vec_dot_q5_1_q8_1_mul_mat;
  4059. NO_DEVICE_CODE;
  4060. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4061. }
  4062. #define MMQ_X_Q8_0_RDNA2 64
  4063. #define MMQ_Y_Q8_0_RDNA2 128
  4064. #define NWARPS_Q8_0_RDNA2 8
  4065. #define MMQ_X_Q8_0_RDNA1 64
  4066. #define MMQ_Y_Q8_0_RDNA1 64
  4067. #define NWARPS_Q8_0_RDNA1 8
  4068. #if defined(CUDA_USE_TENSOR_CORES)
  4069. #define MMQ_X_Q8_0_AMPERE 4
  4070. #define MMQ_Y_Q8_0_AMPERE 32
  4071. #define NWARPS_Q8_0_AMPERE 4
  4072. #else
  4073. #define MMQ_X_Q8_0_AMPERE 128
  4074. #define MMQ_Y_Q8_0_AMPERE 64
  4075. #define NWARPS_Q8_0_AMPERE 4
  4076. #endif
  4077. #define MMQ_X_Q8_0_PASCAL 64
  4078. #define MMQ_Y_Q8_0_PASCAL 64
  4079. #define NWARPS_Q8_0_PASCAL 8
  4080. template <bool need_check> static __global__ void
  4081. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4082. #if defined(RDNA3) || defined(RDNA2)
  4083. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4084. #endif // defined(RDNA3) || defined(RDNA2)
  4085. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4086. mul_mat_q8_0(
  4087. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4088. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4089. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4090. #if defined(RDNA3) || defined(RDNA2)
  4091. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4092. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4093. const int nwarps = NWARPS_Q8_0_RDNA2;
  4094. #else
  4095. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4096. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4097. const int nwarps = NWARPS_Q8_0_RDNA1;
  4098. #endif // defined(RDNA3) || defined(RDNA2)
  4099. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4100. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4101. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4102. #elif __CUDA_ARCH__ >= CC_VOLTA
  4103. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4104. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4105. const int nwarps = NWARPS_Q8_0_AMPERE;
  4106. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4107. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4108. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4109. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4110. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4111. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4112. const int nwarps = NWARPS_Q8_0_PASCAL;
  4113. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4114. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4115. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4116. #else
  4117. (void) vec_dot_q8_0_q8_1_mul_mat;
  4118. NO_DEVICE_CODE;
  4119. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4120. }
  4121. #define MMQ_X_Q2_K_RDNA2 64
  4122. #define MMQ_Y_Q2_K_RDNA2 128
  4123. #define NWARPS_Q2_K_RDNA2 8
  4124. #define MMQ_X_Q2_K_RDNA1 128
  4125. #define MMQ_Y_Q2_K_RDNA1 32
  4126. #define NWARPS_Q2_K_RDNA1 8
  4127. #if defined(CUDA_USE_TENSOR_CORES)
  4128. #define MMQ_X_Q2_K_AMPERE 4
  4129. #define MMQ_Y_Q2_K_AMPERE 32
  4130. #define NWARPS_Q2_K_AMPERE 4
  4131. #else
  4132. #define MMQ_X_Q2_K_AMPERE 64
  4133. #define MMQ_Y_Q2_K_AMPERE 128
  4134. #define NWARPS_Q2_K_AMPERE 4
  4135. #endif
  4136. #define MMQ_X_Q2_K_PASCAL 64
  4137. #define MMQ_Y_Q2_K_PASCAL 64
  4138. #define NWARPS_Q2_K_PASCAL 8
  4139. template <bool need_check> static __global__ void
  4140. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4141. #if defined(RDNA3) || defined(RDNA2)
  4142. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4143. #endif // defined(RDNA3) || defined(RDNA2)
  4144. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4145. mul_mat_q2_K(
  4146. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4147. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4148. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4149. #if defined(RDNA3) || defined(RDNA2)
  4150. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4151. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4152. const int nwarps = NWARPS_Q2_K_RDNA2;
  4153. #else
  4154. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4155. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4156. const int nwarps = NWARPS_Q2_K_RDNA1;
  4157. #endif // defined(RDNA3) || defined(RDNA2)
  4158. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4159. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4160. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4161. #elif __CUDA_ARCH__ >= CC_VOLTA
  4162. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4163. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4164. const int nwarps = NWARPS_Q2_K_AMPERE;
  4165. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4166. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4167. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4168. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4169. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4170. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4171. const int nwarps = NWARPS_Q2_K_PASCAL;
  4172. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4173. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4174. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4175. #else
  4176. (void) vec_dot_q2_K_q8_1_mul_mat;
  4177. NO_DEVICE_CODE;
  4178. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4179. }
  4180. #define MMQ_X_Q3_K_RDNA2 128
  4181. #define MMQ_Y_Q3_K_RDNA2 64
  4182. #define NWARPS_Q3_K_RDNA2 8
  4183. #define MMQ_X_Q3_K_RDNA1 32
  4184. #define MMQ_Y_Q3_K_RDNA1 128
  4185. #define NWARPS_Q3_K_RDNA1 8
  4186. #if defined(CUDA_USE_TENSOR_CORES)
  4187. #define MMQ_X_Q3_K_AMPERE 4
  4188. #define MMQ_Y_Q3_K_AMPERE 32
  4189. #define NWARPS_Q3_K_AMPERE 4
  4190. #else
  4191. #define MMQ_X_Q3_K_AMPERE 128
  4192. #define MMQ_Y_Q3_K_AMPERE 128
  4193. #define NWARPS_Q3_K_AMPERE 4
  4194. #endif
  4195. #define MMQ_X_Q3_K_PASCAL 64
  4196. #define MMQ_Y_Q3_K_PASCAL 64
  4197. #define NWARPS_Q3_K_PASCAL 8
  4198. template <bool need_check> static __global__ void
  4199. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4200. #if defined(RDNA3) || defined(RDNA2)
  4201. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4202. #endif // defined(RDNA3) || defined(RDNA2)
  4203. #elif __CUDA_ARCH__ < CC_VOLTA
  4204. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4205. #endif // __CUDA_ARCH__ < CC_VOLTA
  4206. mul_mat_q3_K(
  4207. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4208. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4209. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4210. #if defined(RDNA3) || defined(RDNA2)
  4211. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4212. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4213. const int nwarps = NWARPS_Q3_K_RDNA2;
  4214. #else
  4215. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4216. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4217. const int nwarps = NWARPS_Q3_K_RDNA1;
  4218. #endif // defined(RDNA3) || defined(RDNA2)
  4219. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4220. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4221. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4222. #elif __CUDA_ARCH__ >= CC_VOLTA
  4223. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4224. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4225. const int nwarps = NWARPS_Q3_K_AMPERE;
  4226. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4227. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4228. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4229. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4230. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4231. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4232. const int nwarps = NWARPS_Q3_K_PASCAL;
  4233. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4234. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4235. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4236. #else
  4237. (void) vec_dot_q3_K_q8_1_mul_mat;
  4238. NO_DEVICE_CODE;
  4239. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4240. }
  4241. #define MMQ_X_Q4_K_RDNA2 64
  4242. #define MMQ_Y_Q4_K_RDNA2 128
  4243. #define NWARPS_Q4_K_RDNA2 8
  4244. #define MMQ_X_Q4_K_RDNA1 32
  4245. #define MMQ_Y_Q4_K_RDNA1 64
  4246. #define NWARPS_Q4_K_RDNA1 8
  4247. #if defined(CUDA_USE_TENSOR_CORES)
  4248. #define MMQ_X_Q4_K_AMPERE 4
  4249. #define MMQ_Y_Q4_K_AMPERE 32
  4250. #define NWARPS_Q4_K_AMPERE 4
  4251. #else
  4252. #define MMQ_X_Q4_K_AMPERE 64
  4253. #define MMQ_Y_Q4_K_AMPERE 128
  4254. #define NWARPS_Q4_K_AMPERE 4
  4255. #endif
  4256. #define MMQ_X_Q4_K_PASCAL 64
  4257. #define MMQ_Y_Q4_K_PASCAL 64
  4258. #define NWARPS_Q4_K_PASCAL 8
  4259. template <bool need_check> static __global__ void
  4260. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4261. #if defined(RDNA3) || defined(RDNA2)
  4262. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4263. #endif // defined(RDNA3) || defined(RDNA2)
  4264. #elif __CUDA_ARCH__ < CC_VOLTA
  4265. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4266. #endif // __CUDA_ARCH__ < CC_VOLTA
  4267. mul_mat_q4_K(
  4268. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4269. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4270. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4271. #if defined(RDNA3) || defined(RDNA2)
  4272. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4273. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4274. const int nwarps = NWARPS_Q4_K_RDNA2;
  4275. #else
  4276. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4277. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4278. const int nwarps = NWARPS_Q4_K_RDNA1;
  4279. #endif // defined(RDNA3) || defined(RDNA2)
  4280. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4281. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4282. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4283. #elif __CUDA_ARCH__ >= CC_VOLTA
  4284. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4285. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4286. const int nwarps = NWARPS_Q4_K_AMPERE;
  4287. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4288. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4289. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4290. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4291. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4292. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4293. const int nwarps = NWARPS_Q4_K_PASCAL;
  4294. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4295. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4296. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4297. #else
  4298. (void) vec_dot_q4_K_q8_1_mul_mat;
  4299. NO_DEVICE_CODE;
  4300. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4301. }
  4302. #define MMQ_X_Q5_K_RDNA2 64
  4303. #define MMQ_Y_Q5_K_RDNA2 128
  4304. #define NWARPS_Q5_K_RDNA2 8
  4305. #define MMQ_X_Q5_K_RDNA1 32
  4306. #define MMQ_Y_Q5_K_RDNA1 64
  4307. #define NWARPS_Q5_K_RDNA1 8
  4308. #if defined(CUDA_USE_TENSOR_CORES)
  4309. #define MMQ_X_Q5_K_AMPERE 4
  4310. #define MMQ_Y_Q5_K_AMPERE 32
  4311. #define NWARPS_Q5_K_AMPERE 4
  4312. #else
  4313. #define MMQ_X_Q5_K_AMPERE 64
  4314. #define MMQ_Y_Q5_K_AMPERE 128
  4315. #define NWARPS_Q5_K_AMPERE 4
  4316. #endif
  4317. #define MMQ_X_Q5_K_PASCAL 64
  4318. #define MMQ_Y_Q5_K_PASCAL 64
  4319. #define NWARPS_Q5_K_PASCAL 8
  4320. template <bool need_check> static __global__ void
  4321. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4322. #if defined(RDNA3) || defined(RDNA2)
  4323. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  4324. #endif // defined(RDNA3) || defined(RDNA2)
  4325. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4326. mul_mat_q5_K(
  4327. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4328. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4329. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4330. #if defined(RDNA3) || defined(RDNA2)
  4331. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  4332. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  4333. const int nwarps = NWARPS_Q5_K_RDNA2;
  4334. #else
  4335. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  4336. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  4337. const int nwarps = NWARPS_Q5_K_RDNA1;
  4338. #endif // defined(RDNA3) || defined(RDNA2)
  4339. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4340. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4341. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4342. #elif __CUDA_ARCH__ >= CC_VOLTA
  4343. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  4344. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  4345. const int nwarps = NWARPS_Q5_K_AMPERE;
  4346. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4347. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4348. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4349. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4350. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  4351. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  4352. const int nwarps = NWARPS_Q5_K_PASCAL;
  4353. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4354. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4355. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4356. #else
  4357. (void) vec_dot_q5_K_q8_1_mul_mat;
  4358. NO_DEVICE_CODE;
  4359. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4360. }
  4361. #define MMQ_X_Q6_K_RDNA2 64
  4362. #define MMQ_Y_Q6_K_RDNA2 128
  4363. #define NWARPS_Q6_K_RDNA2 8
  4364. #define MMQ_X_Q6_K_RDNA1 32
  4365. #define MMQ_Y_Q6_K_RDNA1 64
  4366. #define NWARPS_Q6_K_RDNA1 8
  4367. #if defined(CUDA_USE_TENSOR_CORES)
  4368. #define MMQ_X_Q6_K_AMPERE 4
  4369. #define MMQ_Y_Q6_K_AMPERE 32
  4370. #define NWARPS_Q6_K_AMPERE 4
  4371. #else
  4372. #define MMQ_X_Q6_K_AMPERE 64
  4373. #define MMQ_Y_Q6_K_AMPERE 64
  4374. #define NWARPS_Q6_K_AMPERE 4
  4375. #endif
  4376. #define MMQ_X_Q6_K_PASCAL 64
  4377. #define MMQ_Y_Q6_K_PASCAL 64
  4378. #define NWARPS_Q6_K_PASCAL 8
  4379. template <bool need_check> static __global__ void
  4380. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4381. #if defined(RDNA3) || defined(RDNA2)
  4382. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4383. #endif // defined(RDNA3) || defined(RDNA2)
  4384. #elif __CUDA_ARCH__ < CC_VOLTA
  4385. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4386. #endif // __CUDA_ARCH__ < CC_VOLTA
  4387. mul_mat_q6_K(
  4388. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4389. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4390. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4391. #if defined(RDNA3) || defined(RDNA2)
  4392. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  4393. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  4394. const int nwarps = NWARPS_Q6_K_RDNA2;
  4395. #else
  4396. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  4397. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  4398. const int nwarps = NWARPS_Q6_K_RDNA1;
  4399. #endif // defined(RDNA3) || defined(RDNA2)
  4400. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4401. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4402. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4403. #elif __CUDA_ARCH__ >= CC_VOLTA
  4404. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  4405. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  4406. const int nwarps = NWARPS_Q6_K_AMPERE;
  4407. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4408. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4409. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4410. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4411. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  4412. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  4413. const int nwarps = NWARPS_Q6_K_PASCAL;
  4414. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4415. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4416. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4417. #else
  4418. (void) vec_dot_q6_K_q8_1_mul_mat;
  4419. NO_DEVICE_CODE;
  4420. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4421. }
  4422. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  4423. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4424. // tell the compiler to use as many registers as it wants, see nwarps definition below
  4425. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  4426. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4427. static __global__ void mul_mat_vec_q(
  4428. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4429. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  4430. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  4431. constexpr int nwarps = 1;
  4432. constexpr int rows_per_cuda_block = 1;
  4433. #else
  4434. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  4435. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  4436. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  4437. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  4438. const int row0 = rows_per_cuda_block*blockIdx.x;
  4439. const int blocks_per_row_x = ncols_x / qk;
  4440. const int blocks_per_col_y = nrows_y / QK8_1;
  4441. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  4442. // partial sum for each thread
  4443. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  4444. const block_q_t * x = (const block_q_t *) vx;
  4445. const block_q8_1 * y = (const block_q8_1 *) vy;
  4446. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  4447. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  4448. // x block quant index when casting the quants to int
  4449. const int kqs = vdr * (tid % (qi/vdr));
  4450. #pragma unroll
  4451. for (int j = 0; j < ncols_y; ++j) {
  4452. #pragma unroll
  4453. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4454. tmp[j][i] += vec_dot_q_cuda(
  4455. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  4456. }
  4457. }
  4458. }
  4459. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  4460. if (threadIdx.y > 0) {
  4461. #pragma unroll
  4462. for (int j = 0; j < ncols_y; ++j) {
  4463. #pragma unroll
  4464. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4465. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  4466. }
  4467. }
  4468. }
  4469. __syncthreads();
  4470. if (threadIdx.y > 0) {
  4471. return;
  4472. }
  4473. // sum up partial sums and write back result
  4474. #pragma unroll
  4475. for (int j = 0; j < ncols_y; ++j) {
  4476. #pragma unroll
  4477. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4478. #pragma unroll
  4479. for (int l = 0; l < nwarps-1; ++l) {
  4480. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  4481. }
  4482. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  4483. }
  4484. if (threadIdx.x < rows_per_cuda_block) {
  4485. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  4486. }
  4487. }
  4488. }
  4489. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  4490. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  4491. // qk = quantized weights per x block
  4492. // qr = number of quantized weights per data value in x block
  4493. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4494. if (row >= nrows) {
  4495. return;
  4496. }
  4497. const int tid = threadIdx.x;
  4498. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  4499. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  4500. const int y_offset = qr == 1 ? 1 : qk/2;
  4501. // partial sum for each thread
  4502. #ifdef GGML_CUDA_F16
  4503. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  4504. #else
  4505. float tmp = 0.0f;
  4506. #endif // GGML_CUDA_F16
  4507. for (int i = 0; i < ncols; i += iter_stride) {
  4508. const int col = i + vals_per_iter*tid;
  4509. const int ib = (row*ncols + col)/qk; // x block index
  4510. const int iqs = (col%qk)/qr; // x quant index
  4511. const int iybs = col - col%qk; // y block start index
  4512. // processing >2 values per i iter is faster for fast GPUs
  4513. #pragma unroll
  4514. for (int j = 0; j < vals_per_iter; j += 2) {
  4515. // process 2 vals per j iter
  4516. // dequantize
  4517. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  4518. dfloat2 v;
  4519. dequantize_kernel(vx, ib, iqs + j/qr, v);
  4520. // matrix multiplication
  4521. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  4522. #ifdef GGML_CUDA_F16
  4523. tmp += __hmul2(v, {
  4524. y[iybs + iqs + j/qr + 0],
  4525. y[iybs + iqs + j/qr + y_offset]
  4526. });
  4527. #else
  4528. tmp += v.x * y[iybs + iqs + j/qr + 0];
  4529. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  4530. #endif // GGML_CUDA_F16
  4531. }
  4532. }
  4533. // sum up partial sums and write back result
  4534. tmp = warp_reduce_sum(tmp);
  4535. if (tid == 0) {
  4536. #ifdef GGML_CUDA_F16
  4537. dst[row] = tmp.x + tmp.y;
  4538. #else
  4539. dst[row] = tmp;
  4540. #endif // GGML_CUDA_F16
  4541. }
  4542. }
  4543. static __global__ void mul_mat_p021_f16_f32(
  4544. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  4545. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  4546. const half * x = (const half *) vx;
  4547. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4548. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4549. const int channel_x = channel / (nchannels_y / nchannels_x);
  4550. const int nrows_y = ncols_x;
  4551. const int nrows_dst = nrows_x;
  4552. const int row_dst = row_x;
  4553. float tmp = 0.0f;
  4554. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4555. const int col_x = col_x0 + threadIdx.x;
  4556. if (col_x >= ncols_x) {
  4557. break;
  4558. }
  4559. // x is transposed and permuted
  4560. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  4561. const float xi = __half2float(x[ix]);
  4562. const int row_y = col_x;
  4563. // y is not transposed but permuted
  4564. const int iy = channel*nrows_y + row_y;
  4565. tmp += xi * y[iy];
  4566. }
  4567. // dst is not transposed and not permuted
  4568. const int idst = channel*nrows_dst + row_dst;
  4569. // sum up partial sums and write back result
  4570. tmp = warp_reduce_sum(tmp);
  4571. if (threadIdx.x == 0) {
  4572. dst[idst] = tmp;
  4573. }
  4574. }
  4575. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  4576. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  4577. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  4578. const half * x = (const half *) vx;
  4579. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4580. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4581. const int channel_x = channel / channel_x_divisor;
  4582. const int nrows_y = ncols_x;
  4583. const int nrows_dst = nrows_x;
  4584. const int row_dst = row_x;
  4585. const int idst = channel*nrows_dst + row_dst;
  4586. float tmp = 0.0f;
  4587. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4588. const int col_x = col_x0 + threadIdx.x;
  4589. if (col_x >= ncols_x) {
  4590. break;
  4591. }
  4592. const int row_y = col_x;
  4593. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  4594. const int iy = channel*nrows_y + row_y;
  4595. const float xi = __half2float(x[ix]);
  4596. tmp += xi * y[iy];
  4597. }
  4598. // sum up partial sums and write back result
  4599. tmp = warp_reduce_sum(tmp);
  4600. if (threadIdx.x == 0) {
  4601. dst[idst] = tmp;
  4602. }
  4603. }
  4604. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4605. const float * xi = (const float *) cxi;
  4606. float * dsti = (float *) cdsti;
  4607. *dsti = *xi;
  4608. }
  4609. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4610. const float * xi = (const float *) cxi;
  4611. half * dsti = (half *) cdsti;
  4612. *dsti = __float2half(*xi);
  4613. }
  4614. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4615. const half * xi = (const half *) cxi;
  4616. half * dsti = (half *) cdsti;
  4617. *dsti = *xi;
  4618. }
  4619. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  4620. const half * xi = (const half *) cxi;
  4621. float * dsti = (float *) cdsti;
  4622. *dsti = *xi;
  4623. }
  4624. template <cpy_kernel_t cpy_1>
  4625. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4626. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4627. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4628. const int nb12, const int nb13) {
  4629. const int64_t i = blockDim.x*blockIdx.x + threadIdx.x;
  4630. if (i >= ne) {
  4631. return;
  4632. }
  4633. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4634. // then combine those indices with the corresponding byte offsets to get the total offsets
  4635. const int64_t i03 = i/(ne00 * ne01 * ne02);
  4636. const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4637. const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4638. const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4639. const int64_t x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4640. const int64_t i13 = i/(ne10 * ne11 * ne12);
  4641. const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4642. const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4643. const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4644. const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  4645. cpy_1(cx + x_offset, cdst + dst_offset);
  4646. }
  4647. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4648. const float * xi = (const float *) cxi;
  4649. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4650. float amax = 0.0f; // absolute max
  4651. for (int j = 0; j < QK8_0; j++) {
  4652. const float v = xi[j];
  4653. amax = fmaxf(amax, fabsf(v));
  4654. }
  4655. const float d = amax / ((1 << 7) - 1);
  4656. const float id = d ? 1.0f/d : 0.0f;
  4657. dsti->d = d;
  4658. for (int j = 0; j < QK8_0; ++j) {
  4659. const float x0 = xi[j]*id;
  4660. dsti->qs[j] = roundf(x0);
  4661. }
  4662. }
  4663. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4664. const float * xi = (const float *) cxi;
  4665. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4666. float amax = 0.0f;
  4667. float vmax = 0.0f;
  4668. for (int j = 0; j < QK4_0; ++j) {
  4669. const float v = xi[j];
  4670. if (amax < fabsf(v)) {
  4671. amax = fabsf(v);
  4672. vmax = v;
  4673. }
  4674. }
  4675. const float d = vmax / -8;
  4676. const float id = d ? 1.0f/d : 0.0f;
  4677. dsti->d = d;
  4678. for (int j = 0; j < QK4_0/2; ++j) {
  4679. const float x0 = xi[0 + j]*id;
  4680. const float x1 = xi[QK4_0/2 + j]*id;
  4681. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4682. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4683. dsti->qs[j] = xi0;
  4684. dsti->qs[j] |= xi1 << 4;
  4685. }
  4686. }
  4687. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4688. const float * xi = (const float *) cxi;
  4689. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4690. float vmin = FLT_MAX;
  4691. float vmax = -FLT_MAX;
  4692. for (int j = 0; j < QK4_1; ++j) {
  4693. const float v = xi[j];
  4694. if (v < vmin) vmin = v;
  4695. if (v > vmax) vmax = v;
  4696. }
  4697. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4698. const float id = d ? 1.0f/d : 0.0f;
  4699. dsti->dm.x = d;
  4700. dsti->dm.y = vmin;
  4701. for (int j = 0; j < QK4_1/2; ++j) {
  4702. const float x0 = (xi[0 + j] - vmin)*id;
  4703. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4704. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4705. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4706. dsti->qs[j] = xi0;
  4707. dsti->qs[j] |= xi1 << 4;
  4708. }
  4709. }
  4710. template <cpy_kernel_t cpy_blck, int qk>
  4711. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4712. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4713. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4714. const int nb12, const int nb13) {
  4715. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4716. if (i >= ne) {
  4717. return;
  4718. }
  4719. const int i03 = i/(ne00 * ne01 * ne02);
  4720. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4721. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4722. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4723. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4724. const int i13 = i/(ne10 * ne11 * ne12);
  4725. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4726. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4727. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4728. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  4729. cpy_blck(cx + x_offset, cdst + dst_offset);
  4730. }
  4731. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4732. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4733. return 1.0f - min(1.0f, max(0.0f, y));
  4734. }
  4735. struct rope_corr_dims {
  4736. float v[4];
  4737. };
  4738. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4739. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4740. static __device__ void rope_yarn(
  4741. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4742. float * cos_theta, float * sin_theta
  4743. ) {
  4744. // Get n-d rotational scaling corrected for extrapolation
  4745. float theta_interp = freq_scale * theta_extrap;
  4746. float theta = theta_interp;
  4747. if (ext_factor != 0.0f) {
  4748. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4749. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4750. // Get n-d magnitude scaling corrected for interpolation
  4751. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4752. }
  4753. *cos_theta = cosf(theta) * mscale;
  4754. *sin_theta = sinf(theta) * mscale;
  4755. }
  4756. // rope == RoPE == rotary positional embedding
  4757. template<typename T, bool has_pos>
  4758. static __global__ void rope(
  4759. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4760. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4761. ) {
  4762. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4763. if (col >= ncols) {
  4764. return;
  4765. }
  4766. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4767. const int i = row*ncols + col;
  4768. const int i2 = row/p_delta_rows;
  4769. const int p = has_pos ? pos[i2] : 0;
  4770. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4771. float cos_theta, sin_theta;
  4772. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4773. const float x0 = x[i + 0];
  4774. const float x1 = x[i + 1];
  4775. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4776. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4777. }
  4778. template<typename T, bool has_pos>
  4779. static __global__ void rope_neox(
  4780. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4781. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4782. ) {
  4783. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4784. if (col >= ncols) {
  4785. return;
  4786. }
  4787. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4788. const int ib = col / n_dims;
  4789. const int ic = col % n_dims;
  4790. if (ib > 0) {
  4791. const int i = row*ncols + ib*n_dims + ic;
  4792. dst[i + 0] = x[i + 0];
  4793. dst[i + 1] = x[i + 1];
  4794. return;
  4795. }
  4796. const int i = row*ncols + ib*n_dims + ic/2;
  4797. const int i2 = row/p_delta_rows;
  4798. float cur_rot = inv_ndims * ic - ib;
  4799. const int p = has_pos ? pos[i2] : 0;
  4800. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4801. float cos_theta, sin_theta;
  4802. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4803. const float x0 = x[i + 0];
  4804. const float x1 = x[i + n_dims/2];
  4805. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4806. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4807. }
  4808. static __global__ void rope_glm_f32(
  4809. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4810. int n_ctx
  4811. ) {
  4812. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4813. const int half_n_dims = ncols/4;
  4814. if (col >= half_n_dims) {
  4815. return;
  4816. }
  4817. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4818. const int i = row*ncols + col;
  4819. const int i2 = row/p_delta_rows;
  4820. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4821. // FIXME: this is likely wrong
  4822. const int p = pos != nullptr ? pos[i2] : 0;
  4823. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4824. const float sin_theta = sinf(theta);
  4825. const float cos_theta = cosf(theta);
  4826. const float x0 = x[i + 0];
  4827. const float x1 = x[i + half_n_dims];
  4828. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4829. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4830. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4831. const float sin_block_theta = sinf(block_theta);
  4832. const float cos_block_theta = cosf(block_theta);
  4833. const float x2 = x[i + half_n_dims * 2];
  4834. const float x3 = x[i + half_n_dims * 3];
  4835. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4836. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4837. }
  4838. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4839. const int n_heads_log2_floor, const float m0, const float m1) {
  4840. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4841. if (col >= ncols) {
  4842. return;
  4843. }
  4844. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4845. const int i = row*ncols + col;
  4846. const int k = row/k_rows;
  4847. float m_k;
  4848. if (k < n_heads_log2_floor) {
  4849. m_k = powf(m0, k + 1);
  4850. } else {
  4851. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4852. }
  4853. dst[i] = col * m_k + x[i];
  4854. }
  4855. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4856. const int row = blockIdx.x;
  4857. const int col = threadIdx.x;
  4858. float sum = 0.0f;
  4859. for (int i = col; i < ncols; i += blockDim.x) {
  4860. sum += x[row * ncols + i];
  4861. }
  4862. sum = warp_reduce_sum(sum);
  4863. if (col == 0) {
  4864. dst[row] = sum;
  4865. }
  4866. }
  4867. template<typename T>
  4868. static inline __device__ void swap(T & a, T & b) {
  4869. T tmp = a;
  4870. a = b;
  4871. b = tmp;
  4872. }
  4873. template<ggml_sort_order order>
  4874. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4875. // bitonic sort
  4876. int col = threadIdx.x;
  4877. int row = blockIdx.y;
  4878. if (col >= ncols) return;
  4879. const float * x_row = x + row * ncols;
  4880. int * dst_row = dst + row * ncols;
  4881. // initialize indices
  4882. if (col < ncols) {
  4883. dst_row[col] = col;
  4884. }
  4885. __syncthreads();
  4886. for (int k = 2; k <= ncols; k *= 2) {
  4887. for (int j = k / 2; j > 0; j /= 2) {
  4888. int ixj = col ^ j;
  4889. if (ixj > col) {
  4890. if ((col & k) == 0) {
  4891. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  4892. swap(dst_row[col], dst_row[ixj]);
  4893. }
  4894. } else {
  4895. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  4896. swap(dst_row[col], dst_row[ixj]);
  4897. }
  4898. }
  4899. }
  4900. __syncthreads();
  4901. }
  4902. }
  4903. }
  4904. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4905. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4906. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4907. if (col >= ncols) {
  4908. return;
  4909. }
  4910. const int i = row*ncols + col;
  4911. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4912. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4913. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4914. }
  4915. template <bool vals_smem, int ncols_template, int block_size_template>
  4916. static __global__ void soft_max_f32(const float * x, const float * mask, const float * pos, float * dst, const int ncols_par, const int nrows_y, const float scale, const float max_bias, const float m0, const float m1, uint32_t n_head_log2) {
  4917. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  4918. const int tid = threadIdx.x;
  4919. const int rowx = blockIdx.x;
  4920. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  4921. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  4922. const int warp_id = threadIdx.x / WARP_SIZE;
  4923. const int lane_id = threadIdx.x % WARP_SIZE;
  4924. float slope = 0.0f;
  4925. // ALiBi
  4926. if (max_bias > 0.0f) {
  4927. const int h = rowx/nrows_y; // head index
  4928. const float base = h < n_head_log2 ? m0 : m1;
  4929. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  4930. slope = powf(base, exp);
  4931. }
  4932. extern __shared__ float data_soft_max_f32[];
  4933. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  4934. // shared memory buffer to cache values between iterations:
  4935. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  4936. float max_val = -INFINITY;
  4937. #pragma unroll
  4938. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4939. const int col = col0 + tid;
  4940. if (ncols_template == 0 && col >= ncols) {
  4941. break;
  4942. }
  4943. const int ix = rowx*ncols + col;
  4944. const int iy = rowy*ncols + col;
  4945. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  4946. vals[col] = val;
  4947. max_val = max(max_val, val);
  4948. }
  4949. // find the max value in the block
  4950. max_val = warp_reduce_max(max_val);
  4951. if (block_size > WARP_SIZE) {
  4952. if (warp_id == 0) {
  4953. buf_iw[lane_id] = -INFINITY;
  4954. }
  4955. __syncthreads();
  4956. if (lane_id == 0) {
  4957. buf_iw[warp_id] = max_val;
  4958. }
  4959. __syncthreads();
  4960. max_val = buf_iw[lane_id];
  4961. max_val = warp_reduce_max(max_val);
  4962. }
  4963. float tmp = 0.0f; // partial sum
  4964. #pragma unroll
  4965. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4966. const int col = col0 + tid;
  4967. if (ncols_template == 0 && col >= ncols) {
  4968. break;
  4969. }
  4970. const float val = expf(vals[col] - max_val);
  4971. tmp += val;
  4972. vals[col] = val;
  4973. }
  4974. // find the sum of exps in the block
  4975. tmp = warp_reduce_sum(tmp);
  4976. if (block_size > WARP_SIZE) {
  4977. __syncthreads();
  4978. if (warp_id == 0) {
  4979. buf_iw[lane_id] = 0.0f;
  4980. }
  4981. __syncthreads();
  4982. if (lane_id == 0) {
  4983. buf_iw[warp_id] = tmp;
  4984. }
  4985. __syncthreads();
  4986. tmp = buf_iw[lane_id];
  4987. tmp = warp_reduce_sum(tmp);
  4988. }
  4989. const float inv_sum = 1.0f / tmp;
  4990. #pragma unroll
  4991. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  4992. const int col = col0 + tid;
  4993. if (ncols_template == 0 && col >= ncols) {
  4994. return;
  4995. }
  4996. const int idst = rowx*ncols + col;
  4997. dst[idst] = vals[col] * inv_sum;
  4998. }
  4999. }
  5000. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5001. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5002. if (i >= k) {
  5003. return;
  5004. }
  5005. dst[i] = scale * x[i];
  5006. }
  5007. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5008. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5009. if (i >= k) {
  5010. return;
  5011. }
  5012. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5013. }
  5014. template <typename T>
  5015. static __global__ void im2col_kernel(
  5016. const float * x, T * dst, int64_t batch_offset,
  5017. int64_t offset_delta, int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH, int64_t pelements, int64_t CHW,
  5018. int s0, int s1, int p0, int p1, int d0, int d1) {
  5019. const int64_t i = threadIdx.x + blockIdx.x * blockDim.x;
  5020. if (i >= pelements) {
  5021. return;
  5022. }
  5023. const int64_t ksize = OW * (KH > 1 ? KW : 1);
  5024. const int64_t kx = i / ksize;
  5025. const int64_t kd = kx * ksize;
  5026. const int64_t ky = (i - kd) / OW;
  5027. const int64_t ix = i % OW;
  5028. const int64_t oh = blockIdx.y;
  5029. const int64_t batch = blockIdx.z / IC;
  5030. const int64_t ic = blockIdx.z % IC;
  5031. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5032. const int64_t iih = oh * s1 + ky * d1 - p1;
  5033. const int64_t offset_dst =
  5034. ((batch * OH + oh) * OW + ix) * CHW +
  5035. (ic * (KW * KH) + ky * KW + kx);
  5036. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5037. dst[offset_dst] = 0.0f;
  5038. } else {
  5039. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5040. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5041. }
  5042. }
  5043. template <typename Ti, typename To>
  5044. static __global__ void pool2d_nchw_kernel(
  5045. const int ih, const int iw, const int oh, const int ow,
  5046. const int kh, const int kw, const int sh, const int sw,
  5047. const int ph, const int pw, const int parallel_elements,
  5048. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5049. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5050. if (idx >= parallel_elements) {
  5051. return;
  5052. }
  5053. const int I_HW = ih * iw;
  5054. const int O_HW = oh * ow;
  5055. const int nc = idx / O_HW;
  5056. const int cur_oh = idx % O_HW / ow;
  5057. const int cur_ow = idx % O_HW % ow;
  5058. const Ti* i_ptr = src + nc * I_HW;
  5059. To* o_ptr = dst + nc * O_HW;
  5060. const int start_h = cur_oh * sh - ph;
  5061. const int bh = max(0, start_h);
  5062. const int eh = min(ih, start_h + kh);
  5063. const int start_w = cur_ow * sw - pw;
  5064. const int bw = max(0, start_w);
  5065. const int ew = min(iw, start_w + kw);
  5066. const To scale = 1. / (kh * kw);
  5067. To res = 0;
  5068. switch (op) {
  5069. case GGML_OP_POOL_AVG: res = 0; break;
  5070. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5071. }
  5072. for (int i = bh; i < eh; i += 1) {
  5073. for (int j = bw; j < ew; j += 1) {
  5074. #if __CUDA_ARCH__ >= 350
  5075. Ti cur = __ldg(i_ptr + i * iw + j);
  5076. #else
  5077. Ti cur = i_ptr[i * iw + j];
  5078. #endif
  5079. switch (op) {
  5080. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5081. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5082. }
  5083. }
  5084. }
  5085. o_ptr[cur_oh * ow + cur_ow] = res;
  5086. }
  5087. template<int qk, int qr, dequantize_kernel_t dq>
  5088. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5089. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5090. GGML_TENSOR_BINARY_OP_LOCALS
  5091. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5092. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5093. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5094. // strides in elements
  5095. //const size_t s0 = nb0 / ggml_element_size(dst);
  5096. const size_t s1 = nb1 / ggml_element_size(dst);
  5097. const size_t s2 = nb2 / ggml_element_size(dst);
  5098. const size_t s3 = nb3 / ggml_element_size(dst);
  5099. const size_t s10 = nb10 / ggml_element_size(src1);
  5100. const size_t s11 = nb11 / ggml_element_size(src1);
  5101. const size_t s12 = nb12 / ggml_element_size(src1);
  5102. //const size_t s13 = nb13 / ggml_element_size(src1);
  5103. GGML_ASSERT(ne00 % 2 == 0);
  5104. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5105. src0_dd, src1_dd, dst_dd,
  5106. ne00, /*ne01, ne02, ne03,*/
  5107. /*ne10, ne11,*/ ne12, /*ne13,*/
  5108. /* s0,*/ s1, s2, s3,
  5109. /* nb00,*/ nb01, nb02, nb03,
  5110. s10, s11, s12/*, s13*/);
  5111. (void) dst;
  5112. }
  5113. template<typename src0_t>
  5114. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5115. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5116. GGML_TENSOR_BINARY_OP_LOCALS
  5117. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5118. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5119. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5120. // strides in elements
  5121. //const size_t s0 = nb0 / ggml_element_size(dst);
  5122. const size_t s1 = nb1 / ggml_element_size(dst);
  5123. const size_t s2 = nb2 / ggml_element_size(dst);
  5124. const size_t s3 = nb3 / ggml_element_size(dst);
  5125. const size_t s10 = nb10 / ggml_element_size(src1);
  5126. const size_t s11 = nb11 / ggml_element_size(src1);
  5127. const size_t s12 = nb12 / ggml_element_size(src1);
  5128. //const size_t s13 = nb13 / ggml_element_size(src1);
  5129. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5130. src0_dd, src1_dd, dst_dd,
  5131. ne00, /*ne01, ne02, ne03,*/
  5132. /*ne10, ne11,*/ ne12, /*ne13,*/
  5133. /* s0,*/ s1, s2, s3,
  5134. /* nb00,*/ nb01, nb02, nb03,
  5135. s10, s11, s12/*, s13*/);
  5136. (void) dst;
  5137. }
  5138. template<float (*bin_op)(const float, const float)>
  5139. struct bin_bcast_cuda {
  5140. template<typename src0_t, typename src1_t, typename dst_t>
  5141. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5142. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5143. cudaStream_t stream) {
  5144. GGML_TENSOR_BINARY_OP_LOCALS
  5145. int nr0 = ne10/ne0;
  5146. int nr1 = ne11/ne1;
  5147. int nr2 = ne12/ne2;
  5148. int nr3 = ne13/ne3;
  5149. int nr[4] = { nr0, nr1, nr2, nr3 };
  5150. // collapse dimensions until first broadcast dimension
  5151. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5152. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5153. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5154. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5155. auto collapse = [](int64_t cne[]) {
  5156. cne[0] *= cne[1];
  5157. cne[1] = cne[2];
  5158. cne[2] = cne[3];
  5159. cne[3] = 1;
  5160. };
  5161. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5162. cnb[1] *= cne[1];
  5163. cnb[2] *= cne[2];
  5164. cnb[3] *= cne[3];
  5165. };
  5166. for (int i = 0; i < 4; i++) {
  5167. if (nr[i] != 1) {
  5168. break;
  5169. }
  5170. if (i > 0) {
  5171. collapse_nb(cnb0, cne0);
  5172. collapse_nb(cnb1, cne1);
  5173. collapse(cne0);
  5174. collapse(cne1);
  5175. }
  5176. }
  5177. {
  5178. int64_t ne0 = cne0[0];
  5179. int64_t ne1 = cne0[1];
  5180. int64_t ne2 = cne0[2];
  5181. int64_t ne3 = cne0[3];
  5182. int64_t ne10 = cne1[0];
  5183. int64_t ne11 = cne1[1];
  5184. int64_t ne12 = cne1[2];
  5185. int64_t ne13 = cne1[3];
  5186. size_t nb0 = cnb0[0];
  5187. size_t nb1 = cnb0[1];
  5188. size_t nb2 = cnb0[2];
  5189. size_t nb3 = cnb0[3];
  5190. size_t nb10 = cnb1[0];
  5191. size_t nb11 = cnb1[1];
  5192. size_t nb12 = cnb1[2];
  5193. size_t nb13 = cnb1[3];
  5194. size_t s0 = nb0 / sizeof(dst_t);
  5195. size_t s1 = nb1 / sizeof(dst_t);
  5196. size_t s2 = nb2 / sizeof(dst_t);
  5197. size_t s3 = nb3 / sizeof(dst_t);
  5198. size_t s10 = nb10 / sizeof(src1_t);
  5199. size_t s11 = nb11 / sizeof(src1_t);
  5200. size_t s12 = nb12 / sizeof(src1_t);
  5201. size_t s13 = nb13 / sizeof(src1_t);
  5202. GGML_ASSERT(s0 == 1);
  5203. GGML_ASSERT(s10 == 1);
  5204. const int block_size = 128;
  5205. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5206. dim3 block_dims;
  5207. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5208. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5209. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5210. dim3 block_nums(
  5211. (hne0 + block_dims.x - 1) / block_dims.x,
  5212. (ne1 + block_dims.y - 1) / block_dims.y,
  5213. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5214. );
  5215. if (block_nums.z > 65535) {
  5216. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5217. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5218. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5219. src0_dd, src1_dd, dst_dd,
  5220. ne0, ne1, ne2, ne3,
  5221. ne10, ne11, ne12, ne13,
  5222. /* s0, */ s1, s2, s3,
  5223. /* s10, */ s11, s12, s13);
  5224. } else {
  5225. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5226. src0_dd, src1_dd, dst_dd,
  5227. ne0, ne1, ne2, ne3,
  5228. ne10, ne11, ne12, ne13,
  5229. /* s0, */ s1, s2, s3,
  5230. /* s10, */ s11, s12, s13);
  5231. }
  5232. }
  5233. }
  5234. };
  5235. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5236. const int ne10, const int ne11, const int ne12,
  5237. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5238. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5239. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5240. }
  5241. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5242. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5243. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5244. }
  5245. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5246. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5247. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5248. }
  5249. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5250. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5251. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5252. }
  5253. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5254. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5255. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5256. }
  5257. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5258. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5259. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5260. }
  5261. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5262. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5263. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5264. }
  5265. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5266. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5267. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5268. }
  5269. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5270. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5271. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5272. }
  5273. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5274. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5275. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5276. }
  5277. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5278. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5279. if (ncols < 1024) {
  5280. const dim3 block_dims(WARP_SIZE, 1, 1);
  5281. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5282. } else {
  5283. const dim3 block_dims(1024, 1, 1);
  5284. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5285. }
  5286. }
  5287. static void group_norm_f32_cuda(const float * x, float * dst, const int num_groups, const int group_size, const int ne_elements, cudaStream_t stream) {
  5288. static const float eps = 1e-6f;
  5289. if (group_size < 1024) {
  5290. const dim3 block_dims(WARP_SIZE, 1, 1);
  5291. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5292. } else {
  5293. const dim3 block_dims(1024, 1, 1);
  5294. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5295. }
  5296. }
  5297. static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
  5298. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5299. dim3 gridDim(num_blocks, ne1, ne2);
  5300. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5301. }
  5302. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int ne03,
  5303. const int scale_factor, cudaStream_t stream) {
  5304. int ne0 = (ne00 * scale_factor);
  5305. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5306. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02*ne03);
  5307. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  5308. }
  5309. static void pad_f32_cuda(const float * x, float * dst,
  5310. const int ne00, const int ne01, const int ne02, const int ne03,
  5311. const int ne0, const int ne1, const int ne2, const int ne3, cudaStream_t stream) {
  5312. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  5313. dim3 gridDim(num_blocks, ne1, ne2*ne3);
  5314. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02, ne03);
  5315. }
  5316. static void arange_f32_cuda(float * dst, const int ne0, const float start, const float step, cudaStream_t stream) {
  5317. int num_blocks = (ne0 + CUDA_ARANGE_BLOCK_SIZE - 1) / CUDA_ARANGE_BLOCK_SIZE;
  5318. arange_f32<<<num_blocks, CUDA_ARANGE_BLOCK_SIZE, 0, stream>>>(dst, ne0, start, step);
  5319. }
  5320. static void timestep_embedding_f32_cuda(const float * x, float * dst, const int ne00, const int nb1,
  5321. const int dim, const int max_period, cudaStream_t stream) {
  5322. int half_ceil = (dim + 1) / 2;
  5323. int num_blocks = (half_ceil + CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE - 1) / CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE;
  5324. dim3 gridDim(num_blocks, ne00, 1);
  5325. timestep_embedding_f32<<<gridDim, CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE, 0, stream>>>(x, dst, nb1, dim, max_period);
  5326. }
  5327. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5328. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5329. if (ncols < 1024) {
  5330. const dim3 block_dims(WARP_SIZE, 1, 1);
  5331. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5332. } else {
  5333. const dim3 block_dims(1024, 1, 1);
  5334. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5335. }
  5336. }
  5337. 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) {
  5338. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  5339. const dim3 num_blocks(block_num_x, ky, 1);
  5340. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  5341. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  5342. }
  5343. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5344. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5345. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  5346. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5347. }
  5348. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  5349. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  5350. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  5351. const bool need_check = false;
  5352. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5353. } else {
  5354. const bool need_check = true;
  5355. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5356. }
  5357. }
  5358. template<typename dst_t>
  5359. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5360. const int nb = k / QK_K;
  5361. #if QK_K == 256
  5362. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  5363. #else
  5364. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  5365. #endif
  5366. }
  5367. template<typename dst_t>
  5368. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5369. const int nb = k / QK_K;
  5370. #if QK_K == 256
  5371. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  5372. #else
  5373. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  5374. #endif
  5375. }
  5376. template<typename dst_t>
  5377. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5378. const int nb32 = k / 32;
  5379. const int nb = (k + 255) / 256;
  5380. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5381. }
  5382. template<typename dst_t>
  5383. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5384. const int nb32 = k / 32;
  5385. const int nb = (k + 255) / 256;
  5386. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5387. }
  5388. template<typename dst_t>
  5389. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5390. const int nb = k / QK_K;
  5391. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  5392. }
  5393. template<typename dst_t>
  5394. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5395. const int nb = k / QK_K;
  5396. #if QK_K == 256
  5397. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  5398. #else
  5399. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  5400. #endif
  5401. }
  5402. template<typename dst_t>
  5403. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5404. const int nb = k / QK_K;
  5405. #if QK_K == 256
  5406. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  5407. #else
  5408. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  5409. #endif
  5410. }
  5411. template<typename dst_t>
  5412. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5413. const int nb = k / QK_K;
  5414. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5415. }
  5416. template<typename dst_t>
  5417. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5418. const int nb = k / QK_K;
  5419. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  5420. }
  5421. template<typename dst_t>
  5422. static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5423. const int nb = k / QK_K;
  5424. dequantize_block_iq2_s<<<nb, 32, 0, stream>>>(vx, y);
  5425. }
  5426. template<typename dst_t>
  5427. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5428. const int nb = k / QK_K;
  5429. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5430. }
  5431. template<typename dst_t>
  5432. static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5433. const int nb = k / QK_K;
  5434. dequantize_block_iq3_s<<<nb, 32, 0, stream>>>(vx, y);
  5435. }
  5436. template<typename dst_t>
  5437. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5438. const int nb = k / QK_K;
  5439. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  5440. }
  5441. template<typename dst_t>
  5442. static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5443. const int nb = (k + QK_K - 1) / QK_K;
  5444. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  5445. }
  5446. template<typename dst_t>
  5447. static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5448. const int nb = (k + QK_K - 1) / QK_K;
  5449. #if QK_K == 64
  5450. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  5451. #else
  5452. dequantize_block_iq4_xs<<<nb, 32, 0, stream>>>(vx, y);
  5453. #endif
  5454. }
  5455. template <typename src_t, typename dst_t>
  5456. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5457. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  5458. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5459. }
  5460. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  5461. int id;
  5462. switch (type) {
  5463. case GGML_TYPE_Q4_0:
  5464. return dequantize_row_q4_0_cuda;
  5465. case GGML_TYPE_Q4_1:
  5466. return dequantize_row_q4_1_cuda;
  5467. case GGML_TYPE_Q5_0:
  5468. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5469. case GGML_TYPE_Q5_1:
  5470. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5471. case GGML_TYPE_Q8_0:
  5472. CUDA_CHECK(cudaGetDevice(&id));
  5473. if (g_device_caps[id].cc >= CC_PASCAL) {
  5474. return dequantize_block_q8_0_f16_cuda;
  5475. }
  5476. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5477. case GGML_TYPE_Q2_K:
  5478. return dequantize_row_q2_K_cuda;
  5479. case GGML_TYPE_Q3_K:
  5480. return dequantize_row_q3_K_cuda;
  5481. case GGML_TYPE_Q4_K:
  5482. return dequantize_row_q4_K_cuda;
  5483. case GGML_TYPE_Q5_K:
  5484. return dequantize_row_q5_K_cuda;
  5485. case GGML_TYPE_Q6_K:
  5486. return dequantize_row_q6_K_cuda;
  5487. case GGML_TYPE_IQ2_XXS:
  5488. return dequantize_row_iq2_xxs_cuda;
  5489. case GGML_TYPE_IQ2_XS:
  5490. return dequantize_row_iq2_xs_cuda;
  5491. case GGML_TYPE_IQ2_S:
  5492. return dequantize_row_iq2_s_cuda;
  5493. case GGML_TYPE_IQ3_XXS:
  5494. return dequantize_row_iq3_xxs_cuda;
  5495. case GGML_TYPE_IQ1_S:
  5496. return dequantize_row_iq1_s_cuda;
  5497. case GGML_TYPE_IQ4_NL:
  5498. return dequantize_row_iq4_nl_cuda;
  5499. case GGML_TYPE_IQ4_XS:
  5500. return dequantize_row_iq4_xs_cuda;
  5501. case GGML_TYPE_IQ3_S:
  5502. return dequantize_row_iq3_s_cuda;
  5503. case GGML_TYPE_F32:
  5504. return convert_unary_cuda<float>;
  5505. default:
  5506. return nullptr;
  5507. }
  5508. }
  5509. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  5510. switch (type) {
  5511. case GGML_TYPE_Q4_0:
  5512. return dequantize_row_q4_0_cuda;
  5513. case GGML_TYPE_Q4_1:
  5514. return dequantize_row_q4_1_cuda;
  5515. case GGML_TYPE_Q5_0:
  5516. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5517. case GGML_TYPE_Q5_1:
  5518. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5519. case GGML_TYPE_Q8_0:
  5520. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5521. case GGML_TYPE_Q2_K:
  5522. return dequantize_row_q2_K_cuda;
  5523. case GGML_TYPE_Q3_K:
  5524. return dequantize_row_q3_K_cuda;
  5525. case GGML_TYPE_Q4_K:
  5526. return dequantize_row_q4_K_cuda;
  5527. case GGML_TYPE_Q5_K:
  5528. return dequantize_row_q5_K_cuda;
  5529. case GGML_TYPE_Q6_K:
  5530. return dequantize_row_q6_K_cuda;
  5531. case GGML_TYPE_IQ2_XXS:
  5532. return dequantize_row_iq2_xxs_cuda;
  5533. case GGML_TYPE_IQ2_XS:
  5534. return dequantize_row_iq2_xs_cuda;
  5535. case GGML_TYPE_IQ2_S:
  5536. return dequantize_row_iq2_s_cuda;
  5537. case GGML_TYPE_IQ3_XXS:
  5538. return dequantize_row_iq3_xxs_cuda;
  5539. case GGML_TYPE_IQ1_S:
  5540. return dequantize_row_iq1_s_cuda;
  5541. case GGML_TYPE_IQ4_NL:
  5542. return dequantize_row_iq4_nl_cuda;
  5543. case GGML_TYPE_IQ4_XS:
  5544. return dequantize_row_iq4_xs_cuda;
  5545. case GGML_TYPE_IQ3_S:
  5546. return dequantize_row_iq3_s_cuda;
  5547. case GGML_TYPE_F16:
  5548. return convert_unary_cuda<half>;
  5549. default:
  5550. return nullptr;
  5551. }
  5552. }
  5553. 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) {
  5554. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5555. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5556. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  5557. const dim3 block_nums(block_num_y, 1, 1);
  5558. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5559. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  5560. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5561. }
  5562. 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) {
  5563. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5564. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5565. const dim3 block_nums(block_num_y, 1, 1);
  5566. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5567. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  5568. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5569. }
  5570. 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) {
  5571. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5572. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5573. const dim3 block_nums(block_num_y, 1, 1);
  5574. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5575. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  5576. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5577. }
  5578. 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) {
  5579. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5580. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5581. const dim3 block_nums(block_num_y, 1, 1);
  5582. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5583. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  5584. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5585. }
  5586. 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) {
  5587. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5588. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5589. const dim3 block_nums(block_num_y, 1, 1);
  5590. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5591. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  5592. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5593. }
  5594. 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) {
  5595. GGML_ASSERT(ncols % QK_K == 0);
  5596. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  5597. const int block_num_y = (nrows + ny - 1) / ny;
  5598. const dim3 block_nums(block_num_y, 1, 1);
  5599. const dim3 block_dims(32, ny, 1);
  5600. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5601. }
  5602. 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) {
  5603. GGML_ASSERT(ncols % QK_K == 0);
  5604. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5605. const int block_num_y = (nrows + ny - 1) / ny;
  5606. const dim3 block_nums(block_num_y, 1, 1);
  5607. const dim3 block_dims(32, ny, 1);
  5608. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5609. }
  5610. 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) {
  5611. GGML_ASSERT(ncols % QK_K == 0);
  5612. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5613. const int block_num_y = (nrows + ny - 1) / ny;
  5614. const dim3 block_nums(block_num_y, 1, 1);
  5615. const dim3 block_dims(32, ny, 1);
  5616. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5617. }
  5618. 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) {
  5619. GGML_ASSERT(ncols % QK_K == 0);
  5620. const dim3 block_dims(32, 1, 1);
  5621. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  5622. }
  5623. 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) {
  5624. GGML_ASSERT(ncols % QK_K == 0);
  5625. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5626. const int block_num_y = (nrows + ny - 1) / ny;
  5627. const dim3 block_nums(block_num_y, 1, 1);
  5628. const dim3 block_dims(32, ny, 1);
  5629. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5630. }
  5631. 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) {
  5632. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5633. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5634. const dim3 block_nums(block_num_y, 1, 1);
  5635. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5636. dequantize_mul_mat_vec<1, 1, convert_f16>
  5637. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5638. }
  5639. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  5640. static void mul_mat_vec_q_cuda(
  5641. const void * vx, const void * vy, float * dst,
  5642. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  5643. GGML_ASSERT(ncols_x % qk == 0);
  5644. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  5645. int id;
  5646. CUDA_CHECK(cudaGetDevice(&id));
  5647. int64_t nwarps = 1;
  5648. int64_t rows_per_cuda_block = 1;
  5649. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  5650. switch(ncols_y) {
  5651. case 1:
  5652. nwarps = 4;
  5653. rows_per_cuda_block = 1;
  5654. break;
  5655. case 2:
  5656. case 3:
  5657. case 4:
  5658. nwarps = 4;
  5659. rows_per_cuda_block = 2;
  5660. break;
  5661. case 5:
  5662. case 6:
  5663. case 7:
  5664. case 8:
  5665. nwarps = 2;
  5666. rows_per_cuda_block = 2;
  5667. break;
  5668. default:
  5669. GGML_ASSERT(false);
  5670. break;
  5671. }
  5672. }
  5673. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  5674. const dim3 block_nums(nblocks, 1, 1);
  5675. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5676. switch (ncols_y) {
  5677. case 1:
  5678. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  5679. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5680. break;
  5681. case 2:
  5682. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  5683. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5684. break;
  5685. case 3:
  5686. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  5687. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5688. break;
  5689. case 4:
  5690. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  5691. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5692. break;
  5693. case 5:
  5694. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  5695. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5696. break;
  5697. case 6:
  5698. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  5699. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5700. break;
  5701. case 7:
  5702. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  5703. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5704. break;
  5705. case 8:
  5706. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  5707. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5708. break;
  5709. default:
  5710. GGML_ASSERT(false);
  5711. break;
  5712. }
  5713. }
  5714. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5715. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5716. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5717. int id;
  5718. CUDA_CHECK(cudaGetDevice(&id));
  5719. const int compute_capability = g_device_caps[id].cc;
  5720. int mmq_x, mmq_y, nwarps;
  5721. if (compute_capability >= CC_RDNA2) {
  5722. mmq_x = MMQ_X_Q4_0_RDNA2;
  5723. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5724. nwarps = NWARPS_Q4_0_RDNA2;
  5725. } else if (compute_capability >= CC_OFFSET_AMD) {
  5726. mmq_x = MMQ_X_Q4_0_RDNA1;
  5727. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5728. nwarps = NWARPS_Q4_0_RDNA1;
  5729. } else if (compute_capability >= CC_VOLTA) {
  5730. mmq_x = MMQ_X_Q4_0_AMPERE;
  5731. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5732. nwarps = NWARPS_Q4_0_AMPERE;
  5733. } else if (compute_capability >= MIN_CC_DP4A) {
  5734. mmq_x = MMQ_X_Q4_0_PASCAL;
  5735. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5736. nwarps = NWARPS_Q4_0_PASCAL;
  5737. } else {
  5738. GGML_ASSERT(false);
  5739. }
  5740. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5741. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5742. const dim3 block_nums(block_num_x, block_num_y, 1);
  5743. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5744. if (nrows_x % mmq_y == 0) {
  5745. const bool need_check = false;
  5746. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5747. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5748. } else {
  5749. const bool need_check = true;
  5750. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5751. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5752. }
  5753. }
  5754. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5755. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5756. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5757. int id;
  5758. CUDA_CHECK(cudaGetDevice(&id));
  5759. const int compute_capability = g_device_caps[id].cc;
  5760. int mmq_x, mmq_y, nwarps;
  5761. if (compute_capability >= CC_RDNA2) {
  5762. mmq_x = MMQ_X_Q4_1_RDNA2;
  5763. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5764. nwarps = NWARPS_Q4_1_RDNA2;
  5765. } else if (compute_capability >= CC_OFFSET_AMD) {
  5766. mmq_x = MMQ_X_Q4_1_RDNA1;
  5767. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5768. nwarps = NWARPS_Q4_1_RDNA1;
  5769. } else if (compute_capability >= CC_VOLTA) {
  5770. mmq_x = MMQ_X_Q4_1_AMPERE;
  5771. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5772. nwarps = NWARPS_Q4_1_AMPERE;
  5773. } else if (compute_capability >= MIN_CC_DP4A) {
  5774. mmq_x = MMQ_X_Q4_1_PASCAL;
  5775. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5776. nwarps = NWARPS_Q4_1_PASCAL;
  5777. } else {
  5778. GGML_ASSERT(false);
  5779. }
  5780. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5781. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5782. const dim3 block_nums(block_num_x, block_num_y, 1);
  5783. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5784. if (nrows_x % mmq_y == 0) {
  5785. const bool need_check = false;
  5786. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5787. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5788. } else {
  5789. const bool need_check = true;
  5790. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5791. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5792. }
  5793. }
  5794. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5795. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5796. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5797. int id;
  5798. CUDA_CHECK(cudaGetDevice(&id));
  5799. const int compute_capability = g_device_caps[id].cc;
  5800. int mmq_x, mmq_y, nwarps;
  5801. if (compute_capability >= CC_RDNA2) {
  5802. mmq_x = MMQ_X_Q5_0_RDNA2;
  5803. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5804. nwarps = NWARPS_Q5_0_RDNA2;
  5805. } else if (compute_capability >= CC_OFFSET_AMD) {
  5806. mmq_x = MMQ_X_Q5_0_RDNA1;
  5807. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5808. nwarps = NWARPS_Q5_0_RDNA1;
  5809. } else if (compute_capability >= CC_VOLTA) {
  5810. mmq_x = MMQ_X_Q5_0_AMPERE;
  5811. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5812. nwarps = NWARPS_Q5_0_AMPERE;
  5813. } else if (compute_capability >= MIN_CC_DP4A) {
  5814. mmq_x = MMQ_X_Q5_0_PASCAL;
  5815. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5816. nwarps = NWARPS_Q5_0_PASCAL;
  5817. } else {
  5818. GGML_ASSERT(false);
  5819. }
  5820. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5821. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5822. const dim3 block_nums(block_num_x, block_num_y, 1);
  5823. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5824. if (nrows_x % mmq_y == 0) {
  5825. const bool need_check = false;
  5826. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5827. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5828. } else {
  5829. const bool need_check = true;
  5830. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5831. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5832. }
  5833. }
  5834. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5835. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5836. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5837. int id;
  5838. CUDA_CHECK(cudaGetDevice(&id));
  5839. const int compute_capability = g_device_caps[id].cc;
  5840. int mmq_x, mmq_y, nwarps;
  5841. if (compute_capability >= CC_RDNA2) {
  5842. mmq_x = MMQ_X_Q5_1_RDNA2;
  5843. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5844. nwarps = NWARPS_Q5_1_RDNA2;
  5845. } else if (compute_capability >= CC_OFFSET_AMD) {
  5846. mmq_x = MMQ_X_Q5_1_RDNA1;
  5847. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5848. nwarps = NWARPS_Q5_1_RDNA1;
  5849. } else if (compute_capability >= CC_VOLTA) {
  5850. mmq_x = MMQ_X_Q5_1_AMPERE;
  5851. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5852. nwarps = NWARPS_Q5_1_AMPERE;
  5853. } else if (compute_capability >= MIN_CC_DP4A) {
  5854. mmq_x = MMQ_X_Q5_1_PASCAL;
  5855. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5856. nwarps = NWARPS_Q5_1_PASCAL;
  5857. } else {
  5858. GGML_ASSERT(false);
  5859. }
  5860. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5861. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5862. const dim3 block_nums(block_num_x, block_num_y, 1);
  5863. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5864. if (nrows_x % mmq_y == 0) {
  5865. const bool need_check = false;
  5866. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5867. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5868. } else {
  5869. const bool need_check = true;
  5870. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5871. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5872. }
  5873. }
  5874. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5875. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5876. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5877. int id;
  5878. CUDA_CHECK(cudaGetDevice(&id));
  5879. const int compute_capability = g_device_caps[id].cc;
  5880. int mmq_x, mmq_y, nwarps;
  5881. if (compute_capability >= CC_RDNA2) {
  5882. mmq_x = MMQ_X_Q8_0_RDNA2;
  5883. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5884. nwarps = NWARPS_Q8_0_RDNA2;
  5885. } else if (compute_capability >= CC_OFFSET_AMD) {
  5886. mmq_x = MMQ_X_Q8_0_RDNA1;
  5887. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5888. nwarps = NWARPS_Q8_0_RDNA1;
  5889. } else if (compute_capability >= CC_VOLTA) {
  5890. mmq_x = MMQ_X_Q8_0_AMPERE;
  5891. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5892. nwarps = NWARPS_Q8_0_AMPERE;
  5893. } else if (compute_capability >= MIN_CC_DP4A) {
  5894. mmq_x = MMQ_X_Q8_0_PASCAL;
  5895. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5896. nwarps = NWARPS_Q8_0_PASCAL;
  5897. } else {
  5898. GGML_ASSERT(false);
  5899. }
  5900. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5901. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5902. const dim3 block_nums(block_num_x, block_num_y, 1);
  5903. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5904. if (nrows_x % mmq_y == 0) {
  5905. const bool need_check = false;
  5906. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5907. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5908. } else {
  5909. const bool need_check = true;
  5910. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5911. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5912. }
  5913. }
  5914. static void ggml_mul_mat_q2_K_q8_1_cuda(
  5915. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5916. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5917. int id;
  5918. CUDA_CHECK(cudaGetDevice(&id));
  5919. const int compute_capability = g_device_caps[id].cc;
  5920. int mmq_x, mmq_y, nwarps;
  5921. if (compute_capability >= CC_RDNA2) {
  5922. mmq_x = MMQ_X_Q2_K_RDNA2;
  5923. mmq_y = MMQ_Y_Q2_K_RDNA2;
  5924. nwarps = NWARPS_Q2_K_RDNA2;
  5925. } else if (compute_capability >= CC_OFFSET_AMD) {
  5926. mmq_x = MMQ_X_Q2_K_RDNA1;
  5927. mmq_y = MMQ_Y_Q2_K_RDNA1;
  5928. nwarps = NWARPS_Q2_K_RDNA1;
  5929. } else if (compute_capability >= CC_VOLTA) {
  5930. mmq_x = MMQ_X_Q2_K_AMPERE;
  5931. mmq_y = MMQ_Y_Q2_K_AMPERE;
  5932. nwarps = NWARPS_Q2_K_AMPERE;
  5933. } else if (compute_capability >= MIN_CC_DP4A) {
  5934. mmq_x = MMQ_X_Q2_K_PASCAL;
  5935. mmq_y = MMQ_Y_Q2_K_PASCAL;
  5936. nwarps = NWARPS_Q2_K_PASCAL;
  5937. } else {
  5938. GGML_ASSERT(false);
  5939. }
  5940. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5941. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5942. const dim3 block_nums(block_num_x, block_num_y, 1);
  5943. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5944. if (nrows_x % mmq_y == 0) {
  5945. const bool need_check = false;
  5946. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5947. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5948. } else {
  5949. const bool need_check = true;
  5950. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5951. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5952. }
  5953. }
  5954. static void ggml_mul_mat_q3_K_q8_1_cuda(
  5955. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5956. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5957. #if QK_K == 256
  5958. int id;
  5959. CUDA_CHECK(cudaGetDevice(&id));
  5960. const int compute_capability = g_device_caps[id].cc;
  5961. int mmq_x, mmq_y, nwarps;
  5962. if (compute_capability >= CC_RDNA2) {
  5963. mmq_x = MMQ_X_Q3_K_RDNA2;
  5964. mmq_y = MMQ_Y_Q3_K_RDNA2;
  5965. nwarps = NWARPS_Q3_K_RDNA2;
  5966. } else if (compute_capability >= CC_OFFSET_AMD) {
  5967. mmq_x = MMQ_X_Q3_K_RDNA1;
  5968. mmq_y = MMQ_Y_Q3_K_RDNA1;
  5969. nwarps = NWARPS_Q3_K_RDNA1;
  5970. } else if (compute_capability >= CC_VOLTA) {
  5971. mmq_x = MMQ_X_Q3_K_AMPERE;
  5972. mmq_y = MMQ_Y_Q3_K_AMPERE;
  5973. nwarps = NWARPS_Q3_K_AMPERE;
  5974. } else if (compute_capability >= MIN_CC_DP4A) {
  5975. mmq_x = MMQ_X_Q3_K_PASCAL;
  5976. mmq_y = MMQ_Y_Q3_K_PASCAL;
  5977. nwarps = NWARPS_Q3_K_PASCAL;
  5978. } else {
  5979. GGML_ASSERT(false);
  5980. }
  5981. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5982. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5983. const dim3 block_nums(block_num_x, block_num_y, 1);
  5984. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5985. if (nrows_x % mmq_y == 0) {
  5986. const bool need_check = false;
  5987. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5988. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5989. } else {
  5990. const bool need_check = true;
  5991. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5992. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5993. }
  5994. #endif
  5995. }
  5996. static void ggml_mul_mat_q4_K_q8_1_cuda(
  5997. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5998. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5999. int id;
  6000. CUDA_CHECK(cudaGetDevice(&id));
  6001. const int compute_capability = g_device_caps[id].cc;
  6002. int mmq_x, mmq_y, nwarps;
  6003. if (compute_capability >= CC_RDNA2) {
  6004. mmq_x = MMQ_X_Q4_K_RDNA2;
  6005. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6006. nwarps = NWARPS_Q4_K_RDNA2;
  6007. } else if (compute_capability >= CC_OFFSET_AMD) {
  6008. mmq_x = MMQ_X_Q4_K_RDNA1;
  6009. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6010. nwarps = NWARPS_Q4_K_RDNA1;
  6011. } else if (compute_capability >= CC_VOLTA) {
  6012. mmq_x = MMQ_X_Q4_K_AMPERE;
  6013. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6014. nwarps = NWARPS_Q4_K_AMPERE;
  6015. } else if (compute_capability >= MIN_CC_DP4A) {
  6016. mmq_x = MMQ_X_Q4_K_PASCAL;
  6017. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6018. nwarps = NWARPS_Q4_K_PASCAL;
  6019. } else {
  6020. GGML_ASSERT(false);
  6021. }
  6022. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6023. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6024. const dim3 block_nums(block_num_x, block_num_y, 1);
  6025. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6026. if (nrows_x % mmq_y == 0) {
  6027. const bool need_check = false;
  6028. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6029. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6030. } else {
  6031. const bool need_check = true;
  6032. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6033. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6034. }
  6035. }
  6036. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6037. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6038. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6039. int id;
  6040. CUDA_CHECK(cudaGetDevice(&id));
  6041. const int compute_capability = g_device_caps[id].cc;
  6042. int mmq_x, mmq_y, nwarps;
  6043. if (compute_capability >= CC_RDNA2) {
  6044. mmq_x = MMQ_X_Q5_K_RDNA2;
  6045. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6046. nwarps = NWARPS_Q5_K_RDNA2;
  6047. } else if (compute_capability >= CC_OFFSET_AMD) {
  6048. mmq_x = MMQ_X_Q5_K_RDNA1;
  6049. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6050. nwarps = NWARPS_Q5_K_RDNA1;
  6051. } else if (compute_capability >= CC_VOLTA) {
  6052. mmq_x = MMQ_X_Q5_K_AMPERE;
  6053. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6054. nwarps = NWARPS_Q5_K_AMPERE;
  6055. } else if (compute_capability >= MIN_CC_DP4A) {
  6056. mmq_x = MMQ_X_Q5_K_PASCAL;
  6057. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6058. nwarps = NWARPS_Q5_K_PASCAL;
  6059. } else {
  6060. GGML_ASSERT(false);
  6061. }
  6062. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6063. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6064. const dim3 block_nums(block_num_x, block_num_y, 1);
  6065. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6066. if (nrows_x % mmq_y == 0) {
  6067. const bool need_check = false;
  6068. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6069. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6070. } else {
  6071. const bool need_check = true;
  6072. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6073. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6074. }
  6075. }
  6076. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6077. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6078. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6079. int id;
  6080. CUDA_CHECK(cudaGetDevice(&id));
  6081. const int compute_capability = g_device_caps[id].cc;
  6082. int mmq_x, mmq_y, nwarps;
  6083. if (compute_capability >= CC_RDNA2) {
  6084. mmq_x = MMQ_X_Q6_K_RDNA2;
  6085. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6086. nwarps = NWARPS_Q6_K_RDNA2;
  6087. } else if (compute_capability >= CC_OFFSET_AMD) {
  6088. mmq_x = MMQ_X_Q6_K_RDNA1;
  6089. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6090. nwarps = NWARPS_Q6_K_RDNA1;
  6091. } else if (compute_capability >= CC_VOLTA) {
  6092. mmq_x = MMQ_X_Q6_K_AMPERE;
  6093. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6094. nwarps = NWARPS_Q6_K_AMPERE;
  6095. } else if (compute_capability >= MIN_CC_DP4A) {
  6096. mmq_x = MMQ_X_Q6_K_PASCAL;
  6097. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6098. nwarps = NWARPS_Q6_K_PASCAL;
  6099. } else {
  6100. GGML_ASSERT(false);
  6101. }
  6102. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6103. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6104. const dim3 block_nums(block_num_x, block_num_y, 1);
  6105. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6106. if (nrows_x % mmq_y == 0) {
  6107. const bool need_check = false;
  6108. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6109. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6110. } else {
  6111. const bool need_check = true;
  6112. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6113. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6114. }
  6115. }
  6116. static void ggml_mul_mat_p021_f16_f32_cuda(
  6117. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6118. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6119. const dim3 block_nums(1, nrows_x, nchannels_y);
  6120. const dim3 block_dims(WARP_SIZE, 1, 1);
  6121. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6122. }
  6123. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6124. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6125. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6126. const dim3 block_nums(1, nrows_x, nchannels_y);
  6127. const dim3 block_dims(WARP_SIZE, 1, 1);
  6128. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6129. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6130. }
  6131. static void ggml_cpy_f16_f32_cuda(
  6132. const char * cx, char * cdst, const int ne,
  6133. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6134. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6135. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6136. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6137. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6138. }
  6139. static void ggml_cpy_f32_f32_cuda(
  6140. const char * cx, char * cdst, const int ne,
  6141. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6142. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6143. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6144. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6145. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6146. }
  6147. static void ggml_cpy_f32_f16_cuda(
  6148. const char * cx, char * cdst, const int ne,
  6149. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6150. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6151. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6152. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6153. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6154. }
  6155. static void ggml_cpy_f32_q8_0_cuda(
  6156. const char * cx, char * cdst, const int ne,
  6157. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6158. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6159. GGML_ASSERT(ne % QK8_0 == 0);
  6160. const int num_blocks = ne / QK8_0;
  6161. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6162. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6163. }
  6164. static void ggml_cpy_f32_q4_0_cuda(
  6165. const char * cx, char * cdst, const int ne,
  6166. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6167. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6168. GGML_ASSERT(ne % QK4_0 == 0);
  6169. const int num_blocks = ne / QK4_0;
  6170. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6171. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6172. }
  6173. static void ggml_cpy_f32_q4_1_cuda(
  6174. const char * cx, char * cdst, const int ne,
  6175. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6176. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6177. GGML_ASSERT(ne % QK4_1 == 0);
  6178. const int num_blocks = ne / QK4_1;
  6179. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6180. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6181. }
  6182. static void ggml_cpy_f16_f16_cuda(
  6183. const char * cx, char * cdst, const int ne,
  6184. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6185. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6186. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6187. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6188. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6189. }
  6190. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  6191. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  6192. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  6193. }
  6194. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  6195. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  6196. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  6197. }
  6198. template<typename T>
  6199. static void rope_cuda(
  6200. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6201. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6202. ) {
  6203. GGML_ASSERT(ncols % 2 == 0);
  6204. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6205. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6206. const dim3 block_nums(nrows, num_blocks_x, 1);
  6207. if (pos == nullptr) {
  6208. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6209. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6210. );
  6211. } else {
  6212. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6213. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6214. );
  6215. }
  6216. }
  6217. template<typename T>
  6218. static void rope_neox_cuda(
  6219. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6220. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6221. ) {
  6222. GGML_ASSERT(ncols % 2 == 0);
  6223. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6224. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6225. const dim3 block_nums(nrows, num_blocks_x, 1);
  6226. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6227. const float inv_ndims = -1.0f / n_dims;
  6228. if (pos == nullptr) {
  6229. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6230. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6231. theta_scale, inv_ndims
  6232. );
  6233. } else {
  6234. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6235. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6236. theta_scale, inv_ndims
  6237. );
  6238. }
  6239. }
  6240. static void rope_glm_f32_cuda(
  6241. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6242. float freq_base, int n_ctx, cudaStream_t stream
  6243. ) {
  6244. GGML_ASSERT(ncols % 4 == 0);
  6245. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6246. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6247. const dim3 block_nums(num_blocks_x, nrows, 1);
  6248. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6249. }
  6250. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6251. const int k_rows, const int n_heads_log2_floor, const float m0,
  6252. const float m1, cudaStream_t stream) {
  6253. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6254. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6255. const dim3 block_nums(num_blocks_x, nrows, 1);
  6256. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6257. }
  6258. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6259. const dim3 block_dims(WARP_SIZE, 1, 1);
  6260. const dim3 block_nums(nrows, 1, 1);
  6261. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6262. }
  6263. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6264. // bitonic sort requires ncols to be power of 2
  6265. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6266. const dim3 block_dims(ncols, 1, 1);
  6267. const dim3 block_nums(1, nrows, 1);
  6268. if (order == GGML_SORT_ORDER_ASC) {
  6269. k_argsort_f32_i32<GGML_SORT_ORDER_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6270. } else if (order == GGML_SORT_ORDER_DESC) {
  6271. k_argsort_f32_i32<GGML_SORT_ORDER_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6272. } else {
  6273. GGML_ASSERT(false);
  6274. }
  6275. }
  6276. 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) {
  6277. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6278. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6279. const dim3 block_nums(nrows_x, block_num_x, 1);
  6280. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6281. }
  6282. static void soft_max_f32_cuda(const float * x, const float * mask, const float * pos, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, const float max_bias, cudaStream_t stream) {
  6283. int nth = WARP_SIZE;
  6284. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6285. const dim3 block_dims(nth, 1, 1);
  6286. const dim3 block_nums(nrows_x, 1, 1);
  6287. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6288. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6289. const uint32_t n_head_kv = nrows_x/nrows_y;
  6290. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6291. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6292. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6293. if (shmem < g_device_caps[g_main_device].smpb) {
  6294. switch (ncols_x) {
  6295. case 32:
  6296. soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6297. break;
  6298. case 64:
  6299. soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6300. break;
  6301. case 128:
  6302. soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6303. break;
  6304. case 256:
  6305. soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6306. break;
  6307. case 512:
  6308. soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6309. break;
  6310. case 1024:
  6311. soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6312. break;
  6313. case 2048:
  6314. soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6315. break;
  6316. case 4096:
  6317. soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6318. break;
  6319. default:
  6320. soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6321. break;
  6322. }
  6323. } else {
  6324. const size_t shmem_low = WARP_SIZE*sizeof(float);
  6325. soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6326. }
  6327. }
  6328. template <typename T>
  6329. static void im2col_cuda(const float* x, T* dst,
  6330. int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
  6331. int64_t batch, int64_t batch_offset, int64_t offset_delta,
  6332. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  6333. const int parallel_elements = OW * KW * KH;
  6334. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  6335. dim3 block_nums(num_blocks, OH, batch * IC);
  6336. im2col_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  6337. }
  6338. // buffer pool for cuda
  6339. #define MAX_CUDA_BUFFERS 256
  6340. struct scoped_spin_lock {
  6341. std::atomic_flag& lock;
  6342. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  6343. while (lock.test_and_set(std::memory_order_acquire)) {
  6344. ; // spin
  6345. }
  6346. }
  6347. ~scoped_spin_lock() {
  6348. lock.clear(std::memory_order_release);
  6349. }
  6350. scoped_spin_lock(const scoped_spin_lock&) = delete;
  6351. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  6352. };
  6353. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  6354. // #define DEBUG_CUDA_MALLOC
  6355. struct ggml_cuda_buffer {
  6356. void * ptr = nullptr;
  6357. size_t size = 0;
  6358. };
  6359. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  6360. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  6361. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  6362. scoped_spin_lock lock(g_cuda_pool_lock);
  6363. #ifdef DEBUG_CUDA_MALLOC
  6364. int nnz = 0;
  6365. size_t max_size = 0;
  6366. #endif
  6367. size_t best_diff = 1ull << 36;
  6368. int ibest = -1;
  6369. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6370. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6371. if (b.ptr != nullptr) {
  6372. #ifdef DEBUG_CUDA_MALLOC
  6373. ++nnz;
  6374. if (b.size > max_size) max_size = b.size;
  6375. #endif
  6376. if (b.size >= size) {
  6377. size_t diff = b.size - size;
  6378. if (diff < best_diff) {
  6379. best_diff = diff;
  6380. ibest = i;
  6381. if (!best_diff) {
  6382. void * ptr = b.ptr;
  6383. *actual_size = b.size;
  6384. b.ptr = nullptr;
  6385. b.size = 0;
  6386. return ptr;
  6387. }
  6388. }
  6389. }
  6390. }
  6391. }
  6392. if (ibest >= 0) {
  6393. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  6394. void * ptr = b.ptr;
  6395. *actual_size = b.size;
  6396. b.ptr = nullptr;
  6397. b.size = 0;
  6398. return ptr;
  6399. }
  6400. void * ptr;
  6401. size_t look_ahead_size = (size_t) (1.05 * size);
  6402. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  6403. ggml_cuda_set_device(device);
  6404. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  6405. *actual_size = look_ahead_size;
  6406. g_cuda_pool_size[device] += look_ahead_size;
  6407. #ifdef DEBUG_CUDA_MALLOC
  6408. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, device, nnz,
  6409. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[device]/1024/1024), (uint32_t)(size/1024/1024));
  6410. #endif
  6411. return ptr;
  6412. }
  6413. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  6414. scoped_spin_lock lock(g_cuda_pool_lock);
  6415. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6416. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6417. if (b.ptr == nullptr) {
  6418. b.ptr = ptr;
  6419. b.size = size;
  6420. return;
  6421. }
  6422. }
  6423. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  6424. ggml_cuda_set_device(device);
  6425. CUDA_CHECK(cudaFree(ptr));
  6426. g_cuda_pool_size[device] -= size;
  6427. }
  6428. #if !defined(GGML_USE_HIPBLAS)
  6429. // pool with virtual memory
  6430. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  6431. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  6432. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  6433. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  6434. scoped_spin_lock lock(g_cuda_pool_lock);
  6435. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  6436. const size_t alignment = 128;
  6437. size = alignment * ((size + alignment - 1) / alignment);
  6438. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  6439. if (size > avail) {
  6440. // round up to the next multiple of the granularity
  6441. size_t reserve_size = size - avail;
  6442. const size_t granularity = g_device_caps[device].vmm_granularity;
  6443. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  6444. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  6445. // allocate more physical memory
  6446. CUmemAllocationProp prop = {};
  6447. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6448. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6449. prop.location.id = device;
  6450. CUmemGenericAllocationHandle handle;
  6451. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  6452. // reserve virtual address space (if not already reserved)
  6453. if (g_cuda_pool_addr[device] == 0) {
  6454. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  6455. }
  6456. // map at the end of the pool
  6457. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  6458. // the memory allocation handle is no longer needed after mapping
  6459. CU_CHECK(cuMemRelease(handle));
  6460. // set access
  6461. CUmemAccessDesc access = {};
  6462. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6463. access.location.id = device;
  6464. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  6465. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  6466. // add to the pool
  6467. g_cuda_pool_size[device] += reserve_size;
  6468. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  6469. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  6470. // (unsigned long long) (reserve_size/1024/1024));
  6471. }
  6472. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  6473. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  6474. *actual_size = size;
  6475. g_cuda_pool_used[device] += size;
  6476. #ifdef DEBUG_CUDA_MALLOC
  6477. printf("cuda pool[%d]: allocated %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  6478. #endif
  6479. return ptr;
  6480. }
  6481. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  6482. scoped_spin_lock lock(g_cuda_pool_lock);
  6483. #ifdef DEBUG_CUDA_MALLOC
  6484. printf("cuda pool[%d]: freed %llu bytes at %llx\n", device, (unsigned long long) size, ptr);
  6485. #endif
  6486. g_cuda_pool_used[device] -= size;
  6487. // all deallocations must be in reverse order of the allocations
  6488. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  6489. }
  6490. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  6491. if (g_device_caps[device].vmm) {
  6492. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  6493. } else {
  6494. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  6495. }
  6496. }
  6497. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  6498. if (g_device_caps[device].vmm) {
  6499. ggml_cuda_pool_free_vmm(device, ptr, size);
  6500. } else {
  6501. ggml_cuda_pool_free_leg(device, ptr, size);
  6502. }
  6503. }
  6504. #else
  6505. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  6506. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  6507. #endif // !defined(GGML_USE_HIPBLAS)
  6508. template<typename T>
  6509. struct cuda_pool_alloc {
  6510. int device = -1;
  6511. T * ptr = nullptr;
  6512. size_t actual_size = 0;
  6513. // size is in number of elements
  6514. T * alloc(size_t size) {
  6515. GGML_ASSERT(ptr == nullptr);
  6516. CUDA_CHECK(cudaGetDevice(&device));
  6517. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  6518. return ptr;
  6519. }
  6520. cuda_pool_alloc(size_t size) {
  6521. alloc(size);
  6522. }
  6523. ~cuda_pool_alloc() {
  6524. if (ptr != nullptr) {
  6525. ggml_cuda_pool_free(device, ptr, actual_size);
  6526. }
  6527. }
  6528. T * get() {
  6529. return ptr;
  6530. }
  6531. cuda_pool_alloc() = default;
  6532. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  6533. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  6534. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  6535. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  6536. };
  6537. static bool g_cublas_loaded = false;
  6538. GGML_CALL bool ggml_cublas_loaded(void) {
  6539. return g_cublas_loaded;
  6540. }
  6541. GGML_CALL void ggml_init_cublas() {
  6542. static bool initialized = false;
  6543. if (!initialized) {
  6544. #ifdef __HIP_PLATFORM_AMD__
  6545. // Workaround for a rocBLAS bug when using multiple graphics cards:
  6546. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  6547. rocblas_initialize();
  6548. CUDA_CHECK(cudaDeviceSynchronize());
  6549. #endif
  6550. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  6551. initialized = true;
  6552. g_cublas_loaded = false;
  6553. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  6554. return;
  6555. }
  6556. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  6557. int64_t total_vram = 0;
  6558. #if defined(GGML_CUDA_FORCE_MMQ)
  6559. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  6560. #else
  6561. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  6562. #endif
  6563. #if defined(CUDA_USE_TENSOR_CORES)
  6564. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  6565. #else
  6566. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  6567. #endif
  6568. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  6569. for (int id = 0; id < g_device_count; ++id) {
  6570. int device_vmm = 0;
  6571. #if !defined(GGML_USE_HIPBLAS)
  6572. CUdevice device;
  6573. CU_CHECK(cuDeviceGet(&device, id));
  6574. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  6575. if (device_vmm) {
  6576. CUmemAllocationProp alloc_prop = {};
  6577. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6578. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6579. alloc_prop.location.id = id;
  6580. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  6581. }
  6582. #endif // !defined(GGML_USE_HIPBLAS)
  6583. g_device_caps[id].vmm = !!device_vmm;
  6584. cudaDeviceProp prop;
  6585. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  6586. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  6587. g_default_tensor_split[id] = total_vram;
  6588. total_vram += prop.totalGlobalMem;
  6589. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6590. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  6591. #else
  6592. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  6593. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6594. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  6595. }
  6596. for (int id = 0; id < g_device_count; ++id) {
  6597. g_default_tensor_split[id] /= total_vram;
  6598. }
  6599. for (int id = 0; id < g_device_count; ++id) {
  6600. ggml_cuda_set_device(id);
  6601. // create cuda streams
  6602. for (int is = 0; is < MAX_STREAMS; ++is) {
  6603. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  6604. }
  6605. // create cublas handle
  6606. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  6607. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  6608. }
  6609. // configure logging to stdout
  6610. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  6611. initialized = true;
  6612. g_cublas_loaded = true;
  6613. }
  6614. }
  6615. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  6616. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6617. return nullptr;
  6618. }
  6619. void * ptr = nullptr;
  6620. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6621. if (err != cudaSuccess) {
  6622. // clear the error
  6623. cudaGetLastError();
  6624. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6625. size/1024.0/1024.0, cudaGetErrorString(err));
  6626. return nullptr;
  6627. }
  6628. return ptr;
  6629. }
  6630. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  6631. CUDA_CHECK(cudaFreeHost(ptr));
  6632. }
  6633. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6634. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6635. cudaMemcpyKind kind;
  6636. char * src_ptr;
  6637. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  6638. kind = cudaMemcpyHostToDevice;
  6639. src_ptr = (char *) src->data;
  6640. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  6641. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6642. kind = cudaMemcpyDeviceToDevice;
  6643. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6644. int id;
  6645. CUDA_CHECK(cudaGetDevice(&id));
  6646. src_ptr = (char *) extra->data_device[id];
  6647. } else {
  6648. GGML_ASSERT(false);
  6649. }
  6650. char * dst_ptr = (char *) dst;
  6651. const int64_t ne0 = src->ne[0];
  6652. const int64_t nb0 = src->nb[0];
  6653. const int64_t nb1 = src->nb[1];
  6654. const int64_t nb2 = src->nb[2];
  6655. const int64_t nb3 = src->nb[3];
  6656. const enum ggml_type type = src->type;
  6657. const int64_t ts = ggml_type_size(type);
  6658. const int64_t bs = ggml_blck_size(type);
  6659. int64_t i1_diff = i1_high - i1_low;
  6660. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6661. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6662. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6663. } else if (nb0 == ts) {
  6664. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6665. } else {
  6666. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6667. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6668. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6669. // pretend the row is a matrix with cols=1
  6670. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6671. if (r != cudaSuccess) return r;
  6672. }
  6673. return cudaSuccess;
  6674. }
  6675. }
  6676. static void ggml_cuda_op_get_rows(
  6677. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6678. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6679. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6680. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6681. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6682. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6683. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6684. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6685. switch (src0->type) {
  6686. case GGML_TYPE_F16:
  6687. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6688. break;
  6689. case GGML_TYPE_F32:
  6690. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6691. break;
  6692. case GGML_TYPE_Q4_0:
  6693. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6694. break;
  6695. case GGML_TYPE_Q4_1:
  6696. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6697. break;
  6698. case GGML_TYPE_Q5_0:
  6699. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6700. break;
  6701. case GGML_TYPE_Q5_1:
  6702. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6703. break;
  6704. case GGML_TYPE_Q8_0:
  6705. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6706. break;
  6707. default:
  6708. // TODO: k-quants
  6709. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6710. GGML_ASSERT(false);
  6711. break;
  6712. }
  6713. }
  6714. template<class op>
  6715. static void ggml_cuda_op_bin_bcast(
  6716. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6717. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6718. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6719. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6720. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6721. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6722. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6723. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6724. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6725. } else {
  6726. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6727. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6728. GGML_ASSERT(false);
  6729. }
  6730. }
  6731. static void ggml_cuda_op_repeat(
  6732. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6733. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6734. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6735. (void) src1;
  6736. (void) src1_d;
  6737. }
  6738. static void ggml_cuda_op_add(
  6739. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6740. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6741. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6742. }
  6743. static void ggml_cuda_op_acc(
  6744. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6745. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6746. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6747. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6748. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6749. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6750. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6751. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6752. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6753. int offset = dst->op_params[3] / 4; // offset in bytes
  6754. acc_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  6755. (void) dst;
  6756. }
  6757. static void ggml_cuda_op_mul(
  6758. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6759. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6760. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6761. }
  6762. static void ggml_cuda_op_div(
  6763. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6764. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6765. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6766. }
  6767. static void ggml_cuda_op_gelu(
  6768. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6769. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6770. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6771. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6772. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6773. (void) src1;
  6774. (void) dst;
  6775. (void) src1_dd;
  6776. }
  6777. static void ggml_cuda_op_silu(
  6778. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6779. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6780. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6781. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6782. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6783. (void) src1;
  6784. (void) dst;
  6785. (void) src1_dd;
  6786. }
  6787. static void ggml_cuda_op_gelu_quick(
  6788. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6789. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6790. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6791. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6792. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6793. (void) src1;
  6794. (void) dst;
  6795. (void) src1_dd;
  6796. }
  6797. static void ggml_cuda_op_tanh(
  6798. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6799. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6800. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6801. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6802. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6803. (void) src1;
  6804. (void) dst;
  6805. (void) src1_dd;
  6806. }
  6807. static void ggml_cuda_op_relu(
  6808. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6809. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6810. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6811. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6812. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6813. (void) src1;
  6814. (void) dst;
  6815. (void) src1_dd;
  6816. }
  6817. static void ggml_cuda_op_hardsigmoid(
  6818. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6819. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6820. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6821. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6822. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6823. (void) src1;
  6824. (void) dst;
  6825. (void) src1_dd;
  6826. }
  6827. static void ggml_cuda_op_hardswish(
  6828. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6829. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6830. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6831. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6832. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6833. (void) src1;
  6834. (void) dst;
  6835. (void) src1_dd;
  6836. }
  6837. static void ggml_cuda_op_leaky_relu(
  6838. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6839. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6840. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6841. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6842. float negative_slope;
  6843. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6844. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6845. (void) src1;
  6846. (void) dst;
  6847. (void) src1_dd;
  6848. }
  6849. static void ggml_cuda_op_sqr(
  6850. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6851. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6852. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6853. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6854. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6855. (void) src1;
  6856. (void) dst;
  6857. (void) src1_dd;
  6858. }
  6859. static void ggml_cuda_op_norm(
  6860. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6861. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6862. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6863. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6864. const int64_t ne00 = src0->ne[0];
  6865. const int64_t nrows = ggml_nrows(src0);
  6866. float eps;
  6867. memcpy(&eps, dst->op_params, sizeof(float));
  6868. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6869. (void) src1;
  6870. (void) dst;
  6871. (void) src1_dd;
  6872. }
  6873. static void ggml_cuda_op_group_norm(
  6874. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6875. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6876. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6877. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6878. int num_groups = dst->op_params[0];
  6879. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6880. group_norm_f32_cuda(src0_dd, dst_dd, num_groups * src0->ne[3], group_size, ggml_nelements(src0), main_stream);
  6881. (void) src1;
  6882. (void) dst;
  6883. (void) src1_dd;
  6884. }
  6885. static void ggml_cuda_op_concat(
  6886. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6887. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6888. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6889. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6890. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6891. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6892. concat_f32_cuda(src0_dd + i3 * (src0->nb[3] / 4), src1_dd + i3 * (src1->nb[3] / 4), dst_dd + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], main_stream);
  6893. }
  6894. (void) src1;
  6895. (void) dst;
  6896. }
  6897. static void ggml_cuda_op_upscale(
  6898. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6899. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6900. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6901. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6902. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6903. const int scale_factor = dst->op_params[0];
  6904. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], scale_factor, main_stream);
  6905. (void) src1;
  6906. (void) dst;
  6907. (void) src1_dd;
  6908. }
  6909. static void ggml_cuda_op_pad(
  6910. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6911. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6912. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6913. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6914. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6915. pad_f32_cuda(src0_dd, dst_dd,
  6916. src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
  6917. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], main_stream);
  6918. (void) src1;
  6919. (void) dst;
  6920. (void) src1_dd;
  6921. }
  6922. static void ggml_cuda_op_arange(
  6923. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6924. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6925. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6926. float start;
  6927. float stop;
  6928. float step;
  6929. memcpy(&start, (float *)dst->op_params + 0, sizeof(float));
  6930. memcpy(&stop, (float *)dst->op_params + 1, sizeof(float));
  6931. memcpy(&step, (float *)dst->op_params + 2, sizeof(float));
  6932. int64_t steps = (int64_t)ceil((stop - start) / step);
  6933. GGML_ASSERT(ggml_nelements(dst) == steps);
  6934. arange_f32_cuda(dst_dd, dst->ne[0], start, step, main_stream);
  6935. (void) src0;
  6936. (void) src1;
  6937. (void) src0_dd;
  6938. (void) src1_dd;
  6939. }
  6940. static void ggml_cuda_op_timestep_embedding(
  6941. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6942. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6943. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6944. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6945. const int dim = dst->op_params[0];
  6946. const int max_period = dst->op_params[1];
  6947. timestep_embedding_f32_cuda(src0_dd, dst_dd, src0->ne[0], dst->nb[1], dim, max_period, main_stream);
  6948. (void) src1;
  6949. (void) dst;
  6950. (void) src1_dd;
  6951. }
  6952. static void ggml_cuda_op_rms_norm(
  6953. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6954. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6955. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6956. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6957. const int64_t ne00 = src0->ne[0];
  6958. const int64_t nrows = ggml_nrows(src0);
  6959. float eps;
  6960. memcpy(&eps, dst->op_params, sizeof(float));
  6961. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6962. (void) src1;
  6963. (void) dst;
  6964. (void) src1_dd;
  6965. }
  6966. static void ggml_cuda_op_mul_mat_q(
  6967. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6968. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6969. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6970. const int64_t ne00 = src0->ne[0];
  6971. const int64_t ne10 = src1->ne[0];
  6972. GGML_ASSERT(ne10 % QK8_1 == 0);
  6973. const int64_t ne0 = dst->ne[0];
  6974. const int64_t row_diff = row_high - row_low;
  6975. int id;
  6976. CUDA_CHECK(cudaGetDevice(&id));
  6977. // the main device has a larger memory buffer to hold the results from all GPUs
  6978. // nrows_dst == nrows of the matrix that the kernel writes into
  6979. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  6980. switch (src0->type) {
  6981. case GGML_TYPE_Q4_0:
  6982. 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);
  6983. break;
  6984. case GGML_TYPE_Q4_1:
  6985. 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);
  6986. break;
  6987. case GGML_TYPE_Q5_0:
  6988. 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);
  6989. break;
  6990. case GGML_TYPE_Q5_1:
  6991. 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);
  6992. break;
  6993. case GGML_TYPE_Q8_0:
  6994. 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);
  6995. break;
  6996. case GGML_TYPE_Q2_K:
  6997. 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);
  6998. break;
  6999. case GGML_TYPE_Q3_K:
  7000. 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);
  7001. break;
  7002. case GGML_TYPE_Q4_K:
  7003. 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);
  7004. break;
  7005. case GGML_TYPE_Q5_K:
  7006. 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);
  7007. break;
  7008. case GGML_TYPE_Q6_K:
  7009. 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);
  7010. break;
  7011. default:
  7012. GGML_ASSERT(false);
  7013. break;
  7014. }
  7015. (void) src1;
  7016. (void) dst;
  7017. (void) src1_ddf_i;
  7018. }
  7019. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  7020. int64_t min_compute_capability = INT_MAX;
  7021. int64_t max_compute_capability = INT_MIN;
  7022. for (int id = 0; id < g_device_count; ++id) {
  7023. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  7024. if (min_compute_capability > g_device_caps[id].cc) {
  7025. min_compute_capability = g_device_caps[id].cc;
  7026. }
  7027. if (max_compute_capability < g_device_caps[id].cc) {
  7028. max_compute_capability = g_device_caps[id].cc;
  7029. }
  7030. }
  7031. }
  7032. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7033. switch(type) {
  7034. case GGML_TYPE_Q4_0:
  7035. case GGML_TYPE_Q4_1:
  7036. case GGML_TYPE_Q5_0:
  7037. case GGML_TYPE_Q5_1:
  7038. case GGML_TYPE_Q8_0:
  7039. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7040. case GGML_TYPE_F16:
  7041. case GGML_TYPE_F32:
  7042. return 1;
  7043. case GGML_TYPE_Q2_K:
  7044. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  7045. case GGML_TYPE_Q3_K:
  7046. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  7047. case GGML_TYPE_Q4_K:
  7048. case GGML_TYPE_Q5_K:
  7049. case GGML_TYPE_Q6_K:
  7050. case GGML_TYPE_IQ2_XXS:
  7051. case GGML_TYPE_IQ2_XS:
  7052. case GGML_TYPE_IQ2_S:
  7053. case GGML_TYPE_IQ3_XXS:
  7054. case GGML_TYPE_IQ1_S:
  7055. case GGML_TYPE_IQ4_NL:
  7056. case GGML_TYPE_IQ4_XS:
  7057. case GGML_TYPE_IQ3_S:
  7058. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7059. default:
  7060. GGML_ASSERT(false);
  7061. }
  7062. #else
  7063. switch(type) {
  7064. case GGML_TYPE_Q4_0:
  7065. case GGML_TYPE_Q4_1:
  7066. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7067. case GGML_TYPE_Q5_0:
  7068. case GGML_TYPE_Q5_1:
  7069. case GGML_TYPE_Q8_0:
  7070. return 64;
  7071. case GGML_TYPE_F16:
  7072. case GGML_TYPE_F32:
  7073. return 1;
  7074. case GGML_TYPE_Q2_K:
  7075. case GGML_TYPE_Q3_K:
  7076. case GGML_TYPE_Q4_K:
  7077. case GGML_TYPE_Q5_K:
  7078. case GGML_TYPE_IQ2_XXS:
  7079. case GGML_TYPE_IQ2_XS:
  7080. case GGML_TYPE_IQ2_S:
  7081. case GGML_TYPE_IQ3_XXS:
  7082. case GGML_TYPE_IQ1_S:
  7083. case GGML_TYPE_IQ4_NL:
  7084. case GGML_TYPE_IQ4_XS:
  7085. case GGML_TYPE_IQ3_S:
  7086. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7087. case GGML_TYPE_Q6_K:
  7088. return 64;
  7089. default:
  7090. GGML_ASSERT(false);
  7091. }
  7092. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7093. }
  7094. static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split, int id) {
  7095. const int64_t nrows = ggml_nrows(tensor);
  7096. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  7097. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  7098. *row_low -= *row_low % rounding;
  7099. if (id == g_device_count - 1) {
  7100. *row_high = nrows;
  7101. } else {
  7102. *row_high = nrows*tensor_split[id + 1];
  7103. *row_high -= *row_high % rounding;
  7104. }
  7105. }
  7106. static void ggml_cuda_op_mul_mat_vec_q(
  7107. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7108. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7109. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7110. const int64_t ne00 = src0->ne[0];
  7111. const int64_t row_diff = row_high - row_low;
  7112. const int64_t ne10 = src1->ne[0];
  7113. GGML_ASSERT(ne10 % QK8_1 == 0);
  7114. const int64_t ne0 = dst->ne[0];
  7115. int id;
  7116. CUDA_CHECK(cudaGetDevice(&id));
  7117. // the main device has a larger memory buffer to hold the results from all GPUs
  7118. // nrows_dst == nrows of the matrix that the kernel writes into
  7119. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7120. switch (src0->type) {
  7121. case GGML_TYPE_Q4_0:
  7122. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7123. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7124. break;
  7125. case GGML_TYPE_Q4_1:
  7126. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7127. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7128. break;
  7129. case GGML_TYPE_Q5_0:
  7130. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7131. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7132. break;
  7133. case GGML_TYPE_Q5_1:
  7134. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7135. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7136. break;
  7137. case GGML_TYPE_Q8_0:
  7138. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7139. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7140. break;
  7141. case GGML_TYPE_Q2_K:
  7142. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7143. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7144. break;
  7145. case GGML_TYPE_Q3_K:
  7146. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7147. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7148. break;
  7149. case GGML_TYPE_Q4_K:
  7150. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7151. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7152. break;
  7153. case GGML_TYPE_Q5_K:
  7154. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7155. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7156. break;
  7157. case GGML_TYPE_Q6_K:
  7158. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7159. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7160. break;
  7161. case GGML_TYPE_IQ2_XXS:
  7162. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7163. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7164. break;
  7165. case GGML_TYPE_IQ2_XS:
  7166. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7167. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7168. break;
  7169. case GGML_TYPE_IQ2_S:
  7170. mul_mat_vec_q_cuda<QK_K, QI2_S, block_iq2_s, 1, vec_dot_iq2_s_q8_1>
  7171. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7172. break;
  7173. case GGML_TYPE_IQ3_XXS:
  7174. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7175. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7176. break;
  7177. case GGML_TYPE_IQ1_S:
  7178. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7179. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7180. break;
  7181. case GGML_TYPE_IQ4_NL:
  7182. mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
  7183. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7184. break;
  7185. case GGML_TYPE_IQ4_XS:
  7186. mul_mat_vec_q_cuda<QK_K, QI4_XS, block_iq4_xs, 1, vec_dot_iq4_xs_q8_1>
  7187. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7188. break;
  7189. case GGML_TYPE_IQ3_S:
  7190. mul_mat_vec_q_cuda<QK_K, QI3_XS, block_iq3_s, 1, vec_dot_iq3_s_q8_1>
  7191. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7192. break;
  7193. default:
  7194. GGML_ASSERT(false);
  7195. break;
  7196. }
  7197. (void) src1;
  7198. (void) dst;
  7199. (void) src1_ddf_i;
  7200. (void) src1_ncols;
  7201. (void) src1_padded_row_size;
  7202. }
  7203. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7204. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7205. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7206. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7207. const int64_t ne00 = src0->ne[0];
  7208. const int64_t row_diff = row_high - row_low;
  7209. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7210. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7211. #ifdef GGML_CUDA_F16
  7212. cuda_pool_alloc<half> src1_dfloat_a;
  7213. half * src1_dfloat = nullptr; // dfloat == half
  7214. bool src1_convert_f16 =
  7215. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7216. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7217. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7218. if (src1_convert_f16) {
  7219. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7220. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7221. GGML_ASSERT(to_fp16_cuda != nullptr);
  7222. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7223. }
  7224. #else
  7225. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7226. #endif // GGML_CUDA_F16
  7227. switch (src0->type) {
  7228. case GGML_TYPE_Q4_0:
  7229. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7230. break;
  7231. case GGML_TYPE_Q4_1:
  7232. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7233. break;
  7234. case GGML_TYPE_Q5_0:
  7235. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7236. break;
  7237. case GGML_TYPE_Q5_1:
  7238. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7239. break;
  7240. case GGML_TYPE_Q8_0:
  7241. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7242. break;
  7243. case GGML_TYPE_Q2_K:
  7244. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7245. break;
  7246. case GGML_TYPE_Q3_K:
  7247. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7248. break;
  7249. case GGML_TYPE_Q4_K:
  7250. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7251. break;
  7252. case GGML_TYPE_Q5_K:
  7253. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7254. break;
  7255. case GGML_TYPE_Q6_K:
  7256. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7257. break;
  7258. case GGML_TYPE_F16:
  7259. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7260. break;
  7261. default:
  7262. GGML_ASSERT(false);
  7263. break;
  7264. }
  7265. (void) src1;
  7266. (void) dst;
  7267. (void) src1_ddq_i;
  7268. (void) src1_ncols;
  7269. (void) src1_padded_row_size;
  7270. }
  7271. static void ggml_cuda_op_mul_mat_cublas(
  7272. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7273. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7274. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7275. GGML_ASSERT(src0_dd_i != nullptr);
  7276. GGML_ASSERT(src1_ddf_i != nullptr);
  7277. GGML_ASSERT(dst_dd_i != nullptr);
  7278. const int64_t ne00 = src0->ne[0];
  7279. const int64_t ne10 = src1->ne[0];
  7280. const int64_t ne0 = dst->ne[0];
  7281. const int64_t row_diff = row_high - row_low;
  7282. int id;
  7283. CUDA_CHECK(cudaGetDevice(&id));
  7284. // the main device has a larger memory buffer to hold the results from all GPUs
  7285. // ldc == nrows of the matrix that cuBLAS writes into
  7286. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  7287. const int compute_capability = g_device_caps[id].cc;
  7288. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  7289. //printf("this branch\n");
  7290. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7291. cuda_pool_alloc<half> src0_as_f16;
  7292. if (src0->type != GGML_TYPE_F16) {
  7293. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7294. GGML_ASSERT(to_fp16_cuda != nullptr);
  7295. size_t ne = row_diff*ne00;
  7296. src0_as_f16.alloc(ne);
  7297. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7298. }
  7299. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7300. cuda_pool_alloc<half> src1_as_f16;
  7301. if (src1->type != GGML_TYPE_F16) {
  7302. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7303. GGML_ASSERT(to_fp16_cuda != nullptr);
  7304. size_t ne = src1_ncols*ne10;
  7305. src1_as_f16.alloc(ne);
  7306. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7307. }
  7308. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7309. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7310. const half alpha_f16 = 1.0f;
  7311. const half beta_f16 = 0.0f;
  7312. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7313. CUBLAS_CHECK(
  7314. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7315. row_diff, src1_ncols, ne10,
  7316. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7317. src1_ptr, CUDA_R_16F, ne10,
  7318. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7319. CUBLAS_COMPUTE_16F,
  7320. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7321. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7322. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7323. } else {
  7324. cuda_pool_alloc<float> src0_ddq_as_f32;
  7325. cuda_pool_alloc<float> src1_ddq_as_f32;
  7326. if (src0->type != GGML_TYPE_F32) {
  7327. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7328. GGML_ASSERT(to_fp32_cuda != nullptr);
  7329. src0_ddq_as_f32.alloc(row_diff*ne00);
  7330. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7331. }
  7332. if (src1->type != GGML_TYPE_F32) {
  7333. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7334. GGML_ASSERT(to_fp32_cuda != nullptr);
  7335. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7336. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7337. }
  7338. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7339. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7340. const float alpha = 1.0f;
  7341. const float beta = 0.0f;
  7342. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7343. CUBLAS_CHECK(
  7344. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7345. row_diff, src1_ncols, ne10,
  7346. &alpha, src0_ddf_i, ne00,
  7347. src1_ddf1_i, ne10,
  7348. &beta, dst_dd_i, ldc));
  7349. }
  7350. (void) dst;
  7351. (void) src1_ddq_i;
  7352. (void) src1_padded_row_size;
  7353. }
  7354. static void ggml_cuda_op_rope(
  7355. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7356. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7357. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7358. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7359. GGML_ASSERT(src0->type == dst->type);
  7360. const int64_t ne00 = src0->ne[0];
  7361. const int64_t ne01 = src0->ne[1];
  7362. const int64_t ne2 = dst->ne[2];
  7363. const int64_t nrows = ggml_nrows(src0);
  7364. //const int n_past = ((int32_t *) dst->op_params)[0];
  7365. const int n_dims = ((int32_t *) dst->op_params)[1];
  7366. const int mode = ((int32_t *) dst->op_params)[2];
  7367. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7368. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7369. // RoPE alteration for extended context
  7370. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7371. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7372. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7373. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7374. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7375. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7376. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7377. const int32_t * pos = nullptr;
  7378. if ((mode & 1) == 0) {
  7379. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7380. GGML_ASSERT(src1->ne[0] == ne2);
  7381. pos = (const int32_t *) src1_dd;
  7382. }
  7383. const bool is_neox = mode & 2;
  7384. const bool is_glm = mode & 4;
  7385. rope_corr_dims corr_dims;
  7386. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7387. // compute
  7388. if (is_glm) {
  7389. GGML_ASSERT(false);
  7390. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7391. } else if (is_neox) {
  7392. if (src0->type == GGML_TYPE_F32) {
  7393. rope_neox_cuda(
  7394. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7395. attn_factor, corr_dims, main_stream
  7396. );
  7397. } else if (src0->type == GGML_TYPE_F16) {
  7398. rope_neox_cuda(
  7399. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7400. attn_factor, corr_dims, main_stream
  7401. );
  7402. } else {
  7403. GGML_ASSERT(false);
  7404. }
  7405. } else {
  7406. if (src0->type == GGML_TYPE_F32) {
  7407. rope_cuda(
  7408. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7409. attn_factor, corr_dims, main_stream
  7410. );
  7411. } else if (src0->type == GGML_TYPE_F16) {
  7412. rope_cuda(
  7413. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7414. attn_factor, corr_dims, main_stream
  7415. );
  7416. } else {
  7417. GGML_ASSERT(false);
  7418. }
  7419. }
  7420. (void) src1;
  7421. (void) dst;
  7422. (void) src1_dd;
  7423. }
  7424. static void ggml_cuda_op_alibi(
  7425. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7426. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7427. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7428. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7429. const int64_t ne00 = src0->ne[0];
  7430. const int64_t ne01 = src0->ne[1];
  7431. const int64_t ne02 = src0->ne[2];
  7432. const int64_t nrows = ggml_nrows(src0);
  7433. //const int n_past = ((int32_t *) dst->op_params)[0];
  7434. const int n_head = ((int32_t *) dst->op_params)[1];
  7435. float max_bias;
  7436. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7437. //GGML_ASSERT(ne01 + n_past == ne00);
  7438. GGML_ASSERT(n_head == ne02);
  7439. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7440. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7441. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7442. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7443. (void) src1;
  7444. (void) src1_dd;
  7445. }
  7446. static void ggml_cuda_op_pool2d(
  7447. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7448. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7449. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7450. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7451. const int32_t * opts = (const int32_t *)dst->op_params;
  7452. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  7453. const int k0 = opts[1];
  7454. const int k1 = opts[2];
  7455. const int s0 = opts[3];
  7456. const int s1 = opts[4];
  7457. const int p0 = opts[5];
  7458. const int p1 = opts[6];
  7459. const int64_t IH = src0->ne[1];
  7460. const int64_t IW = src0->ne[0];
  7461. const int64_t N = dst->ne[3];
  7462. const int64_t OC = dst->ne[2];
  7463. const int64_t OH = dst->ne[1];
  7464. const int64_t OW = dst->ne[0];
  7465. const int parallel_elements = N * OC * OH * OW;
  7466. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  7467. dim3 block_nums(num_blocks);
  7468. pool2d_nchw_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, main_stream>>>(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0, parallel_elements, src0_dd, dst_dd, op);
  7469. (void) src1;
  7470. (void) src1_dd;
  7471. }
  7472. static void ggml_cuda_op_im2col(
  7473. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7474. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7475. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7476. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7477. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  7478. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7479. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  7480. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  7481. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  7482. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  7483. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  7484. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  7485. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  7486. const int64_t IH = is_2D ? src1->ne[1] : 1;
  7487. const int64_t IW = src1->ne[0];
  7488. const int64_t KH = is_2D ? src0->ne[1] : 1;
  7489. const int64_t KW = src0->ne[0];
  7490. const int64_t OH = is_2D ? dst->ne[2] : 1;
  7491. const int64_t OW = dst->ne[1];
  7492. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7493. const int64_t batch = src1->ne[3];
  7494. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  7495. if(dst->type == GGML_TYPE_F16) {
  7496. im2col_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7497. } else {
  7498. im2col_cuda(src1_dd, (float*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7499. }
  7500. (void) src0;
  7501. (void) src0_dd;
  7502. }
  7503. static void ggml_cuda_op_sum_rows(
  7504. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7505. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7506. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7507. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7508. const int64_t ncols = src0->ne[0];
  7509. const int64_t nrows = ggml_nrows(src0);
  7510. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  7511. (void) src1;
  7512. (void) dst;
  7513. (void) src1_dd;
  7514. }
  7515. static void ggml_cuda_op_argsort(
  7516. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7517. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7518. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7519. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  7520. const int64_t ncols = src0->ne[0];
  7521. const int64_t nrows = ggml_nrows(src0);
  7522. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  7523. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  7524. (void) src1;
  7525. (void) dst;
  7526. (void) src1_dd;
  7527. }
  7528. static void ggml_cuda_op_diag_mask_inf(
  7529. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7530. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7531. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7532. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7533. const int64_t ne00 = src0->ne[0];
  7534. const int64_t ne01 = src0->ne[1];
  7535. const int nrows0 = ggml_nrows(src0);
  7536. const int n_past = ((int32_t *) dst->op_params)[0];
  7537. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  7538. (void) src1;
  7539. (void) dst;
  7540. (void) src1_dd;
  7541. }
  7542. static void ggml_cuda_op_soft_max(
  7543. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7544. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7545. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7546. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7547. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  7548. const int64_t ne00 = src0->ne[0];
  7549. const int64_t nrows_x = ggml_nrows(src0);
  7550. const int64_t nrows_y = src0->ne[1];
  7551. float scale = 1.0f;
  7552. float max_bias = 0.0f;
  7553. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7554. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7555. // positions tensor
  7556. float * src2_dd = nullptr;
  7557. cuda_pool_alloc<float> src2_f;
  7558. ggml_tensor * src2 = dst->src[2];
  7559. const bool use_src2 = src2 != nullptr;
  7560. if (use_src2) {
  7561. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  7562. if (src2_on_device) {
  7563. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  7564. src2_dd = (float *) src2_extra->data_device[g_main_device];
  7565. } else {
  7566. src2_dd = src2_f.alloc(ggml_nelements(src2));
  7567. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  7568. }
  7569. }
  7570. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  7571. }
  7572. static void ggml_cuda_op_scale(
  7573. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7574. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7575. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7576. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7577. float scale;
  7578. memcpy(&scale, dst->op_params, sizeof(float));
  7579. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  7580. CUDA_CHECK(cudaGetLastError());
  7581. (void) src1;
  7582. (void) dst;
  7583. (void) src1_dd;
  7584. }
  7585. static void ggml_cuda_op_clamp(
  7586. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7587. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7588. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7589. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7590. float min;
  7591. float max;
  7592. memcpy(&min, dst->op_params, sizeof(float));
  7593. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7594. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  7595. CUDA_CHECK(cudaGetLastError());
  7596. (void) src1;
  7597. (void) dst;
  7598. (void) src1_dd;
  7599. }
  7600. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  7601. const int64_t nrows0 = ggml_nrows(src0);
  7602. const bool use_src1 = src1 != nullptr;
  7603. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  7604. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7605. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7606. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7607. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  7608. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7609. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  7610. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  7611. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  7612. // dd = data device
  7613. float * src0_ddf = nullptr;
  7614. float * src1_ddf = nullptr;
  7615. float * dst_ddf = nullptr;
  7616. cuda_pool_alloc<float> src0_f;
  7617. cuda_pool_alloc<float> src1_f;
  7618. cuda_pool_alloc<float> dst_f;
  7619. ggml_cuda_set_device(g_main_device);
  7620. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7621. if (src0_on_device) {
  7622. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  7623. } else {
  7624. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  7625. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  7626. }
  7627. if (use_src1) {
  7628. if (src1_on_device) {
  7629. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7630. } else {
  7631. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  7632. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  7633. }
  7634. }
  7635. if (dst_on_device) {
  7636. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7637. } else {
  7638. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7639. }
  7640. // do the computation
  7641. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7642. CUDA_CHECK(cudaGetLastError());
  7643. // copy dst to host if necessary
  7644. if (!dst_on_device) {
  7645. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7646. }
  7647. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  7648. CUDA_CHECK(cudaDeviceSynchronize());
  7649. }
  7650. }
  7651. static void ggml_cuda_set_peer_access(const int n_tokens) {
  7652. static bool peer_access_enabled = false;
  7653. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  7654. if (peer_access_enabled == enable_peer_access) {
  7655. return;
  7656. }
  7657. #ifdef NDEBUG
  7658. for (int id = 0; id < g_device_count; ++id) {
  7659. ggml_cuda_set_device(id);
  7660. CUDA_CHECK(cudaDeviceSynchronize());
  7661. }
  7662. for (int id = 0; id < g_device_count; ++id) {
  7663. ggml_cuda_set_device(id);
  7664. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  7665. if (id == id_other) {
  7666. continue;
  7667. }
  7668. if (id != g_main_device && id_other != g_main_device) {
  7669. continue;
  7670. }
  7671. int can_access_peer;
  7672. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  7673. if (can_access_peer) {
  7674. if (enable_peer_access) {
  7675. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  7676. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  7677. CUDA_CHECK(err);
  7678. }
  7679. } else {
  7680. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  7681. if (err != cudaErrorPeerAccessNotEnabled) {
  7682. CUDA_CHECK(err);
  7683. }
  7684. }
  7685. }
  7686. }
  7687. }
  7688. #endif // NDEBUG
  7689. peer_access_enabled = enable_peer_access;
  7690. }
  7691. // FIXME: move this somewhere else
  7692. struct ggml_backend_cuda_split_buffer_type_context {
  7693. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7694. };
  7695. static void ggml_cuda_op_mul_mat(
  7696. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  7697. const bool convert_src1_to_q8_1) {
  7698. const int64_t ne00 = src0->ne[0];
  7699. const int64_t ne01 = src0->ne[1];
  7700. const int64_t ne02 = src0->ne[2];
  7701. const int64_t ne03 = src0->ne[3];
  7702. const int64_t ne10 = src1->ne[0];
  7703. const int64_t ne11 = src1->ne[1];
  7704. const int64_t ne12 = src1->ne[2];
  7705. const int64_t ne13 = src1->ne[3];
  7706. const int64_t nrows1 = ggml_nrows(src1);
  7707. GGML_ASSERT(ne03 == ne13);
  7708. const int64_t ne0 = dst->ne[0];
  7709. const int64_t ne1 = dst->ne[1];
  7710. const int nb2 = dst->nb[2];
  7711. const int nb3 = dst->nb[3];
  7712. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7713. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  7714. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  7715. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  7716. const int64_t i02_divisor = ne12 / ne02;
  7717. const size_t src0_ts = ggml_type_size(src0->type);
  7718. const size_t src0_bs = ggml_blck_size(src0->type);
  7719. const size_t q8_1_ts = sizeof(block_q8_1);
  7720. const size_t q8_1_bs = QK8_1;
  7721. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7722. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7723. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7724. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  7725. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  7726. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  7727. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  7728. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  7729. GGML_ASSERT(!(split && ne02 > 1));
  7730. GGML_ASSERT(!(split && ne03 > 1));
  7731. GGML_ASSERT(!(split && ne02 < ne12));
  7732. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7733. if (split) {
  7734. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  7735. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  7736. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  7737. tensor_split = buft_ctx->tensor_split;
  7738. }
  7739. struct dev_data {
  7740. cuda_pool_alloc<char> src0_dd_alloc;
  7741. cuda_pool_alloc<float> src1_ddf_alloc;
  7742. cuda_pool_alloc<char> src1_ddq_alloc;
  7743. cuda_pool_alloc<float> dst_dd_alloc;
  7744. char * src0_dd = nullptr;
  7745. float * src1_ddf = nullptr; // float
  7746. char * src1_ddq = nullptr; // q8_1
  7747. float * dst_dd = nullptr;
  7748. int64_t row_low;
  7749. int64_t row_high;
  7750. };
  7751. dev_data dev[GGML_CUDA_MAX_DEVICES];
  7752. int used_devices = 0;
  7753. for (int id = 0; id < g_device_count; ++id) {
  7754. // by default, use all rows
  7755. dev[id].row_low = 0;
  7756. dev[id].row_high = ne01;
  7757. // for multi GPU, get the row boundaries from tensor split
  7758. // and round to mul_mat_q tile sizes
  7759. if (split) {
  7760. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  7761. if (id != 0) {
  7762. dev[id].row_low = ne01*tensor_split[id];
  7763. if (dev[id].row_low < ne01) {
  7764. dev[id].row_low -= dev[id].row_low % rounding;
  7765. }
  7766. }
  7767. if (id != g_device_count - 1) {
  7768. dev[id].row_high = ne01*tensor_split[id + 1];
  7769. if (dev[id].row_high < ne01) {
  7770. dev[id].row_high -= dev[id].row_high % rounding;
  7771. }
  7772. }
  7773. }
  7774. }
  7775. for (int id = 0; id < g_device_count; ++id) {
  7776. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7777. continue;
  7778. }
  7779. used_devices++;
  7780. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7781. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7782. ggml_cuda_set_device(id);
  7783. cudaStream_t stream = g_cudaStreams[id][0];
  7784. if (src0_on_device && src0_is_contiguous) {
  7785. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  7786. } else {
  7787. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  7788. }
  7789. if (src1_on_device && src1_is_contiguous) {
  7790. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7791. } else {
  7792. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7793. }
  7794. if (convert_src1_to_q8_1) {
  7795. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7796. if (src1_on_device && src1_is_contiguous) {
  7797. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7798. CUDA_CHECK(cudaGetLastError());
  7799. }
  7800. }
  7801. if (dst_on_device) {
  7802. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7803. } else {
  7804. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7805. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7806. }
  7807. }
  7808. // if multiple devices are used they need to wait for the main device
  7809. // here an event is recorded that signals that the main device has finished calculating the input data
  7810. if (split && used_devices > 1) {
  7811. ggml_cuda_set_device(g_main_device);
  7812. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7813. }
  7814. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7815. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7816. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7817. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7818. for (int id = 0; id < g_device_count; ++id) {
  7819. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7820. continue;
  7821. }
  7822. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7823. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device;
  7824. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7825. ggml_cuda_set_device(id);
  7826. cudaStream_t stream = g_cudaStreams[id][is];
  7827. // wait for main GPU data if necessary
  7828. if (split && (id != g_main_device || is != 0)) {
  7829. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7830. }
  7831. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7832. const int64_t i03 = i0 / ne12;
  7833. const int64_t i02 = i0 % ne12;
  7834. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7835. // for split tensors the data begins at i0 == i0_offset_low
  7836. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7837. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7838. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7839. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7840. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7841. // in that case an offset on dst_ddf_i is needed
  7842. if (dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device) {
  7843. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7844. }
  7845. // copy src0, src1 to device if necessary
  7846. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  7847. if (id != g_main_device) {
  7848. if (convert_src1_to_q8_1) {
  7849. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7850. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7851. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7852. } else {
  7853. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7854. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7855. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7856. src1_ncols*ne10*sizeof(float), stream));
  7857. }
  7858. }
  7859. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  7860. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7861. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7862. } else {
  7863. GGML_ASSERT(false);
  7864. }
  7865. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  7866. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7867. CUDA_CHECK(cudaGetLastError());
  7868. }
  7869. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7870. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7871. }
  7872. // do the computation
  7873. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7874. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7875. CUDA_CHECK(cudaGetLastError());
  7876. // copy dst to host or other device if necessary
  7877. if (!dst_on_device) {
  7878. void * dst_off_device;
  7879. cudaMemcpyKind kind;
  7880. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  7881. dst_off_device = dst->data;
  7882. kind = cudaMemcpyDeviceToHost;
  7883. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  7884. dst_off_device = dst_extra->data_device[g_main_device];
  7885. kind = cudaMemcpyDeviceToDevice;
  7886. } else {
  7887. GGML_ASSERT(false);
  7888. }
  7889. if (split) {
  7890. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7891. // dst is NOT transposed.
  7892. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7893. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7894. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7895. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7896. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7897. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7898. #if !defined(GGML_USE_HIPBLAS)
  7899. if (kind == cudaMemcpyDeviceToDevice) {
  7900. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7901. cudaMemcpy3DPeerParms p = {};
  7902. p.dstDevice = g_main_device;
  7903. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7904. p.srcDevice = id;
  7905. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7906. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7907. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7908. } else
  7909. #endif
  7910. {
  7911. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7912. dst_dd_i, row_diff*sizeof(float),
  7913. row_diff*sizeof(float), src1_ncols,
  7914. kind, stream));
  7915. }
  7916. } else {
  7917. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7918. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7919. dhf_dst_i += src1_col_0*ne0;
  7920. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7921. }
  7922. }
  7923. // add event for the main device to wait on until other device is done
  7924. if (split && (id != g_main_device || is != 0)) {
  7925. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7926. }
  7927. }
  7928. }
  7929. }
  7930. // main device waits for all other devices to be finished
  7931. if (split && g_device_count > 1) {
  7932. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7933. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7934. ggml_cuda_set_device(g_main_device);
  7935. for (int id = 0; id < g_device_count; ++id) {
  7936. if (dev[id].row_low == dev[id].row_high) {
  7937. continue;
  7938. }
  7939. for (int64_t is = 0; is < is_max; ++is) {
  7940. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7941. }
  7942. }
  7943. }
  7944. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  7945. ggml_cuda_set_device(g_main_device);
  7946. CUDA_CHECK(cudaDeviceSynchronize());
  7947. }
  7948. }
  7949. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7950. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  7951. }
  7952. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7953. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  7954. }
  7955. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7956. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  7957. }
  7958. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7959. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  7960. }
  7961. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7962. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  7963. }
  7964. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7965. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  7966. }
  7967. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7968. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  7969. }
  7970. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7971. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  7972. }
  7973. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7974. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  7975. }
  7976. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7977. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  7978. }
  7979. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7980. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  7981. }
  7982. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7983. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  7984. }
  7985. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7986. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  7987. }
  7988. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7989. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  7990. }
  7991. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7992. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  7993. }
  7994. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7995. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  7996. }
  7997. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7998. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  7999. }
  8000. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8001. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  8002. }
  8003. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8004. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  8005. }
  8006. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8007. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  8008. }
  8009. static void ggml_cuda_arange(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8010. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8011. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  8012. // dd = data device
  8013. float * src0_ddf = nullptr;
  8014. float * src1_ddf = nullptr;
  8015. float * dst_ddf = nullptr;
  8016. cuda_pool_alloc<float> dst_f;
  8017. ggml_cuda_set_device(g_main_device);
  8018. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8019. if (dst_on_device) {
  8020. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8021. } else {
  8022. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  8023. }
  8024. // do the computation
  8025. ggml_cuda_op_arange(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  8026. CUDA_CHECK(cudaGetLastError());
  8027. // copy dst to host if necessary
  8028. if (!dst_on_device) {
  8029. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  8030. }
  8031. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8032. CUDA_CHECK(cudaDeviceSynchronize());
  8033. }
  8034. }
  8035. static void ggml_cuda_timestep_embedding(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8036. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_timestep_embedding);
  8037. }
  8038. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8039. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  8040. }
  8041. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  8042. if (!g_cublas_loaded) return false;
  8043. const int64_t ne10 = src1->ne[0];
  8044. const int64_t ne0 = dst->ne[0];
  8045. const int64_t ne1 = dst->ne[1];
  8046. // TODO: find the optimal values for these
  8047. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  8048. src1->type == GGML_TYPE_F32 &&
  8049. dst->type == GGML_TYPE_F32 &&
  8050. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  8051. }
  8052. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8053. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  8054. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8055. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  8056. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  8057. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8058. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8059. const int64_t ne00 = src0->ne[0];
  8060. const int64_t ne01 = src0->ne[1];
  8061. const int64_t ne02 = src0->ne[2];
  8062. const int64_t ne12 = src1->ne[2];
  8063. ggml_cuda_set_device(g_main_device);
  8064. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8065. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8066. void * src0_ddq = src0_extra->data_device[g_main_device];
  8067. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8068. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8069. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8070. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8071. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8072. }
  8073. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8074. GGML_ASSERT(!ggml_is_transposed(src0));
  8075. GGML_ASSERT(!ggml_is_transposed(src1));
  8076. GGML_ASSERT(!ggml_is_permuted(src0));
  8077. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8078. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8079. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8080. const int64_t ne00 = src0->ne[0];
  8081. const int64_t ne01 = src0->ne[1];
  8082. const int64_t ne02 = src0->ne[2];
  8083. const int64_t nb01 = src0->nb[1];
  8084. const int64_t nb02 = src0->nb[2];
  8085. const int64_t ne12 = src1->ne[2];
  8086. ggml_cuda_set_device(g_main_device);
  8087. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8088. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8089. void * src0_ddq = src0_extra->data_device[g_main_device];
  8090. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8091. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8092. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8093. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8094. const int64_t row_stride_x = nb01 / sizeof(half);
  8095. const int64_t channel_stride_x = nb02 / sizeof(half);
  8096. 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);
  8097. }
  8098. static __global__ void k_compute_batched_ptrs(
  8099. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8100. const void ** ptrs_src, void ** ptrs_dst,
  8101. int64_t ne12, int64_t ne13,
  8102. int64_t ne23,
  8103. size_t nb02, size_t nb03,
  8104. size_t nb12, size_t nb13,
  8105. size_t nbd2, size_t nbd3,
  8106. int64_t r2, int64_t r3) {
  8107. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8108. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8109. if (i13 >= ne13 || i12 >= ne12) {
  8110. return;
  8111. }
  8112. int64_t i03 = i13 / r3;
  8113. int64_t i02 = i12 / r2;
  8114. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8115. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8116. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8117. }
  8118. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8119. GGML_ASSERT(!ggml_is_transposed(src0));
  8120. GGML_ASSERT(!ggml_is_transposed(src1));
  8121. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8122. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8123. GGML_TENSOR_BINARY_OP_LOCALS
  8124. const int64_t ne_dst = ggml_nelements(dst);
  8125. ggml_cuda_set_device(g_main_device);
  8126. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8127. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8128. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8129. void * src0_ddq = src0_extra->data_device[g_main_device];
  8130. half * src0_f16 = (half *) src0_ddq;
  8131. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8132. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8133. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8134. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8135. // convert src1 to fp16
  8136. cuda_pool_alloc<half> src1_f16_alloc;
  8137. if (src1->type != GGML_TYPE_F16) {
  8138. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8139. const int64_t ne_src1 = ggml_nelements(src1);
  8140. src1_f16_alloc.alloc(ne_src1);
  8141. GGML_ASSERT(to_fp16_cuda != nullptr);
  8142. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8143. }
  8144. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8145. cuda_pool_alloc<half> dst_f16;
  8146. char * dst_t;
  8147. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8148. cudaDataType_t cu_data_type = CUDA_R_16F;
  8149. // dst strides
  8150. size_t nbd2 = dst->nb[2];
  8151. size_t nbd3 = dst->nb[3];
  8152. const half alpha_f16 = 1.0f;
  8153. const half beta_f16 = 0.0f;
  8154. const float alpha_f32 = 1.0f;
  8155. const float beta_f32 = 0.0f;
  8156. const void * alpha = &alpha_f16;
  8157. const void * beta = &beta_f16;
  8158. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8159. dst_t = (char *) dst_f16.alloc(ne_dst);
  8160. nbd2 /= sizeof(float) / sizeof(half);
  8161. nbd3 /= sizeof(float) / sizeof(half);
  8162. } else {
  8163. dst_t = (char *) dst_ddf;
  8164. cu_compute_type = CUBLAS_COMPUTE_32F;
  8165. cu_data_type = CUDA_R_32F;
  8166. alpha = &alpha_f32;
  8167. beta = &beta_f32;
  8168. }
  8169. GGML_ASSERT(ne12 % ne02 == 0);
  8170. GGML_ASSERT(ne13 % ne03 == 0);
  8171. // broadcast factors
  8172. const int64_t r2 = ne12/ne02;
  8173. const int64_t r3 = ne13/ne03;
  8174. #if 0
  8175. // use cublasGemmEx
  8176. {
  8177. for (int i13 = 0; i13 < ne13; ++i13) {
  8178. for (int i12 = 0; i12 < ne12; ++i12) {
  8179. int i03 = i13 / r3;
  8180. int i02 = i12 / r2;
  8181. CUBLAS_CHECK(
  8182. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8183. ne01, ne11, ne10,
  8184. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8185. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8186. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8187. cu_compute_type,
  8188. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8189. }
  8190. }
  8191. }
  8192. #else
  8193. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8194. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8195. // use cublasGemmStridedBatchedEx
  8196. CUBLAS_CHECK(
  8197. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8198. ne01, ne11, ne10,
  8199. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8200. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8201. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8202. ne12*ne13,
  8203. cu_compute_type,
  8204. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8205. } else {
  8206. // use cublasGemmBatchedEx
  8207. const int ne23 = ne12*ne13;
  8208. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8209. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8210. dim3 block_dims(ne13, ne12);
  8211. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8212. src0_f16, src1_f16, dst_t,
  8213. ptrs_src.get(), ptrs_dst.get(),
  8214. ne12, ne13,
  8215. ne23,
  8216. nb02, nb03,
  8217. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8218. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8219. nbd2, nbd3,
  8220. r2, r3);
  8221. CUDA_CHECK(cudaGetLastError());
  8222. CUBLAS_CHECK(
  8223. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8224. ne01, ne11, ne10,
  8225. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8226. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8227. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8228. ne23,
  8229. cu_compute_type,
  8230. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8231. }
  8232. #endif
  8233. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8234. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8235. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8236. }
  8237. }
  8238. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8239. const bool all_on_device =
  8240. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  8241. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  8242. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  8243. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  8244. int64_t min_compute_capability = INT_MAX;
  8245. bool any_pascal_with_slow_fp16 = false;
  8246. if (split) {
  8247. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8248. auto & tensor_split = buft_ctx->tensor_split;
  8249. for (int id = 0; id < g_device_count; ++id) {
  8250. // skip devices that are not going to do any work:
  8251. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8252. continue;
  8253. }
  8254. if (min_compute_capability > g_device_caps[id].cc) {
  8255. min_compute_capability = g_device_caps[id].cc;
  8256. }
  8257. if (g_device_caps[id].cc == 610) {
  8258. any_pascal_with_slow_fp16 = true;
  8259. }
  8260. }
  8261. } else {
  8262. min_compute_capability = g_device_caps[g_main_device].cc;
  8263. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8264. }
  8265. // check data types and tensor shapes for custom matrix multiplication kernels:
  8266. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8267. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8268. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8269. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8270. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8271. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8272. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8273. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8274. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8275. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8276. #ifdef CUDA_USE_TENSOR_CORES
  8277. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8278. #endif // CUDA_USE_TENSOR_CORES
  8279. #else
  8280. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8281. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8282. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8283. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8284. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8285. #ifdef CUDA_USE_TENSOR_CORES
  8286. // when tensor cores are available, use them for large batch size
  8287. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8288. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8289. #endif // CUDA_USE_TENSOR_CORES
  8290. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8291. // if mmvq is available it's a better choice than dmmv:
  8292. #ifndef GGML_CUDA_FORCE_DMMV
  8293. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8294. #endif // GGML_CUDA_FORCE_DMMV
  8295. // debug helpers
  8296. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8297. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8298. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8299. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8300. //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);
  8301. //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);
  8302. if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  8303. // KQ single-batch
  8304. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8305. } else if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  8306. // KQV single-batch
  8307. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8308. } else if (!split && all_on_device && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  8309. // KQ + KQV multi-batch
  8310. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8311. } else if (use_dequantize_mul_mat_vec) {
  8312. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8313. } else if (use_mul_mat_vec_q) {
  8314. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8315. } else if (use_mul_mat_q) {
  8316. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8317. } else {
  8318. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8319. }
  8320. }
  8321. #if 0
  8322. template<typename ... Srcs>
  8323. static __global__ void k_compute_batched_ptrs_id(
  8324. const void ** ptrs_src, void ** ptrs_dst,
  8325. int ne12, int ne13,
  8326. int ne23,
  8327. int nb02, int nb03,
  8328. int nb12, int nb13,
  8329. int nb2, int nb3,
  8330. int r2, int r3,
  8331. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8332. const half * src1_f16, half * dst_f16,
  8333. const int32_t * ids, const int id,
  8334. Srcs... src0s) {
  8335. int i = ids[id];
  8336. half * src0_f16;
  8337. const void * srcs_ar[] = { (const half *) src0s... };
  8338. if (src0_type == GGML_TYPE_F16) {
  8339. src0_f16 = (half *) srcs_ar[i];
  8340. } else {
  8341. src0_f16 = src0_as_f16;
  8342. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8343. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8344. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8345. }
  8346. }
  8347. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8348. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8349. if (i13 >= ne13 || i12 >= ne12) {
  8350. return;
  8351. }
  8352. int i03 = i13 / r3;
  8353. int i02 = i12 / r2;
  8354. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8355. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8356. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8357. }
  8358. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8359. const struct ggml_tensor * ids = dst->src[0];
  8360. const struct ggml_tensor * src1 = dst->src[1];
  8361. const struct ggml_tensor * src00 = dst->src[2];
  8362. const int id = dst->op_params[0];
  8363. GGML_ASSERT(!ggml_is_transposed(src00));
  8364. GGML_ASSERT(!ggml_is_transposed(src1));
  8365. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  8366. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8367. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8368. const int64_t ne01 = src00->ne[1];
  8369. const int64_t ne02 = src00->ne[2];
  8370. const int64_t ne03 = src00->ne[3];
  8371. //const int64_t nb01 = src00->nb[1];
  8372. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8373. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8374. const int64_t ne10 = src1->ne[0];
  8375. const int64_t ne11 = src1->ne[1];
  8376. const int64_t ne12 = src1->ne[2];
  8377. const int64_t ne13 = src1->ne[3];
  8378. //const int64_t nb11 = src1->nb[1];
  8379. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8380. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8381. const int64_t ne1 = ggml_nelements(src1);
  8382. const int64_t ne = ggml_nelements(dst);
  8383. ggml_cuda_set_device(g_main_device);
  8384. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8385. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8386. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8387. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8388. //half * src0_as_f16 = (half *) src0_ddq;
  8389. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8390. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8391. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8392. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8393. // convert src1 to fp16
  8394. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8395. GGML_ASSERT(to_fp16_cuda != nullptr);
  8396. size_t src1_as = 0;
  8397. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8398. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8399. size_t dst_as = 0;
  8400. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8401. GGML_ASSERT(ne12 % ne02 == 0);
  8402. GGML_ASSERT(ne13 % ne03 == 0);
  8403. // broadcast factors
  8404. const int64_t r2 = ne12/ne02;
  8405. const int64_t r3 = ne13/ne03;
  8406. const half alpha_f16 = 1.0f;
  8407. const half beta_f16 = 0.0f;
  8408. // use cublasGemmBatchedEx
  8409. const int ne23 = ne12*ne13;
  8410. const void ** ptrs_src = nullptr;
  8411. void ** ptrs_dst = nullptr;
  8412. size_t ptrs_src_s = 0;
  8413. size_t ptrs_dst_s = 0;
  8414. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8415. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8416. int64_t src0_ne = ggml_nelements(src00);
  8417. half * src0_as_f16 = nullptr;
  8418. size_t src0_as = 0;
  8419. if (src00->type != GGML_TYPE_F16) {
  8420. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8421. }
  8422. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8423. dim3 block_dims(ne13, ne12);
  8424. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8425. ptrs_src, ptrs_dst,
  8426. ne12, ne13,
  8427. ne23,
  8428. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8429. nb12, nb13,
  8430. dst->nb[2], dst->nb[3],
  8431. r2, r3,
  8432. src00->type, src0_as_f16, src0_ne,
  8433. src1_as_f16, dst_f16,
  8434. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8435. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8436. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8437. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8438. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8439. );
  8440. CUDA_CHECK(cudaGetLastError());
  8441. CUBLAS_CHECK(
  8442. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8443. ne01, ne11, ne10,
  8444. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8445. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8446. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8447. ne23,
  8448. CUBLAS_COMPUTE_16F,
  8449. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8450. if (src0_as != 0) {
  8451. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8452. }
  8453. if (ptrs_src_s != 0) {
  8454. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8455. }
  8456. if (ptrs_dst_s != 0) {
  8457. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8458. }
  8459. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8460. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8461. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8462. ggml_cuda_pool_free(dst_f16, dst_as);
  8463. }
  8464. #endif
  8465. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8466. #if 0
  8467. ggml_cuda_mul_mat_id_cublas(dst);
  8468. // TODO: mmq/mmv support
  8469. #endif
  8470. const size_t nb11 = src1->nb[1];
  8471. const size_t nb1 = dst->nb[1];
  8472. const struct ggml_tensor * ids = src0;
  8473. const int32_t id = ((int32_t *) dst->op_params)[0];
  8474. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  8475. std::vector<char> ids_host(ggml_nbytes(ids));
  8476. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  8477. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  8478. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  8479. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  8480. CUDA_CHECK(cudaStreamSynchronize(stream));
  8481. } else {
  8482. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  8483. }
  8484. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  8485. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  8486. ggml_tensor_extra_gpu src1_row_extra;
  8487. ggml_tensor_extra_gpu dst_row_extra;
  8488. ggml_tensor src1_row = *src1;
  8489. ggml_tensor dst_row = *dst;
  8490. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  8491. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  8492. src1_row.extra = &src1_row_extra;
  8493. dst_row.extra = &dst_row_extra;
  8494. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  8495. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  8496. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  8497. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  8498. if (src1->ne[1] == 1) {
  8499. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  8500. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  8501. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8502. //int32_t row_id;
  8503. //CUDA_CHECK(cudaMemcpyAsync(&row_id, ids_dev + i01*ids->nb[1] + id*ids->nb[0], sizeof(int32_t), cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  8504. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  8505. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8506. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8507. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8508. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  8509. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  8510. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  8511. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  8512. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8513. }
  8514. } else {
  8515. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  8516. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  8517. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  8518. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  8519. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_TYPE_CPU ?
  8520. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  8521. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_TYPE_CPU ?
  8522. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  8523. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  8524. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8525. int64_t num_src1_rows = 0;
  8526. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8527. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8528. if (row_id_i != row_id) {
  8529. continue;
  8530. }
  8531. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8532. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  8533. nb11, src1_kind, stream));
  8534. num_src1_rows++;
  8535. }
  8536. if (num_src1_rows == 0) {
  8537. continue;
  8538. }
  8539. src1_row.ne[1] = num_src1_rows;
  8540. dst_row.ne[1] = num_src1_rows;
  8541. src1_row.nb[1] = nb11;
  8542. src1_row.nb[2] = num_src1_rows*nb11;
  8543. src1_row.nb[3] = num_src1_rows*nb11;
  8544. dst_row.nb[1] = nb1;
  8545. dst_row.nb[2] = num_src1_rows*nb1;
  8546. dst_row.nb[3] = num_src1_rows*nb1;
  8547. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8548. num_src1_rows = 0;
  8549. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8550. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8551. if (row_id_i != row_id) {
  8552. continue;
  8553. }
  8554. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8555. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  8556. nb1, dst_kind, stream));
  8557. num_src1_rows++;
  8558. }
  8559. }
  8560. }
  8561. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  8562. CUDA_CHECK(cudaStreamSynchronize(stream));
  8563. }
  8564. }
  8565. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8566. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  8567. }
  8568. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8569. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  8570. }
  8571. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8572. const int64_t ne = ggml_nelements(src0);
  8573. GGML_ASSERT(ne == ggml_nelements(src1));
  8574. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  8575. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  8576. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  8577. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  8578. const int64_t ne00 = src0->ne[0];
  8579. const int64_t ne01 = src0->ne[1];
  8580. const int64_t ne02 = src0->ne[2];
  8581. //GGML_ASSERT(src0->ne[3] == 1);
  8582. const int64_t nb00 = src0->nb[0];
  8583. const int64_t nb01 = src0->nb[1];
  8584. const int64_t nb02 = src0->nb[2];
  8585. const int64_t nb03 = src0->nb[3];
  8586. const int64_t ne10 = src1->ne[0];
  8587. const int64_t ne11 = src1->ne[1];
  8588. const int64_t ne12 = src1->ne[2];
  8589. //GGML_ASSERT(src1->ne[3] == 1);
  8590. const int64_t nb10 = src1->nb[0];
  8591. const int64_t nb11 = src1->nb[1];
  8592. const int64_t nb12 = src1->nb[2];
  8593. const int64_t nb13 = src1->nb[3];
  8594. ggml_cuda_set_device(g_main_device);
  8595. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8596. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8597. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8598. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8599. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  8600. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  8601. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8602. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  8603. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8604. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  8605. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8606. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  8607. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8608. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  8609. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8610. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  8611. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8612. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  8613. ggml_cpy_f16_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8614. } else {
  8615. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  8616. ggml_type_name(src0->type), ggml_type_name(src1->type));
  8617. GGML_ASSERT(false);
  8618. }
  8619. (void) dst;
  8620. }
  8621. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8622. // TODO: why do we pass dst as src1 here?
  8623. ggml_cuda_cpy(src0, dst, nullptr);
  8624. (void) src1;
  8625. }
  8626. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8627. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  8628. }
  8629. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8630. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  8631. }
  8632. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8633. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8634. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  8635. }
  8636. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8637. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  8638. }
  8639. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8640. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  8641. }
  8642. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8643. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  8644. }
  8645. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8646. GGML_ASSERT(ggml_is_contiguous(src0));
  8647. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  8648. }
  8649. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8650. GGML_ASSERT(ggml_is_contiguous(src0));
  8651. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  8652. }
  8653. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8654. (void) src0;
  8655. (void) src1;
  8656. (void) dst;
  8657. }
  8658. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  8659. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  8660. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  8661. }
  8662. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  8663. if (main_device >= g_device_count) {
  8664. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8665. main_device, g_device_count, g_main_device);
  8666. return;
  8667. }
  8668. if (g_main_device != main_device && g_device_count > 1) {
  8669. g_main_device = main_device;
  8670. //cudaDeviceProp prop;
  8671. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8672. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8673. }
  8674. }
  8675. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8676. if (!g_cublas_loaded) return false;
  8677. ggml_cuda_func_t func;
  8678. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  8679. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  8680. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  8681. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8682. return false;
  8683. }
  8684. if (tensor->op == GGML_OP_MUL_MAT) {
  8685. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8686. #ifndef NDEBUG
  8687. 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]);
  8688. #endif
  8689. return false;
  8690. }
  8691. }
  8692. switch (tensor->op) {
  8693. case GGML_OP_REPEAT:
  8694. func = ggml_cuda_repeat;
  8695. break;
  8696. case GGML_OP_GET_ROWS:
  8697. func = ggml_cuda_get_rows;
  8698. break;
  8699. case GGML_OP_DUP:
  8700. func = ggml_cuda_dup;
  8701. break;
  8702. case GGML_OP_ADD:
  8703. func = ggml_cuda_add;
  8704. break;
  8705. case GGML_OP_ACC:
  8706. func = ggml_cuda_acc;
  8707. break;
  8708. case GGML_OP_MUL:
  8709. func = ggml_cuda_mul;
  8710. break;
  8711. case GGML_OP_DIV:
  8712. func = ggml_cuda_div;
  8713. break;
  8714. case GGML_OP_UNARY:
  8715. switch (ggml_get_unary_op(tensor)) {
  8716. case GGML_UNARY_OP_GELU:
  8717. func = ggml_cuda_gelu;
  8718. break;
  8719. case GGML_UNARY_OP_SILU:
  8720. func = ggml_cuda_silu;
  8721. break;
  8722. case GGML_UNARY_OP_GELU_QUICK:
  8723. func = ggml_cuda_gelu_quick;
  8724. break;
  8725. case GGML_UNARY_OP_TANH:
  8726. func = ggml_cuda_tanh;
  8727. break;
  8728. case GGML_UNARY_OP_RELU:
  8729. func = ggml_cuda_relu;
  8730. break;
  8731. case GGML_UNARY_OP_HARDSIGMOID:
  8732. func = ggml_cuda_hardsigmoid;
  8733. break;
  8734. case GGML_UNARY_OP_HARDSWISH:
  8735. func = ggml_cuda_hardswish;
  8736. break;
  8737. default:
  8738. return false;
  8739. }
  8740. break;
  8741. case GGML_OP_NORM:
  8742. func = ggml_cuda_norm;
  8743. break;
  8744. case GGML_OP_GROUP_NORM:
  8745. func = ggml_cuda_group_norm;
  8746. break;
  8747. case GGML_OP_CONCAT:
  8748. func = ggml_cuda_concat;
  8749. break;
  8750. case GGML_OP_UPSCALE:
  8751. func = ggml_cuda_upscale;
  8752. break;
  8753. case GGML_OP_PAD:
  8754. func = ggml_cuda_pad;
  8755. break;
  8756. case GGML_OP_ARANGE:
  8757. func = ggml_cuda_arange;
  8758. break;
  8759. case GGML_OP_TIMESTEP_EMBEDDING:
  8760. func = ggml_cuda_timestep_embedding;
  8761. break;
  8762. case GGML_OP_LEAKY_RELU:
  8763. func = ggml_cuda_leaky_relu;
  8764. break;
  8765. case GGML_OP_RMS_NORM:
  8766. func = ggml_cuda_rms_norm;
  8767. break;
  8768. case GGML_OP_MUL_MAT:
  8769. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8770. return false;
  8771. }
  8772. func = ggml_cuda_mul_mat;
  8773. break;
  8774. case GGML_OP_MUL_MAT_ID:
  8775. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8776. return false;
  8777. }
  8778. func = ggml_cuda_mul_mat_id;
  8779. break;
  8780. case GGML_OP_SCALE:
  8781. func = ggml_cuda_scale;
  8782. break;
  8783. case GGML_OP_SQR:
  8784. func = ggml_cuda_sqr;
  8785. break;
  8786. case GGML_OP_CLAMP:
  8787. func = ggml_cuda_clamp;
  8788. break;
  8789. case GGML_OP_CPY:
  8790. func = ggml_cuda_cpy;
  8791. break;
  8792. case GGML_OP_CONT:
  8793. func = ggml_cuda_dup;
  8794. break;
  8795. case GGML_OP_NONE:
  8796. case GGML_OP_RESHAPE:
  8797. case GGML_OP_VIEW:
  8798. case GGML_OP_PERMUTE:
  8799. case GGML_OP_TRANSPOSE:
  8800. func = ggml_cuda_nop;
  8801. break;
  8802. case GGML_OP_DIAG_MASK_INF:
  8803. func = ggml_cuda_diag_mask_inf;
  8804. break;
  8805. case GGML_OP_SOFT_MAX:
  8806. func = ggml_cuda_soft_max;
  8807. break;
  8808. case GGML_OP_ROPE:
  8809. func = ggml_cuda_rope;
  8810. break;
  8811. case GGML_OP_ALIBI:
  8812. func = ggml_cuda_alibi;
  8813. break;
  8814. case GGML_OP_IM2COL:
  8815. func = ggml_cuda_im2col;
  8816. break;
  8817. case GGML_OP_POOL_2D:
  8818. func = ggml_cuda_pool2d;
  8819. break;
  8820. case GGML_OP_SUM_ROWS:
  8821. func = ggml_cuda_sum_rows;
  8822. break;
  8823. case GGML_OP_ARGSORT:
  8824. func = ggml_cuda_argsort;
  8825. break;
  8826. default:
  8827. return false;
  8828. }
  8829. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  8830. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8831. }
  8832. if (params->ith != 0) {
  8833. return true;
  8834. }
  8835. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  8836. return true;
  8837. }
  8838. func(tensor->src[0], tensor->src[1], tensor);
  8839. return true;
  8840. }
  8841. GGML_CALL int ggml_cuda_get_device_count() {
  8842. int device_count;
  8843. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8844. return 0;
  8845. }
  8846. return device_count;
  8847. }
  8848. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8849. cudaDeviceProp prop;
  8850. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8851. snprintf(description, description_size, "%s", prop.name);
  8852. }
  8853. ////////////////////////////////////////////////////////////////////////////////
  8854. // backend interface
  8855. #define UNUSED GGML_UNUSED
  8856. struct ggml_backend_cuda_context {
  8857. int device;
  8858. std::string name;
  8859. };
  8860. // cuda buffer
  8861. struct ggml_backend_cuda_buffer_context {
  8862. int device;
  8863. void * dev_ptr = nullptr;
  8864. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8865. size_t temp_tensor_extra_index = 0;
  8866. std::string name;
  8867. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  8868. device(device), dev_ptr(dev_ptr),
  8869. name(GGML_CUDA_NAME + std::to_string(device)) {
  8870. }
  8871. ~ggml_backend_cuda_buffer_context() {
  8872. delete[] temp_tensor_extras;
  8873. }
  8874. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8875. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  8876. if (temp_tensor_extras == nullptr) {
  8877. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8878. }
  8879. size_t alloc_index = temp_tensor_extra_index;
  8880. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8881. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8882. memset(extra, 0, sizeof(*extra));
  8883. return extra;
  8884. }
  8885. };
  8886. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  8887. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8888. return ctx->name.c_str();
  8889. }
  8890. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  8891. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  8892. }
  8893. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8894. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8895. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8896. delete ctx;
  8897. }
  8898. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8899. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8900. return ctx->dev_ptr;
  8901. }
  8902. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8903. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8904. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8905. assert(tensor->view_src->buffer->buft == buffer->buft);
  8906. tensor->backend = tensor->view_src->backend;
  8907. tensor->extra = tensor->view_src->extra;
  8908. return;
  8909. }
  8910. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8911. extra->data_device[ctx->device] = tensor->data;
  8912. tensor->backend = GGML_BACKEND_TYPE_GPU;
  8913. tensor->extra = extra;
  8914. if (ggml_is_quantized(tensor->type)) {
  8915. // initialize padding to 0 to avoid possible NaN values
  8916. size_t original_size = ggml_nbytes(tensor);
  8917. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8918. if (padded_size > original_size && tensor->view_src == nullptr) {
  8919. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  8920. }
  8921. }
  8922. }
  8923. GGML_CALL static void ggml_backend_cuda_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8924. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  8925. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8926. ggml_cuda_set_device(ctx->device);
  8927. CUDA_CHECK(cudaDeviceSynchronize());
  8928. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8929. CUDA_CHECK(cudaDeviceSynchronize());
  8930. }
  8931. GGML_CALL static void ggml_backend_cuda_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8932. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  8933. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8934. ggml_cuda_set_device(ctx->device);
  8935. CUDA_CHECK(cudaDeviceSynchronize());
  8936. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8937. CUDA_CHECK(cudaDeviceSynchronize());
  8938. }
  8939. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8940. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  8941. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  8942. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8943. ggml_cuda_set_device(src_ctx->device);
  8944. CUDA_CHECK(cudaDeviceSynchronize());
  8945. ggml_cuda_set_device(dst_ctx->device);
  8946. CUDA_CHECK(cudaDeviceSynchronize());
  8947. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  8948. CUDA_CHECK(cudaDeviceSynchronize());
  8949. return true;
  8950. }
  8951. return false;
  8952. }
  8953. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8954. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8955. ggml_cuda_set_device(ctx->device);
  8956. CUDA_CHECK(cudaDeviceSynchronize());
  8957. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8958. CUDA_CHECK(cudaDeviceSynchronize());
  8959. }
  8960. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  8961. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  8962. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8963. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8964. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8965. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8966. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8967. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  8968. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8969. /* .reset = */ NULL,
  8970. };
  8971. // cuda buffer type
  8972. struct ggml_backend_cuda_buffer_type_context {
  8973. int device;
  8974. std::string name;
  8975. };
  8976. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8977. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8978. return ctx->name.c_str();
  8979. }
  8980. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8981. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8982. ggml_cuda_set_device(buft_ctx->device);
  8983. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  8984. void * dev_ptr;
  8985. cudaError_t err = cudaMalloc(&dev_ptr, size);
  8986. if (err != cudaSuccess) {
  8987. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  8988. return nullptr;
  8989. }
  8990. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  8991. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  8992. }
  8993. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8994. return 128;
  8995. UNUSED(buft);
  8996. }
  8997. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8998. size_t size = ggml_nbytes(tensor);
  8999. int64_t ne0 = tensor->ne[0];
  9000. if (ggml_is_quantized(tensor->type)) {
  9001. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9002. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9003. }
  9004. }
  9005. return size;
  9006. UNUSED(buft);
  9007. }
  9008. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9009. if (!ggml_backend_is_cuda(backend)) {
  9010. return false;
  9011. }
  9012. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9013. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9014. return buft_ctx->device == cuda_ctx->device;
  9015. }
  9016. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  9017. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  9018. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  9019. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  9020. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9021. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  9022. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  9023. /* .is_host = */ NULL,
  9024. };
  9025. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  9026. // FIXME: this is not thread safe
  9027. if (device >= ggml_backend_cuda_get_device_count()) {
  9028. return nullptr;
  9029. }
  9030. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  9031. static bool ggml_backend_cuda_buffer_type_initialized = false;
  9032. if (!ggml_backend_cuda_buffer_type_initialized) {
  9033. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  9034. ggml_backend_cuda_buffer_types[i] = {
  9035. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  9036. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  9037. };
  9038. }
  9039. ggml_backend_cuda_buffer_type_initialized = true;
  9040. }
  9041. return &ggml_backend_cuda_buffer_types[device];
  9042. }
  9043. // cuda split buffer
  9044. struct ggml_backend_cuda_split_buffer_context {
  9045. ~ggml_backend_cuda_split_buffer_context() {
  9046. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  9047. for (int id = 0; id < g_device_count; ++id) {
  9048. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9049. if (extra->events[id][is] != nullptr) {
  9050. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  9051. }
  9052. }
  9053. if (extra->data_device[id] != nullptr) {
  9054. CUDA_CHECK(cudaFree(extra->data_device[id]));
  9055. }
  9056. }
  9057. delete extra;
  9058. }
  9059. }
  9060. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  9061. };
  9062. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  9063. return GGML_CUDA_NAME "_Split";
  9064. UNUSED(buffer);
  9065. }
  9066. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  9067. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  9068. UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  9069. }
  9070. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9071. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9072. delete ctx;
  9073. }
  9074. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  9075. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  9076. return (void *)0x1000;
  9077. UNUSED(buffer);
  9078. }
  9079. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9080. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  9081. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9082. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9083. const int64_t ne0 = tensor->ne[0];
  9084. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  9085. ctx->tensor_extras.push_back(extra);
  9086. for (int id = 0; id < g_device_count; ++id) {
  9087. int64_t row_low, row_high;
  9088. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9089. int64_t nrows_split = row_high - row_low;
  9090. if (nrows_split == 0) {
  9091. continue;
  9092. }
  9093. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9094. const size_t original_size = size;
  9095. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9096. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9097. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9098. }
  9099. // FIXME: do not crash if cudaMalloc fails
  9100. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  9101. ggml_cuda_set_device(id);
  9102. char * buf;
  9103. CUDA_CHECK(cudaMalloc(&buf, size));
  9104. // set padding to 0 to avoid possible NaN values
  9105. if (size > original_size) {
  9106. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  9107. }
  9108. extra->data_device[id] = buf;
  9109. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9110. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  9111. }
  9112. }
  9113. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  9114. tensor->extra = extra;
  9115. }
  9116. GGML_CALL static void ggml_backend_cuda_split_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9117. // split tensors must always be set in their entirety at once
  9118. GGML_ASSERT(offset == 0);
  9119. GGML_ASSERT(size == ggml_nbytes(tensor));
  9120. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9121. const int64_t ne0 = tensor->ne[0];
  9122. const size_t nb1 = tensor->nb[1];
  9123. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9124. for (int id = 0; id < g_device_count; ++id) {
  9125. int64_t row_low, row_high;
  9126. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9127. int64_t nrows_split = row_high - row_low;
  9128. if (nrows_split == 0) {
  9129. continue;
  9130. }
  9131. const size_t offset_split = row_low*nb1;
  9132. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9133. const size_t original_size = size;
  9134. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9135. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9136. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9137. }
  9138. const char * buf_host = (const char *)data + offset_split;
  9139. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  9140. }
  9141. }
  9142. GGML_CALL static void ggml_backend_cuda_split_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9143. // split tensors must always be set in their entirety at once
  9144. GGML_ASSERT(offset == 0);
  9145. GGML_ASSERT(size == ggml_nbytes(tensor));
  9146. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9147. const int64_t ne0 = tensor->ne[0];
  9148. const size_t nb1 = tensor->nb[1];
  9149. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9150. for (int id = 0; id < g_device_count; ++id) {
  9151. int64_t row_low, row_high;
  9152. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9153. int64_t nrows_split = row_high - row_low;
  9154. if (nrows_split == 0) {
  9155. continue;
  9156. }
  9157. const size_t offset_split = row_low*nb1;
  9158. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9159. const size_t original_size = size;
  9160. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9161. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9162. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9163. }
  9164. char * buf_host = (char *)data + offset_split;
  9165. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  9166. }
  9167. }
  9168. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9169. UNUSED(buffer);
  9170. UNUSED(value);
  9171. }
  9172. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  9173. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  9174. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  9175. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  9176. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  9177. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  9178. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  9179. /* .cpy_tensor = */ NULL,
  9180. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  9181. /* .reset = */ NULL,
  9182. };
  9183. // cuda split buffer type
  9184. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9185. return GGML_CUDA_NAME "_Split";
  9186. UNUSED(buft);
  9187. }
  9188. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9189. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  9190. // instead, we allocate them for each tensor separately in init_tensor
  9191. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  9192. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  9193. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  9194. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  9195. }
  9196. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9197. return 128;
  9198. UNUSED(buft);
  9199. }
  9200. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9201. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9202. size_t total_size = 0;
  9203. const int64_t ne0 = tensor->ne[0];
  9204. for (int id = 0; id < g_device_count; ++id) {
  9205. int64_t row_low, row_high;
  9206. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9207. int64_t nrows_split = row_high - row_low;
  9208. if (nrows_split == 0) {
  9209. continue;
  9210. }
  9211. total_size += ggml_nbytes_split(tensor, nrows_split);
  9212. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9213. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9214. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9215. }
  9216. }
  9217. return total_size;
  9218. }
  9219. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9220. return ggml_backend_is_cuda(backend);
  9221. UNUSED(buft);
  9222. }
  9223. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9224. return false;
  9225. UNUSED(buft);
  9226. }
  9227. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9228. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9229. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9230. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9231. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9232. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9233. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9234. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9235. };
  9236. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9237. // FIXME: this is not thread safe
  9238. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9239. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9240. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9241. if (all_zero) {
  9242. tensor_split_arr = g_default_tensor_split;
  9243. } else {
  9244. float split_sum = 0.0f;
  9245. for (int i = 0; i < g_device_count; ++i) {
  9246. tensor_split_arr[i] = split_sum;
  9247. split_sum += tensor_split[i];
  9248. }
  9249. for (int i = 0; i < g_device_count; ++i) {
  9250. tensor_split_arr[i] /= split_sum;
  9251. }
  9252. }
  9253. auto it = buft_map.find(tensor_split_arr);
  9254. if (it != buft_map.end()) {
  9255. return &it->second;
  9256. }
  9257. struct ggml_backend_buffer_type buft {
  9258. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9259. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9260. };
  9261. auto result = buft_map.emplace(tensor_split_arr, buft);
  9262. return &result.first->second;
  9263. }
  9264. // host buffer type
  9265. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9266. return GGML_CUDA_NAME "_Host";
  9267. UNUSED(buft);
  9268. }
  9269. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9270. return GGML_CUDA_NAME "_Host";
  9271. UNUSED(buffer);
  9272. }
  9273. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9274. ggml_cuda_host_free(buffer->context);
  9275. }
  9276. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9277. void * ptr = ggml_cuda_host_malloc(size);
  9278. if (ptr == nullptr) {
  9279. // fallback to cpu buffer
  9280. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9281. }
  9282. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9283. buffer->buft = buft;
  9284. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9285. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9286. return buffer;
  9287. }
  9288. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9289. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9290. /* .iface = */ {
  9291. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9292. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9293. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9294. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9295. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9296. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9297. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9298. },
  9299. /* .context = */ nullptr,
  9300. };
  9301. return &ggml_backend_cuda_buffer_type_host;
  9302. }
  9303. // backend
  9304. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9305. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9306. return cuda_ctx->name.c_str();
  9307. }
  9308. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9309. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9310. delete cuda_ctx;
  9311. delete backend;
  9312. }
  9313. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9314. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9315. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9316. }
  9317. GGML_CALL static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9318. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9319. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9320. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9321. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9322. }
  9323. GGML_CALL static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9324. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9325. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9326. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  9327. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9328. }
  9329. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9330. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9331. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9332. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9333. return true;
  9334. }
  9335. return false;
  9336. }
  9337. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9338. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9339. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9340. UNUSED(backend);
  9341. }
  9342. GGML_CALL static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9343. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9344. ggml_cuda_set_main_device(cuda_ctx->device);
  9345. ggml_compute_params params = {};
  9346. params.type = GGML_TASK_TYPE_COMPUTE;
  9347. params.ith = 0;
  9348. for (int i = 0; i < cgraph->n_nodes; i++) {
  9349. ggml_tensor * node = cgraph->nodes[i];
  9350. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  9351. continue;
  9352. }
  9353. #ifndef NDEBUG
  9354. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9355. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9356. assert(node->extra != nullptr);
  9357. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9358. if (node->src[j] != nullptr) {
  9359. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  9360. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9361. assert(node->src[j]->extra != nullptr);
  9362. }
  9363. }
  9364. #endif
  9365. bool ok = ggml_cuda_compute_forward(&params, node);
  9366. if (!ok) {
  9367. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9368. }
  9369. GGML_ASSERT(ok);
  9370. }
  9371. return GGML_STATUS_SUCCESS;
  9372. }
  9373. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9374. switch (op->op) {
  9375. case GGML_OP_UNARY:
  9376. switch (ggml_get_unary_op(op)) {
  9377. case GGML_UNARY_OP_GELU:
  9378. case GGML_UNARY_OP_SILU:
  9379. case GGML_UNARY_OP_RELU:
  9380. case GGML_UNARY_OP_HARDSIGMOID:
  9381. case GGML_UNARY_OP_HARDSWISH:
  9382. case GGML_UNARY_OP_GELU_QUICK:
  9383. case GGML_UNARY_OP_TANH:
  9384. return true;
  9385. default:
  9386. return false;
  9387. }
  9388. break;
  9389. case GGML_OP_MUL_MAT:
  9390. case GGML_OP_MUL_MAT_ID:
  9391. {
  9392. struct ggml_tensor * a;
  9393. struct ggml_tensor * b;
  9394. if (op->op == GGML_OP_MUL_MAT) {
  9395. a = op->src[0];
  9396. b = op->src[1];
  9397. } else {
  9398. a = op->src[2];
  9399. b = op->src[1];
  9400. }
  9401. if (a->ne[3] != b->ne[3]) {
  9402. return false;
  9403. }
  9404. ggml_type a_type = a->type;
  9405. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  9406. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ3_S ||
  9407. a_type == GGML_TYPE_IQ2_S || a_type == GGML_TYPE_IQ4_XS) {
  9408. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9409. return false;
  9410. }
  9411. }
  9412. return true;
  9413. } break;
  9414. case GGML_OP_GET_ROWS:
  9415. {
  9416. switch (op->src[0]->type) {
  9417. case GGML_TYPE_F16:
  9418. case GGML_TYPE_F32:
  9419. case GGML_TYPE_Q4_0:
  9420. case GGML_TYPE_Q4_1:
  9421. case GGML_TYPE_Q5_0:
  9422. case GGML_TYPE_Q5_1:
  9423. case GGML_TYPE_Q8_0:
  9424. return true;
  9425. default:
  9426. return false;
  9427. }
  9428. } break;
  9429. case GGML_OP_CPY:
  9430. {
  9431. ggml_type src0_type = op->src[0]->type;
  9432. ggml_type src1_type = op->src[1]->type;
  9433. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9434. return true;
  9435. }
  9436. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9437. return true;
  9438. }
  9439. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9440. return true;
  9441. }
  9442. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9443. return true;
  9444. }
  9445. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9446. return true;
  9447. }
  9448. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9449. return true;
  9450. }
  9451. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9452. return true;
  9453. }
  9454. return false;
  9455. } break;
  9456. case GGML_OP_DUP:
  9457. case GGML_OP_REPEAT:
  9458. case GGML_OP_CONCAT:
  9459. {
  9460. ggml_type src0_type = op->src[0]->type;
  9461. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9462. } break;
  9463. case GGML_OP_NONE:
  9464. case GGML_OP_RESHAPE:
  9465. case GGML_OP_VIEW:
  9466. case GGML_OP_PERMUTE:
  9467. case GGML_OP_TRANSPOSE:
  9468. case GGML_OP_NORM:
  9469. case GGML_OP_ADD:
  9470. case GGML_OP_MUL:
  9471. case GGML_OP_DIV:
  9472. case GGML_OP_RMS_NORM:
  9473. case GGML_OP_SCALE:
  9474. case GGML_OP_SQR:
  9475. case GGML_OP_CLAMP:
  9476. case GGML_OP_CONT:
  9477. case GGML_OP_DIAG_MASK_INF:
  9478. case GGML_OP_SOFT_MAX:
  9479. case GGML_OP_ROPE:
  9480. case GGML_OP_ALIBI:
  9481. case GGML_OP_IM2COL:
  9482. case GGML_OP_POOL_2D:
  9483. case GGML_OP_SUM_ROWS:
  9484. case GGML_OP_ARGSORT:
  9485. case GGML_OP_ACC:
  9486. case GGML_OP_GROUP_NORM:
  9487. case GGML_OP_UPSCALE:
  9488. case GGML_OP_PAD:
  9489. case GGML_OP_ARANGE:
  9490. case GGML_OP_TIMESTEP_EMBEDDING:
  9491. case GGML_OP_LEAKY_RELU:
  9492. return true;
  9493. default:
  9494. return false;
  9495. }
  9496. UNUSED(backend);
  9497. }
  9498. static ggml_backend_i ggml_backend_cuda_interface = {
  9499. /* .get_name = */ ggml_backend_cuda_name,
  9500. /* .free = */ ggml_backend_cuda_free,
  9501. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  9502. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  9503. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  9504. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  9505. /* .synchronize = */ ggml_backend_cuda_synchronize,
  9506. /* .graph_plan_create = */ NULL,
  9507. /* .graph_plan_free = */ NULL,
  9508. /* .graph_plan_compute = */ NULL,
  9509. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  9510. /* .supports_op = */ ggml_backend_cuda_supports_op,
  9511. };
  9512. static ggml_guid_t ggml_backend_cuda_guid() {
  9513. static ggml_guid guid = { 0x2c, 0xdd, 0xe8, 0x1c, 0x65, 0xb3, 0x65, 0x73, 0x6a, 0x12, 0x88, 0x61, 0x1c, 0xc9, 0xdc, 0x25 };
  9514. return &guid;
  9515. }
  9516. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  9517. ggml_init_cublas(); // TODO: remove from ggml.c
  9518. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  9519. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  9520. return nullptr;
  9521. }
  9522. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  9523. ggml_cuda_set_main_device(device);
  9524. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  9525. /* .device = */ device,
  9526. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  9527. };
  9528. ggml_backend_t cuda_backend = new ggml_backend {
  9529. /* .guid = */ ggml_backend_cuda_guid(),
  9530. /* .interface = */ ggml_backend_cuda_interface,
  9531. /* .context = */ ctx
  9532. };
  9533. return cuda_backend;
  9534. }
  9535. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  9536. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cuda_guid());
  9537. }
  9538. GGML_CALL int ggml_backend_cuda_get_device_count() {
  9539. return ggml_cuda_get_device_count();
  9540. }
  9541. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  9542. ggml_cuda_get_device_description(device, description, description_size);
  9543. }
  9544. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  9545. ggml_cuda_set_device(device);
  9546. CUDA_CHECK(cudaMemGetInfo(free, total));
  9547. }
  9548. // backend registry
  9549. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  9550. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  9551. return cuda_backend;
  9552. UNUSED(params);
  9553. }
  9554. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  9555. GGML_CALL int ggml_backend_cuda_reg_devices() {
  9556. int device_count = ggml_cuda_get_device_count();
  9557. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  9558. for (int i = 0; i < device_count; i++) {
  9559. char name[128];
  9560. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  9561. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  9562. }
  9563. return device_count;
  9564. }