ggml-cuda.cu 239 KB

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