ggml-cuda.cu 238 KB

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