ggml-cuda.cu 288 KB

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