ggml-cuda.cu 251 KB

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