ggml-cuda.cu 265 KB

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