ggml-cuda.cu 450 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712
  1. #include <algorithm>
  2. #include <assert.h>
  3. #include <atomic>
  4. #include <cinttypes>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <float.h>
  8. #include <limits>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <string>
  12. #include <vector>
  13. #include <map>
  14. #include <array>
  15. // stringize macro for converting __CUDA_ARCH_LIST__ (list of integers) to string
  16. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  17. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  18. #if defined(GGML_USE_HIPBLAS)
  19. #include <hip/hip_runtime.h>
  20. #include <hipblas/hipblas.h>
  21. #include <hip/hip_fp16.h>
  22. #ifdef __HIP_PLATFORM_AMD__
  23. // for rocblas_initialize()
  24. #include "rocblas/rocblas.h"
  25. #endif // __HIP_PLATFORM_AMD__
  26. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  27. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  28. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  29. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  30. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  31. #define CUBLAS_OP_N HIPBLAS_OP_N
  32. #define CUBLAS_OP_T HIPBLAS_OP_T
  33. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  34. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  35. #define CUDA_R_16F HIPBLAS_R_16F
  36. #define CUDA_R_32F HIPBLAS_R_32F
  37. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  38. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  39. #define cublasCreate hipblasCreate
  40. #define cublasGemmEx hipblasGemmEx
  41. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  42. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  43. #define cublasHandle_t hipblasHandle_t
  44. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  45. #define cublasSetStream hipblasSetStream
  46. #define cublasSgemm hipblasSgemm
  47. #define cublasStatus_t hipblasStatus_t
  48. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  49. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  50. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  51. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  52. #define cudaDeviceProp hipDeviceProp_t
  53. #define cudaDeviceSynchronize hipDeviceSynchronize
  54. #define cudaError_t hipError_t
  55. #define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
  56. #define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
  57. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  58. #define cudaEventDisableTiming hipEventDisableTiming
  59. #define cudaEventRecord hipEventRecord
  60. #define cudaEvent_t hipEvent_t
  61. #define cudaEventDestroy hipEventDestroy
  62. #define cudaFree hipFree
  63. #define cudaFreeHost hipHostFree
  64. #define cudaGetDevice hipGetDevice
  65. #define cudaGetDeviceCount hipGetDeviceCount
  66. #define cudaGetDeviceProperties hipGetDeviceProperties
  67. #define cudaGetErrorString hipGetErrorString
  68. #define cudaGetLastError hipGetLastError
  69. #ifdef GGML_HIP_UMA
  70. #define cudaMalloc hipMallocManaged
  71. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  72. #else
  73. #define cudaMalloc hipMalloc
  74. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  75. #endif
  76. #define cudaMemcpy hipMemcpy
  77. #define cudaMemcpyAsync hipMemcpyAsync
  78. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  79. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  80. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  81. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  82. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  83. #define cudaMemcpyKind hipMemcpyKind
  84. #define cudaMemset hipMemset
  85. #define cudaMemsetAsync hipMemsetAsync
  86. #define cudaMemGetInfo hipMemGetInfo
  87. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  88. #define cudaSetDevice hipSetDevice
  89. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  90. #define cudaStreamFireAndForget hipStreamFireAndForget
  91. #define cudaStreamNonBlocking hipStreamNonBlocking
  92. #define cudaStreamSynchronize hipStreamSynchronize
  93. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  94. #define cudaStream_t hipStream_t
  95. #define cudaSuccess hipSuccess
  96. #define __trap abort
  97. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  98. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  99. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  100. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  101. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  102. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  103. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  104. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  105. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  106. #else
  107. #include <cuda_runtime.h>
  108. #include <cuda.h>
  109. #include <cublas_v2.h>
  110. #include <cuda_fp16.h>
  111. #if CUDART_VERSION < 11020
  112. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  113. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  114. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  115. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  116. #define cublasComputeType_t cudaDataType_t
  117. #endif // CUDART_VERSION < 11020
  118. #endif // defined(GGML_USE_HIPBLAS)
  119. // ggml-cuda need half type so keep ggml headers include at last
  120. #include "ggml-cuda.h"
  121. #include "ggml.h"
  122. #include "ggml-backend-impl.h"
  123. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  124. #define CC_PASCAL 600
  125. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  126. #define CC_VOLTA 700
  127. #define CC_OFFSET_AMD 1000000
  128. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  129. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  130. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  131. #define GGML_CUDA_MAX_NODES 8192
  132. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  133. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  134. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  135. // - 7B quantum model: +100-200 MB
  136. // - 13B quantum model: +200-400 MB
  137. //
  138. //#define GGML_CUDA_FORCE_MMQ
  139. // TODO: improve this to be correct for more hardware
  140. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  141. #if !defined(GGML_CUDA_FORCE_MMQ)
  142. #define CUDA_USE_TENSOR_CORES
  143. #endif
  144. #define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
  145. #define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
  146. #if defined(GGML_USE_HIPBLAS)
  147. #define __CUDA_ARCH__ 1300
  148. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  149. defined(__gfx1150__) || defined(__gfx1151__)
  150. #define RDNA3
  151. #endif
  152. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  153. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  154. #define RDNA2
  155. #endif
  156. #ifndef __has_builtin
  157. #define __has_builtin(x) 0
  158. #endif
  159. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  160. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  161. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  162. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  163. #if __has_builtin(__builtin_elementwise_sub_sat)
  164. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  165. return reinterpret_cast<const int &>(c);
  166. #else
  167. int8x4_t c;
  168. int16_t tmp;
  169. #pragma unroll
  170. for (int i = 0; i < 4; i++) {
  171. tmp = va[i] - vb[i];
  172. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  173. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  174. c[i] = tmp;
  175. }
  176. return reinterpret_cast<int &>(c);
  177. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  178. }
  179. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  180. return __vsubss4(a, b);
  181. }
  182. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  183. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  184. c = __builtin_amdgcn_sdot4(a, b, c, false);
  185. #elif defined(RDNA3)
  186. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  187. #elif defined(__gfx1010__) || defined(__gfx900__)
  188. int tmp1;
  189. int tmp2;
  190. asm("\n \
  191. 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 \
  192. 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 \
  193. v_add3_u32 %0, %1, %2, %0 \n \
  194. 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 \
  195. 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 \
  196. v_add3_u32 %0, %1, %2, %0 \n \
  197. "
  198. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  199. : "v"(a), "v"(b)
  200. );
  201. #else
  202. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  203. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  204. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  205. #endif
  206. return c;
  207. }
  208. #endif // defined(GGML_USE_HIPBLAS)
  209. #if defined(_MSC_VER)
  210. #pragma warning(disable: 4244 4267) // possible loss of data
  211. #endif
  212. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  213. [[noreturn]]
  214. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  215. int id = -1; // in case cudaGetDevice fails
  216. cudaGetDevice(&id);
  217. fprintf(stderr, "CUDA error: %s\n", msg);
  218. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  219. fprintf(stderr, " %s\n", stmt);
  220. // abort with GGML_ASSERT to get a stack trace
  221. GGML_ASSERT(!"CUDA error");
  222. }
  223. #define CUDA_CHECK_GEN(err, success, error_fn) \
  224. do { \
  225. auto err_ = (err); \
  226. if (err_ != (success)) { \
  227. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  228. } \
  229. } while (0)
  230. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  231. #if CUDART_VERSION >= 12000
  232. static const char * cublas_get_error_str(const cublasStatus_t err) {
  233. return cublasGetStatusString(err);
  234. }
  235. #else
  236. static const char * cublas_get_error_str(const cublasStatus_t err) {
  237. switch (err) {
  238. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  239. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  240. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  241. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  242. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  243. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  244. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  245. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  246. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  247. default: return "unknown error";
  248. }
  249. }
  250. #endif // CUDART_VERSION >= 12000
  251. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  252. #if !defined(GGML_USE_HIPBLAS)
  253. static const char * cu_get_error_str(CUresult err) {
  254. const char * err_str;
  255. cuGetErrorString(err, &err_str);
  256. return err_str;
  257. }
  258. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  259. #endif
  260. #if CUDART_VERSION >= 11100
  261. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  262. #else
  263. #define GGML_CUDA_ASSUME(x)
  264. #endif // CUDART_VERSION >= 11100
  265. #ifdef GGML_CUDA_F16
  266. typedef half dfloat; // dequantize float
  267. typedef half2 dfloat2;
  268. #else
  269. typedef float dfloat; // dequantize float
  270. typedef float2 dfloat2;
  271. #endif //GGML_CUDA_F16
  272. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  273. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  274. int x32 = 0;
  275. x32 |= x16[0] << 0;
  276. x32 |= x16[1] << 16;
  277. return x32;
  278. }
  279. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  280. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  281. int x32 = 0;
  282. x32 |= x16[0] << 0;
  283. x32 |= x16[1] << 16;
  284. return x32;
  285. }
  286. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  287. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  288. }
  289. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  290. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  291. }
  292. template<typename T>
  293. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  294. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  295. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  296. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  297. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  298. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  299. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  300. typedef void (*ggml_cuda_op_mul_mat_t)(
  301. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  302. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  303. const int64_t src1_padded_row_size, cudaStream_t stream);
  304. typedef void (*ggml_cuda_op_flatten_t)(
  305. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  306. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  307. // QK = number of values after dequantization
  308. // QR = QK / number of values before dequantization
  309. // QI = number of 32 bit integers before dequantization
  310. #define QK4_0 32
  311. #define QR4_0 2
  312. #define QI4_0 (QK4_0 / (4 * QR4_0))
  313. typedef struct {
  314. half d; // delta
  315. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  316. } block_q4_0;
  317. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  318. #define QK4_1 32
  319. #define QR4_1 2
  320. #define QI4_1 (QK4_1 / (4 * QR4_1))
  321. typedef struct {
  322. half2 dm; // dm.x = delta, dm.y = min
  323. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  324. } block_q4_1;
  325. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  326. #define QK5_0 32
  327. #define QR5_0 2
  328. #define QI5_0 (QK5_0 / (4 * QR5_0))
  329. typedef struct {
  330. half d; // delta
  331. uint8_t qh[4]; // 5-th bit of quants
  332. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  333. } block_q5_0;
  334. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  335. #define QK5_1 32
  336. #define QR5_1 2
  337. #define QI5_1 (QK5_1 / (4 * QR5_1))
  338. typedef struct {
  339. half2 dm; // dm.x = delta, dm.y = min
  340. uint8_t qh[4]; // 5-th bit of quants
  341. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  342. } block_q5_1;
  343. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  344. #define QK8_0 32
  345. #define QR8_0 1
  346. #define QI8_0 (QK8_0 / (4 * QR8_0))
  347. typedef struct {
  348. half d; // delta
  349. int8_t qs[QK8_0]; // quants
  350. } block_q8_0;
  351. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  352. #define QK8_1 32
  353. #define QR8_1 1
  354. #define QI8_1 (QK8_1 / (4 * QR8_1))
  355. typedef struct {
  356. half2 ds; // ds.x = delta, ds.y = sum
  357. int8_t qs[QK8_0]; // quants
  358. } block_q8_1;
  359. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  360. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  361. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  362. typedef void (*load_tiles_cuda_t)(
  363. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  364. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  365. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  366. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  367. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  368. //================================= k-quants
  369. #ifdef GGML_QKK_64
  370. #define QK_K 64
  371. #define K_SCALE_SIZE 4
  372. #else
  373. #define QK_K 256
  374. #define K_SCALE_SIZE 12
  375. #endif
  376. #define QR2_K 4
  377. #define QI2_K (QK_K / (4*QR2_K))
  378. typedef struct {
  379. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  380. uint8_t qs[QK_K/4]; // quants
  381. half2 dm; // super-block scale for quantized scales/mins
  382. } block_q2_K;
  383. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  384. #define QR3_K 4
  385. #define QI3_K (QK_K / (4*QR3_K))
  386. typedef struct {
  387. uint8_t hmask[QK_K/8]; // quants - high bit
  388. uint8_t qs[QK_K/4]; // quants - low 2 bits
  389. #ifdef GGML_QKK_64
  390. uint8_t scales[2]; // scales, quantized with 8 bits
  391. #else
  392. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  393. #endif
  394. half d; // super-block scale
  395. } block_q3_K;
  396. //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");
  397. #define QR4_K 2
  398. #define QI4_K (QK_K / (4*QR4_K))
  399. #ifdef GGML_QKK_64
  400. typedef struct {
  401. half dm[2]; // super-block scales/mins
  402. uint8_t scales[2]; // 4-bit block scales/mins
  403. uint8_t qs[QK_K/2]; // 4--bit quants
  404. } block_q4_K;
  405. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  406. #else
  407. typedef struct {
  408. half2 dm; // super-block scale for quantized scales/mins
  409. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  410. uint8_t qs[QK_K/2]; // 4--bit quants
  411. } block_q4_K;
  412. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  413. #endif
  414. #define QR5_K 2
  415. #define QI5_K (QK_K / (4*QR5_K))
  416. #ifdef GGML_QKK_64
  417. typedef struct {
  418. half d; // super-block scale
  419. int8_t scales[QK_K/16]; // block scales
  420. uint8_t qh[QK_K/8]; // quants, high bit
  421. uint8_t qs[QK_K/2]; // quants, low 4 bits
  422. } block_q5_K;
  423. 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");
  424. #else
  425. typedef struct {
  426. half2 dm; // super-block scale for quantized scales/mins
  427. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  428. uint8_t qh[QK_K/8]; // quants, high bit
  429. uint8_t qs[QK_K/2]; // quants, low 4 bits
  430. } block_q5_K;
  431. 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");
  432. #endif
  433. #define QR6_K 2
  434. #define QI6_K (QK_K / (4*QR6_K))
  435. typedef struct {
  436. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  437. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  438. int8_t scales[QK_K/16]; // scales
  439. half d; // delta
  440. } block_q6_K;
  441. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  442. #define QR2_XXS 8
  443. #define QI2_XXS (QK_K / (4*QR2_XXS))
  444. typedef struct {
  445. half d;
  446. uint16_t qs[QK_K/8];
  447. } block_iq2_xxs;
  448. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  449. #define QR2_XS 8
  450. #define QI2_XS (QK_K / (4*QR2_XS))
  451. typedef struct {
  452. half d;
  453. uint16_t qs[QK_K/8];
  454. uint8_t scales[QK_K/32];
  455. } block_iq2_xs;
  456. 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");
  457. #define QR3_XXS 8
  458. #define QI3_XXS (QK_K / (4*QR3_XXS))
  459. typedef struct {
  460. half d;
  461. uint8_t qs[3*(QK_K/8)];
  462. } block_iq3_xxs;
  463. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_fp16_t) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  464. #define QR1_S 8
  465. #define QI1_S (QK_K / (4*QR1_S))
  466. typedef struct {
  467. half d;
  468. uint8_t qs[QK_K/8];
  469. uint8_t scales[QK_K/16];
  470. } block_iq1_s;
  471. static_assert(sizeof(block_iq1_s) == sizeof(ggml_fp16_t) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  472. #define QK4_NL 32
  473. #define QR4_NL 2
  474. #define QI4_NL (QK4_NL / (4*QR4_NL))
  475. typedef struct {
  476. half d;
  477. uint8_t qs[QK4_NL/2];
  478. } block_iq4_nl;
  479. static_assert(sizeof(block_iq4_nl) == sizeof(ggml_fp16_t) + QK4_NL/2, "wrong iq4_nl block size/padding");
  480. #define WARP_SIZE 32
  481. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  482. #define CUDA_GELU_BLOCK_SIZE 256
  483. #define CUDA_SILU_BLOCK_SIZE 256
  484. #define CUDA_TANH_BLOCK_SIZE 256
  485. #define CUDA_RELU_BLOCK_SIZE 256
  486. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  487. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  488. #define CUDA_SQR_BLOCK_SIZE 256
  489. #define CUDA_CPY_BLOCK_SIZE 32
  490. #define CUDA_SCALE_BLOCK_SIZE 256
  491. #define CUDA_CLAMP_BLOCK_SIZE 256
  492. #define CUDA_ROPE_BLOCK_SIZE 256
  493. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  494. #define CUDA_ALIBI_BLOCK_SIZE 32
  495. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  496. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  497. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  498. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  499. #define CUDA_UPSCALE_BLOCK_SIZE 256
  500. #define CUDA_CONCAT_BLOCK_SIZE 256
  501. #define CUDA_PAD_BLOCK_SIZE 256
  502. #define CUDA_ACC_BLOCK_SIZE 256
  503. #define CUDA_IM2COL_BLOCK_SIZE 256
  504. #define CUDA_POOL2D_BLOCK_SIZE 256
  505. #define CUDA_Q8_0_NE_ALIGN 2048
  506. // dmmv = dequantize_mul_mat_vec
  507. #ifndef GGML_CUDA_DMMV_X
  508. #define GGML_CUDA_DMMV_X 32
  509. #endif
  510. #ifndef GGML_CUDA_MMV_Y
  511. #define GGML_CUDA_MMV_Y 1
  512. #endif
  513. #ifndef K_QUANTS_PER_ITERATION
  514. #define K_QUANTS_PER_ITERATION 2
  515. #else
  516. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  517. #endif
  518. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  519. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  520. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  521. #define MUL_MAT_SRC1_COL_STRIDE 128
  522. #define MAX_STREAMS 8
  523. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  524. struct ggml_tensor_extra_gpu {
  525. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  526. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  527. };
  528. // this is faster on Windows
  529. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  530. static void ggml_cuda_set_device(const int device) {
  531. int current_device;
  532. CUDA_CHECK(cudaGetDevice(&current_device));
  533. if (device == current_device) {
  534. return;
  535. }
  536. CUDA_CHECK(cudaSetDevice(device));
  537. }
  538. static int g_device_count = -1;
  539. static int g_main_device = 0;
  540. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  541. struct cuda_device_capabilities {
  542. int cc; // compute capability
  543. size_t smpb; // max. shared memory per block
  544. bool vmm; // virtual memory support
  545. size_t vmm_granularity; // granularity of virtual memory
  546. };
  547. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  548. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  549. [[noreturn]]
  550. static __device__ void no_device_code(
  551. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  552. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  553. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  554. file_name, line, function_name, arch);
  555. (void) arch_list;
  556. #else
  557. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  558. file_name, line, function_name, arch, arch_list);
  559. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  560. __trap();
  561. (void) no_device_code; // suppress unused function warning
  562. }
  563. #ifdef __CUDA_ARCH__
  564. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  565. #else
  566. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  567. #endif // __CUDA_ARCH__
  568. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  569. #pragma unroll
  570. for (int mask = 16; mask > 0; mask >>= 1) {
  571. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  572. }
  573. return x;
  574. }
  575. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  576. #pragma unroll
  577. for (int mask = 16; mask > 0; mask >>= 1) {
  578. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  579. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  580. }
  581. return a;
  582. }
  583. //static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  584. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  585. //#pragma unroll
  586. // for (int mask = 16; mask > 0; mask >>= 1) {
  587. // a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  588. // }
  589. // return a;
  590. //#else
  591. // (void) a;
  592. // NO_DEVICE_CODE;
  593. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  594. //}
  595. static __device__ __forceinline__ float warp_reduce_max(float x) {
  596. #pragma unroll
  597. for (int mask = 16; mask > 0; mask >>= 1) {
  598. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  599. }
  600. return x;
  601. }
  602. //static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  603. //#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  604. //#pragma unroll
  605. // for (int mask = 16; mask > 0; mask >>= 1) {
  606. // x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  607. // }
  608. // return x;
  609. //#else
  610. // (void) x;
  611. // NO_DEVICE_CODE;
  612. //#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  613. //}
  614. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  615. return b;
  616. GGML_UNUSED(a);
  617. }
  618. static __device__ __forceinline__ float op_add(const float a, const float b) {
  619. return a + b;
  620. }
  621. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  622. return a * b;
  623. }
  624. static __device__ __forceinline__ float op_div(const float a, const float b) {
  625. return a / b;
  626. }
  627. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  628. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  629. int ne0, int ne1, int ne2, int ne3,
  630. int ne10, int ne11, int ne12, int ne13,
  631. /*int s0, */ int s1, int s2, int s3,
  632. /*int s10,*/ int s11, int s12, int s13) {
  633. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  634. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  635. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  636. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  637. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  638. return;
  639. }
  640. const int i11 = i1 % ne11;
  641. const int i12 = i2 % ne12;
  642. const int i13 = i3 % ne13;
  643. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  644. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  645. const size_t i_dst = i_src0;
  646. const src0_t * src0_row = src0 + i_src0;
  647. const src1_t * src1_row = src1 + i_src1;
  648. dst_t * dst_row = dst + i_dst;
  649. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  650. const int i10 = i0 % ne10;
  651. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  652. }
  653. }
  654. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  655. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  656. int ne0, int ne1, int ne2, int ne3,
  657. int ne10, int ne11, int ne12, int ne13,
  658. /*int s0, */ int s1, int s2, int s3,
  659. /*int s10,*/ int s11, int s12, int s13) {
  660. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  661. const int i3 = i/(ne2*ne1*ne0);
  662. const int i2 = (i/(ne1*ne0)) % ne2;
  663. const int i1 = (i/ne0) % ne1;
  664. const int i0 = i % ne0;
  665. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  666. return;
  667. }
  668. const int i11 = i1 % ne11;
  669. const int i12 = i2 % ne12;
  670. const int i13 = i3 % ne13;
  671. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  672. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  673. const size_t i_dst = i_src0;
  674. const src0_t * src0_row = src0 + i_src0;
  675. const src1_t * src1_row = src1 + i_src1;
  676. dst_t * dst_row = dst + i_dst;
  677. const int i10 = i0 % ne10;
  678. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  679. }
  680. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  681. const int ne10, const int ne11, const int ne12,
  682. const int nb1, const int nb2, int offset) {
  683. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  684. if (i >= ne) {
  685. return;
  686. }
  687. int src1_idx = i - offset;
  688. int oz = src1_idx / nb2;
  689. int oy = (src1_idx - (oz * nb2)) / nb1;
  690. int ox = src1_idx % nb1;
  691. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  692. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  693. } else {
  694. dst[i] = x[i];
  695. }
  696. }
  697. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  698. const float GELU_COEF_A = 0.044715f;
  699. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  700. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  701. if (i >= k) {
  702. return;
  703. }
  704. float xi = x[i];
  705. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  706. }
  707. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  708. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  709. if (i >= k) {
  710. return;
  711. }
  712. dst[i] = x[i] / (1.0f + expf(-x[i]));
  713. }
  714. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  715. const float GELU_QUICK_COEF = -1.702f;
  716. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  717. if (i >= k) {
  718. return;
  719. }
  720. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  721. }
  722. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  723. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  724. if (i >= k) {
  725. return;
  726. }
  727. dst[i] = tanhf(x[i]);
  728. }
  729. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  730. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  731. if (i >= k) {
  732. return;
  733. }
  734. dst[i] = fmaxf(x[i], 0);
  735. }
  736. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  737. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  738. if (i >= k) {
  739. return;
  740. }
  741. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  742. }
  743. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  744. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  745. if (i >= k) {
  746. return;
  747. }
  748. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  749. }
  750. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  751. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  752. if (i >= k) {
  753. return;
  754. }
  755. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  756. }
  757. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  758. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  759. if (i >= k) {
  760. return;
  761. }
  762. dst[i] = x[i] * x[i];
  763. }
  764. template <int block_size>
  765. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  766. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  767. const int tid = threadIdx.x;
  768. float2 mean_var = make_float2(0.f, 0.f);
  769. for (int col = tid; col < ncols; col += block_size) {
  770. const float xi = x[row*ncols + col];
  771. mean_var.x += xi;
  772. mean_var.y += xi * xi;
  773. }
  774. // sum up partial sums
  775. mean_var = warp_reduce_sum(mean_var);
  776. if (block_size > WARP_SIZE) {
  777. __shared__ float2 s_sum[32];
  778. int warp_id = threadIdx.x / WARP_SIZE;
  779. int lane_id = threadIdx.x % WARP_SIZE;
  780. if (lane_id == 0) {
  781. s_sum[warp_id] = mean_var;
  782. }
  783. __syncthreads();
  784. mean_var = s_sum[lane_id];
  785. mean_var = warp_reduce_sum(mean_var);
  786. }
  787. const float mean = mean_var.x / ncols;
  788. const float var = mean_var.y / ncols - mean * mean;
  789. const float inv_std = rsqrtf(var + eps);
  790. for (int col = tid; col < ncols; col += block_size) {
  791. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  792. }
  793. }
  794. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  795. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  796. if (nidx >= ne0) {
  797. return;
  798. }
  799. // operation
  800. int offset_dst =
  801. nidx +
  802. blockIdx.y * ne0 +
  803. blockIdx.z * ne0 * gridDim.y;
  804. if (blockIdx.z < ne02) { // src0
  805. int offset_src =
  806. nidx +
  807. blockIdx.y * ne0 +
  808. blockIdx.z * ne0 * gridDim.y;
  809. dst[offset_dst] = x[offset_src];
  810. } else {
  811. int offset_src =
  812. nidx +
  813. blockIdx.y * ne0 +
  814. (blockIdx.z - ne02) * ne0 * gridDim.y;
  815. dst[offset_dst] = y[offset_src];
  816. }
  817. }
  818. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  819. int ne0 = ne00 * scale_factor;
  820. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  821. if (nidx >= ne0) {
  822. return;
  823. }
  824. // operation
  825. int i00 = nidx / scale_factor;
  826. int i01 = blockIdx.y / scale_factor;
  827. int offset_src =
  828. i00 +
  829. i01 * ne00 +
  830. blockIdx.z * nb02;
  831. int offset_dst =
  832. nidx +
  833. blockIdx.y * ne0 +
  834. blockIdx.z * ne0 * gridDim.y;
  835. dst[offset_dst] = x[offset_src];
  836. }
  837. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  838. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  839. if (nidx >= ne0) {
  840. return;
  841. }
  842. // operation
  843. int offset_dst =
  844. nidx +
  845. blockIdx.y * ne0 +
  846. blockIdx.z * ne0 * gridDim.y;
  847. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  848. int offset_src =
  849. nidx +
  850. blockIdx.y * ne00 +
  851. blockIdx.z * ne00 * ne01;
  852. dst[offset_dst] = x[offset_src];
  853. } else {
  854. dst[offset_dst] = 0.0f;
  855. }
  856. }
  857. template <int block_size>
  858. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  859. int start = blockIdx.x * group_size;
  860. int end = start + group_size;
  861. start += threadIdx.x;
  862. if (end >= ne_elements) {
  863. end = ne_elements;
  864. }
  865. float tmp = 0.0f; // partial sum for thread in warp
  866. for (int j = start; j < end; j += block_size) {
  867. tmp += x[j];
  868. }
  869. tmp = warp_reduce_sum(tmp);
  870. if (block_size > WARP_SIZE) {
  871. __shared__ float s_sum[32];
  872. int warp_id = threadIdx.x / WARP_SIZE;
  873. int lane_id = threadIdx.x % WARP_SIZE;
  874. if (lane_id == 0) {
  875. s_sum[warp_id] = tmp;
  876. }
  877. __syncthreads();
  878. tmp = s_sum[lane_id];
  879. tmp = warp_reduce_sum(tmp);
  880. }
  881. float mean = tmp / group_size;
  882. tmp = 0.0f;
  883. for (int j = start; j < end; j += block_size) {
  884. float xi = x[j] - mean;
  885. dst[j] = xi;
  886. tmp += xi * xi;
  887. }
  888. tmp = warp_reduce_sum(tmp);
  889. if (block_size > WARP_SIZE) {
  890. __shared__ float s_sum[32];
  891. int warp_id = threadIdx.x / WARP_SIZE;
  892. int lane_id = threadIdx.x % WARP_SIZE;
  893. if (lane_id == 0) {
  894. s_sum[warp_id] = tmp;
  895. }
  896. __syncthreads();
  897. tmp = s_sum[lane_id];
  898. tmp = warp_reduce_sum(tmp);
  899. }
  900. float variance = tmp / group_size;
  901. float scale = rsqrtf(variance + eps);
  902. for (int j = start; j < end; j += block_size) {
  903. dst[j] *= scale;
  904. }
  905. }
  906. template <int block_size>
  907. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  908. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  909. const int tid = threadIdx.x;
  910. float tmp = 0.0f; // partial sum for thread in warp
  911. for (int col = tid; col < ncols; col += block_size) {
  912. const float xi = x[row*ncols + col];
  913. tmp += xi * xi;
  914. }
  915. // sum up partial sums
  916. tmp = warp_reduce_sum(tmp);
  917. if (block_size > WARP_SIZE) {
  918. __shared__ float s_sum[32];
  919. int warp_id = threadIdx.x / WARP_SIZE;
  920. int lane_id = threadIdx.x % WARP_SIZE;
  921. if (lane_id == 0) {
  922. s_sum[warp_id] = tmp;
  923. }
  924. __syncthreads();
  925. tmp = s_sum[lane_id];
  926. tmp = warp_reduce_sum(tmp);
  927. }
  928. const float mean = tmp / ncols;
  929. const float scale = rsqrtf(mean + eps);
  930. for (int col = tid; col < ncols; col += block_size) {
  931. dst[row*ncols + col] = scale * x[row*ncols + col];
  932. }
  933. }
  934. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  935. const block_q4_0 * x = (const block_q4_0 *) vx;
  936. const dfloat d = x[ib].d;
  937. const int vui = x[ib].qs[iqs];
  938. v.x = vui & 0xF;
  939. v.y = vui >> 4;
  940. #ifdef GGML_CUDA_F16
  941. v = __hsub2(v, {8.0f, 8.0f});
  942. v = __hmul2(v, {d, d});
  943. #else
  944. v.x = (v.x - 8.0f) * d;
  945. v.y = (v.y - 8.0f) * d;
  946. #endif // GGML_CUDA_F16
  947. }
  948. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  949. const block_q4_1 * x = (const block_q4_1 *) vx;
  950. const dfloat d = __low2half(x[ib].dm);
  951. const dfloat m = __high2half(x[ib].dm);
  952. const int vui = x[ib].qs[iqs];
  953. v.x = vui & 0xF;
  954. v.y = vui >> 4;
  955. #ifdef GGML_CUDA_F16
  956. v = __hmul2(v, {d, d});
  957. v = __hadd2(v, {m, m});
  958. #else
  959. v.x = (v.x * d) + m;
  960. v.y = (v.y * d) + m;
  961. #endif // GGML_CUDA_F16
  962. }
  963. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  964. const block_q5_0 * x = (const block_q5_0 *) vx;
  965. const dfloat d = x[ib].d;
  966. uint32_t qh;
  967. memcpy(&qh, x[ib].qh, sizeof(qh));
  968. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  969. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  970. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  971. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  972. #ifdef GGML_CUDA_F16
  973. v = __hsub2(v, {16.0f, 16.0f});
  974. v = __hmul2(v, {d, d});
  975. #else
  976. v.x = (v.x - 16.0f) * d;
  977. v.y = (v.y - 16.0f) * d;
  978. #endif // GGML_CUDA_F16
  979. }
  980. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  981. const block_q5_1 * x = (const block_q5_1 *) vx;
  982. const dfloat d = __low2half(x[ib].dm);
  983. const dfloat m = __high2half(x[ib].dm);
  984. uint32_t qh;
  985. memcpy(&qh, x[ib].qh, sizeof(qh));
  986. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  987. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  988. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  989. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  990. #ifdef GGML_CUDA_F16
  991. v = __hmul2(v, {d, d});
  992. v = __hadd2(v, {m, m});
  993. #else
  994. v.x = (v.x * d) + m;
  995. v.y = (v.y * d) + m;
  996. #endif // GGML_CUDA_F16
  997. }
  998. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  999. const block_q8_0 * x = (const block_q8_0 *) vx;
  1000. const dfloat d = x[ib].d;
  1001. v.x = x[ib].qs[iqs + 0];
  1002. v.y = x[ib].qs[iqs + 1];
  1003. #ifdef GGML_CUDA_F16
  1004. v = __hmul2(v, {d, d});
  1005. #else
  1006. v.x *= d;
  1007. v.y *= d;
  1008. #endif // GGML_CUDA_F16
  1009. }
  1010. template<typename dst_t>
  1011. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1012. const int i = blockIdx.x;
  1013. // assume 32 threads
  1014. const int tid = threadIdx.x;
  1015. const int il = tid/8;
  1016. const int ir = tid%8;
  1017. const int ib = 8*i + ir;
  1018. if (ib >= nb32) {
  1019. return;
  1020. }
  1021. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1022. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1023. const float d = __half2float(x->d);
  1024. const float dm = -8*d;
  1025. const uint8_t * q = x->qs + 4*il;
  1026. for (int l = 0; l < 4; ++l) {
  1027. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1028. y[l+16] = d * (q[l] >> 4) + dm;
  1029. }
  1030. }
  1031. template<typename dst_t>
  1032. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1033. const int i = blockIdx.x;
  1034. // assume 32 threads
  1035. const int tid = threadIdx.x;
  1036. const int il = tid/8;
  1037. const int ir = tid%8;
  1038. const int ib = 8*i + ir;
  1039. if (ib >= nb32) {
  1040. return;
  1041. }
  1042. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1043. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1044. const float2 d = __half22float2(x->dm);
  1045. const uint8_t * q = x->qs + 4*il;
  1046. for (int l = 0; l < 4; ++l) {
  1047. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1048. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1049. }
  1050. }
  1051. //================================== k-quants
  1052. template<typename dst_t>
  1053. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1054. const int i = blockIdx.x;
  1055. const block_q2_K * x = (const block_q2_K *) vx;
  1056. const int tid = threadIdx.x;
  1057. #if QK_K == 256
  1058. const int n = tid/32;
  1059. const int l = tid - 32*n;
  1060. const int is = 8*n + l/16;
  1061. const uint8_t q = x[i].qs[32*n + l];
  1062. dst_t * y = yy + i*QK_K + 128*n;
  1063. float dall = __low2half(x[i].dm);
  1064. float dmin = __high2half(x[i].dm);
  1065. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1066. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1067. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1068. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1069. #else
  1070. const int is = tid/16; // 0 or 1
  1071. const int il = tid%16; // 0...15
  1072. const uint8_t q = x[i].qs[il] >> (2*is);
  1073. dst_t * y = yy + i*QK_K + 16*is + il;
  1074. float dall = __low2half(x[i].dm);
  1075. float dmin = __high2half(x[i].dm);
  1076. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1077. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1078. #endif
  1079. }
  1080. template<typename dst_t>
  1081. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1082. const int i = blockIdx.x;
  1083. const block_q3_K * x = (const block_q3_K *) vx;
  1084. #if QK_K == 256
  1085. const int r = threadIdx.x/4;
  1086. const int tid = r/2;
  1087. const int is0 = r%2;
  1088. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1089. const int n = tid / 4;
  1090. const int j = tid - 4*n;
  1091. uint8_t m = 1 << (4*n + j);
  1092. int is = 8*n + 2*j + is0;
  1093. int shift = 2*j;
  1094. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1095. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1096. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1097. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1098. float d_all = x[i].d;
  1099. float dl = d_all * (us - 32);
  1100. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1101. const uint8_t * q = x[i].qs + 32*n;
  1102. const uint8_t * hm = x[i].hmask;
  1103. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1104. #else
  1105. const int tid = threadIdx.x;
  1106. const int is = tid/16; // 0 or 1
  1107. const int il = tid%16; // 0...15
  1108. const int im = il/8; // 0...1
  1109. const int in = il%8; // 0...7
  1110. dst_t * y = yy + i*QK_K + 16*is + il;
  1111. const uint8_t q = x[i].qs[il] >> (2*is);
  1112. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1113. const float d = (float)x[i].d;
  1114. if (is == 0) {
  1115. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1116. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1117. } else {
  1118. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1119. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1120. }
  1121. #endif
  1122. }
  1123. #if QK_K == 256
  1124. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1125. if (j < 4) {
  1126. d = q[j] & 63; m = q[j + 4] & 63;
  1127. } else {
  1128. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1129. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1130. }
  1131. }
  1132. #endif
  1133. template<typename dst_t>
  1134. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1135. const block_q4_K * x = (const block_q4_K *) vx;
  1136. const int i = blockIdx.x;
  1137. #if QK_K == 256
  1138. // assume 32 threads
  1139. const int tid = threadIdx.x;
  1140. const int il = tid/8;
  1141. const int ir = tid%8;
  1142. const int is = 2*il;
  1143. const int n = 4;
  1144. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1145. const float dall = __low2half(x[i].dm);
  1146. const float dmin = __high2half(x[i].dm);
  1147. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1148. uint8_t sc, m;
  1149. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1150. const float d1 = dall * sc; const float m1 = dmin * m;
  1151. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1152. const float d2 = dall * sc; const float m2 = dmin * m;
  1153. for (int l = 0; l < n; ++l) {
  1154. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1155. y[l +32] = d2 * (q[l] >> 4) - m2;
  1156. }
  1157. #else
  1158. const int tid = threadIdx.x;
  1159. const uint8_t * q = x[i].qs;
  1160. dst_t * y = yy + i*QK_K;
  1161. const float d = (float)x[i].dm[0];
  1162. const float m = (float)x[i].dm[1];
  1163. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1164. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1165. #endif
  1166. }
  1167. template<typename dst_t>
  1168. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1169. const block_q5_K * x = (const block_q5_K *) vx;
  1170. const int i = blockIdx.x;
  1171. #if QK_K == 256
  1172. // assume 64 threads - this is very slightly better than the one below
  1173. const int tid = threadIdx.x;
  1174. const int il = tid/16; // il is in 0...3
  1175. const int ir = tid%16; // ir is in 0...15
  1176. const int is = 2*il; // is is in 0...6
  1177. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1178. const float dall = __low2half(x[i].dm);
  1179. const float dmin = __high2half(x[i].dm);
  1180. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1181. const uint8_t * qh = x[i].qh + 2*ir;
  1182. uint8_t sc, m;
  1183. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1184. const float d1 = dall * sc; const float m1 = dmin * m;
  1185. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1186. const float d2 = dall * sc; const float m2 = dmin * m;
  1187. uint8_t hm = 1 << (2*il);
  1188. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1189. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1190. hm <<= 1;
  1191. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1192. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1193. #else
  1194. const int tid = threadIdx.x;
  1195. const uint8_t q = x[i].qs[tid];
  1196. const int im = tid/8; // 0...3
  1197. const int in = tid%8; // 0...7
  1198. const int is = tid/16; // 0 or 1
  1199. const uint8_t h = x[i].qh[in] >> im;
  1200. const float d = x[i].d;
  1201. dst_t * y = yy + i*QK_K + tid;
  1202. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1203. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1204. #endif
  1205. }
  1206. template<typename dst_t>
  1207. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1208. const block_q6_K * x = (const block_q6_K *) vx;
  1209. const int i = blockIdx.x;
  1210. #if QK_K == 256
  1211. // assume 64 threads - this is very slightly better than the one below
  1212. const int tid = threadIdx.x;
  1213. const int ip = tid/32; // ip is 0 or 1
  1214. const int il = tid - 32*ip; // 0...32
  1215. const int is = 8*ip + il/16;
  1216. dst_t * y = yy + i*QK_K + 128*ip + il;
  1217. const float d = x[i].d;
  1218. const uint8_t * ql = x[i].ql + 64*ip + il;
  1219. const uint8_t qh = x[i].qh[32*ip + il];
  1220. const int8_t * sc = x[i].scales + is;
  1221. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1222. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1223. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1224. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1225. #else
  1226. // assume 32 threads
  1227. const int tid = threadIdx.x;
  1228. const int ip = tid/16; // 0 or 1
  1229. const int il = tid - 16*ip; // 0...15
  1230. dst_t * y = yy + i*QK_K + 16*ip + il;
  1231. const float d = x[i].d;
  1232. const uint8_t ql = x[i].ql[16*ip + il];
  1233. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1234. const int8_t * sc = x[i].scales;
  1235. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1236. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1237. #endif
  1238. }
  1239. static const __device__ uint64_t iq2xxs_grid[256] = {
  1240. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1241. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1242. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1243. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1244. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1245. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1246. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1247. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1248. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1249. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1250. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1251. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1252. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1253. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1254. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1255. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1256. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1257. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1258. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1259. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1260. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1261. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1262. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1263. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1264. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1265. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1266. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1267. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1268. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1269. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1270. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1271. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1272. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1273. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1274. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1275. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1276. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1277. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1278. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1279. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1280. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1281. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1282. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1283. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1284. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1285. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1286. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1287. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1288. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1289. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1290. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1291. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1292. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1293. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1294. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1295. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1296. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1297. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1298. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1299. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1300. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1301. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1302. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1303. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1304. };
  1305. static const __device__ uint64_t iq2xs_grid[512] = {
  1306. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1307. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1308. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1309. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1310. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1311. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  1312. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  1313. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  1314. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  1315. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  1316. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  1317. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  1318. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  1319. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  1320. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  1321. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  1322. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  1323. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  1324. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  1325. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  1326. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  1327. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  1328. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  1329. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  1330. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  1331. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  1332. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  1333. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  1334. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  1335. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  1336. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  1337. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  1338. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  1339. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  1340. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  1341. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  1342. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  1343. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  1344. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  1345. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  1346. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  1347. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  1348. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  1349. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  1350. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  1351. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  1352. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  1353. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  1354. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  1355. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  1356. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  1357. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  1358. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  1359. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  1360. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  1361. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  1362. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  1363. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  1364. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  1365. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  1366. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  1367. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  1368. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  1369. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  1370. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  1371. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  1372. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  1373. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  1374. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  1375. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  1376. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  1377. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  1378. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  1379. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  1380. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  1381. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  1382. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  1383. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  1384. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  1385. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  1386. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  1387. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  1388. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  1389. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  1390. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  1391. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  1392. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  1393. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  1394. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  1395. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  1396. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  1397. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  1398. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  1399. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  1400. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  1401. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  1402. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  1403. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  1404. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  1405. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  1406. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  1407. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  1408. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  1409. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  1410. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  1411. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  1412. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  1413. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  1414. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  1415. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  1416. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  1417. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  1418. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  1419. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  1420. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  1421. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  1422. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  1423. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  1424. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  1425. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  1426. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  1427. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  1428. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  1429. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  1430. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  1431. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  1432. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  1433. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  1434. };
  1435. static const __device__ uint32_t iq3xxs_grid[256] = {
  1436. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  1437. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  1438. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  1439. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  1440. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  1441. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  1442. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  1443. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  1444. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  1445. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  1446. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  1447. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  1448. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  1449. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  1450. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  1451. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  1452. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  1453. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  1454. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  1455. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  1456. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  1457. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  1458. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  1459. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  1460. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  1461. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  1462. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  1463. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  1464. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  1465. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  1466. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  1467. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  1468. };
  1469. static const __device__ uint64_t iq1s_grid[512] = {
  1470. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  1471. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  1472. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  1473. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  1474. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  1475. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  1476. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  1477. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  1478. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  1479. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  1480. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  1481. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  1482. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  1483. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  1484. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  1485. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  1486. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  1487. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  1488. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  1489. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  1490. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  1491. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  1492. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  1493. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  1494. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  1495. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  1496. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  1497. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  1498. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  1499. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  1500. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  1501. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  1502. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  1503. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  1504. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  1505. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  1506. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  1507. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  1508. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  1509. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  1510. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  1511. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  1512. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  1513. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  1514. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  1515. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  1516. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  1517. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  1518. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  1519. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  1520. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  1521. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  1522. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  1523. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  1524. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1525. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  1526. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  1527. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  1528. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1529. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  1530. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  1531. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  1532. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  1533. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1534. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  1535. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  1536. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  1537. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  1538. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  1539. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  1540. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  1541. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  1542. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  1543. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  1544. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  1545. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  1546. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  1547. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  1548. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  1549. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  1550. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  1551. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  1552. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  1553. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  1554. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  1555. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  1556. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  1557. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  1558. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  1559. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  1560. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  1561. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  1562. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  1563. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  1564. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  1565. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  1566. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  1567. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  1568. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  1569. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  1570. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  1571. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  1572. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  1573. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  1574. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  1575. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  1576. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  1577. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  1578. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  1579. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  1580. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  1581. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  1582. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  1583. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  1584. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  1585. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  1586. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  1587. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  1588. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  1589. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  1590. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  1591. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  1592. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  1593. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  1594. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  1595. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  1596. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  1597. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  1598. };
  1599. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1600. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1601. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1602. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1603. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1604. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1605. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1606. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1607. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1608. };
  1609. //#if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1610. static const __device__ uint64_t ksigns64[128] = {
  1611. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  1612. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  1613. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  1614. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  1615. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  1616. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  1617. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  1618. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  1619. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  1620. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  1621. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  1622. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  1623. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  1624. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  1625. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  1626. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  1627. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  1628. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  1629. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  1630. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  1631. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  1632. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  1633. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  1634. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  1635. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  1636. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  1637. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  1638. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  1639. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  1640. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  1641. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  1642. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  1643. };
  1644. //#endif
  1645. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  1646. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1647. switch (type) {
  1648. case GGML_TYPE_Q4_0:
  1649. case GGML_TYPE_Q4_1:
  1650. case GGML_TYPE_Q5_0:
  1651. case GGML_TYPE_Q5_1:
  1652. case GGML_TYPE_Q8_0:
  1653. case GGML_TYPE_Q2_K:
  1654. case GGML_TYPE_Q3_K:
  1655. case GGML_TYPE_Q4_K:
  1656. case GGML_TYPE_Q5_K:
  1657. case GGML_TYPE_Q6_K:
  1658. return true;
  1659. default:
  1660. return false;
  1661. }
  1662. }
  1663. template<typename dst_t>
  1664. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1665. const int i = blockIdx.x;
  1666. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1667. const int tid = threadIdx.x;
  1668. #if QK_K == 256
  1669. const int il = tid/8; // 0...3
  1670. const int ib = tid%8; // 0...7
  1671. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1672. const uint16_t * q2 = x[i].qs + 4*ib;
  1673. const uint8_t * aux8 = (const uint8_t *)q2;
  1674. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  1675. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1676. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1677. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1678. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1679. #else
  1680. assert(false);
  1681. #endif
  1682. }
  1683. template<typename dst_t>
  1684. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1685. const int i = blockIdx.x;
  1686. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  1687. const int tid = threadIdx.x;
  1688. #if QK_K == 256
  1689. const int il = tid/8; // 0...3
  1690. const int ib = tid%8; // 0...7
  1691. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1692. const uint16_t * q2 = x[i].qs + 4*ib;
  1693. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  1694. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1695. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  1696. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1697. #else
  1698. assert(false);
  1699. #endif
  1700. }
  1701. template<typename dst_t>
  1702. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1703. const int i = blockIdx.x;
  1704. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  1705. const int tid = threadIdx.x;
  1706. #if QK_K == 256
  1707. const int il = tid/8; // 0...3
  1708. const int ib = tid%8; // 0...7
  1709. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1710. const uint8_t * q3 = x[i].qs + 8*ib;
  1711. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  1712. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  1713. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  1714. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  1715. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  1716. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1717. for (int j = 0; j < 4; ++j) {
  1718. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1719. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1720. }
  1721. #else
  1722. assert(false);
  1723. #endif
  1724. }
  1725. template<typename dst_t>
  1726. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1727. const int i = blockIdx.x;
  1728. const block_iq1_s * x = (const block_iq1_s *) vx;
  1729. const int tid = threadIdx.x;
  1730. #if QK_K == 256
  1731. const int il = tid/8; // 0...3
  1732. const int ib = tid%8; // 0...7
  1733. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1734. const int i8 = 4*ib+il;
  1735. uint8_t h = x[i].scales[i8/2] >> 4*(i8%2);
  1736. const int8_t * grid = (const int8_t *)(iq1s_grid + (x[i].qs[i8] | ((h & 8) << 5)));
  1737. const float d = (float)x[i].d * (2*(h & 7) + 1);
  1738. for (int j = 0; j < 8; ++j) y[j] = d * grid[j];
  1739. #else
  1740. assert(false);
  1741. #endif
  1742. }
  1743. static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  1744. template<typename dst_t>
  1745. static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1746. const int i = blockIdx.x;
  1747. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  1748. const int tid = threadIdx.x;
  1749. const int il = tid/8; // 0...3
  1750. const int ib = tid%8; // 0...7
  1751. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  1752. const uint8_t * q4 = x[ib].qs + 4*il;
  1753. const float d = (float)x[ib].d;
  1754. for (int j = 0; j < 4; ++j) {
  1755. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  1756. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  1757. }
  1758. }
  1759. 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) {
  1760. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1761. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1762. if (row > nrows) return;
  1763. const int num_blocks_per_row = ncols / QK_K;
  1764. const int ib0 = row*num_blocks_per_row;
  1765. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1766. float tmp = 0; // partial sum for thread in warp
  1767. #if QK_K == 256
  1768. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1769. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1770. const int step = 16/K_QUANTS_PER_ITERATION;
  1771. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1772. const int in = tid - step*im; // 0...15 or 0...7
  1773. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1774. const int q_offset = 32*im + l0;
  1775. const int s_offset = 8*im;
  1776. const int y_offset = 128*im + l0;
  1777. uint32_t aux[4];
  1778. const uint8_t * d = (const uint8_t *)aux;
  1779. const uint8_t * m = (const uint8_t *)(aux + 2);
  1780. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1781. const float * y = yy + i * QK_K + y_offset;
  1782. const uint8_t * q = x[i].qs + q_offset;
  1783. const float dall = __low2half(x[i].dm);
  1784. const float dmin = __high2half(x[i].dm);
  1785. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1786. aux[0] = a[0] & 0x0f0f0f0f;
  1787. aux[1] = a[1] & 0x0f0f0f0f;
  1788. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1789. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1790. float sum1 = 0, sum2 = 0;
  1791. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1792. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1793. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1794. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1795. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1796. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1797. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1798. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1799. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1800. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1801. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1802. }
  1803. tmp += dall * sum1 - dmin * sum2;
  1804. }
  1805. #else
  1806. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1807. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1808. const int offset = tid * K_QUANTS_PER_ITERATION;
  1809. uint32_t uaux[2];
  1810. const uint8_t * d = (const uint8_t *)uaux;
  1811. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1812. const float * y = yy + i * QK_K + offset;
  1813. const uint8_t * q = x[i].qs + offset;
  1814. const uint32_t * s = (const uint32_t *)x[i].scales;
  1815. uaux[0] = s[0] & 0x0f0f0f0f;
  1816. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1817. const float2 dall = __half22float2(x[i].dm);
  1818. float sum1 = 0, sum2 = 0;
  1819. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1820. const uint8_t ql = q[l];
  1821. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1822. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1823. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1824. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1825. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1826. }
  1827. tmp += dall.x * sum1 - dall.y * sum2;
  1828. }
  1829. #endif
  1830. // sum up partial sums and write back result
  1831. #pragma unroll
  1832. for (int mask = 16; mask > 0; mask >>= 1) {
  1833. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1834. }
  1835. if (threadIdx.x == 0) {
  1836. dst[row] = tmp;
  1837. }
  1838. }
  1839. 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) {
  1840. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1841. if (row > nrows) return;
  1842. const int num_blocks_per_row = ncols / QK_K;
  1843. const int ib0 = row*num_blocks_per_row;
  1844. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1845. float tmp = 0; // partial sum for thread in warp
  1846. #if QK_K == 256
  1847. const uint16_t kmask1 = 0x0303;
  1848. const uint16_t kmask2 = 0x0f0f;
  1849. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1850. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1851. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1852. const int step = 16/K_QUANTS_PER_ITERATION;
  1853. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1854. const int in = tid - step*im; // 0....15 or 0...7
  1855. const uint8_t m = 1 << (4*im);
  1856. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1857. const int q_offset = 32*im + l0;
  1858. const int y_offset = 128*im + l0;
  1859. uint16_t utmp[4];
  1860. const int8_t * s = (const int8_t *)utmp;
  1861. const uint16_t s_shift = 4*im;
  1862. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1863. const float * y = yy + i * QK_K + y_offset;
  1864. const uint8_t * q = x[i].qs + q_offset;
  1865. const uint8_t * h = x[i].hmask + l0;
  1866. const uint16_t * a = (const uint16_t *)x[i].scales;
  1867. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1868. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1869. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1870. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1871. const float d = x[i].d;
  1872. float sum = 0;
  1873. for (int l = 0; l < n; ++l) {
  1874. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1875. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1876. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1877. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1878. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1879. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1880. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1881. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1882. }
  1883. tmp += d * sum;
  1884. }
  1885. #else
  1886. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1887. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1888. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1889. const int in = offset/8; // 0 or 1
  1890. const int im = offset%8; // 0...7
  1891. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1892. const float * y = yy + i * QK_K + offset;
  1893. const uint8_t * q = x[i].qs + offset;
  1894. const uint8_t * s = x[i].scales;
  1895. const float dall = (float)x[i].d;
  1896. float sum = 0;
  1897. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1898. const uint8_t hl = x[i].hmask[im+l] >> in;
  1899. const uint8_t ql = q[l];
  1900. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1901. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1902. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1903. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1904. }
  1905. tmp += sum;
  1906. }
  1907. #endif
  1908. // sum up partial sums and write back result
  1909. #pragma unroll
  1910. for (int mask = 16; mask > 0; mask >>= 1) {
  1911. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1912. }
  1913. if (threadIdx.x == 0) {
  1914. dst[row] = tmp;
  1915. }
  1916. }
  1917. 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) {
  1918. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1919. if (row > nrows) return;
  1920. const int num_blocks_per_row = ncols / QK_K;
  1921. const int ib0 = row*num_blocks_per_row;
  1922. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1923. #if QK_K == 256
  1924. const uint16_t kmask1 = 0x3f3f;
  1925. const uint16_t kmask2 = 0x0f0f;
  1926. const uint16_t kmask3 = 0xc0c0;
  1927. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1928. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1929. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1930. const int il = tid/step; // 0...3
  1931. const int ir = tid - step*il; // 0...7 or 0...3
  1932. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1933. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1934. const int in = il%2;
  1935. const int l0 = n*(2*ir + in);
  1936. const int q_offset = 32*im + l0;
  1937. const int y_offset = 64*im + l0;
  1938. uint16_t aux[4];
  1939. const uint8_t * sc = (const uint8_t *)aux;
  1940. #if K_QUANTS_PER_ITERATION == 2
  1941. uint32_t q32[4];
  1942. const uint8_t * q4 = (const uint8_t *)q32;
  1943. #else
  1944. uint16_t q16[4];
  1945. const uint8_t * q4 = (const uint8_t *)q16;
  1946. #endif
  1947. float tmp = 0; // partial sum for thread in warp
  1948. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1949. const float * y1 = yy + i*QK_K + y_offset;
  1950. const float * y2 = y1 + 128;
  1951. const float dall = __low2half(x[i].dm);
  1952. const float dmin = __high2half(x[i].dm);
  1953. const uint16_t * a = (const uint16_t *)x[i].scales;
  1954. aux[0] = a[im+0] & kmask1;
  1955. aux[1] = a[im+2] & kmask1;
  1956. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1957. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1958. #if K_QUANTS_PER_ITERATION == 2
  1959. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1960. const uint32_t * q2 = q1 + 16;
  1961. q32[0] = q1[0] & 0x0f0f0f0f;
  1962. q32[1] = q1[0] & 0xf0f0f0f0;
  1963. q32[2] = q2[0] & 0x0f0f0f0f;
  1964. q32[3] = q2[0] & 0xf0f0f0f0;
  1965. float4 s = {0.f, 0.f, 0.f, 0.f};
  1966. float smin = 0;
  1967. for (int l = 0; l < 4; ++l) {
  1968. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1969. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1970. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1971. }
  1972. 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;
  1973. #else
  1974. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1975. const uint16_t * q2 = q1 + 32;
  1976. q16[0] = q1[0] & 0x0f0f;
  1977. q16[1] = q1[0] & 0xf0f0;
  1978. q16[2] = q2[0] & 0x0f0f;
  1979. q16[3] = q2[0] & 0xf0f0;
  1980. float4 s = {0.f, 0.f, 0.f, 0.f};
  1981. float smin = 0;
  1982. for (int l = 0; l < 2; ++l) {
  1983. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1984. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1985. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1986. }
  1987. 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;
  1988. #endif
  1989. }
  1990. #else
  1991. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1992. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1993. const int step = tid * K_QUANTS_PER_ITERATION;
  1994. uint16_t aux16[2];
  1995. const uint8_t * s = (const uint8_t *)aux16;
  1996. float tmp = 0;
  1997. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1998. const uint8_t * q = x[i].qs + step;
  1999. const float * y = yy + i*QK_K + step;
  2000. const uint16_t * a = (const uint16_t *)x[i].scales;
  2001. aux16[0] = a[0] & 0x0f0f;
  2002. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2003. const float d = (float)x[i].dm[0];
  2004. const float m = (float)x[i].dm[1];
  2005. float sum = 0.f;
  2006. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2007. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  2008. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  2009. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  2010. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  2011. }
  2012. tmp += sum;
  2013. }
  2014. #endif
  2015. // sum up partial sums and write back result
  2016. #pragma unroll
  2017. for (int mask = 16; mask > 0; mask >>= 1) {
  2018. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2019. }
  2020. if (tid == 0) {
  2021. dst[row] = tmp;
  2022. }
  2023. }
  2024. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  2025. const int row = blockIdx.x;
  2026. const int num_blocks_per_row = ncols / QK_K;
  2027. const int ib0 = row*num_blocks_per_row;
  2028. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  2029. float tmp = 0; // partial sum for thread in warp
  2030. #if QK_K == 256
  2031. const uint16_t kmask1 = 0x3f3f;
  2032. const uint16_t kmask2 = 0x0f0f;
  2033. const uint16_t kmask3 = 0xc0c0;
  2034. const int tid = threadIdx.x/2; // 0...15
  2035. const int ix = threadIdx.x%2;
  2036. const int il = tid/4; // 0...3
  2037. const int ir = tid - 4*il;// 0...3
  2038. const int n = 2;
  2039. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2040. const int in = il%2;
  2041. const int l0 = n*(2*ir + in);
  2042. const int q_offset = 32*im + l0;
  2043. const int y_offset = 64*im + l0;
  2044. const uint8_t hm1 = 1 << (2*im);
  2045. const uint8_t hm2 = hm1 << 4;
  2046. uint16_t aux[4];
  2047. const uint8_t * sc = (const uint8_t *)aux;
  2048. uint16_t q16[8];
  2049. const uint8_t * q4 = (const uint8_t *)q16;
  2050. for (int i = ix; i < num_blocks_per_row; i += 2) {
  2051. const uint8_t * ql1 = x[i].qs + q_offset;
  2052. const uint8_t * qh = x[i].qh + l0;
  2053. const float * y1 = yy + i*QK_K + y_offset;
  2054. const float * y2 = y1 + 128;
  2055. const float dall = __low2half(x[i].dm);
  2056. const float dmin = __high2half(x[i].dm);
  2057. const uint16_t * a = (const uint16_t *)x[i].scales;
  2058. aux[0] = a[im+0] & kmask1;
  2059. aux[1] = a[im+2] & kmask1;
  2060. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2061. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2062. float4 sum = {0.f, 0.f, 0.f, 0.f};
  2063. float smin = 0;
  2064. const uint16_t * q1 = (const uint16_t *)ql1;
  2065. const uint16_t * q2 = q1 + 32;
  2066. q16[0] = q1[0] & 0x0f0f;
  2067. q16[1] = q1[8] & 0x0f0f;
  2068. q16[2] = (q1[0] >> 4) & 0x0f0f;
  2069. q16[3] = (q1[8] >> 4) & 0x0f0f;
  2070. q16[4] = q2[0] & 0x0f0f;
  2071. q16[5] = q2[8] & 0x0f0f;
  2072. q16[6] = (q2[0] >> 4) & 0x0f0f;
  2073. q16[7] = (q2[8] >> 4) & 0x0f0f;
  2074. for (int l = 0; l < n; ++l) {
  2075. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  2076. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  2077. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  2078. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  2079. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  2080. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  2081. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  2082. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  2083. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  2084. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  2085. }
  2086. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  2087. }
  2088. #else
  2089. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2090. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2091. const int step = tid * K_QUANTS_PER_ITERATION;
  2092. const int im = step/8;
  2093. const int in = step%8;
  2094. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2095. const uint8_t * q = x[i].qs + step;
  2096. const int8_t * s = x[i].scales;
  2097. const float * y = yy + i*QK_K + step;
  2098. const float d = x[i].d;
  2099. float sum = 0.f;
  2100. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2101. const uint8_t h = x[i].qh[in+j] >> im;
  2102. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  2103. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  2104. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  2105. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  2106. }
  2107. tmp += sum;
  2108. }
  2109. #endif
  2110. // sum up partial sums and write back result
  2111. #pragma unroll
  2112. for (int mask = 16; mask > 0; mask >>= 1) {
  2113. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2114. }
  2115. if (threadIdx.x == 0) {
  2116. dst[row] = tmp;
  2117. }
  2118. }
  2119. 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) {
  2120. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2121. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2122. if (row > nrows) return;
  2123. const int num_blocks_per_row = ncols / QK_K;
  2124. const int ib0 = row*num_blocks_per_row;
  2125. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  2126. #if QK_K == 256
  2127. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2128. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  2129. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  2130. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2131. const int in = tid - step*im; // 0...15 or 0...7
  2132. #if K_QUANTS_PER_ITERATION == 1
  2133. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  2134. const int is = 0;
  2135. #else
  2136. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  2137. const int is = in / 4;
  2138. #endif
  2139. const int ql_offset = 64*im + l0;
  2140. const int qh_offset = 32*im + l0;
  2141. const int s_offset = 8*im + is;
  2142. const int y_offset = 128*im + l0;
  2143. float tmp = 0; // partial sum for thread in warp
  2144. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2145. const float * y = yy + i * QK_K + y_offset;
  2146. const uint8_t * ql = x[i].ql + ql_offset;
  2147. const uint8_t * qh = x[i].qh + qh_offset;
  2148. const int8_t * s = x[i].scales + s_offset;
  2149. const float d = x[i].d;
  2150. #if K_QUANTS_PER_ITERATION == 1
  2151. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  2152. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  2153. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  2154. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  2155. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  2156. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  2157. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  2158. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  2159. tmp += sum;
  2160. #else
  2161. float sum = 0;
  2162. for (int l = 0; l < 4; ++l) {
  2163. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  2164. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  2165. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  2166. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  2167. }
  2168. tmp += sum;
  2169. #endif
  2170. }
  2171. #else
  2172. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  2173. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  2174. const int step = tid * K_QUANTS_PER_ITERATION;
  2175. float tmp = 0; // partial sum for thread in warp
  2176. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2177. const float * y = yy + i * QK_K + step;
  2178. const uint8_t * ql = x[i].ql + step;
  2179. const uint8_t * qh = x[i].qh + step;
  2180. const int8_t * s = x[i].scales;
  2181. const float d = x[i+0].d;
  2182. float sum = 0;
  2183. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2184. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  2185. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  2186. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  2187. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  2188. }
  2189. tmp += sum;
  2190. }
  2191. #endif
  2192. // sum up partial sums and write back result
  2193. #pragma unroll
  2194. for (int mask = 16; mask > 0; mask >>= 1) {
  2195. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2196. }
  2197. if (tid == 0) {
  2198. dst[row] = tmp;
  2199. }
  2200. }
  2201. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  2202. const half * x = (const half *) vx;
  2203. // automatic half -> float type cast if dfloat == float
  2204. v.x = x[ib + iqs + 0];
  2205. v.y = x[ib + iqs + 1];
  2206. }
  2207. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  2208. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  2209. if (ix >= kx_padded) {
  2210. return;
  2211. }
  2212. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  2213. const int i_padded = iy*kx_padded + ix;
  2214. block_q8_1 * y = (block_q8_1 *) vy;
  2215. const int ib = i_padded / QK8_1; // block index
  2216. const int iqs = i_padded % QK8_1; // quant index
  2217. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  2218. float amax = fabsf(xi);
  2219. float sum = xi;
  2220. #pragma unroll
  2221. for (int mask = 16; mask > 0; mask >>= 1) {
  2222. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  2223. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  2224. }
  2225. const float d = amax / 127;
  2226. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  2227. y[ib].qs[iqs] = q;
  2228. if (iqs > 0) {
  2229. return;
  2230. }
  2231. reinterpret_cast<half&>(y[ib].ds.x) = d;
  2232. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  2233. }
  2234. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2235. static __global__ void k_get_rows(
  2236. const void * src0, const int32_t * src1, dst_t * dst,
  2237. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2238. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2239. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2240. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2241. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2242. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  2243. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2244. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2245. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2246. if (i00 >= ne00) {
  2247. return;
  2248. }
  2249. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2250. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2251. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  2252. const int ib = i00/qk; // block index
  2253. const int iqs = (i00%qk)/qr; // quant index
  2254. const int iybs = i00 - i00%qk; // dst block start index
  2255. const int y_offset = qr == 1 ? 1 : qk/2;
  2256. // dequantize
  2257. dfloat2 v;
  2258. dequantize_kernel(src0_row, ib, iqs, v);
  2259. dst_row[iybs + iqs + 0] = v.x;
  2260. dst_row[iybs + iqs + y_offset] = v.y;
  2261. }
  2262. template<typename src0_t, typename dst_t>
  2263. static __global__ void k_get_rows_float(
  2264. const src0_t * src0, const int32_t * src1, dst_t * dst,
  2265. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2266. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2267. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2268. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2269. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2270. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2271. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2272. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2273. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2274. if (i00 >= ne00) {
  2275. return;
  2276. }
  2277. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2278. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2279. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2280. dst_row[i00] = src0_row[i00];
  2281. }
  2282. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2283. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2284. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2285. if (i >= k) {
  2286. return;
  2287. }
  2288. const int ib = i/qk; // block index
  2289. const int iqs = (i%qk)/qr; // quant index
  2290. const int iybs = i - i%qk; // y block start index
  2291. const int y_offset = qr == 1 ? 1 : qk/2;
  2292. // dequantize
  2293. dfloat2 v;
  2294. dequantize_kernel(vx, ib, iqs, v);
  2295. y[iybs + iqs + 0] = v.x;
  2296. y[iybs + iqs + y_offset] = v.y;
  2297. }
  2298. template <typename src_t, typename dst_t>
  2299. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2300. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2301. if (i >= k) {
  2302. return;
  2303. }
  2304. const src_t * x = (src_t *) vx;
  2305. y[i] = x[i];
  2306. }
  2307. template <bool need_check>
  2308. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2309. #if __CUDA_ARCH__ >= CC_PASCAL
  2310. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2311. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2312. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2313. half2 * y2 = (half2 *) (y + i0);
  2314. __shared__ int vals[nint];
  2315. #pragma unroll
  2316. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2317. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2318. break;
  2319. }
  2320. const int ix = ix0 + threadIdx.x;
  2321. vals[ix] = x0[ix];
  2322. }
  2323. #pragma unroll
  2324. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2325. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2326. return;
  2327. }
  2328. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2329. const half d = *b0;
  2330. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2331. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2332. }
  2333. #else
  2334. (void) vx; (void) y; (void) k;
  2335. NO_DEVICE_CODE;
  2336. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2337. }
  2338. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2339. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2340. #define VDR_Q4_0_Q8_1_MMVQ 2
  2341. #define VDR_Q4_0_Q8_1_MMQ 4
  2342. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2343. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2344. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2345. int sumi = 0;
  2346. #pragma unroll
  2347. for (int i = 0; i < vdr; ++i) {
  2348. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2349. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2350. // SIMD dot product of quantized values
  2351. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2352. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2353. }
  2354. const float2 ds8f = __half22float2(ds8);
  2355. // second part effectively subtracts 8 from each quant value
  2356. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2357. #else
  2358. NO_DEVICE_CODE;
  2359. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2360. }
  2361. #define VDR_Q4_1_Q8_1_MMVQ 2
  2362. #define VDR_Q4_1_Q8_1_MMQ 4
  2363. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2364. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2365. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2366. int sumi = 0;
  2367. #pragma unroll
  2368. for (int i = 0; i < vdr; ++i) {
  2369. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2370. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2371. // SIMD dot product of quantized values
  2372. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2373. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2374. }
  2375. #ifdef GGML_CUDA_F16
  2376. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2377. const float d4d8 = tmp.x;
  2378. const float m4s8 = tmp.y;
  2379. #else
  2380. const float2 dm4f = __half22float2(dm4);
  2381. const float2 ds8f = __half22float2(ds8);
  2382. const float d4d8 = dm4f.x * ds8f.x;
  2383. const float m4s8 = dm4f.y * ds8f.y;
  2384. #endif // GGML_CUDA_F16
  2385. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2386. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2387. #else
  2388. NO_DEVICE_CODE;
  2389. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2390. }
  2391. #define VDR_Q5_0_Q8_1_MMVQ 2
  2392. #define VDR_Q5_0_Q8_1_MMQ 4
  2393. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2394. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2395. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2396. int sumi = 0;
  2397. #pragma unroll
  2398. for (int i = 0; i < vdr; ++i) {
  2399. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2400. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2401. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2402. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2403. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2404. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2405. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2406. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2407. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2408. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2409. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2410. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2411. }
  2412. const float2 ds8f = __half22float2(ds8);
  2413. // second part effectively subtracts 16 from each quant value
  2414. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2415. #else
  2416. NO_DEVICE_CODE;
  2417. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2418. }
  2419. #define VDR_Q5_1_Q8_1_MMVQ 2
  2420. #define VDR_Q5_1_Q8_1_MMQ 4
  2421. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2422. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2423. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2424. int sumi = 0;
  2425. #pragma unroll
  2426. for (int i = 0; i < vdr; ++i) {
  2427. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2428. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2429. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2430. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2431. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2432. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2433. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2434. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2435. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2436. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2437. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2438. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2439. }
  2440. #ifdef GGML_CUDA_F16
  2441. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2442. const float d5d8 = tmp.x;
  2443. const float m5s8 = tmp.y;
  2444. #else
  2445. const float2 dm5f = __half22float2(dm5);
  2446. const float2 ds8f = __half22float2(ds8);
  2447. const float d5d8 = dm5f.x * ds8f.x;
  2448. const float m5s8 = dm5f.y * ds8f.y;
  2449. #endif // GGML_CUDA_F16
  2450. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2451. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2452. #else
  2453. NO_DEVICE_CODE;
  2454. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2455. }
  2456. #define VDR_Q8_0_Q8_1_MMVQ 2
  2457. #define VDR_Q8_0_Q8_1_MMQ 8
  2458. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2459. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2460. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2461. int sumi = 0;
  2462. #pragma unroll
  2463. for (int i = 0; i < vdr; ++i) {
  2464. // SIMD dot product of quantized values
  2465. sumi = __dp4a(v[i], u[i], sumi);
  2466. }
  2467. return d8_0*d8_1 * sumi;
  2468. #else
  2469. NO_DEVICE_CODE;
  2470. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2471. }
  2472. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2473. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2474. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2475. int sumi = 0;
  2476. #pragma unroll
  2477. for (int i = 0; i < vdr; ++i) {
  2478. // SIMD dot product of quantized values
  2479. sumi = __dp4a(v[i], u[i], sumi);
  2480. }
  2481. #ifdef GGML_CUDA_F16
  2482. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2483. const float d8d8 = tmp.x;
  2484. const float m8s8 = tmp.y;
  2485. #else
  2486. const float2 dm8f = __half22float2(dm8);
  2487. const float2 ds8f = __half22float2(ds8);
  2488. const float d8d8 = dm8f.x * ds8f.x;
  2489. const float m8s8 = dm8f.y * ds8f.y;
  2490. #endif // GGML_CUDA_F16
  2491. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2492. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2493. #else
  2494. NO_DEVICE_CODE;
  2495. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2496. }
  2497. #define VDR_Q2_K_Q8_1_MMVQ 1
  2498. #define VDR_Q2_K_Q8_1_MMQ 2
  2499. // contiguous v/x values
  2500. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2501. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2502. const half2 & dm2, const float * __restrict__ d8) {
  2503. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2504. float sumf_d = 0.0f;
  2505. float sumf_m = 0.0f;
  2506. #pragma unroll
  2507. for (int i = 0; i < QR2_K; ++i) {
  2508. const int sc = scales[2*i];
  2509. const int vi = (v >> (2*i)) & 0x03030303;
  2510. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2511. // fill int with 4x m
  2512. int m = sc >> 4;
  2513. m |= m << 8;
  2514. m |= m << 16;
  2515. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2516. }
  2517. const float2 dm2f = __half22float2(dm2);
  2518. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2519. #else
  2520. NO_DEVICE_CODE;
  2521. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2522. }
  2523. // contiguous u/y values
  2524. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2525. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2526. const half2 & dm2, const float & d8) {
  2527. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2528. int sumi_d = 0;
  2529. int sumi_m = 0;
  2530. #pragma unroll
  2531. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2532. int sumi_d_sc = 0;
  2533. const int sc = scales[i0 / (QI8_1/2)];
  2534. // fill int with 4x m
  2535. int m = sc >> 4;
  2536. m |= m << 8;
  2537. m |= m << 16;
  2538. #pragma unroll
  2539. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2540. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2541. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2542. }
  2543. sumi_d += sumi_d_sc * (sc & 0xF);
  2544. }
  2545. const float2 dm2f = __half22float2(dm2);
  2546. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2547. #else
  2548. NO_DEVICE_CODE;
  2549. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2550. }
  2551. #define VDR_Q3_K_Q8_1_MMVQ 1
  2552. #define VDR_Q3_K_Q8_1_MMQ 2
  2553. // contiguous v/x values
  2554. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2555. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2556. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2557. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2558. float sumf = 0.0f;
  2559. #pragma unroll
  2560. for (int i = 0; i < QR3_K; ++i) {
  2561. const int isc = scale_offset + 2*i;
  2562. const int isc_low = isc % (QK_K/32);
  2563. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2564. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2565. const int isc_high = isc % (QK_K/64);
  2566. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2567. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2568. const int sc = (sc_low | sc_high) - 32;
  2569. const int vil = (vl >> (2*i)) & 0x03030303;
  2570. const int vih = ((vh >> i) << 2) & 0x04040404;
  2571. const int vi = __vsubss4(vil, vih);
  2572. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2573. }
  2574. return d3 * sumf;
  2575. #else
  2576. NO_DEVICE_CODE;
  2577. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2578. }
  2579. // contiguous u/y values
  2580. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2581. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2582. const float & d3, const float & d8) {
  2583. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2584. int sumi = 0;
  2585. #pragma unroll
  2586. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2587. int sumi_sc = 0;
  2588. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2589. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2590. }
  2591. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2592. }
  2593. return d3*d8 * sumi;
  2594. #else
  2595. NO_DEVICE_CODE;
  2596. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2597. }
  2598. #define VDR_Q4_K_Q8_1_MMVQ 2
  2599. #define VDR_Q4_K_Q8_1_MMQ 8
  2600. // contiguous v/x values
  2601. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2602. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2603. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2604. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2605. float sumf_d = 0.0f;
  2606. float sumf_m = 0.0f;
  2607. #pragma unroll
  2608. for (int i = 0; i < QR4_K; ++i) {
  2609. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2610. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2611. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2612. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2613. sumf_d += d8[i] * (dot1 * sc[i]);
  2614. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2615. }
  2616. const float2 dm4f = __half22float2(dm4);
  2617. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2618. #else
  2619. NO_DEVICE_CODE;
  2620. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2621. }
  2622. // contiguous u/y values
  2623. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2624. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2625. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2626. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2627. float sumf_d = 0.0f;
  2628. float sumf_m = 0.0f;
  2629. #pragma unroll
  2630. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2631. int sumi_d = 0;
  2632. #pragma unroll
  2633. for (int j = 0; j < QI8_1; ++j) {
  2634. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2635. }
  2636. const float2 ds8f = __half22float2(ds8[i]);
  2637. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2638. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2639. }
  2640. const float2 dm4f = __half22float2(dm4);
  2641. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2642. #else
  2643. NO_DEVICE_CODE;
  2644. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2645. }
  2646. #define VDR_Q5_K_Q8_1_MMVQ 2
  2647. #define VDR_Q5_K_Q8_1_MMQ 8
  2648. // contiguous v/x values
  2649. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2650. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2651. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2652. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2653. float sumf_d = 0.0f;
  2654. float sumf_m = 0.0f;
  2655. #pragma unroll
  2656. for (int i = 0; i < QR5_K; ++i) {
  2657. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2658. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2659. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2660. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2661. const int v0i = vl0i | vh0i;
  2662. const int v1i = vl1i | vh1i;
  2663. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2664. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2665. sumf_d += d8[i] * (dot1 * sc[i]);
  2666. sumf_m += d8[i] * (dot2 * m[i]);
  2667. }
  2668. const float2 dm5f = __half22float2(dm5);
  2669. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2670. #else
  2671. NO_DEVICE_CODE;
  2672. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2673. }
  2674. // contiguous u/y values
  2675. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2676. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2677. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2678. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2679. float sumf_d = 0.0f;
  2680. float sumf_m = 0.0f;
  2681. #pragma unroll
  2682. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2683. int sumi_d = 0;
  2684. #pragma unroll
  2685. for (int j = 0; j < QI8_1; ++j) {
  2686. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2687. }
  2688. const float2 ds8f = __half22float2(ds8[i]);
  2689. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2690. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2691. }
  2692. const float2 dm4f = __half22float2(dm4);
  2693. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2694. #else
  2695. NO_DEVICE_CODE;
  2696. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2697. }
  2698. #define VDR_Q6_K_Q8_1_MMVQ 1
  2699. #define VDR_Q6_K_Q8_1_MMQ 8
  2700. // contiguous v/x values
  2701. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2702. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2703. const float & d, const float * __restrict__ d8) {
  2704. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2705. float sumf = 0.0f;
  2706. #pragma unroll
  2707. for (int i = 0; i < QR6_K; ++i) {
  2708. const int sc = scales[4*i];
  2709. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2710. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2711. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2712. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2713. }
  2714. return d*sumf;
  2715. #else
  2716. NO_DEVICE_CODE;
  2717. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2718. }
  2719. // contiguous u/y values
  2720. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2721. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2722. const float & d6, const float * __restrict__ d8) {
  2723. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2724. float sumf_d = 0.0f;
  2725. #pragma unroll
  2726. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2727. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2728. #pragma unroll
  2729. for (int i = i0; i < i0 + 2; ++i) {
  2730. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2731. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2732. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2733. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2734. }
  2735. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2736. }
  2737. return d6 * sumf_d;
  2738. #else
  2739. NO_DEVICE_CODE;
  2740. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2741. }
  2742. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2743. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2744. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2745. int v[VDR_Q4_0_Q8_1_MMVQ];
  2746. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2747. #pragma unroll
  2748. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2749. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2750. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2751. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2752. }
  2753. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2754. }
  2755. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2756. (void)x_qh; (void)x_sc;
  2757. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2758. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2759. *x_ql = tile_x_qs;
  2760. *x_dm = (half2 *) tile_x_d;
  2761. }
  2762. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2763. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2764. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2765. (void)x_qh; (void)x_sc;
  2766. GGML_CUDA_ASSUME(i_offset >= 0);
  2767. GGML_CUDA_ASSUME(i_offset < nwarps);
  2768. GGML_CUDA_ASSUME(k >= 0);
  2769. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2770. const int kbx = k / QI4_0;
  2771. const int kqsx = k % QI4_0;
  2772. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2773. float * x_dmf = (float *) x_dm;
  2774. #pragma unroll
  2775. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2776. int i = i0 + i_offset;
  2777. if (need_check) {
  2778. i = min(i, i_max);
  2779. }
  2780. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2781. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2782. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2783. }
  2784. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2785. const int kbxd = k % blocks_per_tile_x_row;
  2786. #pragma unroll
  2787. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2788. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2789. if (need_check) {
  2790. i = min(i, i_max);
  2791. }
  2792. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2793. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2794. }
  2795. }
  2796. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2797. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2798. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2799. (void)x_qh; (void)x_sc;
  2800. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2801. const float * x_dmf = (const float *) x_dm;
  2802. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2803. #pragma unroll
  2804. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2805. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2806. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2807. }
  2808. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2809. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2810. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2811. }
  2812. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2813. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2814. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2815. int v[VDR_Q4_1_Q8_1_MMVQ];
  2816. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2817. #pragma unroll
  2818. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2819. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2820. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2821. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2822. }
  2823. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2824. }
  2825. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2826. (void)x_qh; (void)x_sc;
  2827. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2828. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2829. *x_ql = tile_x_qs;
  2830. *x_dm = tile_x_dm;
  2831. }
  2832. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2833. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2834. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2835. (void)x_qh; (void)x_sc;
  2836. GGML_CUDA_ASSUME(i_offset >= 0);
  2837. GGML_CUDA_ASSUME(i_offset < nwarps);
  2838. GGML_CUDA_ASSUME(k >= 0);
  2839. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2840. const int kbx = k / QI4_1;
  2841. const int kqsx = k % QI4_1;
  2842. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2843. #pragma unroll
  2844. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2845. int i = i0 + i_offset;
  2846. if (need_check) {
  2847. i = min(i, i_max);
  2848. }
  2849. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2850. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2851. }
  2852. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2853. const int kbxd = k % blocks_per_tile_x_row;
  2854. #pragma unroll
  2855. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2856. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2857. if (need_check) {
  2858. i = min(i, i_max);
  2859. }
  2860. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2861. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2862. }
  2863. }
  2864. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2865. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2866. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2867. (void)x_qh; (void)x_sc;
  2868. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2869. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2870. #pragma unroll
  2871. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2872. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2873. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2874. }
  2875. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2876. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2877. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2878. }
  2879. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2880. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2881. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2882. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2883. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2884. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2885. #pragma unroll
  2886. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2887. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2888. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2889. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2890. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2891. }
  2892. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2893. }
  2894. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2895. (void)x_qh; (void)x_sc;
  2896. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2897. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2898. *x_ql = tile_x_ql;
  2899. *x_dm = (half2 *) tile_x_d;
  2900. }
  2901. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2902. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2903. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2904. (void)x_qh; (void)x_sc;
  2905. GGML_CUDA_ASSUME(i_offset >= 0);
  2906. GGML_CUDA_ASSUME(i_offset < nwarps);
  2907. GGML_CUDA_ASSUME(k >= 0);
  2908. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2909. const int kbx = k / QI5_0;
  2910. const int kqsx = k % QI5_0;
  2911. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2912. #pragma unroll
  2913. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2914. int i = i0 + i_offset;
  2915. if (need_check) {
  2916. i = min(i, i_max);
  2917. }
  2918. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2919. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2920. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2921. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2922. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2923. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2924. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2925. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2926. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2927. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2928. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2929. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2930. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2931. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2932. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2933. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2934. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2935. }
  2936. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2937. const int kbxd = k % blocks_per_tile_x_row;
  2938. float * x_dmf = (float *) x_dm;
  2939. #pragma unroll
  2940. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2941. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2942. if (need_check) {
  2943. i = min(i, i_max);
  2944. }
  2945. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2946. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2947. }
  2948. }
  2949. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2950. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2951. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2952. (void)x_qh; (void)x_sc;
  2953. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2954. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2955. const float * x_dmf = (const float *) x_dm;
  2956. const float * y_df = (const float *) y_ds;
  2957. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2958. #pragma unroll
  2959. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2960. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2961. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2962. }
  2963. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2964. (&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)]);
  2965. }
  2966. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2967. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2968. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2969. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2970. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2971. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2972. #pragma unroll
  2973. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2974. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2975. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2976. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2977. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2978. }
  2979. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2980. }
  2981. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2982. (void)x_qh; (void)x_sc;
  2983. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2984. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2985. *x_ql = tile_x_ql;
  2986. *x_dm = tile_x_dm;
  2987. }
  2988. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2989. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2990. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2991. (void)x_qh; (void)x_sc;
  2992. GGML_CUDA_ASSUME(i_offset >= 0);
  2993. GGML_CUDA_ASSUME(i_offset < nwarps);
  2994. GGML_CUDA_ASSUME(k >= 0);
  2995. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2996. const int kbx = k / QI5_1;
  2997. const int kqsx = k % QI5_1;
  2998. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2999. #pragma unroll
  3000. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3001. int i = i0 + i_offset;
  3002. if (need_check) {
  3003. i = min(i, i_max);
  3004. }
  3005. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  3006. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3007. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  3008. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  3009. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  3010. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  3011. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  3012. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  3013. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  3014. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  3015. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  3016. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  3017. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  3018. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  3019. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  3020. }
  3021. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  3022. const int kbxd = k % blocks_per_tile_x_row;
  3023. #pragma unroll
  3024. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  3025. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  3026. if (need_check) {
  3027. i = min(i, i_max);
  3028. }
  3029. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3030. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  3031. }
  3032. }
  3033. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  3034. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3035. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3036. (void)x_qh; (void)x_sc;
  3037. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3038. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  3039. int u[2*VDR_Q5_1_Q8_1_MMQ];
  3040. #pragma unroll
  3041. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  3042. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3043. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  3044. }
  3045. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  3046. (&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)]);
  3047. }
  3048. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  3049. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3050. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  3051. int v[VDR_Q8_0_Q8_1_MMVQ];
  3052. int u[VDR_Q8_0_Q8_1_MMVQ];
  3053. #pragma unroll
  3054. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  3055. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  3056. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3057. }
  3058. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  3059. }
  3060. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3061. (void)x_qh; (void)x_sc;
  3062. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3063. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  3064. *x_ql = tile_x_qs;
  3065. *x_dm = (half2 *) tile_x_d;
  3066. }
  3067. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  3068. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3069. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3070. (void)x_qh; (void)x_sc;
  3071. GGML_CUDA_ASSUME(i_offset >= 0);
  3072. GGML_CUDA_ASSUME(i_offset < nwarps);
  3073. GGML_CUDA_ASSUME(k >= 0);
  3074. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3075. const int kbx = k / QI8_0;
  3076. const int kqsx = k % QI8_0;
  3077. float * x_dmf = (float *) x_dm;
  3078. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  3079. #pragma unroll
  3080. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3081. int i = i0 + i_offset;
  3082. if (need_check) {
  3083. i = min(i, i_max);
  3084. }
  3085. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3086. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  3087. }
  3088. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  3089. const int kbxd = k % blocks_per_tile_x_row;
  3090. #pragma unroll
  3091. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  3092. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  3093. if (need_check) {
  3094. i = min(i, i_max);
  3095. }
  3096. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3097. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  3098. }
  3099. }
  3100. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  3101. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3102. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3103. (void)x_qh; (void)x_sc;
  3104. const float * x_dmf = (const float *) x_dm;
  3105. const float * y_df = (const float *) y_ds;
  3106. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  3107. (&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],
  3108. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  3109. }
  3110. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  3111. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3112. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  3113. const int bq8_offset = QR2_K * (iqs / QI8_1);
  3114. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3115. const uint8_t * scales = bq2_K->scales + scale_offset;
  3116. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  3117. int u[QR2_K];
  3118. float d8[QR2_K];
  3119. #pragma unroll
  3120. for (int i = 0; i < QR2_K; ++ i) {
  3121. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3122. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3123. }
  3124. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  3125. }
  3126. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3127. (void)x_qh;
  3128. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3129. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  3130. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3131. *x_ql = tile_x_ql;
  3132. *x_dm = tile_x_dm;
  3133. *x_sc = tile_x_sc;
  3134. }
  3135. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  3136. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3137. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3138. (void)x_qh;
  3139. GGML_CUDA_ASSUME(i_offset >= 0);
  3140. GGML_CUDA_ASSUME(i_offset < nwarps);
  3141. GGML_CUDA_ASSUME(k >= 0);
  3142. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3143. const int kbx = k / QI2_K;
  3144. const int kqsx = k % QI2_K;
  3145. const block_q2_K * bx0 = (const block_q2_K *) vx;
  3146. #pragma unroll
  3147. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3148. int i = i0 + i_offset;
  3149. if (need_check) {
  3150. i = min(i, i_max);
  3151. }
  3152. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  3153. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3154. }
  3155. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  3156. const int kbxd = k % blocks_per_tile_x_row;
  3157. #pragma unroll
  3158. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  3159. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  3160. if (need_check) {
  3161. i = min(i, i_max);
  3162. }
  3163. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3164. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  3165. }
  3166. #pragma unroll
  3167. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3168. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3169. if (need_check) {
  3170. i = min(i, i_max);
  3171. }
  3172. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  3173. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  3174. }
  3175. }
  3176. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  3177. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3178. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3179. (void)x_qh;
  3180. const int kbx = k / QI2_K;
  3181. const int ky = (k % QI2_K) * QR2_K;
  3182. const float * y_df = (const float *) y_ds;
  3183. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  3184. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  3185. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  3186. #pragma unroll
  3187. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  3188. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3189. }
  3190. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  3191. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  3192. 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]);
  3193. }
  3194. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  3195. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3196. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  3197. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  3198. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3199. const float d = bq3_K->d;
  3200. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  3201. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3202. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  3203. int u[QR3_K];
  3204. float d8[QR3_K];
  3205. #pragma unroll
  3206. for (int i = 0; i < QR3_K; ++i) {
  3207. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3208. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3209. }
  3210. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  3211. }
  3212. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3213. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3214. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  3215. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  3216. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3217. *x_ql = tile_x_ql;
  3218. *x_dm = tile_x_dm;
  3219. *x_qh = tile_x_qh;
  3220. *x_sc = tile_x_sc;
  3221. }
  3222. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  3223. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3224. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3225. GGML_CUDA_ASSUME(i_offset >= 0);
  3226. GGML_CUDA_ASSUME(i_offset < nwarps);
  3227. GGML_CUDA_ASSUME(k >= 0);
  3228. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3229. const int kbx = k / QI3_K;
  3230. const int kqsx = k % QI3_K;
  3231. const block_q3_K * bx0 = (const block_q3_K *) vx;
  3232. #pragma unroll
  3233. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3234. int i = i0 + i_offset;
  3235. if (need_check) {
  3236. i = min(i, i_max);
  3237. }
  3238. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  3239. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3240. }
  3241. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  3242. const int kbxd = k % blocks_per_tile_x_row;
  3243. float * x_dmf = (float *) x_dm;
  3244. #pragma unroll
  3245. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  3246. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  3247. if (need_check) {
  3248. i = min(i, i_max);
  3249. }
  3250. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3251. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  3252. }
  3253. #pragma unroll
  3254. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  3255. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  3256. if (need_check) {
  3257. i = min(i, i_max);
  3258. }
  3259. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  3260. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3261. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  3262. }
  3263. #pragma unroll
  3264. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3265. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3266. if (need_check) {
  3267. i = min(i, i_max);
  3268. }
  3269. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  3270. const int ksc = k % (QI3_K/4);
  3271. const int ksc_low = ksc % (QI3_K/8);
  3272. const int shift_low = 4 * (ksc / (QI3_K/8));
  3273. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3274. const int ksc_high = QI3_K/8;
  3275. const int shift_high = 2 * ksc;
  3276. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3277. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3278. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3279. }
  3280. }
  3281. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3282. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3283. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3284. const int kbx = k / QI3_K;
  3285. const int ky = (k % QI3_K) * QR3_K;
  3286. const float * x_dmf = (const float *) x_dm;
  3287. const float * y_df = (const float *) y_ds;
  3288. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3289. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3290. #pragma unroll
  3291. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3292. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3293. const int shift = 2 * ((ky % 32) / 8);
  3294. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3295. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3296. const int vlh = (vh << 2) & 0x04040404;
  3297. v[l] = __vsubss4(vll, vlh);
  3298. }
  3299. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3300. 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]);
  3301. }
  3302. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3303. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3304. #ifndef GGML_QKK_64
  3305. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3306. int v[2];
  3307. int u[2*QR4_K];
  3308. float d8[QR4_K];
  3309. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3310. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3311. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3312. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3313. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3314. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3315. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3316. v[0] = q4[0];
  3317. v[1] = q4[4];
  3318. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3319. uint16_t aux[2];
  3320. const int j = bq8_offset/2;
  3321. if (j < 2) {
  3322. aux[0] = scales[j+0] & 0x3f3f;
  3323. aux[1] = scales[j+2] & 0x3f3f;
  3324. } else {
  3325. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3326. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3327. }
  3328. const uint8_t * sc = (const uint8_t *)aux;
  3329. const uint8_t * m = sc + 2;
  3330. for (int i = 0; i < QR4_K; ++i) {
  3331. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3332. d8[i] = __low2half(bq8i->ds);
  3333. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3334. u[2*i+0] = q8[0];
  3335. u[2*i+1] = q8[4];
  3336. }
  3337. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3338. #else
  3339. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3340. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3341. float sumf_d = 0.0f;
  3342. float sumf_m = 0.0f;
  3343. uint16_t aux16[2];
  3344. const uint8_t * s = (const uint8_t *)aux16;
  3345. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3346. aux16[0] = a[0] & 0x0f0f;
  3347. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3348. const float dall = bq4_K->dm[0];
  3349. const float dmin = bq4_K->dm[1];
  3350. const float d8_1 = __low2float(bq8_1[0].ds);
  3351. const float d8_2 = __low2float(bq8_1[1].ds);
  3352. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3353. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3354. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3355. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3356. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3357. const int v1 = q4[0];
  3358. const int v2 = q4[4];
  3359. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3360. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3361. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3362. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3363. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3364. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3365. return dall * sumf_d - dmin * sumf_m;
  3366. #else
  3367. NO_DEVICE_CODE;
  3368. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3369. #endif
  3370. }
  3371. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3372. (void)x_qh;
  3373. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3374. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3375. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3376. *x_ql = tile_x_ql;
  3377. *x_dm = tile_x_dm;
  3378. *x_sc = tile_x_sc;
  3379. }
  3380. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3381. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3382. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3383. (void)x_qh;
  3384. GGML_CUDA_ASSUME(i_offset >= 0);
  3385. GGML_CUDA_ASSUME(i_offset < nwarps);
  3386. GGML_CUDA_ASSUME(k >= 0);
  3387. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3388. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3389. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3390. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3391. #pragma unroll
  3392. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3393. int i = i0 + i_offset;
  3394. if (need_check) {
  3395. i = min(i, i_max);
  3396. }
  3397. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3398. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3399. }
  3400. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3401. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3402. #pragma unroll
  3403. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3404. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3405. if (need_check) {
  3406. i = min(i, i_max);
  3407. }
  3408. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3409. #if QK_K == 256
  3410. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3411. #else
  3412. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3413. #endif
  3414. }
  3415. #pragma unroll
  3416. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3417. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3418. if (need_check) {
  3419. i = min(i, i_max);
  3420. }
  3421. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3422. const int * scales = (const int *) bxi->scales;
  3423. const int ksc = k % (WARP_SIZE/8);
  3424. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3425. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3426. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3427. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3428. }
  3429. }
  3430. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3431. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3432. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3433. (void)x_qh;
  3434. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3435. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3436. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3437. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3438. }
  3439. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3440. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3441. #ifndef GGML_QKK_64
  3442. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3443. int vl[2];
  3444. int vh[2];
  3445. int u[2*QR5_K];
  3446. float d8[QR5_K];
  3447. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3448. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3449. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3450. vl[0] = ql[0];
  3451. vl[1] = ql[4];
  3452. vh[0] = qh[0] >> bq8_offset;
  3453. vh[1] = qh[4] >> bq8_offset;
  3454. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3455. uint16_t aux[2];
  3456. const int j = bq8_offset/2;
  3457. if (j < 2) {
  3458. aux[0] = scales[j+0] & 0x3f3f;
  3459. aux[1] = scales[j+2] & 0x3f3f;
  3460. } else {
  3461. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3462. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3463. }
  3464. const uint8_t * sc = (const uint8_t *)aux;
  3465. const uint8_t * m = sc + 2;
  3466. #pragma unroll
  3467. for (int i = 0; i < QR5_K; ++i) {
  3468. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3469. d8[i] = __low2float(bq8i->ds);
  3470. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3471. u[2*i+0] = q8[0];
  3472. u[2*i+1] = q8[4];
  3473. }
  3474. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3475. #else
  3476. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3477. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3478. const int8_t * s = bq5_K->scales;
  3479. const float d = bq5_K->d;
  3480. const float d8_1 = __low2half(bq8_1[0].ds);
  3481. const float d8_2 = __low2half(bq8_1[1].ds);
  3482. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3483. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3484. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3485. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3486. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3487. const int vl1 = ql[0];
  3488. const int vl2 = ql[4];
  3489. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3490. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3491. const int in = step%8; // 0, 4, 0, 4
  3492. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3493. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3494. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3495. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3496. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3497. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3498. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3499. return d * sumf_d;
  3500. #else
  3501. NO_DEVICE_CODE;
  3502. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3503. #endif
  3504. }
  3505. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3506. (void)x_qh;
  3507. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3508. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3509. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3510. *x_ql = tile_x_ql;
  3511. *x_dm = tile_x_dm;
  3512. *x_sc = tile_x_sc;
  3513. }
  3514. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3515. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3516. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3517. (void)x_qh;
  3518. GGML_CUDA_ASSUME(i_offset >= 0);
  3519. GGML_CUDA_ASSUME(i_offset < nwarps);
  3520. GGML_CUDA_ASSUME(k >= 0);
  3521. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3522. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3523. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3524. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3525. #pragma unroll
  3526. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3527. int i = i0 + i_offset;
  3528. if (need_check) {
  3529. i = min(i, i_max);
  3530. }
  3531. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3532. const int ky = QR5_K*kqsx;
  3533. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3534. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3535. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3536. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3537. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3538. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3539. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3540. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3541. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3542. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3543. }
  3544. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3545. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3546. #pragma unroll
  3547. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3548. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3549. if (need_check) {
  3550. i = min(i, i_max);
  3551. }
  3552. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3553. #if QK_K == 256
  3554. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3555. #endif
  3556. }
  3557. #pragma unroll
  3558. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3559. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3560. if (need_check) {
  3561. i = min(i, i_max);
  3562. }
  3563. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3564. const int * scales = (const int *) bxi->scales;
  3565. const int ksc = k % (WARP_SIZE/8);
  3566. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3567. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3568. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3569. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3570. }
  3571. }
  3572. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3573. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3574. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3575. (void)x_qh;
  3576. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3577. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3578. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3579. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3580. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3581. }
  3582. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3583. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3584. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3585. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3586. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3587. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3588. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3589. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3590. const int8_t * scales = bq6_K->scales + scale_offset;
  3591. int u[QR6_K];
  3592. float d8[QR6_K];
  3593. #pragma unroll
  3594. for (int i = 0; i < QR6_K; ++i) {
  3595. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3596. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3597. }
  3598. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3599. }
  3600. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3601. (void)x_qh;
  3602. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3603. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3604. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3605. *x_ql = tile_x_ql;
  3606. *x_dm = tile_x_dm;
  3607. *x_sc = tile_x_sc;
  3608. }
  3609. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3610. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3611. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3612. (void)x_qh;
  3613. GGML_CUDA_ASSUME(i_offset >= 0);
  3614. GGML_CUDA_ASSUME(i_offset < nwarps);
  3615. GGML_CUDA_ASSUME(k >= 0);
  3616. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3617. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3618. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3619. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3620. #pragma unroll
  3621. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3622. int i = i0 + i_offset;
  3623. if (need_check) {
  3624. i = min(i, i_max);
  3625. }
  3626. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3627. const int ky = QR6_K*kqsx;
  3628. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3629. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3630. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3631. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3632. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3633. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3634. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3635. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3636. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3637. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3638. }
  3639. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3640. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3641. float * x_dmf = (float *) x_dm;
  3642. #pragma unroll
  3643. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3644. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3645. if (need_check) {
  3646. i = min(i, i_max);
  3647. }
  3648. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3649. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3650. }
  3651. #pragma unroll
  3652. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3653. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3654. if (need_check) {
  3655. i = min(i, i_max);
  3656. }
  3657. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3658. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3659. }
  3660. }
  3661. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3662. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3663. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3664. (void)x_qh;
  3665. const float * x_dmf = (const float *) x_dm;
  3666. const float * y_df = (const float *) y_ds;
  3667. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3668. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3669. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3670. 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]);
  3671. }
  3672. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3673. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3674. #if QK_K == 256
  3675. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3676. #if QR2_XXS == 8
  3677. const int ib32 = iqs;
  3678. const uint16_t * q2 = bq2->qs + 4*ib32;
  3679. const uint8_t * aux8 = (const uint8_t *)q2;
  3680. const int8_t * q8 = bq8_1[ib32].qs;
  3681. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3682. int sumi = 0;
  3683. for (int l = 0; l < 4; ++l) {
  3684. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3685. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3686. for (int j = 0; j < 8; ++j) {
  3687. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3688. }
  3689. q8 += 8;
  3690. aux32 >>= 7;
  3691. }
  3692. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3693. return d * sumi;
  3694. #else
  3695. // iqs is 0...15
  3696. const int ib32 = iqs/2;
  3697. const int il = iqs%2;
  3698. const uint16_t * q2 = bq2->qs + 4*ib32;
  3699. const uint8_t * aux8 = (const uint8_t *)q2;
  3700. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  3701. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  3702. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3703. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3704. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3705. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3706. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3707. int sumi1 = 0, sumi2 = 0;
  3708. for (int j = 0; j < 8; ++j) {
  3709. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3710. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3711. }
  3712. return d * (sumi1 + sumi2);
  3713. #endif
  3714. #else
  3715. assert(false);
  3716. return 0.f;
  3717. #endif
  3718. }
  3719. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  3720. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3721. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3722. #if QK_K == 256
  3723. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  3724. const int ib32 = iqs;
  3725. const uint16_t * q2 = bq2->qs + 4*ib32;
  3726. const int8_t * q8 = bq8_1[ib32].qs;
  3727. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3728. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3729. int sumi1 = 0;
  3730. for (int l = 0; l < 2; ++l) {
  3731. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3732. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3733. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3734. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3735. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3736. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3737. q8 += 8;
  3738. }
  3739. int sumi2 = 0;
  3740. for (int l = 2; l < 4; ++l) {
  3741. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3742. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3743. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3744. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3745. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3746. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3747. q8 += 8;
  3748. }
  3749. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3750. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3751. #else
  3752. (void) ksigns64;
  3753. assert(false);
  3754. return 0.f;
  3755. #endif
  3756. #else
  3757. (void) ksigns64;
  3758. assert(false);
  3759. return 0.f;
  3760. #endif
  3761. }
  3762. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  3763. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3764. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3765. #if QK_K == 256
  3766. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  3767. const int ib32 = iqs;
  3768. const uint8_t * q3 = bq2->qs + 8*ib32;
  3769. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  3770. const int8_t * q8 = bq8_1[ib32].qs;
  3771. uint32_t aux32 = gas[0] | (gas[1] << 16);
  3772. int sumi = 0;
  3773. for (int l = 0; l < 4; ++l) {
  3774. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  3775. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  3776. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  3777. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  3778. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  3779. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3780. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3781. q8 += 8;
  3782. aux32 >>= 7;
  3783. }
  3784. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  3785. return d * sumi;
  3786. #else
  3787. assert(false);
  3788. return 0.f;
  3789. #endif
  3790. #else
  3791. assert(false);
  3792. return 0.f;
  3793. #endif
  3794. }
  3795. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  3796. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3797. #if QK_K == 256
  3798. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  3799. const int ib32 = iqs;
  3800. int sumi1 = 0, sumi2 = 0, sumi3 = 0, sumi4 = 0;
  3801. const uint8_t h1 = bq1->scales[2*ib32+0];
  3802. const uint8_t h2 = bq1->scales[2*ib32+1];
  3803. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3804. const int * q8 = (const int *)bq8_1[ib32].qs;
  3805. const int * grid1 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3806. const int * grid2 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3807. const int * grid3 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3808. const int * grid4 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3809. for (int j = 0; j < 2; ++j) {
  3810. sumi1 = __dp4a(q8[j+0], grid1[j], sumi1);
  3811. sumi2 = __dp4a(q8[j+2], grid2[j], sumi2);
  3812. sumi3 = __dp4a(q8[j+4], grid3[j], sumi3);
  3813. sumi4 = __dp4a(q8[j+6], grid4[j], sumi4);
  3814. }
  3815. #else
  3816. const int8_t * q8 = bq8_1[ib32].qs;
  3817. const int8_t * grid1 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3818. const int8_t * grid2 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3819. const int8_t * grid3 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3820. const int8_t * grid4 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3821. for (int j = 0; j < 8; ++j) {
  3822. sumi1 += q8[j+ 0] * grid1[j];
  3823. sumi2 += q8[j+ 8] * grid2[j];
  3824. sumi3 += q8[j+16] * grid3[j];
  3825. sumi4 += q8[j+24] * grid4[j];
  3826. }
  3827. #endif
  3828. const float d = (float)bq1->d * __low2float(bq8_1[ib32].ds);
  3829. return d * (sumi1 * (2*(h1 & 7) + 1) + sumi2 * (2*((h1 >> 4) & 7) + 1) +
  3830. sumi3 * (2*(h2 & 7) + 1) + sumi4 * (2*((h2 >> 4) & 7) + 1));
  3831. #else
  3832. assert(false);
  3833. return 0.f;
  3834. #endif
  3835. }
  3836. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3837. static __device__ __forceinline__ void get_int_from_table_16(const uint32_t & q4, const uint8_t * values,
  3838. int & val1, int & val2) {
  3839. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  3840. aux32 = q4 & 0x0f0f0f0f;
  3841. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  3842. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  3843. val1 = v1 | (v2 << 16);
  3844. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  3845. v1 = values[q8[0]] | (values[q8[1]] << 8);
  3846. v2 = values[q8[2]] | (values[q8[3]] << 8);
  3847. val2 = v1 | (v2 << 16);
  3848. }
  3849. #endif
  3850. static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
  3851. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3852. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  3853. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3854. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  3855. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  3856. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  3857. int v1, v2;
  3858. int sumi1 = 0, sumi2 = 0;
  3859. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3860. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  3861. get_int_from_table_16(aux, values, v1, v2);
  3862. sumi1 = __dp4a(v1, q8[l+0], sumi1);
  3863. sumi2 = __dp4a(v2, q8[l+4], sumi2);
  3864. }
  3865. #else
  3866. const uint8_t * q4 = bq->qs + 4*iqs;
  3867. const int8_t * q8 = bq8_1->qs + 4*iqs;
  3868. int sumi1 = 0, sumi2 = 0;
  3869. for (int l = 0; l < 4*VDR_Q4_0_Q8_1_MMVQ; ++l) {
  3870. sumi1 += q8[l+ 0] * kvalues_iq4nl[q4[l] & 0xf];
  3871. sumi2 += q8[l+16] * kvalues_iq4nl[q4[l] >> 4];
  3872. }
  3873. #endif
  3874. const float d = (float)bq->d * __low2float(bq8_1->ds);
  3875. return d * (sumi1 + sumi2);
  3876. }
  3877. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3878. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3879. static __device__ __forceinline__ void mul_mat_q(
  3880. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3881. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3882. const block_q_t * x = (const block_q_t *) vx;
  3883. const block_q8_1 * y = (const block_q8_1 *) vy;
  3884. const int blocks_per_row_x = ncols_x / qk;
  3885. const int blocks_per_col_y = nrows_y / QK8_1;
  3886. const int blocks_per_warp = WARP_SIZE / qi;
  3887. const int & ncols_dst = ncols_y;
  3888. const int row_dst_0 = blockIdx.x*mmq_y;
  3889. const int & row_x_0 = row_dst_0;
  3890. const int col_dst_0 = blockIdx.y*mmq_x;
  3891. const int & col_y_0 = col_dst_0;
  3892. int * tile_x_ql = nullptr;
  3893. half2 * tile_x_dm = nullptr;
  3894. int * tile_x_qh = nullptr;
  3895. int * tile_x_sc = nullptr;
  3896. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3897. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3898. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3899. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3900. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3901. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3902. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3903. #pragma unroll
  3904. for (int ir = 0; ir < qr; ++ir) {
  3905. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3906. const int kbxd = kqs / QI8_1;
  3907. #pragma unroll
  3908. for (int i = 0; i < mmq_x; i += nwarps) {
  3909. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3910. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3911. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3912. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3913. }
  3914. #pragma unroll
  3915. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3916. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3917. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3918. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3919. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3920. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3921. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3922. if (need_sum) {
  3923. *dsi_dst = *dsi_src;
  3924. } else {
  3925. float * dfi_dst = (float *) dsi_dst;
  3926. *dfi_dst = __low2half(*dsi_src);
  3927. }
  3928. }
  3929. __syncthreads();
  3930. // #pragma unroll // unrolling this loop causes too much register pressure
  3931. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3932. #pragma unroll
  3933. for (int j = 0; j < mmq_x; j += nwarps) {
  3934. #pragma unroll
  3935. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3936. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3937. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3938. threadIdx.x + i, threadIdx.y + j, k);
  3939. }
  3940. }
  3941. }
  3942. __syncthreads();
  3943. }
  3944. }
  3945. #pragma unroll
  3946. for (int j = 0; j < mmq_x; j += nwarps) {
  3947. const int col_dst = col_dst_0 + j + threadIdx.y;
  3948. if (col_dst >= ncols_dst) {
  3949. return;
  3950. }
  3951. #pragma unroll
  3952. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3953. const int row_dst = row_dst_0 + threadIdx.x + i;
  3954. if (row_dst >= nrows_dst) {
  3955. continue;
  3956. }
  3957. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3958. }
  3959. }
  3960. }
  3961. #define MMQ_X_Q4_0_RDNA2 64
  3962. #define MMQ_Y_Q4_0_RDNA2 128
  3963. #define NWARPS_Q4_0_RDNA2 8
  3964. #define MMQ_X_Q4_0_RDNA1 64
  3965. #define MMQ_Y_Q4_0_RDNA1 64
  3966. #define NWARPS_Q4_0_RDNA1 8
  3967. #if defined(CUDA_USE_TENSOR_CORES)
  3968. #define MMQ_X_Q4_0_AMPERE 4
  3969. #define MMQ_Y_Q4_0_AMPERE 32
  3970. #define NWARPS_Q4_0_AMPERE 4
  3971. #else
  3972. #define MMQ_X_Q4_0_AMPERE 64
  3973. #define MMQ_Y_Q4_0_AMPERE 128
  3974. #define NWARPS_Q4_0_AMPERE 4
  3975. #endif
  3976. #define MMQ_X_Q4_0_PASCAL 64
  3977. #define MMQ_Y_Q4_0_PASCAL 64
  3978. #define NWARPS_Q4_0_PASCAL 8
  3979. template <bool need_check> static __global__ void
  3980. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3981. #if defined(RDNA3) || defined(RDNA2)
  3982. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3983. #endif // defined(RDNA3) || defined(RDNA2)
  3984. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3985. mul_mat_q4_0(
  3986. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3987. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3988. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3989. #if defined(RDNA3) || defined(RDNA2)
  3990. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3991. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3992. const int nwarps = NWARPS_Q4_0_RDNA2;
  3993. #else
  3994. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3995. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3996. const int nwarps = NWARPS_Q4_0_RDNA1;
  3997. #endif // defined(RDNA3) || defined(RDNA2)
  3998. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3999. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4000. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4001. #elif __CUDA_ARCH__ >= CC_VOLTA
  4002. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  4003. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  4004. const int nwarps = NWARPS_Q4_0_AMPERE;
  4005. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4006. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4007. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4008. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4009. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  4010. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  4011. const int nwarps = NWARPS_Q4_0_PASCAL;
  4012. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  4013. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  4014. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4015. #else
  4016. (void) vec_dot_q4_0_q8_1_mul_mat;
  4017. NO_DEVICE_CODE;
  4018. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4019. }
  4020. #define MMQ_X_Q4_1_RDNA2 64
  4021. #define MMQ_Y_Q4_1_RDNA2 128
  4022. #define NWARPS_Q4_1_RDNA2 8
  4023. #define MMQ_X_Q4_1_RDNA1 64
  4024. #define MMQ_Y_Q4_1_RDNA1 64
  4025. #define NWARPS_Q4_1_RDNA1 8
  4026. #if defined(CUDA_USE_TENSOR_CORES)
  4027. #define MMQ_X_Q4_1_AMPERE 4
  4028. #define MMQ_Y_Q4_1_AMPERE 32
  4029. #define NWARPS_Q4_1_AMPERE 4
  4030. #else
  4031. #define MMQ_X_Q4_1_AMPERE 64
  4032. #define MMQ_Y_Q4_1_AMPERE 128
  4033. #define NWARPS_Q4_1_AMPERE 4
  4034. #endif
  4035. #define MMQ_X_Q4_1_PASCAL 64
  4036. #define MMQ_Y_Q4_1_PASCAL 64
  4037. #define NWARPS_Q4_1_PASCAL 8
  4038. template <bool need_check> static __global__ void
  4039. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4040. #if defined(RDNA3) || defined(RDNA2)
  4041. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  4042. #endif // defined(RDNA3) || defined(RDNA2)
  4043. #elif __CUDA_ARCH__ < CC_VOLTA
  4044. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  4045. #endif // __CUDA_ARCH__ < CC_VOLTA
  4046. mul_mat_q4_1(
  4047. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4048. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4049. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4050. #if defined(RDNA3) || defined(RDNA2)
  4051. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  4052. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  4053. const int nwarps = NWARPS_Q4_1_RDNA2;
  4054. #else
  4055. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  4056. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  4057. const int nwarps = NWARPS_Q4_1_RDNA1;
  4058. #endif // defined(RDNA3) || defined(RDNA2)
  4059. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4060. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4061. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4062. #elif __CUDA_ARCH__ >= CC_VOLTA
  4063. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  4064. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  4065. const int nwarps = NWARPS_Q4_1_AMPERE;
  4066. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4067. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4068. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4069. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4070. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  4071. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  4072. const int nwarps = NWARPS_Q4_1_PASCAL;
  4073. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4074. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4075. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4076. #else
  4077. (void) vec_dot_q4_1_q8_1_mul_mat;
  4078. NO_DEVICE_CODE;
  4079. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4080. }
  4081. #define MMQ_X_Q5_0_RDNA2 64
  4082. #define MMQ_Y_Q5_0_RDNA2 128
  4083. #define NWARPS_Q5_0_RDNA2 8
  4084. #define MMQ_X_Q5_0_RDNA1 64
  4085. #define MMQ_Y_Q5_0_RDNA1 64
  4086. #define NWARPS_Q5_0_RDNA1 8
  4087. #if defined(CUDA_USE_TENSOR_CORES)
  4088. #define MMQ_X_Q5_0_AMPERE 4
  4089. #define MMQ_Y_Q5_0_AMPERE 32
  4090. #define NWARPS_Q5_0_AMPERE 4
  4091. #else
  4092. #define MMQ_X_Q5_0_AMPERE 128
  4093. #define MMQ_Y_Q5_0_AMPERE 64
  4094. #define NWARPS_Q5_0_AMPERE 4
  4095. #endif
  4096. #define MMQ_X_Q5_0_PASCAL 64
  4097. #define MMQ_Y_Q5_0_PASCAL 64
  4098. #define NWARPS_Q5_0_PASCAL 8
  4099. template <bool need_check> static __global__ void
  4100. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4101. #if defined(RDNA3) || defined(RDNA2)
  4102. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  4103. #endif // defined(RDNA3) || defined(RDNA2)
  4104. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4105. mul_mat_q5_0(
  4106. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4107. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4108. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4109. #if defined(RDNA3) || defined(RDNA2)
  4110. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  4111. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  4112. const int nwarps = NWARPS_Q5_0_RDNA2;
  4113. #else
  4114. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  4115. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  4116. const int nwarps = NWARPS_Q5_0_RDNA1;
  4117. #endif // defined(RDNA3) || defined(RDNA2)
  4118. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4119. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4120. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4121. #elif __CUDA_ARCH__ >= CC_VOLTA
  4122. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  4123. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  4124. const int nwarps = NWARPS_Q5_0_AMPERE;
  4125. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4126. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4127. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4128. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4129. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  4130. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  4131. const int nwarps = NWARPS_Q5_0_PASCAL;
  4132. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4133. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4134. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4135. #else
  4136. (void) vec_dot_q5_0_q8_1_mul_mat;
  4137. NO_DEVICE_CODE;
  4138. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4139. }
  4140. #define MMQ_X_Q5_1_RDNA2 64
  4141. #define MMQ_Y_Q5_1_RDNA2 128
  4142. #define NWARPS_Q5_1_RDNA2 8
  4143. #define MMQ_X_Q5_1_RDNA1 64
  4144. #define MMQ_Y_Q5_1_RDNA1 64
  4145. #define NWARPS_Q5_1_RDNA1 8
  4146. #if defined(CUDA_USE_TENSOR_CORES)
  4147. #define MMQ_X_Q5_1_AMPERE 4
  4148. #define MMQ_Y_Q5_1_AMPERE 32
  4149. #define NWARPS_Q5_1_AMPERE 4
  4150. #else
  4151. #define MMQ_X_Q5_1_AMPERE 128
  4152. #define MMQ_Y_Q5_1_AMPERE 64
  4153. #define NWARPS_Q5_1_AMPERE 4
  4154. #endif
  4155. #define MMQ_X_Q5_1_PASCAL 64
  4156. #define MMQ_Y_Q5_1_PASCAL 64
  4157. #define NWARPS_Q5_1_PASCAL 8
  4158. template <bool need_check> static __global__ void
  4159. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4160. #if defined(RDNA3) || defined(RDNA2)
  4161. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4162. #endif // defined(RDNA3) || defined(RDNA2)
  4163. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4164. mul_mat_q5_1(
  4165. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4166. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4167. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4168. #if defined(RDNA3) || defined(RDNA2)
  4169. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4170. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4171. const int nwarps = NWARPS_Q5_1_RDNA2;
  4172. #else
  4173. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4174. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4175. const int nwarps = NWARPS_Q5_1_RDNA1;
  4176. #endif // defined(RDNA3) || defined(RDNA2)
  4177. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4178. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4179. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4180. #elif __CUDA_ARCH__ >= CC_VOLTA
  4181. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4182. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4183. const int nwarps = NWARPS_Q5_1_AMPERE;
  4184. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4185. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4186. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4187. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4188. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4189. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4190. const int nwarps = NWARPS_Q5_1_PASCAL;
  4191. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4192. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4193. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4194. #else
  4195. (void) vec_dot_q5_1_q8_1_mul_mat;
  4196. NO_DEVICE_CODE;
  4197. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4198. }
  4199. #define MMQ_X_Q8_0_RDNA2 64
  4200. #define MMQ_Y_Q8_0_RDNA2 128
  4201. #define NWARPS_Q8_0_RDNA2 8
  4202. #define MMQ_X_Q8_0_RDNA1 64
  4203. #define MMQ_Y_Q8_0_RDNA1 64
  4204. #define NWARPS_Q8_0_RDNA1 8
  4205. #if defined(CUDA_USE_TENSOR_CORES)
  4206. #define MMQ_X_Q8_0_AMPERE 4
  4207. #define MMQ_Y_Q8_0_AMPERE 32
  4208. #define NWARPS_Q8_0_AMPERE 4
  4209. #else
  4210. #define MMQ_X_Q8_0_AMPERE 128
  4211. #define MMQ_Y_Q8_0_AMPERE 64
  4212. #define NWARPS_Q8_0_AMPERE 4
  4213. #endif
  4214. #define MMQ_X_Q8_0_PASCAL 64
  4215. #define MMQ_Y_Q8_0_PASCAL 64
  4216. #define NWARPS_Q8_0_PASCAL 8
  4217. template <bool need_check> static __global__ void
  4218. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4219. #if defined(RDNA3) || defined(RDNA2)
  4220. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4221. #endif // defined(RDNA3) || defined(RDNA2)
  4222. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4223. mul_mat_q8_0(
  4224. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4225. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4226. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4227. #if defined(RDNA3) || defined(RDNA2)
  4228. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4229. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4230. const int nwarps = NWARPS_Q8_0_RDNA2;
  4231. #else
  4232. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4233. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4234. const int nwarps = NWARPS_Q8_0_RDNA1;
  4235. #endif // defined(RDNA3) || defined(RDNA2)
  4236. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4237. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4238. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4239. #elif __CUDA_ARCH__ >= CC_VOLTA
  4240. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4241. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4242. const int nwarps = NWARPS_Q8_0_AMPERE;
  4243. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4244. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4245. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4246. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4247. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4248. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4249. const int nwarps = NWARPS_Q8_0_PASCAL;
  4250. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4251. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4252. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4253. #else
  4254. (void) vec_dot_q8_0_q8_1_mul_mat;
  4255. NO_DEVICE_CODE;
  4256. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4257. }
  4258. #define MMQ_X_Q2_K_RDNA2 64
  4259. #define MMQ_Y_Q2_K_RDNA2 128
  4260. #define NWARPS_Q2_K_RDNA2 8
  4261. #define MMQ_X_Q2_K_RDNA1 128
  4262. #define MMQ_Y_Q2_K_RDNA1 32
  4263. #define NWARPS_Q2_K_RDNA1 8
  4264. #if defined(CUDA_USE_TENSOR_CORES)
  4265. #define MMQ_X_Q2_K_AMPERE 4
  4266. #define MMQ_Y_Q2_K_AMPERE 32
  4267. #define NWARPS_Q2_K_AMPERE 4
  4268. #else
  4269. #define MMQ_X_Q2_K_AMPERE 64
  4270. #define MMQ_Y_Q2_K_AMPERE 128
  4271. #define NWARPS_Q2_K_AMPERE 4
  4272. #endif
  4273. #define MMQ_X_Q2_K_PASCAL 64
  4274. #define MMQ_Y_Q2_K_PASCAL 64
  4275. #define NWARPS_Q2_K_PASCAL 8
  4276. template <bool need_check> static __global__ void
  4277. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4278. #if defined(RDNA3) || defined(RDNA2)
  4279. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4280. #endif // defined(RDNA3) || defined(RDNA2)
  4281. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4282. mul_mat_q2_K(
  4283. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4284. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4285. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4286. #if defined(RDNA3) || defined(RDNA2)
  4287. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4288. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4289. const int nwarps = NWARPS_Q2_K_RDNA2;
  4290. #else
  4291. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4292. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4293. const int nwarps = NWARPS_Q2_K_RDNA1;
  4294. #endif // defined(RDNA3) || defined(RDNA2)
  4295. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4296. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4297. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4298. #elif __CUDA_ARCH__ >= CC_VOLTA
  4299. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4300. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4301. const int nwarps = NWARPS_Q2_K_AMPERE;
  4302. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4303. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4304. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4305. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4306. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4307. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4308. const int nwarps = NWARPS_Q2_K_PASCAL;
  4309. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4310. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4311. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4312. #else
  4313. (void) vec_dot_q2_K_q8_1_mul_mat;
  4314. NO_DEVICE_CODE;
  4315. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4316. }
  4317. #define MMQ_X_Q3_K_RDNA2 128
  4318. #define MMQ_Y_Q3_K_RDNA2 64
  4319. #define NWARPS_Q3_K_RDNA2 8
  4320. #define MMQ_X_Q3_K_RDNA1 32
  4321. #define MMQ_Y_Q3_K_RDNA1 128
  4322. #define NWARPS_Q3_K_RDNA1 8
  4323. #if defined(CUDA_USE_TENSOR_CORES)
  4324. #define MMQ_X_Q3_K_AMPERE 4
  4325. #define MMQ_Y_Q3_K_AMPERE 32
  4326. #define NWARPS_Q3_K_AMPERE 4
  4327. #else
  4328. #define MMQ_X_Q3_K_AMPERE 128
  4329. #define MMQ_Y_Q3_K_AMPERE 128
  4330. #define NWARPS_Q3_K_AMPERE 4
  4331. #endif
  4332. #define MMQ_X_Q3_K_PASCAL 64
  4333. #define MMQ_Y_Q3_K_PASCAL 64
  4334. #define NWARPS_Q3_K_PASCAL 8
  4335. template <bool need_check> static __global__ void
  4336. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4337. #if defined(RDNA3) || defined(RDNA2)
  4338. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4339. #endif // defined(RDNA3) || defined(RDNA2)
  4340. #elif __CUDA_ARCH__ < CC_VOLTA
  4341. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4342. #endif // __CUDA_ARCH__ < CC_VOLTA
  4343. mul_mat_q3_K(
  4344. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4345. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4346. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4347. #if defined(RDNA3) || defined(RDNA2)
  4348. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4349. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4350. const int nwarps = NWARPS_Q3_K_RDNA2;
  4351. #else
  4352. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4353. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4354. const int nwarps = NWARPS_Q3_K_RDNA1;
  4355. #endif // defined(RDNA3) || defined(RDNA2)
  4356. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4357. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4358. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4359. #elif __CUDA_ARCH__ >= CC_VOLTA
  4360. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4361. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4362. const int nwarps = NWARPS_Q3_K_AMPERE;
  4363. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4364. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4365. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4366. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4367. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4368. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4369. const int nwarps = NWARPS_Q3_K_PASCAL;
  4370. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4371. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4372. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4373. #else
  4374. (void) vec_dot_q3_K_q8_1_mul_mat;
  4375. NO_DEVICE_CODE;
  4376. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4377. }
  4378. #define MMQ_X_Q4_K_RDNA2 64
  4379. #define MMQ_Y_Q4_K_RDNA2 128
  4380. #define NWARPS_Q4_K_RDNA2 8
  4381. #define MMQ_X_Q4_K_RDNA1 32
  4382. #define MMQ_Y_Q4_K_RDNA1 64
  4383. #define NWARPS_Q4_K_RDNA1 8
  4384. #if defined(CUDA_USE_TENSOR_CORES)
  4385. #define MMQ_X_Q4_K_AMPERE 4
  4386. #define MMQ_Y_Q4_K_AMPERE 32
  4387. #define NWARPS_Q4_K_AMPERE 4
  4388. #else
  4389. #define MMQ_X_Q4_K_AMPERE 64
  4390. #define MMQ_Y_Q4_K_AMPERE 128
  4391. #define NWARPS_Q4_K_AMPERE 4
  4392. #endif
  4393. #define MMQ_X_Q4_K_PASCAL 64
  4394. #define MMQ_Y_Q4_K_PASCAL 64
  4395. #define NWARPS_Q4_K_PASCAL 8
  4396. template <bool need_check> static __global__ void
  4397. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4398. #if defined(RDNA3) || defined(RDNA2)
  4399. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4400. #endif // defined(RDNA3) || defined(RDNA2)
  4401. #elif __CUDA_ARCH__ < CC_VOLTA
  4402. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4403. #endif // __CUDA_ARCH__ < CC_VOLTA
  4404. mul_mat_q4_K(
  4405. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4406. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4407. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4408. #if defined(RDNA3) || defined(RDNA2)
  4409. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4410. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4411. const int nwarps = NWARPS_Q4_K_RDNA2;
  4412. #else
  4413. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4414. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4415. const int nwarps = NWARPS_Q4_K_RDNA1;
  4416. #endif // defined(RDNA3) || defined(RDNA2)
  4417. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4418. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4419. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4420. #elif __CUDA_ARCH__ >= CC_VOLTA
  4421. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4422. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4423. const int nwarps = NWARPS_Q4_K_AMPERE;
  4424. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4425. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4426. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4427. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4428. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4429. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4430. const int nwarps = NWARPS_Q4_K_PASCAL;
  4431. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4432. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4433. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4434. #else
  4435. (void) vec_dot_q4_K_q8_1_mul_mat;
  4436. NO_DEVICE_CODE;
  4437. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4438. }
  4439. #define MMQ_X_Q5_K_RDNA2 64
  4440. #define MMQ_Y_Q5_K_RDNA2 128
  4441. #define NWARPS_Q5_K_RDNA2 8
  4442. #define MMQ_X_Q5_K_RDNA1 32
  4443. #define MMQ_Y_Q5_K_RDNA1 64
  4444. #define NWARPS_Q5_K_RDNA1 8
  4445. #if defined(CUDA_USE_TENSOR_CORES)
  4446. #define MMQ_X_Q5_K_AMPERE 4
  4447. #define MMQ_Y_Q5_K_AMPERE 32
  4448. #define NWARPS_Q5_K_AMPERE 4
  4449. #else
  4450. #define MMQ_X_Q5_K_AMPERE 64
  4451. #define MMQ_Y_Q5_K_AMPERE 128
  4452. #define NWARPS_Q5_K_AMPERE 4
  4453. #endif
  4454. #define MMQ_X_Q5_K_PASCAL 64
  4455. #define MMQ_Y_Q5_K_PASCAL 64
  4456. #define NWARPS_Q5_K_PASCAL 8
  4457. template <bool need_check> static __global__ void
  4458. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4459. #if defined(RDNA3) || defined(RDNA2)
  4460. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  4461. #endif // defined(RDNA3) || defined(RDNA2)
  4462. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4463. mul_mat_q5_K(
  4464. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4465. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4466. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4467. #if defined(RDNA3) || defined(RDNA2)
  4468. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  4469. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  4470. const int nwarps = NWARPS_Q5_K_RDNA2;
  4471. #else
  4472. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  4473. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  4474. const int nwarps = NWARPS_Q5_K_RDNA1;
  4475. #endif // defined(RDNA3) || defined(RDNA2)
  4476. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4477. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4478. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4479. #elif __CUDA_ARCH__ >= CC_VOLTA
  4480. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  4481. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  4482. const int nwarps = NWARPS_Q5_K_AMPERE;
  4483. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4484. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4485. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4486. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4487. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  4488. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  4489. const int nwarps = NWARPS_Q5_K_PASCAL;
  4490. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4491. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4492. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4493. #else
  4494. (void) vec_dot_q5_K_q8_1_mul_mat;
  4495. NO_DEVICE_CODE;
  4496. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4497. }
  4498. #define MMQ_X_Q6_K_RDNA2 64
  4499. #define MMQ_Y_Q6_K_RDNA2 128
  4500. #define NWARPS_Q6_K_RDNA2 8
  4501. #define MMQ_X_Q6_K_RDNA1 32
  4502. #define MMQ_Y_Q6_K_RDNA1 64
  4503. #define NWARPS_Q6_K_RDNA1 8
  4504. #if defined(CUDA_USE_TENSOR_CORES)
  4505. #define MMQ_X_Q6_K_AMPERE 4
  4506. #define MMQ_Y_Q6_K_AMPERE 32
  4507. #define NWARPS_Q6_K_AMPERE 4
  4508. #else
  4509. #define MMQ_X_Q6_K_AMPERE 64
  4510. #define MMQ_Y_Q6_K_AMPERE 64
  4511. #define NWARPS_Q6_K_AMPERE 4
  4512. #endif
  4513. #define MMQ_X_Q6_K_PASCAL 64
  4514. #define MMQ_Y_Q6_K_PASCAL 64
  4515. #define NWARPS_Q6_K_PASCAL 8
  4516. template <bool need_check> static __global__ void
  4517. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4518. #if defined(RDNA3) || defined(RDNA2)
  4519. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4520. #endif // defined(RDNA3) || defined(RDNA2)
  4521. #elif __CUDA_ARCH__ < CC_VOLTA
  4522. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4523. #endif // __CUDA_ARCH__ < CC_VOLTA
  4524. mul_mat_q6_K(
  4525. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4526. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4527. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4528. #if defined(RDNA3) || defined(RDNA2)
  4529. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  4530. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  4531. const int nwarps = NWARPS_Q6_K_RDNA2;
  4532. #else
  4533. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  4534. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  4535. const int nwarps = NWARPS_Q6_K_RDNA1;
  4536. #endif // defined(RDNA3) || defined(RDNA2)
  4537. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4538. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4539. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4540. #elif __CUDA_ARCH__ >= CC_VOLTA
  4541. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  4542. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  4543. const int nwarps = NWARPS_Q6_K_AMPERE;
  4544. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4545. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4546. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4547. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4548. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  4549. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  4550. const int nwarps = NWARPS_Q6_K_PASCAL;
  4551. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4552. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4553. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4554. #else
  4555. (void) vec_dot_q6_K_q8_1_mul_mat;
  4556. NO_DEVICE_CODE;
  4557. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4558. }
  4559. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  4560. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4561. // tell the compiler to use as many registers as it wants, see nwarps definition below
  4562. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  4563. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4564. static __global__ void mul_mat_vec_q(
  4565. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4566. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  4567. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  4568. constexpr int nwarps = 1;
  4569. constexpr int rows_per_cuda_block = 1;
  4570. #else
  4571. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  4572. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  4573. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  4574. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  4575. const int row0 = rows_per_cuda_block*blockIdx.x;
  4576. const int blocks_per_row_x = ncols_x / qk;
  4577. const int blocks_per_col_y = nrows_y / QK8_1;
  4578. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  4579. // partial sum for each thread
  4580. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  4581. const block_q_t * x = (const block_q_t *) vx;
  4582. const block_q8_1 * y = (const block_q8_1 *) vy;
  4583. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  4584. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  4585. // x block quant index when casting the quants to int
  4586. const int kqs = vdr * (tid % (qi/vdr));
  4587. #pragma unroll
  4588. for (int j = 0; j < ncols_y; ++j) {
  4589. #pragma unroll
  4590. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4591. tmp[j][i] += vec_dot_q_cuda(
  4592. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  4593. }
  4594. }
  4595. }
  4596. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  4597. if (threadIdx.y > 0) {
  4598. #pragma unroll
  4599. for (int j = 0; j < ncols_y; ++j) {
  4600. #pragma unroll
  4601. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4602. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  4603. }
  4604. }
  4605. }
  4606. __syncthreads();
  4607. if (threadIdx.y > 0) {
  4608. return;
  4609. }
  4610. // sum up partial sums and write back result
  4611. #pragma unroll
  4612. for (int j = 0; j < ncols_y; ++j) {
  4613. #pragma unroll
  4614. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4615. #pragma unroll
  4616. for (int l = 0; l < nwarps-1; ++l) {
  4617. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  4618. }
  4619. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  4620. }
  4621. if (threadIdx.x < rows_per_cuda_block) {
  4622. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  4623. }
  4624. }
  4625. }
  4626. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  4627. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  4628. // qk = quantized weights per x block
  4629. // qr = number of quantized weights per data value in x block
  4630. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4631. if (row >= nrows) {
  4632. return;
  4633. }
  4634. const int tid = threadIdx.x;
  4635. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  4636. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  4637. const int y_offset = qr == 1 ? 1 : qk/2;
  4638. // partial sum for each thread
  4639. #ifdef GGML_CUDA_F16
  4640. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  4641. #else
  4642. float tmp = 0.0f;
  4643. #endif // GGML_CUDA_F16
  4644. for (int i = 0; i < ncols; i += iter_stride) {
  4645. const int col = i + vals_per_iter*tid;
  4646. const int ib = (row*ncols + col)/qk; // x block index
  4647. const int iqs = (col%qk)/qr; // x quant index
  4648. const int iybs = col - col%qk; // y block start index
  4649. // processing >2 values per i iter is faster for fast GPUs
  4650. #pragma unroll
  4651. for (int j = 0; j < vals_per_iter; j += 2) {
  4652. // process 2 vals per j iter
  4653. // dequantize
  4654. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  4655. dfloat2 v;
  4656. dequantize_kernel(vx, ib, iqs + j/qr, v);
  4657. // matrix multiplication
  4658. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  4659. #ifdef GGML_CUDA_F16
  4660. tmp += __hmul2(v, {
  4661. y[iybs + iqs + j/qr + 0],
  4662. y[iybs + iqs + j/qr + y_offset]
  4663. });
  4664. #else
  4665. tmp += v.x * y[iybs + iqs + j/qr + 0];
  4666. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  4667. #endif // GGML_CUDA_F16
  4668. }
  4669. }
  4670. // sum up partial sums and write back result
  4671. #pragma unroll
  4672. for (int mask = 16; mask > 0; mask >>= 1) {
  4673. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4674. }
  4675. if (tid == 0) {
  4676. #ifdef GGML_CUDA_F16
  4677. dst[row] = tmp.x + tmp.y;
  4678. #else
  4679. dst[row] = tmp;
  4680. #endif // GGML_CUDA_F16
  4681. }
  4682. }
  4683. static __global__ void mul_mat_p021_f16_f32(
  4684. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  4685. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  4686. const half * x = (const half *) vx;
  4687. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4688. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4689. const int channel_x = channel / (nchannels_y / nchannels_x);
  4690. const int nrows_y = ncols_x;
  4691. const int nrows_dst = nrows_x;
  4692. const int row_dst = row_x;
  4693. float tmp = 0.0f;
  4694. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4695. const int col_x = col_x0 + threadIdx.x;
  4696. if (col_x >= ncols_x) {
  4697. break;
  4698. }
  4699. // x is transposed and permuted
  4700. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  4701. const float xi = __half2float(x[ix]);
  4702. const int row_y = col_x;
  4703. // y is not transposed but permuted
  4704. const int iy = channel*nrows_y + row_y;
  4705. tmp += xi * y[iy];
  4706. }
  4707. // dst is not transposed and not permuted
  4708. const int idst = channel*nrows_dst + row_dst;
  4709. // sum up partial sums and write back result
  4710. #pragma unroll
  4711. for (int mask = 16; mask > 0; mask >>= 1) {
  4712. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4713. }
  4714. if (threadIdx.x == 0) {
  4715. dst[idst] = tmp;
  4716. }
  4717. }
  4718. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  4719. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  4720. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  4721. const half * x = (const half *) vx;
  4722. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4723. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4724. const int channel_x = channel / channel_x_divisor;
  4725. const int nrows_y = ncols_x;
  4726. const int nrows_dst = nrows_x;
  4727. const int row_dst = row_x;
  4728. const int idst = channel*nrows_dst + row_dst;
  4729. float tmp = 0.0f;
  4730. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4731. const int col_x = col_x0 + threadIdx.x;
  4732. if (col_x >= ncols_x) {
  4733. break;
  4734. }
  4735. const int row_y = col_x;
  4736. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  4737. const int iy = channel*nrows_y + row_y;
  4738. const float xi = __half2float(x[ix]);
  4739. tmp += xi * y[iy];
  4740. }
  4741. // sum up partial sums and write back result
  4742. #pragma unroll
  4743. for (int mask = 16; mask > 0; mask >>= 1) {
  4744. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4745. }
  4746. if (threadIdx.x == 0) {
  4747. dst[idst] = tmp;
  4748. }
  4749. }
  4750. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4751. const float * xi = (const float *) cxi;
  4752. float * dsti = (float *) cdsti;
  4753. *dsti = *xi;
  4754. }
  4755. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4756. const float * xi = (const float *) cxi;
  4757. half * dsti = (half *) cdsti;
  4758. *dsti = __float2half(*xi);
  4759. }
  4760. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4761. const half * xi = (const half *) cxi;
  4762. half * dsti = (half *) cdsti;
  4763. *dsti = *xi;
  4764. }
  4765. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  4766. const half * xi = (const half *) cxi;
  4767. float * dsti = (float *) cdsti;
  4768. *dsti = *xi;
  4769. }
  4770. template <cpy_kernel_t cpy_1>
  4771. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4772. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4773. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4774. const int nb12, const int nb13) {
  4775. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4776. if (i >= ne) {
  4777. return;
  4778. }
  4779. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4780. // then combine those indices with the corresponding byte offsets to get the total offsets
  4781. const int i03 = i/(ne00 * ne01 * ne02);
  4782. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4783. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4784. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4785. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4786. const int i13 = i/(ne10 * ne11 * ne12);
  4787. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4788. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4789. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4790. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  4791. cpy_1(cx + x_offset, cdst + dst_offset);
  4792. }
  4793. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4794. const float * xi = (const float *) cxi;
  4795. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4796. float amax = 0.0f; // absolute max
  4797. for (int j = 0; j < QK8_0; j++) {
  4798. const float v = xi[j];
  4799. amax = fmaxf(amax, fabsf(v));
  4800. }
  4801. const float d = amax / ((1 << 7) - 1);
  4802. const float id = d ? 1.0f/d : 0.0f;
  4803. dsti->d = d;
  4804. for (int j = 0; j < QK8_0; ++j) {
  4805. const float x0 = xi[j]*id;
  4806. dsti->qs[j] = roundf(x0);
  4807. }
  4808. }
  4809. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4810. const float * xi = (const float *) cxi;
  4811. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4812. float amax = 0.0f;
  4813. float vmax = 0.0f;
  4814. for (int j = 0; j < QK4_0; ++j) {
  4815. const float v = xi[j];
  4816. if (amax < fabsf(v)) {
  4817. amax = fabsf(v);
  4818. vmax = v;
  4819. }
  4820. }
  4821. const float d = vmax / -8;
  4822. const float id = d ? 1.0f/d : 0.0f;
  4823. dsti->d = d;
  4824. for (int j = 0; j < QK4_0/2; ++j) {
  4825. const float x0 = xi[0 + j]*id;
  4826. const float x1 = xi[QK4_0/2 + j]*id;
  4827. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4828. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4829. dsti->qs[j] = xi0;
  4830. dsti->qs[j] |= xi1 << 4;
  4831. }
  4832. }
  4833. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4834. const float * xi = (const float *) cxi;
  4835. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4836. float vmin = FLT_MAX;
  4837. float vmax = -FLT_MAX;
  4838. for (int j = 0; j < QK4_1; ++j) {
  4839. const float v = xi[j];
  4840. if (v < vmin) vmin = v;
  4841. if (v > vmax) vmax = v;
  4842. }
  4843. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4844. const float id = d ? 1.0f/d : 0.0f;
  4845. dsti->dm.x = d;
  4846. dsti->dm.y = vmin;
  4847. for (int j = 0; j < QK4_1/2; ++j) {
  4848. const float x0 = (xi[0 + j] - vmin)*id;
  4849. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4850. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4851. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4852. dsti->qs[j] = xi0;
  4853. dsti->qs[j] |= xi1 << 4;
  4854. }
  4855. }
  4856. template <cpy_kernel_t cpy_blck, int qk>
  4857. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4858. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4859. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4860. const int nb12, const int nb13) {
  4861. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4862. if (i >= ne) {
  4863. return;
  4864. }
  4865. const int i03 = i/(ne00 * ne01 * ne02);
  4866. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4867. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4868. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4869. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4870. const int i13 = i/(ne10 * ne11 * ne12);
  4871. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4872. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4873. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4874. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  4875. cpy_blck(cx + x_offset, cdst + dst_offset);
  4876. }
  4877. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4878. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4879. return 1.0f - min(1.0f, max(0.0f, y));
  4880. }
  4881. struct rope_corr_dims {
  4882. float v[4];
  4883. };
  4884. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4885. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4886. static __device__ void rope_yarn(
  4887. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4888. float * cos_theta, float * sin_theta
  4889. ) {
  4890. // Get n-d rotational scaling corrected for extrapolation
  4891. float theta_interp = freq_scale * theta_extrap;
  4892. float theta = theta_interp;
  4893. if (ext_factor != 0.0f) {
  4894. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4895. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4896. // Get n-d magnitude scaling corrected for interpolation
  4897. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4898. }
  4899. *cos_theta = cosf(theta) * mscale;
  4900. *sin_theta = sinf(theta) * mscale;
  4901. }
  4902. // rope == RoPE == rotary positional embedding
  4903. template<typename T, bool has_pos>
  4904. static __global__ void rope(
  4905. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4906. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4907. ) {
  4908. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4909. if (col >= ncols) {
  4910. return;
  4911. }
  4912. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4913. const int i = row*ncols + col;
  4914. const int i2 = row/p_delta_rows;
  4915. const int p = has_pos ? pos[i2] : 0;
  4916. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4917. float cos_theta, sin_theta;
  4918. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4919. const float x0 = x[i + 0];
  4920. const float x1 = x[i + 1];
  4921. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4922. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4923. }
  4924. template<typename T, bool has_pos>
  4925. static __global__ void rope_neox(
  4926. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4927. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4928. ) {
  4929. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4930. if (col >= ncols) {
  4931. return;
  4932. }
  4933. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4934. const int ib = col / n_dims;
  4935. const int ic = col % n_dims;
  4936. if (ib > 0) {
  4937. const int i = row*ncols + ib*n_dims + ic;
  4938. dst[i + 0] = x[i + 0];
  4939. dst[i + 1] = x[i + 1];
  4940. return;
  4941. }
  4942. const int i = row*ncols + ib*n_dims + ic/2;
  4943. const int i2 = row/p_delta_rows;
  4944. float cur_rot = inv_ndims * ic - ib;
  4945. const int p = has_pos ? pos[i2] : 0;
  4946. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4947. float cos_theta, sin_theta;
  4948. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4949. const float x0 = x[i + 0];
  4950. const float x1 = x[i + n_dims/2];
  4951. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4952. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4953. }
  4954. static __global__ void rope_glm_f32(
  4955. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4956. int n_ctx
  4957. ) {
  4958. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4959. const int half_n_dims = ncols/4;
  4960. if (col >= half_n_dims) {
  4961. return;
  4962. }
  4963. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4964. const int i = row*ncols + col;
  4965. const int i2 = row/p_delta_rows;
  4966. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4967. // FIXME: this is likely wrong
  4968. const int p = pos != nullptr ? pos[i2] : 0;
  4969. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4970. const float sin_theta = sinf(theta);
  4971. const float cos_theta = cosf(theta);
  4972. const float x0 = x[i + 0];
  4973. const float x1 = x[i + half_n_dims];
  4974. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4975. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4976. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4977. const float sin_block_theta = sinf(block_theta);
  4978. const float cos_block_theta = cosf(block_theta);
  4979. const float x2 = x[i + half_n_dims * 2];
  4980. const float x3 = x[i + half_n_dims * 3];
  4981. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4982. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4983. }
  4984. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4985. const int n_heads_log2_floor, const float m0, const float m1) {
  4986. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4987. if (col >= ncols) {
  4988. return;
  4989. }
  4990. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4991. const int i = row*ncols + col;
  4992. const int k = row/k_rows;
  4993. float m_k;
  4994. if (k < n_heads_log2_floor) {
  4995. m_k = powf(m0, k + 1);
  4996. } else {
  4997. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4998. }
  4999. dst[i] = col * m_k + x[i];
  5000. }
  5001. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  5002. const int row = blockIdx.x;
  5003. const int col = threadIdx.x;
  5004. float sum = 0.0f;
  5005. for (int i = col; i < ncols; i += blockDim.x) {
  5006. sum += x[row * ncols + i];
  5007. }
  5008. sum = warp_reduce_sum(sum);
  5009. if (col == 0) {
  5010. dst[row] = sum;
  5011. }
  5012. }
  5013. template<typename T>
  5014. static inline __device__ void swap(T & a, T & b) {
  5015. T tmp = a;
  5016. a = b;
  5017. b = tmp;
  5018. }
  5019. template<ggml_sort_order order>
  5020. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  5021. // bitonic sort
  5022. int col = threadIdx.x;
  5023. int row = blockIdx.y;
  5024. if (col >= ncols) return;
  5025. const float * x_row = x + row * ncols;
  5026. int * dst_row = dst + row * ncols;
  5027. // initialize indices
  5028. if (col < ncols) {
  5029. dst_row[col] = col;
  5030. }
  5031. __syncthreads();
  5032. for (int k = 2; k <= ncols; k *= 2) {
  5033. for (int j = k / 2; j > 0; j /= 2) {
  5034. int ixj = col ^ j;
  5035. if (ixj > col) {
  5036. if ((col & k) == 0) {
  5037. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  5038. swap(dst_row[col], dst_row[ixj]);
  5039. }
  5040. } else {
  5041. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  5042. swap(dst_row[col], dst_row[ixj]);
  5043. }
  5044. }
  5045. }
  5046. __syncthreads();
  5047. }
  5048. }
  5049. }
  5050. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  5051. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  5052. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  5053. if (col >= ncols) {
  5054. return;
  5055. }
  5056. const int i = row*ncols + col;
  5057. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  5058. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  5059. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  5060. }
  5061. template <bool vals_smem, int ncols_template, int block_size_template>
  5062. 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) {
  5063. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  5064. const int tid = threadIdx.x;
  5065. const int rowx = blockIdx.x;
  5066. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  5067. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  5068. const int warp_id = threadIdx.x / WARP_SIZE;
  5069. const int lane_id = threadIdx.x % WARP_SIZE;
  5070. float slope = 0.0f;
  5071. // ALiBi
  5072. if (max_bias > 0.0f) {
  5073. const int h = rowx/nrows_y; // head index
  5074. const float base = h < n_head_log2 ? m0 : m1;
  5075. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  5076. slope = powf(base, exp);
  5077. }
  5078. extern __shared__ float data_soft_max_f32[];
  5079. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  5080. // shared memory buffer to cache values between iterations:
  5081. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  5082. float max_val = -INFINITY;
  5083. #pragma unroll
  5084. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5085. const int col = col0 + tid;
  5086. if (ncols_template == 0 && col >= ncols) {
  5087. break;
  5088. }
  5089. const int ix = rowx*ncols + col;
  5090. const int iy = rowy*ncols + col;
  5091. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  5092. vals[col] = val;
  5093. max_val = max(max_val, val);
  5094. }
  5095. // find the max value in the block
  5096. max_val = warp_reduce_max(max_val);
  5097. if (block_size > WARP_SIZE) {
  5098. if (warp_id == 0) {
  5099. buf_iw[lane_id] = -INFINITY;
  5100. }
  5101. __syncthreads();
  5102. if (lane_id == 0) {
  5103. buf_iw[warp_id] = max_val;
  5104. }
  5105. __syncthreads();
  5106. max_val = buf_iw[lane_id];
  5107. max_val = warp_reduce_max(max_val);
  5108. }
  5109. float tmp = 0.0f; // partial sum
  5110. #pragma unroll
  5111. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5112. const int col = col0 + tid;
  5113. if (ncols_template == 0 && col >= ncols) {
  5114. break;
  5115. }
  5116. const float val = expf(vals[col] - max_val);
  5117. tmp += val;
  5118. vals[col] = val;
  5119. }
  5120. // find the sum of exps in the block
  5121. tmp = warp_reduce_sum(tmp);
  5122. if (block_size > WARP_SIZE) {
  5123. if (warp_id == 0) {
  5124. buf_iw[lane_id] = 0.0f;
  5125. }
  5126. __syncthreads();
  5127. if (lane_id == 0) {
  5128. buf_iw[warp_id] = tmp;
  5129. }
  5130. __syncthreads();
  5131. tmp = buf_iw[lane_id];
  5132. tmp = warp_reduce_sum(tmp);
  5133. }
  5134. const float inv_sum = 1.0f / tmp;
  5135. #pragma unroll
  5136. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5137. const int col = col0 + tid;
  5138. if (ncols_template == 0 && col >= ncols) {
  5139. return;
  5140. }
  5141. const int idst = rowx*ncols + col;
  5142. dst[idst] = vals[col] * inv_sum;
  5143. }
  5144. }
  5145. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5146. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5147. if (i >= k) {
  5148. return;
  5149. }
  5150. dst[i] = scale * x[i];
  5151. }
  5152. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5153. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5154. if (i >= k) {
  5155. return;
  5156. }
  5157. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5158. }
  5159. template <typename T>
  5160. static __global__ void im2col_kernel(
  5161. const float * x, T * dst, int batch_offset,
  5162. int offset_delta, int IC, int IW, int IH, int OH, int OW, int KW, int KH, int pelements, int CHW,
  5163. int s0, int s1, int p0, int p1, int d0, int d1) {
  5164. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  5165. if (i >= pelements) {
  5166. return;
  5167. }
  5168. const int ksize = OW * (KH > 1 ? KW : 1);
  5169. const int kx = i / ksize;
  5170. const int kd = kx * ksize;
  5171. const int ky = (i - kd) / OW;
  5172. const int ix = i % OW;
  5173. const int oh = blockIdx.y;
  5174. const int batch = blockIdx.z / IC;
  5175. const int ic = blockIdx.z % IC;
  5176. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5177. const int64_t iih = oh * s1 + ky * d1 - p1;
  5178. const int64_t offset_dst =
  5179. ((batch * OH + oh) * OW + ix) * CHW +
  5180. (ic * (KW * KH) + ky * KW + kx);
  5181. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5182. dst[offset_dst] = 0.0f;
  5183. } else {
  5184. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5185. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5186. }
  5187. }
  5188. template <typename Ti, typename To>
  5189. static __global__ void pool2d_nchw_kernel(
  5190. const int ih, const int iw, const int oh, const int ow,
  5191. const int kh, const int kw, const int sh, const int sw,
  5192. const int ph, const int pw, const int parallel_elements,
  5193. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5194. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5195. if (idx >= parallel_elements) {
  5196. return;
  5197. }
  5198. const int I_HW = ih * iw;
  5199. const int O_HW = oh * ow;
  5200. const int nc = idx / O_HW;
  5201. const int cur_oh = idx % O_HW / ow;
  5202. const int cur_ow = idx % O_HW % ow;
  5203. const Ti* i_ptr = src + nc * I_HW;
  5204. To* o_ptr = dst + nc * O_HW;
  5205. const int start_h = cur_oh * sh - ph;
  5206. const int bh = max(0, start_h);
  5207. const int eh = min(ih, start_h + kh);
  5208. const int start_w = cur_ow * sw - pw;
  5209. const int bw = max(0, start_w);
  5210. const int ew = min(iw, start_w + kw);
  5211. const To scale = 1. / (kh * kw);
  5212. To res = 0;
  5213. switch (op) {
  5214. case GGML_OP_POOL_AVG: res = 0; break;
  5215. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5216. }
  5217. for (int i = bh; i < eh; i += 1) {
  5218. for (int j = bw; j < ew; j += 1) {
  5219. #if __CUDA_ARCH__ >= 350
  5220. Ti cur = __ldg(i_ptr + i * iw + j);
  5221. #else
  5222. Ti cur = i_ptr[i * iw + j];
  5223. #endif
  5224. switch (op) {
  5225. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5226. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5227. }
  5228. }
  5229. }
  5230. o_ptr[cur_oh * ow + cur_ow] = res;
  5231. }
  5232. template<int qk, int qr, dequantize_kernel_t dq>
  5233. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5234. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5235. GGML_TENSOR_BINARY_OP_LOCALS
  5236. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5237. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5238. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5239. // strides in elements
  5240. //const size_t s0 = nb0 / ggml_element_size(dst);
  5241. const size_t s1 = nb1 / ggml_element_size(dst);
  5242. const size_t s2 = nb2 / ggml_element_size(dst);
  5243. const size_t s3 = nb3 / ggml_element_size(dst);
  5244. const size_t s10 = nb10 / ggml_element_size(src1);
  5245. const size_t s11 = nb11 / ggml_element_size(src1);
  5246. const size_t s12 = nb12 / ggml_element_size(src1);
  5247. //const size_t s13 = nb13 / ggml_element_size(src1);
  5248. GGML_ASSERT(ne00 % 2 == 0);
  5249. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5250. src0_dd, src1_dd, dst_dd,
  5251. ne00, /*ne01, ne02, ne03,*/
  5252. /*ne10, ne11,*/ ne12, /*ne13,*/
  5253. /* s0,*/ s1, s2, s3,
  5254. /* nb00,*/ nb01, nb02, nb03,
  5255. s10, s11, s12/*, s13*/);
  5256. (void) dst;
  5257. }
  5258. template<typename src0_t>
  5259. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5260. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5261. GGML_TENSOR_BINARY_OP_LOCALS
  5262. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5263. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5264. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5265. // strides in elements
  5266. //const size_t s0 = nb0 / ggml_element_size(dst);
  5267. const size_t s1 = nb1 / ggml_element_size(dst);
  5268. const size_t s2 = nb2 / ggml_element_size(dst);
  5269. const size_t s3 = nb3 / ggml_element_size(dst);
  5270. const size_t s10 = nb10 / ggml_element_size(src1);
  5271. const size_t s11 = nb11 / ggml_element_size(src1);
  5272. const size_t s12 = nb12 / ggml_element_size(src1);
  5273. //const size_t s13 = nb13 / ggml_element_size(src1);
  5274. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5275. src0_dd, src1_dd, dst_dd,
  5276. ne00, /*ne01, ne02, ne03,*/
  5277. /*ne10, ne11,*/ ne12, /*ne13,*/
  5278. /* s0,*/ s1, s2, s3,
  5279. /* nb00,*/ nb01, nb02, nb03,
  5280. s10, s11, s12/*, s13*/);
  5281. (void) dst;
  5282. }
  5283. template<float (*bin_op)(const float, const float)>
  5284. struct bin_bcast_cuda {
  5285. template<typename src0_t, typename src1_t, typename dst_t>
  5286. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5287. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5288. cudaStream_t stream) {
  5289. GGML_TENSOR_BINARY_OP_LOCALS
  5290. int nr0 = ne10/ne0;
  5291. int nr1 = ne11/ne1;
  5292. int nr2 = ne12/ne2;
  5293. int nr3 = ne13/ne3;
  5294. int nr[4] = { nr0, nr1, nr2, nr3 };
  5295. // collapse dimensions until first broadcast dimension
  5296. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5297. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5298. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5299. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5300. auto collapse = [](int64_t cne[]) {
  5301. cne[0] *= cne[1];
  5302. cne[1] = cne[2];
  5303. cne[2] = cne[3];
  5304. cne[3] = 1;
  5305. };
  5306. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5307. cnb[1] *= cne[1];
  5308. cnb[2] *= cne[2];
  5309. cnb[3] *= cne[3];
  5310. };
  5311. for (int i = 0; i < 4; i++) {
  5312. if (nr[i] != 1) {
  5313. break;
  5314. }
  5315. if (i > 0) {
  5316. collapse_nb(cnb0, cne0);
  5317. collapse_nb(cnb1, cne1);
  5318. collapse(cne0);
  5319. collapse(cne1);
  5320. }
  5321. }
  5322. {
  5323. int64_t ne0 = cne0[0];
  5324. int64_t ne1 = cne0[1];
  5325. int64_t ne2 = cne0[2];
  5326. int64_t ne3 = cne0[3];
  5327. int64_t ne10 = cne1[0];
  5328. int64_t ne11 = cne1[1];
  5329. int64_t ne12 = cne1[2];
  5330. int64_t ne13 = cne1[3];
  5331. size_t nb0 = cnb0[0];
  5332. size_t nb1 = cnb0[1];
  5333. size_t nb2 = cnb0[2];
  5334. size_t nb3 = cnb0[3];
  5335. size_t nb10 = cnb1[0];
  5336. size_t nb11 = cnb1[1];
  5337. size_t nb12 = cnb1[2];
  5338. size_t nb13 = cnb1[3];
  5339. size_t s0 = nb0 / sizeof(dst_t);
  5340. size_t s1 = nb1 / sizeof(dst_t);
  5341. size_t s2 = nb2 / sizeof(dst_t);
  5342. size_t s3 = nb3 / sizeof(dst_t);
  5343. size_t s10 = nb10 / sizeof(src1_t);
  5344. size_t s11 = nb11 / sizeof(src1_t);
  5345. size_t s12 = nb12 / sizeof(src1_t);
  5346. size_t s13 = nb13 / sizeof(src1_t);
  5347. GGML_ASSERT(s0 == 1);
  5348. GGML_ASSERT(s10 == 1);
  5349. const int block_size = 128;
  5350. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5351. dim3 block_dims;
  5352. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5353. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5354. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5355. dim3 block_nums(
  5356. (hne0 + block_dims.x - 1) / block_dims.x,
  5357. (ne1 + block_dims.y - 1) / block_dims.y,
  5358. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5359. );
  5360. if (block_nums.z > 65535) {
  5361. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5362. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5363. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5364. src0_dd, src1_dd, dst_dd,
  5365. ne0, ne1, ne2, ne3,
  5366. ne10, ne11, ne12, ne13,
  5367. /* s0, */ s1, s2, s3,
  5368. /* s10, */ s11, s12, s13);
  5369. } else {
  5370. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5371. src0_dd, src1_dd, dst_dd,
  5372. ne0, ne1, ne2, ne3,
  5373. ne10, ne11, ne12, ne13,
  5374. /* s0, */ s1, s2, s3,
  5375. /* s10, */ s11, s12, s13);
  5376. }
  5377. }
  5378. }
  5379. };
  5380. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5381. const int ne10, const int ne11, const int ne12,
  5382. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5383. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5384. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5385. }
  5386. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5387. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5388. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5389. }
  5390. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5391. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5392. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5393. }
  5394. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5395. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5396. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5397. }
  5398. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5399. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5400. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5401. }
  5402. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5403. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5404. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5405. }
  5406. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5407. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5408. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5409. }
  5410. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5411. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5412. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5413. }
  5414. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5415. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5416. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5417. }
  5418. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5419. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5420. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5421. }
  5422. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5423. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5424. if (ncols < 1024) {
  5425. const dim3 block_dims(WARP_SIZE, 1, 1);
  5426. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5427. } else {
  5428. const dim3 block_dims(1024, 1, 1);
  5429. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5430. }
  5431. }
  5432. 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) {
  5433. static const float eps = 1e-6f;
  5434. if (group_size < 1024) {
  5435. const dim3 block_dims(WARP_SIZE, 1, 1);
  5436. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5437. } else {
  5438. const dim3 block_dims(1024, 1, 1);
  5439. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5440. }
  5441. }
  5442. 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) {
  5443. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5444. dim3 gridDim(num_blocks, ne1, ne2);
  5445. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5446. }
  5447. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int scale_factor, cudaStream_t stream) {
  5448. int ne0 = (ne00 * scale_factor);
  5449. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5450. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  5451. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  5452. }
  5453. static void pad_f32_cuda(const float * x, float * dst,
  5454. const int ne00, const int ne01, const int ne02,
  5455. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  5456. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  5457. dim3 gridDim(num_blocks, ne1, ne2);
  5458. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  5459. }
  5460. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5461. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5462. if (ncols < 1024) {
  5463. const dim3 block_dims(WARP_SIZE, 1, 1);
  5464. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5465. } else {
  5466. const dim3 block_dims(1024, 1, 1);
  5467. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5468. }
  5469. }
  5470. 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) {
  5471. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  5472. const dim3 num_blocks(block_num_x, ky, 1);
  5473. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  5474. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  5475. }
  5476. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5477. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5478. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  5479. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5480. }
  5481. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  5482. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  5483. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  5484. const bool need_check = false;
  5485. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5486. } else {
  5487. const bool need_check = true;
  5488. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5489. }
  5490. }
  5491. template<typename dst_t>
  5492. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5493. const int nb = k / QK_K;
  5494. #if QK_K == 256
  5495. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  5496. #else
  5497. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  5498. #endif
  5499. }
  5500. template<typename dst_t>
  5501. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5502. const int nb = k / QK_K;
  5503. #if QK_K == 256
  5504. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  5505. #else
  5506. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  5507. #endif
  5508. }
  5509. template<typename dst_t>
  5510. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5511. const int nb32 = k / 32;
  5512. const int nb = (k + 255) / 256;
  5513. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5514. }
  5515. template<typename dst_t>
  5516. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5517. const int nb32 = k / 32;
  5518. const int nb = (k + 255) / 256;
  5519. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5520. }
  5521. template<typename dst_t>
  5522. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5523. const int nb = k / QK_K;
  5524. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  5525. }
  5526. template<typename dst_t>
  5527. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5528. const int nb = k / QK_K;
  5529. #if QK_K == 256
  5530. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  5531. #else
  5532. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  5533. #endif
  5534. }
  5535. template<typename dst_t>
  5536. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5537. const int nb = k / QK_K;
  5538. #if QK_K == 256
  5539. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  5540. #else
  5541. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  5542. #endif
  5543. }
  5544. template<typename dst_t>
  5545. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5546. const int nb = k / QK_K;
  5547. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5548. }
  5549. template<typename dst_t>
  5550. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5551. const int nb = k / QK_K;
  5552. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  5553. }
  5554. template<typename dst_t>
  5555. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5556. const int nb = k / QK_K;
  5557. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5558. }
  5559. template<typename dst_t>
  5560. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5561. const int nb = k / QK_K;
  5562. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  5563. }
  5564. template<typename dst_t>
  5565. static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5566. const int nb = (k + QK_K - 1) / QK_K;
  5567. dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
  5568. }
  5569. template <typename src_t, typename dst_t>
  5570. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5571. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  5572. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5573. }
  5574. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  5575. int id;
  5576. switch (type) {
  5577. case GGML_TYPE_Q4_0:
  5578. return dequantize_row_q4_0_cuda;
  5579. case GGML_TYPE_Q4_1:
  5580. return dequantize_row_q4_1_cuda;
  5581. case GGML_TYPE_Q5_0:
  5582. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5583. case GGML_TYPE_Q5_1:
  5584. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5585. case GGML_TYPE_Q8_0:
  5586. CUDA_CHECK(cudaGetDevice(&id));
  5587. if (g_device_caps[id].cc >= CC_PASCAL) {
  5588. return dequantize_block_q8_0_f16_cuda;
  5589. }
  5590. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5591. case GGML_TYPE_Q2_K:
  5592. return dequantize_row_q2_K_cuda;
  5593. case GGML_TYPE_Q3_K:
  5594. return dequantize_row_q3_K_cuda;
  5595. case GGML_TYPE_Q4_K:
  5596. return dequantize_row_q4_K_cuda;
  5597. case GGML_TYPE_Q5_K:
  5598. return dequantize_row_q5_K_cuda;
  5599. case GGML_TYPE_Q6_K:
  5600. return dequantize_row_q6_K_cuda;
  5601. case GGML_TYPE_IQ2_XXS:
  5602. return dequantize_row_iq2_xxs_cuda;
  5603. case GGML_TYPE_IQ2_XS:
  5604. return dequantize_row_iq2_xs_cuda;
  5605. case GGML_TYPE_IQ3_XXS:
  5606. return dequantize_row_iq3_xxs_cuda;
  5607. case GGML_TYPE_IQ1_S:
  5608. return dequantize_row_iq1_s_cuda;
  5609. case GGML_TYPE_IQ4_NL:
  5610. return dequantize_row_iq4_nl_cuda;
  5611. case GGML_TYPE_F32:
  5612. return convert_unary_cuda<float>;
  5613. default:
  5614. return nullptr;
  5615. }
  5616. }
  5617. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  5618. switch (type) {
  5619. case GGML_TYPE_Q4_0:
  5620. return dequantize_row_q4_0_cuda;
  5621. case GGML_TYPE_Q4_1:
  5622. return dequantize_row_q4_1_cuda;
  5623. case GGML_TYPE_Q5_0:
  5624. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5625. case GGML_TYPE_Q5_1:
  5626. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5627. case GGML_TYPE_Q8_0:
  5628. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5629. case GGML_TYPE_Q2_K:
  5630. return dequantize_row_q2_K_cuda;
  5631. case GGML_TYPE_Q3_K:
  5632. return dequantize_row_q3_K_cuda;
  5633. case GGML_TYPE_Q4_K:
  5634. return dequantize_row_q4_K_cuda;
  5635. case GGML_TYPE_Q5_K:
  5636. return dequantize_row_q5_K_cuda;
  5637. case GGML_TYPE_Q6_K:
  5638. return dequantize_row_q6_K_cuda;
  5639. case GGML_TYPE_IQ2_XXS:
  5640. return dequantize_row_iq2_xxs_cuda;
  5641. case GGML_TYPE_IQ2_XS:
  5642. return dequantize_row_iq2_xs_cuda;
  5643. case GGML_TYPE_IQ3_XXS:
  5644. return dequantize_row_iq3_xxs_cuda;
  5645. case GGML_TYPE_IQ1_S:
  5646. return dequantize_row_iq1_s_cuda;
  5647. case GGML_TYPE_IQ4_NL:
  5648. return dequantize_row_iq4_nl_cuda;
  5649. case GGML_TYPE_F16:
  5650. return convert_unary_cuda<half>;
  5651. default:
  5652. return nullptr;
  5653. }
  5654. }
  5655. 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) {
  5656. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5657. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5658. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  5659. const dim3 block_nums(block_num_y, 1, 1);
  5660. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5661. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  5662. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5663. }
  5664. 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) {
  5665. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5666. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5667. const dim3 block_nums(block_num_y, 1, 1);
  5668. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5669. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  5670. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5671. }
  5672. 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) {
  5673. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5674. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5675. const dim3 block_nums(block_num_y, 1, 1);
  5676. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5677. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  5678. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5679. }
  5680. 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) {
  5681. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5682. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5683. const dim3 block_nums(block_num_y, 1, 1);
  5684. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5685. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  5686. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5687. }
  5688. 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) {
  5689. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5690. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5691. const dim3 block_nums(block_num_y, 1, 1);
  5692. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5693. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  5694. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5695. }
  5696. 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) {
  5697. GGML_ASSERT(ncols % QK_K == 0);
  5698. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  5699. const int block_num_y = (nrows + ny - 1) / ny;
  5700. const dim3 block_nums(block_num_y, 1, 1);
  5701. const dim3 block_dims(32, ny, 1);
  5702. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5703. }
  5704. 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) {
  5705. GGML_ASSERT(ncols % QK_K == 0);
  5706. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5707. const int block_num_y = (nrows + ny - 1) / ny;
  5708. const dim3 block_nums(block_num_y, 1, 1);
  5709. const dim3 block_dims(32, ny, 1);
  5710. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5711. }
  5712. 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) {
  5713. GGML_ASSERT(ncols % QK_K == 0);
  5714. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5715. const int block_num_y = (nrows + ny - 1) / ny;
  5716. const dim3 block_nums(block_num_y, 1, 1);
  5717. const dim3 block_dims(32, ny, 1);
  5718. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5719. }
  5720. 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) {
  5721. GGML_ASSERT(ncols % QK_K == 0);
  5722. const dim3 block_dims(32, 1, 1);
  5723. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  5724. }
  5725. 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) {
  5726. GGML_ASSERT(ncols % QK_K == 0);
  5727. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5728. const int block_num_y = (nrows + ny - 1) / ny;
  5729. const dim3 block_nums(block_num_y, 1, 1);
  5730. const dim3 block_dims(32, ny, 1);
  5731. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5732. }
  5733. 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) {
  5734. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5735. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5736. const dim3 block_nums(block_num_y, 1, 1);
  5737. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5738. dequantize_mul_mat_vec<1, 1, convert_f16>
  5739. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5740. }
  5741. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  5742. static void mul_mat_vec_q_cuda(
  5743. const void * vx, const void * vy, float * dst,
  5744. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  5745. GGML_ASSERT(ncols_x % qk == 0);
  5746. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  5747. int id;
  5748. CUDA_CHECK(cudaGetDevice(&id));
  5749. int64_t nwarps = 1;
  5750. int64_t rows_per_cuda_block = 1;
  5751. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  5752. switch(ncols_y) {
  5753. case 1:
  5754. nwarps = 4;
  5755. rows_per_cuda_block = 1;
  5756. break;
  5757. case 2:
  5758. case 3:
  5759. case 4:
  5760. nwarps = 4;
  5761. rows_per_cuda_block = 2;
  5762. break;
  5763. case 5:
  5764. case 6:
  5765. case 7:
  5766. case 8:
  5767. nwarps = 2;
  5768. rows_per_cuda_block = 2;
  5769. break;
  5770. default:
  5771. GGML_ASSERT(false);
  5772. break;
  5773. }
  5774. }
  5775. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  5776. const dim3 block_nums(nblocks, 1, 1);
  5777. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5778. switch (ncols_y) {
  5779. case 1:
  5780. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  5781. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5782. break;
  5783. case 2:
  5784. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  5785. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5786. break;
  5787. case 3:
  5788. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  5789. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5790. break;
  5791. case 4:
  5792. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  5793. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5794. break;
  5795. case 5:
  5796. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  5797. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5798. break;
  5799. case 6:
  5800. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  5801. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5802. break;
  5803. case 7:
  5804. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  5805. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5806. break;
  5807. case 8:
  5808. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  5809. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5810. break;
  5811. default:
  5812. GGML_ASSERT(false);
  5813. break;
  5814. }
  5815. }
  5816. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5817. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5818. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5819. int id;
  5820. CUDA_CHECK(cudaGetDevice(&id));
  5821. const int compute_capability = g_device_caps[id].cc;
  5822. int mmq_x, mmq_y, nwarps;
  5823. if (compute_capability >= CC_RDNA2) {
  5824. mmq_x = MMQ_X_Q4_0_RDNA2;
  5825. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5826. nwarps = NWARPS_Q4_0_RDNA2;
  5827. } else if (compute_capability >= CC_OFFSET_AMD) {
  5828. mmq_x = MMQ_X_Q4_0_RDNA1;
  5829. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5830. nwarps = NWARPS_Q4_0_RDNA1;
  5831. } else if (compute_capability >= CC_VOLTA) {
  5832. mmq_x = MMQ_X_Q4_0_AMPERE;
  5833. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5834. nwarps = NWARPS_Q4_0_AMPERE;
  5835. } else if (compute_capability >= MIN_CC_DP4A) {
  5836. mmq_x = MMQ_X_Q4_0_PASCAL;
  5837. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5838. nwarps = NWARPS_Q4_0_PASCAL;
  5839. } else {
  5840. GGML_ASSERT(false);
  5841. }
  5842. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5843. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5844. const dim3 block_nums(block_num_x, block_num_y, 1);
  5845. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5846. if (nrows_x % mmq_y == 0) {
  5847. const bool need_check = false;
  5848. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5849. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5850. } else {
  5851. const bool need_check = true;
  5852. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5853. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5854. }
  5855. }
  5856. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5857. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5858. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5859. int id;
  5860. CUDA_CHECK(cudaGetDevice(&id));
  5861. const int compute_capability = g_device_caps[id].cc;
  5862. int mmq_x, mmq_y, nwarps;
  5863. if (compute_capability >= CC_RDNA2) {
  5864. mmq_x = MMQ_X_Q4_1_RDNA2;
  5865. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5866. nwarps = NWARPS_Q4_1_RDNA2;
  5867. } else if (compute_capability >= CC_OFFSET_AMD) {
  5868. mmq_x = MMQ_X_Q4_1_RDNA1;
  5869. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5870. nwarps = NWARPS_Q4_1_RDNA1;
  5871. } else if (compute_capability >= CC_VOLTA) {
  5872. mmq_x = MMQ_X_Q4_1_AMPERE;
  5873. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5874. nwarps = NWARPS_Q4_1_AMPERE;
  5875. } else if (compute_capability >= MIN_CC_DP4A) {
  5876. mmq_x = MMQ_X_Q4_1_PASCAL;
  5877. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5878. nwarps = NWARPS_Q4_1_PASCAL;
  5879. } else {
  5880. GGML_ASSERT(false);
  5881. }
  5882. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5883. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5884. const dim3 block_nums(block_num_x, block_num_y, 1);
  5885. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5886. if (nrows_x % mmq_y == 0) {
  5887. const bool need_check = false;
  5888. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5889. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5890. } else {
  5891. const bool need_check = true;
  5892. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5893. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5894. }
  5895. }
  5896. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5897. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5898. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5899. int id;
  5900. CUDA_CHECK(cudaGetDevice(&id));
  5901. const int compute_capability = g_device_caps[id].cc;
  5902. int mmq_x, mmq_y, nwarps;
  5903. if (compute_capability >= CC_RDNA2) {
  5904. mmq_x = MMQ_X_Q5_0_RDNA2;
  5905. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5906. nwarps = NWARPS_Q5_0_RDNA2;
  5907. } else if (compute_capability >= CC_OFFSET_AMD) {
  5908. mmq_x = MMQ_X_Q5_0_RDNA1;
  5909. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5910. nwarps = NWARPS_Q5_0_RDNA1;
  5911. } else if (compute_capability >= CC_VOLTA) {
  5912. mmq_x = MMQ_X_Q5_0_AMPERE;
  5913. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5914. nwarps = NWARPS_Q5_0_AMPERE;
  5915. } else if (compute_capability >= MIN_CC_DP4A) {
  5916. mmq_x = MMQ_X_Q5_0_PASCAL;
  5917. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5918. nwarps = NWARPS_Q5_0_PASCAL;
  5919. } else {
  5920. GGML_ASSERT(false);
  5921. }
  5922. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5923. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5924. const dim3 block_nums(block_num_x, block_num_y, 1);
  5925. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5926. if (nrows_x % mmq_y == 0) {
  5927. const bool need_check = false;
  5928. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5929. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5930. } else {
  5931. const bool need_check = true;
  5932. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5933. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5934. }
  5935. }
  5936. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5937. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5938. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5939. int id;
  5940. CUDA_CHECK(cudaGetDevice(&id));
  5941. const int compute_capability = g_device_caps[id].cc;
  5942. int mmq_x, mmq_y, nwarps;
  5943. if (compute_capability >= CC_RDNA2) {
  5944. mmq_x = MMQ_X_Q5_1_RDNA2;
  5945. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5946. nwarps = NWARPS_Q5_1_RDNA2;
  5947. } else if (compute_capability >= CC_OFFSET_AMD) {
  5948. mmq_x = MMQ_X_Q5_1_RDNA1;
  5949. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5950. nwarps = NWARPS_Q5_1_RDNA1;
  5951. } else if (compute_capability >= CC_VOLTA) {
  5952. mmq_x = MMQ_X_Q5_1_AMPERE;
  5953. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5954. nwarps = NWARPS_Q5_1_AMPERE;
  5955. } else if (compute_capability >= MIN_CC_DP4A) {
  5956. mmq_x = MMQ_X_Q5_1_PASCAL;
  5957. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5958. nwarps = NWARPS_Q5_1_PASCAL;
  5959. } else {
  5960. GGML_ASSERT(false);
  5961. }
  5962. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5963. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5964. const dim3 block_nums(block_num_x, block_num_y, 1);
  5965. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5966. if (nrows_x % mmq_y == 0) {
  5967. const bool need_check = false;
  5968. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5969. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5970. } else {
  5971. const bool need_check = true;
  5972. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5973. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5974. }
  5975. }
  5976. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5977. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5978. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5979. int id;
  5980. CUDA_CHECK(cudaGetDevice(&id));
  5981. const int compute_capability = g_device_caps[id].cc;
  5982. int mmq_x, mmq_y, nwarps;
  5983. if (compute_capability >= CC_RDNA2) {
  5984. mmq_x = MMQ_X_Q8_0_RDNA2;
  5985. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5986. nwarps = NWARPS_Q8_0_RDNA2;
  5987. } else if (compute_capability >= CC_OFFSET_AMD) {
  5988. mmq_x = MMQ_X_Q8_0_RDNA1;
  5989. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5990. nwarps = NWARPS_Q8_0_RDNA1;
  5991. } else if (compute_capability >= CC_VOLTA) {
  5992. mmq_x = MMQ_X_Q8_0_AMPERE;
  5993. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5994. nwarps = NWARPS_Q8_0_AMPERE;
  5995. } else if (compute_capability >= MIN_CC_DP4A) {
  5996. mmq_x = MMQ_X_Q8_0_PASCAL;
  5997. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5998. nwarps = NWARPS_Q8_0_PASCAL;
  5999. } else {
  6000. GGML_ASSERT(false);
  6001. }
  6002. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6003. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6004. const dim3 block_nums(block_num_x, block_num_y, 1);
  6005. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6006. if (nrows_x % mmq_y == 0) {
  6007. const bool need_check = false;
  6008. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6009. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6010. } else {
  6011. const bool need_check = true;
  6012. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  6013. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6014. }
  6015. }
  6016. static void ggml_mul_mat_q2_K_q8_1_cuda(
  6017. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6018. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6019. int id;
  6020. CUDA_CHECK(cudaGetDevice(&id));
  6021. const int compute_capability = g_device_caps[id].cc;
  6022. int mmq_x, mmq_y, nwarps;
  6023. if (compute_capability >= CC_RDNA2) {
  6024. mmq_x = MMQ_X_Q2_K_RDNA2;
  6025. mmq_y = MMQ_Y_Q2_K_RDNA2;
  6026. nwarps = NWARPS_Q2_K_RDNA2;
  6027. } else if (compute_capability >= CC_OFFSET_AMD) {
  6028. mmq_x = MMQ_X_Q2_K_RDNA1;
  6029. mmq_y = MMQ_Y_Q2_K_RDNA1;
  6030. nwarps = NWARPS_Q2_K_RDNA1;
  6031. } else if (compute_capability >= CC_VOLTA) {
  6032. mmq_x = MMQ_X_Q2_K_AMPERE;
  6033. mmq_y = MMQ_Y_Q2_K_AMPERE;
  6034. nwarps = NWARPS_Q2_K_AMPERE;
  6035. } else if (compute_capability >= MIN_CC_DP4A) {
  6036. mmq_x = MMQ_X_Q2_K_PASCAL;
  6037. mmq_y = MMQ_Y_Q2_K_PASCAL;
  6038. nwarps = NWARPS_Q2_K_PASCAL;
  6039. } else {
  6040. GGML_ASSERT(false);
  6041. }
  6042. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6043. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6044. const dim3 block_nums(block_num_x, block_num_y, 1);
  6045. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6046. if (nrows_x % mmq_y == 0) {
  6047. const bool need_check = false;
  6048. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6049. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6050. } else {
  6051. const bool need_check = true;
  6052. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6053. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6054. }
  6055. }
  6056. static void ggml_mul_mat_q3_K_q8_1_cuda(
  6057. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6058. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6059. #if QK_K == 256
  6060. int id;
  6061. CUDA_CHECK(cudaGetDevice(&id));
  6062. const int compute_capability = g_device_caps[id].cc;
  6063. int mmq_x, mmq_y, nwarps;
  6064. if (compute_capability >= CC_RDNA2) {
  6065. mmq_x = MMQ_X_Q3_K_RDNA2;
  6066. mmq_y = MMQ_Y_Q3_K_RDNA2;
  6067. nwarps = NWARPS_Q3_K_RDNA2;
  6068. } else if (compute_capability >= CC_OFFSET_AMD) {
  6069. mmq_x = MMQ_X_Q3_K_RDNA1;
  6070. mmq_y = MMQ_Y_Q3_K_RDNA1;
  6071. nwarps = NWARPS_Q3_K_RDNA1;
  6072. } else if (compute_capability >= CC_VOLTA) {
  6073. mmq_x = MMQ_X_Q3_K_AMPERE;
  6074. mmq_y = MMQ_Y_Q3_K_AMPERE;
  6075. nwarps = NWARPS_Q3_K_AMPERE;
  6076. } else if (compute_capability >= MIN_CC_DP4A) {
  6077. mmq_x = MMQ_X_Q3_K_PASCAL;
  6078. mmq_y = MMQ_Y_Q3_K_PASCAL;
  6079. nwarps = NWARPS_Q3_K_PASCAL;
  6080. } else {
  6081. GGML_ASSERT(false);
  6082. }
  6083. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6084. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6085. const dim3 block_nums(block_num_x, block_num_y, 1);
  6086. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6087. if (nrows_x % mmq_y == 0) {
  6088. const bool need_check = false;
  6089. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6090. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6091. } else {
  6092. const bool need_check = true;
  6093. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6094. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6095. }
  6096. #endif
  6097. }
  6098. static void ggml_mul_mat_q4_K_q8_1_cuda(
  6099. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6100. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6101. int id;
  6102. CUDA_CHECK(cudaGetDevice(&id));
  6103. const int compute_capability = g_device_caps[id].cc;
  6104. int mmq_x, mmq_y, nwarps;
  6105. if (compute_capability >= CC_RDNA2) {
  6106. mmq_x = MMQ_X_Q4_K_RDNA2;
  6107. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6108. nwarps = NWARPS_Q4_K_RDNA2;
  6109. } else if (compute_capability >= CC_OFFSET_AMD) {
  6110. mmq_x = MMQ_X_Q4_K_RDNA1;
  6111. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6112. nwarps = NWARPS_Q4_K_RDNA1;
  6113. } else if (compute_capability >= CC_VOLTA) {
  6114. mmq_x = MMQ_X_Q4_K_AMPERE;
  6115. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6116. nwarps = NWARPS_Q4_K_AMPERE;
  6117. } else if (compute_capability >= MIN_CC_DP4A) {
  6118. mmq_x = MMQ_X_Q4_K_PASCAL;
  6119. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6120. nwarps = NWARPS_Q4_K_PASCAL;
  6121. } else {
  6122. GGML_ASSERT(false);
  6123. }
  6124. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6125. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6126. const dim3 block_nums(block_num_x, block_num_y, 1);
  6127. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6128. if (nrows_x % mmq_y == 0) {
  6129. const bool need_check = false;
  6130. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6131. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6132. } else {
  6133. const bool need_check = true;
  6134. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6135. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6136. }
  6137. }
  6138. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6139. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6140. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6141. int id;
  6142. CUDA_CHECK(cudaGetDevice(&id));
  6143. const int compute_capability = g_device_caps[id].cc;
  6144. int mmq_x, mmq_y, nwarps;
  6145. if (compute_capability >= CC_RDNA2) {
  6146. mmq_x = MMQ_X_Q5_K_RDNA2;
  6147. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6148. nwarps = NWARPS_Q5_K_RDNA2;
  6149. } else if (compute_capability >= CC_OFFSET_AMD) {
  6150. mmq_x = MMQ_X_Q5_K_RDNA1;
  6151. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6152. nwarps = NWARPS_Q5_K_RDNA1;
  6153. } else if (compute_capability >= CC_VOLTA) {
  6154. mmq_x = MMQ_X_Q5_K_AMPERE;
  6155. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6156. nwarps = NWARPS_Q5_K_AMPERE;
  6157. } else if (compute_capability >= MIN_CC_DP4A) {
  6158. mmq_x = MMQ_X_Q5_K_PASCAL;
  6159. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6160. nwarps = NWARPS_Q5_K_PASCAL;
  6161. } else {
  6162. GGML_ASSERT(false);
  6163. }
  6164. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6165. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6166. const dim3 block_nums(block_num_x, block_num_y, 1);
  6167. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6168. if (nrows_x % mmq_y == 0) {
  6169. const bool need_check = false;
  6170. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6171. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6172. } else {
  6173. const bool need_check = true;
  6174. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6175. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6176. }
  6177. }
  6178. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6179. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6180. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6181. int id;
  6182. CUDA_CHECK(cudaGetDevice(&id));
  6183. const int compute_capability = g_device_caps[id].cc;
  6184. int mmq_x, mmq_y, nwarps;
  6185. if (compute_capability >= CC_RDNA2) {
  6186. mmq_x = MMQ_X_Q6_K_RDNA2;
  6187. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6188. nwarps = NWARPS_Q6_K_RDNA2;
  6189. } else if (compute_capability >= CC_OFFSET_AMD) {
  6190. mmq_x = MMQ_X_Q6_K_RDNA1;
  6191. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6192. nwarps = NWARPS_Q6_K_RDNA1;
  6193. } else if (compute_capability >= CC_VOLTA) {
  6194. mmq_x = MMQ_X_Q6_K_AMPERE;
  6195. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6196. nwarps = NWARPS_Q6_K_AMPERE;
  6197. } else if (compute_capability >= MIN_CC_DP4A) {
  6198. mmq_x = MMQ_X_Q6_K_PASCAL;
  6199. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6200. nwarps = NWARPS_Q6_K_PASCAL;
  6201. } else {
  6202. GGML_ASSERT(false);
  6203. }
  6204. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6205. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6206. const dim3 block_nums(block_num_x, block_num_y, 1);
  6207. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6208. if (nrows_x % mmq_y == 0) {
  6209. const bool need_check = false;
  6210. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6211. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6212. } else {
  6213. const bool need_check = true;
  6214. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6215. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6216. }
  6217. }
  6218. static void ggml_mul_mat_p021_f16_f32_cuda(
  6219. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6220. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6221. const dim3 block_nums(1, nrows_x, nchannels_y);
  6222. const dim3 block_dims(WARP_SIZE, 1, 1);
  6223. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6224. }
  6225. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6226. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6227. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6228. const dim3 block_nums(1, nrows_x, nchannels_y);
  6229. const dim3 block_dims(WARP_SIZE, 1, 1);
  6230. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6231. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6232. }
  6233. static void ggml_cpy_f16_f32_cuda(
  6234. const char * cx, char * cdst, const int ne,
  6235. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6236. 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) {
  6237. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6238. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6239. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6240. }
  6241. static void ggml_cpy_f32_f32_cuda(
  6242. const char * cx, char * cdst, const int ne,
  6243. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6244. 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) {
  6245. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6246. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6247. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6248. }
  6249. static void ggml_cpy_f32_f16_cuda(
  6250. const char * cx, char * cdst, const int ne,
  6251. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6252. 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) {
  6253. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6254. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6255. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6256. }
  6257. static void ggml_cpy_f32_q8_0_cuda(
  6258. const char * cx, char * cdst, const int ne,
  6259. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6260. 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) {
  6261. GGML_ASSERT(ne % QK8_0 == 0);
  6262. const int num_blocks = ne / QK8_0;
  6263. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6264. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6265. }
  6266. static void ggml_cpy_f32_q4_0_cuda(
  6267. const char * cx, char * cdst, const int ne,
  6268. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6269. 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) {
  6270. GGML_ASSERT(ne % QK4_0 == 0);
  6271. const int num_blocks = ne / QK4_0;
  6272. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6273. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6274. }
  6275. static void ggml_cpy_f32_q4_1_cuda(
  6276. const char * cx, char * cdst, const int ne,
  6277. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6278. 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) {
  6279. GGML_ASSERT(ne % QK4_1 == 0);
  6280. const int num_blocks = ne / QK4_1;
  6281. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6282. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6283. }
  6284. static void ggml_cpy_f16_f16_cuda(
  6285. const char * cx, char * cdst, const int ne,
  6286. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6287. 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) {
  6288. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6289. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6290. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6291. }
  6292. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  6293. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  6294. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  6295. }
  6296. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  6297. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  6298. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  6299. }
  6300. template<typename T>
  6301. static void rope_cuda(
  6302. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6303. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6304. ) {
  6305. GGML_ASSERT(ncols % 2 == 0);
  6306. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6307. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6308. const dim3 block_nums(nrows, num_blocks_x, 1);
  6309. if (pos == nullptr) {
  6310. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6311. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6312. );
  6313. } else {
  6314. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6315. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6316. );
  6317. }
  6318. }
  6319. template<typename T>
  6320. static void rope_neox_cuda(
  6321. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6322. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6323. ) {
  6324. GGML_ASSERT(ncols % 2 == 0);
  6325. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6326. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6327. const dim3 block_nums(nrows, num_blocks_x, 1);
  6328. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6329. const float inv_ndims = -1.0f / n_dims;
  6330. if (pos == nullptr) {
  6331. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6332. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6333. theta_scale, inv_ndims
  6334. );
  6335. } else {
  6336. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6337. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6338. theta_scale, inv_ndims
  6339. );
  6340. }
  6341. }
  6342. static void rope_glm_f32_cuda(
  6343. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6344. float freq_base, int n_ctx, cudaStream_t stream
  6345. ) {
  6346. GGML_ASSERT(ncols % 4 == 0);
  6347. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6348. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6349. const dim3 block_nums(num_blocks_x, nrows, 1);
  6350. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6351. }
  6352. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6353. const int k_rows, const int n_heads_log2_floor, const float m0,
  6354. const float m1, cudaStream_t stream) {
  6355. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6356. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6357. const dim3 block_nums(num_blocks_x, nrows, 1);
  6358. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6359. }
  6360. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6361. const dim3 block_dims(WARP_SIZE, 1, 1);
  6362. const dim3 block_nums(nrows, 1, 1);
  6363. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6364. }
  6365. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6366. // bitonic sort requires ncols to be power of 2
  6367. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6368. const dim3 block_dims(ncols, 1, 1);
  6369. const dim3 block_nums(1, nrows, 1);
  6370. if (order == GGML_SORT_ASC) {
  6371. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6372. } else if (order == GGML_SORT_DESC) {
  6373. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6374. } else {
  6375. GGML_ASSERT(false);
  6376. }
  6377. }
  6378. 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) {
  6379. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6380. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6381. const dim3 block_nums(nrows_x, block_num_x, 1);
  6382. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6383. }
  6384. 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) {
  6385. int nth = WARP_SIZE;
  6386. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6387. const dim3 block_dims(nth, 1, 1);
  6388. const dim3 block_nums(nrows_x, 1, 1);
  6389. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6390. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6391. const uint32_t n_head_kv = nrows_x/nrows_y;
  6392. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6393. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6394. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6395. if (shmem < g_device_caps[g_main_device].smpb) {
  6396. switch (ncols_x) {
  6397. case 32:
  6398. 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);
  6399. break;
  6400. case 64:
  6401. 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);
  6402. break;
  6403. case 128:
  6404. 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);
  6405. break;
  6406. case 256:
  6407. 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);
  6408. break;
  6409. case 512:
  6410. 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);
  6411. break;
  6412. case 1024:
  6413. 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);
  6414. break;
  6415. case 2048:
  6416. 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);
  6417. break;
  6418. case 4096:
  6419. 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);
  6420. break;
  6421. default:
  6422. 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);
  6423. break;
  6424. }
  6425. } else {
  6426. const size_t shmem_low = WARP_SIZE*sizeof(float);
  6427. 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);
  6428. }
  6429. }
  6430. template <typename T>
  6431. static void im2col_cuda(const float* x, T* dst,
  6432. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  6433. int batch, int batch_offset, int offset_delta,
  6434. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  6435. const int parallel_elements = OW * KW * KH;
  6436. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  6437. dim3 block_nums(num_blocks, OH, batch * IC);
  6438. 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);
  6439. }
  6440. // buffer pool for cuda
  6441. #define MAX_CUDA_BUFFERS 256
  6442. struct scoped_spin_lock {
  6443. std::atomic_flag& lock;
  6444. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  6445. while (lock.test_and_set(std::memory_order_acquire)) {
  6446. ; // spin
  6447. }
  6448. }
  6449. ~scoped_spin_lock() {
  6450. lock.clear(std::memory_order_release);
  6451. }
  6452. scoped_spin_lock(const scoped_spin_lock&) = delete;
  6453. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  6454. };
  6455. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  6456. // #define DEBUG_CUDA_MALLOC
  6457. struct ggml_cuda_buffer {
  6458. void * ptr = nullptr;
  6459. size_t size = 0;
  6460. };
  6461. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  6462. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  6463. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  6464. scoped_spin_lock lock(g_cuda_pool_lock);
  6465. #ifdef DEBUG_CUDA_MALLOC
  6466. int nnz = 0;
  6467. size_t max_size = 0;
  6468. #endif
  6469. size_t best_diff = 1ull << 36;
  6470. int ibest = -1;
  6471. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6472. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6473. if (b.ptr != nullptr) {
  6474. #ifdef DEBUG_CUDA_MALLOC
  6475. ++nnz;
  6476. if (b.size > max_size) max_size = b.size;
  6477. #endif
  6478. if (b.size >= size) {
  6479. size_t diff = b.size - size;
  6480. if (diff < best_diff) {
  6481. best_diff = diff;
  6482. ibest = i;
  6483. if (!best_diff) {
  6484. void * ptr = b.ptr;
  6485. *actual_size = b.size;
  6486. b.ptr = nullptr;
  6487. b.size = 0;
  6488. return ptr;
  6489. }
  6490. }
  6491. }
  6492. }
  6493. }
  6494. if (ibest >= 0) {
  6495. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  6496. void * ptr = b.ptr;
  6497. *actual_size = b.size;
  6498. b.ptr = nullptr;
  6499. b.size = 0;
  6500. return ptr;
  6501. }
  6502. void * ptr;
  6503. size_t look_ahead_size = (size_t) (1.05 * size);
  6504. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  6505. ggml_cuda_set_device(device);
  6506. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  6507. *actual_size = look_ahead_size;
  6508. g_cuda_pool_size[device] += look_ahead_size;
  6509. #ifdef DEBUG_CUDA_MALLOC
  6510. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  6511. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  6512. #endif
  6513. return ptr;
  6514. }
  6515. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  6516. scoped_spin_lock lock(g_cuda_pool_lock);
  6517. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6518. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6519. if (b.ptr == nullptr) {
  6520. b.ptr = ptr;
  6521. b.size = size;
  6522. return;
  6523. }
  6524. }
  6525. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  6526. ggml_cuda_set_device(device);
  6527. CUDA_CHECK(cudaFree(ptr));
  6528. g_cuda_pool_size[device] -= size;
  6529. }
  6530. #if !defined(GGML_USE_HIPBLAS)
  6531. // pool with virtual memory
  6532. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  6533. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  6534. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  6535. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  6536. scoped_spin_lock lock(g_cuda_pool_lock);
  6537. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  6538. const size_t alignment = 128;
  6539. size = alignment * ((size + alignment - 1) / alignment);
  6540. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  6541. if (size > avail) {
  6542. // round up to the next multiple of the granularity
  6543. size_t reserve_size = size - avail;
  6544. const size_t granularity = g_device_caps[device].vmm_granularity;
  6545. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  6546. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  6547. // allocate more physical memory
  6548. CUmemAllocationProp prop = {};
  6549. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6550. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6551. prop.location.id = device;
  6552. CUmemGenericAllocationHandle handle;
  6553. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  6554. // reserve virtual address space (if not already reserved)
  6555. if (g_cuda_pool_addr[device] == 0) {
  6556. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  6557. }
  6558. // map at the end of the pool
  6559. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  6560. // the memory allocation handle is no longer needed after mapping
  6561. CU_CHECK(cuMemRelease(handle));
  6562. // set access
  6563. CUmemAccessDesc access = {};
  6564. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6565. access.location.id = device;
  6566. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  6567. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  6568. // add to the pool
  6569. g_cuda_pool_size[device] += reserve_size;
  6570. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  6571. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  6572. // (unsigned long long) (reserve_size/1024/1024));
  6573. }
  6574. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  6575. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  6576. *actual_size = size;
  6577. g_cuda_pool_used[device] += size;
  6578. #ifdef DEBUG_CUDA_MALLOC
  6579. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  6580. #endif
  6581. return ptr;
  6582. }
  6583. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  6584. scoped_spin_lock lock(g_cuda_pool_lock);
  6585. #ifdef DEBUG_CUDA_MALLOC
  6586. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  6587. #endif
  6588. g_cuda_pool_used[device] -= size;
  6589. // all deallocations must be in reverse order of the allocations
  6590. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  6591. }
  6592. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  6593. if (g_device_caps[device].vmm) {
  6594. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  6595. } else {
  6596. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  6597. }
  6598. }
  6599. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  6600. if (g_device_caps[device].vmm) {
  6601. ggml_cuda_pool_free_vmm(device, ptr, size);
  6602. } else {
  6603. ggml_cuda_pool_free_leg(device, ptr, size);
  6604. }
  6605. }
  6606. #else
  6607. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  6608. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  6609. #endif // !defined(GGML_USE_HIPBLAS)
  6610. template<typename T>
  6611. struct cuda_pool_alloc {
  6612. int device = -1;
  6613. T * ptr = nullptr;
  6614. size_t actual_size = 0;
  6615. // size is in number of elements
  6616. T * alloc(size_t size) {
  6617. GGML_ASSERT(ptr == nullptr);
  6618. CUDA_CHECK(cudaGetDevice(&device));
  6619. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  6620. return ptr;
  6621. }
  6622. cuda_pool_alloc(size_t size) {
  6623. alloc(size);
  6624. }
  6625. ~cuda_pool_alloc() {
  6626. if (ptr != nullptr) {
  6627. ggml_cuda_pool_free(device, ptr, actual_size);
  6628. }
  6629. }
  6630. T * get() {
  6631. return ptr;
  6632. }
  6633. cuda_pool_alloc() = default;
  6634. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  6635. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  6636. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  6637. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  6638. };
  6639. static bool g_cublas_loaded = false;
  6640. GGML_CALL bool ggml_cublas_loaded(void) {
  6641. return g_cublas_loaded;
  6642. }
  6643. GGML_CALL void ggml_init_cublas() {
  6644. static bool initialized = false;
  6645. if (!initialized) {
  6646. #ifdef __HIP_PLATFORM_AMD__
  6647. // Workaround for a rocBLAS bug when using multiple graphics cards:
  6648. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  6649. rocblas_initialize();
  6650. CUDA_CHECK(cudaDeviceSynchronize());
  6651. #endif
  6652. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  6653. initialized = true;
  6654. g_cublas_loaded = false;
  6655. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  6656. return;
  6657. }
  6658. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  6659. int64_t total_vram = 0;
  6660. #if defined(GGML_CUDA_FORCE_MMQ)
  6661. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  6662. #else
  6663. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  6664. #endif
  6665. #if defined(CUDA_USE_TENSOR_CORES)
  6666. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  6667. #else
  6668. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  6669. #endif
  6670. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  6671. for (int id = 0; id < g_device_count; ++id) {
  6672. int device_vmm = 0;
  6673. #if !defined(GGML_USE_HIPBLAS)
  6674. CUdevice device;
  6675. CU_CHECK(cuDeviceGet(&device, id));
  6676. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  6677. if (device_vmm) {
  6678. CUmemAllocationProp alloc_prop = {};
  6679. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6680. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6681. alloc_prop.location.id = id;
  6682. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  6683. }
  6684. #endif // !defined(GGML_USE_HIPBLAS)
  6685. g_device_caps[id].vmm = !!device_vmm;
  6686. cudaDeviceProp prop;
  6687. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  6688. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  6689. g_default_tensor_split[id] = total_vram;
  6690. total_vram += prop.totalGlobalMem;
  6691. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6692. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  6693. #else
  6694. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  6695. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6696. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  6697. }
  6698. for (int id = 0; id < g_device_count; ++id) {
  6699. g_default_tensor_split[id] /= total_vram;
  6700. }
  6701. for (int id = 0; id < g_device_count; ++id) {
  6702. ggml_cuda_set_device(id);
  6703. // create cuda streams
  6704. for (int is = 0; is < MAX_STREAMS; ++is) {
  6705. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  6706. }
  6707. // create cublas handle
  6708. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  6709. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  6710. }
  6711. // configure logging to stdout
  6712. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  6713. initialized = true;
  6714. g_cublas_loaded = true;
  6715. }
  6716. }
  6717. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  6718. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6719. return nullptr;
  6720. }
  6721. void * ptr = nullptr;
  6722. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6723. if (err != cudaSuccess) {
  6724. // clear the error
  6725. cudaGetLastError();
  6726. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6727. size/1024.0/1024.0, cudaGetErrorString(err));
  6728. return nullptr;
  6729. }
  6730. return ptr;
  6731. }
  6732. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  6733. CUDA_CHECK(cudaFreeHost(ptr));
  6734. }
  6735. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6736. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6737. cudaMemcpyKind kind;
  6738. char * src_ptr;
  6739. if (src->backend == GGML_BACKEND_CPU) {
  6740. kind = cudaMemcpyHostToDevice;
  6741. src_ptr = (char *) src->data;
  6742. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  6743. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6744. kind = cudaMemcpyDeviceToDevice;
  6745. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6746. int id;
  6747. CUDA_CHECK(cudaGetDevice(&id));
  6748. src_ptr = (char *) extra->data_device[id];
  6749. } else {
  6750. GGML_ASSERT(false);
  6751. }
  6752. char * dst_ptr = (char *) dst;
  6753. const int64_t ne0 = src->ne[0];
  6754. const int64_t nb0 = src->nb[0];
  6755. const int64_t nb1 = src->nb[1];
  6756. const int64_t nb2 = src->nb[2];
  6757. const int64_t nb3 = src->nb[3];
  6758. const enum ggml_type type = src->type;
  6759. const int64_t ts = ggml_type_size(type);
  6760. const int64_t bs = ggml_blck_size(type);
  6761. int64_t i1_diff = i1_high - i1_low;
  6762. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6763. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6764. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6765. } else if (nb0 == ts) {
  6766. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6767. } else {
  6768. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6769. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6770. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6771. // pretend the row is a matrix with cols=1
  6772. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6773. if (r != cudaSuccess) return r;
  6774. }
  6775. return cudaSuccess;
  6776. }
  6777. }
  6778. static void ggml_cuda_op_get_rows(
  6779. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6780. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6781. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6782. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6783. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6784. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6785. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6786. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6787. switch (src0->type) {
  6788. case GGML_TYPE_F16:
  6789. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6790. break;
  6791. case GGML_TYPE_F32:
  6792. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6793. break;
  6794. case GGML_TYPE_Q4_0:
  6795. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6796. break;
  6797. case GGML_TYPE_Q4_1:
  6798. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6799. break;
  6800. case GGML_TYPE_Q5_0:
  6801. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6802. break;
  6803. case GGML_TYPE_Q5_1:
  6804. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6805. break;
  6806. case GGML_TYPE_Q8_0:
  6807. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6808. break;
  6809. default:
  6810. // TODO: k-quants
  6811. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6812. GGML_ASSERT(false);
  6813. break;
  6814. }
  6815. }
  6816. template<class op>
  6817. static void ggml_cuda_op_bin_bcast(
  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(src1->type == GGML_TYPE_F32);
  6821. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6822. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6823. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6824. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6825. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6826. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6827. } else {
  6828. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6829. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6830. GGML_ASSERT(false);
  6831. }
  6832. }
  6833. static void ggml_cuda_op_repeat(
  6834. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6835. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6836. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6837. (void) src1;
  6838. (void) src1_d;
  6839. }
  6840. static void ggml_cuda_op_add(
  6841. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6842. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6843. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6844. }
  6845. static void ggml_cuda_op_acc(
  6846. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6847. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6848. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6849. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6850. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6851. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6852. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6853. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6854. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6855. int offset = dst->op_params[3] / 4; // offset in bytes
  6856. 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);
  6857. (void) dst;
  6858. }
  6859. static void ggml_cuda_op_mul(
  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_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6863. }
  6864. static void ggml_cuda_op_div(
  6865. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6866. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6867. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6868. }
  6869. static void ggml_cuda_op_gelu(
  6870. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6871. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6872. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6873. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6874. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6875. (void) src1;
  6876. (void) dst;
  6877. (void) src1_dd;
  6878. }
  6879. static void ggml_cuda_op_silu(
  6880. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6881. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6882. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6883. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6884. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6885. (void) src1;
  6886. (void) dst;
  6887. (void) src1_dd;
  6888. }
  6889. static void ggml_cuda_op_gelu_quick(
  6890. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6891. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6892. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6893. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6894. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6895. (void) src1;
  6896. (void) dst;
  6897. (void) src1_dd;
  6898. }
  6899. static void ggml_cuda_op_tanh(
  6900. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6901. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6902. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6903. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6904. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6905. (void) src1;
  6906. (void) dst;
  6907. (void) src1_dd;
  6908. }
  6909. static void ggml_cuda_op_relu(
  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. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6915. (void) src1;
  6916. (void) dst;
  6917. (void) src1_dd;
  6918. }
  6919. static void ggml_cuda_op_hardsigmoid(
  6920. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6921. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6922. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6923. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6924. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6925. (void) src1;
  6926. (void) dst;
  6927. (void) src1_dd;
  6928. }
  6929. static void ggml_cuda_op_hardswish(
  6930. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6931. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6932. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6933. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6934. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6935. (void) src1;
  6936. (void) dst;
  6937. (void) src1_dd;
  6938. }
  6939. static void ggml_cuda_op_leaky_relu(
  6940. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6941. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6942. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6943. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6944. float negative_slope;
  6945. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6946. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6947. (void) src1;
  6948. (void) dst;
  6949. (void) src1_dd;
  6950. }
  6951. static void ggml_cuda_op_sqr(
  6952. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6953. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6954. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6955. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6956. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6957. (void) src1;
  6958. (void) dst;
  6959. (void) src1_dd;
  6960. }
  6961. static void ggml_cuda_op_norm(
  6962. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6963. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6964. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6965. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6966. const int64_t ne00 = src0->ne[0];
  6967. const int64_t nrows = ggml_nrows(src0);
  6968. float eps;
  6969. memcpy(&eps, dst->op_params, sizeof(float));
  6970. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6971. (void) src1;
  6972. (void) dst;
  6973. (void) src1_dd;
  6974. }
  6975. static void ggml_cuda_op_group_norm(
  6976. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6977. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6978. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6979. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6980. int num_groups = dst->op_params[0];
  6981. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6982. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  6983. (void) src1;
  6984. (void) dst;
  6985. (void) src1_dd;
  6986. }
  6987. static void ggml_cuda_op_concat(
  6988. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6989. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6990. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6991. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6992. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6993. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6994. 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);
  6995. }
  6996. (void) src1;
  6997. (void) dst;
  6998. }
  6999. static void ggml_cuda_op_upscale(
  7000. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7001. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7002. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7003. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7004. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7005. const int scale_factor = dst->op_params[0];
  7006. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  7007. (void) src1;
  7008. (void) dst;
  7009. (void) src1_dd;
  7010. }
  7011. static void ggml_cuda_op_pad(
  7012. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7013. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7014. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7015. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7016. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  7017. pad_f32_cuda(src0_dd, dst_dd,
  7018. src0->ne[0], src0->ne[1], src0->ne[2],
  7019. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  7020. (void) src1;
  7021. (void) dst;
  7022. (void) src1_dd;
  7023. }
  7024. static void ggml_cuda_op_rms_norm(
  7025. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7026. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7027. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7028. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7029. const int64_t ne00 = src0->ne[0];
  7030. const int64_t nrows = ggml_nrows(src0);
  7031. float eps;
  7032. memcpy(&eps, dst->op_params, sizeof(float));
  7033. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  7034. (void) src1;
  7035. (void) dst;
  7036. (void) src1_dd;
  7037. }
  7038. static void ggml_cuda_op_mul_mat_q(
  7039. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7040. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7041. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7042. const int64_t ne00 = src0->ne[0];
  7043. const int64_t ne10 = src1->ne[0];
  7044. GGML_ASSERT(ne10 % QK8_1 == 0);
  7045. const int64_t ne0 = dst->ne[0];
  7046. const int64_t row_diff = row_high - row_low;
  7047. int id;
  7048. CUDA_CHECK(cudaGetDevice(&id));
  7049. // the main device has a larger memory buffer to hold the results from all GPUs
  7050. // nrows_dst == nrows of the matrix that the kernel writes into
  7051. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7052. switch (src0->type) {
  7053. case GGML_TYPE_Q4_0:
  7054. 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);
  7055. break;
  7056. case GGML_TYPE_Q4_1:
  7057. 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);
  7058. break;
  7059. case GGML_TYPE_Q5_0:
  7060. 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);
  7061. break;
  7062. case GGML_TYPE_Q5_1:
  7063. 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);
  7064. break;
  7065. case GGML_TYPE_Q8_0:
  7066. 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);
  7067. break;
  7068. case GGML_TYPE_Q2_K:
  7069. 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);
  7070. break;
  7071. case GGML_TYPE_Q3_K:
  7072. 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);
  7073. break;
  7074. case GGML_TYPE_Q4_K:
  7075. 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);
  7076. break;
  7077. case GGML_TYPE_Q5_K:
  7078. 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);
  7079. break;
  7080. case GGML_TYPE_Q6_K:
  7081. 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);
  7082. break;
  7083. default:
  7084. GGML_ASSERT(false);
  7085. break;
  7086. }
  7087. (void) src1;
  7088. (void) dst;
  7089. (void) src1_ddf_i;
  7090. }
  7091. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  7092. int64_t min_compute_capability = INT_MAX;
  7093. int64_t max_compute_capability = INT_MIN;
  7094. for (int id = 0; id < g_device_count; ++id) {
  7095. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  7096. if (min_compute_capability > g_device_caps[id].cc) {
  7097. min_compute_capability = g_device_caps[id].cc;
  7098. }
  7099. if (max_compute_capability < g_device_caps[id].cc) {
  7100. max_compute_capability = g_device_caps[id].cc;
  7101. }
  7102. }
  7103. }
  7104. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7105. switch(type) {
  7106. case GGML_TYPE_Q4_0:
  7107. case GGML_TYPE_Q4_1:
  7108. case GGML_TYPE_Q5_0:
  7109. case GGML_TYPE_Q5_1:
  7110. case GGML_TYPE_Q8_0:
  7111. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7112. case GGML_TYPE_F16:
  7113. case GGML_TYPE_F32:
  7114. return 1;
  7115. case GGML_TYPE_Q2_K:
  7116. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  7117. case GGML_TYPE_Q3_K:
  7118. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  7119. case GGML_TYPE_Q4_K:
  7120. case GGML_TYPE_Q5_K:
  7121. case GGML_TYPE_Q6_K:
  7122. case GGML_TYPE_IQ2_XXS:
  7123. case GGML_TYPE_IQ2_XS:
  7124. case GGML_TYPE_IQ3_XXS:
  7125. case GGML_TYPE_IQ1_S:
  7126. case GGML_TYPE_IQ4_NL:
  7127. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7128. default:
  7129. GGML_ASSERT(false);
  7130. }
  7131. #else
  7132. switch(type) {
  7133. case GGML_TYPE_Q4_0:
  7134. case GGML_TYPE_Q4_1:
  7135. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7136. case GGML_TYPE_Q5_0:
  7137. case GGML_TYPE_Q5_1:
  7138. case GGML_TYPE_Q8_0:
  7139. return 64;
  7140. case GGML_TYPE_F16:
  7141. case GGML_TYPE_F32:
  7142. return 1;
  7143. case GGML_TYPE_Q2_K:
  7144. case GGML_TYPE_Q3_K:
  7145. case GGML_TYPE_Q4_K:
  7146. case GGML_TYPE_Q5_K:
  7147. case GGML_TYPE_IQ2_XXS:
  7148. case GGML_TYPE_IQ2_XS:
  7149. case GGML_TYPE_IQ3_XXS:
  7150. case GGML_TYPE_IQ1_S:
  7151. case GGML_TYPE_IQ4_NL:
  7152. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7153. case GGML_TYPE_Q6_K:
  7154. return 64;
  7155. default:
  7156. GGML_ASSERT(false);
  7157. }
  7158. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7159. }
  7160. 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) {
  7161. const int64_t nrows = ggml_nrows(tensor);
  7162. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  7163. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  7164. *row_low -= *row_low % rounding;
  7165. if (id == g_device_count - 1) {
  7166. *row_high = nrows;
  7167. } else {
  7168. *row_high = nrows*tensor_split[id + 1];
  7169. *row_high -= *row_high % rounding;
  7170. }
  7171. }
  7172. static void ggml_cuda_op_mul_mat_vec_q(
  7173. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7174. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7175. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7176. const int64_t ne00 = src0->ne[0];
  7177. const int64_t row_diff = row_high - row_low;
  7178. const int64_t ne10 = src1->ne[0];
  7179. GGML_ASSERT(ne10 % QK8_1 == 0);
  7180. const int64_t ne0 = dst->ne[0];
  7181. int id;
  7182. CUDA_CHECK(cudaGetDevice(&id));
  7183. // the main device has a larger memory buffer to hold the results from all GPUs
  7184. // nrows_dst == nrows of the matrix that the kernel writes into
  7185. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7186. switch (src0->type) {
  7187. case GGML_TYPE_Q4_0:
  7188. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7189. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7190. break;
  7191. case GGML_TYPE_Q4_1:
  7192. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7193. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7194. break;
  7195. case GGML_TYPE_Q5_0:
  7196. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7197. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7198. break;
  7199. case GGML_TYPE_Q5_1:
  7200. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7201. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7202. break;
  7203. case GGML_TYPE_Q8_0:
  7204. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7205. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7206. break;
  7207. case GGML_TYPE_Q2_K:
  7208. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7209. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7210. break;
  7211. case GGML_TYPE_Q3_K:
  7212. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7213. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7214. break;
  7215. case GGML_TYPE_Q4_K:
  7216. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7217. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7218. break;
  7219. case GGML_TYPE_Q5_K:
  7220. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7221. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7222. break;
  7223. case GGML_TYPE_Q6_K:
  7224. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7225. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7226. break;
  7227. case GGML_TYPE_IQ2_XXS:
  7228. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7229. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7230. break;
  7231. case GGML_TYPE_IQ2_XS:
  7232. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7233. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7234. break;
  7235. case GGML_TYPE_IQ3_XXS:
  7236. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7237. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7238. break;
  7239. case GGML_TYPE_IQ1_S:
  7240. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7241. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7242. break;
  7243. case GGML_TYPE_IQ4_NL:
  7244. mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
  7245. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7246. break;
  7247. default:
  7248. GGML_ASSERT(false);
  7249. break;
  7250. }
  7251. (void) src1;
  7252. (void) dst;
  7253. (void) src1_ddf_i;
  7254. (void) src1_ncols;
  7255. (void) src1_padded_row_size;
  7256. }
  7257. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7258. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7259. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7260. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7261. const int64_t ne00 = src0->ne[0];
  7262. const int64_t row_diff = row_high - row_low;
  7263. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7264. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7265. #ifdef GGML_CUDA_F16
  7266. cuda_pool_alloc<half> src1_dfloat_a;
  7267. half * src1_dfloat = nullptr; // dfloat == half
  7268. bool src1_convert_f16 =
  7269. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7270. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7271. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7272. if (src1_convert_f16) {
  7273. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7274. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7275. GGML_ASSERT(to_fp16_cuda != nullptr);
  7276. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7277. }
  7278. #else
  7279. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7280. #endif // GGML_CUDA_F16
  7281. switch (src0->type) {
  7282. case GGML_TYPE_Q4_0:
  7283. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7284. break;
  7285. case GGML_TYPE_Q4_1:
  7286. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7287. break;
  7288. case GGML_TYPE_Q5_0:
  7289. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7290. break;
  7291. case GGML_TYPE_Q5_1:
  7292. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7293. break;
  7294. case GGML_TYPE_Q8_0:
  7295. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7296. break;
  7297. case GGML_TYPE_Q2_K:
  7298. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7299. break;
  7300. case GGML_TYPE_Q3_K:
  7301. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7302. break;
  7303. case GGML_TYPE_Q4_K:
  7304. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7305. break;
  7306. case GGML_TYPE_Q5_K:
  7307. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7308. break;
  7309. case GGML_TYPE_Q6_K:
  7310. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7311. break;
  7312. case GGML_TYPE_F16:
  7313. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7314. break;
  7315. default:
  7316. GGML_ASSERT(false);
  7317. break;
  7318. }
  7319. (void) src1;
  7320. (void) dst;
  7321. (void) src1_ddq_i;
  7322. (void) src1_ncols;
  7323. (void) src1_padded_row_size;
  7324. }
  7325. static void ggml_cuda_op_mul_mat_cublas(
  7326. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7327. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7328. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7329. GGML_ASSERT(src0_dd_i != nullptr);
  7330. GGML_ASSERT(src1_ddf_i != nullptr);
  7331. GGML_ASSERT(dst_dd_i != nullptr);
  7332. const int64_t ne00 = src0->ne[0];
  7333. const int64_t ne10 = src1->ne[0];
  7334. const int64_t ne0 = dst->ne[0];
  7335. const int64_t row_diff = row_high - row_low;
  7336. int id;
  7337. CUDA_CHECK(cudaGetDevice(&id));
  7338. // the main device has a larger memory buffer to hold the results from all GPUs
  7339. // ldc == nrows of the matrix that cuBLAS writes into
  7340. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7341. const int compute_capability = g_device_caps[id].cc;
  7342. 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) {
  7343. //printf("this branch\n");
  7344. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7345. cuda_pool_alloc<half> src0_as_f16;
  7346. if (src0->type != GGML_TYPE_F16) {
  7347. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7348. GGML_ASSERT(to_fp16_cuda != nullptr);
  7349. size_t ne = row_diff*ne00;
  7350. src0_as_f16.alloc(ne);
  7351. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7352. }
  7353. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7354. cuda_pool_alloc<half> src1_as_f16;
  7355. if (src1->type != GGML_TYPE_F16) {
  7356. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7357. GGML_ASSERT(to_fp16_cuda != nullptr);
  7358. size_t ne = src1_ncols*ne10;
  7359. src1_as_f16.alloc(ne);
  7360. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7361. }
  7362. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7363. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7364. const half alpha_f16 = 1.0f;
  7365. const half beta_f16 = 0.0f;
  7366. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7367. CUBLAS_CHECK(
  7368. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7369. row_diff, src1_ncols, ne10,
  7370. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7371. src1_ptr, CUDA_R_16F, ne10,
  7372. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7373. CUBLAS_COMPUTE_16F,
  7374. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7375. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7376. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7377. } else {
  7378. cuda_pool_alloc<float> src0_ddq_as_f32;
  7379. cuda_pool_alloc<float> src1_ddq_as_f32;
  7380. if (src0->type != GGML_TYPE_F32) {
  7381. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7382. GGML_ASSERT(to_fp32_cuda != nullptr);
  7383. src0_ddq_as_f32.alloc(row_diff*ne00);
  7384. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7385. }
  7386. if (src1->type != GGML_TYPE_F32) {
  7387. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7388. GGML_ASSERT(to_fp32_cuda != nullptr);
  7389. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7390. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7391. }
  7392. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7393. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7394. const float alpha = 1.0f;
  7395. const float beta = 0.0f;
  7396. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7397. CUBLAS_CHECK(
  7398. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7399. row_diff, src1_ncols, ne10,
  7400. &alpha, src0_ddf_i, ne00,
  7401. src1_ddf1_i, ne10,
  7402. &beta, dst_dd_i, ldc));
  7403. }
  7404. (void) dst;
  7405. (void) src1_ddq_i;
  7406. (void) src1_padded_row_size;
  7407. }
  7408. static void ggml_cuda_op_rope(
  7409. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7410. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7411. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7412. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7413. GGML_ASSERT(src0->type == dst->type);
  7414. const int64_t ne00 = src0->ne[0];
  7415. const int64_t ne01 = src0->ne[1];
  7416. const int64_t ne2 = dst->ne[2];
  7417. const int64_t nrows = ggml_nrows(src0);
  7418. //const int n_past = ((int32_t *) dst->op_params)[0];
  7419. const int n_dims = ((int32_t *) dst->op_params)[1];
  7420. const int mode = ((int32_t *) dst->op_params)[2];
  7421. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7422. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7423. // RoPE alteration for extended context
  7424. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7425. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7426. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7427. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7428. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7429. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7430. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7431. const int32_t * pos = nullptr;
  7432. if ((mode & 1) == 0) {
  7433. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7434. GGML_ASSERT(src1->ne[0] == ne2);
  7435. pos = (const int32_t *) src1_dd;
  7436. }
  7437. const bool is_neox = mode & 2;
  7438. const bool is_glm = mode & 4;
  7439. rope_corr_dims corr_dims;
  7440. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7441. // compute
  7442. if (is_glm) {
  7443. GGML_ASSERT(false);
  7444. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7445. } else if (is_neox) {
  7446. if (src0->type == GGML_TYPE_F32) {
  7447. rope_neox_cuda(
  7448. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7449. attn_factor, corr_dims, main_stream
  7450. );
  7451. } else if (src0->type == GGML_TYPE_F16) {
  7452. rope_neox_cuda(
  7453. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7454. attn_factor, corr_dims, main_stream
  7455. );
  7456. } else {
  7457. GGML_ASSERT(false);
  7458. }
  7459. } else {
  7460. if (src0->type == GGML_TYPE_F32) {
  7461. rope_cuda(
  7462. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7463. attn_factor, corr_dims, main_stream
  7464. );
  7465. } else if (src0->type == GGML_TYPE_F16) {
  7466. rope_cuda(
  7467. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7468. attn_factor, corr_dims, main_stream
  7469. );
  7470. } else {
  7471. GGML_ASSERT(false);
  7472. }
  7473. }
  7474. (void) src1;
  7475. (void) dst;
  7476. (void) src1_dd;
  7477. }
  7478. static void ggml_cuda_op_alibi(
  7479. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7480. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7481. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7482. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7483. const int64_t ne00 = src0->ne[0];
  7484. const int64_t ne01 = src0->ne[1];
  7485. const int64_t ne02 = src0->ne[2];
  7486. const int64_t nrows = ggml_nrows(src0);
  7487. //const int n_past = ((int32_t *) dst->op_params)[0];
  7488. const int n_head = ((int32_t *) dst->op_params)[1];
  7489. float max_bias;
  7490. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7491. //GGML_ASSERT(ne01 + n_past == ne00);
  7492. GGML_ASSERT(n_head == ne02);
  7493. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7494. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7495. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7496. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7497. (void) src1;
  7498. (void) src1_dd;
  7499. }
  7500. static void ggml_cuda_op_pool2d(
  7501. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7502. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7503. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7504. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7505. const int32_t * opts = (const int32_t *)dst->op_params;
  7506. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  7507. const int k0 = opts[1];
  7508. const int k1 = opts[2];
  7509. const int s0 = opts[3];
  7510. const int s1 = opts[4];
  7511. const int p0 = opts[5];
  7512. const int p1 = opts[6];
  7513. const int64_t IH = src0->ne[1];
  7514. const int64_t IW = src0->ne[0];
  7515. const int64_t N = dst->ne[3];
  7516. const int64_t OC = dst->ne[2];
  7517. const int64_t OH = dst->ne[1];
  7518. const int64_t OW = dst->ne[0];
  7519. const int parallel_elements = N * OC * OH * OW;
  7520. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  7521. dim3 block_nums(num_blocks);
  7522. 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);
  7523. (void) src1;
  7524. (void) src1_dd;
  7525. }
  7526. static void ggml_cuda_op_im2col(
  7527. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7528. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7529. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7530. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7531. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  7532. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7533. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  7534. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  7535. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  7536. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  7537. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  7538. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  7539. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  7540. const int64_t IH = is_2D ? src1->ne[1] : 1;
  7541. const int64_t IW = src1->ne[0];
  7542. const int64_t KH = is_2D ? src0->ne[1] : 1;
  7543. const int64_t KW = src0->ne[0];
  7544. const int64_t OH = is_2D ? dst->ne[2] : 1;
  7545. const int64_t OW = dst->ne[1];
  7546. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7547. const int64_t batch = src1->ne[3];
  7548. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  7549. if(dst->type == GGML_TYPE_F16) {
  7550. 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);
  7551. } else {
  7552. 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);
  7553. }
  7554. (void) src0;
  7555. (void) src0_dd;
  7556. }
  7557. static void ggml_cuda_op_sum_rows(
  7558. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7559. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7560. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7561. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7562. const int64_t ncols = src0->ne[0];
  7563. const int64_t nrows = ggml_nrows(src0);
  7564. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  7565. (void) src1;
  7566. (void) dst;
  7567. (void) src1_dd;
  7568. }
  7569. static void ggml_cuda_op_argsort(
  7570. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7571. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7572. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7573. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  7574. const int64_t ncols = src0->ne[0];
  7575. const int64_t nrows = ggml_nrows(src0);
  7576. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  7577. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  7578. (void) src1;
  7579. (void) dst;
  7580. (void) src1_dd;
  7581. }
  7582. static void ggml_cuda_op_diag_mask_inf(
  7583. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7584. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7585. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7586. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7587. const int64_t ne00 = src0->ne[0];
  7588. const int64_t ne01 = src0->ne[1];
  7589. const int nrows0 = ggml_nrows(src0);
  7590. const int n_past = ((int32_t *) dst->op_params)[0];
  7591. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  7592. (void) src1;
  7593. (void) dst;
  7594. (void) src1_dd;
  7595. }
  7596. static void ggml_cuda_op_soft_max(
  7597. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7598. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7599. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7600. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7601. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  7602. const int64_t ne00 = src0->ne[0];
  7603. const int64_t nrows_x = ggml_nrows(src0);
  7604. const int64_t nrows_y = src0->ne[1];
  7605. float scale = 1.0f;
  7606. float max_bias = 0.0f;
  7607. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7608. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7609. // positions tensor
  7610. float * src2_dd = nullptr;
  7611. cuda_pool_alloc<float> src2_f;
  7612. ggml_tensor * src2 = dst->src[2];
  7613. const bool use_src2 = src2 != nullptr;
  7614. if (use_src2) {
  7615. const bool src2_on_device = src2->backend == GGML_BACKEND_GPU;
  7616. if (src2_on_device) {
  7617. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  7618. src2_dd = (float *) src2_extra->data_device[g_main_device];
  7619. } else {
  7620. src2_dd = src2_f.alloc(ggml_nelements(src2));
  7621. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  7622. }
  7623. }
  7624. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  7625. }
  7626. static void ggml_cuda_op_scale(
  7627. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7628. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7629. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7630. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7631. float scale;
  7632. memcpy(&scale, dst->op_params, sizeof(float));
  7633. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  7634. CUDA_CHECK(cudaGetLastError());
  7635. (void) src1;
  7636. (void) dst;
  7637. (void) src1_dd;
  7638. }
  7639. static void ggml_cuda_op_clamp(
  7640. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7641. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7642. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7643. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7644. float min;
  7645. float max;
  7646. memcpy(&min, dst->op_params, sizeof(float));
  7647. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7648. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  7649. CUDA_CHECK(cudaGetLastError());
  7650. (void) src1;
  7651. (void) dst;
  7652. (void) src1_dd;
  7653. }
  7654. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  7655. const int64_t nrows0 = ggml_nrows(src0);
  7656. const bool use_src1 = src1 != nullptr;
  7657. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  7658. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  7659. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  7660. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7661. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  7662. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7663. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7664. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  7665. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  7666. // dd = data device
  7667. float * src0_ddf = nullptr;
  7668. float * src1_ddf = nullptr;
  7669. float * dst_ddf = nullptr;
  7670. cuda_pool_alloc<float> src0_f;
  7671. cuda_pool_alloc<float> src1_f;
  7672. cuda_pool_alloc<float> dst_f;
  7673. ggml_cuda_set_device(g_main_device);
  7674. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7675. if (src0_on_device) {
  7676. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  7677. } else {
  7678. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  7679. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  7680. }
  7681. if (use_src1) {
  7682. if (src1_on_device) {
  7683. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7684. } else {
  7685. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  7686. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  7687. }
  7688. }
  7689. if (dst_on_device) {
  7690. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7691. } else {
  7692. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7693. }
  7694. // do the computation
  7695. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7696. CUDA_CHECK(cudaGetLastError());
  7697. // copy dst to host if necessary
  7698. if (!dst_on_device) {
  7699. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7700. }
  7701. if (dst->backend == GGML_BACKEND_CPU) {
  7702. CUDA_CHECK(cudaDeviceSynchronize());
  7703. }
  7704. }
  7705. static void ggml_cuda_set_peer_access(const int n_tokens) {
  7706. static bool peer_access_enabled = false;
  7707. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  7708. if (peer_access_enabled == enable_peer_access) {
  7709. return;
  7710. }
  7711. #ifdef NDEBUG
  7712. for (int id = 0; id < g_device_count; ++id) {
  7713. ggml_cuda_set_device(id);
  7714. CUDA_CHECK(cudaDeviceSynchronize());
  7715. }
  7716. for (int id = 0; id < g_device_count; ++id) {
  7717. ggml_cuda_set_device(id);
  7718. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  7719. if (id == id_other) {
  7720. continue;
  7721. }
  7722. if (id != g_main_device && id_other != g_main_device) {
  7723. continue;
  7724. }
  7725. int can_access_peer;
  7726. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  7727. if (can_access_peer) {
  7728. if (enable_peer_access) {
  7729. cudaError_t err = cudaDeviceEnablePeerAccess(id_other, 0);
  7730. if (err != cudaErrorPeerAccessAlreadyEnabled) {
  7731. CUDA_CHECK(err);
  7732. }
  7733. } else {
  7734. cudaError_t err = cudaDeviceDisablePeerAccess(id_other);
  7735. if (err != cudaErrorPeerAccessNotEnabled) {
  7736. CUDA_CHECK(err);
  7737. }
  7738. }
  7739. }
  7740. }
  7741. }
  7742. #endif // NDEBUG
  7743. peer_access_enabled = enable_peer_access;
  7744. }
  7745. // FIXME: move this somewhere else
  7746. struct ggml_backend_cuda_split_buffer_type_context {
  7747. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7748. };
  7749. static void ggml_cuda_op_mul_mat(
  7750. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  7751. const bool convert_src1_to_q8_1) {
  7752. const int64_t ne00 = src0->ne[0];
  7753. const int64_t ne01 = src0->ne[1];
  7754. const int64_t ne02 = src0->ne[2];
  7755. const int64_t ne03 = src0->ne[3];
  7756. const int64_t ne10 = src1->ne[0];
  7757. const int64_t ne11 = src1->ne[1];
  7758. const int64_t ne12 = src1->ne[2];
  7759. const int64_t ne13 = src1->ne[3];
  7760. const int64_t nrows1 = ggml_nrows(src1);
  7761. GGML_ASSERT(ne03 == ne13);
  7762. const int64_t ne0 = dst->ne[0];
  7763. const int64_t ne1 = dst->ne[1];
  7764. const int nb2 = dst->nb[2];
  7765. const int nb3 = dst->nb[3];
  7766. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  7767. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  7768. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  7769. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  7770. const int64_t i02_divisor = ne12 / ne02;
  7771. const size_t src0_ts = ggml_type_size(src0->type);
  7772. const size_t src0_bs = ggml_blck_size(src0->type);
  7773. const size_t q8_1_ts = sizeof(block_q8_1);
  7774. const size_t q8_1_bs = QK8_1;
  7775. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7776. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7777. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7778. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7779. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  7780. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  7781. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  7782. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  7783. GGML_ASSERT(!(split && ne02 > 1));
  7784. GGML_ASSERT(!(split && ne03 > 1));
  7785. GGML_ASSERT(!(split && ne02 < ne12));
  7786. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7787. if (split) {
  7788. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_GPU_SPLIT check
  7789. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  7790. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  7791. tensor_split = buft_ctx->tensor_split;
  7792. }
  7793. struct dev_data {
  7794. cuda_pool_alloc<char> src0_dd_alloc;
  7795. cuda_pool_alloc<float> src1_ddf_alloc;
  7796. cuda_pool_alloc<char> src1_ddq_alloc;
  7797. cuda_pool_alloc<float> dst_dd_alloc;
  7798. char * src0_dd = nullptr;
  7799. float * src1_ddf = nullptr; // float
  7800. char * src1_ddq = nullptr; // q8_1
  7801. float * dst_dd = nullptr;
  7802. int64_t row_low;
  7803. int64_t row_high;
  7804. };
  7805. dev_data dev[GGML_CUDA_MAX_DEVICES];
  7806. int used_devices = 0;
  7807. for (int id = 0; id < g_device_count; ++id) {
  7808. // by default, use all rows
  7809. dev[id].row_low = 0;
  7810. dev[id].row_high = ne01;
  7811. // for multi GPU, get the row boundaries from tensor split
  7812. // and round to mul_mat_q tile sizes
  7813. if (split) {
  7814. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  7815. if (id != 0) {
  7816. dev[id].row_low = ne01*tensor_split[id];
  7817. if (dev[id].row_low < ne01) {
  7818. dev[id].row_low -= dev[id].row_low % rounding;
  7819. }
  7820. }
  7821. if (id != g_device_count - 1) {
  7822. dev[id].row_high = ne01*tensor_split[id + 1];
  7823. if (dev[id].row_high < ne01) {
  7824. dev[id].row_high -= dev[id].row_high % rounding;
  7825. }
  7826. }
  7827. }
  7828. }
  7829. for (int id = 0; id < g_device_count; ++id) {
  7830. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7831. continue;
  7832. }
  7833. used_devices++;
  7834. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7835. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7836. ggml_cuda_set_device(id);
  7837. cudaStream_t stream = g_cudaStreams[id][0];
  7838. if (src0_on_device && src0_is_contiguous) {
  7839. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  7840. } else {
  7841. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  7842. }
  7843. if (src1_on_device && src1_is_contiguous) {
  7844. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7845. } else {
  7846. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7847. }
  7848. if (convert_src1_to_q8_1) {
  7849. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7850. if (src1_on_device && src1_is_contiguous) {
  7851. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7852. CUDA_CHECK(cudaGetLastError());
  7853. }
  7854. }
  7855. if (dst_on_device) {
  7856. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7857. } else {
  7858. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7859. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7860. }
  7861. }
  7862. // if multiple devices are used they need to wait for the main device
  7863. // here an event is recorded that signals that the main device has finished calculating the input data
  7864. if (split && used_devices > 1) {
  7865. ggml_cuda_set_device(g_main_device);
  7866. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7867. }
  7868. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7869. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7870. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7871. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7872. for (int id = 0; id < g_device_count; ++id) {
  7873. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7874. continue;
  7875. }
  7876. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7877. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7878. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7879. ggml_cuda_set_device(id);
  7880. cudaStream_t stream = g_cudaStreams[id][is];
  7881. // wait for main GPU data if necessary
  7882. if (split && (id != g_main_device || is != 0)) {
  7883. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7884. }
  7885. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7886. const int64_t i03 = i0 / ne12;
  7887. const int64_t i02 = i0 % ne12;
  7888. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7889. // for split tensors the data begins at i0 == i0_offset_low
  7890. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7891. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7892. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7893. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7894. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7895. // in that case an offset on dst_ddf_i is needed
  7896. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  7897. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7898. }
  7899. // copy src0, src1 to device if necessary
  7900. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  7901. if (id != g_main_device) {
  7902. if (convert_src1_to_q8_1) {
  7903. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7904. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7905. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7906. } else {
  7907. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7908. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7909. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7910. src1_ncols*ne10*sizeof(float), stream));
  7911. }
  7912. }
  7913. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  7914. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7915. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7916. } else {
  7917. GGML_ASSERT(false);
  7918. }
  7919. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  7920. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7921. CUDA_CHECK(cudaGetLastError());
  7922. }
  7923. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7924. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7925. }
  7926. // do the computation
  7927. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7928. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7929. CUDA_CHECK(cudaGetLastError());
  7930. // copy dst to host or other device if necessary
  7931. if (!dst_on_device) {
  7932. void * dst_off_device;
  7933. cudaMemcpyKind kind;
  7934. if (dst->backend == GGML_BACKEND_CPU) {
  7935. dst_off_device = dst->data;
  7936. kind = cudaMemcpyDeviceToHost;
  7937. } else if (dst->backend == GGML_BACKEND_GPU) {
  7938. dst_off_device = dst_extra->data_device[g_main_device];
  7939. kind = cudaMemcpyDeviceToDevice;
  7940. } else {
  7941. GGML_ASSERT(false);
  7942. }
  7943. if (split) {
  7944. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7945. // dst is NOT transposed.
  7946. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7947. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7948. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7949. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7950. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7951. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7952. #if !defined(GGML_USE_HIPBLAS)
  7953. if (kind == cudaMemcpyDeviceToDevice) {
  7954. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7955. cudaMemcpy3DPeerParms p = {};
  7956. p.dstDevice = g_main_device;
  7957. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7958. p.srcDevice = id;
  7959. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7960. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7961. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7962. } else
  7963. #endif
  7964. {
  7965. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7966. dst_dd_i, row_diff*sizeof(float),
  7967. row_diff*sizeof(float), src1_ncols,
  7968. kind, stream));
  7969. }
  7970. } else {
  7971. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7972. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7973. dhf_dst_i += src1_col_0*ne0;
  7974. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7975. }
  7976. }
  7977. // add event for the main device to wait on until other device is done
  7978. if (split && (id != g_main_device || is != 0)) {
  7979. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7980. }
  7981. }
  7982. }
  7983. }
  7984. // main device waits for all other devices to be finished
  7985. if (split && g_device_count > 1) {
  7986. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7987. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7988. ggml_cuda_set_device(g_main_device);
  7989. for (int id = 0; id < g_device_count; ++id) {
  7990. if (dev[id].row_low == dev[id].row_high) {
  7991. continue;
  7992. }
  7993. for (int64_t is = 0; is < is_max; ++is) {
  7994. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7995. }
  7996. }
  7997. }
  7998. if (dst->backend == GGML_BACKEND_CPU) {
  7999. ggml_cuda_set_device(g_main_device);
  8000. CUDA_CHECK(cudaDeviceSynchronize());
  8001. }
  8002. }
  8003. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8004. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  8005. }
  8006. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8007. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  8008. }
  8009. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8010. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  8011. }
  8012. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8013. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  8014. }
  8015. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8016. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  8017. }
  8018. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8019. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  8020. }
  8021. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8022. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  8023. }
  8024. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8025. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  8026. }
  8027. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8028. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  8029. }
  8030. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8031. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  8032. }
  8033. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8034. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  8035. }
  8036. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8037. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  8038. }
  8039. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8040. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  8041. }
  8042. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8043. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  8044. }
  8045. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8046. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  8047. }
  8048. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8049. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  8050. }
  8051. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8052. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  8053. }
  8054. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8055. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  8056. }
  8057. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8058. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  8059. }
  8060. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8061. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  8062. }
  8063. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8064. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  8065. }
  8066. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  8067. if (!g_cublas_loaded) return false;
  8068. const int64_t ne10 = src1->ne[0];
  8069. const int64_t ne0 = dst->ne[0];
  8070. const int64_t ne1 = dst->ne[1];
  8071. // TODO: find the optimal values for these
  8072. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  8073. src1->type == GGML_TYPE_F32 &&
  8074. dst->type == GGML_TYPE_F32 &&
  8075. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  8076. }
  8077. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8078. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  8079. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8080. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  8081. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  8082. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8083. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8084. const int64_t ne00 = src0->ne[0];
  8085. const int64_t ne01 = src0->ne[1];
  8086. const int64_t ne02 = src0->ne[2];
  8087. const int64_t ne12 = src1->ne[2];
  8088. ggml_cuda_set_device(g_main_device);
  8089. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8090. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8091. void * src0_ddq = src0_extra->data_device[g_main_device];
  8092. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8093. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8094. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8095. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8096. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8097. }
  8098. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8099. GGML_ASSERT(!ggml_is_transposed(src0));
  8100. GGML_ASSERT(!ggml_is_transposed(src1));
  8101. GGML_ASSERT(!ggml_is_permuted(src0));
  8102. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8103. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8104. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8105. const int64_t ne00 = src0->ne[0];
  8106. const int64_t ne01 = src0->ne[1];
  8107. const int64_t ne02 = src0->ne[2];
  8108. const int64_t nb01 = src0->nb[1];
  8109. const int64_t nb02 = src0->nb[2];
  8110. const int64_t ne12 = src1->ne[2];
  8111. ggml_cuda_set_device(g_main_device);
  8112. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8113. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8114. void * src0_ddq = src0_extra->data_device[g_main_device];
  8115. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8116. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8117. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8118. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8119. const int64_t row_stride_x = nb01 / sizeof(half);
  8120. const int64_t channel_stride_x = nb02 / sizeof(half);
  8121. 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);
  8122. }
  8123. static __global__ void k_compute_batched_ptrs(
  8124. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8125. const void ** ptrs_src, void ** ptrs_dst,
  8126. int64_t ne12, int64_t ne13,
  8127. int64_t ne23,
  8128. size_t nb02, size_t nb03,
  8129. size_t nb12, size_t nb13,
  8130. size_t nbd2, size_t nbd3,
  8131. int64_t r2, int64_t r3) {
  8132. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8133. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8134. if (i13 >= ne13 || i12 >= ne12) {
  8135. return;
  8136. }
  8137. int64_t i03 = i13 / r3;
  8138. int64_t i02 = i12 / r2;
  8139. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8140. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8141. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8142. }
  8143. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8144. GGML_ASSERT(!ggml_is_transposed(src0));
  8145. GGML_ASSERT(!ggml_is_transposed(src1));
  8146. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8147. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8148. GGML_TENSOR_BINARY_OP_LOCALS
  8149. const int64_t ne_dst = ggml_nelements(dst);
  8150. ggml_cuda_set_device(g_main_device);
  8151. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8152. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8153. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8154. void * src0_ddq = src0_extra->data_device[g_main_device];
  8155. half * src0_f16 = (half *) src0_ddq;
  8156. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8157. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8158. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8159. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8160. // convert src1 to fp16
  8161. cuda_pool_alloc<half> src1_f16_alloc;
  8162. if (src1->type != GGML_TYPE_F16) {
  8163. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8164. const int64_t ne_src1 = ggml_nelements(src1);
  8165. src1_f16_alloc.alloc(ne_src1);
  8166. GGML_ASSERT(to_fp16_cuda != nullptr);
  8167. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8168. }
  8169. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8170. cuda_pool_alloc<half> dst_f16;
  8171. char * dst_t;
  8172. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8173. cudaDataType_t cu_data_type = CUDA_R_16F;
  8174. // dst strides
  8175. size_t nbd2 = dst->nb[2];
  8176. size_t nbd3 = dst->nb[3];
  8177. const half alpha_f16 = 1.0f;
  8178. const half beta_f16 = 0.0f;
  8179. const float alpha_f32 = 1.0f;
  8180. const float beta_f32 = 0.0f;
  8181. const void * alpha = &alpha_f16;
  8182. const void * beta = &beta_f16;
  8183. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8184. dst_t = (char *) dst_f16.alloc(ne_dst);
  8185. nbd2 /= sizeof(float) / sizeof(half);
  8186. nbd3 /= sizeof(float) / sizeof(half);
  8187. } else {
  8188. dst_t = (char *) dst_ddf;
  8189. cu_compute_type = CUBLAS_COMPUTE_32F;
  8190. cu_data_type = CUDA_R_32F;
  8191. alpha = &alpha_f32;
  8192. beta = &beta_f32;
  8193. }
  8194. GGML_ASSERT(ne12 % ne02 == 0);
  8195. GGML_ASSERT(ne13 % ne03 == 0);
  8196. // broadcast factors
  8197. const int64_t r2 = ne12/ne02;
  8198. const int64_t r3 = ne13/ne03;
  8199. #if 0
  8200. // use cublasGemmEx
  8201. {
  8202. for (int i13 = 0; i13 < ne13; ++i13) {
  8203. for (int i12 = 0; i12 < ne12; ++i12) {
  8204. int i03 = i13 / r3;
  8205. int i02 = i12 / r2;
  8206. CUBLAS_CHECK(
  8207. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8208. ne01, ne11, ne10,
  8209. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8210. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8211. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8212. cu_compute_type,
  8213. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8214. }
  8215. }
  8216. }
  8217. #else
  8218. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8219. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8220. // use cublasGemmStridedBatchedEx
  8221. CUBLAS_CHECK(
  8222. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8223. ne01, ne11, ne10,
  8224. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8225. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8226. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8227. ne12*ne13,
  8228. cu_compute_type,
  8229. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8230. } else {
  8231. // use cublasGemmBatchedEx
  8232. const int ne23 = ne12*ne13;
  8233. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8234. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8235. dim3 block_dims(ne13, ne12);
  8236. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8237. src0_f16, src1_f16, dst_t,
  8238. ptrs_src.get(), ptrs_dst.get(),
  8239. ne12, ne13,
  8240. ne23,
  8241. nb02, nb03,
  8242. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8243. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8244. nbd2, nbd3,
  8245. r2, r3);
  8246. CUDA_CHECK(cudaGetLastError());
  8247. CUBLAS_CHECK(
  8248. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8249. ne01, ne11, ne10,
  8250. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8251. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8252. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8253. ne23,
  8254. cu_compute_type,
  8255. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8256. }
  8257. #endif
  8258. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8259. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8260. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8261. }
  8262. }
  8263. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8264. const bool all_on_device =
  8265. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  8266. (src1->backend == GGML_BACKEND_GPU) &&
  8267. ( dst->backend == GGML_BACKEND_GPU);
  8268. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  8269. int64_t min_compute_capability = INT_MAX;
  8270. bool any_pascal_with_slow_fp16 = false;
  8271. if (split) {
  8272. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8273. auto & tensor_split = buft_ctx->tensor_split;
  8274. for (int id = 0; id < g_device_count; ++id) {
  8275. // skip devices that are not going to do any work:
  8276. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8277. continue;
  8278. }
  8279. if (min_compute_capability > g_device_caps[id].cc) {
  8280. min_compute_capability = g_device_caps[id].cc;
  8281. }
  8282. if (g_device_caps[id].cc == 610) {
  8283. any_pascal_with_slow_fp16 = true;
  8284. }
  8285. }
  8286. } else {
  8287. min_compute_capability = g_device_caps[g_main_device].cc;
  8288. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8289. }
  8290. // check data types and tensor shapes for custom matrix multiplication kernels:
  8291. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8292. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8293. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8294. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8295. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8296. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8297. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8298. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8299. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8300. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8301. #ifdef CUDA_USE_TENSOR_CORES
  8302. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8303. #endif // CUDA_USE_TENSOR_CORES
  8304. #else
  8305. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8306. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8307. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8308. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8309. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8310. #ifdef CUDA_USE_TENSOR_CORES
  8311. // when tensor cores are available, use them for large batch size
  8312. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8313. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8314. #endif // CUDA_USE_TENSOR_CORES
  8315. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8316. // if mmvq is available it's a better choice than dmmv:
  8317. #ifndef GGML_CUDA_FORCE_DMMV
  8318. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8319. #endif // GGML_CUDA_FORCE_DMMV
  8320. // debug helpers
  8321. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8322. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8323. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8324. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8325. //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);
  8326. //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);
  8327. 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) {
  8328. // KQ single-batch
  8329. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8330. } 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) {
  8331. // KQV single-batch
  8332. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8333. } 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) {
  8334. // KQ + KQV multi-batch
  8335. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8336. } else if (use_dequantize_mul_mat_vec) {
  8337. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8338. } else if (use_mul_mat_vec_q) {
  8339. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8340. } else if (use_mul_mat_q) {
  8341. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8342. } else {
  8343. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8344. }
  8345. }
  8346. #if 0
  8347. template<typename ... Srcs>
  8348. static __global__ void k_compute_batched_ptrs_id(
  8349. const void ** ptrs_src, void ** ptrs_dst,
  8350. int ne12, int ne13,
  8351. int ne23,
  8352. int nb02, int nb03,
  8353. int nb12, int nb13,
  8354. int nb2, int nb3,
  8355. int r2, int r3,
  8356. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8357. const half * src1_f16, half * dst_f16,
  8358. const int32_t * ids, const int id,
  8359. Srcs... src0s) {
  8360. int i = ids[id];
  8361. half * src0_f16;
  8362. const void * srcs_ar[] = { (const half *) src0s... };
  8363. if (src0_type == GGML_TYPE_F16) {
  8364. src0_f16 = (half *) srcs_ar[i];
  8365. } else {
  8366. src0_f16 = src0_as_f16;
  8367. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8368. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8369. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8370. }
  8371. }
  8372. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8373. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8374. if (i13 >= ne13 || i12 >= ne12) {
  8375. return;
  8376. }
  8377. int i03 = i13 / r3;
  8378. int i02 = i12 / r2;
  8379. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8380. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8381. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8382. }
  8383. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8384. const struct ggml_tensor * ids = dst->src[0];
  8385. const struct ggml_tensor * src1 = dst->src[1];
  8386. const struct ggml_tensor * src00 = dst->src[2];
  8387. const int id = dst->op_params[0];
  8388. GGML_ASSERT(!ggml_is_transposed(src00));
  8389. GGML_ASSERT(!ggml_is_transposed(src1));
  8390. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  8391. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8392. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8393. const int64_t ne01 = src00->ne[1];
  8394. const int64_t ne02 = src00->ne[2];
  8395. const int64_t ne03 = src00->ne[3];
  8396. //const int64_t nb01 = src00->nb[1];
  8397. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8398. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8399. const int64_t ne10 = src1->ne[0];
  8400. const int64_t ne11 = src1->ne[1];
  8401. const int64_t ne12 = src1->ne[2];
  8402. const int64_t ne13 = src1->ne[3];
  8403. //const int64_t nb11 = src1->nb[1];
  8404. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8405. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8406. const int64_t ne1 = ggml_nelements(src1);
  8407. const int64_t ne = ggml_nelements(dst);
  8408. ggml_cuda_set_device(g_main_device);
  8409. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8410. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8411. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8412. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8413. //half * src0_as_f16 = (half *) src0_ddq;
  8414. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8415. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8416. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8417. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8418. // convert src1 to fp16
  8419. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8420. GGML_ASSERT(to_fp16_cuda != nullptr);
  8421. size_t src1_as = 0;
  8422. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8423. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8424. size_t dst_as = 0;
  8425. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8426. GGML_ASSERT(ne12 % ne02 == 0);
  8427. GGML_ASSERT(ne13 % ne03 == 0);
  8428. // broadcast factors
  8429. const int64_t r2 = ne12/ne02;
  8430. const int64_t r3 = ne13/ne03;
  8431. const half alpha_f16 = 1.0f;
  8432. const half beta_f16 = 0.0f;
  8433. // use cublasGemmBatchedEx
  8434. const int ne23 = ne12*ne13;
  8435. const void ** ptrs_src = nullptr;
  8436. void ** ptrs_dst = nullptr;
  8437. size_t ptrs_src_s = 0;
  8438. size_t ptrs_dst_s = 0;
  8439. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8440. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8441. int64_t src0_ne = ggml_nelements(src00);
  8442. half * src0_as_f16 = nullptr;
  8443. size_t src0_as = 0;
  8444. if (src00->type != GGML_TYPE_F16) {
  8445. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8446. }
  8447. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8448. dim3 block_dims(ne13, ne12);
  8449. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8450. ptrs_src, ptrs_dst,
  8451. ne12, ne13,
  8452. ne23,
  8453. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8454. nb12, nb13,
  8455. dst->nb[2], dst->nb[3],
  8456. r2, r3,
  8457. src00->type, src0_as_f16, src0_ne,
  8458. src1_as_f16, dst_f16,
  8459. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8460. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8461. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8462. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8463. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8464. );
  8465. CUDA_CHECK(cudaGetLastError());
  8466. CUBLAS_CHECK(
  8467. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8468. ne01, ne11, ne10,
  8469. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8470. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8471. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8472. ne23,
  8473. CUBLAS_COMPUTE_16F,
  8474. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8475. if (src0_as != 0) {
  8476. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8477. }
  8478. if (ptrs_src_s != 0) {
  8479. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8480. }
  8481. if (ptrs_dst_s != 0) {
  8482. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8483. }
  8484. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8485. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8486. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8487. ggml_cuda_pool_free(dst_f16, dst_as);
  8488. }
  8489. #endif
  8490. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8491. #if 0
  8492. ggml_cuda_mul_mat_id_cublas(dst);
  8493. // TODO: mmq/mmv support
  8494. #endif
  8495. const size_t nb11 = src1->nb[1];
  8496. const size_t nb1 = dst->nb[1];
  8497. const struct ggml_tensor * ids = src0;
  8498. const int32_t id = ((int32_t *) dst->op_params)[0];
  8499. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  8500. std::vector<char> ids_host(ggml_nbytes(ids));
  8501. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  8502. if (ids->backend == GGML_BACKEND_GPU) {
  8503. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  8504. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  8505. CUDA_CHECK(cudaStreamSynchronize(stream));
  8506. } else {
  8507. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  8508. }
  8509. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  8510. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  8511. ggml_tensor_extra_gpu src1_row_extra;
  8512. ggml_tensor_extra_gpu dst_row_extra;
  8513. ggml_tensor src1_row = *src1;
  8514. ggml_tensor dst_row = *dst;
  8515. src1_row.backend = GGML_BACKEND_GPU;
  8516. dst_row.backend = GGML_BACKEND_GPU;
  8517. src1_row.extra = &src1_row_extra;
  8518. dst_row.extra = &dst_row_extra;
  8519. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  8520. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  8521. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  8522. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  8523. if (src1->ne[1] == 1) {
  8524. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8525. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  8526. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8527. //int32_t row_id;
  8528. //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]));
  8529. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  8530. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8531. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8532. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8533. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  8534. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  8535. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  8536. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  8537. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8538. }
  8539. } else {
  8540. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  8541. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  8542. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  8543. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  8544. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  8545. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  8546. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  8547. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  8548. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  8549. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8550. int64_t num_src1_rows = 0;
  8551. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8552. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8553. if (row_id_i != row_id) {
  8554. continue;
  8555. }
  8556. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8557. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  8558. nb11, src1_kind, stream));
  8559. num_src1_rows++;
  8560. }
  8561. if (num_src1_rows == 0) {
  8562. continue;
  8563. }
  8564. src1_row.ne[1] = num_src1_rows;
  8565. dst_row.ne[1] = num_src1_rows;
  8566. src1_row.nb[1] = nb11;
  8567. src1_row.nb[2] = num_src1_rows*nb11;
  8568. src1_row.nb[3] = num_src1_rows*nb11;
  8569. dst_row.nb[1] = nb1;
  8570. dst_row.nb[2] = num_src1_rows*nb1;
  8571. dst_row.nb[3] = num_src1_rows*nb1;
  8572. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8573. num_src1_rows = 0;
  8574. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8575. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8576. if (row_id_i != row_id) {
  8577. continue;
  8578. }
  8579. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8580. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  8581. nb1, dst_kind, stream));
  8582. num_src1_rows++;
  8583. }
  8584. }
  8585. }
  8586. if (dst->backend == GGML_BACKEND_CPU) {
  8587. CUDA_CHECK(cudaStreamSynchronize(stream));
  8588. }
  8589. }
  8590. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8591. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  8592. }
  8593. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8594. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  8595. }
  8596. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8597. const int64_t ne = ggml_nelements(src0);
  8598. GGML_ASSERT(ne == ggml_nelements(src1));
  8599. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  8600. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8601. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  8602. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  8603. const int64_t ne00 = src0->ne[0];
  8604. const int64_t ne01 = src0->ne[1];
  8605. const int64_t ne02 = src0->ne[2];
  8606. //GGML_ASSERT(src0->ne[3] == 1);
  8607. const int64_t nb00 = src0->nb[0];
  8608. const int64_t nb01 = src0->nb[1];
  8609. const int64_t nb02 = src0->nb[2];
  8610. const int64_t nb03 = src0->nb[3];
  8611. const int64_t ne10 = src1->ne[0];
  8612. const int64_t ne11 = src1->ne[1];
  8613. const int64_t ne12 = src1->ne[2];
  8614. //GGML_ASSERT(src1->ne[3] == 1);
  8615. const int64_t nb10 = src1->nb[0];
  8616. const int64_t nb11 = src1->nb[1];
  8617. const int64_t nb12 = src1->nb[2];
  8618. const int64_t nb13 = src1->nb[3];
  8619. ggml_cuda_set_device(g_main_device);
  8620. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8621. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8622. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8623. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8624. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  8625. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  8626. 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);
  8627. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  8628. 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);
  8629. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  8630. 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);
  8631. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  8632. 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);
  8633. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  8634. 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);
  8635. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  8636. 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);
  8637. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  8638. 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);
  8639. } else {
  8640. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  8641. ggml_type_name(src0->type), ggml_type_name(src1->type));
  8642. GGML_ASSERT(false);
  8643. }
  8644. (void) dst;
  8645. }
  8646. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8647. // TODO: why do we pass dst as src1 here?
  8648. ggml_cuda_cpy(src0, dst, nullptr);
  8649. (void) src1;
  8650. }
  8651. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8652. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  8653. }
  8654. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8655. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  8656. }
  8657. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8658. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8659. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  8660. }
  8661. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8662. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  8663. }
  8664. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8665. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  8666. }
  8667. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8668. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  8669. }
  8670. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8671. GGML_ASSERT(ggml_is_contiguous(src0));
  8672. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  8673. }
  8674. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8675. GGML_ASSERT(ggml_is_contiguous(src0));
  8676. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  8677. }
  8678. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8679. (void) src0;
  8680. (void) src1;
  8681. (void) dst;
  8682. }
  8683. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  8684. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  8685. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  8686. }
  8687. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  8688. if (main_device >= g_device_count) {
  8689. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8690. main_device, g_device_count, g_main_device);
  8691. return;
  8692. }
  8693. if (g_main_device != main_device && g_device_count > 1) {
  8694. g_main_device = main_device;
  8695. //cudaDeviceProp prop;
  8696. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8697. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8698. }
  8699. }
  8700. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8701. if (!g_cublas_loaded) return false;
  8702. ggml_cuda_func_t func;
  8703. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  8704. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  8705. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  8706. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8707. return false;
  8708. }
  8709. if (tensor->op == GGML_OP_MUL_MAT) {
  8710. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8711. #ifndef NDEBUG
  8712. 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]);
  8713. #endif
  8714. return false;
  8715. }
  8716. }
  8717. switch (tensor->op) {
  8718. case GGML_OP_REPEAT:
  8719. func = ggml_cuda_repeat;
  8720. break;
  8721. case GGML_OP_GET_ROWS:
  8722. func = ggml_cuda_get_rows;
  8723. break;
  8724. case GGML_OP_DUP:
  8725. func = ggml_cuda_dup;
  8726. break;
  8727. case GGML_OP_ADD:
  8728. func = ggml_cuda_add;
  8729. break;
  8730. case GGML_OP_ACC:
  8731. func = ggml_cuda_acc;
  8732. break;
  8733. case GGML_OP_MUL:
  8734. func = ggml_cuda_mul;
  8735. break;
  8736. case GGML_OP_DIV:
  8737. func = ggml_cuda_div;
  8738. break;
  8739. case GGML_OP_UNARY:
  8740. switch (ggml_get_unary_op(tensor)) {
  8741. case GGML_UNARY_OP_GELU:
  8742. func = ggml_cuda_gelu;
  8743. break;
  8744. case GGML_UNARY_OP_SILU:
  8745. func = ggml_cuda_silu;
  8746. break;
  8747. case GGML_UNARY_OP_GELU_QUICK:
  8748. func = ggml_cuda_gelu_quick;
  8749. break;
  8750. case GGML_UNARY_OP_TANH:
  8751. func = ggml_cuda_tanh;
  8752. break;
  8753. case GGML_UNARY_OP_RELU:
  8754. func = ggml_cuda_relu;
  8755. break;
  8756. case GGML_UNARY_OP_HARDSIGMOID:
  8757. func = ggml_cuda_hardsigmoid;
  8758. break;
  8759. case GGML_UNARY_OP_HARDSWISH:
  8760. func = ggml_cuda_hardswish;
  8761. break;
  8762. default:
  8763. return false;
  8764. }
  8765. break;
  8766. case GGML_OP_NORM:
  8767. func = ggml_cuda_norm;
  8768. break;
  8769. case GGML_OP_GROUP_NORM:
  8770. func = ggml_cuda_group_norm;
  8771. break;
  8772. case GGML_OP_CONCAT:
  8773. func = ggml_cuda_concat;
  8774. break;
  8775. case GGML_OP_UPSCALE:
  8776. func = ggml_cuda_upscale;
  8777. break;
  8778. case GGML_OP_PAD:
  8779. func = ggml_cuda_pad;
  8780. break;
  8781. case GGML_OP_LEAKY_RELU:
  8782. func = ggml_cuda_leaky_relu;
  8783. break;
  8784. case GGML_OP_RMS_NORM:
  8785. func = ggml_cuda_rms_norm;
  8786. break;
  8787. case GGML_OP_MUL_MAT:
  8788. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8789. return false;
  8790. }
  8791. func = ggml_cuda_mul_mat;
  8792. break;
  8793. case GGML_OP_MUL_MAT_ID:
  8794. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8795. return false;
  8796. }
  8797. func = ggml_cuda_mul_mat_id;
  8798. break;
  8799. case GGML_OP_SCALE:
  8800. func = ggml_cuda_scale;
  8801. break;
  8802. case GGML_OP_SQR:
  8803. func = ggml_cuda_sqr;
  8804. break;
  8805. case GGML_OP_CLAMP:
  8806. func = ggml_cuda_clamp;
  8807. break;
  8808. case GGML_OP_CPY:
  8809. func = ggml_cuda_cpy;
  8810. break;
  8811. case GGML_OP_CONT:
  8812. func = ggml_cuda_dup;
  8813. break;
  8814. case GGML_OP_NONE:
  8815. case GGML_OP_RESHAPE:
  8816. case GGML_OP_VIEW:
  8817. case GGML_OP_PERMUTE:
  8818. case GGML_OP_TRANSPOSE:
  8819. func = ggml_cuda_nop;
  8820. break;
  8821. case GGML_OP_DIAG_MASK_INF:
  8822. func = ggml_cuda_diag_mask_inf;
  8823. break;
  8824. case GGML_OP_SOFT_MAX:
  8825. func = ggml_cuda_soft_max;
  8826. break;
  8827. case GGML_OP_ROPE:
  8828. func = ggml_cuda_rope;
  8829. break;
  8830. case GGML_OP_ALIBI:
  8831. func = ggml_cuda_alibi;
  8832. break;
  8833. case GGML_OP_IM2COL:
  8834. func = ggml_cuda_im2col;
  8835. break;
  8836. case GGML_OP_POOL_2D:
  8837. func = ggml_cuda_pool2d;
  8838. break;
  8839. case GGML_OP_SUM_ROWS:
  8840. func = ggml_cuda_sum_rows;
  8841. break;
  8842. case GGML_OP_ARGSORT:
  8843. func = ggml_cuda_argsort;
  8844. break;
  8845. default:
  8846. return false;
  8847. }
  8848. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  8849. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8850. }
  8851. if (params->ith != 0) {
  8852. return true;
  8853. }
  8854. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  8855. return true;
  8856. }
  8857. func(tensor->src[0], tensor->src[1], tensor);
  8858. return true;
  8859. }
  8860. GGML_CALL int ggml_cuda_get_device_count() {
  8861. int device_count;
  8862. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8863. return 0;
  8864. }
  8865. return device_count;
  8866. }
  8867. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8868. cudaDeviceProp prop;
  8869. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8870. snprintf(description, description_size, "%s", prop.name);
  8871. }
  8872. ////////////////////////////////////////////////////////////////////////////////
  8873. // backend interface
  8874. #define UNUSED GGML_UNUSED
  8875. struct ggml_backend_cuda_context {
  8876. int device;
  8877. std::string name;
  8878. };
  8879. // cuda buffer
  8880. struct ggml_backend_cuda_buffer_context {
  8881. int device;
  8882. void * dev_ptr = nullptr;
  8883. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8884. size_t temp_tensor_extra_index = 0;
  8885. std::string name;
  8886. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  8887. device(device), dev_ptr(dev_ptr),
  8888. name(GGML_CUDA_NAME + std::to_string(device)) {
  8889. }
  8890. ~ggml_backend_cuda_buffer_context() {
  8891. delete[] temp_tensor_extras;
  8892. }
  8893. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8894. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  8895. if (temp_tensor_extras == nullptr) {
  8896. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8897. }
  8898. size_t alloc_index = temp_tensor_extra_index;
  8899. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8900. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8901. memset(extra, 0, sizeof(*extra));
  8902. return extra;
  8903. }
  8904. };
  8905. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  8906. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8907. return ctx->name.c_str();
  8908. }
  8909. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  8910. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  8911. }
  8912. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8913. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8914. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8915. delete ctx;
  8916. }
  8917. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8918. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8919. return ctx->dev_ptr;
  8920. }
  8921. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8922. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8923. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8924. assert(tensor->view_src->buffer->buft == buffer->buft);
  8925. tensor->backend = tensor->view_src->backend;
  8926. tensor->extra = tensor->view_src->extra;
  8927. return;
  8928. }
  8929. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8930. extra->data_device[ctx->device] = tensor->data;
  8931. tensor->backend = GGML_BACKEND_GPU;
  8932. tensor->extra = extra;
  8933. if (ggml_is_quantized(tensor->type)) {
  8934. // initialize padding to 0 to avoid possible NaN values
  8935. size_t original_size = ggml_nbytes(tensor);
  8936. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8937. if (padded_size > original_size && tensor->view_src == nullptr) {
  8938. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  8939. }
  8940. }
  8941. }
  8942. 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) {
  8943. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8944. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8945. ggml_cuda_set_device(ctx->device);
  8946. CUDA_CHECK(cudaDeviceSynchronize());
  8947. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8948. CUDA_CHECK(cudaDeviceSynchronize());
  8949. }
  8950. 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) {
  8951. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8952. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8953. ggml_cuda_set_device(ctx->device);
  8954. CUDA_CHECK(cudaDeviceSynchronize());
  8955. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8956. CUDA_CHECK(cudaDeviceSynchronize());
  8957. }
  8958. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8959. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  8960. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  8961. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8962. ggml_cuda_set_device(src_ctx->device);
  8963. CUDA_CHECK(cudaDeviceSynchronize());
  8964. ggml_cuda_set_device(dst_ctx->device);
  8965. CUDA_CHECK(cudaDeviceSynchronize());
  8966. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  8967. CUDA_CHECK(cudaDeviceSynchronize());
  8968. return true;
  8969. }
  8970. return false;
  8971. }
  8972. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8973. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8974. ggml_cuda_set_device(ctx->device);
  8975. CUDA_CHECK(cudaDeviceSynchronize());
  8976. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8977. CUDA_CHECK(cudaDeviceSynchronize());
  8978. }
  8979. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  8980. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  8981. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8982. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8983. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8984. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8985. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8986. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  8987. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8988. /* .reset = */ NULL,
  8989. };
  8990. // cuda buffer type
  8991. struct ggml_backend_cuda_buffer_type_context {
  8992. int device;
  8993. std::string name;
  8994. };
  8995. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8996. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8997. return ctx->name.c_str();
  8998. }
  8999. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9000. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9001. ggml_cuda_set_device(buft_ctx->device);
  9002. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  9003. void * dev_ptr;
  9004. cudaError_t err = cudaMalloc(&dev_ptr, size);
  9005. if (err != cudaSuccess) {
  9006. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  9007. return nullptr;
  9008. }
  9009. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  9010. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  9011. }
  9012. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9013. return 128;
  9014. UNUSED(buft);
  9015. }
  9016. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9017. size_t size = ggml_nbytes(tensor);
  9018. int64_t ne0 = tensor->ne[0];
  9019. if (ggml_is_quantized(tensor->type)) {
  9020. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9021. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9022. }
  9023. }
  9024. return size;
  9025. UNUSED(buft);
  9026. }
  9027. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9028. if (!ggml_backend_is_cuda(backend)) {
  9029. return false;
  9030. }
  9031. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  9032. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9033. return buft_ctx->device == cuda_ctx->device;
  9034. }
  9035. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  9036. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  9037. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  9038. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  9039. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9040. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  9041. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  9042. /* .is_host = */ NULL,
  9043. };
  9044. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  9045. // FIXME: this is not thread safe
  9046. if (device >= ggml_backend_cuda_get_device_count()) {
  9047. return nullptr;
  9048. }
  9049. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  9050. static bool ggml_backend_cuda_buffer_type_initialized = false;
  9051. if (!ggml_backend_cuda_buffer_type_initialized) {
  9052. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  9053. ggml_backend_cuda_buffer_types[i] = {
  9054. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  9055. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  9056. };
  9057. }
  9058. ggml_backend_cuda_buffer_type_initialized = true;
  9059. }
  9060. return &ggml_backend_cuda_buffer_types[device];
  9061. }
  9062. // cuda split buffer
  9063. struct ggml_backend_cuda_split_buffer_context {
  9064. ~ggml_backend_cuda_split_buffer_context() {
  9065. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  9066. for (int id = 0; id < g_device_count; ++id) {
  9067. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9068. if (extra->events[id][is] != nullptr) {
  9069. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  9070. }
  9071. }
  9072. if (extra->data_device[id] != nullptr) {
  9073. CUDA_CHECK(cudaFree(extra->data_device[id]));
  9074. }
  9075. }
  9076. delete extra;
  9077. }
  9078. }
  9079. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  9080. };
  9081. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  9082. return GGML_CUDA_NAME "_Split";
  9083. UNUSED(buffer);
  9084. }
  9085. static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  9086. return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  9087. UNUSED(ggml_backend_buffer_is_cuda_split); // only used in debug builds currently, avoid unused function warning in release builds
  9088. }
  9089. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9090. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9091. delete ctx;
  9092. }
  9093. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  9094. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  9095. return (void *)0x1000;
  9096. UNUSED(buffer);
  9097. }
  9098. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9099. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  9100. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9101. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9102. const int64_t ne0 = tensor->ne[0];
  9103. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  9104. ctx->tensor_extras.push_back(extra);
  9105. for (int id = 0; id < g_device_count; ++id) {
  9106. int64_t row_low, row_high;
  9107. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9108. int64_t nrows_split = row_high - row_low;
  9109. if (nrows_split == 0) {
  9110. continue;
  9111. }
  9112. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9113. const size_t original_size = size;
  9114. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9115. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9116. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9117. }
  9118. // FIXME: do not crash if cudaMalloc fails
  9119. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  9120. ggml_cuda_set_device(id);
  9121. char * buf;
  9122. CUDA_CHECK(cudaMalloc(&buf, size));
  9123. // set padding to 0 to avoid possible NaN values
  9124. if (size > original_size) {
  9125. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  9126. }
  9127. extra->data_device[id] = buf;
  9128. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9129. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  9130. }
  9131. }
  9132. tensor->backend = GGML_BACKEND_GPU_SPLIT;
  9133. tensor->extra = extra;
  9134. }
  9135. 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) {
  9136. // split tensors must always be set in their entirety at once
  9137. GGML_ASSERT(offset == 0);
  9138. GGML_ASSERT(size == ggml_nbytes(tensor));
  9139. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9140. const int64_t ne0 = tensor->ne[0];
  9141. const size_t nb1 = tensor->nb[1];
  9142. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9143. for (int id = 0; id < g_device_count; ++id) {
  9144. int64_t row_low, row_high;
  9145. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9146. int64_t nrows_split = row_high - row_low;
  9147. if (nrows_split == 0) {
  9148. continue;
  9149. }
  9150. const size_t offset_split = row_low*nb1;
  9151. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9152. const size_t original_size = size;
  9153. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9154. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9155. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9156. }
  9157. const char * buf_host = (const char *)data + offset_split;
  9158. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  9159. }
  9160. }
  9161. 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) {
  9162. // split tensors must always be set in their entirety at once
  9163. GGML_ASSERT(offset == 0);
  9164. GGML_ASSERT(size == ggml_nbytes(tensor));
  9165. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9166. const int64_t ne0 = tensor->ne[0];
  9167. const size_t nb1 = tensor->nb[1];
  9168. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9169. for (int id = 0; id < g_device_count; ++id) {
  9170. int64_t row_low, row_high;
  9171. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9172. int64_t nrows_split = row_high - row_low;
  9173. if (nrows_split == 0) {
  9174. continue;
  9175. }
  9176. const size_t offset_split = row_low*nb1;
  9177. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9178. const size_t original_size = size;
  9179. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9180. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9181. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9182. }
  9183. char * buf_host = (char *)data + offset_split;
  9184. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  9185. }
  9186. }
  9187. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9188. UNUSED(buffer);
  9189. UNUSED(value);
  9190. }
  9191. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  9192. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  9193. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  9194. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  9195. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  9196. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  9197. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  9198. /* .cpy_tensor = */ NULL,
  9199. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  9200. /* .reset = */ NULL,
  9201. };
  9202. // cuda split buffer type
  9203. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9204. return GGML_CUDA_NAME "_Split";
  9205. UNUSED(buft);
  9206. }
  9207. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9208. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  9209. // instead, we allocate them for each tensor separately in init_tensor
  9210. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  9211. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  9212. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  9213. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  9214. }
  9215. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9216. return 128;
  9217. UNUSED(buft);
  9218. }
  9219. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9220. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9221. size_t total_size = 0;
  9222. const int64_t ne0 = tensor->ne[0];
  9223. for (int id = 0; id < g_device_count; ++id) {
  9224. int64_t row_low, row_high;
  9225. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9226. int64_t nrows_split = row_high - row_low;
  9227. if (nrows_split == 0) {
  9228. continue;
  9229. }
  9230. total_size += ggml_nbytes_split(tensor, nrows_split);
  9231. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9232. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9233. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9234. }
  9235. }
  9236. return total_size;
  9237. }
  9238. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9239. return ggml_backend_is_cuda(backend);
  9240. UNUSED(buft);
  9241. }
  9242. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9243. return false;
  9244. UNUSED(buft);
  9245. }
  9246. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9247. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9248. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9249. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9250. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9251. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9252. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9253. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9254. };
  9255. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9256. // FIXME: this is not thread safe
  9257. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9258. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9259. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9260. if (all_zero) {
  9261. tensor_split_arr = g_default_tensor_split;
  9262. } else {
  9263. float split_sum = 0.0f;
  9264. for (int i = 0; i < g_device_count; ++i) {
  9265. tensor_split_arr[i] = split_sum;
  9266. split_sum += tensor_split[i];
  9267. }
  9268. for (int i = 0; i < g_device_count; ++i) {
  9269. tensor_split_arr[i] /= split_sum;
  9270. }
  9271. }
  9272. auto it = buft_map.find(tensor_split_arr);
  9273. if (it != buft_map.end()) {
  9274. return &it->second;
  9275. }
  9276. struct ggml_backend_buffer_type buft {
  9277. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9278. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9279. };
  9280. auto result = buft_map.emplace(tensor_split_arr, buft);
  9281. return &result.first->second;
  9282. }
  9283. // host buffer type
  9284. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9285. return GGML_CUDA_NAME "_Host";
  9286. UNUSED(buft);
  9287. }
  9288. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9289. return GGML_CUDA_NAME "_Host";
  9290. UNUSED(buffer);
  9291. }
  9292. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9293. ggml_cuda_host_free(buffer->context);
  9294. }
  9295. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9296. void * ptr = ggml_cuda_host_malloc(size);
  9297. if (ptr == nullptr) {
  9298. // fallback to cpu buffer
  9299. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9300. }
  9301. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9302. buffer->buft = buft;
  9303. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9304. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9305. return buffer;
  9306. }
  9307. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9308. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9309. /* .iface = */ {
  9310. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9311. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9312. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9313. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9314. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9315. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9316. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9317. },
  9318. /* .context = */ nullptr,
  9319. };
  9320. return &ggml_backend_cuda_buffer_type_host;
  9321. }
  9322. // backend
  9323. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9324. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9325. return cuda_ctx->name.c_str();
  9326. }
  9327. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9328. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9329. delete cuda_ctx;
  9330. delete backend;
  9331. }
  9332. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9333. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9334. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9335. }
  9336. 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) {
  9337. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9338. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9339. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9340. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9341. }
  9342. 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) {
  9343. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9344. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9345. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9346. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9347. }
  9348. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9349. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9350. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9351. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9352. return true;
  9353. }
  9354. return false;
  9355. }
  9356. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9357. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9358. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9359. UNUSED(backend);
  9360. }
  9361. GGML_CALL static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9362. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9363. ggml_cuda_set_main_device(cuda_ctx->device);
  9364. ggml_compute_params params = {};
  9365. params.type = GGML_TASK_COMPUTE;
  9366. params.ith = 0;
  9367. for (int i = 0; i < cgraph->n_nodes; i++) {
  9368. ggml_tensor * node = cgraph->nodes[i];
  9369. 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) {
  9370. continue;
  9371. }
  9372. #ifndef NDEBUG
  9373. assert(node->backend == GGML_BACKEND_GPU || node->backend == GGML_BACKEND_GPU_SPLIT);
  9374. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9375. assert(node->extra != nullptr);
  9376. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9377. if (node->src[j] != nullptr) {
  9378. assert(node->src[j]->backend == GGML_BACKEND_GPU || node->src[j]->backend == GGML_BACKEND_GPU_SPLIT);
  9379. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) || ggml_backend_buffer_is_cuda_split(node->src[j]->buffer));
  9380. assert(node->src[j]->extra != nullptr);
  9381. }
  9382. }
  9383. #endif
  9384. bool ok = ggml_cuda_compute_forward(&params, node);
  9385. if (!ok) {
  9386. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9387. }
  9388. GGML_ASSERT(ok);
  9389. }
  9390. return true;
  9391. }
  9392. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9393. switch (op->op) {
  9394. case GGML_OP_UNARY:
  9395. switch (ggml_get_unary_op(op)) {
  9396. case GGML_UNARY_OP_GELU:
  9397. case GGML_UNARY_OP_SILU:
  9398. case GGML_UNARY_OP_RELU:
  9399. case GGML_UNARY_OP_HARDSIGMOID:
  9400. case GGML_UNARY_OP_HARDSWISH:
  9401. case GGML_UNARY_OP_GELU_QUICK:
  9402. case GGML_UNARY_OP_TANH:
  9403. return true;
  9404. default:
  9405. return false;
  9406. }
  9407. break;
  9408. case GGML_OP_MUL_MAT:
  9409. case GGML_OP_MUL_MAT_ID:
  9410. {
  9411. struct ggml_tensor * a;
  9412. struct ggml_tensor * b;
  9413. if (op->op == GGML_OP_MUL_MAT) {
  9414. a = op->src[0];
  9415. b = op->src[1];
  9416. } else {
  9417. a = op->src[2];
  9418. b = op->src[1];
  9419. }
  9420. if (a->ne[3] != b->ne[3]) {
  9421. return false;
  9422. }
  9423. ggml_type a_type = a->type;
  9424. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  9425. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL) {
  9426. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9427. return false;
  9428. }
  9429. }
  9430. return true;
  9431. } break;
  9432. case GGML_OP_GET_ROWS:
  9433. {
  9434. switch (op->src[0]->type) {
  9435. case GGML_TYPE_F16:
  9436. case GGML_TYPE_F32:
  9437. case GGML_TYPE_Q4_0:
  9438. case GGML_TYPE_Q4_1:
  9439. case GGML_TYPE_Q5_0:
  9440. case GGML_TYPE_Q5_1:
  9441. case GGML_TYPE_Q8_0:
  9442. return true;
  9443. default:
  9444. return false;
  9445. }
  9446. } break;
  9447. case GGML_OP_CPY:
  9448. {
  9449. ggml_type src0_type = op->src[0]->type;
  9450. ggml_type src1_type = op->src[1]->type;
  9451. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9452. return true;
  9453. }
  9454. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9455. return true;
  9456. }
  9457. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9458. return true;
  9459. }
  9460. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9461. return true;
  9462. }
  9463. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9464. return true;
  9465. }
  9466. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9467. return true;
  9468. }
  9469. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9470. return true;
  9471. }
  9472. return false;
  9473. } break;
  9474. case GGML_OP_DUP:
  9475. case GGML_OP_REPEAT:
  9476. case GGML_OP_CONCAT:
  9477. {
  9478. ggml_type src0_type = op->src[0]->type;
  9479. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9480. } break;
  9481. case GGML_OP_NONE:
  9482. case GGML_OP_RESHAPE:
  9483. case GGML_OP_VIEW:
  9484. case GGML_OP_PERMUTE:
  9485. case GGML_OP_TRANSPOSE:
  9486. case GGML_OP_NORM:
  9487. case GGML_OP_ADD:
  9488. case GGML_OP_MUL:
  9489. case GGML_OP_DIV:
  9490. case GGML_OP_RMS_NORM:
  9491. case GGML_OP_SCALE:
  9492. case GGML_OP_SQR:
  9493. case GGML_OP_CLAMP:
  9494. case GGML_OP_CONT:
  9495. case GGML_OP_DIAG_MASK_INF:
  9496. case GGML_OP_SOFT_MAX:
  9497. case GGML_OP_ROPE:
  9498. case GGML_OP_ALIBI:
  9499. case GGML_OP_IM2COL:
  9500. case GGML_OP_POOL_2D:
  9501. case GGML_OP_SUM_ROWS:
  9502. case GGML_OP_ARGSORT:
  9503. case GGML_OP_ACC:
  9504. case GGML_OP_GROUP_NORM:
  9505. case GGML_OP_UPSCALE:
  9506. case GGML_OP_PAD:
  9507. case GGML_OP_LEAKY_RELU:
  9508. return true;
  9509. default:
  9510. return false;
  9511. }
  9512. UNUSED(backend);
  9513. }
  9514. static ggml_backend_i ggml_backend_cuda_interface = {
  9515. /* .get_name = */ ggml_backend_cuda_name,
  9516. /* .free = */ ggml_backend_cuda_free,
  9517. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  9518. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  9519. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  9520. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  9521. /* .synchronize = */ ggml_backend_cuda_synchronize,
  9522. /* .graph_plan_create = */ NULL,
  9523. /* .graph_plan_free = */ NULL,
  9524. /* .graph_plan_compute = */ NULL,
  9525. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  9526. /* .supports_op = */ ggml_backend_cuda_supports_op,
  9527. };
  9528. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  9529. ggml_init_cublas(); // TODO: remove from ggml.c
  9530. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  9531. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  9532. return nullptr;
  9533. }
  9534. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  9535. ggml_cuda_set_main_device(device);
  9536. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  9537. /* .device = */ device,
  9538. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  9539. };
  9540. ggml_backend_t cuda_backend = new ggml_backend {
  9541. /* .interface = */ ggml_backend_cuda_interface,
  9542. /* .context = */ ctx
  9543. };
  9544. return cuda_backend;
  9545. }
  9546. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  9547. return backend && backend->iface.get_name == ggml_backend_cuda_name;
  9548. }
  9549. GGML_CALL int ggml_backend_cuda_get_device_count() {
  9550. return ggml_cuda_get_device_count();
  9551. }
  9552. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  9553. ggml_cuda_get_device_description(device, description, description_size);
  9554. }
  9555. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  9556. ggml_cuda_set_device(device);
  9557. CUDA_CHECK(cudaMemGetInfo(free, total));
  9558. }
  9559. // backend registry
  9560. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  9561. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  9562. return cuda_backend;
  9563. UNUSED(params);
  9564. }
  9565. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  9566. GGML_CALL int ggml_backend_cuda_reg_devices() {
  9567. int device_count = ggml_cuda_get_device_count();
  9568. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  9569. for (int i = 0; i < device_count; i++) {
  9570. char name[128];
  9571. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  9572. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  9573. }
  9574. return device_count;
  9575. }