1
0

ggml-cuda.cu 248 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744
  1. #include <cstddef>
  2. #include <cstdint>
  3. #include <limits>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <atomic>
  7. #include <assert.h>
  8. #if defined(GGML_USE_HIPBLAS)
  9. #include <hip/hip_runtime.h>
  10. #include <hipblas/hipblas.h>
  11. #include <hip/hip_fp16.h>
  12. #ifdef __HIP_PLATFORM_AMD__
  13. // for rocblas_initialize()
  14. #include "rocblas/rocblas.h"
  15. #endif
  16. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  17. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  18. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  19. #define CUBLAS_OP_N HIPBLAS_OP_N
  20. #define CUBLAS_OP_T HIPBLAS_OP_T
  21. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  22. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  23. #define CUDA_R_16F HIPBLAS_R_16F
  24. #define CUDA_R_32F HIPBLAS_R_32F
  25. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  26. #define cublasCreate hipblasCreate
  27. #define cublasGemmEx hipblasGemmEx
  28. #define cublasHandle_t hipblasHandle_t
  29. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  30. #define cublasSetStream hipblasSetStream
  31. #define cublasSgemm hipblasSgemm
  32. #define cublasStatus_t hipblasStatus_t
  33. #define cudaDeviceProp hipDeviceProp_t
  34. #define cudaDeviceSynchronize hipDeviceSynchronize
  35. #define cudaError_t hipError_t
  36. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  37. #define cudaEventDisableTiming hipEventDisableTiming
  38. #define cudaEventRecord hipEventRecord
  39. #define cudaEvent_t hipEvent_t
  40. #define cudaEventDestroy hipEventDestroy
  41. #define cudaFree hipFree
  42. #define cudaFreeHost hipHostFree
  43. #define cudaGetDevice hipGetDevice
  44. #define cudaGetDeviceCount hipGetDeviceCount
  45. #define cudaGetDeviceProperties hipGetDeviceProperties
  46. #define cudaGetErrorString hipGetErrorString
  47. #define cudaGetLastError hipGetLastError
  48. #define cudaMalloc hipMalloc
  49. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  50. #define cudaMemcpy hipMemcpy
  51. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  52. #define cudaMemcpyAsync hipMemcpyAsync
  53. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  54. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  55. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  56. #define cudaMemcpyKind hipMemcpyKind
  57. #define cudaMemset hipMemset
  58. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  59. #define cudaSetDevice hipSetDevice
  60. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  61. #define cudaStreamNonBlocking hipStreamNonBlocking
  62. #define cudaStreamSynchronize hipStreamSynchronize
  63. #define cudaStreamWaitEvent(stream, event) hipStreamWaitEvent(stream, event, 0)
  64. #define cudaStream_t hipStream_t
  65. #define cudaSuccess hipSuccess
  66. #else
  67. #include <cuda_runtime.h>
  68. #include <cublas_v2.h>
  69. #include <cuda_fp16.h>
  70. #endif
  71. #include "ggml-cuda.h"
  72. #include "ggml.h"
  73. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  74. #ifndef CC_TURING
  75. #define CC_TURING 700
  76. #endif
  77. #if defined(GGML_USE_HIPBLAS)
  78. #define __CUDA_ARCH__ 1300
  79. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  80. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  81. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  82. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  83. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  84. return reinterpret_cast<const int&>(c);
  85. }
  86. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  87. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  88. c = __builtin_amdgcn_sdot4(a, b, c, false);
  89. #elif defined(__gfx1100__)
  90. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  91. #elif defined(__gfx1010__) || defined(__gfx900__)
  92. int tmp1;
  93. int tmp2;
  94. asm("\n \
  95. 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 \
  96. 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 \
  97. v_add3_u32 %0, %1, %2, %0 \n \
  98. 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 \
  99. 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 \
  100. v_add3_u32 %0, %1, %2, %0 \n \
  101. "
  102. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  103. : "v"(a), "v"(b)
  104. );
  105. #else
  106. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  107. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  108. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  109. #endif
  110. return c;
  111. }
  112. #endif
  113. #if defined(_MSC_VER)
  114. #pragma warning(disable: 4244 4267) // possible loss of data
  115. #endif
  116. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  117. #define CUDA_CHECK(err) \
  118. do { \
  119. cudaError_t err_ = (err); \
  120. if (err_ != cudaSuccess) { \
  121. fprintf(stderr, "CUDA error %d at %s:%d: %s\n", err_, __FILE__, __LINE__, \
  122. cudaGetErrorString(err_)); \
  123. exit(1); \
  124. } \
  125. } while (0)
  126. #if CUDART_VERSION >= 12000
  127. #define CUBLAS_CHECK(err) \
  128. do { \
  129. cublasStatus_t err_ = (err); \
  130. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  131. fprintf(stderr, "\ncuBLAS error %d at %s:%d: %s\n", \
  132. err_, __FILE__, __LINE__, cublasGetStatusString(err_)); \
  133. exit(1); \
  134. } \
  135. } while (0)
  136. #else
  137. #define CUBLAS_CHECK(err) \
  138. do { \
  139. cublasStatus_t err_ = (err); \
  140. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  141. fprintf(stderr, "\ncuBLAS error %d at %s:%d\n", err_, __FILE__, __LINE__); \
  142. exit(1); \
  143. } \
  144. } while (0)
  145. #endif // CUDART_VERSION >= 11
  146. #ifdef GGML_CUDA_F16
  147. typedef half dfloat; // dequantize float
  148. typedef half2 dfloat2;
  149. #else
  150. typedef float dfloat; // dequantize float
  151. typedef float2 dfloat2;
  152. #endif //GGML_CUDA_F16
  153. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  154. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  155. int x32 = 0;
  156. x32 |= x16[0] << 0;
  157. x32 |= x16[1] << 16;
  158. return x32;
  159. }
  160. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  161. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  162. int x32 = 0;
  163. x32 |= x16[0] << 0;
  164. x32 |= x16[1] << 16;
  165. return x32;
  166. }
  167. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  168. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  169. }
  170. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  171. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  172. }
  173. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  174. typedef void (*to_fp32_cuda_t)(const void * __restrict__ x, float * __restrict__ y, int k, cudaStream_t stream);
  175. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  176. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  177. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  178. typedef void (*ggml_cuda_op_t)(
  179. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i, float * src0_ddf_i,
  180. float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  181. cudaStream_t & cudaStream_main);
  182. // QK = number of values after dequantization
  183. // QR = QK / number of values before dequantization
  184. // QI = number of 32 bit integers before dequantization
  185. #define QK4_0 32
  186. #define QR4_0 2
  187. #define QI4_0 (QK4_0 / (4 * QR4_0))
  188. typedef struct {
  189. half d; // delta
  190. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  191. } block_q4_0;
  192. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  193. #define QK4_1 32
  194. #define QR4_1 2
  195. #define QI4_1 (QK4_1 / (4 * QR4_1))
  196. typedef struct {
  197. half2 dm; // dm.x = delta, dm.y = min
  198. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  199. } block_q4_1;
  200. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  201. #define QK5_0 32
  202. #define QR5_0 2
  203. #define QI5_0 (QK5_0 / (4 * QR5_0))
  204. typedef struct {
  205. half d; // delta
  206. uint8_t qh[4]; // 5-th bit of quants
  207. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  208. } block_q5_0;
  209. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  210. #define QK5_1 32
  211. #define QR5_1 2
  212. #define QI5_1 (QK5_1 / (4 * QR5_1))
  213. typedef struct {
  214. half2 dm; // dm.x = delta, dm.y = min
  215. uint8_t qh[4]; // 5-th bit of quants
  216. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  217. } block_q5_1;
  218. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  219. #define QK8_0 32
  220. #define QR8_0 1
  221. #define QI8_0 (QK8_0 / (4 * QR8_0))
  222. typedef struct {
  223. half d; // delta
  224. int8_t qs[QK8_0]; // quants
  225. } block_q8_0;
  226. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  227. #define QK8_1 32
  228. #define QR8_1 1
  229. #define QI8_1 (QK8_1 / (4 * QR8_1))
  230. typedef struct {
  231. half2 ds; // ds.x = delta, ds.y = sum
  232. int8_t qs[QK8_0]; // quants
  233. } block_q8_1;
  234. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  235. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  236. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  237. typedef void (*load_tiles_cuda_t)(
  238. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  239. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  240. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  241. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  242. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  243. //================================= k-quants
  244. #ifdef GGML_QKK_64
  245. #define QK_K 64
  246. #define K_SCALE_SIZE 4
  247. #else
  248. #define QK_K 256
  249. #define K_SCALE_SIZE 12
  250. #endif
  251. #define QR2_K 4
  252. #define QI2_K (QK_K / (4*QR2_K))
  253. typedef struct {
  254. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  255. uint8_t qs[QK_K/4]; // quants
  256. half2 dm; // super-block scale for quantized scales/mins
  257. } block_q2_K;
  258. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  259. #define QR3_K 4
  260. #define QI3_K (QK_K / (4*QR3_K))
  261. typedef struct {
  262. uint8_t hmask[QK_K/8]; // quants - high bit
  263. uint8_t qs[QK_K/4]; // quants - low 2 bits
  264. #ifdef GGML_QKK_64
  265. uint8_t scales[2]; // scales, quantized with 8 bits
  266. #else
  267. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  268. #endif
  269. half d; // super-block scale
  270. } block_q3_K;
  271. //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");
  272. #define QR4_K 2
  273. #define QI4_K (QK_K / (4*QR4_K))
  274. #ifdef GGML_QKK_64
  275. typedef struct {
  276. half d[2]; // super-block scales/mins
  277. uint8_t scales[2]; // 4-bit block scales/mins
  278. uint8_t qs[QK_K/2]; // 4--bit quants
  279. } block_q4_K;
  280. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + QK_K/2 + 2, "wrong q4_K block size/padding");
  281. #else
  282. typedef struct {
  283. half2 dm; // super-block scale for quantized scales/mins
  284. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  285. uint8_t qs[QK_K/2]; // 4--bit quants
  286. } block_q4_K;
  287. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  288. #endif
  289. #define QR5_K 2
  290. #define QI5_K (QK_K / (4*QR5_K))
  291. #ifdef GGML_QKK_64
  292. typedef struct {
  293. half d; // super-block scale
  294. int8_t scales[QK_K/16]; // block scales
  295. uint8_t qh[QK_K/8]; // quants, high bit
  296. uint8_t qs[QK_K/2]; // quants, low 4 bits
  297. } block_q5_K;
  298. 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");
  299. #else
  300. typedef struct {
  301. half2 dm; // super-block scale for quantized scales/mins
  302. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  303. uint8_t qh[QK_K/8]; // quants, high bit
  304. uint8_t qs[QK_K/2]; // quants, low 4 bits
  305. } block_q5_K;
  306. 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");
  307. #endif
  308. #define QR6_K 2
  309. #define QI6_K (QK_K / (4*QR6_K))
  310. typedef struct {
  311. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  312. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  313. int8_t scales[QK_K/16]; // scales
  314. half d; // delta
  315. } block_q6_K;
  316. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  317. #define WARP_SIZE 32
  318. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  319. #define CUDA_ADD_BLOCK_SIZE 256
  320. #define CUDA_MUL_BLOCK_SIZE 256
  321. #define CUDA_GELU_BLOCK_SIZE 256
  322. #define CUDA_SILU_BLOCK_SIZE 256
  323. #define CUDA_CPY_BLOCK_SIZE 32
  324. #define CUDA_SCALE_BLOCK_SIZE 256
  325. #define CUDA_ROPE_BLOCK_SIZE 256
  326. #define CUDA_ALIBI_BLOCK_SIZE 32
  327. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  328. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  329. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  330. // dmmv = dequantize_mul_mat_vec
  331. #ifndef GGML_CUDA_DMMV_X
  332. #define GGML_CUDA_DMMV_X 32
  333. #endif
  334. #ifndef GGML_CUDA_MMV_Y
  335. #define GGML_CUDA_MMV_Y 1
  336. #endif
  337. #ifndef K_QUANTS_PER_ITERATION
  338. #define K_QUANTS_PER_ITERATION 2
  339. #else
  340. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  341. #endif
  342. struct ggml_tensor_extra_gpu {
  343. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  344. cudaEvent_t events[GGML_CUDA_MAX_DEVICES]; // events for synchronizing multiple GPUs
  345. };
  346. static int g_device_count = -1;
  347. static int g_main_device = 0;
  348. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  349. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  350. static bool g_mul_mat_q = true;
  351. static void * g_scratch_buffer = nullptr;
  352. static size_t g_scratch_size = 1024*1024*1024; // 1 GB by default
  353. static size_t g_scratch_offset = 0;
  354. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  355. static cudaStream_t g_cudaStreams_main[GGML_CUDA_MAX_DEVICES] = { nullptr };
  356. static __global__ void add_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  357. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  358. if (i >= kx) {
  359. return;
  360. }
  361. dst[i] = x[i] + y[i%ky];
  362. }
  363. static __global__ void add_f16_f32_f16(const half * x, const float * y, half * dst, const int k) {
  364. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  365. if (i >= k) {
  366. return;
  367. }
  368. dst[i] = __hadd(x[i], __float2half(y[i]));
  369. }
  370. static __global__ void mul_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  371. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  372. if (i >= kx) {
  373. return;
  374. }
  375. dst[i] = x[i] * y[i%ky];
  376. }
  377. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  378. const float GELU_COEF_A = 0.044715f;
  379. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  380. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  381. if (i >= k) {
  382. return;
  383. }
  384. float xi = x[i];
  385. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  386. }
  387. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  388. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  389. if (i >= k) {
  390. return;
  391. }
  392. dst[i] = x[i] / (1.0f + expf(-x[i]));
  393. }
  394. static __global__ void norm_f32(const float * x, float * dst, const int ncols) {
  395. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  396. const int tid = threadIdx.x;
  397. const float eps = 1e-5f;
  398. float mean = 0.0f;
  399. float var = 0.0f;
  400. for (int col = tid; col < ncols; col += WARP_SIZE) {
  401. const float xi = x[row*ncols + col];
  402. mean += xi;
  403. var += xi * xi;
  404. }
  405. // sum up partial sums
  406. #pragma unroll
  407. for (int mask = 16; mask > 0; mask >>= 1) {
  408. mean += __shfl_xor_sync(0xffffffff, mean, mask, 32);
  409. var += __shfl_xor_sync(0xffffffff, var, mask, 32);
  410. }
  411. mean /= ncols;
  412. var = var / ncols - mean * mean;
  413. const float inv_var = rsqrtf(var + eps);
  414. for (int col = tid; col < ncols; col += WARP_SIZE) {
  415. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_var;
  416. }
  417. }
  418. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  419. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  420. const int tid = threadIdx.x;
  421. float tmp = 0.0f; // partial sum for thread in warp
  422. for (int col = tid; col < ncols; col += WARP_SIZE) {
  423. const float xi = x[row*ncols + col];
  424. tmp += xi * xi;
  425. }
  426. // sum up partial sums
  427. #pragma unroll
  428. for (int mask = 16; mask > 0; mask >>= 1) {
  429. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  430. }
  431. const float mean = tmp / ncols;
  432. const float scale = rsqrtf(mean + eps);
  433. for (int col = tid; col < ncols; col += WARP_SIZE) {
  434. dst[row*ncols + col] = scale * x[row*ncols + col];
  435. }
  436. }
  437. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  438. const block_q4_0 * x = (const block_q4_0 *) vx;
  439. const dfloat d = x[ib].d;
  440. const int vui = x[ib].qs[iqs];
  441. v.x = vui & 0xF;
  442. v.y = vui >> 4;
  443. #ifdef GGML_CUDA_F16
  444. v = __hsub2(v, {8.0f, 8.0f});
  445. v = __hmul2(v, {d, d});
  446. #else
  447. v.x = (v.x - 8.0f) * d;
  448. v.y = (v.y - 8.0f) * d;
  449. #endif // GGML_CUDA_F16
  450. }
  451. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  452. const block_q4_1 * x = (const block_q4_1 *) vx;
  453. const dfloat d = __low2half(x[ib].dm);
  454. const dfloat m = __high2half(x[ib].dm);
  455. const int vui = x[ib].qs[iqs];
  456. v.x = vui & 0xF;
  457. v.y = vui >> 4;
  458. #ifdef GGML_CUDA_F16
  459. v = __hmul2(v, {d, d});
  460. v = __hadd2(v, {m, m});
  461. #else
  462. v.x = (v.x * d) + m;
  463. v.y = (v.y * d) + m;
  464. #endif // GGML_CUDA_F16
  465. }
  466. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  467. const block_q5_0 * x = (const block_q5_0 *) vx;
  468. const dfloat d = x[ib].d;
  469. uint32_t qh;
  470. memcpy(&qh, x[ib].qh, sizeof(qh));
  471. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  472. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  473. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  474. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  475. #ifdef GGML_CUDA_F16
  476. v = __hsub2(v, {16.0f, 16.0f});
  477. v = __hmul2(v, {d, d});
  478. #else
  479. v.x = (v.x - 16.0f) * d;
  480. v.y = (v.y - 16.0f) * d;
  481. #endif // GGML_CUDA_F16
  482. }
  483. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  484. const block_q5_1 * x = (const block_q5_1 *) vx;
  485. const dfloat d = __low2half(x[ib].dm);
  486. const dfloat m = __high2half(x[ib].dm);
  487. uint32_t qh;
  488. memcpy(&qh, x[ib].qh, sizeof(qh));
  489. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  490. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  491. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  492. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  493. #ifdef GGML_CUDA_F16
  494. v = __hmul2(v, {d, d});
  495. v = __hadd2(v, {m, m});
  496. #else
  497. v.x = (v.x * d) + m;
  498. v.y = (v.y * d) + m;
  499. #endif // GGML_CUDA_F16
  500. }
  501. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  502. const block_q8_0 * x = (const block_q8_0 *) vx;
  503. const dfloat d = x[ib].d;
  504. v.x = x[ib].qs[iqs + 0];
  505. v.y = x[ib].qs[iqs + 1];
  506. #ifdef GGML_CUDA_F16
  507. v = __hmul2(v, {d, d});
  508. #else
  509. v.x *= d;
  510. v.y *= d;
  511. #endif // GGML_CUDA_F16
  512. }
  513. //================================== k-quants
  514. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, float * __restrict__ yy) {
  515. const int i = blockIdx.x;
  516. const block_q2_K * x = (const block_q2_K *) vx;
  517. const int tid = threadIdx.x;
  518. #if QK_K == 256
  519. const int n = tid/32;
  520. const int l = tid - 32*n;
  521. const int is = 8*n + l/16;
  522. const uint8_t q = x[i].qs[32*n + l];
  523. float * y = yy + i*QK_K + 128*n;
  524. float dall = __low2half(x[i].dm);
  525. float dmin = __high2half(x[i].dm);
  526. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  527. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  528. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  529. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  530. #else
  531. const int is = tid/16; // 0 or 1
  532. const int il = tid%16; // 0...15
  533. const uint8_t q = x[i].qs[il] >> (2*is);
  534. float * y = yy + i*QK_K + 16*is + il;
  535. float dall = __low2half(x[i].dm);
  536. float dmin = __high2half(x[i].dm);
  537. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  538. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  539. #endif
  540. }
  541. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, float * __restrict__ yy) {
  542. const int i = blockIdx.x;
  543. const block_q3_K * x = (const block_q3_K *) vx;
  544. #if QK_K == 256
  545. const int r = threadIdx.x/4;
  546. const int tid = r/2;
  547. const int is0 = r%2;
  548. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  549. const int n = tid / 4;
  550. const int j = tid - 4*n;
  551. uint8_t m = 1 << (4*n + j);
  552. int is = 8*n + 2*j + is0;
  553. int shift = 2*j;
  554. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  555. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  556. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  557. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  558. float d_all = x[i].d;
  559. float dl = d_all * (us - 32);
  560. float * y = yy + i*QK_K + 128*n + 32*j;
  561. const uint8_t * q = x[i].qs + 32*n;
  562. const uint8_t * hm = x[i].hmask;
  563. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  564. #else
  565. const int tid = threadIdx.x;
  566. const int is = tid/16; // 0 or 1
  567. const int il = tid%16; // 0...15
  568. const int im = il/8; // 0...1
  569. const int in = il%8; // 0...7
  570. float * y = yy + i*QK_K + 16*is + il;
  571. const uint8_t q = x[i].qs[il] >> (2*is);
  572. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  573. const float d = (float)x[i].d;
  574. if (is == 0) {
  575. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  576. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  577. } else {
  578. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  579. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  580. }
  581. #endif
  582. }
  583. #if QK_K == 256
  584. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  585. if (j < 4) {
  586. d = q[j] & 63; m = q[j + 4] & 63;
  587. } else {
  588. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  589. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  590. }
  591. }
  592. #endif
  593. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, float * __restrict__ yy) {
  594. const block_q4_K * x = (const block_q4_K *) vx;
  595. const int i = blockIdx.x;
  596. #if QK_K == 256
  597. // assume 32 threads
  598. const int tid = threadIdx.x;
  599. const int il = tid/8;
  600. const int ir = tid%8;
  601. const int is = 2*il;
  602. const int n = 4;
  603. float * y = yy + i*QK_K + 64*il + n*ir;
  604. const float dall = __low2half(x[i].dm);
  605. const float dmin = __high2half(x[i].dm);
  606. const uint8_t * q = x[i].qs + 32*il + n*ir;
  607. uint8_t sc, m;
  608. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  609. const float d1 = dall * sc; const float m1 = dmin * m;
  610. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  611. const float d2 = dall * sc; const float m2 = dmin * m;
  612. for (int l = 0; l < n; ++l) {
  613. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  614. y[l +32] = d2 * (q[l] >> 4) - m2;
  615. }
  616. #else
  617. const int tid = threadIdx.x;
  618. const uint8_t * q = x[i].qs;
  619. float * y = yy + i*QK_K;
  620. const float d = (float)x[i].d[0];
  621. const float m = (float)x[i].d[1];
  622. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  623. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  624. #endif
  625. }
  626. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, float * __restrict__ yy) {
  627. const block_q5_K * x = (const block_q5_K *) vx;
  628. const int i = blockIdx.x;
  629. #if QK_K == 256
  630. // assume 64 threads - this is very slightly better than the one below
  631. const int tid = threadIdx.x;
  632. const int il = tid/16; // il is in 0...3
  633. const int ir = tid%16; // ir is in 0...15
  634. const int is = 2*il; // is is in 0...6
  635. float * y = yy + i*QK_K + 64*il + 2*ir;
  636. const float dall = __low2half(x[i].dm);
  637. const float dmin = __high2half(x[i].dm);
  638. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  639. const uint8_t * qh = x[i].qh + 2*ir;
  640. uint8_t sc, m;
  641. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  642. const float d1 = dall * sc; const float m1 = dmin * m;
  643. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  644. const float d2 = dall * sc; const float m2 = dmin * m;
  645. uint8_t hm = 1 << (2*il);
  646. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  647. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  648. hm <<= 1;
  649. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  650. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  651. #else
  652. const int tid = threadIdx.x;
  653. const uint8_t q = x[i].qs[tid];
  654. const int im = tid/8; // 0...3
  655. const int in = tid%8; // 0...7
  656. const int is = tid/16; // 0 or 1
  657. const uint8_t h = x[i].qh[in] >> im;
  658. const float d = x[i].d;
  659. float * y = yy + i*QK_K + tid;
  660. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  661. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  662. #endif
  663. }
  664. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, float * __restrict__ yy) {
  665. const block_q6_K * x = (const block_q6_K *) vx;
  666. const int i = blockIdx.x;
  667. #if QK_K == 256
  668. // assume 64 threads - this is very slightly better than the one below
  669. const int tid = threadIdx.x;
  670. const int ip = tid/32; // ip is 0 or 1
  671. const int il = tid - 32*ip; // 0...32
  672. const int is = 8*ip + il/16;
  673. float * y = yy + i*QK_K + 128*ip + il;
  674. const float d = x[i].d;
  675. const uint8_t * ql = x[i].ql + 64*ip + il;
  676. const uint8_t qh = x[i].qh[32*ip + il];
  677. const int8_t * sc = x[i].scales + is;
  678. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  679. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  680. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  681. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  682. #else
  683. // assume 32 threads
  684. const int tid = threadIdx.x;
  685. const int ip = tid/16; // 0 or 1
  686. const int il = tid - 16*ip; // 0...15
  687. float * y = yy + i*QK_K + 16*ip + il;
  688. const float d = x[i].d;
  689. const uint8_t ql = x[i].ql[16*ip + il];
  690. const uint8_t qh = x[i].qh[il] >> (2*ip);
  691. const int8_t * sc = x[i].scales;
  692. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  693. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  694. #endif
  695. }
  696. 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) {
  697. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  698. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  699. if (row > nrows) return;
  700. const int num_blocks_per_row = ncols / QK_K;
  701. const int ib0 = row*num_blocks_per_row;
  702. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  703. float tmp = 0; // partial sum for thread in warp
  704. #if QK_K == 256
  705. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  706. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  707. const int step = 16/K_QUANTS_PER_ITERATION;
  708. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  709. const int in = tid - step*im; // 0...15 or 0...7
  710. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  711. const int q_offset = 32*im + l0;
  712. const int s_offset = 8*im;
  713. const int y_offset = 128*im + l0;
  714. uint32_t aux[4];
  715. const uint8_t * d = (const uint8_t *)aux;
  716. const uint8_t * m = (const uint8_t *)(aux + 2);
  717. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  718. const float * y = yy + i * QK_K + y_offset;
  719. const uint8_t * q = x[i].qs + q_offset;
  720. const float dall = __low2half(x[i].dm);
  721. const float dmin = __high2half(x[i].dm);
  722. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  723. aux[0] = a[0] & 0x0f0f0f0f;
  724. aux[1] = a[1] & 0x0f0f0f0f;
  725. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  726. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  727. float sum1 = 0, sum2 = 0;
  728. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  729. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  730. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  731. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  732. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  733. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  734. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  735. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  736. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  737. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  738. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  739. }
  740. tmp += dall * sum1 - dmin * sum2;
  741. }
  742. #else
  743. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  744. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  745. const int offset = tid * K_QUANTS_PER_ITERATION;
  746. uint32_t uaux[2];
  747. const uint8_t * d = (const uint8_t *)uaux;
  748. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  749. const float * y = yy + i * QK_K + offset;
  750. const uint8_t * q = x[i].qs + offset;
  751. const uint32_t * s = (const uint32_t *)x[i].scales;
  752. uaux[0] = s[0] & 0x0f0f0f0f;
  753. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  754. const float2 dall = __half22float2(x[i].dm);
  755. float sum1 = 0, sum2 = 0;
  756. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  757. const uint8_t ql = q[l];
  758. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  759. + y[l+16] * d[1] * ((ql >> 2) & 3)
  760. + y[l+32] * d[2] * ((ql >> 4) & 3)
  761. + y[l+48] * d[3] * ((ql >> 6) & 3);
  762. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  763. }
  764. tmp += dall.x * sum1 - dall.y * sum2;
  765. }
  766. #endif
  767. // sum up partial sums and write back result
  768. #pragma unroll
  769. for (int mask = 16; mask > 0; mask >>= 1) {
  770. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  771. }
  772. if (threadIdx.x == 0) {
  773. dst[row] = tmp;
  774. }
  775. }
  776. 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) {
  777. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  778. if (row > nrows) return;
  779. const int num_blocks_per_row = ncols / QK_K;
  780. const int ib0 = row*num_blocks_per_row;
  781. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  782. float tmp = 0; // partial sum for thread in warp
  783. #if QK_K == 256
  784. const uint16_t kmask1 = 0x0303;
  785. const uint16_t kmask2 = 0x0f0f;
  786. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  787. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  788. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  789. const int step = 16/K_QUANTS_PER_ITERATION;
  790. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  791. const int in = tid - step*im; // 0....15 or 0...7
  792. const uint8_t m = 1 << (4*im);
  793. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  794. const int q_offset = 32*im + l0;
  795. const int y_offset = 128*im + l0;
  796. uint16_t utmp[4];
  797. const int8_t * s = (const int8_t *)utmp;
  798. const uint16_t s_shift = 4*im;
  799. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  800. const float * y = yy + i * QK_K + y_offset;
  801. const uint8_t * q = x[i].qs + q_offset;
  802. const uint8_t * h = x[i].hmask + l0;
  803. const uint16_t * a = (const uint16_t *)x[i].scales;
  804. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  805. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  806. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  807. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  808. const float d = x[i].d;
  809. float sum = 0;
  810. for (int l = 0; l < n; ++l) {
  811. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  812. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  813. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  814. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  815. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  816. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  817. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  818. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  819. }
  820. tmp += d * sum;
  821. }
  822. #else
  823. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  824. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  825. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  826. const int in = offset/8; // 0 or 1
  827. const int im = offset%8; // 0...7
  828. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  829. const float * y = yy + i * QK_K + offset;
  830. const uint8_t * q = x[i].qs + offset;
  831. const uint8_t * s = x[i].scales;
  832. const float dall = (float)x[i].d;
  833. float sum = 0;
  834. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  835. const uint8_t hl = x[i].hmask[im+l] >> in;
  836. const uint8_t ql = q[l];
  837. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  838. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  839. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  840. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  841. }
  842. tmp += sum;
  843. }
  844. #endif
  845. // sum up partial sums and write back result
  846. #pragma unroll
  847. for (int mask = 16; mask > 0; mask >>= 1) {
  848. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  849. }
  850. if (threadIdx.x == 0) {
  851. dst[row] = tmp;
  852. }
  853. }
  854. 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) {
  855. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  856. if (row > nrows) return;
  857. const int num_blocks_per_row = ncols / QK_K;
  858. const int ib0 = row*num_blocks_per_row;
  859. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  860. #if QK_K == 256
  861. const uint16_t kmask1 = 0x3f3f;
  862. const uint16_t kmask2 = 0x0f0f;
  863. const uint16_t kmask3 = 0xc0c0;
  864. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  865. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  866. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  867. const int il = tid/step; // 0...3
  868. const int ir = tid - step*il; // 0...7 or 0...3
  869. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  870. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  871. const int in = il%2;
  872. const int l0 = n*(2*ir + in);
  873. const int q_offset = 32*im + l0;
  874. const int y_offset = 64*im + l0;
  875. uint16_t aux[4];
  876. const uint8_t * sc = (const uint8_t *)aux;
  877. #if K_QUANTS_PER_ITERATION == 2
  878. uint32_t q32[4];
  879. const uint8_t * q4 = (const uint8_t *)q32;
  880. #else
  881. uint16_t q16[4];
  882. const uint8_t * q4 = (const uint8_t *)q16;
  883. #endif
  884. float tmp = 0; // partial sum for thread in warp
  885. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  886. const float * y1 = yy + i*QK_K + y_offset;
  887. const float * y2 = y1 + 128;
  888. const float dall = __low2half(x[i].dm);
  889. const float dmin = __high2half(x[i].dm);
  890. const uint16_t * a = (const uint16_t *)x[i].scales;
  891. aux[0] = a[im+0] & kmask1;
  892. aux[1] = a[im+2] & kmask1;
  893. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  894. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  895. #if K_QUANTS_PER_ITERATION == 2
  896. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  897. const uint32_t * q2 = q1 + 16;
  898. q32[0] = q1[0] & 0x0f0f0f0f;
  899. q32[1] = q1[0] & 0xf0f0f0f0;
  900. q32[2] = q2[0] & 0x0f0f0f0f;
  901. q32[3] = q2[0] & 0xf0f0f0f0;
  902. float4 s = {0.f, 0.f, 0.f, 0.f};
  903. float smin = 0;
  904. for (int l = 0; l < 4; ++l) {
  905. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  906. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  907. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  908. }
  909. 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;
  910. #else
  911. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  912. const uint16_t * q2 = q1 + 32;
  913. q16[0] = q1[0] & 0x0f0f;
  914. q16[1] = q1[0] & 0xf0f0;
  915. q16[2] = q2[0] & 0x0f0f;
  916. q16[3] = q2[0] & 0xf0f0;
  917. float4 s = {0.f, 0.f, 0.f, 0.f};
  918. float smin = 0;
  919. for (int l = 0; l < 2; ++l) {
  920. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  921. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  922. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  923. }
  924. 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;
  925. #endif
  926. }
  927. #else
  928. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  929. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  930. const int step = tid * K_QUANTS_PER_ITERATION;
  931. uint16_t aux16[2];
  932. const uint8_t * s = (const uint8_t *)aux16;
  933. float tmp = 0;
  934. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  935. const uint8_t * q = x[i].qs + step;
  936. const float * y = yy + i*QK_K + step;
  937. const uint16_t * a = (const uint16_t *)x[i].scales;
  938. aux16[0] = a[0] & 0x0f0f;
  939. aux16[1] = (a[0] >> 4) & 0x0f0f;
  940. const float d = (float)x[i].d[0];
  941. const float m = (float)x[i].d[1];
  942. float sum = 0.f;
  943. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  944. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  945. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  946. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  947. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  948. }
  949. tmp += sum;
  950. }
  951. #endif
  952. // sum up partial sums and write back result
  953. #pragma unroll
  954. for (int mask = 16; mask > 0; mask >>= 1) {
  955. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  956. }
  957. if (tid == 0) {
  958. dst[row] = tmp;
  959. }
  960. }
  961. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  962. const int row = blockIdx.x;
  963. const int num_blocks_per_row = ncols / QK_K;
  964. const int ib0 = row*num_blocks_per_row;
  965. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  966. float tmp = 0; // partial sum for thread in warp
  967. #if QK_K == 256
  968. const uint16_t kmask1 = 0x3f3f;
  969. const uint16_t kmask2 = 0x0f0f;
  970. const uint16_t kmask3 = 0xc0c0;
  971. const int tid = threadIdx.x/2; // 0...15
  972. const int ix = threadIdx.x%2;
  973. const int il = tid/4; // 0...3
  974. const int ir = tid - 4*il;// 0...3
  975. const int n = 2;
  976. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  977. const int in = il%2;
  978. const int l0 = n*(2*ir + in);
  979. const int q_offset = 32*im + l0;
  980. const int y_offset = 64*im + l0;
  981. const uint8_t hm1 = 1 << (2*im);
  982. const uint8_t hm2 = hm1 << 4;
  983. uint16_t aux[4];
  984. const uint8_t * sc = (const uint8_t *)aux;
  985. uint16_t q16[8];
  986. const uint8_t * q4 = (const uint8_t *)q16;
  987. for (int i = ix; i < num_blocks_per_row; i += 2) {
  988. const uint8_t * ql1 = x[i].qs + q_offset;
  989. const uint8_t * qh = x[i].qh + l0;
  990. const float * y1 = yy + i*QK_K + y_offset;
  991. const float * y2 = y1 + 128;
  992. const float dall = __low2half(x[i].dm);
  993. const float dmin = __high2half(x[i].dm);
  994. const uint16_t * a = (const uint16_t *)x[i].scales;
  995. aux[0] = a[im+0] & kmask1;
  996. aux[1] = a[im+2] & kmask1;
  997. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  998. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  999. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1000. float smin = 0;
  1001. const uint16_t * q1 = (const uint16_t *)ql1;
  1002. const uint16_t * q2 = q1 + 32;
  1003. q16[0] = q1[0] & 0x0f0f;
  1004. q16[1] = q1[8] & 0x0f0f;
  1005. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1006. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1007. q16[4] = q2[0] & 0x0f0f;
  1008. q16[5] = q2[8] & 0x0f0f;
  1009. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1010. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1011. for (int l = 0; l < n; ++l) {
  1012. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1013. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1014. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1015. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1016. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1017. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1018. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1019. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1020. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1021. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1022. }
  1023. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1024. }
  1025. #else
  1026. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1027. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1028. const int step = tid * K_QUANTS_PER_ITERATION;
  1029. const int im = step/8;
  1030. const int in = step%8;
  1031. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1032. const uint8_t * q = x[i].qs + step;
  1033. const int8_t * s = x[i].scales;
  1034. const float * y = yy + i*QK_K + step;
  1035. const float d = x[i].d;
  1036. float sum = 0.f;
  1037. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1038. const uint8_t h = x[i].qh[in+j] >> im;
  1039. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1040. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1041. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1042. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1043. }
  1044. tmp += sum;
  1045. }
  1046. #endif
  1047. // sum up partial sums and write back result
  1048. #pragma unroll
  1049. for (int mask = 16; mask > 0; mask >>= 1) {
  1050. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1051. }
  1052. if (threadIdx.x == 0) {
  1053. dst[row] = tmp;
  1054. }
  1055. }
  1056. 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) {
  1057. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1058. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  1059. if (row > nrows) return;
  1060. const int num_blocks_per_row = ncols / QK_K;
  1061. const int ib0 = row*num_blocks_per_row;
  1062. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1063. #if QK_K == 256
  1064. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1065. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1066. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1067. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1068. const int in = tid - step*im; // 0...15 or 0...7
  1069. #if K_QUANTS_PER_ITERATION == 1
  1070. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1071. const int is = 0;
  1072. #else
  1073. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1074. const int is = in / 4;
  1075. #endif
  1076. const int ql_offset = 64*im + l0;
  1077. const int qh_offset = 32*im + l0;
  1078. const int s_offset = 8*im + is;
  1079. const int y_offset = 128*im + l0;
  1080. float tmp = 0; // partial sum for thread in warp
  1081. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1082. const float * y = yy + i * QK_K + y_offset;
  1083. const uint8_t * ql = x[i].ql + ql_offset;
  1084. const uint8_t * qh = x[i].qh + qh_offset;
  1085. const int8_t * s = x[i].scales + s_offset;
  1086. const float d = x[i].d;
  1087. #if K_QUANTS_PER_ITERATION == 1
  1088. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1089. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1090. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1091. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1092. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1093. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1094. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1095. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1096. tmp += sum;
  1097. #else
  1098. float sum = 0;
  1099. for (int l = 0; l < 4; ++l) {
  1100. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1101. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1102. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1103. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1104. }
  1105. tmp += sum;
  1106. #endif
  1107. }
  1108. #else
  1109. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1110. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1111. const int step = tid * K_QUANTS_PER_ITERATION;
  1112. float tmp = 0; // partial sum for thread in warp
  1113. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1114. const float * y = yy + i * QK_K + step;
  1115. const uint8_t * ql = x[i].ql + step;
  1116. const uint8_t * qh = x[i].qh + step;
  1117. const int8_t * s = x[i].scales;
  1118. const float d = x[i+0].d;
  1119. float sum = 0;
  1120. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1121. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1122. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1123. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1124. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1125. }
  1126. tmp += sum;
  1127. }
  1128. #endif
  1129. // sum up partial sums and write back result
  1130. #pragma unroll
  1131. for (int mask = 16; mask > 0; mask >>= 1) {
  1132. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1133. }
  1134. if (tid == 0) {
  1135. dst[row] = tmp;
  1136. }
  1137. }
  1138. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1139. const half * x = (const half *) vx;
  1140. // automatic half -> float type cast if dfloat == float
  1141. v.x = x[ib + iqs + 0];
  1142. v.y = x[ib + iqs + 1];
  1143. }
  1144. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1145. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1146. if (ix >= kx_padded) {
  1147. return;
  1148. }
  1149. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1150. const int i_padded = iy*kx_padded + ix;
  1151. block_q8_1 * y = (block_q8_1 *) vy;
  1152. const int ib = i_padded / QK8_1; // block index
  1153. const int iqs = i_padded % QK8_1; // quant index
  1154. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1155. float amax = fabsf(xi);
  1156. float sum = xi;
  1157. #pragma unroll
  1158. for (int mask = 16; mask > 0; mask >>= 1) {
  1159. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1160. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1161. }
  1162. const float d = amax / 127;
  1163. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1164. y[ib].qs[iqs] = q;
  1165. if (iqs > 0) {
  1166. return;
  1167. }
  1168. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1169. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1170. }
  1171. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  1172. static __global__ void dequantize_block(const void * __restrict__ vx, float * __restrict__ y, const int k) {
  1173. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1174. if (i >= k) {
  1175. return;
  1176. }
  1177. const int ib = i/qk; // block index
  1178. const int iqs = (i%qk)/qr; // quant index
  1179. const int iybs = i - i%qk; // y block start index
  1180. const int y_offset = qr == 1 ? 1 : qk/2;
  1181. // dequantize
  1182. dfloat2 v;
  1183. dequantize_kernel(vx, ib, iqs, v);
  1184. y[iybs + iqs + 0] = v.x;
  1185. y[iybs + iqs + y_offset] = v.y;
  1186. }
  1187. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1188. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1189. #define VDR_Q4_0_Q8_1_MMVQ 2
  1190. #define VDR_Q4_0_Q8_1_MMQ 4
  1191. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1192. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1193. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1194. int sumi = 0;
  1195. #pragma unroll
  1196. for (int i = 0; i < vdr; ++i) {
  1197. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1198. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1199. // SIMD dot product of quantized values
  1200. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1201. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1202. }
  1203. const float2 ds8f = __half22float2(ds8);
  1204. // second part effectively subtracts 8 from each quant value
  1205. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1206. #else
  1207. assert(false);
  1208. return 0.0f; // only to satisfy the compiler
  1209. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1210. }
  1211. #define VDR_Q4_1_Q8_1_MMVQ 2
  1212. #define VDR_Q4_1_Q8_1_MMQ 4
  1213. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1214. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1215. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1216. int sumi = 0;
  1217. #pragma unroll
  1218. for (int i = 0; i < vdr; ++i) {
  1219. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1220. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1221. // SIMD dot product of quantized values
  1222. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1223. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1224. }
  1225. #ifdef GGML_CUDA_F16
  1226. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1227. const float d4d8 = tmp.x;
  1228. const float m4s8 = tmp.y;
  1229. #else
  1230. const float2 dm4f = __half22float2(dm4);
  1231. const float2 ds8f = __half22float2(ds8);
  1232. const float d4d8 = dm4f.x * ds8f.x;
  1233. const float m4s8 = dm4f.y * ds8f.y;
  1234. #endif // GGML_CUDA_F16
  1235. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1236. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1237. #else
  1238. assert(false);
  1239. return 0.0f; // only to satisfy the compiler
  1240. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1241. }
  1242. #define VDR_Q5_0_Q8_1_MMVQ 2
  1243. #define VDR_Q5_0_Q8_1_MMQ 4
  1244. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1245. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1246. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1247. int sumi = 0;
  1248. #pragma unroll
  1249. for (int i = 0; i < vdr; ++i) {
  1250. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1251. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1252. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1253. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1254. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1255. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1256. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1257. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1258. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1259. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1260. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1261. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1262. }
  1263. const float2 ds8f = __half22float2(ds8);
  1264. // second part effectively subtracts 16 from each quant value
  1265. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1266. #else
  1267. assert(false);
  1268. return 0.0f; // only to satisfy the compiler
  1269. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1270. }
  1271. #define VDR_Q5_1_Q8_1_MMVQ 2
  1272. #define VDR_Q5_1_Q8_1_MMQ 4
  1273. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1274. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1275. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1276. int sumi = 0;
  1277. #pragma unroll
  1278. for (int i = 0; i < vdr; ++i) {
  1279. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1280. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1281. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1282. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1283. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1284. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1285. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1286. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1287. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1288. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1289. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1290. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1291. }
  1292. #ifdef GGML_CUDA_F16
  1293. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1294. const float d5d8 = tmp.x;
  1295. const float m5s8 = tmp.y;
  1296. #else
  1297. const float2 dm5f = __half22float2(dm5);
  1298. const float2 ds8f = __half22float2(ds8);
  1299. const float d5d8 = dm5f.x * ds8f.x;
  1300. const float m5s8 = dm5f.y * ds8f.y;
  1301. #endif // GGML_CUDA_F16
  1302. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1303. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1304. #else
  1305. assert(false);
  1306. return 0.0f; // only to satisfy the compiler
  1307. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1308. }
  1309. #define VDR_Q8_0_Q8_1_MMVQ 2
  1310. #define VDR_Q8_0_Q8_1_MMQ 8
  1311. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1312. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1313. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1314. int sumi = 0;
  1315. #pragma unroll
  1316. for (int i = 0; i < vdr; ++i) {
  1317. // SIMD dot product of quantized values
  1318. sumi = __dp4a(v[i], u[i], sumi);
  1319. }
  1320. return d8_0*d8_1 * sumi;
  1321. #else
  1322. assert(false);
  1323. return 0.0f; // only to satisfy the compiler
  1324. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1325. }
  1326. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1327. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1328. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1329. int sumi = 0;
  1330. #pragma unroll
  1331. for (int i = 0; i < vdr; ++i) {
  1332. // SIMD dot product of quantized values
  1333. sumi = __dp4a(v[i], u[i], sumi);
  1334. }
  1335. #ifdef GGML_CUDA_F16
  1336. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1337. const float d8d8 = tmp.x;
  1338. const float m8s8 = tmp.y;
  1339. #else
  1340. const float2 dm8f = __half22float2(dm8);
  1341. const float2 ds8f = __half22float2(ds8);
  1342. const float d8d8 = dm8f.x * ds8f.x;
  1343. const float m8s8 = dm8f.y * ds8f.y;
  1344. #endif // GGML_CUDA_F16
  1345. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1346. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1347. #else
  1348. assert(false);
  1349. return 0.0f; // only to satisfy the compiler
  1350. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1351. }
  1352. #define VDR_Q2_K_Q8_1_MMVQ 1
  1353. #define VDR_Q2_K_Q8_1_MMQ 2
  1354. // contiguous v/x values
  1355. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1356. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1357. const half2 & dm2, const float * __restrict__ d8) {
  1358. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1359. float sumf_d = 0.0f;
  1360. float sumf_m = 0.0f;
  1361. #pragma unroll
  1362. for (int i = 0; i < QR2_K; ++i) {
  1363. const int sc = scales[2*i];
  1364. const int vi = (v >> (2*i)) & 0x03030303;
  1365. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1366. // fill int with 4x m
  1367. int m = sc >> 4;
  1368. m |= m << 8;
  1369. m |= m << 16;
  1370. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1371. }
  1372. const float2 dm2f = __half22float2(dm2);
  1373. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1374. #else
  1375. assert(false);
  1376. return 0.0f; // only to satisfy the compiler
  1377. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1378. }
  1379. // contiguous u/y values
  1380. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1381. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1382. const half2 & dm2, const float & d8) {
  1383. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1384. int sumi_d = 0;
  1385. int sumi_m = 0;
  1386. #pragma unroll
  1387. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1388. int sumi_d_sc = 0;
  1389. const int sc = scales[i0 / (QI8_1/2)];
  1390. // fill int with 4x m
  1391. int m = sc >> 4;
  1392. m |= m << 8;
  1393. m |= m << 16;
  1394. #pragma unroll
  1395. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1396. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1397. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1398. }
  1399. sumi_d += sumi_d_sc * (sc & 0xF);
  1400. }
  1401. const float2 dm2f = __half22float2(dm2);
  1402. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1403. #else
  1404. assert(false);
  1405. return 0.0f; // only to satisfy the compiler
  1406. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1407. }
  1408. #define VDR_Q3_K_Q8_1_MMVQ 1
  1409. #define VDR_Q3_K_Q8_1_MMQ 2
  1410. // contiguous v/x values
  1411. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1412. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1413. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1414. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1415. float sumf = 0.0f;
  1416. #pragma unroll
  1417. for (int i = 0; i < QR3_K; ++i) {
  1418. const int isc = scale_offset + 2*i;
  1419. const int isc_low = isc % (QK_K/32);
  1420. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1421. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1422. const int isc_high = isc % (QK_K/64);
  1423. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1424. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1425. const int sc = (sc_low | sc_high) - 32;
  1426. const int vil = (vl >> (2*i)) & 0x03030303;
  1427. const int vih = ((vh >> i) << 2) & 0x04040404;
  1428. const int vi = __vsubss4(vil, vih);
  1429. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1430. }
  1431. return d3 * sumf;
  1432. #else
  1433. assert(false);
  1434. return 0.0f; // only to satisfy the compiler
  1435. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1436. }
  1437. // contiguous u/y values
  1438. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1439. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1440. const float & d3, const float & d8) {
  1441. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1442. int sumi = 0;
  1443. #pragma unroll
  1444. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1445. int sumi_sc = 0;
  1446. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1447. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1448. }
  1449. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1450. }
  1451. return d3*d8 * sumi;
  1452. #else
  1453. assert(false);
  1454. return 0.0f; // only to satisfy the compiler
  1455. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1456. }
  1457. #define VDR_Q4_K_Q8_1_MMVQ 2
  1458. #define VDR_Q4_K_Q8_1_MMQ 8
  1459. // contiguous v/x values
  1460. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1461. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1462. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1463. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1464. float sumf_d = 0.0f;
  1465. float sumf_m = 0.0f;
  1466. #pragma unroll
  1467. for (int i = 0; i < QR4_K; ++i) {
  1468. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1469. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1470. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1471. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1472. sumf_d += d8[i] * (dot1 * sc[i]);
  1473. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1474. }
  1475. const float2 dm4f = __half22float2(dm4);
  1476. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1477. #else
  1478. assert(false);
  1479. return 0.0f; // only to satisfy the compiler
  1480. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1481. }
  1482. // contiguous u/y values
  1483. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1484. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1485. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1486. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1487. float sumf_d = 0.0f;
  1488. float sumf_m = 0.0f;
  1489. #pragma unroll
  1490. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1491. int sumi_d = 0;
  1492. #pragma unroll
  1493. for (int j = 0; j < QI8_1; ++j) {
  1494. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1495. }
  1496. const float2 ds8f = __half22float2(ds8[i]);
  1497. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1498. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1499. }
  1500. const float2 dm4f = __half22float2(dm4);
  1501. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1502. #else
  1503. assert(false);
  1504. return 0.0f; // only to satisfy the compiler
  1505. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1506. }
  1507. #define VDR_Q5_K_Q8_1_MMVQ 2
  1508. #define VDR_Q5_K_Q8_1_MMQ 8
  1509. // contiguous v/x values
  1510. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1511. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1512. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1513. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1514. float sumf_d = 0.0f;
  1515. float sumf_m = 0.0f;
  1516. #pragma unroll
  1517. for (int i = 0; i < QR5_K; ++i) {
  1518. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1519. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1520. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1521. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1522. const int v0i = vl0i | vh0i;
  1523. const int v1i = vl1i | vh1i;
  1524. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1525. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1526. sumf_d += d8[i] * (dot1 * sc[i]);
  1527. sumf_m += d8[i] * (dot2 * m[i]);
  1528. }
  1529. const float2 dm5f = __half22float2(dm5);
  1530. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1531. #else
  1532. assert(false);
  1533. return 0.0f; // only to satisfy the compiler
  1534. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1535. }
  1536. // contiguous u/y values
  1537. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1538. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1539. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1540. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1541. float sumf_d = 0.0f;
  1542. float sumf_m = 0.0f;
  1543. #pragma unroll
  1544. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1545. int sumi_d = 0;
  1546. #pragma unroll
  1547. for (int j = 0; j < QI8_1; ++j) {
  1548. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1549. }
  1550. const float2 ds8f = __half22float2(ds8[i]);
  1551. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1552. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1553. }
  1554. const float2 dm4f = __half22float2(dm4);
  1555. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1556. #else
  1557. assert(false);
  1558. return 0.0f; // only to satisfy the compiler
  1559. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1560. }
  1561. #define VDR_Q6_K_Q8_1_MMVQ 1
  1562. #define VDR_Q6_K_Q8_1_MMQ 8
  1563. // contiguous v/x values
  1564. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1565. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1566. const float & d, const float * __restrict__ d8) {
  1567. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1568. float sumf = 0.0f;
  1569. #pragma unroll
  1570. for (int i = 0; i < QR6_K; ++i) {
  1571. const int sc = scales[4*i];
  1572. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1573. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1574. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1575. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1576. }
  1577. return d*sumf;
  1578. #else
  1579. assert(false);
  1580. return 0.0f; // only to satisfy the compiler
  1581. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1582. }
  1583. // contiguous u/y values
  1584. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  1585. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  1586. const float & d6, const float * __restrict__ d8) {
  1587. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1588. float sumf_d = 0.0f;
  1589. #pragma unroll
  1590. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  1591. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  1592. #pragma unroll
  1593. for (int i = i0; i < i0 + 2; ++i) {
  1594. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  1595. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  1596. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  1597. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  1598. }
  1599. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  1600. }
  1601. return d6 * sumf_d;
  1602. #else
  1603. assert(false);
  1604. return 0.0f; // only to satisfy the compiler
  1605. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1606. }
  1607. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  1608. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1609. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  1610. int v[VDR_Q4_0_Q8_1_MMVQ];
  1611. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  1612. #pragma unroll
  1613. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  1614. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  1615. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1616. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  1617. }
  1618. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  1619. }
  1620. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1621. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  1622. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  1623. *x_ql = tile_x_qs;
  1624. *x_dm = (half2 *) tile_x_d;
  1625. }
  1626. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  1627. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1628. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1629. __builtin_assume(i_offset >= 0);
  1630. __builtin_assume(i_offset < nwarps);
  1631. __builtin_assume(k >= 0);
  1632. __builtin_assume(k < WARP_SIZE);
  1633. const int kbx = k / QI4_0;
  1634. const int kqsx = k % QI4_0;
  1635. const block_q4_0 * bx0 = (block_q4_0 *) vx;
  1636. float * x_dmf = (float *) x_dm;
  1637. #pragma unroll
  1638. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1639. int i = i0 + i_offset;
  1640. if (need_check) {
  1641. i = min(i, i_max);
  1642. }
  1643. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1644. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  1645. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  1646. }
  1647. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  1648. const int kbxd = k % blocks_per_tile_x_row;
  1649. #pragma unroll
  1650. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  1651. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  1652. if (need_check) {
  1653. i = min(i, i_max);
  1654. }
  1655. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1656. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  1657. }
  1658. }
  1659. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  1660. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1661. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1662. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1663. const float * x_dmf = (float *) x_dm;
  1664. int u[2*VDR_Q4_0_Q8_1_MMQ];
  1665. #pragma unroll
  1666. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  1667. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1668. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  1669. }
  1670. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  1671. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  1672. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1673. }
  1674. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  1675. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1676. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  1677. int v[VDR_Q4_1_Q8_1_MMVQ];
  1678. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  1679. #pragma unroll
  1680. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  1681. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  1682. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1683. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  1684. }
  1685. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  1686. }
  1687. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1688. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  1689. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  1690. *x_ql = tile_x_qs;
  1691. *x_dm = tile_x_dm;
  1692. }
  1693. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  1694. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1695. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1696. __builtin_assume(i_offset >= 0);
  1697. __builtin_assume(i_offset < nwarps);
  1698. __builtin_assume(k >= 0);
  1699. __builtin_assume(k < WARP_SIZE);
  1700. const int kbx = k / QI4_1;
  1701. const int kqsx = k % QI4_1;
  1702. const block_q4_1 * bx0 = (block_q4_1 *) vx;
  1703. #pragma unroll
  1704. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1705. int i = i0 + i_offset;
  1706. if (need_check) {
  1707. i = min(i, i_max);
  1708. }
  1709. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  1710. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1711. }
  1712. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  1713. const int kbxd = k % blocks_per_tile_x_row;
  1714. #pragma unroll
  1715. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  1716. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  1717. if (need_check) {
  1718. i = min(i, i_max);
  1719. }
  1720. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  1721. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  1722. }
  1723. }
  1724. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  1725. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1726. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1727. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1728. int u[2*VDR_Q4_1_Q8_1_MMQ];
  1729. #pragma unroll
  1730. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  1731. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1732. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  1733. }
  1734. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  1735. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  1736. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1737. }
  1738. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  1739. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1740. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  1741. int vl[VDR_Q5_0_Q8_1_MMVQ];
  1742. int vh[VDR_Q5_0_Q8_1_MMVQ];
  1743. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  1744. #pragma unroll
  1745. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  1746. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  1747. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  1748. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1749. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  1750. }
  1751. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  1752. }
  1753. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1754. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  1755. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  1756. *x_ql = tile_x_ql;
  1757. *x_dm = (half2 *) tile_x_d;
  1758. }
  1759. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  1760. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1761. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1762. __builtin_assume(i_offset >= 0);
  1763. __builtin_assume(i_offset < nwarps);
  1764. __builtin_assume(k >= 0);
  1765. __builtin_assume(k < WARP_SIZE);
  1766. const int kbx = k / QI5_0;
  1767. const int kqsx = k % QI5_0;
  1768. const block_q5_0 * bx0 = (block_q5_0 *) vx;
  1769. #pragma unroll
  1770. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1771. int i = i0 + i_offset;
  1772. if (need_check) {
  1773. i = min(i, i_max);
  1774. }
  1775. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1776. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  1777. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  1778. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  1779. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  1780. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  1781. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  1782. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  1783. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  1784. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  1785. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  1786. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  1787. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  1788. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  1789. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  1790. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  1791. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  1792. }
  1793. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  1794. const int kbxd = k % blocks_per_tile_x_row;
  1795. float * x_dmf = (float *) x_dm;
  1796. #pragma unroll
  1797. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  1798. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  1799. if (need_check) {
  1800. i = min(i, i_max);
  1801. }
  1802. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1803. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  1804. }
  1805. }
  1806. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  1807. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1808. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1809. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1810. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  1811. const float * x_dmf = (const float *) x_dm;
  1812. const float * y_df = (const float *) y_ds;
  1813. int u[2*VDR_Q5_0_Q8_1_MMQ];
  1814. #pragma unroll
  1815. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  1816. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1817. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  1818. }
  1819. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  1820. (&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)]);
  1821. }
  1822. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  1823. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1824. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  1825. int vl[VDR_Q5_1_Q8_1_MMVQ];
  1826. int vh[VDR_Q5_1_Q8_1_MMVQ];
  1827. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  1828. #pragma unroll
  1829. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  1830. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  1831. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  1832. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1833. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  1834. }
  1835. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  1836. }
  1837. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1838. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  1839. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  1840. *x_ql = tile_x_ql;
  1841. *x_dm = tile_x_dm;
  1842. }
  1843. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  1844. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1845. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1846. __builtin_assume(i_offset >= 0);
  1847. __builtin_assume(i_offset < nwarps);
  1848. __builtin_assume(k >= 0);
  1849. __builtin_assume(k < WARP_SIZE);
  1850. const int kbx = k / QI5_1;
  1851. const int kqsx = k % QI5_1;
  1852. const block_q5_1 * bx0 = (block_q5_1 *) vx;
  1853. #pragma unroll
  1854. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1855. int i = i0 + i_offset;
  1856. if (need_check) {
  1857. i = min(i, i_max);
  1858. }
  1859. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  1860. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1861. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  1862. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  1863. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  1864. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  1865. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  1866. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  1867. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  1868. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  1869. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  1870. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  1871. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  1872. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  1873. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  1874. }
  1875. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  1876. const int kbxd = k % blocks_per_tile_x_row;
  1877. #pragma unroll
  1878. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  1879. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  1880. if (need_check) {
  1881. i = min(i, i_max);
  1882. }
  1883. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  1884. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  1885. }
  1886. }
  1887. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  1888. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1889. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1890. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1891. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  1892. int u[2*VDR_Q5_1_Q8_1_MMQ];
  1893. #pragma unroll
  1894. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  1895. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1896. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  1897. }
  1898. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  1899. (&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)]);
  1900. }
  1901. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  1902. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1903. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  1904. int v[VDR_Q8_0_Q8_1_MMVQ];
  1905. int u[VDR_Q8_0_Q8_1_MMVQ];
  1906. #pragma unroll
  1907. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  1908. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  1909. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1910. }
  1911. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  1912. }
  1913. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1914. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  1915. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  1916. *x_ql = tile_x_qs;
  1917. *x_dm = (half2 *) tile_x_d;
  1918. }
  1919. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  1920. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1921. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1922. __builtin_assume(i_offset >= 0);
  1923. __builtin_assume(i_offset < nwarps);
  1924. __builtin_assume(k >= 0);
  1925. __builtin_assume(k < WARP_SIZE);
  1926. const int kbx = k / QI8_0;
  1927. const int kqsx = k % QI8_0;
  1928. float * x_dmf = (float *) x_dm;
  1929. const block_q8_0 * bx0 = (block_q8_0 *) vx;
  1930. #pragma unroll
  1931. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1932. int i = i0 + i_offset;
  1933. if (need_check) {
  1934. i = min(i, i_max);
  1935. }
  1936. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1937. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  1938. }
  1939. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  1940. const int kbxd = k % blocks_per_tile_x_row;
  1941. #pragma unroll
  1942. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  1943. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  1944. if (need_check) {
  1945. i = min(i, i_max);
  1946. }
  1947. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1948. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  1949. }
  1950. }
  1951. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  1952. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1953. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1954. const float * x_dmf = (const float *) x_dm;
  1955. const float * y_df = (const float *) y_ds;
  1956. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  1957. (&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],
  1958. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  1959. }
  1960. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  1961. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1962. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  1963. const int bq8_offset = QR2_K * (iqs / QI8_1);
  1964. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  1965. const uint8_t * scales = bq2_K->scales + scale_offset;
  1966. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  1967. int u[QR2_K];
  1968. float d8[QR2_K];
  1969. #pragma unroll
  1970. for (int i = 0; i < QR2_K; ++ i) {
  1971. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  1972. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  1973. }
  1974. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  1975. }
  1976. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1977. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  1978. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  1979. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  1980. *x_ql = tile_x_ql;
  1981. *x_dm = tile_x_dm;
  1982. *x_sc = tile_x_sc;
  1983. }
  1984. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  1985. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1986. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1987. __builtin_assume(i_offset >= 0);
  1988. __builtin_assume(i_offset < nwarps);
  1989. __builtin_assume(k >= 0);
  1990. __builtin_assume(k < WARP_SIZE);
  1991. const int kbx = k / QI2_K;
  1992. const int kqsx = k % QI2_K;
  1993. const block_q2_K * bx0 = (block_q2_K *) vx;
  1994. #pragma unroll
  1995. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1996. int i = i0 + i_offset;
  1997. if (need_check) {
  1998. i = min(i, i_max);
  1999. }
  2000. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2001. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2002. }
  2003. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2004. const int kbxd = k % blocks_per_tile_x_row;
  2005. #pragma unroll
  2006. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2007. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2008. if (need_check) {
  2009. i = min(i, i_max);
  2010. }
  2011. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2012. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2013. }
  2014. #pragma unroll
  2015. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2016. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2017. if (need_check) {
  2018. i = min(i, i_max);
  2019. }
  2020. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2021. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2022. }
  2023. }
  2024. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2025. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2026. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2027. const int kbx = k / QI2_K;
  2028. const int ky = (k % QI2_K) * QR2_K;
  2029. const float * y_df = (const float *) y_ds;
  2030. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2031. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2032. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2033. #pragma unroll
  2034. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2035. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2036. }
  2037. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2038. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2039. 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]);
  2040. }
  2041. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2042. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2043. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2044. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2045. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2046. const float d = bq3_K->d;
  2047. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2048. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2049. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2050. int u[QR3_K];
  2051. float d8[QR3_K];
  2052. #pragma unroll
  2053. for (int i = 0; i < QR3_K; ++i) {
  2054. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2055. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2056. }
  2057. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2058. }
  2059. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2060. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2061. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2062. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2063. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2064. *x_ql = tile_x_ql;
  2065. *x_dm = tile_x_dm;
  2066. *x_qh = tile_x_qh;
  2067. *x_sc = tile_x_sc;
  2068. }
  2069. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2070. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2071. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2072. __builtin_assume(i_offset >= 0);
  2073. __builtin_assume(i_offset < nwarps);
  2074. __builtin_assume(k >= 0);
  2075. __builtin_assume(k < WARP_SIZE);
  2076. const int kbx = k / QI3_K;
  2077. const int kqsx = k % QI3_K;
  2078. const block_q3_K * bx0 = (block_q3_K *) vx;
  2079. #pragma unroll
  2080. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2081. int i = i0 + i_offset;
  2082. if (need_check) {
  2083. i = min(i, i_max);
  2084. }
  2085. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2086. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2087. }
  2088. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2089. const int kbxd = k % blocks_per_tile_x_row;
  2090. float * x_dmf = (float *) x_dm;
  2091. #pragma unroll
  2092. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2093. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2094. if (need_check) {
  2095. i = min(i, i_max);
  2096. }
  2097. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2098. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2099. }
  2100. #pragma unroll
  2101. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2102. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2103. if (need_check) {
  2104. i = min(i, i_max);
  2105. }
  2106. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2107. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2108. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2109. }
  2110. #pragma unroll
  2111. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2112. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2113. if (need_check) {
  2114. i = min(i, i_max);
  2115. }
  2116. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2117. const int ksc = k % (QI3_K/4);
  2118. const int ksc_low = ksc % (QI3_K/8);
  2119. const int shift_low = 4 * (ksc / (QI3_K/8));
  2120. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2121. const int ksc_high = QI3_K/8;
  2122. const int shift_high = 2 * ksc;
  2123. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2124. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2125. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2126. }
  2127. }
  2128. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2129. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2130. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2131. const int kbx = k / QI3_K;
  2132. const int ky = (k % QI3_K) * QR3_K;
  2133. const float * x_dmf = (const float *) x_dm;
  2134. const float * y_df = (const float *) y_ds;
  2135. const int8_t * scales = ((int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2136. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2137. #pragma unroll
  2138. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2139. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2140. const int shift = 2 * ((ky % 32) / 8);
  2141. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2142. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2143. const int vlh = (vh << 2) & 0x04040404;
  2144. v[l] = __vsubss4(vll, vlh);
  2145. }
  2146. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2147. 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]);
  2148. }
  2149. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2150. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2151. #ifndef GGML_QKK_64
  2152. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2153. int v[2];
  2154. int u[2*QR4_K];
  2155. float d8[QR4_K];
  2156. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2157. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2158. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2159. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2160. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2161. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2162. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2163. v[0] = q4[0];
  2164. v[1] = q4[4];
  2165. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2166. uint16_t aux[2];
  2167. const int j = bq8_offset/2;
  2168. if (j < 2) {
  2169. aux[0] = scales[j+0] & 0x3f3f;
  2170. aux[1] = scales[j+2] & 0x3f3f;
  2171. } else {
  2172. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2173. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2174. }
  2175. const uint8_t * sc = (const uint8_t *)aux;
  2176. const uint8_t * m = sc + 2;
  2177. for (int i = 0; i < QR4_K; ++i) {
  2178. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2179. d8[i] = __low2half(bq8i->ds);
  2180. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2181. u[2*i+0] = q8[0];
  2182. u[2*i+1] = q8[4];
  2183. }
  2184. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2185. #else
  2186. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2187. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2188. float sumf_d = 0.0f;
  2189. float sumf_m = 0.0f;
  2190. uint16_t aux16[2];
  2191. const uint8_t * s = (const uint8_t *)aux16;
  2192. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2193. aux16[0] = a[0] & 0x0f0f;
  2194. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2195. const float dall = bq4_K->d[0];
  2196. const float dmin = bq4_K->d[1];
  2197. const float d8_1 = __low2float(bq8_1[0].ds);
  2198. const float d8_2 = __low2float(bq8_1[1].ds);
  2199. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2200. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2201. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2202. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2203. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2204. const int v1 = q4[0];
  2205. const int v2 = q4[4];
  2206. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2207. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2208. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2209. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2210. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2211. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2212. return dall * sumf_d - dmin * sumf_m;
  2213. #else
  2214. assert(false);
  2215. return 0.0f; // only to satisfy the compiler
  2216. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2217. #endif
  2218. }
  2219. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2220. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2221. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2222. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2223. *x_ql = tile_x_ql;
  2224. *x_dm = tile_x_dm;
  2225. *x_sc = tile_x_sc;
  2226. }
  2227. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2228. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2229. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2230. __builtin_assume(i_offset >= 0);
  2231. __builtin_assume(i_offset < nwarps);
  2232. __builtin_assume(k >= 0);
  2233. __builtin_assume(k < WARP_SIZE);
  2234. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2235. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2236. const block_q4_K * bx0 = (block_q4_K *) vx;
  2237. #pragma unroll
  2238. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2239. int i = i0 + i_offset;
  2240. if (need_check) {
  2241. i = min(i, i_max);
  2242. }
  2243. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2244. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2245. }
  2246. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2247. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2248. #pragma unroll
  2249. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2250. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2251. if (need_check) {
  2252. i = min(i, i_max);
  2253. }
  2254. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2255. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2256. }
  2257. #pragma unroll
  2258. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2259. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2260. if (need_check) {
  2261. i = min(i, i_max);
  2262. }
  2263. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2264. const int * scales = (int *) bxi->scales;
  2265. const int ksc = k % (WARP_SIZE/8);
  2266. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2267. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2268. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2269. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2270. }
  2271. }
  2272. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2273. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2274. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2275. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2276. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2277. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2278. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2279. }
  2280. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2281. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2282. #ifndef GGML_QKK_64
  2283. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2284. int vl[2];
  2285. int vh[2];
  2286. int u[2*QR5_K];
  2287. float d8[QR5_K];
  2288. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2289. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2290. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2291. vl[0] = ql[0];
  2292. vl[1] = ql[4];
  2293. vh[0] = qh[0] >> bq8_offset;
  2294. vh[1] = qh[4] >> bq8_offset;
  2295. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2296. uint16_t aux[2];
  2297. const int j = bq8_offset/2;
  2298. if (j < 2) {
  2299. aux[0] = scales[j+0] & 0x3f3f;
  2300. aux[1] = scales[j+2] & 0x3f3f;
  2301. } else {
  2302. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2303. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2304. }
  2305. const uint8_t * sc = (const uint8_t *)aux;
  2306. const uint8_t * m = sc + 2;
  2307. #pragma unroll
  2308. for (int i = 0; i < QR5_K; ++i) {
  2309. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2310. d8[i] = __low2float(bq8i->ds);
  2311. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2312. u[2*i+0] = q8[0];
  2313. u[2*i+1] = q8[4];
  2314. }
  2315. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2316. #else
  2317. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2318. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2319. const int8_t * s = bq5_K->scales;
  2320. const float d = bq5_K->d;
  2321. const float d8_1 = __low2half(bq8_1[0].ds);
  2322. const float d8_2 = __low2half(bq8_1[1].ds);
  2323. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2324. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2325. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2326. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2327. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2328. const int vl1 = ql[0];
  2329. const int vl2 = ql[4];
  2330. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2331. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2332. const int in = step%8; // 0, 4, 0, 4
  2333. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2334. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2335. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2336. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2337. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2338. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2339. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2340. return d * sumf_d;
  2341. #else
  2342. assert(false);
  2343. return 0.0f; // only to satisfy the compiler
  2344. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2345. #endif
  2346. }
  2347. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2348. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2349. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2350. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2351. *x_ql = tile_x_ql;
  2352. *x_dm = tile_x_dm;
  2353. *x_sc = tile_x_sc;
  2354. }
  2355. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2356. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2357. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2358. __builtin_assume(i_offset >= 0);
  2359. __builtin_assume(i_offset < nwarps);
  2360. __builtin_assume(k >= 0);
  2361. __builtin_assume(k < WARP_SIZE);
  2362. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2363. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2364. const block_q5_K * bx0 = (block_q5_K *) vx;
  2365. #pragma unroll
  2366. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2367. int i = i0 + i_offset;
  2368. if (need_check) {
  2369. i = min(i, i_max);
  2370. }
  2371. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2372. const int ky = QR5_K*kqsx;
  2373. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2374. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2375. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2376. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2377. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2378. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2379. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2380. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2381. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2382. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2383. }
  2384. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2385. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2386. #pragma unroll
  2387. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2388. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2389. if (need_check) {
  2390. i = min(i, i_max);
  2391. }
  2392. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2393. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2394. }
  2395. #pragma unroll
  2396. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2397. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2398. if (need_check) {
  2399. i = min(i, i_max);
  2400. }
  2401. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2402. const int * scales = (int *) bxi->scales;
  2403. const int ksc = k % (WARP_SIZE/8);
  2404. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2405. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2406. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2407. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2408. }
  2409. }
  2410. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2411. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2412. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2413. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2414. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2415. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2416. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2417. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2418. }
  2419. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2420. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2421. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2422. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2423. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2424. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2425. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2426. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2427. const int8_t * scales = bq6_K->scales + scale_offset;
  2428. int u[QR6_K];
  2429. float d8[QR6_K];
  2430. #pragma unroll
  2431. for (int i = 0; i < QR6_K; ++i) {
  2432. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2433. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2434. }
  2435. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2436. }
  2437. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2438. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2439. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2440. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2441. *x_ql = tile_x_ql;
  2442. *x_dm = tile_x_dm;
  2443. *x_sc = tile_x_sc;
  2444. }
  2445. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2446. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2447. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2448. __builtin_assume(i_offset >= 0);
  2449. __builtin_assume(i_offset < nwarps);
  2450. __builtin_assume(k >= 0);
  2451. __builtin_assume(k < WARP_SIZE);
  2452. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2453. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2454. const block_q6_K * bx0 = (block_q6_K *) vx;
  2455. #pragma unroll
  2456. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2457. int i = i0 + i_offset;
  2458. if (need_check) {
  2459. i = min(i, i_max);
  2460. }
  2461. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2462. const int ky = QR6_K*kqsx;
  2463. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2464. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2465. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2466. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2467. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2468. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2469. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2470. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2471. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2472. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2473. }
  2474. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2475. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2476. float * x_dmf = (float *) x_dm;
  2477. #pragma unroll
  2478. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2479. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2480. if (need_check) {
  2481. i = min(i, i_max);
  2482. }
  2483. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2484. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2485. }
  2486. #pragma unroll
  2487. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2488. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2489. if (need_check) {
  2490. i = min(i, i_max);
  2491. }
  2492. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2493. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2494. }
  2495. }
  2496. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2497. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2498. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2499. const float * x_dmf = (const float *) x_dm;
  2500. const float * y_df = (const float *) y_ds;
  2501. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2502. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2503. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2504. 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]);
  2505. }
  2506. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2507. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2508. static __device__ __forceinline__ void mul_mat_q(
  2509. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2510. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2511. const block_q_t * x = (const block_q_t *) vx;
  2512. const block_q8_1 * y = (const block_q8_1 *) vy;
  2513. const int blocks_per_row_x = ncols_x / qk;
  2514. const int blocks_per_col_y = nrows_y / QK8_1;
  2515. const int blocks_per_warp = WARP_SIZE / qi;
  2516. const int & ncols_dst = ncols_y;
  2517. const int row_dst_0 = blockIdx.x*mmq_y;
  2518. const int & row_x_0 = row_dst_0;
  2519. const int col_dst_0 = blockIdx.y*mmq_x;
  2520. const int & col_y_0 = col_dst_0;
  2521. int * tile_x_ql = nullptr;
  2522. half2 * tile_x_dm = nullptr;
  2523. int * tile_x_qh = nullptr;
  2524. int * tile_x_sc = nullptr;
  2525. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2526. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2527. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2528. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {0.0f};
  2529. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2530. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2531. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2532. #pragma unroll
  2533. for (int ir = 0; ir < qr; ++ir) {
  2534. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2535. const int kbxd = kqs / QI8_1;
  2536. #pragma unroll
  2537. for (int i = 0; i < mmq_x; i += nwarps) {
  2538. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2539. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2540. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2541. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2542. }
  2543. #pragma unroll
  2544. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2545. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2546. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2547. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2548. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2549. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  2550. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  2551. if (need_sum) {
  2552. *dsi_dst = *dsi_src;
  2553. } else {
  2554. float * dfi_dst = (float *) dsi_dst;
  2555. *dfi_dst = __low2half(*dsi_src);
  2556. }
  2557. }
  2558. __syncthreads();
  2559. // #pragma unroll // unrolling this loop causes too much register pressure
  2560. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  2561. #pragma unroll
  2562. for (int j = 0; j < mmq_x; j += nwarps) {
  2563. #pragma unroll
  2564. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2565. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  2566. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  2567. threadIdx.x + i, threadIdx.y + j, k);
  2568. }
  2569. }
  2570. }
  2571. __syncthreads();
  2572. }
  2573. }
  2574. #pragma unroll
  2575. for (int j = 0; j < mmq_x; j += nwarps) {
  2576. const int col_dst = col_dst_0 + j + threadIdx.y;
  2577. if (col_dst >= ncols_dst) {
  2578. return;
  2579. }
  2580. #pragma unroll
  2581. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2582. const int row_dst = row_dst_0 + threadIdx.x + i;
  2583. if (row_dst >= nrows_dst) {
  2584. continue;
  2585. }
  2586. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  2587. }
  2588. }
  2589. }
  2590. #define MMQ_X_Q4_0_AMPERE 64
  2591. #define MMQ_Y_Q4_0_AMPERE 128
  2592. #define NWARPS_Q4_0_AMPERE 4
  2593. #define MMQ_X_Q4_0_PASCAL 64
  2594. #define MMQ_Y_Q4_0_PASCAL 64
  2595. #define NWARPS_Q4_0_PASCAL 8
  2596. template <bool need_check> static __global__ void mul_mat_q4_0(
  2597. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2598. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2599. #if __CUDA_ARCH__ >= CC_TURING
  2600. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  2601. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  2602. const int nwarps = NWARPS_Q4_0_AMPERE;
  2603. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2604. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2605. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2606. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2607. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  2608. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  2609. const int nwarps = NWARPS_Q4_0_PASCAL;
  2610. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2611. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2612. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2613. #else
  2614. (void) vec_dot_q4_0_q8_1_mul_mat;
  2615. assert(false);
  2616. #endif // __CUDA_ARCH__ >= CC_TURING
  2617. }
  2618. #define MMQ_X_Q4_1_AMPERE 64
  2619. #define MMQ_Y_Q4_1_AMPERE 128
  2620. #define NWARPS_Q4_1_AMPERE 4
  2621. #define MMQ_X_Q4_1_PASCAL 64
  2622. #define MMQ_Y_Q4_1_PASCAL 64
  2623. #define NWARPS_Q4_1_PASCAL 8
  2624. template <bool need_check> static __global__ void
  2625. #if __CUDA_ARCH__ < CC_TURING
  2626. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  2627. #endif // __CUDA_ARCH__ < CC_TURING
  2628. mul_mat_q4_1(
  2629. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2630. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2631. #if __CUDA_ARCH__ >= CC_TURING
  2632. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  2633. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  2634. const int nwarps = NWARPS_Q4_1_AMPERE;
  2635. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2636. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2637. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2638. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2639. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  2640. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  2641. const int nwarps = NWARPS_Q4_1_PASCAL;
  2642. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2643. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2644. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2645. #else
  2646. (void) vec_dot_q4_1_q8_1_mul_mat;
  2647. assert(false);
  2648. #endif // __CUDA_ARCH__ >= CC_TURING
  2649. }
  2650. #define MMQ_X_Q5_0_AMPERE 128
  2651. #define MMQ_Y_Q5_0_AMPERE 64
  2652. #define NWARPS_Q5_0_AMPERE 4
  2653. #define MMQ_X_Q5_0_PASCAL 64
  2654. #define MMQ_Y_Q5_0_PASCAL 64
  2655. #define NWARPS_Q5_0_PASCAL 8
  2656. template <bool need_check> static __global__ void mul_mat_q5_0(
  2657. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2658. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2659. #if __CUDA_ARCH__ >= CC_TURING
  2660. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  2661. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  2662. const int nwarps = NWARPS_Q5_0_AMPERE;
  2663. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2664. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2665. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2666. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2667. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  2668. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  2669. const int nwarps = NWARPS_Q5_0_PASCAL;
  2670. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2671. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2672. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2673. #else
  2674. (void) vec_dot_q5_0_q8_1_mul_mat;
  2675. assert(false);
  2676. #endif // __CUDA_ARCH__ >= CC_TURING
  2677. }
  2678. #define MMQ_X_Q5_1_AMPERE 128
  2679. #define MMQ_Y_Q5_1_AMPERE 64
  2680. #define NWARPS_Q5_1_AMPERE 4
  2681. #define MMQ_X_Q5_1_PASCAL 64
  2682. #define MMQ_Y_Q5_1_PASCAL 64
  2683. #define NWARPS_Q5_1_PASCAL 8
  2684. template <bool need_check> static __global__ void mul_mat_q5_1(
  2685. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2686. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2687. #if __CUDA_ARCH__ >= CC_TURING
  2688. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  2689. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  2690. const int nwarps = NWARPS_Q5_1_AMPERE;
  2691. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2692. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  2693. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2694. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2695. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  2696. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  2697. const int nwarps = NWARPS_Q5_1_PASCAL;
  2698. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2699. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  2700. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2701. #else
  2702. (void) vec_dot_q5_1_q8_1_mul_mat;
  2703. assert(false);
  2704. #endif // __CUDA_ARCH__ >= CC_TURING
  2705. }
  2706. #define MMQ_X_Q8_0_AMPERE 128
  2707. #define MMQ_Y_Q8_0_AMPERE 64
  2708. #define NWARPS_Q8_0_AMPERE 4
  2709. #define MMQ_X_Q8_0_PASCAL 64
  2710. #define MMQ_Y_Q8_0_PASCAL 64
  2711. #define NWARPS_Q8_0_PASCAL 8
  2712. template <bool need_check> static __global__ void mul_mat_q8_0(
  2713. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2714. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2715. #if __CUDA_ARCH__ >= CC_TURING
  2716. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  2717. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  2718. const int nwarps = NWARPS_Q8_0_AMPERE;
  2719. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  2720. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  2721. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2722. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2723. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  2724. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  2725. const int nwarps = NWARPS_Q8_0_PASCAL;
  2726. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  2727. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  2728. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2729. #else
  2730. (void) vec_dot_q8_0_q8_1_mul_mat;
  2731. assert(false);
  2732. #endif // __CUDA_ARCH__ >= CC_TURING
  2733. }
  2734. #define MMQ_X_Q2_K_AMPERE 64
  2735. #define MMQ_Y_Q2_K_AMPERE 128
  2736. #define NWARPS_Q2_K_AMPERE 4
  2737. #define MMQ_X_Q2_K_PASCAL 64
  2738. #define MMQ_Y_Q2_K_PASCAL 64
  2739. #define NWARPS_Q2_K_PASCAL 8
  2740. template <bool need_check> static __global__ void mul_mat_q2_K(
  2741. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2742. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2743. #if __CUDA_ARCH__ >= CC_TURING
  2744. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  2745. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  2746. const int nwarps = NWARPS_Q2_K_AMPERE;
  2747. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  2748. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  2749. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2750. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2751. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  2752. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  2753. const int nwarps = NWARPS_Q2_K_PASCAL;
  2754. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  2755. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  2756. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2757. #else
  2758. (void) vec_dot_q2_K_q8_1_mul_mat;
  2759. assert(false);
  2760. #endif // __CUDA_ARCH__ >= CC_TURING
  2761. }
  2762. #define MMQ_X_Q3_K_AMPERE 128
  2763. #define MMQ_Y_Q3_K_AMPERE 128
  2764. #define NWARPS_Q3_K_AMPERE 4
  2765. #define MMQ_X_Q3_K_PASCAL 64
  2766. #define MMQ_Y_Q3_K_PASCAL 64
  2767. #define NWARPS_Q3_K_PASCAL 8
  2768. template <bool need_check> static __global__ void
  2769. #if __CUDA_ARCH__ < CC_TURING
  2770. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  2771. #endif // __CUDA_ARCH__ < CC_TURING
  2772. mul_mat_q3_K(
  2773. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2774. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2775. #if __CUDA_ARCH__ >= CC_TURING
  2776. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  2777. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  2778. const int nwarps = NWARPS_Q3_K_AMPERE;
  2779. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  2780. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  2781. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2782. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2783. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  2784. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  2785. const int nwarps = NWARPS_Q3_K_PASCAL;
  2786. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  2787. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  2788. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2789. #else
  2790. (void) vec_dot_q3_K_q8_1_mul_mat;
  2791. assert(false);
  2792. #endif // __CUDA_ARCH__ >= CC_TURING
  2793. }
  2794. #define MMQ_X_Q4_K_AMPERE 64
  2795. #define MMQ_Y_Q4_K_AMPERE 128
  2796. #define NWARPS_Q4_K_AMPERE 4
  2797. #define MMQ_X_Q4_K_PASCAL 64
  2798. #define MMQ_Y_Q4_K_PASCAL 64
  2799. #define NWARPS_Q4_K_PASCAL 8
  2800. template <bool need_check> static __global__ void
  2801. #if __CUDA_ARCH__ < CC_TURING
  2802. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  2803. #endif // __CUDA_ARCH__ < CC_TURING
  2804. mul_mat_q4_K(
  2805. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2806. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2807. #if __CUDA_ARCH__ >= CC_TURING
  2808. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  2809. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  2810. const int nwarps = NWARPS_Q4_K_AMPERE;
  2811. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  2812. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  2813. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2814. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2815. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  2816. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  2817. const int nwarps = NWARPS_Q4_K_PASCAL;
  2818. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  2819. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  2820. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2821. #else
  2822. (void) vec_dot_q4_K_q8_1_mul_mat;
  2823. assert(false);
  2824. #endif // __CUDA_ARCH__ >= CC_TURING
  2825. }
  2826. #define MMQ_X_Q5_K_AMPERE 64
  2827. #define MMQ_Y_Q5_K_AMPERE 128
  2828. #define NWARPS_Q5_K_AMPERE 4
  2829. #define MMQ_X_Q5_K_PASCAL 64
  2830. #define MMQ_Y_Q5_K_PASCAL 64
  2831. #define NWARPS_Q5_K_PASCAL 8
  2832. template <bool need_check> static __global__ void mul_mat_q5_K(
  2833. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2834. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2835. #if __CUDA_ARCH__ >= CC_TURING
  2836. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  2837. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  2838. const int nwarps = NWARPS_Q5_K_AMPERE;
  2839. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  2840. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  2841. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2842. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2843. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  2844. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  2845. const int nwarps = NWARPS_Q5_K_PASCAL;
  2846. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  2847. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  2848. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2849. #else
  2850. (void) vec_dot_q5_K_q8_1_mul_mat;
  2851. assert(false);
  2852. #endif // __CUDA_ARCH__ >= CC_TURING
  2853. }
  2854. #define MMQ_X_Q6_K_AMPERE 64
  2855. #define MMQ_Y_Q6_K_AMPERE 64
  2856. #define NWARPS_Q6_K_AMPERE 4
  2857. #define MMQ_X_Q6_K_PASCAL 64
  2858. #define MMQ_Y_Q6_K_PASCAL 64
  2859. #define NWARPS_Q6_K_PASCAL 8
  2860. template <bool need_check> static __global__ void
  2861. #if __CUDA_ARCH__ < CC_TURING
  2862. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  2863. #endif // __CUDA_ARCH__ < CC_TURING
  2864. mul_mat_q6_K(
  2865. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2866. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2867. #if __CUDA_ARCH__ >= CC_TURING
  2868. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  2869. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  2870. const int nwarps = NWARPS_Q6_K_AMPERE;
  2871. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  2872. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  2873. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2874. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2875. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  2876. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  2877. const int nwarps = NWARPS_Q6_K_PASCAL;
  2878. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  2879. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  2880. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2881. #else
  2882. (void) vec_dot_q6_K_q8_1_mul_mat;
  2883. assert(false);
  2884. #endif // __CUDA_ARCH__ >= CC_TURING
  2885. }
  2886. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  2887. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  2888. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  2889. if (row >= nrows) {
  2890. return;
  2891. }
  2892. const int blocks_per_row = ncols / qk;
  2893. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  2894. // partial sum for each thread
  2895. float tmp = 0.0f;
  2896. const block_q_t * x = (const block_q_t *) vx;
  2897. const block_q8_1 * y = (const block_q8_1 *) vy;
  2898. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  2899. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  2900. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  2901. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  2902. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  2903. }
  2904. // sum up partial sums and write back result
  2905. #pragma unroll
  2906. for (int mask = 16; mask > 0; mask >>= 1) {
  2907. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2908. }
  2909. if (threadIdx.x == 0) {
  2910. dst[row] = tmp;
  2911. }
  2912. }
  2913. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  2914. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  2915. // qk = quantized weights per x block
  2916. // qr = number of quantized weights per data value in x block
  2917. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  2918. if (row >= nrows) {
  2919. return;
  2920. }
  2921. const int tid = threadIdx.x;
  2922. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  2923. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  2924. const int y_offset = qr == 1 ? 1 : qk/2;
  2925. // partial sum for each thread
  2926. #ifdef GGML_CUDA_F16
  2927. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  2928. #else
  2929. float tmp = 0.0f;
  2930. #endif // GGML_CUDA_F16
  2931. for (int i = 0; i < ncols; i += iter_stride) {
  2932. const int col = i + vals_per_iter*tid;
  2933. const int ib = (row*ncols + col)/qk; // x block index
  2934. const int iqs = (col%qk)/qr; // x quant index
  2935. const int iybs = col - col%qk; // y block start index
  2936. // processing >2 values per i iter is faster for fast GPUs
  2937. #pragma unroll
  2938. for (int j = 0; j < vals_per_iter; j += 2) {
  2939. // process 2 vals per j iter
  2940. // dequantize
  2941. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  2942. dfloat2 v;
  2943. dequantize_kernel(vx, ib, iqs + j/qr, v);
  2944. // matrix multiplication
  2945. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  2946. #ifdef GGML_CUDA_F16
  2947. tmp += __hmul2(v, {
  2948. y[iybs + iqs + j/qr + 0],
  2949. y[iybs + iqs + j/qr + y_offset]
  2950. });
  2951. #else
  2952. tmp += v.x * y[iybs + iqs + j/qr + 0];
  2953. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  2954. #endif // GGML_CUDA_F16
  2955. }
  2956. }
  2957. // sum up partial sums and write back result
  2958. #pragma unroll
  2959. for (int mask = 16; mask > 0; mask >>= 1) {
  2960. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2961. }
  2962. if (tid == 0) {
  2963. #ifdef GGML_CUDA_F16
  2964. dst[row] = tmp.x + tmp.y;
  2965. #else
  2966. dst[row] = tmp;
  2967. #endif // GGML_CUDA_F16
  2968. }
  2969. }
  2970. static __global__ void mul_mat_p021_f16_f32(
  2971. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  2972. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  2973. const half * x = (const half *) vx;
  2974. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  2975. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  2976. const int channel_x = channel / (nchannels_y / nchannels_x);
  2977. const int nrows_y = ncols_x;
  2978. const int nrows_dst = nrows_x;
  2979. const int row_dst = row_x;
  2980. float tmp = 0.0f;
  2981. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  2982. const int col_x = col_x0 + threadIdx.x;
  2983. if (col_x >= ncols_x) {
  2984. break;
  2985. }
  2986. // x is transposed and permuted
  2987. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  2988. const float xi = __half2float(x[ix]);
  2989. const int row_y = col_x;
  2990. // y is not transposed but permuted
  2991. const int iy = channel*nrows_y + row_y;
  2992. tmp += xi * y[iy];
  2993. }
  2994. // dst is not transposed and not permuted
  2995. const int idst = channel*nrows_dst + row_dst;
  2996. // sum up partial sums and write back result
  2997. #pragma unroll
  2998. for (int mask = 16; mask > 0; mask >>= 1) {
  2999. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3000. }
  3001. if (threadIdx.x == 0) {
  3002. dst[idst] = tmp;
  3003. }
  3004. }
  3005. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3006. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3007. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3008. const half * x = (const half *) vx;
  3009. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3010. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3011. const int channel_x = channel / channel_x_divisor;
  3012. const int nrows_y = ncols_x;
  3013. const int nrows_dst = nrows_x;
  3014. const int row_dst = row_x;
  3015. const int idst = channel*nrows_dst + row_dst;
  3016. float tmp = 0.0f;
  3017. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3018. const int col_x = col_x0 + threadIdx.x;
  3019. if (col_x >= ncols_x) {
  3020. break;
  3021. }
  3022. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3023. const float xi = __half2float(x[ix]);
  3024. const int row_y = col_x;
  3025. const int iy = channel*nrows_y + row_y;
  3026. tmp += xi * y[iy];
  3027. }
  3028. // sum up partial sums and write back result
  3029. #pragma unroll
  3030. for (int mask = 16; mask > 0; mask >>= 1) {
  3031. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3032. }
  3033. if (threadIdx.x == 0) {
  3034. dst[idst] = tmp;
  3035. }
  3036. }
  3037. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3038. const float * xi = (const float *) cxi;
  3039. float * dsti = (float *) cdsti;
  3040. *dsti = *xi;
  3041. }
  3042. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3043. const float * xi = (const float *) cxi;
  3044. half * dsti = (half *) cdsti;
  3045. *dsti = __float2half(*xi);
  3046. }
  3047. template <cpy_kernel_t cpy_1>
  3048. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3049. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3050. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3051. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3052. if (i >= ne) {
  3053. return;
  3054. }
  3055. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3056. // then combine those indices with the corresponding byte offsets to get the total offsets
  3057. const int i02 = i / (ne00*ne01);
  3058. const int i01 = (i - i02*ne01*ne00) / ne00;
  3059. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3060. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3061. const int i12 = i / (ne10*ne11);
  3062. const int i11 = (i - i12*ne10*ne11) / ne10;
  3063. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3064. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3065. cpy_1(cx + x_offset, cdst + dst_offset);
  3066. }
  3067. // rope == RoPE == rotary positional embedding
  3068. static __global__ void rope_f32(const float * x, float * dst, const int ncols, const float p0,
  3069. const float p_delta, const int p_delta_rows, const float theta_scale) {
  3070. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3071. if (col >= ncols) {
  3072. return;
  3073. }
  3074. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3075. const int i = row*ncols + col;
  3076. const float theta = (p0 + p_delta * (row/p_delta_rows))*powf(theta_scale, col/2);
  3077. const float sin_theta = sinf(theta);
  3078. const float cos_theta = cosf(theta);
  3079. const float x0 = x[i + 0];
  3080. const float x1 = x[i + 1];
  3081. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3082. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3083. }
  3084. static __global__ void rope_neox_f32(const float * x, float * dst, const int ncols, const float p0,
  3085. const float p_delta, const int p_delta_rows, const float theta_scale) {
  3086. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3087. if (col >= ncols) {
  3088. return;
  3089. }
  3090. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3091. const int i = row*ncols + col/2;
  3092. const float theta = (p0 + p_delta * (row/p_delta_rows))*powf(theta_scale, col/2);
  3093. const float sin_theta = sinf(theta);
  3094. const float cos_theta = cosf(theta);
  3095. const float x0 = x[i + 0];
  3096. const float x1 = x[i + ncols/2];
  3097. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3098. dst[i + ncols/2] = x0*sin_theta + x1*cos_theta;
  3099. }
  3100. static __global__ void rope_glm_f32(const float * x, float * dst, const int ncols, const float p, const float block_p, const float theta_scale) {
  3101. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3102. const int half_n_dims = ncols/4;
  3103. if (col >= half_n_dims) {
  3104. return;
  3105. }
  3106. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3107. const int i = row*ncols + col;
  3108. const float col_theta_scale = powf(theta_scale, col);
  3109. const float theta = p*col_theta_scale;
  3110. const float sin_theta = sinf(theta);
  3111. const float cos_theta = cosf(theta);
  3112. const float x0 = x[i + 0];
  3113. const float x1 = x[i + half_n_dims];
  3114. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3115. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  3116. const float block_theta = block_p*col_theta_scale;
  3117. const float sin_block_theta = sinf(block_theta);
  3118. const float cos_block_theta = cosf(block_theta);
  3119. const float x2 = x[i + half_n_dims * 2];
  3120. const float x3 = x[i + half_n_dims * 3];
  3121. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  3122. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  3123. }
  3124. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  3125. const int n_heads_log2_floor, const float m0, const float m1) {
  3126. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3127. if (col >= ncols) {
  3128. return;
  3129. }
  3130. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3131. const int i = row*ncols + col;
  3132. const int k = row/k_rows;
  3133. float m_k;
  3134. if (k < n_heads_log2_floor) {
  3135. m_k = powf(m0, k + 1);
  3136. } else {
  3137. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  3138. }
  3139. dst[i] = col * m_k + x[i];
  3140. }
  3141. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  3142. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  3143. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3144. if (col >= ncols) {
  3145. return;
  3146. }
  3147. const int i = row*ncols + col;
  3148. // dst[i] = col > n_past + row ? -INFINITY : x[i];
  3149. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  3150. }
  3151. // the CUDA soft max implementation differs from the CPU implementation
  3152. // instead of doubles floats are used
  3153. static __global__ void soft_max_f32(const float * x, float * dst, const int ncols) {
  3154. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3155. const int block_size = blockDim.y;
  3156. const int tid = threadIdx.y;
  3157. float max_val = -INFINITY;
  3158. for (int col = tid; col < ncols; col += block_size) {
  3159. const int i = row*ncols + col;
  3160. max_val = max(max_val, x[i]);
  3161. }
  3162. // find the max value in the block
  3163. #pragma unroll
  3164. for (int mask = 16; mask > 0; mask >>= 1) {
  3165. max_val = max(max_val, __shfl_xor_sync(0xffffffff, max_val, mask, 32));
  3166. }
  3167. float tmp = 0.f;
  3168. for (int col = tid; col < ncols; col += block_size) {
  3169. const int i = row*ncols + col;
  3170. const float val = expf(x[i] - max_val);
  3171. tmp += val;
  3172. dst[i] = val;
  3173. }
  3174. // sum up partial sums
  3175. #pragma unroll
  3176. for (int mask = 16; mask > 0; mask >>= 1) {
  3177. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3178. }
  3179. const float inv_tmp = 1.f / tmp;
  3180. for (int col = tid; col < ncols; col += block_size) {
  3181. const int i = row*ncols + col;
  3182. dst[i] *= inv_tmp;
  3183. }
  3184. }
  3185. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  3186. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3187. if (i >= k) {
  3188. return;
  3189. }
  3190. dst[i] = scale * x[i];
  3191. }
  3192. static void add_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  3193. const int num_blocks = (kx + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  3194. add_f32<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  3195. }
  3196. static void add_f16_f32_f16_cuda(const half * x, const float * y, half * dst, const int k, cudaStream_t stream) {
  3197. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  3198. add_f16_f32_f16<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  3199. }
  3200. static void mul_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  3201. const int num_blocks = (kx + CUDA_MUL_BLOCK_SIZE - 1) / CUDA_MUL_BLOCK_SIZE;
  3202. mul_f32<<<num_blocks, CUDA_MUL_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  3203. }
  3204. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3205. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  3206. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3207. }
  3208. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3209. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  3210. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3211. }
  3212. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3213. GGML_ASSERT(ncols % WARP_SIZE == 0);
  3214. const dim3 block_dims(WARP_SIZE, 1, 1);
  3215. norm_f32<<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  3216. }
  3217. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  3218. GGML_ASSERT(ncols % WARP_SIZE == 0);
  3219. const dim3 block_dims(WARP_SIZE, 1, 1);
  3220. rms_norm_f32<<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  3221. }
  3222. 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) {
  3223. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  3224. const dim3 num_blocks(block_num_x, ky, 1);
  3225. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  3226. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  3227. }
  3228. static void dequantize_row_q4_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3229. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3230. dequantize_block<QK4_0, QR4_0, dequantize_q4_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3231. }
  3232. static void dequantize_row_q4_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3233. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3234. dequantize_block<QK4_1, QR4_1, dequantize_q4_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3235. }
  3236. static void dequantize_row_q5_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3237. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3238. dequantize_block<QK5_0, QR5_0, dequantize_q5_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3239. }
  3240. static void dequantize_row_q5_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3241. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3242. dequantize_block<QK5_1, QR5_1, dequantize_q5_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3243. }
  3244. static void dequantize_row_q8_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3245. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3246. dequantize_block<QK8_0, QR8_0, dequantize_q8_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3247. }
  3248. static void dequantize_row_q2_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3249. const int nb = k / QK_K;
  3250. #if QK_K == 256
  3251. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  3252. #else
  3253. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  3254. #endif
  3255. }
  3256. static void dequantize_row_q3_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3257. const int nb = k / QK_K;
  3258. #if QK_K == 256
  3259. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  3260. #else
  3261. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  3262. #endif
  3263. }
  3264. static void dequantize_row_q4_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3265. const int nb = k / QK_K;
  3266. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  3267. }
  3268. static void dequantize_row_q5_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3269. const int nb = k / QK_K;
  3270. #if QK_K == 256
  3271. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  3272. #else
  3273. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  3274. #endif
  3275. }
  3276. static void dequantize_row_q6_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3277. const int nb = k / QK_K;
  3278. #if QK_K == 256
  3279. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  3280. #else
  3281. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  3282. #endif
  3283. }
  3284. 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) {
  3285. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3286. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3287. const dim3 block_nums(1, block_num_y, 1);
  3288. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3289. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  3290. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3291. }
  3292. 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) {
  3293. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3294. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3295. const dim3 block_nums(1, block_num_y, 1);
  3296. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3297. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  3298. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3299. }
  3300. 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) {
  3301. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3302. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3303. const dim3 block_nums(1, block_num_y, 1);
  3304. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3305. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  3306. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3307. }
  3308. 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) {
  3309. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3310. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3311. const dim3 block_nums(1, block_num_y, 1);
  3312. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3313. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  3314. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3315. }
  3316. 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) {
  3317. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3318. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3319. const dim3 block_nums(1, block_num_y, 1);
  3320. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3321. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  3322. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3323. }
  3324. 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) {
  3325. GGML_ASSERT(ncols % QK_K == 0);
  3326. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  3327. const int block_num_y = (nrows + ny - 1) / ny;
  3328. const dim3 block_nums(1, block_num_y, 1);
  3329. const dim3 block_dims(32, ny, 1);
  3330. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3331. }
  3332. 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) {
  3333. GGML_ASSERT(ncols % QK_K == 0);
  3334. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3335. const int block_num_y = (nrows + ny - 1) / ny;
  3336. const dim3 block_nums(1, block_num_y, 1);
  3337. const dim3 block_dims(32, ny, 1);
  3338. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3339. }
  3340. 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) {
  3341. GGML_ASSERT(ncols % QK_K == 0);
  3342. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3343. const int block_num_y = (nrows + ny - 1) / ny;
  3344. const dim3 block_nums(1, block_num_y, 1);
  3345. const dim3 block_dims(32, ny, 1);
  3346. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3347. }
  3348. 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) {
  3349. GGML_ASSERT(ncols % QK_K == 0);
  3350. const dim3 block_dims(32, 1, 1);
  3351. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  3352. }
  3353. 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) {
  3354. GGML_ASSERT(ncols % QK_K == 0);
  3355. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3356. const int block_num_y = (nrows + ny - 1) / ny;
  3357. const dim3 block_nums(1, block_num_y, 1);
  3358. const dim3 block_dims(32, ny, 1);
  3359. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3360. }
  3361. static void mul_mat_vec_q4_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3362. GGML_ASSERT(ncols % QK4_0 == 0);
  3363. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3364. const dim3 block_nums(1, block_num_y, 1);
  3365. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3366. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  3367. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3368. }
  3369. static void mul_mat_vec_q4_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3370. GGML_ASSERT(ncols % QK4_1 == 0);
  3371. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3372. const dim3 block_nums(1, block_num_y, 1);
  3373. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3374. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  3375. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3376. }
  3377. static void mul_mat_vec_q5_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3378. GGML_ASSERT(ncols % QK5_0 == 0);
  3379. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3380. const dim3 block_nums(1, block_num_y, 1);
  3381. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3382. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  3383. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3384. }
  3385. static void mul_mat_vec_q5_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3386. GGML_ASSERT(ncols % QK5_1 == 0);
  3387. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3388. const dim3 block_nums(1, block_num_y, 1);
  3389. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3390. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  3391. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3392. }
  3393. static void mul_mat_vec_q8_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3394. GGML_ASSERT(ncols % QK8_0 == 0);
  3395. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3396. const dim3 block_nums(1, block_num_y, 1);
  3397. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3398. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  3399. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3400. }
  3401. static void mul_mat_vec_q2_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3402. GGML_ASSERT(ncols % QK_K == 0);
  3403. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3404. const dim3 block_nums(1, block_num_y, 1);
  3405. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3406. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  3407. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3408. }
  3409. static void mul_mat_vec_q3_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3410. GGML_ASSERT(ncols % QK_K == 0);
  3411. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3412. const dim3 block_nums(1, block_num_y, 1);
  3413. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3414. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  3415. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3416. }
  3417. static void mul_mat_vec_q4_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3418. GGML_ASSERT(ncols % QK_K == 0);
  3419. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3420. const dim3 block_nums(1, block_num_y, 1);
  3421. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3422. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  3423. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3424. }
  3425. static void mul_mat_vec_q5_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3426. GGML_ASSERT(ncols % QK_K == 0);
  3427. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3428. const dim3 block_nums(1, block_num_y, 1);
  3429. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3430. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  3431. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3432. }
  3433. static void mul_mat_vec_q6_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3434. GGML_ASSERT(ncols % QK_K == 0);
  3435. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3436. const dim3 block_nums(1, block_num_y, 1);
  3437. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3438. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  3439. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3440. }
  3441. static void convert_fp16_to_fp32_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3442. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3443. dequantize_block<1, 1, convert_f16><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3444. }
  3445. 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) {
  3446. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3447. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3448. const dim3 block_nums(1, block_num_y, 1);
  3449. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3450. dequantize_mul_mat_vec<1, 1, convert_f16>
  3451. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3452. }
  3453. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  3454. switch (type) {
  3455. case GGML_TYPE_Q4_0:
  3456. return dequantize_row_q4_0_cuda;
  3457. case GGML_TYPE_Q4_1:
  3458. return dequantize_row_q4_1_cuda;
  3459. case GGML_TYPE_Q5_0:
  3460. return dequantize_row_q5_0_cuda;
  3461. case GGML_TYPE_Q5_1:
  3462. return dequantize_row_q5_1_cuda;
  3463. case GGML_TYPE_Q8_0:
  3464. return dequantize_row_q8_0_cuda;
  3465. case GGML_TYPE_Q2_K:
  3466. return dequantize_row_q2_K_cuda;
  3467. case GGML_TYPE_Q3_K:
  3468. return dequantize_row_q3_K_cuda;
  3469. case GGML_TYPE_Q4_K:
  3470. return dequantize_row_q4_K_cuda;
  3471. case GGML_TYPE_Q5_K:
  3472. return dequantize_row_q5_K_cuda;
  3473. case GGML_TYPE_Q6_K:
  3474. return dequantize_row_q6_K_cuda;
  3475. case GGML_TYPE_F16:
  3476. return convert_fp16_to_fp32_cuda;
  3477. default:
  3478. return nullptr;
  3479. }
  3480. }
  3481. static void ggml_mul_mat_q4_0_q8_1_cuda(
  3482. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3483. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3484. int id;
  3485. CUDA_CHECK(cudaGetDevice(&id));
  3486. const int compute_capability = g_compute_capabilities[id];
  3487. int mmq_x, mmq_y, nwarps;
  3488. if (compute_capability >= CC_TURING) {
  3489. mmq_x = MMQ_X_Q4_0_AMPERE;
  3490. mmq_y = MMQ_Y_Q4_0_AMPERE;
  3491. nwarps = NWARPS_Q4_0_AMPERE;
  3492. } else if (compute_capability >= MIN_CC_DP4A) {
  3493. mmq_x = MMQ_X_Q4_0_PASCAL;
  3494. mmq_y = MMQ_Y_Q4_0_PASCAL;
  3495. nwarps = NWARPS_Q4_0_PASCAL;
  3496. } else {
  3497. GGML_ASSERT(false);
  3498. }
  3499. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3500. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3501. const dim3 block_nums(block_num_x, block_num_y, 1);
  3502. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3503. if (nrows_x % mmq_y == 0) {
  3504. const bool need_check = false;
  3505. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3506. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3507. } else {
  3508. const bool need_check = true;
  3509. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3510. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3511. }
  3512. }
  3513. static void ggml_mul_mat_q4_1_q8_1_cuda(
  3514. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3515. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3516. int id;
  3517. CUDA_CHECK(cudaGetDevice(&id));
  3518. const int compute_capability = g_compute_capabilities[id];
  3519. int mmq_x, mmq_y, nwarps;
  3520. if (compute_capability >= CC_TURING) {
  3521. mmq_x = MMQ_X_Q4_1_AMPERE;
  3522. mmq_y = MMQ_Y_Q4_1_AMPERE;
  3523. nwarps = NWARPS_Q4_1_AMPERE;
  3524. } else if (compute_capability >= MIN_CC_DP4A) {
  3525. mmq_x = MMQ_X_Q4_1_PASCAL;
  3526. mmq_y = MMQ_Y_Q4_1_PASCAL;
  3527. nwarps = NWARPS_Q4_1_PASCAL;
  3528. } else {
  3529. GGML_ASSERT(false);
  3530. }
  3531. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3532. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3533. const dim3 block_nums(block_num_x, block_num_y, 1);
  3534. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3535. if (nrows_x % mmq_y == 0) {
  3536. const bool need_check = false;
  3537. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  3538. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3539. } else {
  3540. const bool need_check = true;
  3541. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  3542. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3543. }
  3544. }
  3545. static void ggml_mul_mat_q5_0_q8_1_cuda(
  3546. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3547. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3548. int id;
  3549. CUDA_CHECK(cudaGetDevice(&id));
  3550. const int compute_capability = g_compute_capabilities[id];
  3551. int mmq_x, mmq_y, nwarps;
  3552. if (compute_capability >= CC_TURING) {
  3553. mmq_x = MMQ_X_Q5_0_AMPERE;
  3554. mmq_y = MMQ_Y_Q5_0_AMPERE;
  3555. nwarps = NWARPS_Q5_0_AMPERE;
  3556. } else if (compute_capability >= MIN_CC_DP4A) {
  3557. mmq_x = MMQ_X_Q5_0_PASCAL;
  3558. mmq_y = MMQ_Y_Q5_0_PASCAL;
  3559. nwarps = NWARPS_Q5_0_PASCAL;
  3560. } else {
  3561. GGML_ASSERT(false);
  3562. }
  3563. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3564. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3565. const dim3 block_nums(block_num_x, block_num_y, 1);
  3566. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3567. if (nrows_x % mmq_y == 0) {
  3568. const bool need_check = false;
  3569. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3570. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3571. } else {
  3572. const bool need_check = true;
  3573. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3574. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3575. }
  3576. }
  3577. static void ggml_mul_mat_q5_1_q8_1_cuda(
  3578. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3579. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3580. int id;
  3581. CUDA_CHECK(cudaGetDevice(&id));
  3582. const int compute_capability = g_compute_capabilities[id];
  3583. int mmq_x, mmq_y, nwarps;
  3584. if (compute_capability >= CC_TURING) {
  3585. mmq_x = MMQ_X_Q5_1_AMPERE;
  3586. mmq_y = MMQ_Y_Q5_1_AMPERE;
  3587. nwarps = NWARPS_Q5_1_AMPERE;
  3588. } else if (compute_capability >= MIN_CC_DP4A) {
  3589. mmq_x = MMQ_X_Q5_1_PASCAL;
  3590. mmq_y = MMQ_Y_Q5_1_PASCAL;
  3591. nwarps = NWARPS_Q5_1_PASCAL;
  3592. } else {
  3593. GGML_ASSERT(false);
  3594. }
  3595. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3596. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3597. const dim3 block_nums(block_num_x, block_num_y, 1);
  3598. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3599. if (nrows_x % mmq_y == 0) {
  3600. const bool need_check = false;
  3601. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  3602. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3603. } else {
  3604. const bool need_check = true;
  3605. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  3606. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3607. }
  3608. }
  3609. static void ggml_mul_mat_q8_0_q8_1_cuda(
  3610. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3611. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3612. int id;
  3613. CUDA_CHECK(cudaGetDevice(&id));
  3614. const int compute_capability = g_compute_capabilities[id];
  3615. int mmq_x, mmq_y, nwarps;
  3616. if (compute_capability >= CC_TURING) {
  3617. mmq_x = MMQ_X_Q8_0_AMPERE;
  3618. mmq_y = MMQ_Y_Q8_0_AMPERE;
  3619. nwarps = NWARPS_Q8_0_AMPERE;
  3620. } else if (compute_capability >= MIN_CC_DP4A) {
  3621. mmq_x = MMQ_X_Q8_0_PASCAL;
  3622. mmq_y = MMQ_Y_Q8_0_PASCAL;
  3623. nwarps = NWARPS_Q8_0_PASCAL;
  3624. } else {
  3625. GGML_ASSERT(false);
  3626. }
  3627. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3628. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3629. const dim3 block_nums(block_num_x, block_num_y, 1);
  3630. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3631. if (nrows_x % mmq_y == 0) {
  3632. const bool need_check = false;
  3633. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3634. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3635. } else {
  3636. const bool need_check = true;
  3637. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3638. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3639. }
  3640. }
  3641. static void ggml_mul_mat_q2_K_q8_1_cuda(
  3642. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3643. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3644. int id;
  3645. CUDA_CHECK(cudaGetDevice(&id));
  3646. const int compute_capability = g_compute_capabilities[id];
  3647. int mmq_x, mmq_y, nwarps;
  3648. if (compute_capability >= CC_TURING) {
  3649. mmq_x = MMQ_X_Q2_K_AMPERE;
  3650. mmq_y = MMQ_Y_Q2_K_AMPERE;
  3651. nwarps = NWARPS_Q2_K_AMPERE;
  3652. } else if (compute_capability >= MIN_CC_DP4A) {
  3653. mmq_x = MMQ_X_Q2_K_PASCAL;
  3654. mmq_y = MMQ_Y_Q2_K_PASCAL;
  3655. nwarps = NWARPS_Q2_K_PASCAL;
  3656. } else {
  3657. GGML_ASSERT(false);
  3658. }
  3659. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3660. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3661. const dim3 block_nums(block_num_x, block_num_y, 1);
  3662. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3663. if (nrows_x % mmq_y == 0) {
  3664. const bool need_check = false;
  3665. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3666. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3667. } else {
  3668. const bool need_check = true;
  3669. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3670. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3671. }
  3672. }
  3673. static void ggml_mul_mat_q3_K_q8_1_cuda(
  3674. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3675. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3676. int id;
  3677. CUDA_CHECK(cudaGetDevice(&id));
  3678. const int compute_capability = g_compute_capabilities[id];
  3679. int mmq_x, mmq_y, nwarps;
  3680. if (compute_capability >= CC_TURING) {
  3681. mmq_x = MMQ_X_Q3_K_AMPERE;
  3682. mmq_y = MMQ_Y_Q3_K_AMPERE;
  3683. nwarps = NWARPS_Q3_K_AMPERE;
  3684. } else if (compute_capability >= MIN_CC_DP4A) {
  3685. mmq_x = MMQ_X_Q3_K_PASCAL;
  3686. mmq_y = MMQ_Y_Q3_K_PASCAL;
  3687. nwarps = NWARPS_Q3_K_PASCAL;
  3688. } else {
  3689. GGML_ASSERT(false);
  3690. }
  3691. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3692. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3693. const dim3 block_nums(block_num_x, block_num_y, 1);
  3694. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3695. if (nrows_x % mmq_y == 0) {
  3696. const bool need_check = false;
  3697. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3698. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3699. } else {
  3700. const bool need_check = true;
  3701. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3702. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3703. }
  3704. }
  3705. static void ggml_mul_mat_q4_K_q8_1_cuda(
  3706. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3707. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3708. int id;
  3709. CUDA_CHECK(cudaGetDevice(&id));
  3710. const int compute_capability = g_compute_capabilities[id];
  3711. int mmq_x, mmq_y, nwarps;
  3712. if (compute_capability >= CC_TURING) {
  3713. mmq_x = MMQ_X_Q4_K_AMPERE;
  3714. mmq_y = MMQ_Y_Q4_K_AMPERE;
  3715. nwarps = NWARPS_Q4_K_AMPERE;
  3716. } else if (compute_capability >= MIN_CC_DP4A) {
  3717. mmq_x = MMQ_X_Q4_K_PASCAL;
  3718. mmq_y = MMQ_Y_Q4_K_PASCAL;
  3719. nwarps = NWARPS_Q4_K_PASCAL;
  3720. } else {
  3721. GGML_ASSERT(false);
  3722. }
  3723. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3724. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3725. const dim3 block_nums(block_num_x, block_num_y, 1);
  3726. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3727. if (nrows_x % mmq_y == 0) {
  3728. const bool need_check = false;
  3729. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3730. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3731. } else {
  3732. const bool need_check = true;
  3733. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3734. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3735. }
  3736. }
  3737. static void ggml_mul_mat_q5_K_q8_1_cuda(
  3738. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3739. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3740. int id;
  3741. CUDA_CHECK(cudaGetDevice(&id));
  3742. const int compute_capability = g_compute_capabilities[id];
  3743. int mmq_x, mmq_y, nwarps;
  3744. if (compute_capability >= CC_TURING) {
  3745. mmq_x = MMQ_X_Q5_K_AMPERE;
  3746. mmq_y = MMQ_Y_Q5_K_AMPERE;
  3747. nwarps = NWARPS_Q5_K_AMPERE;
  3748. } else if (compute_capability >= MIN_CC_DP4A) {
  3749. mmq_x = MMQ_X_Q5_K_PASCAL;
  3750. mmq_y = MMQ_Y_Q5_K_PASCAL;
  3751. nwarps = NWARPS_Q5_K_PASCAL;
  3752. } else {
  3753. GGML_ASSERT(false);
  3754. }
  3755. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3756. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3757. const dim3 block_nums(block_num_x, block_num_y, 1);
  3758. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3759. if (nrows_x % mmq_y == 0) {
  3760. const bool need_check = false;
  3761. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3762. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3763. } else {
  3764. const bool need_check = true;
  3765. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3766. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3767. }
  3768. }
  3769. static void ggml_mul_mat_q6_K_q8_1_cuda(
  3770. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3771. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3772. int id;
  3773. CUDA_CHECK(cudaGetDevice(&id));
  3774. const int compute_capability = g_compute_capabilities[id];
  3775. int mmq_x, mmq_y, nwarps;
  3776. if (compute_capability >= CC_TURING) {
  3777. mmq_x = MMQ_X_Q6_K_AMPERE;
  3778. mmq_y = MMQ_Y_Q6_K_AMPERE;
  3779. nwarps = NWARPS_Q6_K_AMPERE;
  3780. } else if (compute_capability >= MIN_CC_DP4A) {
  3781. mmq_x = MMQ_X_Q6_K_PASCAL;
  3782. mmq_y = MMQ_Y_Q6_K_PASCAL;
  3783. nwarps = NWARPS_Q6_K_PASCAL;
  3784. } else {
  3785. GGML_ASSERT(false);
  3786. }
  3787. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3788. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3789. const dim3 block_nums(block_num_x, block_num_y, 1);
  3790. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3791. if (nrows_x % mmq_y == 0) {
  3792. const bool need_check = false;
  3793. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3794. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3795. } else {
  3796. const bool need_check = true;
  3797. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  3798. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3799. }
  3800. }
  3801. static void ggml_mul_mat_p021_f16_f32_cuda(
  3802. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  3803. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  3804. const dim3 block_nums(1, nrows_x, nchannels_y);
  3805. const dim3 block_dims(WARP_SIZE, 1, 1);
  3806. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  3807. }
  3808. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  3809. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  3810. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  3811. const dim3 block_nums(1, nrows_x, nchannels_y);
  3812. const dim3 block_dims(WARP_SIZE, 1, 1);
  3813. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  3814. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  3815. }
  3816. static void ggml_cpy_f32_f32_cuda(
  3817. const char * cx, char * cdst, const int ne,
  3818. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3819. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  3820. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  3821. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  3822. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  3823. }
  3824. static void ggml_cpy_f32_f16_cuda(
  3825. const char * cx, char * cdst, const int ne,
  3826. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3827. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  3828. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  3829. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  3830. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  3831. }
  3832. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  3833. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  3834. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  3835. }
  3836. static void rope_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p0,
  3837. const float p_delta, const int p_delta_rows, const float theta_scale, cudaStream_t stream) {
  3838. GGML_ASSERT(nrows % 2 == 0); // GG: is this assert really needed? I don't see why
  3839. const dim3 block_dims(1, 2*CUDA_ROPE_BLOCK_SIZE, 1);
  3840. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  3841. const dim3 block_nums(nrows, num_blocks_x, 1);
  3842. rope_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p0, p_delta, p_delta_rows, theta_scale);
  3843. }
  3844. static void rope_neox_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p0,
  3845. const float p_delta, const int p_delta_rows, const float theta_scale, cudaStream_t stream) {
  3846. const dim3 block_dims(1, 2*CUDA_ROPE_BLOCK_SIZE, 1);
  3847. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  3848. const dim3 block_nums(nrows, num_blocks_x, 1);
  3849. rope_neox_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p0, p_delta, p_delta_rows, theta_scale);
  3850. }
  3851. static void rope_glm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p, const float block_p, const float theta_scale, cudaStream_t stream) {
  3852. GGML_ASSERT(nrows % 4 == 0);
  3853. const dim3 block_dims(4*CUDA_ROPE_BLOCK_SIZE, 1, 1);
  3854. const int num_blocks_x = (ncols + 4*CUDA_ROPE_BLOCK_SIZE - 1) / (4*CUDA_ROPE_BLOCK_SIZE);
  3855. const dim3 block_nums(num_blocks_x, nrows, 1);
  3856. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p, block_p, theta_scale);
  3857. }
  3858. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  3859. const int k_rows, const int n_heads_log2_floor, const float m0,
  3860. const float m1, cudaStream_t stream) {
  3861. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  3862. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  3863. const dim3 block_nums(num_blocks_x, nrows, 1);
  3864. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  3865. }
  3866. 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) {
  3867. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  3868. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  3869. const dim3 block_nums(nrows_x, block_num_x, 1);
  3870. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  3871. }
  3872. static void soft_max_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, cudaStream_t stream) {
  3873. const dim3 block_dims(1, WARP_SIZE, 1);
  3874. const dim3 block_nums(nrows_x, 1, 1);
  3875. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x);
  3876. }
  3877. // buffer pool for cuda
  3878. #define MAX_CUDA_BUFFERS 256
  3879. struct scoped_spin_lock {
  3880. std::atomic_flag& lock;
  3881. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  3882. while (lock.test_and_set(std::memory_order_acquire)) {
  3883. ; // spin
  3884. }
  3885. }
  3886. ~scoped_spin_lock() {
  3887. lock.clear(std::memory_order_release);
  3888. }
  3889. scoped_spin_lock(const scoped_spin_lock&) = delete;
  3890. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  3891. };
  3892. struct cuda_buffer {
  3893. void * ptr = nullptr;
  3894. size_t size = 0;
  3895. };
  3896. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  3897. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  3898. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  3899. scoped_spin_lock lock(g_cuda_pool_lock);
  3900. int id;
  3901. CUDA_CHECK(cudaGetDevice(&id));
  3902. #ifdef DEBUG_CUDA_MALLOC
  3903. int nnz = 0;
  3904. size_t max_size = 0, tot_size = 0;
  3905. #endif
  3906. size_t best_diff = 1ull << 36;
  3907. int ibest = -1;
  3908. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  3909. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  3910. if (b.ptr != nullptr) {
  3911. #ifdef DEBUG_CUDA_MALLOC
  3912. ++nnz;
  3913. tot_size += b.size;
  3914. if (b.size > max_size) max_size = b.size;
  3915. #endif
  3916. if (b.size >= size) {
  3917. size_t diff = b.size - size;
  3918. if (diff < best_diff) {
  3919. best_diff = diff;
  3920. ibest = i;
  3921. if (!best_diff) {
  3922. void * ptr = b.ptr;
  3923. *actual_size = b.size;
  3924. b.ptr = nullptr;
  3925. b.size = 0;
  3926. return ptr;
  3927. }
  3928. }
  3929. }
  3930. }
  3931. }
  3932. if (ibest >= 0) {
  3933. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  3934. void * ptr = b.ptr;
  3935. *actual_size = b.size;
  3936. b.ptr = nullptr;
  3937. b.size = 0;
  3938. return ptr;
  3939. }
  3940. #ifdef DEBUG_CUDA_MALLOC
  3941. fprintf(stderr, "%s: %d buffers, max_size = %u MB, tot_size = %u MB, requested %u MB\n", __func__, nnz,
  3942. (uint32_t)(max_size/1024/1024), (uint32_t)(tot_size/1024/1024), (uint32_t)(size/1024/1024));
  3943. #endif
  3944. void * ptr;
  3945. size_t look_ahead_size = (size_t) (1.05 * size);
  3946. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  3947. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  3948. *actual_size = look_ahead_size;
  3949. return ptr;
  3950. }
  3951. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  3952. scoped_spin_lock lock(g_cuda_pool_lock);
  3953. int id;
  3954. CUDA_CHECK(cudaGetDevice(&id));
  3955. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  3956. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  3957. if (b.ptr == nullptr) {
  3958. b.ptr = ptr;
  3959. b.size = size;
  3960. return;
  3961. }
  3962. }
  3963. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  3964. CUDA_CHECK(cudaFree(ptr));
  3965. }
  3966. void ggml_init_cublas() {
  3967. static bool initialized = false;
  3968. if (!initialized) {
  3969. #ifdef __HIP_PLATFORM_AMD__
  3970. // Workaround for a rocBLAS bug when using multiple graphics cards:
  3971. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  3972. rocblas_initialize();
  3973. CUDA_CHECK(cudaDeviceSynchronize());
  3974. #endif
  3975. CUDA_CHECK(cudaGetDeviceCount(&g_device_count));
  3976. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  3977. int64_t total_vram = 0;
  3978. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  3979. for (int id = 0; id < g_device_count; ++id) {
  3980. cudaDeviceProp prop;
  3981. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  3982. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  3983. g_tensor_split[id] = total_vram;
  3984. total_vram += prop.totalGlobalMem;
  3985. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  3986. }
  3987. for (int id = 0; id < g_device_count; ++id) {
  3988. g_tensor_split[id] /= total_vram;
  3989. }
  3990. for (int id = 0; id < g_device_count; ++id) {
  3991. CUDA_CHECK(cudaSetDevice(id));
  3992. // create main stream
  3993. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams_main[id], cudaStreamNonBlocking));
  3994. // create cublas handle
  3995. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  3996. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  3997. }
  3998. // configure logging to stdout
  3999. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  4000. initialized = true;
  4001. }
  4002. }
  4003. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  4004. if (tensor_split == nullptr) {
  4005. return;
  4006. }
  4007. bool all_zero = true;
  4008. for (int i = 0; i < g_device_count; ++i) {
  4009. if (tensor_split[i] != 0.0f) {
  4010. all_zero = false;
  4011. break;
  4012. }
  4013. }
  4014. if (all_zero) {
  4015. return;
  4016. }
  4017. float split_sum = 0.0f;
  4018. for (int i = 0; i < g_device_count; ++i) {
  4019. g_tensor_split[i] = split_sum;
  4020. split_sum += tensor_split[i];
  4021. }
  4022. for (int i = 0; i < g_device_count; ++i) {
  4023. g_tensor_split[i] /= split_sum;
  4024. }
  4025. }
  4026. void * ggml_cuda_host_malloc(size_t size) {
  4027. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  4028. return nullptr;
  4029. }
  4030. void * ptr = nullptr;
  4031. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  4032. if (err != cudaSuccess) {
  4033. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  4034. // This can fixed the OOM error in WSL.
  4035. cudaGetLastError();
  4036. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  4037. size/1024.0/1024.0, cudaGetErrorString(err));
  4038. return nullptr;
  4039. }
  4040. return ptr;
  4041. }
  4042. void ggml_cuda_host_free(void * ptr) {
  4043. CUDA_CHECK(cudaFreeHost(ptr));
  4044. }
  4045. static cudaError_t ggml_cuda_cpy_tensor_2d(
  4046. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  4047. cudaMemcpyKind kind;
  4048. char * src_ptr;
  4049. if (src->backend == GGML_BACKEND_CPU) {
  4050. kind = cudaMemcpyHostToDevice;
  4051. src_ptr = (char *) src->data;
  4052. } else if (src->backend == GGML_BACKEND_GPU) {
  4053. kind = cudaMemcpyDeviceToDevice;
  4054. struct ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  4055. int id;
  4056. CUDA_CHECK(cudaGetDevice(&id));
  4057. src_ptr = (char *) extra->data_device[id];
  4058. } else {
  4059. GGML_ASSERT(false);
  4060. }
  4061. char * dst_ptr = (char *) dst;
  4062. const int64_t ne0 = src->ne[0];
  4063. const int64_t nb0 = src->nb[0];
  4064. const int64_t nb1 = src->nb[1];
  4065. const int64_t nb2 = src->nb[2];
  4066. const int64_t nb3 = src->nb[3];
  4067. const enum ggml_type type = src->type;
  4068. const int64_t ts = ggml_type_size(type);
  4069. const int64_t bs = ggml_blck_size(type);
  4070. int64_t i1_diff = i1_high - i1_low;
  4071. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  4072. if (nb0 == ts && nb1 == ts*ne0/bs) {
  4073. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  4074. } else if (nb0 == ts) {
  4075. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  4076. } else {
  4077. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  4078. const void * rx = (const void *) ((const char *) x + i1*nb1);
  4079. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  4080. // pretend the row is a matrix with cols=1
  4081. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  4082. if (r != cudaSuccess) return r;
  4083. }
  4084. return cudaSuccess;
  4085. }
  4086. }
  4087. inline void ggml_cuda_op_add(
  4088. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4089. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4090. cudaStream_t & cudaStream_main){
  4091. GGML_ASSERT(src0_ddq_i != nullptr || src0_ddf_i != nullptr);
  4092. GGML_ASSERT(src1_ddf_i != nullptr);
  4093. GGML_ASSERT(dst_ddf_i != nullptr);
  4094. const int64_t ne00 = src0->ne[0];
  4095. const int64_t i01_diff = i01_high - i01_low;
  4096. const int64_t ne10 = src1->ne[0];
  4097. const int64_t ne11 = src1->ne[1];
  4098. // compute
  4099. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4100. add_f32_cuda(src0_ddf_i, src1_ddf_i, dst_ddf_i, ne00*i01_diff, ne10*ne11, cudaStream_main);
  4101. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4102. add_f16_f32_f16_cuda((half *) src0_ddq_i, src1_ddf_i, (half *) dst_ddf_i, ne00*i01_diff, cudaStream_main);
  4103. } else {
  4104. GGML_ASSERT(false);
  4105. }
  4106. (void) src1;
  4107. (void) dst;
  4108. (void) src0_ddq_i;
  4109. (void) i02;
  4110. (void) i1;
  4111. }
  4112. inline void ggml_cuda_op_mul(
  4113. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4114. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4115. cudaStream_t & cudaStream_main){
  4116. GGML_ASSERT(src0_ddf_i != nullptr);
  4117. GGML_ASSERT(src1_ddf_i != nullptr);
  4118. GGML_ASSERT(dst_ddf_i != nullptr);
  4119. const int64_t ne00 = src0->ne[0];
  4120. const int64_t i01_diff = i01_high - i01_low;
  4121. const int64_t ne10 = src1->ne[0];
  4122. const int64_t ne11 = src1->ne[1];
  4123. mul_f32_cuda(src0_ddf_i, src1_ddf_i, dst_ddf_i, ne00*i01_diff, ne10*ne11, cudaStream_main);
  4124. (void) dst;
  4125. (void) src0_ddq_i;
  4126. (void) i02;
  4127. (void) i1;
  4128. }
  4129. inline void ggml_cuda_op_gelu(
  4130. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4131. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4132. cudaStream_t & cudaStream_main){
  4133. GGML_ASSERT(src0_ddf_i != nullptr);
  4134. GGML_ASSERT(dst_ddf_i != nullptr);
  4135. const int64_t ne00 = src0->ne[0];
  4136. const int64_t i01_diff = i01_high - i01_low;
  4137. // compute
  4138. gelu_f32_cuda(src0_ddf_i, dst_ddf_i, ne00*i01_diff, cudaStream_main);
  4139. (void) src1;
  4140. (void) dst;
  4141. (void) src0_ddq_i;
  4142. (void) src1_ddf_i;
  4143. (void) i02;
  4144. (void) i1;
  4145. }
  4146. inline void ggml_cuda_op_silu(
  4147. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4148. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4149. cudaStream_t & cudaStream_main){
  4150. GGML_ASSERT(src0_ddf_i != nullptr);
  4151. GGML_ASSERT(dst_ddf_i != nullptr);
  4152. const int64_t ne00 = src0->ne[0];
  4153. const int64_t i01_diff = i01_high - i01_low;
  4154. // compute
  4155. silu_f32_cuda(src0_ddf_i, dst_ddf_i, ne00*i01_diff, cudaStream_main);
  4156. (void) src1;
  4157. (void) dst;
  4158. (void) src0_ddq_i;
  4159. (void) src1_ddf_i;
  4160. (void) i02;
  4161. (void) i1;
  4162. }
  4163. inline void ggml_cuda_op_norm(
  4164. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4165. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4166. cudaStream_t & cudaStream_main){
  4167. GGML_ASSERT(src0_ddf_i != nullptr);
  4168. GGML_ASSERT(dst_ddf_i != nullptr);
  4169. const int64_t ne00 = src0->ne[0];
  4170. const int64_t i01_diff = i01_high - i01_low;
  4171. // compute
  4172. norm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  4173. (void) src1;
  4174. (void) dst;
  4175. (void) src0_ddq_i;
  4176. (void) src1_ddf_i;
  4177. (void) i02;
  4178. (void) i1;
  4179. }
  4180. inline void ggml_cuda_op_rms_norm(
  4181. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4182. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4183. cudaStream_t & cudaStream_main){
  4184. GGML_ASSERT(src0_ddf_i != nullptr);
  4185. GGML_ASSERT(dst_ddf_i != nullptr);
  4186. const int64_t ne00 = src0->ne[0];
  4187. const int64_t i01_diff = i01_high - i01_low;
  4188. float eps;
  4189. memcpy(&eps, dst->op_params, sizeof(float));
  4190. // compute
  4191. rms_norm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, eps, cudaStream_main);
  4192. (void) src1;
  4193. (void) dst;
  4194. (void) src0_ddq_i;
  4195. (void) src1_ddf_i;
  4196. (void) i02;
  4197. (void) i1;
  4198. }
  4199. inline void ggml_cuda_op_mul_mat_q(
  4200. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4201. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4202. cudaStream_t & cudaStream_main){
  4203. GGML_ASSERT(src0_ddq_i != nullptr);
  4204. GGML_ASSERT(src1_ddf_i != nullptr);
  4205. GGML_ASSERT(dst_ddf_i != nullptr);
  4206. const int64_t ne00 = src0->ne[0];
  4207. const int64_t ne10 = src1->ne[0];
  4208. const int64_t ne11 = src1->ne[1];
  4209. GGML_ASSERT(ne10 % QK8_1 == 0);
  4210. const int64_t ne0 = dst->ne[0];
  4211. const int64_t i01_diff = i01_high - i01_low;
  4212. int id;
  4213. CUDA_CHECK(cudaGetDevice(&id));
  4214. // the main device has a larger memory buffer to hold the results from all GPUs
  4215. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  4216. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : i01_diff;
  4217. const int64_t padded_row_size = ne10 % MATRIX_ROW_PADDING == 0 ?
  4218. ne10 : ne10 - ne10 % MATRIX_ROW_PADDING + MATRIX_ROW_PADDING;
  4219. size_t as;
  4220. void * src1_q8_1 = ggml_cuda_pool_malloc(padded_row_size*ne11*sizeof(block_q8_1)/QK8_1, &as);
  4221. quantize_row_q8_1_cuda(src1_ddf_i, src1_q8_1, ne10, ne11, padded_row_size, cudaStream_main);
  4222. switch (src0->type) {
  4223. case GGML_TYPE_Q4_0:
  4224. ggml_mul_mat_q4_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4225. break;
  4226. case GGML_TYPE_Q4_1:
  4227. ggml_mul_mat_q4_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4228. break;
  4229. case GGML_TYPE_Q5_0:
  4230. ggml_mul_mat_q5_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4231. break;
  4232. case GGML_TYPE_Q5_1:
  4233. ggml_mul_mat_q5_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4234. break;
  4235. case GGML_TYPE_Q8_0:
  4236. ggml_mul_mat_q8_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4237. break;
  4238. case GGML_TYPE_Q2_K:
  4239. ggml_mul_mat_q2_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4240. break;
  4241. case GGML_TYPE_Q3_K:
  4242. ggml_mul_mat_q3_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4243. break;
  4244. case GGML_TYPE_Q4_K:
  4245. ggml_mul_mat_q4_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4246. break;
  4247. case GGML_TYPE_Q5_K:
  4248. ggml_mul_mat_q5_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4249. break;
  4250. case GGML_TYPE_Q6_K:
  4251. ggml_mul_mat_q6_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, i01_diff, ne11, padded_row_size, nrows_dst, cudaStream_main);
  4252. break;
  4253. default:
  4254. GGML_ASSERT(false);
  4255. break;
  4256. }
  4257. ggml_cuda_pool_free(src1_q8_1, as);
  4258. (void) src1;
  4259. (void) dst;
  4260. (void) src0_ddf_i;
  4261. (void) i02;
  4262. (void) i1;
  4263. }
  4264. static int64_t get_row_rounding(ggml_type type) {
  4265. int max_compute_capability = INT_MIN;
  4266. for (int id = 0; id < g_device_count; ++id) {
  4267. if (max_compute_capability < g_compute_capabilities[id]
  4268. && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  4269. max_compute_capability = g_compute_capabilities[id];
  4270. }
  4271. }
  4272. switch(type) {
  4273. case GGML_TYPE_Q4_0:
  4274. case GGML_TYPE_Q4_1:
  4275. return max_compute_capability >= CC_TURING ? 128 : 64;
  4276. case GGML_TYPE_Q5_0:
  4277. case GGML_TYPE_Q5_1:
  4278. case GGML_TYPE_Q8_0:
  4279. return 64;
  4280. case GGML_TYPE_F16:
  4281. return 1;
  4282. case GGML_TYPE_Q2_K:
  4283. case GGML_TYPE_Q3_K:
  4284. case GGML_TYPE_Q4_K:
  4285. case GGML_TYPE_Q5_K:
  4286. return max_compute_capability >= CC_TURING ? 128 : 64;
  4287. case GGML_TYPE_Q6_K:
  4288. return 64;
  4289. default:
  4290. GGML_ASSERT(false);
  4291. }
  4292. }
  4293. inline void ggml_cuda_op_mul_mat_vec(
  4294. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4295. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4296. cudaStream_t & cudaStream_main){
  4297. GGML_ASSERT(src0_ddq_i != nullptr);
  4298. GGML_ASSERT(src1_ddf_i != nullptr);
  4299. GGML_ASSERT(dst_ddf_i != nullptr);
  4300. const int64_t ne00 = src0->ne[0];
  4301. const int64_t nrows = i01_high - i01_low;
  4302. #ifdef GGML_CUDA_FORCE_DMMV
  4303. const bool use_mul_mat_vec_q = false;
  4304. (void) g_compute_capabilities[0];
  4305. #else
  4306. int id;
  4307. CUDA_CHECK(cudaGetDevice(&id));
  4308. bool mul_mat_vec_q_implemented =
  4309. src0->type == GGML_TYPE_Q4_0 ||
  4310. src0->type == GGML_TYPE_Q4_1 ||
  4311. src0->type == GGML_TYPE_Q5_0 ||
  4312. src0->type == GGML_TYPE_Q5_1 ||
  4313. src0->type == GGML_TYPE_Q8_0;
  4314. #if QK_K == 256
  4315. mul_mat_vec_q_implemented = mul_mat_vec_q_implemented ||
  4316. src0->type == GGML_TYPE_Q2_K ||
  4317. src0->type == GGML_TYPE_Q3_K ||
  4318. src0->type == GGML_TYPE_Q4_K ||
  4319. src0->type == GGML_TYPE_Q5_K ||
  4320. src0->type == GGML_TYPE_Q6_K;
  4321. #endif // QK_K == 256
  4322. const bool use_mul_mat_vec_q = g_compute_capabilities[id] >= MIN_CC_DP4A && mul_mat_vec_q_implemented;
  4323. #endif
  4324. if (use_mul_mat_vec_q) {
  4325. const int64_t padded_row_size = ne00 % MATRIX_ROW_PADDING == 0 ?
  4326. ne00 : ne00 - ne00 % MATRIX_ROW_PADDING + MATRIX_ROW_PADDING;
  4327. size_t as;
  4328. void * src1_q8_1 = ggml_cuda_pool_malloc(padded_row_size*sizeof(block_q8_1)/QK8_1, &as);
  4329. quantize_row_q8_1_cuda(src1_ddf_i, src1_q8_1, ne00, 1, padded_row_size, cudaStream_main);
  4330. switch (src0->type) {
  4331. case GGML_TYPE_Q4_0:
  4332. mul_mat_vec_q4_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4333. break;
  4334. case GGML_TYPE_Q4_1:
  4335. mul_mat_vec_q4_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4336. break;
  4337. case GGML_TYPE_Q5_0:
  4338. mul_mat_vec_q5_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4339. break;
  4340. case GGML_TYPE_Q5_1:
  4341. mul_mat_vec_q5_1_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4342. break;
  4343. case GGML_TYPE_Q8_0:
  4344. mul_mat_vec_q8_0_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4345. break;
  4346. case GGML_TYPE_Q2_K:
  4347. mul_mat_vec_q2_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4348. break;
  4349. case GGML_TYPE_Q3_K:
  4350. mul_mat_vec_q3_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4351. break;
  4352. case GGML_TYPE_Q4_K:
  4353. mul_mat_vec_q4_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4354. break;
  4355. case GGML_TYPE_Q5_K:
  4356. mul_mat_vec_q5_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4357. break;
  4358. case GGML_TYPE_Q6_K:
  4359. mul_mat_vec_q6_K_q8_1_cuda(src0_ddq_i, src1_q8_1, dst_ddf_i, ne00, nrows, cudaStream_main);
  4360. break;
  4361. default:
  4362. GGML_ASSERT(false);
  4363. break;
  4364. }
  4365. ggml_cuda_pool_free(src1_q8_1, as);
  4366. } else {
  4367. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  4368. #ifdef GGML_CUDA_F16
  4369. size_t ash;
  4370. dfloat * src1_dfloat = nullptr; // dfloat == half
  4371. bool src1_convert_f16 = src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  4372. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  4373. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  4374. if (src1_convert_f16) {
  4375. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  4376. ggml_cpy_f32_f16_cuda((char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  4377. ne00, 1, sizeof(float), 0, 0,
  4378. ne00, 1, sizeof(half), 0, 0, cudaStream_main);
  4379. }
  4380. #else
  4381. dfloat * src1_dfloat = src1_ddf_i; // dfloat == float, no conversion
  4382. #endif // GGML_CUDA_F16
  4383. switch (src0->type) {
  4384. case GGML_TYPE_Q4_0:
  4385. dequantize_mul_mat_vec_q4_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4386. break;
  4387. case GGML_TYPE_Q4_1:
  4388. dequantize_mul_mat_vec_q4_1_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4389. break;
  4390. case GGML_TYPE_Q5_0:
  4391. dequantize_mul_mat_vec_q5_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4392. break;
  4393. case GGML_TYPE_Q5_1:
  4394. dequantize_mul_mat_vec_q5_1_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4395. break;
  4396. case GGML_TYPE_Q8_0:
  4397. dequantize_mul_mat_vec_q8_0_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4398. break;
  4399. case GGML_TYPE_Q2_K:
  4400. dequantize_mul_mat_vec_q2_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4401. break;
  4402. case GGML_TYPE_Q3_K:
  4403. dequantize_mul_mat_vec_q3_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4404. break;
  4405. case GGML_TYPE_Q4_K:
  4406. dequantize_mul_mat_vec_q4_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4407. break;
  4408. case GGML_TYPE_Q5_K:
  4409. dequantize_mul_mat_vec_q5_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4410. break;
  4411. case GGML_TYPE_Q6_K:
  4412. dequantize_mul_mat_vec_q6_K_cuda(src0_ddq_i, src1_ddf_i, dst_ddf_i, ne00, nrows, cudaStream_main);
  4413. break;
  4414. case GGML_TYPE_F16:
  4415. convert_mul_mat_vec_f16_cuda(src0_ddq_i, src1_dfloat, dst_ddf_i, ne00, nrows, cudaStream_main);
  4416. break;
  4417. default:
  4418. GGML_ASSERT(false);
  4419. break;
  4420. }
  4421. #ifdef GGML_CUDA_F16
  4422. if (src1_convert_f16) {
  4423. ggml_cuda_pool_free(src1_dfloat, ash);
  4424. }
  4425. #endif // GGML_CUDA_F16
  4426. }
  4427. (void) src1;
  4428. (void) dst;
  4429. (void) src0_ddf_i;
  4430. (void) i02;
  4431. (void) i1;
  4432. }
  4433. inline void ggml_cuda_op_mul_mat_cublas(
  4434. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4435. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4436. cudaStream_t & cudaStream_main){
  4437. GGML_ASSERT(src0_ddf_i != nullptr);
  4438. GGML_ASSERT(src1_ddf_i != nullptr);
  4439. GGML_ASSERT(dst_ddf_i != nullptr);
  4440. const float alpha = 1.0f;
  4441. const float beta = 0.0f;
  4442. const int64_t ne00 = src0->ne[0];
  4443. const int64_t ne10 = src1->ne[0];
  4444. const int64_t ne11 = src1->ne[1];
  4445. const int64_t ne0 = dst->ne[0];
  4446. const int64_t i01_diff = i01_high - i01_low;
  4447. int id;
  4448. CUDA_CHECK(cudaGetDevice(&id));
  4449. // the main device has a larger memory buffer to hold the results from all GPUs
  4450. // ldc == nrows of the matrix that cuBLAS writes into
  4451. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : i01_diff;
  4452. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], cudaStream_main));
  4453. CUBLAS_CHECK(
  4454. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  4455. i01_diff, ne11, ne10,
  4456. &alpha, src0_ddf_i, ne00,
  4457. src1_ddf_i, ne10,
  4458. &beta, dst_ddf_i, ldc));
  4459. (void) dst;
  4460. (void) src0_ddq_i;
  4461. (void) i02;
  4462. (void) i1;
  4463. }
  4464. inline void ggml_cuda_op_rope(
  4465. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4466. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4467. cudaStream_t & cudaStream_main){
  4468. GGML_ASSERT(src0_ddf_i != nullptr);
  4469. GGML_ASSERT(dst_ddf_i != nullptr);
  4470. const int64_t ne00 = src0->ne[0];
  4471. const int64_t ne01 = src0->ne[1];
  4472. const int64_t i01_diff = i01_high - i01_low;
  4473. const int n_past = ((int32_t *) dst->op_params)[0];
  4474. const int n_dims = ((int32_t *) dst->op_params)[1];
  4475. const int mode = ((int32_t *) dst->op_params)[2];
  4476. const int n_ctx = ((int32_t *) dst->op_params)[3];
  4477. // RoPE alteration for extended context
  4478. float freq_base, freq_scale;
  4479. memcpy(&freq_base, (int32_t *) dst->op_params + 4, sizeof(float));
  4480. memcpy(&freq_scale, (int32_t *) dst->op_params + 5, sizeof(float));
  4481. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  4482. const bool is_neox = mode & 2;
  4483. const bool is_glm = mode & 4;
  4484. // compute
  4485. if (is_glm) {
  4486. const float p = (((mode & 1) == 0 ? n_past + i02 : i02)) * freq_scale;
  4487. const float id_p = min(p, n_ctx - 2.f);
  4488. const float block_p = max(p - (n_ctx - 2.f), 0.f);
  4489. rope_glm_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, id_p, block_p, theta_scale, cudaStream_main);
  4490. } else if (is_neox) {
  4491. GGML_ASSERT(ne00 == n_dims && "ne00 != n_dims is not implemented for CUDA yet");
  4492. const float p0 = (((mode & 1) == 0 ? n_past : 0)) * freq_scale;
  4493. rope_neox_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, p0, freq_scale, ne01, theta_scale, cudaStream_main);
  4494. } else {
  4495. const float p0 = (((mode & 1) == 0 ? n_past : 0)) * freq_scale;
  4496. rope_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, p0, freq_scale, ne01, theta_scale, cudaStream_main);
  4497. }
  4498. (void) src1;
  4499. (void) dst;
  4500. (void) src0_ddq_i;
  4501. (void) src1_ddf_i;
  4502. (void) i1;
  4503. }
  4504. inline void ggml_cuda_op_alibi(
  4505. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4506. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4507. cudaStream_t & cudaStream_main){
  4508. GGML_ASSERT(src0_ddf_i != nullptr);
  4509. GGML_ASSERT(dst_ddf_i != nullptr);
  4510. const int64_t ne00 = src0->ne[0];
  4511. const int64_t ne01 = src0->ne[1];
  4512. const int64_t ne02 = src0->ne[2];
  4513. const int64_t i01_diff = i01_high - i01_low;
  4514. const int n_past = ((int32_t *) dst->op_params)[0];
  4515. const int n_head = ((int32_t *) dst->op_params)[1];
  4516. float max_bias;
  4517. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  4518. GGML_ASSERT(ne01 + n_past == ne00);
  4519. GGML_ASSERT(n_head == ne02);
  4520. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  4521. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  4522. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  4523. // compute
  4524. alibi_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, ne01, n_heads_log2_floor, m0, m1, cudaStream_main);
  4525. (void) src1;
  4526. (void) src0_ddq_i;
  4527. (void) src1_ddf_i;
  4528. (void) i1;
  4529. }
  4530. inline void ggml_cuda_op_diag_mask_inf(
  4531. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4532. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4533. cudaStream_t & cudaStream_main){
  4534. GGML_ASSERT(src0_ddf_i != nullptr);
  4535. GGML_ASSERT(dst_ddf_i != nullptr);
  4536. const int64_t ne00 = src0->ne[0];
  4537. const int64_t ne01 = src0->ne[1];
  4538. const int64_t i01_diff = i01_high - i01_low;
  4539. const int n_past = ((int32_t *) dst->op_params)[0];
  4540. // compute
  4541. diag_mask_inf_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, ne01, n_past, cudaStream_main);
  4542. (void) src1;
  4543. (void) dst;
  4544. (void) src0_ddq_i;
  4545. (void) src1_ddf_i;
  4546. (void) i02;
  4547. (void) i1;
  4548. }
  4549. inline void ggml_cuda_op_soft_max(
  4550. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4551. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4552. cudaStream_t & cudaStream_main){
  4553. GGML_ASSERT(src0_ddf_i != nullptr);
  4554. GGML_ASSERT(dst_ddf_i != nullptr);
  4555. const int64_t ne00 = src0->ne[0];
  4556. const int64_t i01_diff = i01_high - i01_low;
  4557. // compute
  4558. soft_max_f32_cuda(src0_ddf_i, dst_ddf_i, ne00, i01_diff, cudaStream_main);
  4559. (void) src1;
  4560. (void) dst;
  4561. (void) src0_ddq_i;
  4562. (void) src1_ddf_i;
  4563. (void) i02;
  4564. (void) i1;
  4565. }
  4566. inline void ggml_cuda_op_scale(
  4567. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, char * src0_ddq_i,
  4568. float * src0_ddf_i, float * src1_ddf_i, float * dst_ddf_i, int64_t i02, int64_t i01_low, int64_t i01_high, int i1,
  4569. cudaStream_t & cudaStream_main){
  4570. GGML_ASSERT(src0_ddf_i != nullptr);
  4571. GGML_ASSERT(dst_ddf_i != nullptr);
  4572. const float scale = ((float *) src1->data)[0];
  4573. const int64_t ne00 = src0->ne[0];
  4574. const int64_t i01_diff = i01_high - i01_low;
  4575. // compute
  4576. scale_f32_cuda(src0_ddf_i, dst_ddf_i, scale, ne00*i01_diff, cudaStream_main);
  4577. CUDA_CHECK(cudaGetLastError());
  4578. (void) src1;
  4579. (void) dst;
  4580. (void) src0_ddq_i;
  4581. (void) src1_ddf_i;
  4582. (void) i02;
  4583. (void) i1;
  4584. }
  4585. static void ggml_cuda_op(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4586. ggml_cuda_op_t op, bool src0_needs_f32, bool flatten_rows) {
  4587. const int64_t ne00 = src0->ne[0];
  4588. const int64_t ne01 = src0->ne[1];
  4589. const int64_t ne02 = src0->ne[2];
  4590. const int64_t ne03 = src0->ne[3];
  4591. const int64_t nrows0 = ggml_nrows(src0);
  4592. const bool use_src1 = src1 != nullptr;
  4593. const int64_t ne10 = use_src1 ? src1->ne[0] : 1;
  4594. const int64_t ne11 = use_src1 ? src1->ne[1] : 1;
  4595. const int64_t ne12 = use_src1 ? src1->ne[2] : 1;
  4596. const int64_t ne13 = use_src1 ? src1->ne[3] : 1;
  4597. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  4598. GGML_ASSERT(ne03 == ne13);
  4599. const int64_t ne0 = dst->ne[0];
  4600. const int64_t ne1 = dst->ne[1];
  4601. const int nb2 = dst->nb[2];
  4602. const int nb3 = dst->nb[3];
  4603. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  4604. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  4605. // strides for iteration over dims 3 and 2
  4606. const int64_t num_iters_0 = ne02 >= ne12 ? ne02*ne03 : ne12*ne13;
  4607. const int64_t num_iters = flatten_rows ? 1 : num_iters_0;
  4608. const int64_t stride_mod = flatten_rows ? num_iters_0 : 1;
  4609. const int64_t src0_stride = ne00 * ne01 * stride_mod;
  4610. const int64_t src1_stride = ne10 * ne11 * stride_mod;
  4611. const int64_t dst_stride = ne0 * ne1 * stride_mod;
  4612. const int64_t rows_per_iter = flatten_rows ? nrows0 : ne01;
  4613. const int64_t i03_max = flatten_rows ? 1 : ne03;
  4614. const int64_t i02_max = flatten_rows ? 1 : (ne02 >= ne12 ? ne02 : ne12);
  4615. const int64_t i02_divisor = ne02 >= ne12 ? 1 : ne12 / ne02;
  4616. GGML_ASSERT(!(flatten_rows && ne02 < ne12));
  4617. const size_t src0_ts = ggml_type_size(src0->type);
  4618. const size_t src0_bs = ggml_blck_size(src0->type);
  4619. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  4620. struct ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  4621. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  4622. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  4623. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  4624. const bool src0_is_f32 = src0->type == GGML_TYPE_F32;
  4625. const bool src1_is_contiguous = use_src1 && ggml_is_contiguous(src1);
  4626. const bool src1_stays_on_host = use_src1 && (
  4627. dst->op == GGML_OP_SCALE || dst->op == GGML_OP_DIAG_MASK_INF || dst->op == GGML_OP_ROPE);
  4628. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  4629. GGML_ASSERT(!(split && ne02 < ne12));
  4630. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  4631. // dd = data device
  4632. char * src0_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // quantized
  4633. float * src0_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  4634. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr};
  4635. float * dst_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr};
  4636. // asq = actual size quantized, asf = actual size float
  4637. size_t src0_asq[GGML_CUDA_MAX_DEVICES] = {0};
  4638. size_t src0_asf[GGML_CUDA_MAX_DEVICES] = {0};
  4639. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  4640. size_t dst_asf[GGML_CUDA_MAX_DEVICES] = {0};
  4641. // if multiple devices are used they need to wait for the main device
  4642. // here an event is recorded that signifies that the main device has finished calculating the input data
  4643. if (split && g_device_count > 1) {
  4644. CUDA_CHECK(cudaSetDevice(g_main_device));
  4645. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device], g_cudaStreams_main[g_main_device]));
  4646. }
  4647. for (int id = 0; id < g_device_count; ++id) {
  4648. if (!split && id != g_main_device) {
  4649. continue;
  4650. }
  4651. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  4652. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  4653. int64_t row_low, row_high;
  4654. if (split) {
  4655. const int64_t rounding = get_row_rounding(src0->type);
  4656. row_low = id == 0 ? 0 : nrows0*g_tensor_split[id];
  4657. row_low -= row_low % rounding;
  4658. if (id == g_device_count - 1) {
  4659. row_high = nrows0;
  4660. } else {
  4661. row_high = nrows0*g_tensor_split[id + 1];
  4662. row_high -= row_high % rounding;
  4663. }
  4664. } else {
  4665. row_low = 0;
  4666. row_high = nrows0*i02_divisor;
  4667. }
  4668. if (row_low == row_high) {
  4669. continue;
  4670. }
  4671. int64_t row_diff = row_high - row_low;
  4672. cudaSetDevice(id);
  4673. cudaStream_t cudaStream_main = g_cudaStreams_main[id];
  4674. // wait for main GPU data if necessary
  4675. if (split && id != g_main_device) {
  4676. CUDA_CHECK(cudaStreamWaitEvent(cudaStream_main, src0_extra->events[g_main_device]));
  4677. }
  4678. if (src0_on_device && src0_is_contiguous) {
  4679. if (src0_is_f32) {
  4680. src0_ddf[id] = (float *) src0_extra->data_device[id];
  4681. } else {
  4682. src0_ddq[id] = (char *) src0_extra->data_device[id];
  4683. }
  4684. } else {
  4685. if (src0_is_f32) {
  4686. src0_ddf[id] = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_asf[id]);
  4687. } else {
  4688. src0_ddq[id] = (char *) ggml_cuda_pool_malloc(row_diff*ne00 * src0_ts/src0_bs, &src0_asq[id]);
  4689. }
  4690. }
  4691. if (src0_needs_f32 && !src0_is_f32) {
  4692. src0_ddf[id] = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_asf[id]);
  4693. }
  4694. if (use_src1 && !src1_stays_on_host) {
  4695. if (src1_on_device && src1_is_contiguous) {
  4696. src1_ddf[id] = (float *) src1_extra->data_device[id];
  4697. } else {
  4698. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(num_iters*src1_stride * sizeof(float), &src1_asf[id]);
  4699. }
  4700. }
  4701. if (dst_on_device) {
  4702. dst_ddf[id] = (float *) dst_extra->data_device[id];
  4703. } else {
  4704. size_t size_dst_ddf = split ? row_diff*ne1 * sizeof(float) : num_iters*dst_stride * sizeof(float);
  4705. dst_ddf[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_asf[id]);
  4706. }
  4707. for (int64_t i03 = 0; i03 < i03_max; i03++) {
  4708. const int64_t i13 = i03 % ne13;
  4709. for (int64_t i02 = 0; i02 < i02_max; i02++) {
  4710. const int64_t i12 = i02 % ne12;
  4711. const int64_t i0 = i03*i02_max + i02;
  4712. // i0 values that contain the lower/upper rows for a split tensor when using multiple GPUs
  4713. const int64_t i0_offset_low = row_low/rows_per_iter;
  4714. const int64_t i0_offset_high = row_high/rows_per_iter;
  4715. int64_t i01_low = 0;
  4716. int64_t i01_high = rows_per_iter;
  4717. if (split) {
  4718. if (i0 < i0_offset_low || i0 > i0_offset_high) {
  4719. continue;
  4720. }
  4721. if (i0 == i0_offset_low) {
  4722. i01_low = row_low % rows_per_iter;
  4723. }
  4724. if (i0 == i0_offset_high) {
  4725. i01_high = row_high % rows_per_iter;
  4726. }
  4727. }
  4728. // There is possibly a bug in the Windows nvcc compiler regarding instruction reordering or optimizing out local variables.
  4729. // Removing the first assert or changing the order of the arguments causes the second assert to fail.
  4730. // Removing both asserts results in i01_high becoming 0 which in turn results in garbage output.
  4731. // The root cause seems to be a problem with i0_offset_high becoming 0 when it should always be >0 (for single GPU).
  4732. GGML_ASSERT(i01_low == 0 || g_device_count > 1);
  4733. GGML_ASSERT(i01_high == rows_per_iter || g_device_count > 1);
  4734. const int64_t i01_diff = i01_high - i01_low;
  4735. if (i01_diff == 0) {
  4736. continue;
  4737. }
  4738. const int64_t i11 = i13*ne12 + i12;
  4739. // for split tensors the data begins at i0 == i0_offset_low
  4740. char * src0_ddq_i = src0_ddq[id] + (i0/i02_divisor - i0_offset_low)*src0_stride*src0_ts/src0_bs;
  4741. float * src0_ddf_i = src0_ddf[id] + (i0/i02_divisor - i0_offset_low)*src0_stride;
  4742. float * src1_ddf_i = src1_ddf[id] + i11*src1_stride;
  4743. float * dst_ddf_i = dst_ddf[id] + (i0 - i0_offset_low)*dst_stride;
  4744. // for split tensors the data pointer needs to be rounded down
  4745. // to the bin edge for i03, i02 bins beyond the first
  4746. if (i0 - i0_offset_low > 0) {
  4747. GGML_ASSERT(!flatten_rows);
  4748. src0_ddq_i -= (row_low % ne01)*ne00 * src0_ts/src0_bs;
  4749. src0_ddf_i -= (row_low % ne01)*ne00;
  4750. dst_ddf_i -= (row_low % ne0)*ne1;
  4751. }
  4752. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  4753. // in that case an offset on dst_ddf_i is needed
  4754. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  4755. dst_ddf_i += i01_low; // offset is 0 if no tensor split
  4756. }
  4757. // copy src0, src1 to device if necessary
  4758. if (use_src1 && !src1_stays_on_host) {
  4759. if (src1->backend == GGML_BACKEND_CPU) {
  4760. GGML_ASSERT(!flatten_rows || nrows0 == ggml_nrows(src1));
  4761. int64_t nrows1 = flatten_rows ? nrows0 : ne11;
  4762. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf_i, src1, i03, i02, 0, nrows1, cudaStream_main));
  4763. } else if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  4764. if (id != g_main_device) {
  4765. GGML_ASSERT(!flatten_rows);
  4766. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  4767. src1_ddf_i_source += i11*src1_stride;
  4768. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_stride*sizeof(float),
  4769. cudaMemcpyDeviceToDevice, cudaStream_main));
  4770. }
  4771. } else if (src1_on_device && !src1_is_contiguous) {
  4772. GGML_ASSERT(!split);
  4773. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf_i, src1, i03, i02, 0, ne11, cudaStream_main));
  4774. } else {
  4775. GGML_ASSERT(false);
  4776. }
  4777. }
  4778. if ((!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  4779. if (src0_is_f32) {
  4780. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf_i, src0, i03, i02/i02_divisor, i01_low, i01_high, cudaStream_main));
  4781. } else {
  4782. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddq_i, src0, i03, i02/i02_divisor, i01_low, i01_high, cudaStream_main));
  4783. }
  4784. }
  4785. // convert src0 to f32 if it is necessary for the ggml_cuda_op
  4786. if (src0_needs_f32 && !src0_is_f32) {
  4787. to_fp32_cuda(src0_ddq_i, src0_ddf_i, i01_diff*ne00, cudaStream_main);
  4788. CUDA_CHECK(cudaGetLastError());
  4789. }
  4790. // do the computation
  4791. op(src0, src1, dst, src0_ddq_i, src0_ddf_i, src1_ddf_i, dst_ddf_i, i02, i01_low, i01_high, i11, cudaStream_main);
  4792. CUDA_CHECK(cudaGetLastError());
  4793. // copy dst to host or other device if necessary
  4794. if (!dst_on_device) {
  4795. void * dst_off_device;
  4796. cudaMemcpyKind kind;
  4797. if (dst->backend == GGML_BACKEND_CPU) {
  4798. dst_off_device = dst->data;
  4799. kind = cudaMemcpyDeviceToHost;
  4800. } else if (dst->backend == GGML_BACKEND_GPU) {
  4801. dst_off_device = dst_extra->data_device[g_main_device];
  4802. kind = cudaMemcpyDeviceToDevice;
  4803. } else {
  4804. GGML_ASSERT(false);
  4805. }
  4806. if (split) {
  4807. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  4808. // dst is NOT transposed.
  4809. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  4810. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  4811. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  4812. float * dhf_dst_i = (float *) ((char *) dst_off_device + i01_low*sizeof(float) + i02*nb2 + i03*nb3);
  4813. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_ddf_i, i01_diff*sizeof(float),
  4814. i01_diff*sizeof(float), ne1, kind, cudaStream_main));
  4815. } else {
  4816. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  4817. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_ddf_i, dst_stride*sizeof(float), kind, cudaStream_main));
  4818. }
  4819. }
  4820. // signify to main device that other device is done
  4821. if (split && g_device_count > 1 && id != g_main_device) {
  4822. CUDA_CHECK(cudaEventRecord(src0_extra->events[id], cudaStream_main));
  4823. }
  4824. }
  4825. }
  4826. }
  4827. // wait until each device is finished, then free their buffers
  4828. for (int id = 0; id < g_device_count; ++id) {
  4829. if (src0_asq[id] == 0 && src0_asf[id] == 0 && src1_asf[id] == 0 && dst_asf[id] == 0) {
  4830. continue;
  4831. }
  4832. CUDA_CHECK(cudaSetDevice(id));
  4833. if (src0_asq[id] > 0) {
  4834. ggml_cuda_pool_free(src0_ddq[id], src0_asq[id]);
  4835. }
  4836. if (src0_asf[id] > 0) {
  4837. ggml_cuda_pool_free(src0_ddf[id], src0_asf[id]);
  4838. }
  4839. if (src1_asf[id] > 0) {
  4840. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  4841. }
  4842. if (dst_asf[id] > 0) {
  4843. ggml_cuda_pool_free(dst_ddf[id], dst_asf[id]);
  4844. }
  4845. }
  4846. // main device waits for all other devices to be finished
  4847. if (split && g_device_count > 1) {
  4848. CUDA_CHECK(cudaSetDevice(g_main_device));
  4849. for (int id = 0; id < g_device_count; ++id) {
  4850. if (id != g_main_device && src0_extra->events[id]) {
  4851. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams_main[g_main_device], src0_extra->events[id]));
  4852. }
  4853. }
  4854. }
  4855. if (dst->backend == GGML_BACKEND_CPU) {
  4856. CUDA_CHECK(cudaSetDevice(g_main_device));
  4857. CUDA_CHECK(cudaDeviceSynchronize());
  4858. }
  4859. }
  4860. void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4861. // ggml_cuda_add permits f16 dst even though this could in theory cause problems with the pointer arithmetic in ggml_cuda_op.
  4862. // Due to flatten_rows == true this does in practice not make a difference however.
  4863. // Better solution would be nice but right now that would require disproportionate changes.
  4864. GGML_ASSERT(
  4865. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16) &&
  4866. src1->type == GGML_TYPE_F32 &&
  4867. (dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16));
  4868. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_add, false, true);
  4869. }
  4870. void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4871. GGML_ASSERT(src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4872. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul, true, false); // TODO ggml_cuda_op needs modification for flatten
  4873. }
  4874. void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4875. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4876. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_gelu, true, true);
  4877. }
  4878. void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4879. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4880. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_silu, true, true);
  4881. }
  4882. void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4883. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4884. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_norm, true, true);
  4885. }
  4886. void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4887. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4888. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_rms_norm, true, true);
  4889. }
  4890. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  4891. const int64_t ne10 = src1->ne[0];
  4892. const int64_t ne0 = dst->ne[0];
  4893. const int64_t ne1 = dst->ne[1];
  4894. // TODO: find the optimal values for these
  4895. if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  4896. src1->type == GGML_TYPE_F32 &&
  4897. dst->type == GGML_TYPE_F32 &&
  4898. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32)) {
  4899. return true;
  4900. }
  4901. return false;
  4902. }
  4903. void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  4904. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4905. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  4906. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  4907. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  4908. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4909. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4910. const int64_t ne00 = src0->ne[0];
  4911. const int64_t ne01 = src0->ne[1];
  4912. const int64_t ne02 = src0->ne[2];
  4913. const int64_t ne12 = src1->ne[2];
  4914. CUDA_CHECK(cudaSetDevice(g_main_device));
  4915. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  4916. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  4917. void * src0_ddq = src0_extra->data_device[g_main_device];
  4918. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  4919. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  4920. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  4921. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  4922. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, cudaStream_main);
  4923. }
  4924. void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  4925. GGML_ASSERT(!ggml_is_contiguous(src0) && ggml_is_contiguous(src1));
  4926. GGML_ASSERT(!ggml_is_permuted(src0));
  4927. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  4928. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4929. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4930. const int64_t ne00 = src0->ne[0];
  4931. const int64_t ne01 = src0->ne[1];
  4932. const int64_t ne02 = src0->ne[2];
  4933. const int64_t ne12 = src1->ne[2];
  4934. const int64_t nb01 = src0->nb[1];
  4935. const int64_t nb02 = src0->nb[2];
  4936. CUDA_CHECK(cudaSetDevice(g_main_device));
  4937. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  4938. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  4939. void * src0_ddq = src0_extra->data_device[g_main_device];
  4940. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  4941. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  4942. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  4943. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  4944. const int row_stride_x = nb01 / sizeof(half);
  4945. const int channel_stride_x = nb02 / sizeof(half);
  4946. ggml_mul_mat_vec_nc_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, row_stride_x, ne02, ne12, channel_stride_x, cudaStream_main);
  4947. }
  4948. void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4949. bool all_on_device = (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  4950. src1->backend == GGML_BACKEND_GPU && dst->backend == GGML_BACKEND_GPU;
  4951. if (all_on_device && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  4952. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  4953. } else if (all_on_device && !ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && src1->ne[1] == 1) {
  4954. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  4955. }else if (src0->type == GGML_TYPE_F32) {
  4956. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, true, false);
  4957. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  4958. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  4959. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_vec, false, false);
  4960. } else {
  4961. int min_compute_capability = INT_MAX;
  4962. for (int id = 0; id < g_device_count; ++id) {
  4963. if (min_compute_capability > g_compute_capabilities[id]
  4964. && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  4965. min_compute_capability = g_compute_capabilities[id];
  4966. }
  4967. }
  4968. if (g_mul_mat_q && ggml_is_quantized(src0->type) && min_compute_capability >= MIN_CC_DP4A) {
  4969. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_q, false, false);
  4970. } else {
  4971. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, true, false);
  4972. }
  4973. }
  4974. } else {
  4975. GGML_ASSERT(false);
  4976. }
  4977. }
  4978. void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4979. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  4980. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_scale, true, true);
  4981. }
  4982. void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  4983. const int64_t ne = ggml_nelements(src0);
  4984. GGML_ASSERT(ne == ggml_nelements(src1));
  4985. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  4986. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  4987. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  4988. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  4989. const int64_t ne00 = src0->ne[0];
  4990. const int64_t ne01 = src0->ne[1];
  4991. GGML_ASSERT(src0->ne[3] == 1);
  4992. const int64_t nb00 = src0->nb[0];
  4993. const int64_t nb01 = src0->nb[1];
  4994. const int64_t nb02 = src0->nb[2];
  4995. const int64_t ne10 = src1->ne[0];
  4996. const int64_t ne11 = src1->ne[1];
  4997. GGML_ASSERT(src1->ne[3] == 1);
  4998. const int64_t nb10 = src1->nb[0];
  4999. const int64_t nb11 = src1->nb[1];
  5000. const int64_t nb12 = src1->nb[2];
  5001. CUDA_CHECK(cudaSetDevice(g_main_device));
  5002. cudaStream_t cudaStream_main = g_cudaStreams_main[g_main_device];
  5003. const struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5004. const struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5005. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  5006. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  5007. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  5008. ggml_cpy_f32_f32_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  5009. ne10, ne11, nb10, nb11, nb12, cudaStream_main);
  5010. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  5011. ggml_cpy_f32_f16_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  5012. ne10, ne11, nb10, nb11, nb12, cudaStream_main);
  5013. } else {
  5014. GGML_ASSERT(false);
  5015. }
  5016. (void) dst;
  5017. }
  5018. void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5019. ggml_cuda_cpy(src0, dst, nullptr);
  5020. (void) src1;
  5021. }
  5022. void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5023. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  5024. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_diag_mask_inf, true, true);
  5025. }
  5026. void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5027. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  5028. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_soft_max, true, true);
  5029. }
  5030. void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5031. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  5032. const int mode = ((int32_t *) dst->op_params)[2];
  5033. const bool is_glm = mode & 4;
  5034. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_rope, true, !is_glm); // flatten support not implemented for glm
  5035. }
  5036. void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5037. GGML_ASSERT(src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  5038. ggml_cuda_op(src0, src1, dst, ggml_cuda_op_alibi, true, true);
  5039. }
  5040. void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5041. (void) src0;
  5042. (void) src1;
  5043. (void) dst;
  5044. }
  5045. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  5046. int nrows = ggml_nrows(tensor);
  5047. const int64_t ne0 = tensor->ne[0];
  5048. const size_t nb1 = tensor->nb[1];
  5049. ggml_backend backend = tensor->backend;
  5050. struct ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  5051. memset(extra, 0, sizeof(*extra));
  5052. for (int id = 0; id < g_device_count; ++id) {
  5053. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  5054. continue;
  5055. }
  5056. cudaSetDevice(id);
  5057. int row_low, row_high;
  5058. if (backend == GGML_BACKEND_GPU) {
  5059. row_low = 0;
  5060. row_high = nrows;
  5061. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  5062. const int64_t rounding = get_row_rounding(tensor->type);
  5063. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  5064. row_low -= row_low % rounding;
  5065. if (id == g_device_count - 1) {
  5066. row_high = nrows;
  5067. } else {
  5068. row_high = nrows*g_tensor_split[id + 1];
  5069. row_high -= row_high % rounding;
  5070. }
  5071. } else {
  5072. GGML_ASSERT(false);
  5073. }
  5074. if (row_low == row_high) {
  5075. continue;
  5076. }
  5077. int64_t nrows_split = row_high - row_low;
  5078. const size_t offset_split = row_low*nb1;
  5079. size_t size = ggml_nbytes_split(tensor, nrows_split);
  5080. const size_t original_size = size;
  5081. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  5082. if (ne0 % MATRIX_ROW_PADDING != 0) {
  5083. size += (MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING)
  5084. * ggml_type_size(tensor->type)/ggml_blck_size(tensor->type);
  5085. }
  5086. char * buf;
  5087. CUDA_CHECK(cudaMalloc(&buf, size));
  5088. char * buf_host = (char*)data + offset_split;
  5089. // set padding to 0 to avoid possible NaN values
  5090. if (size > original_size) {
  5091. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  5092. }
  5093. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  5094. extra->data_device[id] = buf;
  5095. if (backend == GGML_BACKEND_GPU_SPLIT) {
  5096. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id], cudaEventDisableTiming));
  5097. }
  5098. }
  5099. tensor->extra = extra;
  5100. }
  5101. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  5102. if (!tensor || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  5103. return;
  5104. }
  5105. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5106. for (int id = 0; id < g_device_count; ++id) {
  5107. if (extra->data_device[id] != nullptr) {
  5108. CUDA_CHECK(cudaSetDevice(id));
  5109. CUDA_CHECK(cudaFree(extra->data_device[id]));
  5110. }
  5111. if (extra->events[id] != nullptr) {
  5112. CUDA_CHECK(cudaSetDevice(id));
  5113. CUDA_CHECK(cudaEventDestroy(extra->events[id]));
  5114. }
  5115. }
  5116. delete extra;
  5117. }
  5118. static struct ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  5119. static size_t g_temp_tensor_extra_index = 0;
  5120. static struct ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  5121. if (g_temp_tensor_extras == nullptr) {
  5122. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_MAX_NODES];
  5123. }
  5124. size_t alloc_index = g_temp_tensor_extra_index;
  5125. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_MAX_NODES;
  5126. struct ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  5127. memset(extra, 0, sizeof(*extra));
  5128. return extra;
  5129. }
  5130. void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  5131. if (scratch && g_scratch_size == 0) {
  5132. return;
  5133. }
  5134. // recursively assign CUDA buffers until a compute tensor is found
  5135. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  5136. const ggml_op src0_op = tensor->src[0]->op;
  5137. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  5138. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  5139. }
  5140. }
  5141. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  5142. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  5143. }
  5144. tensor->backend = GGML_BACKEND_GPU;
  5145. if (scratch && no_alloc) {
  5146. return;
  5147. }
  5148. struct ggml_tensor_extra_gpu * extra;
  5149. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  5150. tensor->op == GGML_OP_VIEW ||
  5151. force_inplace;
  5152. const size_t size = ggml_nbytes(tensor);
  5153. CUDA_CHECK(cudaSetDevice(g_main_device));
  5154. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  5155. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  5156. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  5157. size_t offset = 0;
  5158. if (tensor->op == GGML_OP_VIEW) {
  5159. memcpy(&offset, tensor->op_params, sizeof(size_t));
  5160. }
  5161. extra = ggml_cuda_alloc_temp_tensor_extra();
  5162. extra->data_device[g_main_device] = src0_ddc + offset;
  5163. } else if (tensor->op == GGML_OP_CPY) {
  5164. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  5165. void * src1_ddv = src1_extra->data_device[g_main_device];
  5166. extra = ggml_cuda_alloc_temp_tensor_extra();
  5167. extra->data_device[g_main_device] = src1_ddv;
  5168. } else if (scratch) {
  5169. GGML_ASSERT(size <= g_scratch_size);
  5170. if (g_scratch_offset + size > g_scratch_size) {
  5171. g_scratch_offset = 0;
  5172. }
  5173. char * data = (char *) g_scratch_buffer;
  5174. if (data == nullptr) {
  5175. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  5176. g_scratch_buffer = data;
  5177. }
  5178. extra = ggml_cuda_alloc_temp_tensor_extra();
  5179. extra->data_device[g_main_device] = data + g_scratch_offset;
  5180. g_scratch_offset += size;
  5181. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  5182. } else { // allocate new buffers outside of scratch
  5183. void * data;
  5184. CUDA_CHECK(cudaMalloc(&data, size));
  5185. CUDA_CHECK(cudaMemset(data, 0, size));
  5186. extra = new ggml_tensor_extra_gpu;
  5187. memset(extra, 0, sizeof(*extra));
  5188. extra->data_device[g_main_device] = data;
  5189. }
  5190. tensor->extra = extra;
  5191. }
  5192. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  5193. if (g_scratch_size == 0) {
  5194. return;
  5195. }
  5196. if (g_scratch_buffer == nullptr) {
  5197. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  5198. }
  5199. struct ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  5200. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  5201. tensor->op == GGML_OP_VIEW;
  5202. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  5203. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  5204. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  5205. size_t view_offset = 0;
  5206. if (tensor->op == GGML_OP_VIEW) {
  5207. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  5208. }
  5209. extra->data_device[g_main_device] = src0_ddc + view_offset;
  5210. } else {
  5211. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  5212. }
  5213. tensor->extra = extra;
  5214. }
  5215. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  5216. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  5217. }
  5218. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  5219. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  5220. }
  5221. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  5222. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  5223. }
  5224. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  5225. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  5226. }
  5227. void ggml_cuda_set_main_device(int main_device) {
  5228. if (main_device >= g_device_count) {
  5229. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  5230. main_device, g_device_count, g_main_device);
  5231. return;
  5232. }
  5233. g_main_device = main_device;
  5234. if (g_device_count > 1) {
  5235. cudaDeviceProp prop;
  5236. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  5237. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  5238. }
  5239. }
  5240. void ggml_cuda_set_mul_mat_q(bool mul_mat_q) {
  5241. g_mul_mat_q = mul_mat_q;
  5242. }
  5243. void ggml_cuda_set_scratch_size(size_t scratch_size) {
  5244. g_scratch_size = scratch_size;
  5245. }
  5246. void ggml_cuda_free_scratch() {
  5247. if (g_scratch_buffer == nullptr) {
  5248. return;
  5249. }
  5250. CUDA_CHECK(cudaFree(g_scratch_buffer));
  5251. g_scratch_buffer = nullptr;
  5252. }
  5253. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor){
  5254. ggml_cuda_func_t func;
  5255. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  5256. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  5257. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  5258. switch (tensor->op) {
  5259. case GGML_OP_DUP:
  5260. if (!any_on_device) {
  5261. return false;
  5262. }
  5263. func = ggml_cuda_dup;
  5264. break;
  5265. case GGML_OP_ADD:
  5266. if (!any_on_device) {
  5267. return false;
  5268. }
  5269. func = ggml_cuda_add;
  5270. break;
  5271. case GGML_OP_MUL:
  5272. if (!any_on_device) {
  5273. return false;
  5274. }
  5275. func = ggml_cuda_mul;
  5276. break;
  5277. case GGML_OP_UNARY:
  5278. switch (ggml_get_unary_op(tensor)) {
  5279. case GGML_UNARY_OP_GELU:
  5280. if (!any_on_device) {
  5281. return false;
  5282. }
  5283. func = ggml_cuda_gelu;
  5284. break;
  5285. case GGML_UNARY_OP_SILU:
  5286. if (!any_on_device) {
  5287. return false;
  5288. }
  5289. func = ggml_cuda_silu;
  5290. break;
  5291. default:
  5292. return false;
  5293. } break;
  5294. case GGML_OP_NORM:
  5295. if (!any_on_device) {
  5296. return false;
  5297. }
  5298. func = ggml_cuda_norm;
  5299. break;
  5300. case GGML_OP_RMS_NORM:
  5301. if (!any_on_device) {
  5302. return false;
  5303. }
  5304. func = ggml_cuda_rms_norm;
  5305. break;
  5306. case GGML_OP_MUL_MAT:
  5307. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  5308. return false;
  5309. }
  5310. func = ggml_cuda_mul_mat;
  5311. break;
  5312. case GGML_OP_SCALE:
  5313. if (!any_on_device) {
  5314. return false;
  5315. }
  5316. func = ggml_cuda_scale;
  5317. break;
  5318. case GGML_OP_CPY:
  5319. if (!any_on_device) {
  5320. return false;
  5321. }
  5322. func = ggml_cuda_cpy;
  5323. break;
  5324. case GGML_OP_CONT:
  5325. if (!any_on_device) {
  5326. return false;
  5327. }
  5328. func = ggml_cuda_dup;
  5329. break;
  5330. case GGML_OP_RESHAPE:
  5331. case GGML_OP_VIEW:
  5332. case GGML_OP_PERMUTE:
  5333. case GGML_OP_TRANSPOSE:
  5334. if (!any_on_device) {
  5335. return false;
  5336. }
  5337. func = ggml_cuda_nop;
  5338. break;
  5339. case GGML_OP_DIAG_MASK_INF:
  5340. if (!any_on_device) {
  5341. return false;
  5342. }
  5343. func = ggml_cuda_diag_mask_inf;
  5344. break;
  5345. case GGML_OP_SOFT_MAX:
  5346. if (!any_on_device) {
  5347. return false;
  5348. }
  5349. func = ggml_cuda_soft_max;
  5350. break;
  5351. case GGML_OP_ROPE:
  5352. if (!any_on_device) {
  5353. return false;
  5354. }
  5355. func = ggml_cuda_rope;
  5356. break;
  5357. case GGML_OP_ALIBI:
  5358. if (!any_on_device) {
  5359. return false;
  5360. }
  5361. func = ggml_cuda_alibi;
  5362. break;
  5363. default:
  5364. return false;
  5365. }
  5366. if (params->ith != 0) {
  5367. return true;
  5368. }
  5369. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  5370. return true;
  5371. }
  5372. func(tensor->src[0], tensor->src[1], tensor);
  5373. return true;
  5374. }
  5375. int ggml_cuda_get_device_count() {
  5376. int device_count;
  5377. CUDA_CHECK(cudaGetDeviceCount(&device_count));
  5378. return device_count;
  5379. }
  5380. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  5381. cudaDeviceProp prop;
  5382. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  5383. snprintf(description, description_size, "%s", prop.name);
  5384. }