ggml-cuda.cu 310 KB

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