ggml-cuda.cu 365 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006
  1. #include <algorithm>
  2. #include <assert.h>
  3. #include <atomic>
  4. #include <cinttypes>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <float.h>
  8. #include <limits>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <vector>
  12. #if defined(GGML_USE_HIPBLAS)
  13. #include <hip/hip_runtime.h>
  14. #include <hipblas/hipblas.h>
  15. #include <hip/hip_fp16.h>
  16. #ifdef __HIP_PLATFORM_AMD__
  17. // for rocblas_initialize()
  18. #include "rocblas/rocblas.h"
  19. #endif // __HIP_PLATFORM_AMD__
  20. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  21. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  22. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  23. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  24. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  25. #define CUBLAS_OP_N HIPBLAS_OP_N
  26. #define CUBLAS_OP_T HIPBLAS_OP_T
  27. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  28. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  29. #define CUDA_R_16F HIPBLAS_R_16F
  30. #define CUDA_R_32F HIPBLAS_R_32F
  31. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  32. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  33. #define cublasCreate hipblasCreate
  34. #define cublasGemmEx hipblasGemmEx
  35. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  36. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  37. #define cublasHandle_t hipblasHandle_t
  38. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  39. #define cublasSetStream hipblasSetStream
  40. #define cublasSgemm hipblasSgemm
  41. #define cublasStatus_t hipblasStatus_t
  42. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  43. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  44. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  45. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  46. #define cudaDeviceProp hipDeviceProp_t
  47. #define cudaDeviceSynchronize hipDeviceSynchronize
  48. #define cudaError_t hipError_t
  49. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  50. #define cudaEventDisableTiming hipEventDisableTiming
  51. #define cudaEventRecord hipEventRecord
  52. #define cudaEvent_t hipEvent_t
  53. #define cudaEventDestroy hipEventDestroy
  54. #define cudaFree hipFree
  55. #define cudaFreeHost hipHostFree
  56. #define cudaGetDevice hipGetDevice
  57. #define cudaGetDeviceCount hipGetDeviceCount
  58. #define cudaGetDeviceProperties hipGetDeviceProperties
  59. #define cudaGetErrorString hipGetErrorString
  60. #define cudaGetLastError hipGetLastError
  61. #ifdef GGML_HIP_UMA
  62. #define cudaMalloc hipMallocManaged
  63. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  64. #else
  65. #define cudaMalloc hipMalloc
  66. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  67. #endif
  68. #define cudaMemcpy hipMemcpy
  69. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  70. #define cudaMemcpyAsync hipMemcpyAsync
  71. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  72. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  73. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  74. #define cudaMemcpyKind hipMemcpyKind
  75. #define cudaMemset hipMemset
  76. #define cudaMemsetAsync hipMemsetAsync
  77. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  78. #define cudaSetDevice hipSetDevice
  79. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  80. #define cudaStreamFireAndForget hipStreamFireAndForget
  81. #define cudaStreamNonBlocking hipStreamNonBlocking
  82. #define cudaStreamSynchronize hipStreamSynchronize
  83. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  84. #define cudaStream_t hipStream_t
  85. #define cudaSuccess hipSuccess
  86. #define __trap abort
  87. #else
  88. #include <cuda_runtime.h>
  89. #include <cublas_v2.h>
  90. #include <cuda_fp16.h>
  91. #endif // defined(GGML_USE_HIPBLAS)
  92. #include "ggml-cuda.h"
  93. #include "ggml.h"
  94. #include "ggml-backend-impl.h"
  95. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  96. #define CC_VOLTA 700
  97. #define CC_OFFSET_AMD 1000000
  98. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  99. #define GGML_CUDA_MAX_NODES 8192
  100. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  101. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  102. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  103. // - 7B quantum model: +100-200 MB
  104. // - 13B quantum model: +200-400 MB
  105. //
  106. //#define GGML_CUDA_FORCE_MMQ
  107. // TODO: improve this to be correct for more hardware
  108. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  109. // probably other such cases, and not sure what happens on AMD hardware
  110. #if !defined(GGML_CUDA_FORCE_MMQ)
  111. #define CUDA_USE_TENSOR_CORES
  112. #endif
  113. // max batch size to use MMQ kernels when tensor cores are available
  114. #define MMQ_MAX_BATCH_SIZE 32
  115. #if defined(GGML_USE_HIPBLAS)
  116. #define __CUDA_ARCH__ 1300
  117. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  118. defined(__gfx1150__) || defined(__gfx1151__)
  119. #define RDNA3
  120. #endif
  121. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  122. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  123. #define RDNA2
  124. #endif
  125. #ifndef __has_builtin
  126. #define __has_builtin(x) 0
  127. #endif
  128. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  129. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  130. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  131. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  132. #if __has_builtin(__builtin_elementwise_sub_sat)
  133. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  134. return reinterpret_cast<const int&>(c);
  135. #else
  136. int8x4_t c;
  137. int16_t tmp;
  138. #pragma unroll
  139. for (int i = 0; i < 4; i++) {
  140. tmp = va[i] - vb[i];
  141. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  142. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  143. c[i] = tmp;
  144. }
  145. return reinterpret_cast<int&>(c);
  146. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  147. }
  148. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  149. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  150. c = __builtin_amdgcn_sdot4(a, b, c, false);
  151. #elif defined(__gfx1100__)
  152. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  153. #elif defined(__gfx1010__) || defined(__gfx900__)
  154. int tmp1;
  155. int tmp2;
  156. asm("\n \
  157. 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 \
  158. 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 \
  159. v_add3_u32 %0, %1, %2, %0 \n \
  160. 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 \
  161. 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 \
  162. v_add3_u32 %0, %1, %2, %0 \n \
  163. "
  164. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  165. : "v"(a), "v"(b)
  166. );
  167. #else
  168. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  169. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  170. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  171. #endif
  172. return c;
  173. }
  174. #endif // defined(GGML_USE_HIPBLAS)
  175. #if defined(_MSC_VER)
  176. #pragma warning(disable: 4244 4267) // possible loss of data
  177. #endif
  178. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  179. #define CUDA_CHECK(err) \
  180. do { \
  181. cudaError_t err_ = (err); \
  182. if (err_ != cudaSuccess) { \
  183. int id; \
  184. cudaGetDevice(&id); \
  185. fprintf(stderr, "\nCUDA error %d at %s:%d: %s\n", err_, __FILE__, __LINE__, \
  186. cudaGetErrorString(err_)); \
  187. fprintf(stderr, "current device: %d\n", id); \
  188. GGML_ASSERT(!"CUDA error"); \
  189. } \
  190. } while (0)
  191. #if CUDART_VERSION >= 12000
  192. #define CUBLAS_CHECK(err) \
  193. do { \
  194. cublasStatus_t err_ = (err); \
  195. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  196. int id; \
  197. cudaGetDevice(&id); \
  198. fprintf(stderr, "\ncuBLAS error %d at %s:%d: %s\n", \
  199. err_, __FILE__, __LINE__, cublasGetStatusString(err_)); \
  200. fprintf(stderr, "current device: %d\n", id); \
  201. GGML_ASSERT(!"cuBLAS error"); \
  202. } \
  203. } while (0)
  204. #else
  205. #define CUBLAS_CHECK(err) \
  206. do { \
  207. cublasStatus_t err_ = (err); \
  208. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  209. int id; \
  210. cudaGetDevice(&id); \
  211. fprintf(stderr, "\ncuBLAS error %d at %s:%d\n", err_, __FILE__, __LINE__); \
  212. fprintf(stderr, "current device: %d\n", id); \
  213. GGML_ASSERT(!"cuBLAS error"); \
  214. } \
  215. } while (0)
  216. #endif // CUDART_VERSION >= 11
  217. #if CUDART_VERSION >= 11100
  218. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  219. #else
  220. #define GGML_CUDA_ASSUME(x)
  221. #endif // CUDART_VERSION >= 11100
  222. #ifdef GGML_CUDA_F16
  223. typedef half dfloat; // dequantize float
  224. typedef half2 dfloat2;
  225. #else
  226. typedef float dfloat; // dequantize float
  227. typedef float2 dfloat2;
  228. #endif //GGML_CUDA_F16
  229. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  230. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  231. int x32 = 0;
  232. x32 |= x16[0] << 0;
  233. x32 |= x16[1] << 16;
  234. return x32;
  235. }
  236. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  237. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  238. int x32 = 0;
  239. x32 |= x16[0] << 0;
  240. x32 |= x16[1] << 16;
  241. return x32;
  242. }
  243. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  244. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  245. }
  246. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  247. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  248. }
  249. template<typename T>
  250. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  251. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  252. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  253. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  254. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  255. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  256. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  257. typedef void (*ggml_cuda_op_mul_mat_t)(
  258. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  259. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  260. const int64_t src1_padded_row_size, const cudaStream_t & stream);
  261. typedef void (*ggml_cuda_op_flatten_t)(
  262. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  263. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream);
  264. // QK = number of values after dequantization
  265. // QR = QK / number of values before dequantization
  266. // QI = number of 32 bit integers before dequantization
  267. #define QK4_0 32
  268. #define QR4_0 2
  269. #define QI4_0 (QK4_0 / (4 * QR4_0))
  270. typedef struct {
  271. half d; // delta
  272. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  273. } block_q4_0;
  274. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  275. #define QK4_1 32
  276. #define QR4_1 2
  277. #define QI4_1 (QK4_1 / (4 * QR4_1))
  278. typedef struct {
  279. half2 dm; // dm.x = delta, dm.y = min
  280. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  281. } block_q4_1;
  282. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  283. #define QK5_0 32
  284. #define QR5_0 2
  285. #define QI5_0 (QK5_0 / (4 * QR5_0))
  286. typedef struct {
  287. half d; // delta
  288. uint8_t qh[4]; // 5-th bit of quants
  289. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  290. } block_q5_0;
  291. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  292. #define QK5_1 32
  293. #define QR5_1 2
  294. #define QI5_1 (QK5_1 / (4 * QR5_1))
  295. typedef struct {
  296. half2 dm; // dm.x = delta, dm.y = min
  297. uint8_t qh[4]; // 5-th bit of quants
  298. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  299. } block_q5_1;
  300. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  301. #define QK8_0 32
  302. #define QR8_0 1
  303. #define QI8_0 (QK8_0 / (4 * QR8_0))
  304. typedef struct {
  305. half d; // delta
  306. int8_t qs[QK8_0]; // quants
  307. } block_q8_0;
  308. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  309. #define QK8_1 32
  310. #define QR8_1 1
  311. #define QI8_1 (QK8_1 / (4 * QR8_1))
  312. typedef struct {
  313. half2 ds; // ds.x = delta, ds.y = sum
  314. int8_t qs[QK8_0]; // quants
  315. } block_q8_1;
  316. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  317. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  318. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  319. typedef void (*load_tiles_cuda_t)(
  320. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  321. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  322. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  323. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  324. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  325. //================================= k-quants
  326. #ifdef GGML_QKK_64
  327. #define QK_K 64
  328. #define K_SCALE_SIZE 4
  329. #else
  330. #define QK_K 256
  331. #define K_SCALE_SIZE 12
  332. #endif
  333. #define QR2_K 4
  334. #define QI2_K (QK_K / (4*QR2_K))
  335. typedef struct {
  336. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  337. uint8_t qs[QK_K/4]; // quants
  338. half2 dm; // super-block scale for quantized scales/mins
  339. } block_q2_K;
  340. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  341. #define QR3_K 4
  342. #define QI3_K (QK_K / (4*QR3_K))
  343. typedef struct {
  344. uint8_t hmask[QK_K/8]; // quants - high bit
  345. uint8_t qs[QK_K/4]; // quants - low 2 bits
  346. #ifdef GGML_QKK_64
  347. uint8_t scales[2]; // scales, quantized with 8 bits
  348. #else
  349. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  350. #endif
  351. half d; // super-block scale
  352. } block_q3_K;
  353. //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");
  354. #define QR4_K 2
  355. #define QI4_K (QK_K / (4*QR4_K))
  356. #ifdef GGML_QKK_64
  357. typedef struct {
  358. half dm[2]; // super-block scales/mins
  359. uint8_t scales[2]; // 4-bit block scales/mins
  360. uint8_t qs[QK_K/2]; // 4--bit quants
  361. } block_q4_K;
  362. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  363. #else
  364. typedef struct {
  365. half2 dm; // super-block scale for quantized scales/mins
  366. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  367. uint8_t qs[QK_K/2]; // 4--bit quants
  368. } block_q4_K;
  369. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  370. #endif
  371. #define QR5_K 2
  372. #define QI5_K (QK_K / (4*QR5_K))
  373. #ifdef GGML_QKK_64
  374. typedef struct {
  375. half d; // super-block scale
  376. int8_t scales[QK_K/16]; // block scales
  377. uint8_t qh[QK_K/8]; // quants, high bit
  378. uint8_t qs[QK_K/2]; // quants, low 4 bits
  379. } block_q5_K;
  380. 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");
  381. #else
  382. typedef struct {
  383. half2 dm; // super-block scale for quantized scales/mins
  384. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  385. uint8_t qh[QK_K/8]; // quants, high bit
  386. uint8_t qs[QK_K/2]; // quants, low 4 bits
  387. } block_q5_K;
  388. 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");
  389. #endif
  390. #define QR6_K 2
  391. #define QI6_K (QK_K / (4*QR6_K))
  392. typedef struct {
  393. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  394. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  395. int8_t scales[QK_K/16]; // scales
  396. half d; // delta
  397. } block_q6_K;
  398. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  399. #define WARP_SIZE 32
  400. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  401. #define CUDA_GELU_BLOCK_SIZE 256
  402. #define CUDA_SILU_BLOCK_SIZE 256
  403. #define CUDA_TANH_BLOCK_SIZE 256
  404. #define CUDA_RELU_BLOCK_SIZE 256
  405. #define CUDA_SQR_BLOCK_SIZE 256
  406. #define CUDA_CPY_BLOCK_SIZE 32
  407. #define CUDA_SCALE_BLOCK_SIZE 256
  408. #define CUDA_CLAMP_BLOCK_SIZE 256
  409. #define CUDA_ROPE_BLOCK_SIZE 256
  410. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  411. #define CUDA_ALIBI_BLOCK_SIZE 32
  412. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  413. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  414. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  415. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  416. #define CUDA_UPSCALE_BLOCK_SIZE 256
  417. #define CUDA_CONCAT_BLOCK_SIZE 256
  418. #define CUDA_PAD_BLOCK_SIZE 256
  419. #define CUDA_ACC_BLOCK_SIZE 256
  420. #define CUDA_IM2COL_BLOCK_SIZE 256
  421. // dmmv = dequantize_mul_mat_vec
  422. #ifndef GGML_CUDA_DMMV_X
  423. #define GGML_CUDA_DMMV_X 32
  424. #endif
  425. #ifndef GGML_CUDA_MMV_Y
  426. #define GGML_CUDA_MMV_Y 1
  427. #endif
  428. #ifndef K_QUANTS_PER_ITERATION
  429. #define K_QUANTS_PER_ITERATION 2
  430. #else
  431. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  432. #endif
  433. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  434. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  435. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  436. #define MUL_MAT_SRC1_COL_STRIDE 128
  437. #define MAX_STREAMS 8
  438. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  439. struct ggml_tensor_extra_gpu {
  440. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  441. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  442. };
  443. // this is faster on Windows
  444. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  445. inline cudaError_t ggml_cuda_set_device(const int device) {
  446. int current_device;
  447. CUDA_CHECK(cudaGetDevice(&current_device));
  448. if (device == current_device) {
  449. return cudaSuccess;
  450. }
  451. return cudaSetDevice(device);
  452. }
  453. static int g_device_count = -1;
  454. static int g_main_device = 0;
  455. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  456. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  457. static void * g_scratch_buffer = nullptr;
  458. static size_t g_scratch_size = 0; // disabled by default
  459. static size_t g_scratch_offset = 0;
  460. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  461. [[noreturn]]
  462. static __device__ void bad_arch() {
  463. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  464. __trap();
  465. (void) bad_arch; // suppress unused function warning
  466. }
  467. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  468. #pragma unroll
  469. for (int mask = 16; mask > 0; mask >>= 1) {
  470. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  471. }
  472. return x;
  473. }
  474. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  475. #pragma unroll
  476. for (int mask = 16; mask > 0; mask >>= 1) {
  477. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  478. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  479. }
  480. return a;
  481. }
  482. static __device__ __forceinline__ float warp_reduce_max(float x) {
  483. #pragma unroll
  484. for (int mask = 16; mask > 0; mask >>= 1) {
  485. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  486. }
  487. return x;
  488. }
  489. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  490. return b;
  491. }
  492. static __device__ __forceinline__ float op_add(const float a, const float b) {
  493. return a + b;
  494. }
  495. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  496. return a * b;
  497. }
  498. static __device__ __forceinline__ float op_div(const float a, const float b) {
  499. return a / b;
  500. }
  501. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  502. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  503. int ne0, int ne1, int ne2, int ne3,
  504. int ne10, int ne11, int ne12, int ne13,
  505. /*int s0, */ int s1, int s2, int s3,
  506. /*int s10,*/ int s11, int s12, int s13) {
  507. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  508. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  509. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  510. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  511. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  512. return;
  513. }
  514. const int i11 = i1 % ne11;
  515. const int i12 = i2 % ne12;
  516. const int i13 = i3 % ne13;
  517. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  518. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  519. const size_t i_dst = i_src0;
  520. const src0_t * src0_row = src0 + i_src0;
  521. const src1_t * src1_row = src1 + i_src1;
  522. dst_t * dst_row = dst + i_dst;
  523. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  524. const int i10 = i0 % ne10;
  525. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  526. }
  527. }
  528. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  529. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  530. int ne0, int ne1, int ne2, int ne3,
  531. int ne10, int ne11, int ne12, int ne13,
  532. /*int s0, */ int s1, int s2, int s3,
  533. /*int s10,*/ int s11, int s12, int s13) {
  534. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  535. const int i3 = i/(ne2*ne1*ne0);
  536. const int i2 = (i/(ne1*ne0)) % ne2;
  537. const int i1 = (i/ne0) % ne1;
  538. const int i0 = i % ne0;
  539. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  540. return;
  541. }
  542. const int i11 = i1 % ne11;
  543. const int i12 = i2 % ne12;
  544. const int i13 = i3 % ne13;
  545. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  546. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  547. const size_t i_dst = i_src0;
  548. const src0_t * src0_row = src0 + i_src0;
  549. const src1_t * src1_row = src1 + i_src1;
  550. dst_t * dst_row = dst + i_dst;
  551. const int i10 = i0 % ne10;
  552. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  553. }
  554. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  555. const int ne10, const int ne11, const int ne12,
  556. const int nb1, const int nb2, int offset) {
  557. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  558. if (i >= ne) {
  559. return;
  560. }
  561. int src1_idx = i - offset;
  562. int oz = src1_idx / nb2;
  563. int oy = (src1_idx - (oz * nb2)) / nb1;
  564. int ox = src1_idx % nb1;
  565. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  566. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  567. } else {
  568. dst[i] = x[i];
  569. }
  570. }
  571. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  572. const float GELU_COEF_A = 0.044715f;
  573. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  574. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  575. if (i >= k) {
  576. return;
  577. }
  578. float xi = x[i];
  579. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  580. }
  581. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  582. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  583. if (i >= k) {
  584. return;
  585. }
  586. dst[i] = x[i] / (1.0f + expf(-x[i]));
  587. }
  588. static __global__ void gelu_quick_f32(const float *x, float *dst, int k) {
  589. const float GELU_QUICK_COEF = -1.702f;
  590. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  591. if (i >= k) {
  592. return;
  593. }
  594. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  595. }
  596. static __global__ void tanh_f32(const float *x, float *dst, int k) {
  597. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  598. if (i >= k) {
  599. return;
  600. }
  601. dst[i] = tanhf(x[i]);
  602. }
  603. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  604. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  605. if (i >= k) {
  606. return;
  607. }
  608. dst[i] = fmaxf(x[i], 0);
  609. }
  610. static __global__ void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope) {
  611. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  612. if (i >= k) {
  613. return;
  614. }
  615. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  616. }
  617. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  618. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  619. if (i >= k) {
  620. return;
  621. }
  622. dst[i] = x[i] * x[i];
  623. }
  624. template <int block_size>
  625. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  626. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  627. const int tid = threadIdx.x;
  628. float2 mean_var = make_float2(0.f, 0.f);
  629. for (int col = tid; col < ncols; col += block_size) {
  630. const float xi = x[row*ncols + col];
  631. mean_var.x += xi;
  632. mean_var.y += xi * xi;
  633. }
  634. // sum up partial sums
  635. mean_var = warp_reduce_sum(mean_var);
  636. if (block_size > WARP_SIZE) {
  637. __shared__ float2 s_sum[32];
  638. int warp_id = threadIdx.x / WARP_SIZE;
  639. int lane_id = threadIdx.x % WARP_SIZE;
  640. if (lane_id == 0) {
  641. s_sum[warp_id] = mean_var;
  642. }
  643. __syncthreads();
  644. mean_var = s_sum[lane_id];
  645. mean_var = warp_reduce_sum(mean_var);
  646. }
  647. const float mean = mean_var.x / ncols;
  648. const float var = mean_var.y / ncols - mean * mean;
  649. const float inv_std = rsqrtf(var + eps);
  650. for (int col = tid; col < ncols; col += block_size) {
  651. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  652. }
  653. }
  654. static __global__ void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02) {
  655. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  656. if (nidx >= ne0) {
  657. return;
  658. }
  659. // operation
  660. int offset_dst =
  661. nidx +
  662. blockIdx.y * ne0 +
  663. blockIdx.z * ne0 * gridDim.y;
  664. if (blockIdx.z < ne02) { // src0
  665. int offset_src =
  666. nidx +
  667. blockIdx.y * ne0 +
  668. blockIdx.z * ne0 * gridDim.y;
  669. dst[offset_dst] = x[offset_src];
  670. } else {
  671. int offset_src =
  672. nidx +
  673. blockIdx.y * ne0 +
  674. (blockIdx.z - ne02) * ne0 * gridDim.y;
  675. dst[offset_dst] = y[offset_src];
  676. }
  677. }
  678. static __global__ void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor) {
  679. int ne0 = ne00 * scale_factor;
  680. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  681. if (nidx >= ne0) {
  682. return;
  683. }
  684. // operation
  685. int i00 = nidx / scale_factor;
  686. int i01 = blockIdx.y / scale_factor;
  687. int offset_src =
  688. i00 +
  689. i01 * ne00 +
  690. blockIdx.z * nb02;
  691. int offset_dst =
  692. nidx +
  693. blockIdx.y * ne0 +
  694. blockIdx.z * ne0 * gridDim.y;
  695. dst[offset_dst] = x[offset_src];
  696. }
  697. static __global__ void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  698. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  699. if (nidx >= ne0) {
  700. return;
  701. }
  702. // operation
  703. int offset_dst =
  704. nidx +
  705. blockIdx.y * ne0 +
  706. blockIdx.z * ne0 * gridDim.y;
  707. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  708. int offset_src =
  709. nidx +
  710. blockIdx.y * ne00 +
  711. blockIdx.z * ne00 * ne01;
  712. dst[offset_dst] = x[offset_src];
  713. } else {
  714. dst[offset_dst] = 0.0f;
  715. }
  716. }
  717. template <int block_size>
  718. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  719. int start = blockIdx.x * group_size;
  720. int end = start + group_size;
  721. start += threadIdx.x;
  722. if (end >= ne_elements) {
  723. end = ne_elements;
  724. }
  725. float tmp = 0.0f; // partial sum for thread in warp
  726. for (int j = start; j < end; j += block_size) {
  727. tmp += x[j];
  728. }
  729. tmp = warp_reduce_sum(tmp);
  730. if (block_size > WARP_SIZE) {
  731. __shared__ float s_sum[32];
  732. int warp_id = threadIdx.x / WARP_SIZE;
  733. int lane_id = threadIdx.x % WARP_SIZE;
  734. if (lane_id == 0) {
  735. s_sum[warp_id] = tmp;
  736. }
  737. __syncthreads();
  738. tmp = s_sum[lane_id];
  739. tmp = warp_reduce_sum(tmp);
  740. }
  741. float mean = tmp / group_size;
  742. tmp = 0.0f;
  743. for (int j = start; j < end; j += block_size) {
  744. float xi = x[j] - mean;
  745. dst[j] = xi;
  746. tmp += xi * xi;
  747. }
  748. tmp = warp_reduce_sum(tmp);
  749. if (block_size > WARP_SIZE) {
  750. __shared__ float s_sum[32];
  751. int warp_id = threadIdx.x / WARP_SIZE;
  752. int lane_id = threadIdx.x % WARP_SIZE;
  753. if (lane_id == 0) {
  754. s_sum[warp_id] = tmp;
  755. }
  756. __syncthreads();
  757. tmp = s_sum[lane_id];
  758. tmp = warp_reduce_sum(tmp);
  759. }
  760. float variance = tmp / group_size;
  761. float scale = rsqrtf(variance + eps);
  762. for (int j = start; j < end; j += block_size) {
  763. dst[j] *= scale;
  764. }
  765. }
  766. template <int block_size>
  767. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  768. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  769. const int tid = threadIdx.x;
  770. float tmp = 0.0f; // partial sum for thread in warp
  771. for (int col = tid; col < ncols; col += block_size) {
  772. const float xi = x[row*ncols + col];
  773. tmp += xi * xi;
  774. }
  775. // sum up partial sums
  776. tmp = warp_reduce_sum(tmp);
  777. if (block_size > WARP_SIZE) {
  778. __shared__ float s_sum[32];
  779. int warp_id = threadIdx.x / WARP_SIZE;
  780. int lane_id = threadIdx.x % WARP_SIZE;
  781. if (lane_id == 0) {
  782. s_sum[warp_id] = tmp;
  783. }
  784. __syncthreads();
  785. tmp = s_sum[lane_id];
  786. tmp = warp_reduce_sum(tmp);
  787. }
  788. const float mean = tmp / ncols;
  789. const float scale = rsqrtf(mean + eps);
  790. for (int col = tid; col < ncols; col += block_size) {
  791. dst[row*ncols + col] = scale * x[row*ncols + col];
  792. }
  793. }
  794. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  795. const block_q4_0 * x = (const block_q4_0 *) vx;
  796. const dfloat d = x[ib].d;
  797. const int vui = x[ib].qs[iqs];
  798. v.x = vui & 0xF;
  799. v.y = vui >> 4;
  800. #ifdef GGML_CUDA_F16
  801. v = __hsub2(v, {8.0f, 8.0f});
  802. v = __hmul2(v, {d, d});
  803. #else
  804. v.x = (v.x - 8.0f) * d;
  805. v.y = (v.y - 8.0f) * d;
  806. #endif // GGML_CUDA_F16
  807. }
  808. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  809. const block_q4_1 * x = (const block_q4_1 *) vx;
  810. const dfloat d = __low2half(x[ib].dm);
  811. const dfloat m = __high2half(x[ib].dm);
  812. const int vui = x[ib].qs[iqs];
  813. v.x = vui & 0xF;
  814. v.y = vui >> 4;
  815. #ifdef GGML_CUDA_F16
  816. v = __hmul2(v, {d, d});
  817. v = __hadd2(v, {m, m});
  818. #else
  819. v.x = (v.x * d) + m;
  820. v.y = (v.y * d) + m;
  821. #endif // GGML_CUDA_F16
  822. }
  823. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  824. const block_q5_0 * x = (const block_q5_0 *) vx;
  825. const dfloat d = x[ib].d;
  826. uint32_t qh;
  827. memcpy(&qh, x[ib].qh, sizeof(qh));
  828. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  829. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  830. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  831. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  832. #ifdef GGML_CUDA_F16
  833. v = __hsub2(v, {16.0f, 16.0f});
  834. v = __hmul2(v, {d, d});
  835. #else
  836. v.x = (v.x - 16.0f) * d;
  837. v.y = (v.y - 16.0f) * d;
  838. #endif // GGML_CUDA_F16
  839. }
  840. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  841. const block_q5_1 * x = (const block_q5_1 *) vx;
  842. const dfloat d = __low2half(x[ib].dm);
  843. const dfloat m = __high2half(x[ib].dm);
  844. uint32_t qh;
  845. memcpy(&qh, x[ib].qh, sizeof(qh));
  846. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  847. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  848. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  849. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  850. #ifdef GGML_CUDA_F16
  851. v = __hmul2(v, {d, d});
  852. v = __hadd2(v, {m, m});
  853. #else
  854. v.x = (v.x * d) + m;
  855. v.y = (v.y * d) + m;
  856. #endif // GGML_CUDA_F16
  857. }
  858. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  859. const block_q8_0 * x = (const block_q8_0 *) vx;
  860. const dfloat d = x[ib].d;
  861. v.x = x[ib].qs[iqs + 0];
  862. v.y = x[ib].qs[iqs + 1];
  863. #ifdef GGML_CUDA_F16
  864. v = __hmul2(v, {d, d});
  865. #else
  866. v.x *= d;
  867. v.y *= d;
  868. #endif // GGML_CUDA_F16
  869. }
  870. //================================== k-quants
  871. template<typename dst_t>
  872. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  873. const int i = blockIdx.x;
  874. const block_q2_K * x = (const block_q2_K *) vx;
  875. const int tid = threadIdx.x;
  876. #if QK_K == 256
  877. const int n = tid/32;
  878. const int l = tid - 32*n;
  879. const int is = 8*n + l/16;
  880. const uint8_t q = x[i].qs[32*n + l];
  881. dst_t * y = yy + i*QK_K + 128*n;
  882. float dall = __low2half(x[i].dm);
  883. float dmin = __high2half(x[i].dm);
  884. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  885. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  886. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  887. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  888. #else
  889. const int is = tid/16; // 0 or 1
  890. const int il = tid%16; // 0...15
  891. const uint8_t q = x[i].qs[il] >> (2*is);
  892. dst_t * y = yy + i*QK_K + 16*is + il;
  893. float dall = __low2half(x[i].dm);
  894. float dmin = __high2half(x[i].dm);
  895. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  896. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  897. #endif
  898. }
  899. template<typename dst_t>
  900. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  901. const int i = blockIdx.x;
  902. const block_q3_K * x = (const block_q3_K *) vx;
  903. #if QK_K == 256
  904. const int r = threadIdx.x/4;
  905. const int tid = r/2;
  906. const int is0 = r%2;
  907. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  908. const int n = tid / 4;
  909. const int j = tid - 4*n;
  910. uint8_t m = 1 << (4*n + j);
  911. int is = 8*n + 2*j + is0;
  912. int shift = 2*j;
  913. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  914. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  915. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  916. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  917. float d_all = x[i].d;
  918. float dl = d_all * (us - 32);
  919. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  920. const uint8_t * q = x[i].qs + 32*n;
  921. const uint8_t * hm = x[i].hmask;
  922. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  923. #else
  924. const int tid = threadIdx.x;
  925. const int is = tid/16; // 0 or 1
  926. const int il = tid%16; // 0...15
  927. const int im = il/8; // 0...1
  928. const int in = il%8; // 0...7
  929. dst_t * y = yy + i*QK_K + 16*is + il;
  930. const uint8_t q = x[i].qs[il] >> (2*is);
  931. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  932. const float d = (float)x[i].d;
  933. if (is == 0) {
  934. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  935. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  936. } else {
  937. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  938. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  939. }
  940. #endif
  941. }
  942. #if QK_K == 256
  943. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  944. if (j < 4) {
  945. d = q[j] & 63; m = q[j + 4] & 63;
  946. } else {
  947. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  948. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  949. }
  950. }
  951. #endif
  952. template<typename dst_t>
  953. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  954. const block_q4_K * x = (const block_q4_K *) vx;
  955. const int i = blockIdx.x;
  956. #if QK_K == 256
  957. // assume 32 threads
  958. const int tid = threadIdx.x;
  959. const int il = tid/8;
  960. const int ir = tid%8;
  961. const int is = 2*il;
  962. const int n = 4;
  963. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  964. const float dall = __low2half(x[i].dm);
  965. const float dmin = __high2half(x[i].dm);
  966. const uint8_t * q = x[i].qs + 32*il + n*ir;
  967. uint8_t sc, m;
  968. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  969. const float d1 = dall * sc; const float m1 = dmin * m;
  970. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  971. const float d2 = dall * sc; const float m2 = dmin * m;
  972. for (int l = 0; l < n; ++l) {
  973. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  974. y[l +32] = d2 * (q[l] >> 4) - m2;
  975. }
  976. #else
  977. const int tid = threadIdx.x;
  978. const uint8_t * q = x[i].qs;
  979. dst_t * y = yy + i*QK_K;
  980. const float d = (float)x[i].dm[0];
  981. const float m = (float)x[i].dm[1];
  982. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  983. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  984. #endif
  985. }
  986. template<typename dst_t>
  987. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  988. const block_q5_K * x = (const block_q5_K *) vx;
  989. const int i = blockIdx.x;
  990. #if QK_K == 256
  991. // assume 64 threads - this is very slightly better than the one below
  992. const int tid = threadIdx.x;
  993. const int il = tid/16; // il is in 0...3
  994. const int ir = tid%16; // ir is in 0...15
  995. const int is = 2*il; // is is in 0...6
  996. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  997. const float dall = __low2half(x[i].dm);
  998. const float dmin = __high2half(x[i].dm);
  999. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1000. const uint8_t * qh = x[i].qh + 2*ir;
  1001. uint8_t sc, m;
  1002. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1003. const float d1 = dall * sc; const float m1 = dmin * m;
  1004. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1005. const float d2 = dall * sc; const float m2 = dmin * m;
  1006. uint8_t hm = 1 << (2*il);
  1007. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1008. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1009. hm <<= 1;
  1010. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1011. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1012. #else
  1013. const int tid = threadIdx.x;
  1014. const uint8_t q = x[i].qs[tid];
  1015. const int im = tid/8; // 0...3
  1016. const int in = tid%8; // 0...7
  1017. const int is = tid/16; // 0 or 1
  1018. const uint8_t h = x[i].qh[in] >> im;
  1019. const float d = x[i].d;
  1020. dst_t * y = yy + i*QK_K + tid;
  1021. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1022. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1023. #endif
  1024. }
  1025. template<typename dst_t>
  1026. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1027. const block_q6_K * x = (const block_q6_K *) vx;
  1028. const int i = blockIdx.x;
  1029. #if QK_K == 256
  1030. // assume 64 threads - this is very slightly better than the one below
  1031. const int tid = threadIdx.x;
  1032. const int ip = tid/32; // ip is 0 or 1
  1033. const int il = tid - 32*ip; // 0...32
  1034. const int is = 8*ip + il/16;
  1035. dst_t * y = yy + i*QK_K + 128*ip + il;
  1036. const float d = x[i].d;
  1037. const uint8_t * ql = x[i].ql + 64*ip + il;
  1038. const uint8_t qh = x[i].qh[32*ip + il];
  1039. const int8_t * sc = x[i].scales + is;
  1040. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1041. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1042. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1043. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1044. #else
  1045. // assume 32 threads
  1046. const int tid = threadIdx.x;
  1047. const int ip = tid/16; // 0 or 1
  1048. const int il = tid - 16*ip; // 0...15
  1049. dst_t * y = yy + i*QK_K + 16*ip + il;
  1050. const float d = x[i].d;
  1051. const uint8_t ql = x[i].ql[16*ip + il];
  1052. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1053. const int8_t * sc = x[i].scales;
  1054. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1055. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1056. #endif
  1057. }
  1058. 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) {
  1059. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1060. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1061. if (row > nrows) return;
  1062. const int num_blocks_per_row = ncols / QK_K;
  1063. const int ib0 = row*num_blocks_per_row;
  1064. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1065. float tmp = 0; // partial sum for thread in warp
  1066. #if QK_K == 256
  1067. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1068. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1069. const int step = 16/K_QUANTS_PER_ITERATION;
  1070. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1071. const int in = tid - step*im; // 0...15 or 0...7
  1072. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1073. const int q_offset = 32*im + l0;
  1074. const int s_offset = 8*im;
  1075. const int y_offset = 128*im + l0;
  1076. uint32_t aux[4];
  1077. const uint8_t * d = (const uint8_t *)aux;
  1078. const uint8_t * m = (const uint8_t *)(aux + 2);
  1079. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1080. const float * y = yy + i * QK_K + y_offset;
  1081. const uint8_t * q = x[i].qs + q_offset;
  1082. const float dall = __low2half(x[i].dm);
  1083. const float dmin = __high2half(x[i].dm);
  1084. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1085. aux[0] = a[0] & 0x0f0f0f0f;
  1086. aux[1] = a[1] & 0x0f0f0f0f;
  1087. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1088. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1089. float sum1 = 0, sum2 = 0;
  1090. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1091. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1092. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1093. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1094. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1095. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1096. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1097. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1098. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1099. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1100. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1101. }
  1102. tmp += dall * sum1 - dmin * sum2;
  1103. }
  1104. #else
  1105. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1106. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1107. const int offset = tid * K_QUANTS_PER_ITERATION;
  1108. uint32_t uaux[2];
  1109. const uint8_t * d = (const uint8_t *)uaux;
  1110. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1111. const float * y = yy + i * QK_K + offset;
  1112. const uint8_t * q = x[i].qs + offset;
  1113. const uint32_t * s = (const uint32_t *)x[i].scales;
  1114. uaux[0] = s[0] & 0x0f0f0f0f;
  1115. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1116. const float2 dall = __half22float2(x[i].dm);
  1117. float sum1 = 0, sum2 = 0;
  1118. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1119. const uint8_t ql = q[l];
  1120. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1121. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1122. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1123. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1124. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1125. }
  1126. tmp += dall.x * sum1 - dall.y * sum2;
  1127. }
  1128. #endif
  1129. // sum up partial sums and write back result
  1130. #pragma unroll
  1131. for (int mask = 16; mask > 0; mask >>= 1) {
  1132. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1133. }
  1134. if (threadIdx.x == 0) {
  1135. dst[row] = tmp;
  1136. }
  1137. }
  1138. 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) {
  1139. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1140. if (row > nrows) return;
  1141. const int num_blocks_per_row = ncols / QK_K;
  1142. const int ib0 = row*num_blocks_per_row;
  1143. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1144. float tmp = 0; // partial sum for thread in warp
  1145. #if QK_K == 256
  1146. const uint16_t kmask1 = 0x0303;
  1147. const uint16_t kmask2 = 0x0f0f;
  1148. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1149. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1150. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1151. const int step = 16/K_QUANTS_PER_ITERATION;
  1152. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1153. const int in = tid - step*im; // 0....15 or 0...7
  1154. const uint8_t m = 1 << (4*im);
  1155. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1156. const int q_offset = 32*im + l0;
  1157. const int y_offset = 128*im + l0;
  1158. uint16_t utmp[4];
  1159. const int8_t * s = (const int8_t *)utmp;
  1160. const uint16_t s_shift = 4*im;
  1161. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1162. const float * y = yy + i * QK_K + y_offset;
  1163. const uint8_t * q = x[i].qs + q_offset;
  1164. const uint8_t * h = x[i].hmask + l0;
  1165. const uint16_t * a = (const uint16_t *)x[i].scales;
  1166. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1167. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1168. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1169. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1170. const float d = x[i].d;
  1171. float sum = 0;
  1172. for (int l = 0; l < n; ++l) {
  1173. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1174. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1175. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1176. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1177. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1178. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1179. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1180. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1181. }
  1182. tmp += d * sum;
  1183. }
  1184. #else
  1185. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1186. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1187. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1188. const int in = offset/8; // 0 or 1
  1189. const int im = offset%8; // 0...7
  1190. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1191. const float * y = yy + i * QK_K + offset;
  1192. const uint8_t * q = x[i].qs + offset;
  1193. const uint8_t * s = x[i].scales;
  1194. const float dall = (float)x[i].d;
  1195. float sum = 0;
  1196. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1197. const uint8_t hl = x[i].hmask[im+l] >> in;
  1198. const uint8_t ql = q[l];
  1199. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1200. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1201. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1202. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1203. }
  1204. tmp += sum;
  1205. }
  1206. #endif
  1207. // sum up partial sums and write back result
  1208. #pragma unroll
  1209. for (int mask = 16; mask > 0; mask >>= 1) {
  1210. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1211. }
  1212. if (threadIdx.x == 0) {
  1213. dst[row] = tmp;
  1214. }
  1215. }
  1216. 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) {
  1217. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1218. if (row > nrows) return;
  1219. const int num_blocks_per_row = ncols / QK_K;
  1220. const int ib0 = row*num_blocks_per_row;
  1221. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1222. #if QK_K == 256
  1223. const uint16_t kmask1 = 0x3f3f;
  1224. const uint16_t kmask2 = 0x0f0f;
  1225. const uint16_t kmask3 = 0xc0c0;
  1226. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1227. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1228. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1229. const int il = tid/step; // 0...3
  1230. const int ir = tid - step*il; // 0...7 or 0...3
  1231. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1232. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1233. const int in = il%2;
  1234. const int l0 = n*(2*ir + in);
  1235. const int q_offset = 32*im + l0;
  1236. const int y_offset = 64*im + l0;
  1237. uint16_t aux[4];
  1238. const uint8_t * sc = (const uint8_t *)aux;
  1239. #if K_QUANTS_PER_ITERATION == 2
  1240. uint32_t q32[4];
  1241. const uint8_t * q4 = (const uint8_t *)q32;
  1242. #else
  1243. uint16_t q16[4];
  1244. const uint8_t * q4 = (const uint8_t *)q16;
  1245. #endif
  1246. float tmp = 0; // partial sum for thread in warp
  1247. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1248. const float * y1 = yy + i*QK_K + y_offset;
  1249. const float * y2 = y1 + 128;
  1250. const float dall = __low2half(x[i].dm);
  1251. const float dmin = __high2half(x[i].dm);
  1252. const uint16_t * a = (const uint16_t *)x[i].scales;
  1253. aux[0] = a[im+0] & kmask1;
  1254. aux[1] = a[im+2] & kmask1;
  1255. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1256. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1257. #if K_QUANTS_PER_ITERATION == 2
  1258. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1259. const uint32_t * q2 = q1 + 16;
  1260. q32[0] = q1[0] & 0x0f0f0f0f;
  1261. q32[1] = q1[0] & 0xf0f0f0f0;
  1262. q32[2] = q2[0] & 0x0f0f0f0f;
  1263. q32[3] = q2[0] & 0xf0f0f0f0;
  1264. float4 s = {0.f, 0.f, 0.f, 0.f};
  1265. float smin = 0;
  1266. for (int l = 0; l < 4; ++l) {
  1267. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1268. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1269. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1270. }
  1271. 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;
  1272. #else
  1273. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1274. const uint16_t * q2 = q1 + 32;
  1275. q16[0] = q1[0] & 0x0f0f;
  1276. q16[1] = q1[0] & 0xf0f0;
  1277. q16[2] = q2[0] & 0x0f0f;
  1278. q16[3] = q2[0] & 0xf0f0;
  1279. float4 s = {0.f, 0.f, 0.f, 0.f};
  1280. float smin = 0;
  1281. for (int l = 0; l < 2; ++l) {
  1282. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1283. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1284. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1285. }
  1286. 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;
  1287. #endif
  1288. }
  1289. #else
  1290. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1291. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1292. const int step = tid * K_QUANTS_PER_ITERATION;
  1293. uint16_t aux16[2];
  1294. const uint8_t * s = (const uint8_t *)aux16;
  1295. float tmp = 0;
  1296. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1297. const uint8_t * q = x[i].qs + step;
  1298. const float * y = yy + i*QK_K + step;
  1299. const uint16_t * a = (const uint16_t *)x[i].scales;
  1300. aux16[0] = a[0] & 0x0f0f;
  1301. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1302. const float d = (float)x[i].dm[0];
  1303. const float m = (float)x[i].dm[1];
  1304. float sum = 0.f;
  1305. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1306. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1307. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1308. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1309. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1310. }
  1311. tmp += sum;
  1312. }
  1313. #endif
  1314. // sum up partial sums and write back result
  1315. #pragma unroll
  1316. for (int mask = 16; mask > 0; mask >>= 1) {
  1317. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1318. }
  1319. if (tid == 0) {
  1320. dst[row] = tmp;
  1321. }
  1322. }
  1323. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1324. const int row = blockIdx.x;
  1325. const int num_blocks_per_row = ncols / QK_K;
  1326. const int ib0 = row*num_blocks_per_row;
  1327. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1328. float tmp = 0; // partial sum for thread in warp
  1329. #if QK_K == 256
  1330. const uint16_t kmask1 = 0x3f3f;
  1331. const uint16_t kmask2 = 0x0f0f;
  1332. const uint16_t kmask3 = 0xc0c0;
  1333. const int tid = threadIdx.x/2; // 0...15
  1334. const int ix = threadIdx.x%2;
  1335. const int il = tid/4; // 0...3
  1336. const int ir = tid - 4*il;// 0...3
  1337. const int n = 2;
  1338. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1339. const int in = il%2;
  1340. const int l0 = n*(2*ir + in);
  1341. const int q_offset = 32*im + l0;
  1342. const int y_offset = 64*im + l0;
  1343. const uint8_t hm1 = 1 << (2*im);
  1344. const uint8_t hm2 = hm1 << 4;
  1345. uint16_t aux[4];
  1346. const uint8_t * sc = (const uint8_t *)aux;
  1347. uint16_t q16[8];
  1348. const uint8_t * q4 = (const uint8_t *)q16;
  1349. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1350. const uint8_t * ql1 = x[i].qs + q_offset;
  1351. const uint8_t * qh = x[i].qh + l0;
  1352. const float * y1 = yy + i*QK_K + y_offset;
  1353. const float * y2 = y1 + 128;
  1354. const float dall = __low2half(x[i].dm);
  1355. const float dmin = __high2half(x[i].dm);
  1356. const uint16_t * a = (const uint16_t *)x[i].scales;
  1357. aux[0] = a[im+0] & kmask1;
  1358. aux[1] = a[im+2] & kmask1;
  1359. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1360. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1361. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1362. float smin = 0;
  1363. const uint16_t * q1 = (const uint16_t *)ql1;
  1364. const uint16_t * q2 = q1 + 32;
  1365. q16[0] = q1[0] & 0x0f0f;
  1366. q16[1] = q1[8] & 0x0f0f;
  1367. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1368. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1369. q16[4] = q2[0] & 0x0f0f;
  1370. q16[5] = q2[8] & 0x0f0f;
  1371. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1372. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1373. for (int l = 0; l < n; ++l) {
  1374. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1375. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1376. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1377. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1378. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1379. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1380. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1381. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1382. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1383. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1384. }
  1385. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1386. }
  1387. #else
  1388. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1389. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1390. const int step = tid * K_QUANTS_PER_ITERATION;
  1391. const int im = step/8;
  1392. const int in = step%8;
  1393. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1394. const uint8_t * q = x[i].qs + step;
  1395. const int8_t * s = x[i].scales;
  1396. const float * y = yy + i*QK_K + step;
  1397. const float d = x[i].d;
  1398. float sum = 0.f;
  1399. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1400. const uint8_t h = x[i].qh[in+j] >> im;
  1401. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1402. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1403. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1404. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1405. }
  1406. tmp += sum;
  1407. }
  1408. #endif
  1409. // sum up partial sums and write back result
  1410. #pragma unroll
  1411. for (int mask = 16; mask > 0; mask >>= 1) {
  1412. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1413. }
  1414. if (threadIdx.x == 0) {
  1415. dst[row] = tmp;
  1416. }
  1417. }
  1418. 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) {
  1419. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1420. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1421. if (row > nrows) return;
  1422. const int num_blocks_per_row = ncols / QK_K;
  1423. const int ib0 = row*num_blocks_per_row;
  1424. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1425. #if QK_K == 256
  1426. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1427. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1428. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1429. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1430. const int in = tid - step*im; // 0...15 or 0...7
  1431. #if K_QUANTS_PER_ITERATION == 1
  1432. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1433. const int is = 0;
  1434. #else
  1435. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1436. const int is = in / 4;
  1437. #endif
  1438. const int ql_offset = 64*im + l0;
  1439. const int qh_offset = 32*im + l0;
  1440. const int s_offset = 8*im + is;
  1441. const int y_offset = 128*im + l0;
  1442. float tmp = 0; // partial sum for thread in warp
  1443. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1444. const float * y = yy + i * QK_K + y_offset;
  1445. const uint8_t * ql = x[i].ql + ql_offset;
  1446. const uint8_t * qh = x[i].qh + qh_offset;
  1447. const int8_t * s = x[i].scales + s_offset;
  1448. const float d = x[i].d;
  1449. #if K_QUANTS_PER_ITERATION == 1
  1450. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1451. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1452. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1453. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1454. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1455. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1456. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1457. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1458. tmp += sum;
  1459. #else
  1460. float sum = 0;
  1461. for (int l = 0; l < 4; ++l) {
  1462. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1463. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1464. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1465. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1466. }
  1467. tmp += sum;
  1468. #endif
  1469. }
  1470. #else
  1471. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1472. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1473. const int step = tid * K_QUANTS_PER_ITERATION;
  1474. float tmp = 0; // partial sum for thread in warp
  1475. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1476. const float * y = yy + i * QK_K + step;
  1477. const uint8_t * ql = x[i].ql + step;
  1478. const uint8_t * qh = x[i].qh + step;
  1479. const int8_t * s = x[i].scales;
  1480. const float d = x[i+0].d;
  1481. float sum = 0;
  1482. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1483. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1484. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1485. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1486. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1487. }
  1488. tmp += sum;
  1489. }
  1490. #endif
  1491. // sum up partial sums and write back result
  1492. #pragma unroll
  1493. for (int mask = 16; mask > 0; mask >>= 1) {
  1494. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1495. }
  1496. if (tid == 0) {
  1497. dst[row] = tmp;
  1498. }
  1499. }
  1500. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1501. const half * x = (const half *) vx;
  1502. // automatic half -> float type cast if dfloat == float
  1503. v.x = x[ib + iqs + 0];
  1504. v.y = x[ib + iqs + 1];
  1505. }
  1506. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1507. const float * x = (const float *) vx;
  1508. // automatic half -> float type cast if dfloat == float
  1509. v.x = x[ib + iqs + 0];
  1510. v.y = x[ib + iqs + 1];
  1511. }
  1512. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1513. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1514. if (ix >= kx_padded) {
  1515. return;
  1516. }
  1517. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1518. const int i_padded = iy*kx_padded + ix;
  1519. block_q8_1 * y = (block_q8_1 *) vy;
  1520. const int ib = i_padded / QK8_1; // block index
  1521. const int iqs = i_padded % QK8_1; // quant index
  1522. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1523. float amax = fabsf(xi);
  1524. float sum = xi;
  1525. #pragma unroll
  1526. for (int mask = 16; mask > 0; mask >>= 1) {
  1527. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1528. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1529. }
  1530. const float d = amax / 127;
  1531. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1532. y[ib].qs[iqs] = q;
  1533. if (iqs > 0) {
  1534. return;
  1535. }
  1536. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1537. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1538. }
  1539. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1540. static __global__ void k_get_rows(
  1541. const void * src0, const int32_t * src1, dst_t * dst,
  1542. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1543. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1544. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1545. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1546. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1547. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1548. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1549. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1550. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1551. if (i00 >= ne00) {
  1552. return;
  1553. }
  1554. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1555. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1556. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1557. const int ib = i00/qk; // block index
  1558. const int iqs = (i00%qk)/qr; // quant index
  1559. const int iybs = i00 - i00%qk; // dst block start index
  1560. const int y_offset = qr == 1 ? 1 : qk/2;
  1561. // dequantize
  1562. dfloat2 v;
  1563. dequantize_kernel(src0_row, ib, iqs, v);
  1564. dst_row[iybs + iqs + 0] = v.x;
  1565. dst_row[iybs + iqs + y_offset] = v.y;
  1566. }
  1567. template<typename src0_t, typename dst_t>
  1568. static __global__ void k_get_rows_float(
  1569. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1570. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1571. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1572. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1573. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1574. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1575. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1576. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1577. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1578. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1579. if (i00 >= ne00) {
  1580. return;
  1581. }
  1582. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1583. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1584. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1585. dst_row[i00] = src0_row[i00];
  1586. }
  1587. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1588. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1589. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1590. if (i >= k) {
  1591. return;
  1592. }
  1593. const int ib = i/qk; // block index
  1594. const int iqs = (i%qk)/qr; // quant index
  1595. const int iybs = i - i%qk; // y block start index
  1596. const int y_offset = qr == 1 ? 1 : qk/2;
  1597. // dequantize
  1598. dfloat2 v;
  1599. dequantize_kernel(vx, ib, iqs, v);
  1600. y[iybs + iqs + 0] = v.x;
  1601. y[iybs + iqs + y_offset] = v.y;
  1602. }
  1603. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1604. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1605. #define VDR_Q4_0_Q8_1_MMVQ 2
  1606. #define VDR_Q4_0_Q8_1_MMQ 4
  1607. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1608. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1609. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1610. int sumi = 0;
  1611. #pragma unroll
  1612. for (int i = 0; i < vdr; ++i) {
  1613. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1614. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1615. // SIMD dot product of quantized values
  1616. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1617. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1618. }
  1619. const float2 ds8f = __half22float2(ds8);
  1620. // second part effectively subtracts 8 from each quant value
  1621. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1622. #else
  1623. bad_arch();
  1624. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1625. }
  1626. #define VDR_Q4_1_Q8_1_MMVQ 2
  1627. #define VDR_Q4_1_Q8_1_MMQ 4
  1628. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1629. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1630. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1631. int sumi = 0;
  1632. #pragma unroll
  1633. for (int i = 0; i < vdr; ++i) {
  1634. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1635. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1636. // SIMD dot product of quantized values
  1637. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1638. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1639. }
  1640. #ifdef GGML_CUDA_F16
  1641. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1642. const float d4d8 = tmp.x;
  1643. const float m4s8 = tmp.y;
  1644. #else
  1645. const float2 dm4f = __half22float2(dm4);
  1646. const float2 ds8f = __half22float2(ds8);
  1647. const float d4d8 = dm4f.x * ds8f.x;
  1648. const float m4s8 = dm4f.y * ds8f.y;
  1649. #endif // GGML_CUDA_F16
  1650. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1651. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1652. #else
  1653. bad_arch();
  1654. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1655. }
  1656. #define VDR_Q5_0_Q8_1_MMVQ 2
  1657. #define VDR_Q5_0_Q8_1_MMQ 4
  1658. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1659. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1660. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1661. int sumi = 0;
  1662. #pragma unroll
  1663. for (int i = 0; i < vdr; ++i) {
  1664. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1665. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1666. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1667. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1668. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1669. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1670. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1671. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1672. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1673. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1674. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1675. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1676. }
  1677. const float2 ds8f = __half22float2(ds8);
  1678. // second part effectively subtracts 16 from each quant value
  1679. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1680. #else
  1681. bad_arch();
  1682. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1683. }
  1684. #define VDR_Q5_1_Q8_1_MMVQ 2
  1685. #define VDR_Q5_1_Q8_1_MMQ 4
  1686. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1687. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1688. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1689. int sumi = 0;
  1690. #pragma unroll
  1691. for (int i = 0; i < vdr; ++i) {
  1692. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1693. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1694. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1695. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1696. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1697. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1698. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1699. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1700. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1701. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1702. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1703. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1704. }
  1705. #ifdef GGML_CUDA_F16
  1706. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1707. const float d5d8 = tmp.x;
  1708. const float m5s8 = tmp.y;
  1709. #else
  1710. const float2 dm5f = __half22float2(dm5);
  1711. const float2 ds8f = __half22float2(ds8);
  1712. const float d5d8 = dm5f.x * ds8f.x;
  1713. const float m5s8 = dm5f.y * ds8f.y;
  1714. #endif // GGML_CUDA_F16
  1715. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1716. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1717. #else
  1718. bad_arch();
  1719. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1720. }
  1721. #define VDR_Q8_0_Q8_1_MMVQ 2
  1722. #define VDR_Q8_0_Q8_1_MMQ 8
  1723. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1724. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1725. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1726. int sumi = 0;
  1727. #pragma unroll
  1728. for (int i = 0; i < vdr; ++i) {
  1729. // SIMD dot product of quantized values
  1730. sumi = __dp4a(v[i], u[i], sumi);
  1731. }
  1732. return d8_0*d8_1 * sumi;
  1733. #else
  1734. bad_arch();
  1735. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1736. }
  1737. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1738. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1739. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1740. int sumi = 0;
  1741. #pragma unroll
  1742. for (int i = 0; i < vdr; ++i) {
  1743. // SIMD dot product of quantized values
  1744. sumi = __dp4a(v[i], u[i], sumi);
  1745. }
  1746. #ifdef GGML_CUDA_F16
  1747. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1748. const float d8d8 = tmp.x;
  1749. const float m8s8 = tmp.y;
  1750. #else
  1751. const float2 dm8f = __half22float2(dm8);
  1752. const float2 ds8f = __half22float2(ds8);
  1753. const float d8d8 = dm8f.x * ds8f.x;
  1754. const float m8s8 = dm8f.y * ds8f.y;
  1755. #endif // GGML_CUDA_F16
  1756. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1757. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1758. #else
  1759. bad_arch();
  1760. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1761. }
  1762. #define VDR_Q2_K_Q8_1_MMVQ 1
  1763. #define VDR_Q2_K_Q8_1_MMQ 2
  1764. // contiguous v/x values
  1765. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1766. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1767. const half2 & dm2, const float * __restrict__ d8) {
  1768. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1769. float sumf_d = 0.0f;
  1770. float sumf_m = 0.0f;
  1771. #pragma unroll
  1772. for (int i = 0; i < QR2_K; ++i) {
  1773. const int sc = scales[2*i];
  1774. const int vi = (v >> (2*i)) & 0x03030303;
  1775. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1776. // fill int with 4x m
  1777. int m = sc >> 4;
  1778. m |= m << 8;
  1779. m |= m << 16;
  1780. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1781. }
  1782. const float2 dm2f = __half22float2(dm2);
  1783. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1784. #else
  1785. bad_arch();
  1786. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1787. }
  1788. // contiguous u/y values
  1789. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1790. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1791. const half2 & dm2, const float & d8) {
  1792. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1793. int sumi_d = 0;
  1794. int sumi_m = 0;
  1795. #pragma unroll
  1796. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1797. int sumi_d_sc = 0;
  1798. const int sc = scales[i0 / (QI8_1/2)];
  1799. // fill int with 4x m
  1800. int m = sc >> 4;
  1801. m |= m << 8;
  1802. m |= m << 16;
  1803. #pragma unroll
  1804. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1805. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1806. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1807. }
  1808. sumi_d += sumi_d_sc * (sc & 0xF);
  1809. }
  1810. const float2 dm2f = __half22float2(dm2);
  1811. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1812. #else
  1813. bad_arch();
  1814. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1815. }
  1816. #define VDR_Q3_K_Q8_1_MMVQ 1
  1817. #define VDR_Q3_K_Q8_1_MMQ 2
  1818. // contiguous v/x values
  1819. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1820. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1821. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1822. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1823. float sumf = 0.0f;
  1824. #pragma unroll
  1825. for (int i = 0; i < QR3_K; ++i) {
  1826. const int isc = scale_offset + 2*i;
  1827. const int isc_low = isc % (QK_K/32);
  1828. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1829. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1830. const int isc_high = isc % (QK_K/64);
  1831. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1832. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1833. const int sc = (sc_low | sc_high) - 32;
  1834. const int vil = (vl >> (2*i)) & 0x03030303;
  1835. const int vih = ((vh >> i) << 2) & 0x04040404;
  1836. const int vi = __vsubss4(vil, vih);
  1837. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1838. }
  1839. return d3 * sumf;
  1840. #else
  1841. bad_arch();
  1842. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1843. }
  1844. // contiguous u/y values
  1845. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1846. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1847. const float & d3, const float & d8) {
  1848. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1849. int sumi = 0;
  1850. #pragma unroll
  1851. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1852. int sumi_sc = 0;
  1853. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1854. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1855. }
  1856. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1857. }
  1858. return d3*d8 * sumi;
  1859. #else
  1860. bad_arch();
  1861. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1862. }
  1863. #define VDR_Q4_K_Q8_1_MMVQ 2
  1864. #define VDR_Q4_K_Q8_1_MMQ 8
  1865. // contiguous v/x values
  1866. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1867. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1868. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1869. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1870. float sumf_d = 0.0f;
  1871. float sumf_m = 0.0f;
  1872. #pragma unroll
  1873. for (int i = 0; i < QR4_K; ++i) {
  1874. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1875. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1876. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1877. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1878. sumf_d += d8[i] * (dot1 * sc[i]);
  1879. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1880. }
  1881. const float2 dm4f = __half22float2(dm4);
  1882. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1883. #else
  1884. bad_arch();
  1885. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1886. }
  1887. // contiguous u/y values
  1888. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1889. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1890. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1891. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1892. float sumf_d = 0.0f;
  1893. float sumf_m = 0.0f;
  1894. #pragma unroll
  1895. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1896. int sumi_d = 0;
  1897. #pragma unroll
  1898. for (int j = 0; j < QI8_1; ++j) {
  1899. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1900. }
  1901. const float2 ds8f = __half22float2(ds8[i]);
  1902. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1903. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1904. }
  1905. const float2 dm4f = __half22float2(dm4);
  1906. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1907. #else
  1908. bad_arch();
  1909. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1910. }
  1911. #define VDR_Q5_K_Q8_1_MMVQ 2
  1912. #define VDR_Q5_K_Q8_1_MMQ 8
  1913. // contiguous v/x values
  1914. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1915. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1916. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1917. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1918. float sumf_d = 0.0f;
  1919. float sumf_m = 0.0f;
  1920. #pragma unroll
  1921. for (int i = 0; i < QR5_K; ++i) {
  1922. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1923. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1924. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1925. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1926. const int v0i = vl0i | vh0i;
  1927. const int v1i = vl1i | vh1i;
  1928. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1929. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1930. sumf_d += d8[i] * (dot1 * sc[i]);
  1931. sumf_m += d8[i] * (dot2 * m[i]);
  1932. }
  1933. const float2 dm5f = __half22float2(dm5);
  1934. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1935. #else
  1936. bad_arch();
  1937. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1938. }
  1939. // contiguous u/y values
  1940. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1941. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1942. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1943. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1944. float sumf_d = 0.0f;
  1945. float sumf_m = 0.0f;
  1946. #pragma unroll
  1947. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1948. int sumi_d = 0;
  1949. #pragma unroll
  1950. for (int j = 0; j < QI8_1; ++j) {
  1951. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1952. }
  1953. const float2 ds8f = __half22float2(ds8[i]);
  1954. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1955. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1956. }
  1957. const float2 dm4f = __half22float2(dm4);
  1958. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1959. #else
  1960. bad_arch();
  1961. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1962. }
  1963. #define VDR_Q6_K_Q8_1_MMVQ 1
  1964. #define VDR_Q6_K_Q8_1_MMQ 8
  1965. // contiguous v/x values
  1966. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1967. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1968. const float & d, const float * __restrict__ d8) {
  1969. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1970. float sumf = 0.0f;
  1971. #pragma unroll
  1972. for (int i = 0; i < QR6_K; ++i) {
  1973. const int sc = scales[4*i];
  1974. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1975. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1976. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1977. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1978. }
  1979. return d*sumf;
  1980. #else
  1981. bad_arch();
  1982. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1983. }
  1984. // contiguous u/y values
  1985. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  1986. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  1987. const float & d6, const float * __restrict__ d8) {
  1988. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1989. float sumf_d = 0.0f;
  1990. #pragma unroll
  1991. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  1992. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  1993. #pragma unroll
  1994. for (int i = i0; i < i0 + 2; ++i) {
  1995. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  1996. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  1997. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  1998. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  1999. }
  2000. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2001. }
  2002. return d6 * sumf_d;
  2003. #else
  2004. bad_arch();
  2005. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2006. }
  2007. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2008. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2009. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2010. int v[VDR_Q4_0_Q8_1_MMVQ];
  2011. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2012. #pragma unroll
  2013. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2014. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2015. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2016. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2017. }
  2018. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2019. }
  2020. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2021. (void)x_qh; (void)x_sc;
  2022. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2023. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2024. *x_ql = tile_x_qs;
  2025. *x_dm = (half2 *) tile_x_d;
  2026. }
  2027. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2028. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2029. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2030. (void)x_qh; (void)x_sc;
  2031. GGML_CUDA_ASSUME(i_offset >= 0);
  2032. GGML_CUDA_ASSUME(i_offset < nwarps);
  2033. GGML_CUDA_ASSUME(k >= 0);
  2034. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2035. const int kbx = k / QI4_0;
  2036. const int kqsx = k % QI4_0;
  2037. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2038. float * x_dmf = (float *) x_dm;
  2039. #pragma unroll
  2040. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2041. int i = i0 + i_offset;
  2042. if (need_check) {
  2043. i = min(i, i_max);
  2044. }
  2045. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2046. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2047. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2048. }
  2049. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2050. const int kbxd = k % blocks_per_tile_x_row;
  2051. #pragma unroll
  2052. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2053. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2054. if (need_check) {
  2055. i = min(i, i_max);
  2056. }
  2057. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2058. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2059. }
  2060. }
  2061. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2062. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2063. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2064. (void)x_qh; (void)x_sc;
  2065. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2066. const float * x_dmf = (const float *) x_dm;
  2067. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2068. #pragma unroll
  2069. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2070. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2071. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2072. }
  2073. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2074. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2075. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2076. }
  2077. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2078. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2079. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2080. int v[VDR_Q4_1_Q8_1_MMVQ];
  2081. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2082. #pragma unroll
  2083. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2084. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2085. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2086. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2087. }
  2088. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2089. }
  2090. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2091. (void)x_qh; (void)x_sc;
  2092. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2093. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2094. *x_ql = tile_x_qs;
  2095. *x_dm = tile_x_dm;
  2096. }
  2097. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2098. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2099. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2100. (void)x_qh; (void)x_sc;
  2101. GGML_CUDA_ASSUME(i_offset >= 0);
  2102. GGML_CUDA_ASSUME(i_offset < nwarps);
  2103. GGML_CUDA_ASSUME(k >= 0);
  2104. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2105. const int kbx = k / QI4_1;
  2106. const int kqsx = k % QI4_1;
  2107. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2108. #pragma unroll
  2109. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2110. int i = i0 + i_offset;
  2111. if (need_check) {
  2112. i = min(i, i_max);
  2113. }
  2114. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2115. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2116. }
  2117. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2118. const int kbxd = k % blocks_per_tile_x_row;
  2119. #pragma unroll
  2120. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2121. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2122. if (need_check) {
  2123. i = min(i, i_max);
  2124. }
  2125. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2126. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2127. }
  2128. }
  2129. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2130. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2131. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2132. (void)x_qh; (void)x_sc;
  2133. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2134. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2135. #pragma unroll
  2136. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2137. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2138. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2139. }
  2140. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2141. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2142. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2143. }
  2144. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2145. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2146. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2147. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2148. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2149. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2150. #pragma unroll
  2151. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2152. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2153. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2154. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2155. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2156. }
  2157. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2158. }
  2159. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2160. (void)x_qh; (void)x_sc;
  2161. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2162. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2163. *x_ql = tile_x_ql;
  2164. *x_dm = (half2 *) tile_x_d;
  2165. }
  2166. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2167. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2168. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2169. (void)x_qh; (void)x_sc;
  2170. GGML_CUDA_ASSUME(i_offset >= 0);
  2171. GGML_CUDA_ASSUME(i_offset < nwarps);
  2172. GGML_CUDA_ASSUME(k >= 0);
  2173. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2174. const int kbx = k / QI5_0;
  2175. const int kqsx = k % QI5_0;
  2176. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2177. #pragma unroll
  2178. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2179. int i = i0 + i_offset;
  2180. if (need_check) {
  2181. i = min(i, i_max);
  2182. }
  2183. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2184. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2185. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2186. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2187. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2188. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2189. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2190. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2191. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2192. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2193. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2194. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2195. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2196. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2197. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2198. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2199. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2200. }
  2201. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2202. const int kbxd = k % blocks_per_tile_x_row;
  2203. float * x_dmf = (float *) x_dm;
  2204. #pragma unroll
  2205. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2206. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2207. if (need_check) {
  2208. i = min(i, i_max);
  2209. }
  2210. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2211. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2212. }
  2213. }
  2214. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2215. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2216. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2217. (void)x_qh; (void)x_sc;
  2218. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2219. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2220. const float * x_dmf = (const float *) x_dm;
  2221. const float * y_df = (const float *) y_ds;
  2222. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2223. #pragma unroll
  2224. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2225. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2226. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2227. }
  2228. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2229. (&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)]);
  2230. }
  2231. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2232. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2233. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2234. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2235. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2236. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2237. #pragma unroll
  2238. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2239. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2240. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2241. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2242. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2243. }
  2244. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2245. }
  2246. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2247. (void)x_qh; (void)x_sc;
  2248. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2249. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2250. *x_ql = tile_x_ql;
  2251. *x_dm = tile_x_dm;
  2252. }
  2253. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2254. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2255. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2256. (void)x_qh; (void)x_sc;
  2257. GGML_CUDA_ASSUME(i_offset >= 0);
  2258. GGML_CUDA_ASSUME(i_offset < nwarps);
  2259. GGML_CUDA_ASSUME(k >= 0);
  2260. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2261. const int kbx = k / QI5_1;
  2262. const int kqsx = k % QI5_1;
  2263. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2264. #pragma unroll
  2265. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2266. int i = i0 + i_offset;
  2267. if (need_check) {
  2268. i = min(i, i_max);
  2269. }
  2270. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2271. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2272. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2273. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2274. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2275. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2276. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2277. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2278. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2279. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2280. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2281. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2282. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2283. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2284. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2285. }
  2286. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2287. const int kbxd = k % blocks_per_tile_x_row;
  2288. #pragma unroll
  2289. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2290. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2291. if (need_check) {
  2292. i = min(i, i_max);
  2293. }
  2294. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2295. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2296. }
  2297. }
  2298. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2299. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2300. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2301. (void)x_qh; (void)x_sc;
  2302. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2303. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2304. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2305. #pragma unroll
  2306. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2307. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2308. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2309. }
  2310. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2311. (&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)]);
  2312. }
  2313. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2314. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2315. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2316. int v[VDR_Q8_0_Q8_1_MMVQ];
  2317. int u[VDR_Q8_0_Q8_1_MMVQ];
  2318. #pragma unroll
  2319. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2320. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2321. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2322. }
  2323. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2324. }
  2325. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2326. (void)x_qh; (void)x_sc;
  2327. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2328. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2329. *x_ql = tile_x_qs;
  2330. *x_dm = (half2 *) tile_x_d;
  2331. }
  2332. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2333. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2334. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2335. (void)x_qh; (void)x_sc;
  2336. GGML_CUDA_ASSUME(i_offset >= 0);
  2337. GGML_CUDA_ASSUME(i_offset < nwarps);
  2338. GGML_CUDA_ASSUME(k >= 0);
  2339. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2340. const int kbx = k / QI8_0;
  2341. const int kqsx = k % QI8_0;
  2342. float * x_dmf = (float *) x_dm;
  2343. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2344. #pragma unroll
  2345. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2346. int i = i0 + i_offset;
  2347. if (need_check) {
  2348. i = min(i, i_max);
  2349. }
  2350. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2351. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2352. }
  2353. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2354. const int kbxd = k % blocks_per_tile_x_row;
  2355. #pragma unroll
  2356. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2357. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2358. if (need_check) {
  2359. i = min(i, i_max);
  2360. }
  2361. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2362. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2363. }
  2364. }
  2365. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2366. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2367. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2368. (void)x_qh; (void)x_sc;
  2369. const float * x_dmf = (const float *) x_dm;
  2370. const float * y_df = (const float *) y_ds;
  2371. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2372. (&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],
  2373. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2374. }
  2375. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2376. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2377. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2378. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2379. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2380. const uint8_t * scales = bq2_K->scales + scale_offset;
  2381. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2382. int u[QR2_K];
  2383. float d8[QR2_K];
  2384. #pragma unroll
  2385. for (int i = 0; i < QR2_K; ++ i) {
  2386. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2387. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2388. }
  2389. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2390. }
  2391. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2392. (void)x_qh;
  2393. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2394. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2395. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2396. *x_ql = tile_x_ql;
  2397. *x_dm = tile_x_dm;
  2398. *x_sc = tile_x_sc;
  2399. }
  2400. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2401. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2402. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2403. (void)x_qh;
  2404. GGML_CUDA_ASSUME(i_offset >= 0);
  2405. GGML_CUDA_ASSUME(i_offset < nwarps);
  2406. GGML_CUDA_ASSUME(k >= 0);
  2407. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2408. const int kbx = k / QI2_K;
  2409. const int kqsx = k % QI2_K;
  2410. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2411. #pragma unroll
  2412. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2413. int i = i0 + i_offset;
  2414. if (need_check) {
  2415. i = min(i, i_max);
  2416. }
  2417. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2418. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2419. }
  2420. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2421. const int kbxd = k % blocks_per_tile_x_row;
  2422. #pragma unroll
  2423. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2424. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2425. if (need_check) {
  2426. i = min(i, i_max);
  2427. }
  2428. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2429. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2430. }
  2431. #pragma unroll
  2432. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2433. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2434. if (need_check) {
  2435. i = min(i, i_max);
  2436. }
  2437. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2438. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2439. }
  2440. }
  2441. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2442. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2443. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2444. (void)x_qh;
  2445. const int kbx = k / QI2_K;
  2446. const int ky = (k % QI2_K) * QR2_K;
  2447. const float * y_df = (const float *) y_ds;
  2448. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2449. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2450. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2451. #pragma unroll
  2452. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2453. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2454. }
  2455. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2456. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2457. 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]);
  2458. }
  2459. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2460. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2461. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2462. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2463. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2464. const float d = bq3_K->d;
  2465. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2466. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2467. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2468. int u[QR3_K];
  2469. float d8[QR3_K];
  2470. #pragma unroll
  2471. for (int i = 0; i < QR3_K; ++i) {
  2472. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2473. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2474. }
  2475. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2476. }
  2477. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2478. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2479. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2480. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2481. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2482. *x_ql = tile_x_ql;
  2483. *x_dm = tile_x_dm;
  2484. *x_qh = tile_x_qh;
  2485. *x_sc = tile_x_sc;
  2486. }
  2487. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2488. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2489. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2490. GGML_CUDA_ASSUME(i_offset >= 0);
  2491. GGML_CUDA_ASSUME(i_offset < nwarps);
  2492. GGML_CUDA_ASSUME(k >= 0);
  2493. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2494. const int kbx = k / QI3_K;
  2495. const int kqsx = k % QI3_K;
  2496. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2497. #pragma unroll
  2498. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2499. int i = i0 + i_offset;
  2500. if (need_check) {
  2501. i = min(i, i_max);
  2502. }
  2503. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2504. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2505. }
  2506. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2507. const int kbxd = k % blocks_per_tile_x_row;
  2508. float * x_dmf = (float *) x_dm;
  2509. #pragma unroll
  2510. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2511. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2512. if (need_check) {
  2513. i = min(i, i_max);
  2514. }
  2515. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2516. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2517. }
  2518. #pragma unroll
  2519. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2520. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2521. if (need_check) {
  2522. i = min(i, i_max);
  2523. }
  2524. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2525. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2526. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2527. }
  2528. #pragma unroll
  2529. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2530. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2531. if (need_check) {
  2532. i = min(i, i_max);
  2533. }
  2534. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2535. const int ksc = k % (QI3_K/4);
  2536. const int ksc_low = ksc % (QI3_K/8);
  2537. const int shift_low = 4 * (ksc / (QI3_K/8));
  2538. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2539. const int ksc_high = QI3_K/8;
  2540. const int shift_high = 2 * ksc;
  2541. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2542. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2543. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2544. }
  2545. }
  2546. static __device__ __forceinline__ float vec_dot_q3_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 int kbx = k / QI3_K;
  2550. const int ky = (k % QI3_K) * QR3_K;
  2551. const float * x_dmf = (const float *) x_dm;
  2552. const float * y_df = (const float *) y_ds;
  2553. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2554. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2555. #pragma unroll
  2556. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2557. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2558. const int shift = 2 * ((ky % 32) / 8);
  2559. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2560. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2561. const int vlh = (vh << 2) & 0x04040404;
  2562. v[l] = __vsubss4(vll, vlh);
  2563. }
  2564. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2565. 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]);
  2566. }
  2567. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2568. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2569. #ifndef GGML_QKK_64
  2570. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2571. int v[2];
  2572. int u[2*QR4_K];
  2573. float d8[QR4_K];
  2574. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2575. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2576. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2577. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2578. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2579. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2580. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2581. v[0] = q4[0];
  2582. v[1] = q4[4];
  2583. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2584. uint16_t aux[2];
  2585. const int j = bq8_offset/2;
  2586. if (j < 2) {
  2587. aux[0] = scales[j+0] & 0x3f3f;
  2588. aux[1] = scales[j+2] & 0x3f3f;
  2589. } else {
  2590. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2591. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2592. }
  2593. const uint8_t * sc = (const uint8_t *)aux;
  2594. const uint8_t * m = sc + 2;
  2595. for (int i = 0; i < QR4_K; ++i) {
  2596. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2597. d8[i] = __low2half(bq8i->ds);
  2598. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2599. u[2*i+0] = q8[0];
  2600. u[2*i+1] = q8[4];
  2601. }
  2602. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2603. #else
  2604. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2605. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2606. float sumf_d = 0.0f;
  2607. float sumf_m = 0.0f;
  2608. uint16_t aux16[2];
  2609. const uint8_t * s = (const uint8_t *)aux16;
  2610. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2611. aux16[0] = a[0] & 0x0f0f;
  2612. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2613. const float dall = bq4_K->dm[0];
  2614. const float dmin = bq4_K->dm[1];
  2615. const float d8_1 = __low2float(bq8_1[0].ds);
  2616. const float d8_2 = __low2float(bq8_1[1].ds);
  2617. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2618. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2619. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2620. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2621. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2622. const int v1 = q4[0];
  2623. const int v2 = q4[4];
  2624. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2625. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2626. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2627. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2628. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2629. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2630. return dall * sumf_d - dmin * sumf_m;
  2631. #else
  2632. bad_arch();
  2633. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2634. #endif
  2635. }
  2636. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2637. (void)x_qh;
  2638. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2639. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2640. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2641. *x_ql = tile_x_ql;
  2642. *x_dm = tile_x_dm;
  2643. *x_sc = tile_x_sc;
  2644. }
  2645. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2646. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2647. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2648. (void)x_qh;
  2649. GGML_CUDA_ASSUME(i_offset >= 0);
  2650. GGML_CUDA_ASSUME(i_offset < nwarps);
  2651. GGML_CUDA_ASSUME(k >= 0);
  2652. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2653. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2654. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2655. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2656. #pragma unroll
  2657. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2658. int i = i0 + i_offset;
  2659. if (need_check) {
  2660. i = min(i, i_max);
  2661. }
  2662. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2663. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2664. }
  2665. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2666. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2667. #pragma unroll
  2668. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2669. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2670. if (need_check) {
  2671. i = min(i, i_max);
  2672. }
  2673. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2674. #if QK_K == 256
  2675. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2676. #else
  2677. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2678. #endif
  2679. }
  2680. #pragma unroll
  2681. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2682. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2683. if (need_check) {
  2684. i = min(i, i_max);
  2685. }
  2686. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2687. const int * scales = (const int *) bxi->scales;
  2688. const int ksc = k % (WARP_SIZE/8);
  2689. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2690. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2691. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2692. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2693. }
  2694. }
  2695. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2696. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2697. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2698. (void)x_qh;
  2699. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2700. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2701. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2702. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2703. }
  2704. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2705. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2706. #ifndef GGML_QKK_64
  2707. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2708. int vl[2];
  2709. int vh[2];
  2710. int u[2*QR5_K];
  2711. float d8[QR5_K];
  2712. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2713. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2714. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2715. vl[0] = ql[0];
  2716. vl[1] = ql[4];
  2717. vh[0] = qh[0] >> bq8_offset;
  2718. vh[1] = qh[4] >> bq8_offset;
  2719. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2720. uint16_t aux[2];
  2721. const int j = bq8_offset/2;
  2722. if (j < 2) {
  2723. aux[0] = scales[j+0] & 0x3f3f;
  2724. aux[1] = scales[j+2] & 0x3f3f;
  2725. } else {
  2726. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2727. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2728. }
  2729. const uint8_t * sc = (const uint8_t *)aux;
  2730. const uint8_t * m = sc + 2;
  2731. #pragma unroll
  2732. for (int i = 0; i < QR5_K; ++i) {
  2733. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2734. d8[i] = __low2float(bq8i->ds);
  2735. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2736. u[2*i+0] = q8[0];
  2737. u[2*i+1] = q8[4];
  2738. }
  2739. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2740. #else
  2741. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2742. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2743. const int8_t * s = bq5_K->scales;
  2744. const float d = bq5_K->d;
  2745. const float d8_1 = __low2half(bq8_1[0].ds);
  2746. const float d8_2 = __low2half(bq8_1[1].ds);
  2747. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2748. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2749. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2750. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2751. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2752. const int vl1 = ql[0];
  2753. const int vl2 = ql[4];
  2754. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2755. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2756. const int in = step%8; // 0, 4, 0, 4
  2757. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2758. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2759. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2760. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2761. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2762. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2763. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2764. return d * sumf_d;
  2765. #else
  2766. bad_arch();
  2767. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2768. #endif
  2769. }
  2770. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2771. (void)x_qh;
  2772. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2773. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2774. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2775. *x_ql = tile_x_ql;
  2776. *x_dm = tile_x_dm;
  2777. *x_sc = tile_x_sc;
  2778. }
  2779. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2780. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2781. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2782. (void)x_qh;
  2783. GGML_CUDA_ASSUME(i_offset >= 0);
  2784. GGML_CUDA_ASSUME(i_offset < nwarps);
  2785. GGML_CUDA_ASSUME(k >= 0);
  2786. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2787. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2788. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2789. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2790. #pragma unroll
  2791. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2792. int i = i0 + i_offset;
  2793. if (need_check) {
  2794. i = min(i, i_max);
  2795. }
  2796. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2797. const int ky = QR5_K*kqsx;
  2798. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2799. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2800. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2801. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2802. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2803. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2804. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2805. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2806. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2807. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2808. }
  2809. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2810. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2811. #pragma unroll
  2812. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2813. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2814. if (need_check) {
  2815. i = min(i, i_max);
  2816. }
  2817. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2818. #if QK_K == 256
  2819. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2820. #endif
  2821. }
  2822. #pragma unroll
  2823. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2824. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2825. if (need_check) {
  2826. i = min(i, i_max);
  2827. }
  2828. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2829. const int * scales = (const int *) bxi->scales;
  2830. const int ksc = k % (WARP_SIZE/8);
  2831. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2832. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2833. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2834. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2835. }
  2836. }
  2837. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2838. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2839. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2840. (void)x_qh;
  2841. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2842. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2843. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2844. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2845. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2846. }
  2847. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2848. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2849. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2850. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2851. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2852. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2853. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2854. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2855. const int8_t * scales = bq6_K->scales + scale_offset;
  2856. int u[QR6_K];
  2857. float d8[QR6_K];
  2858. #pragma unroll
  2859. for (int i = 0; i < QR6_K; ++i) {
  2860. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2861. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2862. }
  2863. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2864. }
  2865. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2866. (void)x_qh;
  2867. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2868. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2869. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2870. *x_ql = tile_x_ql;
  2871. *x_dm = tile_x_dm;
  2872. *x_sc = tile_x_sc;
  2873. }
  2874. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2875. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2876. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2877. (void)x_qh;
  2878. GGML_CUDA_ASSUME(i_offset >= 0);
  2879. GGML_CUDA_ASSUME(i_offset < nwarps);
  2880. GGML_CUDA_ASSUME(k >= 0);
  2881. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2882. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2883. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2884. const block_q6_K * bx0 = (const block_q6_K *) vx;
  2885. #pragma unroll
  2886. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2887. int i = i0 + i_offset;
  2888. if (need_check) {
  2889. i = min(i, i_max);
  2890. }
  2891. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2892. const int ky = QR6_K*kqsx;
  2893. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2894. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2895. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2896. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2897. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2898. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2899. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2900. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2901. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2902. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2903. }
  2904. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2905. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2906. float * x_dmf = (float *) x_dm;
  2907. #pragma unroll
  2908. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2909. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2910. if (need_check) {
  2911. i = min(i, i_max);
  2912. }
  2913. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2914. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2915. }
  2916. #pragma unroll
  2917. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2918. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2919. if (need_check) {
  2920. i = min(i, i_max);
  2921. }
  2922. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2923. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2924. }
  2925. }
  2926. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2927. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2928. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2929. (void)x_qh;
  2930. const float * x_dmf = (const float *) x_dm;
  2931. const float * y_df = (const float *) y_ds;
  2932. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2933. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2934. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2935. 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]);
  2936. }
  2937. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2938. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2939. static __device__ __forceinline__ void mul_mat_q(
  2940. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2941. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2942. const block_q_t * x = (const block_q_t *) vx;
  2943. const block_q8_1 * y = (const block_q8_1 *) vy;
  2944. const int blocks_per_row_x = ncols_x / qk;
  2945. const int blocks_per_col_y = nrows_y / QK8_1;
  2946. const int blocks_per_warp = WARP_SIZE / qi;
  2947. const int & ncols_dst = ncols_y;
  2948. const int row_dst_0 = blockIdx.x*mmq_y;
  2949. const int & row_x_0 = row_dst_0;
  2950. const int col_dst_0 = blockIdx.y*mmq_x;
  2951. const int & col_y_0 = col_dst_0;
  2952. int * tile_x_ql = nullptr;
  2953. half2 * tile_x_dm = nullptr;
  2954. int * tile_x_qh = nullptr;
  2955. int * tile_x_sc = nullptr;
  2956. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2957. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2958. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2959. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  2960. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2961. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2962. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2963. #pragma unroll
  2964. for (int ir = 0; ir < qr; ++ir) {
  2965. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2966. const int kbxd = kqs / QI8_1;
  2967. #pragma unroll
  2968. for (int i = 0; i < mmq_x; i += nwarps) {
  2969. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2970. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2971. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2972. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2973. }
  2974. #pragma unroll
  2975. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2976. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2977. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2978. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2979. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2980. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  2981. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  2982. if (need_sum) {
  2983. *dsi_dst = *dsi_src;
  2984. } else {
  2985. float * dfi_dst = (float *) dsi_dst;
  2986. *dfi_dst = __low2half(*dsi_src);
  2987. }
  2988. }
  2989. __syncthreads();
  2990. // #pragma unroll // unrolling this loop causes too much register pressure
  2991. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  2992. #pragma unroll
  2993. for (int j = 0; j < mmq_x; j += nwarps) {
  2994. #pragma unroll
  2995. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2996. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  2997. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  2998. threadIdx.x + i, threadIdx.y + j, k);
  2999. }
  3000. }
  3001. }
  3002. __syncthreads();
  3003. }
  3004. }
  3005. #pragma unroll
  3006. for (int j = 0; j < mmq_x; j += nwarps) {
  3007. const int col_dst = col_dst_0 + j + threadIdx.y;
  3008. if (col_dst >= ncols_dst) {
  3009. return;
  3010. }
  3011. #pragma unroll
  3012. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3013. const int row_dst = row_dst_0 + threadIdx.x + i;
  3014. if (row_dst >= nrows_dst) {
  3015. continue;
  3016. }
  3017. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3018. }
  3019. }
  3020. }
  3021. #define MMQ_X_Q4_0_RDNA2 64
  3022. #define MMQ_Y_Q4_0_RDNA2 128
  3023. #define NWARPS_Q4_0_RDNA2 8
  3024. #define MMQ_X_Q4_0_RDNA1 64
  3025. #define MMQ_Y_Q4_0_RDNA1 64
  3026. #define NWARPS_Q4_0_RDNA1 8
  3027. #if defined(CUDA_USE_TENSOR_CORES)
  3028. #define MMQ_X_Q4_0_AMPERE 4
  3029. #define MMQ_Y_Q4_0_AMPERE 32
  3030. #define NWARPS_Q4_0_AMPERE 4
  3031. #else
  3032. #define MMQ_X_Q4_0_AMPERE 64
  3033. #define MMQ_Y_Q4_0_AMPERE 128
  3034. #define NWARPS_Q4_0_AMPERE 4
  3035. #endif
  3036. #define MMQ_X_Q4_0_PASCAL 64
  3037. #define MMQ_Y_Q4_0_PASCAL 64
  3038. #define NWARPS_Q4_0_PASCAL 8
  3039. template <bool need_check> static __global__ void
  3040. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3041. #if defined(RDNA3) || defined(RDNA2)
  3042. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3043. #endif // defined(RDNA3) || defined(RDNA2)
  3044. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3045. mul_mat_q4_0(
  3046. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3047. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3048. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3049. #if defined(RDNA3) || defined(RDNA2)
  3050. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3051. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3052. const int nwarps = NWARPS_Q4_0_RDNA2;
  3053. #else
  3054. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3055. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3056. const int nwarps = NWARPS_Q4_0_RDNA1;
  3057. #endif // defined(RDNA3) || defined(RDNA2)
  3058. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3059. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3060. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3061. #elif __CUDA_ARCH__ >= CC_VOLTA
  3062. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3063. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3064. const int nwarps = NWARPS_Q4_0_AMPERE;
  3065. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3066. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3067. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3068. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3069. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3070. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3071. const int nwarps = NWARPS_Q4_0_PASCAL;
  3072. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3073. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3074. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3075. #else
  3076. (void) vec_dot_q4_0_q8_1_mul_mat;
  3077. bad_arch();
  3078. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3079. }
  3080. #define MMQ_X_Q4_1_RDNA2 64
  3081. #define MMQ_Y_Q4_1_RDNA2 128
  3082. #define NWARPS_Q4_1_RDNA2 8
  3083. #define MMQ_X_Q4_1_RDNA1 64
  3084. #define MMQ_Y_Q4_1_RDNA1 64
  3085. #define NWARPS_Q4_1_RDNA1 8
  3086. #if defined(CUDA_USE_TENSOR_CORES)
  3087. #define MMQ_X_Q4_1_AMPERE 4
  3088. #define MMQ_Y_Q4_1_AMPERE 32
  3089. #define NWARPS_Q4_1_AMPERE 4
  3090. #else
  3091. #define MMQ_X_Q4_1_AMPERE 64
  3092. #define MMQ_Y_Q4_1_AMPERE 128
  3093. #define NWARPS_Q4_1_AMPERE 4
  3094. #endif
  3095. #define MMQ_X_Q4_1_PASCAL 64
  3096. #define MMQ_Y_Q4_1_PASCAL 64
  3097. #define NWARPS_Q4_1_PASCAL 8
  3098. template <bool need_check> static __global__ void
  3099. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3100. #if defined(RDNA3) || defined(RDNA2)
  3101. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3102. #endif // defined(RDNA3) || defined(RDNA2)
  3103. #elif __CUDA_ARCH__ < CC_VOLTA
  3104. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3105. #endif // __CUDA_ARCH__ < CC_VOLTA
  3106. mul_mat_q4_1(
  3107. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3108. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3109. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3110. #if defined(RDNA3) || defined(RDNA2)
  3111. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3112. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3113. const int nwarps = NWARPS_Q4_1_RDNA2;
  3114. #else
  3115. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3116. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3117. const int nwarps = NWARPS_Q4_1_RDNA1;
  3118. #endif // defined(RDNA3) || defined(RDNA2)
  3119. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3120. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3121. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3122. #elif __CUDA_ARCH__ >= CC_VOLTA
  3123. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3124. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3125. const int nwarps = NWARPS_Q4_1_AMPERE;
  3126. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3127. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3128. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3129. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3130. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3131. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3132. const int nwarps = NWARPS_Q4_1_PASCAL;
  3133. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3134. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3135. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3136. #else
  3137. (void) vec_dot_q4_1_q8_1_mul_mat;
  3138. bad_arch();
  3139. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3140. }
  3141. #define MMQ_X_Q5_0_RDNA2 64
  3142. #define MMQ_Y_Q5_0_RDNA2 128
  3143. #define NWARPS_Q5_0_RDNA2 8
  3144. #define MMQ_X_Q5_0_RDNA1 64
  3145. #define MMQ_Y_Q5_0_RDNA1 64
  3146. #define NWARPS_Q5_0_RDNA1 8
  3147. #if defined(CUDA_USE_TENSOR_CORES)
  3148. #define MMQ_X_Q5_0_AMPERE 4
  3149. #define MMQ_Y_Q5_0_AMPERE 32
  3150. #define NWARPS_Q5_0_AMPERE 4
  3151. #else
  3152. #define MMQ_X_Q5_0_AMPERE 128
  3153. #define MMQ_Y_Q5_0_AMPERE 64
  3154. #define NWARPS_Q5_0_AMPERE 4
  3155. #endif
  3156. #define MMQ_X_Q5_0_PASCAL 64
  3157. #define MMQ_Y_Q5_0_PASCAL 64
  3158. #define NWARPS_Q5_0_PASCAL 8
  3159. template <bool need_check> static __global__ void
  3160. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3161. #if defined(RDNA3) || defined(RDNA2)
  3162. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3163. #endif // defined(RDNA3) || defined(RDNA2)
  3164. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3165. mul_mat_q5_0(
  3166. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3167. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3168. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3169. #if defined(RDNA3) || defined(RDNA2)
  3170. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3171. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3172. const int nwarps = NWARPS_Q5_0_RDNA2;
  3173. #else
  3174. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3175. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3176. const int nwarps = NWARPS_Q5_0_RDNA1;
  3177. #endif // defined(RDNA3) || defined(RDNA2)
  3178. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3179. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3180. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3181. #elif __CUDA_ARCH__ >= CC_VOLTA
  3182. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3183. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3184. const int nwarps = NWARPS_Q5_0_AMPERE;
  3185. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3186. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3187. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3188. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3189. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3190. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3191. const int nwarps = NWARPS_Q5_0_PASCAL;
  3192. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3193. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3194. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3195. #else
  3196. (void) vec_dot_q5_0_q8_1_mul_mat;
  3197. bad_arch();
  3198. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3199. }
  3200. #define MMQ_X_Q5_1_RDNA2 64
  3201. #define MMQ_Y_Q5_1_RDNA2 128
  3202. #define NWARPS_Q5_1_RDNA2 8
  3203. #define MMQ_X_Q5_1_RDNA1 64
  3204. #define MMQ_Y_Q5_1_RDNA1 64
  3205. #define NWARPS_Q5_1_RDNA1 8
  3206. #if defined(CUDA_USE_TENSOR_CORES)
  3207. #define MMQ_X_Q5_1_AMPERE 4
  3208. #define MMQ_Y_Q5_1_AMPERE 32
  3209. #define NWARPS_Q5_1_AMPERE 4
  3210. #else
  3211. #define MMQ_X_Q5_1_AMPERE 128
  3212. #define MMQ_Y_Q5_1_AMPERE 64
  3213. #define NWARPS_Q5_1_AMPERE 4
  3214. #endif
  3215. #define MMQ_X_Q5_1_PASCAL 64
  3216. #define MMQ_Y_Q5_1_PASCAL 64
  3217. #define NWARPS_Q5_1_PASCAL 8
  3218. template <bool need_check> static __global__ void
  3219. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3220. #if defined(RDNA3) || defined(RDNA2)
  3221. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3222. #endif // defined(RDNA3) || defined(RDNA2)
  3223. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3224. mul_mat_q5_1(
  3225. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3226. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3227. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3228. #if defined(RDNA3) || defined(RDNA2)
  3229. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3230. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3231. const int nwarps = NWARPS_Q5_1_RDNA2;
  3232. #else
  3233. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3234. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3235. const int nwarps = NWARPS_Q5_1_RDNA1;
  3236. #endif // defined(RDNA3) || defined(RDNA2)
  3237. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3238. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3239. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3240. #elif __CUDA_ARCH__ >= CC_VOLTA
  3241. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3242. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3243. const int nwarps = NWARPS_Q5_1_AMPERE;
  3244. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3245. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3246. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3247. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3248. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3249. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3250. const int nwarps = NWARPS_Q5_1_PASCAL;
  3251. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3252. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3253. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3254. #else
  3255. (void) vec_dot_q5_1_q8_1_mul_mat;
  3256. bad_arch();
  3257. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3258. }
  3259. #define MMQ_X_Q8_0_RDNA2 64
  3260. #define MMQ_Y_Q8_0_RDNA2 128
  3261. #define NWARPS_Q8_0_RDNA2 8
  3262. #define MMQ_X_Q8_0_RDNA1 64
  3263. #define MMQ_Y_Q8_0_RDNA1 64
  3264. #define NWARPS_Q8_0_RDNA1 8
  3265. #if defined(CUDA_USE_TENSOR_CORES)
  3266. #define MMQ_X_Q8_0_AMPERE 4
  3267. #define MMQ_Y_Q8_0_AMPERE 32
  3268. #define NWARPS_Q8_0_AMPERE 4
  3269. #else
  3270. #define MMQ_X_Q8_0_AMPERE 128
  3271. #define MMQ_Y_Q8_0_AMPERE 64
  3272. #define NWARPS_Q8_0_AMPERE 4
  3273. #endif
  3274. #define MMQ_X_Q8_0_PASCAL 64
  3275. #define MMQ_Y_Q8_0_PASCAL 64
  3276. #define NWARPS_Q8_0_PASCAL 8
  3277. template <bool need_check> static __global__ void
  3278. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3279. #if defined(RDNA3) || defined(RDNA2)
  3280. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3281. #endif // defined(RDNA3) || defined(RDNA2)
  3282. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3283. mul_mat_q8_0(
  3284. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3285. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3286. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3287. #if defined(RDNA3) || defined(RDNA2)
  3288. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3289. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3290. const int nwarps = NWARPS_Q8_0_RDNA2;
  3291. #else
  3292. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3293. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3294. const int nwarps = NWARPS_Q8_0_RDNA1;
  3295. #endif // defined(RDNA3) || defined(RDNA2)
  3296. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3297. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3298. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3299. #elif __CUDA_ARCH__ >= CC_VOLTA
  3300. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3301. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3302. const int nwarps = NWARPS_Q8_0_AMPERE;
  3303. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3304. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3305. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3306. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3307. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3308. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3309. const int nwarps = NWARPS_Q8_0_PASCAL;
  3310. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3311. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3312. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3313. #else
  3314. (void) vec_dot_q8_0_q8_1_mul_mat;
  3315. bad_arch();
  3316. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3317. }
  3318. #define MMQ_X_Q2_K_RDNA2 64
  3319. #define MMQ_Y_Q2_K_RDNA2 128
  3320. #define NWARPS_Q2_K_RDNA2 8
  3321. #define MMQ_X_Q2_K_RDNA1 128
  3322. #define MMQ_Y_Q2_K_RDNA1 32
  3323. #define NWARPS_Q2_K_RDNA1 8
  3324. #if defined(CUDA_USE_TENSOR_CORES)
  3325. #define MMQ_X_Q2_K_AMPERE 4
  3326. #define MMQ_Y_Q2_K_AMPERE 32
  3327. #define NWARPS_Q2_K_AMPERE 4
  3328. #else
  3329. #define MMQ_X_Q2_K_AMPERE 64
  3330. #define MMQ_Y_Q2_K_AMPERE 128
  3331. #define NWARPS_Q2_K_AMPERE 4
  3332. #endif
  3333. #define MMQ_X_Q2_K_PASCAL 64
  3334. #define MMQ_Y_Q2_K_PASCAL 64
  3335. #define NWARPS_Q2_K_PASCAL 8
  3336. template <bool need_check> static __global__ void
  3337. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3338. #if defined(RDNA3) || defined(RDNA2)
  3339. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3340. #endif // defined(RDNA3) || defined(RDNA2)
  3341. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3342. mul_mat_q2_K(
  3343. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3344. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3345. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3346. #if defined(RDNA3) || defined(RDNA2)
  3347. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3348. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3349. const int nwarps = NWARPS_Q2_K_RDNA2;
  3350. #else
  3351. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3352. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3353. const int nwarps = NWARPS_Q2_K_RDNA1;
  3354. #endif // defined(RDNA3) || defined(RDNA2)
  3355. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3356. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3357. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3358. #elif __CUDA_ARCH__ >= CC_VOLTA
  3359. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3360. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3361. const int nwarps = NWARPS_Q2_K_AMPERE;
  3362. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3363. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3364. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3365. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3366. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3367. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3368. const int nwarps = NWARPS_Q2_K_PASCAL;
  3369. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3370. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3371. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3372. #else
  3373. (void) vec_dot_q2_K_q8_1_mul_mat;
  3374. bad_arch();
  3375. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3376. }
  3377. #define MMQ_X_Q3_K_RDNA2 128
  3378. #define MMQ_Y_Q3_K_RDNA2 64
  3379. #define NWARPS_Q3_K_RDNA2 8
  3380. #define MMQ_X_Q3_K_RDNA1 32
  3381. #define MMQ_Y_Q3_K_RDNA1 128
  3382. #define NWARPS_Q3_K_RDNA1 8
  3383. #if defined(CUDA_USE_TENSOR_CORES)
  3384. #define MMQ_X_Q3_K_AMPERE 4
  3385. #define MMQ_Y_Q3_K_AMPERE 32
  3386. #define NWARPS_Q3_K_AMPERE 4
  3387. #else
  3388. #define MMQ_X_Q3_K_AMPERE 128
  3389. #define MMQ_Y_Q3_K_AMPERE 128
  3390. #define NWARPS_Q3_K_AMPERE 4
  3391. #endif
  3392. #define MMQ_X_Q3_K_PASCAL 64
  3393. #define MMQ_Y_Q3_K_PASCAL 64
  3394. #define NWARPS_Q3_K_PASCAL 8
  3395. template <bool need_check> static __global__ void
  3396. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3397. #if defined(RDNA3) || defined(RDNA2)
  3398. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3399. #endif // defined(RDNA3) || defined(RDNA2)
  3400. #elif __CUDA_ARCH__ < CC_VOLTA
  3401. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3402. #endif // __CUDA_ARCH__ < CC_VOLTA
  3403. mul_mat_q3_K(
  3404. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3405. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3406. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3407. #if defined(RDNA3) || defined(RDNA2)
  3408. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3409. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3410. const int nwarps = NWARPS_Q3_K_RDNA2;
  3411. #else
  3412. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3413. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3414. const int nwarps = NWARPS_Q3_K_RDNA1;
  3415. #endif // defined(RDNA3) || defined(RDNA2)
  3416. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3417. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3418. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3419. #elif __CUDA_ARCH__ >= CC_VOLTA
  3420. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3421. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3422. const int nwarps = NWARPS_Q3_K_AMPERE;
  3423. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3424. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3425. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3426. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3427. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3428. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3429. const int nwarps = NWARPS_Q3_K_PASCAL;
  3430. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3431. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3432. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3433. #else
  3434. (void) vec_dot_q3_K_q8_1_mul_mat;
  3435. bad_arch();
  3436. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3437. }
  3438. #define MMQ_X_Q4_K_RDNA2 64
  3439. #define MMQ_Y_Q4_K_RDNA2 128
  3440. #define NWARPS_Q4_K_RDNA2 8
  3441. #define MMQ_X_Q4_K_RDNA1 32
  3442. #define MMQ_Y_Q4_K_RDNA1 64
  3443. #define NWARPS_Q4_K_RDNA1 8
  3444. #if defined(CUDA_USE_TENSOR_CORES)
  3445. #define MMQ_X_Q4_K_AMPERE 4
  3446. #define MMQ_Y_Q4_K_AMPERE 32
  3447. #define NWARPS_Q4_K_AMPERE 4
  3448. #else
  3449. #define MMQ_X_Q4_K_AMPERE 64
  3450. #define MMQ_Y_Q4_K_AMPERE 128
  3451. #define NWARPS_Q4_K_AMPERE 4
  3452. #endif
  3453. #define MMQ_X_Q4_K_PASCAL 64
  3454. #define MMQ_Y_Q4_K_PASCAL 64
  3455. #define NWARPS_Q4_K_PASCAL 8
  3456. template <bool need_check> static __global__ void
  3457. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3458. #if defined(RDNA3) || defined(RDNA2)
  3459. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3460. #endif // defined(RDNA3) || defined(RDNA2)
  3461. #elif __CUDA_ARCH__ < CC_VOLTA
  3462. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3463. #endif // __CUDA_ARCH__ < CC_VOLTA
  3464. mul_mat_q4_K(
  3465. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3466. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3467. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3468. #if defined(RDNA3) || defined(RDNA2)
  3469. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3470. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3471. const int nwarps = NWARPS_Q4_K_RDNA2;
  3472. #else
  3473. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3474. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3475. const int nwarps = NWARPS_Q4_K_RDNA1;
  3476. #endif // defined(RDNA3) || defined(RDNA2)
  3477. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3478. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3479. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3480. #elif __CUDA_ARCH__ >= CC_VOLTA
  3481. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3482. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3483. const int nwarps = NWARPS_Q4_K_AMPERE;
  3484. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3485. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3486. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3487. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3488. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3489. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3490. const int nwarps = NWARPS_Q4_K_PASCAL;
  3491. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3492. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3493. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3494. #else
  3495. (void) vec_dot_q4_K_q8_1_mul_mat;
  3496. bad_arch();
  3497. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3498. }
  3499. #define MMQ_X_Q5_K_RDNA2 64
  3500. #define MMQ_Y_Q5_K_RDNA2 128
  3501. #define NWARPS_Q5_K_RDNA2 8
  3502. #define MMQ_X_Q5_K_RDNA1 32
  3503. #define MMQ_Y_Q5_K_RDNA1 64
  3504. #define NWARPS_Q5_K_RDNA1 8
  3505. #if defined(CUDA_USE_TENSOR_CORES)
  3506. #define MMQ_X_Q5_K_AMPERE 4
  3507. #define MMQ_Y_Q5_K_AMPERE 32
  3508. #define NWARPS_Q5_K_AMPERE 4
  3509. #else
  3510. #define MMQ_X_Q5_K_AMPERE 64
  3511. #define MMQ_Y_Q5_K_AMPERE 128
  3512. #define NWARPS_Q5_K_AMPERE 4
  3513. #endif
  3514. #define MMQ_X_Q5_K_PASCAL 64
  3515. #define MMQ_Y_Q5_K_PASCAL 64
  3516. #define NWARPS_Q5_K_PASCAL 8
  3517. template <bool need_check> static __global__ void
  3518. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3519. #if defined(RDNA3) || defined(RDNA2)
  3520. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3521. #endif // defined(RDNA3) || defined(RDNA2)
  3522. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3523. mul_mat_q5_K(
  3524. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3525. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3526. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3527. #if defined(RDNA3) || defined(RDNA2)
  3528. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3529. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3530. const int nwarps = NWARPS_Q5_K_RDNA2;
  3531. #else
  3532. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3533. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3534. const int nwarps = NWARPS_Q5_K_RDNA1;
  3535. #endif // defined(RDNA3) || defined(RDNA2)
  3536. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3537. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3538. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3539. #elif __CUDA_ARCH__ >= CC_VOLTA
  3540. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3541. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3542. const int nwarps = NWARPS_Q5_K_AMPERE;
  3543. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3544. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3545. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3546. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3547. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3548. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3549. const int nwarps = NWARPS_Q5_K_PASCAL;
  3550. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3551. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3552. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3553. #else
  3554. (void) vec_dot_q5_K_q8_1_mul_mat;
  3555. bad_arch();
  3556. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3557. }
  3558. #define MMQ_X_Q6_K_RDNA2 64
  3559. #define MMQ_Y_Q6_K_RDNA2 128
  3560. #define NWARPS_Q6_K_RDNA2 8
  3561. #define MMQ_X_Q6_K_RDNA1 32
  3562. #define MMQ_Y_Q6_K_RDNA1 64
  3563. #define NWARPS_Q6_K_RDNA1 8
  3564. #if defined(CUDA_USE_TENSOR_CORES)
  3565. #define MMQ_X_Q6_K_AMPERE 4
  3566. #define MMQ_Y_Q6_K_AMPERE 32
  3567. #define NWARPS_Q6_K_AMPERE 4
  3568. #else
  3569. #define MMQ_X_Q6_K_AMPERE 64
  3570. #define MMQ_Y_Q6_K_AMPERE 64
  3571. #define NWARPS_Q6_K_AMPERE 4
  3572. #endif
  3573. #define MMQ_X_Q6_K_PASCAL 64
  3574. #define MMQ_Y_Q6_K_PASCAL 64
  3575. #define NWARPS_Q6_K_PASCAL 8
  3576. template <bool need_check> static __global__ void
  3577. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3578. #if defined(RDNA3) || defined(RDNA2)
  3579. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3580. #endif // defined(RDNA3) || defined(RDNA2)
  3581. #elif __CUDA_ARCH__ < CC_VOLTA
  3582. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3583. #endif // __CUDA_ARCH__ < CC_VOLTA
  3584. mul_mat_q6_K(
  3585. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3586. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3587. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3588. #if defined(RDNA3) || defined(RDNA2)
  3589. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3590. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3591. const int nwarps = NWARPS_Q6_K_RDNA2;
  3592. #else
  3593. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3594. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3595. const int nwarps = NWARPS_Q6_K_RDNA1;
  3596. #endif // defined(RDNA3) || defined(RDNA2)
  3597. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3598. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3599. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3600. #elif __CUDA_ARCH__ >= CC_VOLTA
  3601. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3602. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3603. const int nwarps = NWARPS_Q6_K_AMPERE;
  3604. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3605. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3606. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3607. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3608. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3609. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3610. const int nwarps = NWARPS_Q6_K_PASCAL;
  3611. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3612. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3613. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3614. #else
  3615. (void) vec_dot_q6_K_q8_1_mul_mat;
  3616. bad_arch();
  3617. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3618. }
  3619. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3620. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3621. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3622. if (row >= nrows) {
  3623. return;
  3624. }
  3625. const int blocks_per_row = ncols / qk;
  3626. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3627. // partial sum for each thread
  3628. float tmp = 0.0f;
  3629. const block_q_t * x = (const block_q_t *) vx;
  3630. const block_q8_1 * y = (const block_q8_1 *) vy;
  3631. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3632. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3633. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3634. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3635. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3636. }
  3637. // sum up partial sums and write back result
  3638. #pragma unroll
  3639. for (int mask = 16; mask > 0; mask >>= 1) {
  3640. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3641. }
  3642. if (threadIdx.x == 0) {
  3643. dst[row] = tmp;
  3644. }
  3645. }
  3646. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3647. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3648. // qk = quantized weights per x block
  3649. // qr = number of quantized weights per data value in x block
  3650. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3651. if (row >= nrows) {
  3652. return;
  3653. }
  3654. const int tid = threadIdx.x;
  3655. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3656. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3657. const int y_offset = qr == 1 ? 1 : qk/2;
  3658. // partial sum for each thread
  3659. #ifdef GGML_CUDA_F16
  3660. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3661. #else
  3662. float tmp = 0.0f;
  3663. #endif // GGML_CUDA_F16
  3664. for (int i = 0; i < ncols; i += iter_stride) {
  3665. const int col = i + vals_per_iter*tid;
  3666. const int ib = (row*ncols + col)/qk; // x block index
  3667. const int iqs = (col%qk)/qr; // x quant index
  3668. const int iybs = col - col%qk; // y block start index
  3669. // processing >2 values per i iter is faster for fast GPUs
  3670. #pragma unroll
  3671. for (int j = 0; j < vals_per_iter; j += 2) {
  3672. // process 2 vals per j iter
  3673. // dequantize
  3674. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3675. dfloat2 v;
  3676. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3677. // matrix multiplication
  3678. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3679. #ifdef GGML_CUDA_F16
  3680. tmp += __hmul2(v, {
  3681. y[iybs + iqs + j/qr + 0],
  3682. y[iybs + iqs + j/qr + y_offset]
  3683. });
  3684. #else
  3685. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3686. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3687. #endif // GGML_CUDA_F16
  3688. }
  3689. }
  3690. // sum up partial sums and write back result
  3691. #pragma unroll
  3692. for (int mask = 16; mask > 0; mask >>= 1) {
  3693. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3694. }
  3695. if (tid == 0) {
  3696. #ifdef GGML_CUDA_F16
  3697. dst[row] = tmp.x + tmp.y;
  3698. #else
  3699. dst[row] = tmp;
  3700. #endif // GGML_CUDA_F16
  3701. }
  3702. }
  3703. static __global__ void mul_mat_p021_f16_f32(
  3704. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3705. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3706. const half * x = (const half *) vx;
  3707. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3708. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3709. const int channel_x = channel / (nchannels_y / nchannels_x);
  3710. const int nrows_y = ncols_x;
  3711. const int nrows_dst = nrows_x;
  3712. const int row_dst = row_x;
  3713. float tmp = 0.0f;
  3714. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3715. const int col_x = col_x0 + threadIdx.x;
  3716. if (col_x >= ncols_x) {
  3717. break;
  3718. }
  3719. // x is transposed and permuted
  3720. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3721. const float xi = __half2float(x[ix]);
  3722. const int row_y = col_x;
  3723. // y is not transposed but permuted
  3724. const int iy = channel*nrows_y + row_y;
  3725. tmp += xi * y[iy];
  3726. }
  3727. // dst is not transposed and not permuted
  3728. const int idst = channel*nrows_dst + row_dst;
  3729. // sum up partial sums and write back result
  3730. #pragma unroll
  3731. for (int mask = 16; mask > 0; mask >>= 1) {
  3732. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3733. }
  3734. if (threadIdx.x == 0) {
  3735. dst[idst] = tmp;
  3736. }
  3737. }
  3738. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3739. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3740. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3741. const half * x = (const half *) vx;
  3742. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3743. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3744. const int channel_x = channel / channel_x_divisor;
  3745. const int nrows_y = ncols_x;
  3746. const int nrows_dst = nrows_x;
  3747. const int row_dst = row_x;
  3748. const int idst = channel*nrows_dst + row_dst;
  3749. float tmp = 0.0f;
  3750. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3751. const int col_x = col_x0 + threadIdx.x;
  3752. if (col_x >= ncols_x) {
  3753. break;
  3754. }
  3755. const int row_y = col_x;
  3756. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3757. const int iy = channel*nrows_y + row_y;
  3758. const float xi = __half2float(x[ix]);
  3759. tmp += xi * y[iy];
  3760. }
  3761. // sum up partial sums and write back result
  3762. #pragma unroll
  3763. for (int mask = 16; mask > 0; mask >>= 1) {
  3764. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3765. }
  3766. if (threadIdx.x == 0) {
  3767. dst[idst] = tmp;
  3768. }
  3769. }
  3770. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3771. const float * xi = (const float *) cxi;
  3772. float * dsti = (float *) cdsti;
  3773. *dsti = *xi;
  3774. }
  3775. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3776. const float * xi = (const float *) cxi;
  3777. half * dsti = (half *) cdsti;
  3778. *dsti = __float2half(*xi);
  3779. }
  3780. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3781. const half * xi = (const half *) cxi;
  3782. half * dsti = (half *) cdsti;
  3783. *dsti = *xi;
  3784. }
  3785. template <cpy_kernel_t cpy_1>
  3786. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3787. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3788. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3789. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3790. if (i >= ne) {
  3791. return;
  3792. }
  3793. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3794. // then combine those indices with the corresponding byte offsets to get the total offsets
  3795. const int i02 = i / (ne00*ne01);
  3796. const int i01 = (i - i02*ne01*ne00) / ne00;
  3797. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3798. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3799. const int i12 = i / (ne10*ne11);
  3800. const int i11 = (i - i12*ne10*ne11) / ne10;
  3801. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3802. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3803. cpy_1(cx + x_offset, cdst + dst_offset);
  3804. }
  3805. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  3806. const float * xi = (const float *) cxi;
  3807. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  3808. float amax = 0.0f; // absolute max
  3809. for (int j = 0; j < QK8_0; j++) {
  3810. const float v = xi[j];
  3811. amax = fmaxf(amax, fabsf(v));
  3812. }
  3813. const float d = amax / ((1 << 7) - 1);
  3814. const float id = d ? 1.0f/d : 0.0f;
  3815. dsti->d = d;
  3816. for (int j = 0; j < QK8_0; ++j) {
  3817. const float x0 = xi[j]*id;
  3818. dsti->qs[j] = roundf(x0);
  3819. }
  3820. }
  3821. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  3822. const float * xi = (const float *) cxi;
  3823. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  3824. float amax = 0.0f;
  3825. float vmax = 0.0f;
  3826. for (int j = 0; j < QK4_0; ++j) {
  3827. const float v = xi[j];
  3828. if (amax < fabsf(v)) {
  3829. amax = fabsf(v);
  3830. vmax = v;
  3831. }
  3832. }
  3833. const float d = vmax / -8;
  3834. const float id = d ? 1.0f/d : 0.0f;
  3835. dsti->d = d;
  3836. for (int j = 0; j < QK4_0/2; ++j) {
  3837. const float x0 = xi[0 + j]*id;
  3838. const float x1 = xi[QK4_0/2 + j]*id;
  3839. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  3840. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  3841. dsti->qs[j] = xi0;
  3842. dsti->qs[j] |= xi1 << 4;
  3843. }
  3844. }
  3845. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  3846. const float * xi = (const float *) cxi;
  3847. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  3848. float vmin = FLT_MAX;
  3849. float vmax = -FLT_MAX;
  3850. for (int j = 0; j < QK4_1; ++j) {
  3851. const float v = xi[j];
  3852. if (v < vmin) vmin = v;
  3853. if (v > vmax) vmax = v;
  3854. }
  3855. const float d = (vmax - vmin) / ((1 << 4) - 1);
  3856. const float id = d ? 1.0f/d : 0.0f;
  3857. dsti->dm.x = d;
  3858. dsti->dm.y = vmin;
  3859. for (int j = 0; j < QK4_1/2; ++j) {
  3860. const float x0 = (xi[0 + j] - vmin)*id;
  3861. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  3862. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  3863. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  3864. dsti->qs[j] = xi0;
  3865. dsti->qs[j] |= xi1 << 4;
  3866. }
  3867. }
  3868. template <cpy_kernel_t cpy_blck, int qk>
  3869. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  3870. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3871. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3872. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  3873. if (i >= ne) {
  3874. return;
  3875. }
  3876. const int i02 = i / (ne00*ne01);
  3877. const int i01 = (i - i02*ne01*ne00) / ne00;
  3878. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  3879. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3880. const int i12 = i / (ne10*ne11);
  3881. const int i11 = (i - i12*ne10*ne11) / ne10;
  3882. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  3883. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3884. cpy_blck(cx + x_offset, cdst + dst_offset);
  3885. }
  3886. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3887. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3888. return 1.0f - min(1.0f, max(0.0f, y));
  3889. }
  3890. struct rope_corr_dims {
  3891. float v[4];
  3892. };
  3893. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3894. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3895. static __device__ void rope_yarn(
  3896. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3897. float * cos_theta, float * sin_theta
  3898. ) {
  3899. // Get n-d rotational scaling corrected for extrapolation
  3900. float theta_interp = freq_scale * theta_extrap;
  3901. float theta = theta_interp;
  3902. if (ext_factor != 0.0f) {
  3903. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3904. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3905. // Get n-d magnitude scaling corrected for interpolation
  3906. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3907. }
  3908. *cos_theta = cosf(theta) * mscale;
  3909. *sin_theta = sinf(theta) * mscale;
  3910. }
  3911. // rope == RoPE == rotary positional embedding
  3912. template<typename T, bool has_pos>
  3913. static __global__ void rope(
  3914. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3915. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3916. ) {
  3917. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3918. if (col >= ncols) {
  3919. return;
  3920. }
  3921. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3922. const int i = row*ncols + col;
  3923. const int i2 = row/p_delta_rows;
  3924. const int p = has_pos ? pos[i2] : 0;
  3925. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3926. float cos_theta, sin_theta;
  3927. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3928. const float x0 = x[i + 0];
  3929. const float x1 = x[i + 1];
  3930. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3931. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3932. }
  3933. template<typename T, bool has_pos>
  3934. static __global__ void rope_neox(
  3935. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  3936. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  3937. ) {
  3938. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3939. if (col >= ncols) {
  3940. return;
  3941. }
  3942. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3943. const int ib = col / n_dims;
  3944. const int ic = col % n_dims;
  3945. if (ib > 0) {
  3946. const int i = row*ncols + ib*n_dims + ic;
  3947. dst[i + 0] = x[i + 0];
  3948. dst[i + 1] = x[i + 1];
  3949. return;
  3950. }
  3951. const int i = row*ncols + ib*n_dims + ic/2;
  3952. const int i2 = row/p_delta_rows;
  3953. float cur_rot = inv_ndims * ic - ib;
  3954. const int p = has_pos ? pos[i2] : 0;
  3955. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  3956. float cos_theta, sin_theta;
  3957. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3958. const float x0 = x[i + 0];
  3959. const float x1 = x[i + n_dims/2];
  3960. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3961. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  3962. }
  3963. static __global__ void rope_glm_f32(
  3964. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3965. int n_ctx
  3966. ) {
  3967. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3968. const int half_n_dims = ncols/4;
  3969. if (col >= half_n_dims) {
  3970. return;
  3971. }
  3972. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3973. const int i = row*ncols + col;
  3974. const int i2 = row/p_delta_rows;
  3975. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  3976. // FIXME: this is likely wrong
  3977. const int p = pos != nullptr ? pos[i2] : 0;
  3978. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  3979. const float sin_theta = sinf(theta);
  3980. const float cos_theta = cosf(theta);
  3981. const float x0 = x[i + 0];
  3982. const float x1 = x[i + half_n_dims];
  3983. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3984. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  3985. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  3986. const float sin_block_theta = sinf(block_theta);
  3987. const float cos_block_theta = cosf(block_theta);
  3988. const float x2 = x[i + half_n_dims * 2];
  3989. const float x3 = x[i + half_n_dims * 3];
  3990. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  3991. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  3992. }
  3993. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  3994. const int n_heads_log2_floor, const float m0, const float m1) {
  3995. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3996. if (col >= ncols) {
  3997. return;
  3998. }
  3999. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4000. const int i = row*ncols + col;
  4001. const int k = row/k_rows;
  4002. float m_k;
  4003. if (k < n_heads_log2_floor) {
  4004. m_k = powf(m0, k + 1);
  4005. } else {
  4006. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4007. }
  4008. dst[i] = col * m_k + x[i];
  4009. }
  4010. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4011. const int row = blockIdx.y;
  4012. const int col = threadIdx.x;
  4013. float sum = 0.0f;
  4014. for (int i = col; i < ncols; i += blockDim.x) {
  4015. sum += x[row * ncols + i];
  4016. }
  4017. sum = warp_reduce_sum(sum);
  4018. if (col == 0) {
  4019. dst[row] = sum;
  4020. }
  4021. }
  4022. template<typename T>
  4023. static inline __device__ void swap(T & a, T & b) {
  4024. T tmp = a;
  4025. a = b;
  4026. b = tmp;
  4027. }
  4028. template<ggml_sort_order order>
  4029. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4030. // bitonic sort
  4031. int col = threadIdx.x;
  4032. int row = blockIdx.y;
  4033. if (col >= ncols) return;
  4034. const float * x_row = x + row * ncols;
  4035. int * dst_row = dst + row * ncols;
  4036. // initialize indices
  4037. if (col < ncols) {
  4038. dst_row[col] = col;
  4039. }
  4040. __syncthreads();
  4041. for (int k = 2; k <= ncols; k *= 2) {
  4042. for (int j = k / 2; j > 0; j /= 2) {
  4043. int ixj = col ^ j;
  4044. if (ixj > col) {
  4045. if ((col & k) == 0) {
  4046. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  4047. swap(dst_row[col], dst_row[ixj]);
  4048. }
  4049. } else {
  4050. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  4051. swap(dst_row[col], dst_row[ixj]);
  4052. }
  4053. }
  4054. }
  4055. __syncthreads();
  4056. }
  4057. }
  4058. }
  4059. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4060. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4061. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4062. if (col >= ncols) {
  4063. return;
  4064. }
  4065. const int i = row*ncols + col;
  4066. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4067. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4068. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4069. }
  4070. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {
  4071. const int tid = threadIdx.x;
  4072. const int rowx = blockIdx.x;
  4073. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4074. const int block_size = blockDim.x;
  4075. const int warp_id = threadIdx.x / WARP_SIZE;
  4076. const int lane_id = threadIdx.x % WARP_SIZE;
  4077. __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];
  4078. float max_val = -INFINITY;
  4079. for (int col = tid; col < ncols; col += block_size) {
  4080. const int ix = rowx*ncols + col;
  4081. const int iy = rowy*ncols + col;
  4082. max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));
  4083. }
  4084. // find the max value in the block
  4085. max_val = warp_reduce_max(max_val);
  4086. if (block_size > WARP_SIZE) {
  4087. if (warp_id == 0) {
  4088. buf[lane_id] = -INFINITY;
  4089. }
  4090. __syncthreads();
  4091. if (lane_id == 0) {
  4092. buf[warp_id] = max_val;
  4093. }
  4094. __syncthreads();
  4095. max_val = buf[lane_id];
  4096. max_val = warp_reduce_max(max_val);
  4097. }
  4098. float tmp = 0.f;
  4099. for (int col = tid; col < ncols; col += block_size) {
  4100. const int ix = rowx*ncols + col;
  4101. const int iy = rowy*ncols + col;
  4102. const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);
  4103. tmp += val;
  4104. dst[ix] = val;
  4105. }
  4106. // find the sum of exps in the block
  4107. tmp = warp_reduce_sum(tmp);
  4108. if (block_size > WARP_SIZE) {
  4109. if (warp_id == 0) {
  4110. buf[lane_id] = 0.f;
  4111. }
  4112. __syncthreads();
  4113. if (lane_id == 0) {
  4114. buf[warp_id] = tmp;
  4115. }
  4116. __syncthreads();
  4117. tmp = buf[lane_id];
  4118. tmp = warp_reduce_sum(tmp);
  4119. }
  4120. const float inv_tmp = 1.f / tmp;
  4121. for (int col = tid; col < ncols; col += block_size) {
  4122. const int i = rowx*ncols + col;
  4123. dst[i] *= inv_tmp;
  4124. }
  4125. }
  4126. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4127. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4128. if (i >= k) {
  4129. return;
  4130. }
  4131. dst[i] = scale * x[i];
  4132. }
  4133. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4134. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4135. if (i >= k) {
  4136. return;
  4137. }
  4138. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4139. }
  4140. static __global__ void im2col_f32_f16(
  4141. const float * x, half * dst,
  4142. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4143. int s0, int s1, int p0, int p1, int d0, int d1) {
  4144. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4145. if (i >= pelements) {
  4146. return;
  4147. }
  4148. const int ksize = OW * (KH > 1 ? KW : 1);
  4149. const int kx = i / ksize;
  4150. const int kd = kx * ksize;
  4151. const int ky = (i - kd) / OW;
  4152. const int ix = i % OW;
  4153. const int iiw = ix * s0 + kx * d0 - p0;
  4154. const int iih = blockIdx.y * s1 + ky * d1 - p1;
  4155. const int offset_dst =
  4156. (blockIdx.y * OW + ix) * CHW +
  4157. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4158. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4159. dst[offset_dst] = __float2half(0.0f);
  4160. } else {
  4161. const int offset_src = blockIdx.z * offset_delta;
  4162. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4163. }
  4164. }
  4165. template<int qk, int qr, dequantize_kernel_t dq>
  4166. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4167. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4168. GGML_TENSOR_BINARY_OP_LOCALS
  4169. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4170. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4171. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4172. // strides in elements
  4173. //const size_t s0 = nb0 / ggml_element_size(dst);
  4174. const size_t s1 = nb1 / ggml_element_size(dst);
  4175. const size_t s2 = nb2 / ggml_element_size(dst);
  4176. const size_t s3 = nb3 / ggml_element_size(dst);
  4177. const size_t s10 = nb10 / ggml_element_size(src1);
  4178. const size_t s11 = nb11 / ggml_element_size(src1);
  4179. const size_t s12 = nb12 / ggml_element_size(src1);
  4180. //const size_t s13 = nb13 / ggml_element_size(src1);
  4181. GGML_ASSERT(ne00 % 2 == 0);
  4182. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4183. src0_dd, src1_dd, dst_dd,
  4184. ne00, /*ne01, ne02, ne03,*/
  4185. /*ne10, ne11,*/ ne12, /*ne13,*/
  4186. /* s0,*/ s1, s2, s3,
  4187. /* nb00,*/ nb01, nb02, nb03,
  4188. s10, s11, s12/*, s13*/);
  4189. (void) dst;
  4190. }
  4191. template<typename src0_t>
  4192. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4193. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4194. GGML_TENSOR_BINARY_OP_LOCALS
  4195. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4196. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4197. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4198. // strides in elements
  4199. //const size_t s0 = nb0 / ggml_element_size(dst);
  4200. const size_t s1 = nb1 / ggml_element_size(dst);
  4201. const size_t s2 = nb2 / ggml_element_size(dst);
  4202. const size_t s3 = nb3 / ggml_element_size(dst);
  4203. const size_t s10 = nb10 / ggml_element_size(src1);
  4204. const size_t s11 = nb11 / ggml_element_size(src1);
  4205. const size_t s12 = nb12 / ggml_element_size(src1);
  4206. //const size_t s13 = nb13 / ggml_element_size(src1);
  4207. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4208. src0_dd, src1_dd, dst_dd,
  4209. ne00, /*ne01, ne02, ne03,*/
  4210. /*ne10, ne11,*/ ne12, /*ne13,*/
  4211. /* s0,*/ s1, s2, s3,
  4212. /* nb00,*/ nb01, nb02, nb03,
  4213. s10, s11, s12/*, s13*/);
  4214. (void) dst;
  4215. }
  4216. template<float (*bin_op)(const float, const float)>
  4217. struct bin_bcast_cuda {
  4218. template<typename src0_t, typename src1_t, typename dst_t>
  4219. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4220. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4221. cudaStream_t stream) {
  4222. GGML_TENSOR_BINARY_OP_LOCALS
  4223. int nr0 = ne10/ne0;
  4224. int nr1 = ne11/ne1;
  4225. int nr2 = ne12/ne2;
  4226. int nr3 = ne13/ne3;
  4227. int nr[4] = { nr0, nr1, nr2, nr3 };
  4228. // collapse dimensions until first broadcast dimension
  4229. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4230. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4231. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4232. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4233. auto collapse = [](int64_t cne[]) {
  4234. cne[0] *= cne[1];
  4235. cne[1] = cne[2];
  4236. cne[2] = cne[3];
  4237. cne[3] = 1;
  4238. };
  4239. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  4240. cnb[1] *= cne[1];
  4241. cnb[2] *= cne[2];
  4242. cnb[3] *= cne[3];
  4243. };
  4244. for (int i = 0; i < 4; i++) {
  4245. if (nr[i] != 1) {
  4246. break;
  4247. }
  4248. if (i > 0) {
  4249. collapse_nb(cnb0, cne0);
  4250. collapse_nb(cnb1, cne1);
  4251. collapse(cne0);
  4252. collapse(cne1);
  4253. }
  4254. }
  4255. {
  4256. int64_t ne0 = cne0[0];
  4257. int64_t ne1 = cne0[1];
  4258. int64_t ne2 = cne0[2];
  4259. int64_t ne3 = cne0[3];
  4260. int64_t ne10 = cne1[0];
  4261. int64_t ne11 = cne1[1];
  4262. int64_t ne12 = cne1[2];
  4263. int64_t ne13 = cne1[3];
  4264. size_t nb0 = cnb0[0];
  4265. size_t nb1 = cnb0[1];
  4266. size_t nb2 = cnb0[2];
  4267. size_t nb3 = cnb0[3];
  4268. size_t nb10 = cnb1[0];
  4269. size_t nb11 = cnb1[1];
  4270. size_t nb12 = cnb1[2];
  4271. size_t nb13 = cnb1[3];
  4272. size_t s0 = nb0 / sizeof(dst_t);
  4273. size_t s1 = nb1 / sizeof(dst_t);
  4274. size_t s2 = nb2 / sizeof(dst_t);
  4275. size_t s3 = nb3 / sizeof(dst_t);
  4276. size_t s10 = nb10 / sizeof(src1_t);
  4277. size_t s11 = nb11 / sizeof(src1_t);
  4278. size_t s12 = nb12 / sizeof(src1_t);
  4279. size_t s13 = nb13 / sizeof(src1_t);
  4280. GGML_ASSERT(s0 == 1);
  4281. GGML_ASSERT(s10 == 1);
  4282. const int block_size = 128;
  4283. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4284. dim3 block_dims;
  4285. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4286. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4287. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4288. dim3 block_nums(
  4289. (hne0 + block_dims.x - 1) / block_dims.x,
  4290. (ne1 + block_dims.y - 1) / block_dims.y,
  4291. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4292. );
  4293. if (block_nums.z > 65535) {
  4294. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4295. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4296. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4297. src0_dd, src1_dd, dst_dd,
  4298. ne0, ne1, ne2, ne3,
  4299. ne10, ne11, ne12, ne13,
  4300. /* s0, */ s1, s2, s3,
  4301. /* s10, */ s11, s12, s13);
  4302. } else {
  4303. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4304. src0_dd, src1_dd, dst_dd,
  4305. ne0, ne1, ne2, ne3,
  4306. ne10, ne11, ne12, ne13,
  4307. /* s0, */ s1, s2, s3,
  4308. /* s10, */ s11, s12, s13);
  4309. }
  4310. }
  4311. }
  4312. };
  4313. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4314. const int ne10, const int ne11, const int ne12,
  4315. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4316. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4317. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4318. }
  4319. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4320. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4321. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4322. }
  4323. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4324. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4325. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4326. }
  4327. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4328. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4329. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4330. }
  4331. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4332. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4333. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4334. }
  4335. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4336. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4337. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4338. }
  4339. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4340. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4341. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4342. }
  4343. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4344. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4345. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4346. }
  4347. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4348. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4349. if (ncols < 1024) {
  4350. const dim3 block_dims(WARP_SIZE, 1, 1);
  4351. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4352. } else {
  4353. const dim3 block_dims(1024, 1, 1);
  4354. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4355. }
  4356. }
  4357. static void group_norm_f32_cuda(const float * x, float * dst, const int num_groups, const int group_size, const int ne_elements, cudaStream_t stream) {
  4358. static const float eps = 1e-6f;
  4359. if (group_size < 1024) {
  4360. const dim3 block_dims(WARP_SIZE, 1, 1);
  4361. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4362. } else {
  4363. const dim3 block_dims(1024, 1, 1);
  4364. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4365. }
  4366. }
  4367. static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
  4368. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4369. dim3 gridDim(num_blocks, ne1, ne2);
  4370. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4371. }
  4372. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int scale_factor, cudaStream_t stream) {
  4373. int ne0 = (ne00 * scale_factor);
  4374. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4375. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4376. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4377. }
  4378. static void pad_f32_cuda(const float * x, float * dst,
  4379. const int ne00, const int ne01, const int ne02,
  4380. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4381. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4382. dim3 gridDim(num_blocks, ne1, ne2);
  4383. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4384. }
  4385. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4386. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4387. if (ncols < 1024) {
  4388. const dim3 block_dims(WARP_SIZE, 1, 1);
  4389. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4390. } else {
  4391. const dim3 block_dims(1024, 1, 1);
  4392. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4393. }
  4394. }
  4395. 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) {
  4396. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4397. const dim3 num_blocks(block_num_x, ky, 1);
  4398. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4399. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4400. }
  4401. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4402. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4403. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4404. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4405. }
  4406. template<typename dst_t>
  4407. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4408. const int nb = k / QK_K;
  4409. #if QK_K == 256
  4410. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4411. #else
  4412. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4413. #endif
  4414. }
  4415. template<typename dst_t>
  4416. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4417. const int nb = k / QK_K;
  4418. #if QK_K == 256
  4419. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4420. #else
  4421. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4422. #endif
  4423. }
  4424. template<typename dst_t>
  4425. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4426. const int nb = k / QK_K;
  4427. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4428. }
  4429. template<typename dst_t>
  4430. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4431. const int nb = k / QK_K;
  4432. #if QK_K == 256
  4433. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4434. #else
  4435. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4436. #endif
  4437. }
  4438. template<typename dst_t>
  4439. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4440. const int nb = k / QK_K;
  4441. #if QK_K == 256
  4442. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4443. #else
  4444. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4445. #endif
  4446. }
  4447. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4448. switch (type) {
  4449. case GGML_TYPE_Q4_0:
  4450. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4451. case GGML_TYPE_Q4_1:
  4452. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4453. case GGML_TYPE_Q5_0:
  4454. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4455. case GGML_TYPE_Q5_1:
  4456. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4457. case GGML_TYPE_Q8_0:
  4458. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4459. case GGML_TYPE_Q2_K:
  4460. return dequantize_row_q2_K_cuda;
  4461. case GGML_TYPE_Q3_K:
  4462. return dequantize_row_q3_K_cuda;
  4463. case GGML_TYPE_Q4_K:
  4464. return dequantize_row_q4_K_cuda;
  4465. case GGML_TYPE_Q5_K:
  4466. return dequantize_row_q5_K_cuda;
  4467. case GGML_TYPE_Q6_K:
  4468. return dequantize_row_q6_K_cuda;
  4469. case GGML_TYPE_F32:
  4470. return dequantize_block_cuda<1, 1, convert_f32>;
  4471. default:
  4472. return nullptr;
  4473. }
  4474. }
  4475. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4476. switch (type) {
  4477. case GGML_TYPE_Q4_0:
  4478. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4479. case GGML_TYPE_Q4_1:
  4480. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4481. case GGML_TYPE_Q5_0:
  4482. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4483. case GGML_TYPE_Q5_1:
  4484. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4485. case GGML_TYPE_Q8_0:
  4486. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4487. case GGML_TYPE_Q2_K:
  4488. return dequantize_row_q2_K_cuda;
  4489. case GGML_TYPE_Q3_K:
  4490. return dequantize_row_q3_K_cuda;
  4491. case GGML_TYPE_Q4_K:
  4492. return dequantize_row_q4_K_cuda;
  4493. case GGML_TYPE_Q5_K:
  4494. return dequantize_row_q5_K_cuda;
  4495. case GGML_TYPE_Q6_K:
  4496. return dequantize_row_q6_K_cuda;
  4497. case GGML_TYPE_F16:
  4498. return dequantize_block_cuda<1, 1, convert_f16>;
  4499. default:
  4500. return nullptr;
  4501. }
  4502. }
  4503. 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) {
  4504. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4505. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4506. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4507. const dim3 block_nums(block_num_y, 1, 1);
  4508. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4509. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4510. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4511. }
  4512. 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) {
  4513. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4514. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4515. const dim3 block_nums(block_num_y, 1, 1);
  4516. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4517. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4518. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4519. }
  4520. 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) {
  4521. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4522. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4523. const dim3 block_nums(block_num_y, 1, 1);
  4524. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4525. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4526. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4527. }
  4528. 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) {
  4529. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4530. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4531. const dim3 block_nums(block_num_y, 1, 1);
  4532. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4533. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4534. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4535. }
  4536. 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) {
  4537. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4538. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4539. const dim3 block_nums(block_num_y, 1, 1);
  4540. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4541. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4542. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4543. }
  4544. 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) {
  4545. GGML_ASSERT(ncols % QK_K == 0);
  4546. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4547. const int block_num_y = (nrows + ny - 1) / ny;
  4548. const dim3 block_nums(block_num_y, 1, 1);
  4549. const dim3 block_dims(32, ny, 1);
  4550. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4551. }
  4552. 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) {
  4553. GGML_ASSERT(ncols % QK_K == 0);
  4554. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4555. const int block_num_y = (nrows + ny - 1) / ny;
  4556. const dim3 block_nums(block_num_y, 1, 1);
  4557. const dim3 block_dims(32, ny, 1);
  4558. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4559. }
  4560. 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) {
  4561. GGML_ASSERT(ncols % QK_K == 0);
  4562. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4563. const int block_num_y = (nrows + ny - 1) / ny;
  4564. const dim3 block_nums(block_num_y, 1, 1);
  4565. const dim3 block_dims(32, ny, 1);
  4566. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4567. }
  4568. 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) {
  4569. GGML_ASSERT(ncols % QK_K == 0);
  4570. const dim3 block_dims(32, 1, 1);
  4571. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4572. }
  4573. 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) {
  4574. GGML_ASSERT(ncols % QK_K == 0);
  4575. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4576. const int block_num_y = (nrows + ny - 1) / ny;
  4577. const dim3 block_nums(block_num_y, 1, 1);
  4578. const dim3 block_dims(32, ny, 1);
  4579. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4580. }
  4581. 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) {
  4582. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4583. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4584. const dim3 block_nums(block_num_y, 1, 1);
  4585. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4586. dequantize_mul_mat_vec<1, 1, convert_f16>
  4587. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4588. }
  4589. 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) {
  4590. GGML_ASSERT(ncols % QK4_0 == 0);
  4591. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4592. const dim3 block_nums(block_num_y, 1, 1);
  4593. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4594. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4595. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4596. }
  4597. 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) {
  4598. GGML_ASSERT(ncols % QK4_1 == 0);
  4599. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4600. const dim3 block_nums(block_num_y, 1, 1);
  4601. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4602. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4603. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4604. }
  4605. 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) {
  4606. GGML_ASSERT(ncols % QK5_0 == 0);
  4607. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4608. const dim3 block_nums(block_num_y, 1, 1);
  4609. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4610. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4611. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4612. }
  4613. 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) {
  4614. GGML_ASSERT(ncols % QK5_1 == 0);
  4615. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4616. const dim3 block_nums(block_num_y, 1, 1);
  4617. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4618. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4619. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4620. }
  4621. 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) {
  4622. GGML_ASSERT(ncols % QK8_0 == 0);
  4623. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4624. const dim3 block_nums(block_num_y, 1, 1);
  4625. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4626. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4627. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4628. }
  4629. 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) {
  4630. GGML_ASSERT(ncols % QK_K == 0);
  4631. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4632. const dim3 block_nums(block_num_y, 1, 1);
  4633. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4634. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  4635. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4636. }
  4637. 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) {
  4638. GGML_ASSERT(ncols % QK_K == 0);
  4639. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4640. const dim3 block_nums(block_num_y, 1, 1);
  4641. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4642. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4643. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4644. }
  4645. 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) {
  4646. GGML_ASSERT(ncols % QK_K == 0);
  4647. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4648. const dim3 block_nums(block_num_y, 1, 1);
  4649. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4650. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4651. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4652. }
  4653. 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) {
  4654. GGML_ASSERT(ncols % QK_K == 0);
  4655. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4656. const dim3 block_nums(block_num_y, 1, 1);
  4657. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4658. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4659. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4660. }
  4661. 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) {
  4662. GGML_ASSERT(ncols % QK_K == 0);
  4663. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4664. const dim3 block_nums(block_num_y, 1, 1);
  4665. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4666. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4667. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4668. }
  4669. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4670. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4671. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4672. int id;
  4673. CUDA_CHECK(cudaGetDevice(&id));
  4674. const int compute_capability = g_compute_capabilities[id];
  4675. int mmq_x, mmq_y, nwarps;
  4676. if (compute_capability >= CC_RDNA2) {
  4677. mmq_x = MMQ_X_Q4_0_RDNA2;
  4678. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4679. nwarps = NWARPS_Q4_0_RDNA2;
  4680. } else if (compute_capability >= CC_OFFSET_AMD) {
  4681. mmq_x = MMQ_X_Q4_0_RDNA1;
  4682. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4683. nwarps = NWARPS_Q4_0_RDNA1;
  4684. } else if (compute_capability >= CC_VOLTA) {
  4685. mmq_x = MMQ_X_Q4_0_AMPERE;
  4686. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4687. nwarps = NWARPS_Q4_0_AMPERE;
  4688. } else if (compute_capability >= MIN_CC_DP4A) {
  4689. mmq_x = MMQ_X_Q4_0_PASCAL;
  4690. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4691. nwarps = NWARPS_Q4_0_PASCAL;
  4692. } else {
  4693. GGML_ASSERT(false);
  4694. }
  4695. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4696. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4697. const dim3 block_nums(block_num_x, block_num_y, 1);
  4698. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4699. if (nrows_x % mmq_y == 0) {
  4700. const bool need_check = false;
  4701. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4702. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4703. } else {
  4704. const bool need_check = true;
  4705. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4706. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4707. }
  4708. }
  4709. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4710. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4711. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4712. int id;
  4713. CUDA_CHECK(cudaGetDevice(&id));
  4714. const int compute_capability = g_compute_capabilities[id];
  4715. int mmq_x, mmq_y, nwarps;
  4716. if (compute_capability >= CC_RDNA2) {
  4717. mmq_x = MMQ_X_Q4_1_RDNA2;
  4718. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4719. nwarps = NWARPS_Q4_1_RDNA2;
  4720. } else if (compute_capability >= CC_OFFSET_AMD) {
  4721. mmq_x = MMQ_X_Q4_1_RDNA1;
  4722. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4723. nwarps = NWARPS_Q4_1_RDNA1;
  4724. } else if (compute_capability >= CC_VOLTA) {
  4725. mmq_x = MMQ_X_Q4_1_AMPERE;
  4726. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4727. nwarps = NWARPS_Q4_1_AMPERE;
  4728. } else if (compute_capability >= MIN_CC_DP4A) {
  4729. mmq_x = MMQ_X_Q4_1_PASCAL;
  4730. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4731. nwarps = NWARPS_Q4_1_PASCAL;
  4732. } else {
  4733. GGML_ASSERT(false);
  4734. }
  4735. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4736. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4737. const dim3 block_nums(block_num_x, block_num_y, 1);
  4738. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4739. if (nrows_x % mmq_y == 0) {
  4740. const bool need_check = false;
  4741. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4742. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4743. } else {
  4744. const bool need_check = true;
  4745. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4746. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4747. }
  4748. }
  4749. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4750. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4751. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4752. int id;
  4753. CUDA_CHECK(cudaGetDevice(&id));
  4754. const int compute_capability = g_compute_capabilities[id];
  4755. int mmq_x, mmq_y, nwarps;
  4756. if (compute_capability >= CC_RDNA2) {
  4757. mmq_x = MMQ_X_Q5_0_RDNA2;
  4758. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4759. nwarps = NWARPS_Q5_0_RDNA2;
  4760. } else if (compute_capability >= CC_OFFSET_AMD) {
  4761. mmq_x = MMQ_X_Q5_0_RDNA1;
  4762. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4763. nwarps = NWARPS_Q5_0_RDNA1;
  4764. } else if (compute_capability >= CC_VOLTA) {
  4765. mmq_x = MMQ_X_Q5_0_AMPERE;
  4766. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4767. nwarps = NWARPS_Q5_0_AMPERE;
  4768. } else if (compute_capability >= MIN_CC_DP4A) {
  4769. mmq_x = MMQ_X_Q5_0_PASCAL;
  4770. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4771. nwarps = NWARPS_Q5_0_PASCAL;
  4772. } else {
  4773. GGML_ASSERT(false);
  4774. }
  4775. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4776. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4777. const dim3 block_nums(block_num_x, block_num_y, 1);
  4778. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4779. if (nrows_x % mmq_y == 0) {
  4780. const bool need_check = false;
  4781. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4782. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4783. } else {
  4784. const bool need_check = true;
  4785. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4786. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4787. }
  4788. }
  4789. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4790. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4791. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4792. int id;
  4793. CUDA_CHECK(cudaGetDevice(&id));
  4794. const int compute_capability = g_compute_capabilities[id];
  4795. int mmq_x, mmq_y, nwarps;
  4796. if (compute_capability >= CC_RDNA2) {
  4797. mmq_x = MMQ_X_Q5_1_RDNA2;
  4798. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4799. nwarps = NWARPS_Q5_1_RDNA2;
  4800. } else if (compute_capability >= CC_OFFSET_AMD) {
  4801. mmq_x = MMQ_X_Q5_1_RDNA1;
  4802. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4803. nwarps = NWARPS_Q5_1_RDNA1;
  4804. } else if (compute_capability >= CC_VOLTA) {
  4805. mmq_x = MMQ_X_Q5_1_AMPERE;
  4806. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4807. nwarps = NWARPS_Q5_1_AMPERE;
  4808. } else if (compute_capability >= MIN_CC_DP4A) {
  4809. mmq_x = MMQ_X_Q5_1_PASCAL;
  4810. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4811. nwarps = NWARPS_Q5_1_PASCAL;
  4812. } else {
  4813. GGML_ASSERT(false);
  4814. }
  4815. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4816. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4817. const dim3 block_nums(block_num_x, block_num_y, 1);
  4818. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4819. if (nrows_x % mmq_y == 0) {
  4820. const bool need_check = false;
  4821. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4822. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4823. } else {
  4824. const bool need_check = true;
  4825. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4826. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4827. }
  4828. }
  4829. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4830. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4831. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4832. int id;
  4833. CUDA_CHECK(cudaGetDevice(&id));
  4834. const int compute_capability = g_compute_capabilities[id];
  4835. int mmq_x, mmq_y, nwarps;
  4836. if (compute_capability >= CC_RDNA2) {
  4837. mmq_x = MMQ_X_Q8_0_RDNA2;
  4838. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4839. nwarps = NWARPS_Q8_0_RDNA2;
  4840. } else if (compute_capability >= CC_OFFSET_AMD) {
  4841. mmq_x = MMQ_X_Q8_0_RDNA1;
  4842. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4843. nwarps = NWARPS_Q8_0_RDNA1;
  4844. } else if (compute_capability >= CC_VOLTA) {
  4845. mmq_x = MMQ_X_Q8_0_AMPERE;
  4846. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4847. nwarps = NWARPS_Q8_0_AMPERE;
  4848. } else if (compute_capability >= MIN_CC_DP4A) {
  4849. mmq_x = MMQ_X_Q8_0_PASCAL;
  4850. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4851. nwarps = NWARPS_Q8_0_PASCAL;
  4852. } else {
  4853. GGML_ASSERT(false);
  4854. }
  4855. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4856. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4857. const dim3 block_nums(block_num_x, block_num_y, 1);
  4858. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4859. if (nrows_x % mmq_y == 0) {
  4860. const bool need_check = false;
  4861. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4862. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4863. } else {
  4864. const bool need_check = true;
  4865. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4866. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4867. }
  4868. }
  4869. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4870. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4871. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4872. int id;
  4873. CUDA_CHECK(cudaGetDevice(&id));
  4874. const int compute_capability = g_compute_capabilities[id];
  4875. int mmq_x, mmq_y, nwarps;
  4876. if (compute_capability >= CC_RDNA2) {
  4877. mmq_x = MMQ_X_Q2_K_RDNA2;
  4878. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4879. nwarps = NWARPS_Q2_K_RDNA2;
  4880. } else if (compute_capability >= CC_OFFSET_AMD) {
  4881. mmq_x = MMQ_X_Q2_K_RDNA1;
  4882. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4883. nwarps = NWARPS_Q2_K_RDNA1;
  4884. } else if (compute_capability >= CC_VOLTA) {
  4885. mmq_x = MMQ_X_Q2_K_AMPERE;
  4886. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4887. nwarps = NWARPS_Q2_K_AMPERE;
  4888. } else if (compute_capability >= MIN_CC_DP4A) {
  4889. mmq_x = MMQ_X_Q2_K_PASCAL;
  4890. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4891. nwarps = NWARPS_Q2_K_PASCAL;
  4892. } else {
  4893. GGML_ASSERT(false);
  4894. }
  4895. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4896. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4897. const dim3 block_nums(block_num_x, block_num_y, 1);
  4898. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4899. if (nrows_x % mmq_y == 0) {
  4900. const bool need_check = false;
  4901. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4902. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4903. } else {
  4904. const bool need_check = true;
  4905. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4906. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4907. }
  4908. }
  4909. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4910. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4911. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4912. #if QK_K == 256
  4913. int id;
  4914. CUDA_CHECK(cudaGetDevice(&id));
  4915. const int compute_capability = g_compute_capabilities[id];
  4916. int mmq_x, mmq_y, nwarps;
  4917. if (compute_capability >= CC_RDNA2) {
  4918. mmq_x = MMQ_X_Q3_K_RDNA2;
  4919. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4920. nwarps = NWARPS_Q3_K_RDNA2;
  4921. } else if (compute_capability >= CC_OFFSET_AMD) {
  4922. mmq_x = MMQ_X_Q3_K_RDNA1;
  4923. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4924. nwarps = NWARPS_Q3_K_RDNA1;
  4925. } else if (compute_capability >= CC_VOLTA) {
  4926. mmq_x = MMQ_X_Q3_K_AMPERE;
  4927. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4928. nwarps = NWARPS_Q3_K_AMPERE;
  4929. } else if (compute_capability >= MIN_CC_DP4A) {
  4930. mmq_x = MMQ_X_Q3_K_PASCAL;
  4931. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4932. nwarps = NWARPS_Q3_K_PASCAL;
  4933. } else {
  4934. GGML_ASSERT(false);
  4935. }
  4936. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4937. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4938. const dim3 block_nums(block_num_x, block_num_y, 1);
  4939. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4940. if (nrows_x % mmq_y == 0) {
  4941. const bool need_check = false;
  4942. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4943. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4944. } else {
  4945. const bool need_check = true;
  4946. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4947. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4948. }
  4949. #endif
  4950. }
  4951. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4952. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4953. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4954. int id;
  4955. CUDA_CHECK(cudaGetDevice(&id));
  4956. const int compute_capability = g_compute_capabilities[id];
  4957. int mmq_x, mmq_y, nwarps;
  4958. if (compute_capability >= CC_RDNA2) {
  4959. mmq_x = MMQ_X_Q4_K_RDNA2;
  4960. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4961. nwarps = NWARPS_Q4_K_RDNA2;
  4962. } else if (compute_capability >= CC_OFFSET_AMD) {
  4963. mmq_x = MMQ_X_Q4_K_RDNA1;
  4964. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4965. nwarps = NWARPS_Q4_K_RDNA1;
  4966. } else if (compute_capability >= CC_VOLTA) {
  4967. mmq_x = MMQ_X_Q4_K_AMPERE;
  4968. mmq_y = MMQ_Y_Q4_K_AMPERE;
  4969. nwarps = NWARPS_Q4_K_AMPERE;
  4970. } else if (compute_capability >= MIN_CC_DP4A) {
  4971. mmq_x = MMQ_X_Q4_K_PASCAL;
  4972. mmq_y = MMQ_Y_Q4_K_PASCAL;
  4973. nwarps = NWARPS_Q4_K_PASCAL;
  4974. } else {
  4975. GGML_ASSERT(false);
  4976. }
  4977. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4978. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4979. const dim3 block_nums(block_num_x, block_num_y, 1);
  4980. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4981. if (nrows_x % mmq_y == 0) {
  4982. const bool need_check = false;
  4983. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4984. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4985. } else {
  4986. const bool need_check = true;
  4987. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4988. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4989. }
  4990. }
  4991. static void ggml_mul_mat_q5_K_q8_1_cuda(
  4992. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4993. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4994. int id;
  4995. CUDA_CHECK(cudaGetDevice(&id));
  4996. const int compute_capability = g_compute_capabilities[id];
  4997. int mmq_x, mmq_y, nwarps;
  4998. if (compute_capability >= CC_RDNA2) {
  4999. mmq_x = MMQ_X_Q5_K_RDNA2;
  5000. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5001. nwarps = NWARPS_Q5_K_RDNA2;
  5002. } else if (compute_capability >= CC_OFFSET_AMD) {
  5003. mmq_x = MMQ_X_Q5_K_RDNA1;
  5004. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5005. nwarps = NWARPS_Q5_K_RDNA1;
  5006. } else if (compute_capability >= CC_VOLTA) {
  5007. mmq_x = MMQ_X_Q5_K_AMPERE;
  5008. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5009. nwarps = NWARPS_Q5_K_AMPERE;
  5010. } else if (compute_capability >= MIN_CC_DP4A) {
  5011. mmq_x = MMQ_X_Q5_K_PASCAL;
  5012. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5013. nwarps = NWARPS_Q5_K_PASCAL;
  5014. } else {
  5015. GGML_ASSERT(false);
  5016. }
  5017. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5018. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5019. const dim3 block_nums(block_num_x, block_num_y, 1);
  5020. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5021. if (nrows_x % mmq_y == 0) {
  5022. const bool need_check = false;
  5023. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5024. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5025. } else {
  5026. const bool need_check = true;
  5027. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5028. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5029. }
  5030. }
  5031. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5032. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5033. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5034. int id;
  5035. CUDA_CHECK(cudaGetDevice(&id));
  5036. const int compute_capability = g_compute_capabilities[id];
  5037. int mmq_x, mmq_y, nwarps;
  5038. if (compute_capability >= CC_RDNA2) {
  5039. mmq_x = MMQ_X_Q6_K_RDNA2;
  5040. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5041. nwarps = NWARPS_Q6_K_RDNA2;
  5042. } else if (compute_capability >= CC_OFFSET_AMD) {
  5043. mmq_x = MMQ_X_Q6_K_RDNA1;
  5044. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5045. nwarps = NWARPS_Q6_K_RDNA1;
  5046. } else if (compute_capability >= CC_VOLTA) {
  5047. mmq_x = MMQ_X_Q6_K_AMPERE;
  5048. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5049. nwarps = NWARPS_Q6_K_AMPERE;
  5050. } else if (compute_capability >= MIN_CC_DP4A) {
  5051. mmq_x = MMQ_X_Q6_K_PASCAL;
  5052. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5053. nwarps = NWARPS_Q6_K_PASCAL;
  5054. } else {
  5055. GGML_ASSERT(false);
  5056. }
  5057. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5058. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5059. const dim3 block_nums(block_num_x, block_num_y, 1);
  5060. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5061. if (nrows_x % mmq_y == 0) {
  5062. const bool need_check = false;
  5063. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5064. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5065. } else {
  5066. const bool need_check = true;
  5067. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5068. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5069. }
  5070. }
  5071. static void ggml_mul_mat_p021_f16_f32_cuda(
  5072. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5073. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5074. const dim3 block_nums(1, nrows_x, nchannels_y);
  5075. const dim3 block_dims(WARP_SIZE, 1, 1);
  5076. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5077. }
  5078. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5079. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5080. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5081. const dim3 block_nums(1, nrows_x, nchannels_y);
  5082. const dim3 block_dims(WARP_SIZE, 1, 1);
  5083. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5084. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5085. }
  5086. static void ggml_cpy_f32_f32_cuda(
  5087. const char * cx, char * cdst, const int ne,
  5088. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5089. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5090. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5091. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5092. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5093. }
  5094. static void ggml_cpy_f32_f16_cuda(
  5095. const char * cx, char * cdst, const int ne,
  5096. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5097. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5098. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5099. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5100. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5101. }
  5102. static void ggml_cpy_f32_q8_0_cuda(
  5103. const char * cx, char * cdst, const int ne,
  5104. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5105. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5106. GGML_ASSERT(ne % QK8_0 == 0);
  5107. const int num_blocks = ne / QK8_0;
  5108. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5109. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5110. }
  5111. static void ggml_cpy_f32_q4_0_cuda(
  5112. const char * cx, char * cdst, const int ne,
  5113. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5114. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5115. GGML_ASSERT(ne % QK4_0 == 0);
  5116. const int num_blocks = ne / QK4_0;
  5117. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5118. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5119. }
  5120. static void ggml_cpy_f32_q4_1_cuda(
  5121. const char * cx, char * cdst, const int ne,
  5122. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5123. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5124. GGML_ASSERT(ne % QK4_1 == 0);
  5125. const int num_blocks = ne / QK4_1;
  5126. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5127. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5128. }
  5129. static void ggml_cpy_f16_f16_cuda(
  5130. const char * cx, char * cdst, const int ne,
  5131. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5132. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5133. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5134. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5135. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5136. }
  5137. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5138. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5139. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5140. }
  5141. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5142. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5143. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5144. }
  5145. template<typename T>
  5146. static void rope_cuda(
  5147. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5148. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5149. ) {
  5150. GGML_ASSERT(ncols % 2 == 0);
  5151. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5152. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5153. const dim3 block_nums(nrows, num_blocks_x, 1);
  5154. if (pos == nullptr) {
  5155. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5156. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5157. );
  5158. } else {
  5159. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5160. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5161. );
  5162. }
  5163. }
  5164. template<typename T>
  5165. static void rope_neox_cuda(
  5166. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5167. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5168. ) {
  5169. GGML_ASSERT(ncols % 2 == 0);
  5170. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5171. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5172. const dim3 block_nums(nrows, num_blocks_x, 1);
  5173. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5174. const float inv_ndims = -1.0f / n_dims;
  5175. if (pos == nullptr) {
  5176. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5177. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5178. theta_scale, inv_ndims
  5179. );
  5180. } else {
  5181. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5182. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5183. theta_scale, inv_ndims
  5184. );
  5185. }
  5186. }
  5187. static void rope_glm_f32_cuda(
  5188. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5189. float freq_base, int n_ctx, cudaStream_t stream
  5190. ) {
  5191. GGML_ASSERT(ncols % 4 == 0);
  5192. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5193. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5194. const dim3 block_nums(num_blocks_x, nrows, 1);
  5195. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5196. }
  5197. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5198. const int k_rows, const int n_heads_log2_floor, const float m0,
  5199. const float m1, cudaStream_t stream) {
  5200. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5201. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5202. const dim3 block_nums(num_blocks_x, nrows, 1);
  5203. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5204. }
  5205. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5206. const dim3 block_dims(WARP_SIZE, 1, 1);
  5207. const dim3 block_nums(1, nrows, 1);
  5208. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5209. }
  5210. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5211. // bitonic sort requires ncols to be power of 2
  5212. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5213. const dim3 block_dims(ncols, 1, 1);
  5214. const dim3 block_nums(1, nrows, 1);
  5215. if (order == GGML_SORT_ASC) {
  5216. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5217. } else if (order == GGML_SORT_DESC) {
  5218. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5219. } else {
  5220. GGML_ASSERT(false);
  5221. }
  5222. }
  5223. 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) {
  5224. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5225. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5226. const dim3 block_nums(nrows_x, block_num_x, 1);
  5227. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5228. }
  5229. static void soft_max_f32_cuda(const float * x, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, cudaStream_t stream) {
  5230. int nth = WARP_SIZE;
  5231. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5232. const dim3 block_dims(nth, 1, 1);
  5233. const dim3 block_nums(nrows_x, 1, 1);
  5234. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5235. }
  5236. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5237. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5238. int offset_delta,
  5239. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5240. const int parallel_elements = OW * KW * KH;
  5241. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5242. dim3 block_nums(num_blocks, OH, IC);
  5243. im2col_f32_f16<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, offset_delta, IW, IH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  5244. }
  5245. // buffer pool for cuda
  5246. #define MAX_CUDA_BUFFERS 256
  5247. struct scoped_spin_lock {
  5248. std::atomic_flag& lock;
  5249. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5250. while (lock.test_and_set(std::memory_order_acquire)) {
  5251. ; // spin
  5252. }
  5253. }
  5254. ~scoped_spin_lock() {
  5255. lock.clear(std::memory_order_release);
  5256. }
  5257. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5258. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5259. };
  5260. struct cuda_buffer {
  5261. void * ptr = nullptr;
  5262. size_t size = 0;
  5263. };
  5264. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5265. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5266. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  5267. scoped_spin_lock lock(g_cuda_pool_lock);
  5268. int id;
  5269. CUDA_CHECK(cudaGetDevice(&id));
  5270. #ifdef DEBUG_CUDA_MALLOC
  5271. int nnz = 0;
  5272. size_t max_size = 0, tot_size = 0;
  5273. #endif
  5274. size_t best_diff = 1ull << 36;
  5275. int ibest = -1;
  5276. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5277. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5278. if (b.ptr != nullptr) {
  5279. #ifdef DEBUG_CUDA_MALLOC
  5280. ++nnz;
  5281. tot_size += b.size;
  5282. if (b.size > max_size) max_size = b.size;
  5283. #endif
  5284. if (b.size >= size) {
  5285. size_t diff = b.size - size;
  5286. if (diff < best_diff) {
  5287. best_diff = diff;
  5288. ibest = i;
  5289. if (!best_diff) {
  5290. void * ptr = b.ptr;
  5291. *actual_size = b.size;
  5292. b.ptr = nullptr;
  5293. b.size = 0;
  5294. return ptr;
  5295. }
  5296. }
  5297. }
  5298. }
  5299. }
  5300. if (ibest >= 0) {
  5301. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  5302. void * ptr = b.ptr;
  5303. *actual_size = b.size;
  5304. b.ptr = nullptr;
  5305. b.size = 0;
  5306. return ptr;
  5307. }
  5308. #ifdef DEBUG_CUDA_MALLOC
  5309. fprintf(stderr, "%s: %d buffers, max_size = %u MB, tot_size = %u MB, requested %u MB\n", __func__, nnz,
  5310. (uint32_t)(max_size/1024/1024), (uint32_t)(tot_size/1024/1024), (uint32_t)(size/1024/1024));
  5311. #endif
  5312. void * ptr;
  5313. size_t look_ahead_size = (size_t) (1.05 * size);
  5314. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5315. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5316. *actual_size = look_ahead_size;
  5317. return ptr;
  5318. }
  5319. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  5320. scoped_spin_lock lock(g_cuda_pool_lock);
  5321. int id;
  5322. CUDA_CHECK(cudaGetDevice(&id));
  5323. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5324. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5325. if (b.ptr == nullptr) {
  5326. b.ptr = ptr;
  5327. b.size = size;
  5328. return;
  5329. }
  5330. }
  5331. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5332. CUDA_CHECK(cudaFree(ptr));
  5333. }
  5334. static bool g_cublas_loaded = false;
  5335. bool ggml_cublas_loaded(void) {
  5336. return g_cublas_loaded;
  5337. }
  5338. void ggml_init_cublas() {
  5339. static bool initialized = false;
  5340. if (!initialized) {
  5341. #ifdef __HIP_PLATFORM_AMD__
  5342. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5343. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5344. rocblas_initialize();
  5345. CUDA_CHECK(cudaDeviceSynchronize());
  5346. #endif
  5347. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5348. initialized = true;
  5349. g_cublas_loaded = false;
  5350. return;
  5351. }
  5352. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5353. int64_t total_vram = 0;
  5354. #if defined(GGML_CUDA_FORCE_MMQ)
  5355. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5356. #else
  5357. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5358. #endif
  5359. #if defined(CUDA_USE_TENSOR_CORES)
  5360. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5361. #else
  5362. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5363. #endif
  5364. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5365. for (int id = 0; id < g_device_count; ++id) {
  5366. cudaDeviceProp prop;
  5367. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5368. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  5369. g_tensor_split[id] = total_vram;
  5370. total_vram += prop.totalGlobalMem;
  5371. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5372. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5373. #else
  5374. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  5375. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5376. }
  5377. for (int id = 0; id < g_device_count; ++id) {
  5378. g_tensor_split[id] /= total_vram;
  5379. }
  5380. for (int id = 0; id < g_device_count; ++id) {
  5381. CUDA_CHECK(ggml_cuda_set_device(id));
  5382. // create cuda streams
  5383. for (int is = 0; is < MAX_STREAMS; ++is) {
  5384. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5385. }
  5386. // create cublas handle
  5387. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5388. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5389. }
  5390. // configure logging to stdout
  5391. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5392. initialized = true;
  5393. g_cublas_loaded = true;
  5394. }
  5395. }
  5396. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5397. if (tensor_split == nullptr) {
  5398. return;
  5399. }
  5400. bool all_zero = true;
  5401. for (int i = 0; i < g_device_count; ++i) {
  5402. if (tensor_split[i] != 0.0f) {
  5403. all_zero = false;
  5404. break;
  5405. }
  5406. }
  5407. if (all_zero) {
  5408. return;
  5409. }
  5410. float split_sum = 0.0f;
  5411. for (int i = 0; i < g_device_count; ++i) {
  5412. g_tensor_split[i] = split_sum;
  5413. split_sum += tensor_split[i];
  5414. }
  5415. for (int i = 0; i < g_device_count; ++i) {
  5416. g_tensor_split[i] /= split_sum;
  5417. }
  5418. }
  5419. void * ggml_cuda_host_malloc(size_t size) {
  5420. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  5421. return nullptr;
  5422. }
  5423. void * ptr = nullptr;
  5424. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  5425. if (err != cudaSuccess) {
  5426. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  5427. // This can fixed the OOM error in WSL.
  5428. cudaGetLastError();
  5429. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  5430. size/1024.0/1024.0, cudaGetErrorString(err));
  5431. return nullptr;
  5432. }
  5433. return ptr;
  5434. }
  5435. void ggml_cuda_host_free(void * ptr) {
  5436. CUDA_CHECK(cudaFreeHost(ptr));
  5437. }
  5438. static cudaError_t ggml_cuda_cpy_tensor_2d(
  5439. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  5440. cudaMemcpyKind kind;
  5441. char * src_ptr;
  5442. if (src->backend == GGML_BACKEND_CPU) {
  5443. kind = cudaMemcpyHostToDevice;
  5444. src_ptr = (char *) src->data;
  5445. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  5446. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  5447. kind = cudaMemcpyDeviceToDevice;
  5448. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  5449. int id;
  5450. CUDA_CHECK(cudaGetDevice(&id));
  5451. src_ptr = (char *) extra->data_device[id];
  5452. } else {
  5453. GGML_ASSERT(false);
  5454. }
  5455. char * dst_ptr = (char *) dst;
  5456. const int64_t ne0 = src->ne[0];
  5457. const int64_t nb0 = src->nb[0];
  5458. const int64_t nb1 = src->nb[1];
  5459. const int64_t nb2 = src->nb[2];
  5460. const int64_t nb3 = src->nb[3];
  5461. const enum ggml_type type = src->type;
  5462. const int64_t ts = ggml_type_size(type);
  5463. const int64_t bs = ggml_blck_size(type);
  5464. int64_t i1_diff = i1_high - i1_low;
  5465. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  5466. if (nb0 == ts && nb1 == ts*ne0/bs) {
  5467. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  5468. } else if (nb0 == ts) {
  5469. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  5470. } else {
  5471. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  5472. const void * rx = (const void *) ((const char *) x + i1*nb1);
  5473. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  5474. // pretend the row is a matrix with cols=1
  5475. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  5476. if (r != cudaSuccess) return r;
  5477. }
  5478. return cudaSuccess;
  5479. }
  5480. }
  5481. static void ggml_cuda_op_get_rows(
  5482. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5483. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & stream) {
  5484. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5485. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5486. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  5487. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  5488. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  5489. const int32_t * src1_i32 = (const int32_t *) src1_d;
  5490. switch (src0->type) {
  5491. case GGML_TYPE_F16:
  5492. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  5493. break;
  5494. case GGML_TYPE_F32:
  5495. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5496. break;
  5497. case GGML_TYPE_Q4_0:
  5498. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5499. break;
  5500. case GGML_TYPE_Q4_1:
  5501. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5502. break;
  5503. case GGML_TYPE_Q5_0:
  5504. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5505. break;
  5506. case GGML_TYPE_Q5_1:
  5507. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5508. break;
  5509. case GGML_TYPE_Q8_0:
  5510. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5511. break;
  5512. default:
  5513. // TODO: k-quants
  5514. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  5515. GGML_ASSERT(false);
  5516. break;
  5517. }
  5518. }
  5519. template<class op>
  5520. inline void ggml_cuda_op_bin_bcast(
  5521. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5522. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5523. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5524. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5525. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5526. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5527. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  5528. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5529. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  5530. } else {
  5531. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  5532. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  5533. GGML_ASSERT(false);
  5534. }
  5535. }
  5536. static void ggml_cuda_op_repeat(
  5537. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5538. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & main_stream) {
  5539. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  5540. (void) src1;
  5541. (void) src1_d;
  5542. }
  5543. inline void ggml_cuda_op_add(
  5544. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5545. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5546. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5547. }
  5548. inline void ggml_cuda_op_acc(
  5549. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5550. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5551. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5552. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5553. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5554. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  5555. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5556. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5557. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5558. int offset = dst->op_params[3] / 4; // offset in bytes
  5559. acc_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  5560. (void) dst;
  5561. }
  5562. inline void ggml_cuda_op_mul(
  5563. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5564. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5565. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5566. }
  5567. inline void ggml_cuda_op_div(
  5568. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5569. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5570. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5571. }
  5572. inline void ggml_cuda_op_gelu(
  5573. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5574. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5575. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5576. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5577. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5578. (void) src1;
  5579. (void) dst;
  5580. (void) src1_dd;
  5581. }
  5582. inline void ggml_cuda_op_silu(
  5583. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5584. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5585. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5586. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5587. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5588. (void) src1;
  5589. (void) dst;
  5590. (void) src1_dd;
  5591. }
  5592. inline void ggml_cuda_op_gelu_quick(
  5593. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5594. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5595. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5596. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5597. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5598. (void) src1;
  5599. (void) dst;
  5600. (void) src1_dd;
  5601. }
  5602. inline void ggml_cuda_op_tanh(
  5603. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5604. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5605. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5606. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5607. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5608. (void) src1;
  5609. (void) dst;
  5610. (void) src1_dd;
  5611. }
  5612. inline void ggml_cuda_op_relu(
  5613. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5614. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5615. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5616. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5617. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5618. (void) src1;
  5619. (void) dst;
  5620. (void) src1_dd;
  5621. }
  5622. inline void ggml_cuda_op_leaky_relu(
  5623. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5624. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5625. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5626. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5627. float negative_slope;
  5628. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5629. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  5630. (void) src1;
  5631. (void) dst;
  5632. (void) src1_dd;
  5633. }
  5634. inline void ggml_cuda_op_sqr(
  5635. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5636. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5637. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5638. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5639. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5640. (void) src1;
  5641. (void) dst;
  5642. (void) src1_dd;
  5643. }
  5644. inline void ggml_cuda_op_norm(
  5645. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5646. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5647. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5648. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5649. const int64_t ne00 = src0->ne[0];
  5650. const int64_t nrows = ggml_nrows(src0);
  5651. float eps;
  5652. memcpy(&eps, dst->op_params, sizeof(float));
  5653. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5654. (void) src1;
  5655. (void) dst;
  5656. (void) src1_dd;
  5657. }
  5658. inline void ggml_cuda_op_group_norm(
  5659. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5660. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5661. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5662. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5663. int num_groups = dst->op_params[0];
  5664. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5665. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  5666. (void) src1;
  5667. (void) dst;
  5668. (void) src1_dd;
  5669. }
  5670. inline void ggml_cuda_op_concat(
  5671. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5672. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5673. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5674. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5675. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5676. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  5677. concat_f32_cuda(src0_dd + i3 * (src0->nb[3] / 4), src1_dd + i3 * (src1->nb[3] / 4), dst_dd + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], main_stream);
  5678. }
  5679. (void) src1;
  5680. (void) dst;
  5681. }
  5682. inline void ggml_cuda_op_upscale(
  5683. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5684. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5685. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5686. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5687. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5688. const int scale_factor = dst->op_params[0];
  5689. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  5690. (void) src1;
  5691. (void) dst;
  5692. (void) src1_dd;
  5693. }
  5694. inline void ggml_cuda_op_pad(
  5695. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5696. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5697. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5698. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5699. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5700. pad_f32_cuda(src0_dd, dst_dd,
  5701. src0->ne[0], src0->ne[1], src0->ne[2],
  5702. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  5703. (void) src1;
  5704. (void) dst;
  5705. (void) src1_dd;
  5706. }
  5707. inline void ggml_cuda_op_rms_norm(
  5708. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5709. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5710. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5711. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5712. const int64_t ne00 = src0->ne[0];
  5713. const int64_t nrows = ggml_nrows(src0);
  5714. float eps;
  5715. memcpy(&eps, dst->op_params, sizeof(float));
  5716. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5717. (void) src1;
  5718. (void) dst;
  5719. (void) src1_dd;
  5720. }
  5721. inline void ggml_cuda_op_mul_mat_q(
  5722. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5723. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5724. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5725. const int64_t ne00 = src0->ne[0];
  5726. const int64_t ne10 = src1->ne[0];
  5727. GGML_ASSERT(ne10 % QK8_1 == 0);
  5728. const int64_t ne0 = dst->ne[0];
  5729. const int64_t row_diff = row_high - row_low;
  5730. int id;
  5731. CUDA_CHECK(cudaGetDevice(&id));
  5732. // the main device has a larger memory buffer to hold the results from all GPUs
  5733. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5734. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5735. switch (src0->type) {
  5736. case GGML_TYPE_Q4_0:
  5737. 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);
  5738. break;
  5739. case GGML_TYPE_Q4_1:
  5740. 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);
  5741. break;
  5742. case GGML_TYPE_Q5_0:
  5743. 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);
  5744. break;
  5745. case GGML_TYPE_Q5_1:
  5746. 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);
  5747. break;
  5748. case GGML_TYPE_Q8_0:
  5749. 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);
  5750. break;
  5751. case GGML_TYPE_Q2_K:
  5752. 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);
  5753. break;
  5754. case GGML_TYPE_Q3_K:
  5755. 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);
  5756. break;
  5757. case GGML_TYPE_Q4_K:
  5758. 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);
  5759. break;
  5760. case GGML_TYPE_Q5_K:
  5761. 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);
  5762. break;
  5763. case GGML_TYPE_Q6_K:
  5764. 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);
  5765. break;
  5766. default:
  5767. GGML_ASSERT(false);
  5768. break;
  5769. }
  5770. (void) src1;
  5771. (void) dst;
  5772. (void) src1_ddf_i;
  5773. }
  5774. static int64_t get_row_rounding(ggml_type type) {
  5775. int64_t min_compute_capability = INT_MAX;
  5776. int64_t max_compute_capability = INT_MIN;
  5777. for (int64_t id = 0; id < g_device_count; ++id) {
  5778. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5779. if (min_compute_capability > g_compute_capabilities[id]) {
  5780. min_compute_capability = g_compute_capabilities[id];
  5781. }
  5782. if (max_compute_capability < g_compute_capabilities[id]) {
  5783. max_compute_capability = g_compute_capabilities[id];
  5784. }
  5785. }
  5786. }
  5787. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5788. switch(type) {
  5789. case GGML_TYPE_Q4_0:
  5790. case GGML_TYPE_Q4_1:
  5791. case GGML_TYPE_Q5_0:
  5792. case GGML_TYPE_Q5_1:
  5793. case GGML_TYPE_Q8_0:
  5794. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5795. case GGML_TYPE_F16:
  5796. case GGML_TYPE_F32:
  5797. return 1;
  5798. case GGML_TYPE_Q2_K:
  5799. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5800. case GGML_TYPE_Q3_K:
  5801. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5802. case GGML_TYPE_Q4_K:
  5803. case GGML_TYPE_Q5_K:
  5804. case GGML_TYPE_Q6_K:
  5805. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5806. default:
  5807. GGML_ASSERT(false);
  5808. }
  5809. #else
  5810. switch(type) {
  5811. case GGML_TYPE_Q4_0:
  5812. case GGML_TYPE_Q4_1:
  5813. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5814. case GGML_TYPE_Q5_0:
  5815. case GGML_TYPE_Q5_1:
  5816. case GGML_TYPE_Q8_0:
  5817. return 64;
  5818. case GGML_TYPE_F16:
  5819. case GGML_TYPE_F32:
  5820. return 1;
  5821. case GGML_TYPE_Q2_K:
  5822. case GGML_TYPE_Q3_K:
  5823. case GGML_TYPE_Q4_K:
  5824. case GGML_TYPE_Q5_K:
  5825. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5826. case GGML_TYPE_Q6_K:
  5827. return 64;
  5828. default:
  5829. GGML_ASSERT(false);
  5830. }
  5831. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5832. }
  5833. inline void ggml_cuda_op_mul_mat_vec_q(
  5834. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5835. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5836. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5837. GGML_ASSERT(ggml_nrows(src1) == 1);
  5838. const int64_t ne00 = src0->ne[0];
  5839. const int64_t row_diff = row_high - row_low;
  5840. switch (src0->type) {
  5841. case GGML_TYPE_Q4_0:
  5842. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5843. break;
  5844. case GGML_TYPE_Q4_1:
  5845. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5846. break;
  5847. case GGML_TYPE_Q5_0:
  5848. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5849. break;
  5850. case GGML_TYPE_Q5_1:
  5851. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5852. break;
  5853. case GGML_TYPE_Q8_0:
  5854. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5855. break;
  5856. case GGML_TYPE_Q2_K:
  5857. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5858. break;
  5859. case GGML_TYPE_Q3_K:
  5860. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5861. break;
  5862. case GGML_TYPE_Q4_K:
  5863. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5864. break;
  5865. case GGML_TYPE_Q5_K:
  5866. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5867. break;
  5868. case GGML_TYPE_Q6_K:
  5869. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5870. break;
  5871. default:
  5872. GGML_ASSERT(false);
  5873. break;
  5874. }
  5875. (void) src1;
  5876. (void) dst;
  5877. (void) src1_ddf_i;
  5878. (void) src1_ncols;
  5879. (void) src1_padded_row_size;
  5880. }
  5881. inline void ggml_cuda_op_dequantize_mul_mat_vec(
  5882. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5883. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5884. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5885. const int64_t ne00 = src0->ne[0];
  5886. const int64_t row_diff = row_high - row_low;
  5887. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  5888. #ifdef GGML_CUDA_F16
  5889. size_t ash;
  5890. dfloat * src1_dfloat = nullptr; // dfloat == half
  5891. bool src1_convert_f16 =
  5892. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  5893. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  5894. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  5895. if (src1_convert_f16) {
  5896. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  5897. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  5898. ne00, 1, sizeof(float), 0, 0,
  5899. ne00, 1, sizeof(half), 0, 0, stream);
  5900. }
  5901. #else
  5902. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  5903. #endif // GGML_CUDA_F16
  5904. switch (src0->type) {
  5905. case GGML_TYPE_Q4_0:
  5906. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5907. break;
  5908. case GGML_TYPE_Q4_1:
  5909. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5910. break;
  5911. case GGML_TYPE_Q5_0:
  5912. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5913. break;
  5914. case GGML_TYPE_Q5_1:
  5915. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5916. break;
  5917. case GGML_TYPE_Q8_0:
  5918. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5919. break;
  5920. case GGML_TYPE_Q2_K:
  5921. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5922. break;
  5923. case GGML_TYPE_Q3_K:
  5924. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5925. break;
  5926. case GGML_TYPE_Q4_K:
  5927. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5928. break;
  5929. case GGML_TYPE_Q5_K:
  5930. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5931. break;
  5932. case GGML_TYPE_Q6_K:
  5933. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5934. break;
  5935. case GGML_TYPE_F16:
  5936. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5937. break;
  5938. default:
  5939. GGML_ASSERT(false);
  5940. break;
  5941. }
  5942. #ifdef GGML_CUDA_F16
  5943. if (src1_convert_f16) {
  5944. ggml_cuda_pool_free(src1_dfloat, ash);
  5945. }
  5946. #endif // GGML_CUDA_F16
  5947. (void) src1;
  5948. (void) dst;
  5949. (void) src1_ddq_i;
  5950. (void) src1_ncols;
  5951. (void) src1_padded_row_size;
  5952. }
  5953. inline void ggml_cuda_op_mul_mat_cublas(
  5954. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5955. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5956. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5957. GGML_ASSERT(src0_dd_i != nullptr);
  5958. GGML_ASSERT(src1_ddf_i != nullptr);
  5959. GGML_ASSERT(dst_dd_i != nullptr);
  5960. const int64_t ne00 = src0->ne[0];
  5961. const int64_t ne10 = src1->ne[0];
  5962. const int64_t ne0 = dst->ne[0];
  5963. const int64_t row_diff = row_high - row_low;
  5964. int id;
  5965. CUDA_CHECK(cudaGetDevice(&id));
  5966. // the main device has a larger memory buffer to hold the results from all GPUs
  5967. // ldc == nrows of the matrix that cuBLAS writes into
  5968. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5969. const int compute_capability = g_compute_capabilities[id];
  5970. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  5971. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  5972. half * src0_as_f16 = nullptr;
  5973. size_t src0_as = 0;
  5974. if (src0->type != GGML_TYPE_F16) {
  5975. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  5976. GGML_ASSERT(to_fp16_cuda != nullptr);
  5977. size_t ne = row_diff*ne00;
  5978. src0_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src0_as);
  5979. to_fp16_cuda(src0_dd_i, src0_as_f16, ne, stream);
  5980. }
  5981. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16;
  5982. half * src1_as_f16 = nullptr;
  5983. size_t src1_as = 0;
  5984. if (src1->type != GGML_TYPE_F16) {
  5985. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  5986. GGML_ASSERT(to_fp16_cuda != nullptr);
  5987. size_t ne = src1_ncols*ne10;
  5988. src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src1_as);
  5989. to_fp16_cuda(src1_ddf_i, src1_as_f16, ne, stream);
  5990. }
  5991. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16;
  5992. size_t dst_as = 0;
  5993. half * dst_f16 = (half *) ggml_cuda_pool_malloc(row_diff*src1_ncols * sizeof(half), &dst_as);
  5994. const half alpha_f16 = 1.0f;
  5995. const half beta_f16 = 0.0f;
  5996. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  5997. CUBLAS_CHECK(
  5998. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  5999. row_diff, src1_ncols, ne10,
  6000. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6001. src1_ptr, CUDA_R_16F, ne10,
  6002. &beta_f16, dst_f16, CUDA_R_16F, ldc,
  6003. CUBLAS_COMPUTE_16F,
  6004. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6005. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6006. to_fp32_cuda(dst_f16, dst_dd_i, row_diff*src1_ncols, stream);
  6007. ggml_cuda_pool_free(dst_f16, dst_as);
  6008. if (src0_as != 0) {
  6009. ggml_cuda_pool_free(src0_as_f16, src0_as);
  6010. }
  6011. if (src1_as != 0) {
  6012. ggml_cuda_pool_free(src1_as_f16, src1_as);
  6013. }
  6014. }
  6015. else {
  6016. float * src0_ddq_as_f32 = nullptr;
  6017. size_t src0_as = 0;
  6018. if (src0->type != GGML_TYPE_F32) {
  6019. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6020. GGML_ASSERT(to_fp32_cuda != nullptr);
  6021. src0_ddq_as_f32 = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_as); // NOLINT
  6022. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32, row_diff*ne00, stream);
  6023. }
  6024. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32;
  6025. const float alpha = 1.0f;
  6026. const float beta = 0.0f;
  6027. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6028. CUBLAS_CHECK(
  6029. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6030. row_diff, src1_ncols, ne10,
  6031. &alpha, src0_ddf_i, ne00,
  6032. src1_ddf_i, ne10,
  6033. &beta, dst_dd_i, ldc));
  6034. if (src0_as != 0) {
  6035. ggml_cuda_pool_free(src0_ddq_as_f32, src0_as);
  6036. }
  6037. }
  6038. (void) dst;
  6039. (void) src1_ddq_i;
  6040. (void) src1_padded_row_size;
  6041. }
  6042. inline void ggml_cuda_op_rope(
  6043. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6044. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6045. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6046. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6047. GGML_ASSERT(src0->type == dst->type);
  6048. const int64_t ne00 = src0->ne[0];
  6049. const int64_t ne01 = src0->ne[1];
  6050. const int64_t ne2 = dst->ne[2];
  6051. const int64_t nrows = ggml_nrows(src0);
  6052. //const int n_past = ((int32_t *) dst->op_params)[0];
  6053. const int n_dims = ((int32_t *) dst->op_params)[1];
  6054. const int mode = ((int32_t *) dst->op_params)[2];
  6055. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6056. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6057. // RoPE alteration for extended context
  6058. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6059. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6060. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6061. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6062. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6063. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6064. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6065. const int32_t * pos = nullptr;
  6066. if ((mode & 1) == 0) {
  6067. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6068. GGML_ASSERT(src1->ne[0] == ne2);
  6069. pos = (const int32_t *) src1_dd;
  6070. }
  6071. const bool is_neox = mode & 2;
  6072. const bool is_glm = mode & 4;
  6073. rope_corr_dims corr_dims;
  6074. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6075. // compute
  6076. if (is_glm) {
  6077. GGML_ASSERT(false);
  6078. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6079. } else if (is_neox) {
  6080. if (src0->type == GGML_TYPE_F32) {
  6081. rope_neox_cuda(
  6082. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6083. attn_factor, corr_dims, main_stream
  6084. );
  6085. } else if (src0->type == GGML_TYPE_F16) {
  6086. rope_neox_cuda(
  6087. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6088. attn_factor, corr_dims, main_stream
  6089. );
  6090. } else {
  6091. GGML_ASSERT(false);
  6092. }
  6093. } else {
  6094. if (src0->type == GGML_TYPE_F32) {
  6095. rope_cuda(
  6096. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6097. attn_factor, corr_dims, main_stream
  6098. );
  6099. } else if (src0->type == GGML_TYPE_F16) {
  6100. rope_cuda(
  6101. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6102. attn_factor, corr_dims, main_stream
  6103. );
  6104. } else {
  6105. GGML_ASSERT(false);
  6106. }
  6107. }
  6108. (void) src1;
  6109. (void) dst;
  6110. (void) src1_dd;
  6111. }
  6112. inline void ggml_cuda_op_alibi(
  6113. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6114. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6115. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6116. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6117. const int64_t ne00 = src0->ne[0];
  6118. const int64_t ne01 = src0->ne[1];
  6119. const int64_t ne02 = src0->ne[2];
  6120. const int64_t nrows = ggml_nrows(src0);
  6121. //const int n_past = ((int32_t *) dst->op_params)[0];
  6122. const int n_head = ((int32_t *) dst->op_params)[1];
  6123. float max_bias;
  6124. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6125. //GGML_ASSERT(ne01 + n_past == ne00);
  6126. GGML_ASSERT(n_head == ne02);
  6127. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6128. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6129. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6130. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6131. (void) src1;
  6132. (void) src1_dd;
  6133. }
  6134. inline void ggml_cuda_op_im2col(
  6135. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6136. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6137. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6138. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6139. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6140. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6141. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6142. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6143. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6144. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6145. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6146. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6147. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6148. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6149. const int64_t IW = src1->ne[0];
  6150. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6151. const int64_t KW = src0->ne[0];
  6152. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6153. const int64_t OW = dst->ne[1];
  6154. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6155. im2col_f32_f16_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  6156. (void) src0;
  6157. (void) src0_dd;
  6158. }
  6159. inline void ggml_cuda_op_sum_rows(
  6160. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6161. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6162. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6163. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6164. const int64_t ncols = src0->ne[0];
  6165. const int64_t nrows = ggml_nrows(src0);
  6166. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6167. (void) src1;
  6168. (void) dst;
  6169. (void) src1_dd;
  6170. }
  6171. inline void ggml_cuda_op_argsort(
  6172. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6173. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6174. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6175. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6176. const int64_t ncols = src0->ne[0];
  6177. const int64_t nrows = ggml_nrows(src0);
  6178. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6179. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6180. (void) src1;
  6181. (void) dst;
  6182. (void) src1_dd;
  6183. }
  6184. inline void ggml_cuda_op_diag_mask_inf(
  6185. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6186. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6187. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6188. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6189. const int64_t ne00 = src0->ne[0];
  6190. const int64_t ne01 = src0->ne[1];
  6191. const int nrows0 = ggml_nrows(src0);
  6192. const int n_past = ((int32_t *) dst->op_params)[0];
  6193. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6194. (void) src1;
  6195. (void) dst;
  6196. (void) src1_dd;
  6197. }
  6198. inline void ggml_cuda_op_soft_max(
  6199. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6200. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6201. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6202. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6203. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6204. const int64_t ne00 = src0->ne[0];
  6205. const int64_t nrows_x = ggml_nrows(src0);
  6206. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6207. float scale = 1.0f;
  6208. memcpy(&scale, dst->op_params, sizeof(float));
  6209. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6210. (void) dst;
  6211. }
  6212. inline void ggml_cuda_op_scale(
  6213. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6214. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6215. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6216. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6217. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6218. float scale;
  6219. // HACK: support for ggml backend interface
  6220. if (src1->backend == GGML_BACKEND_CPU) {
  6221. scale = ((float *) src1->data)[0];
  6222. } else {
  6223. // TODO: pass pointer to kernel instead of copying to host
  6224. CUDA_CHECK(cudaMemcpy(&scale, src1->data, sizeof(float), cudaMemcpyDeviceToHost));
  6225. }
  6226. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6227. CUDA_CHECK(cudaGetLastError());
  6228. (void) src1;
  6229. (void) dst;
  6230. (void) src1_dd;
  6231. }
  6232. inline void ggml_cuda_op_clamp(
  6233. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6234. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6235. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6236. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6237. float min;
  6238. float max;
  6239. memcpy(&min, dst->op_params, sizeof(float));
  6240. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6241. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6242. CUDA_CHECK(cudaGetLastError());
  6243. (void) src1;
  6244. (void) dst;
  6245. (void) src1_dd;
  6246. }
  6247. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6248. const int64_t nrows0 = ggml_nrows(src0);
  6249. const bool use_src1 = src1 != nullptr;
  6250. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6251. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6252. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6253. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6254. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6255. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6256. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6257. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6258. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6259. const bool src1_stays_on_host = use_src1 && dst->op == GGML_OP_SCALE;
  6260. // dd = data device
  6261. float * src0_ddf = nullptr;
  6262. float * src1_ddf = nullptr;
  6263. float * dst_ddf = nullptr;
  6264. // as = actual size
  6265. size_t src0_asf = 0;
  6266. size_t src1_asf = 0;
  6267. size_t dst_asf = 0;
  6268. ggml_cuda_set_device(g_main_device);
  6269. const cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6270. if (src0_on_device) {
  6271. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6272. } else {
  6273. src0_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_asf);
  6274. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6275. }
  6276. if (use_src1 && !src1_stays_on_host) {
  6277. if (src1_on_device) {
  6278. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6279. } else {
  6280. src1_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf);
  6281. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6282. }
  6283. }
  6284. if (dst_on_device) {
  6285. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6286. } else {
  6287. dst_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(dst), &dst_asf);
  6288. }
  6289. // do the computation
  6290. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6291. CUDA_CHECK(cudaGetLastError());
  6292. // copy dst to host if necessary
  6293. if (!dst_on_device) {
  6294. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6295. }
  6296. if (src0_asf > 0) {
  6297. ggml_cuda_pool_free(src0_ddf, src0_asf);
  6298. }
  6299. if (src1_asf > 0) {
  6300. ggml_cuda_pool_free(src1_ddf, src1_asf);
  6301. }
  6302. if (dst_asf > 0) {
  6303. ggml_cuda_pool_free(dst_ddf, dst_asf);
  6304. }
  6305. if (dst->backend == GGML_BACKEND_CPU) {
  6306. CUDA_CHECK(cudaDeviceSynchronize());
  6307. }
  6308. }
  6309. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6310. static bool peer_access_enabled = false;
  6311. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6312. if (peer_access_enabled == enable_peer_access) {
  6313. return;
  6314. }
  6315. #ifdef NDEBUG
  6316. for (int id = 0; id < g_device_count; ++id) {
  6317. CUDA_CHECK(ggml_cuda_set_device(id));
  6318. CUDA_CHECK(cudaDeviceSynchronize());
  6319. }
  6320. for (int id = 0; id < g_device_count; ++id) {
  6321. CUDA_CHECK(ggml_cuda_set_device(id));
  6322. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6323. if (id == id_other) {
  6324. continue;
  6325. }
  6326. if (id != g_main_device && id_other != g_main_device) {
  6327. continue;
  6328. }
  6329. int can_access_peer;
  6330. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6331. if (can_access_peer) {
  6332. if (enable_peer_access) {
  6333. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6334. } else {
  6335. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6336. }
  6337. }
  6338. }
  6339. }
  6340. #endif // NDEBUG
  6341. peer_access_enabled = enable_peer_access;
  6342. }
  6343. static void ggml_cuda_op_mul_mat(
  6344. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6345. const bool convert_src1_to_q8_1) {
  6346. const int64_t ne00 = src0->ne[0];
  6347. const int64_t ne01 = src0->ne[1];
  6348. const int64_t ne02 = src0->ne[2];
  6349. const int64_t ne03 = src0->ne[3];
  6350. const int64_t nrows0 = ggml_nrows(src0);
  6351. const int64_t ne10 = src1->ne[0];
  6352. const int64_t ne11 = src1->ne[1];
  6353. const int64_t ne12 = src1->ne[2];
  6354. const int64_t ne13 = src1->ne[3];
  6355. const int64_t nrows1 = ggml_nrows(src1);
  6356. GGML_ASSERT(ne03 == ne13);
  6357. const int64_t ne0 = dst->ne[0];
  6358. const int64_t ne1 = dst->ne[1];
  6359. const int nb2 = dst->nb[2];
  6360. const int nb3 = dst->nb[3];
  6361. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6362. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6363. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6364. const int64_t i02_divisor = ne12 / ne02;
  6365. const size_t src0_ts = ggml_type_size(src0->type);
  6366. const size_t src0_bs = ggml_blck_size(src0->type);
  6367. const size_t q8_1_ts = sizeof(block_q8_1);
  6368. const size_t q8_1_bs = QK8_1;
  6369. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6370. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6371. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6372. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6373. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6374. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6375. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6376. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6377. GGML_ASSERT(!(split && ne02 > 1));
  6378. GGML_ASSERT(!(split && ne03 > 1));
  6379. GGML_ASSERT(!(split && ne02 < ne12));
  6380. // dd = data device
  6381. char * src0_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6382. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  6383. char * src1_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // q8_1
  6384. float * dst_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6385. // as = actual size
  6386. size_t src0_as[GGML_CUDA_MAX_DEVICES] = {0};
  6387. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  6388. size_t src1_asq[GGML_CUDA_MAX_DEVICES] = {0};
  6389. size_t dst_as[GGML_CUDA_MAX_DEVICES] = {0};
  6390. int64_t row_low[GGML_CUDA_MAX_DEVICES];
  6391. int64_t row_high[GGML_CUDA_MAX_DEVICES];
  6392. int used_devices = 0;
  6393. for (int64_t id = 0; id < g_device_count; ++id) {
  6394. // by default, use all rows
  6395. row_low[id] = 0;
  6396. row_high[id] = ne01;
  6397. // for multi GPU, get the row boundaries from tensor split
  6398. // and round to mul_mat_q tile sizes
  6399. if (split) {
  6400. const int64_t rounding = get_row_rounding(src0->type);
  6401. if (id != 0) {
  6402. row_low[id] = ne01*g_tensor_split[id];
  6403. row_low[id] -= row_low[id] % rounding;
  6404. }
  6405. if (id != g_device_count - 1) {
  6406. row_high[id] = ne01*g_tensor_split[id + 1];
  6407. row_high[id] -= row_high[id] % rounding;
  6408. }
  6409. }
  6410. }
  6411. for (int64_t id = 0; id < g_device_count; ++id) {
  6412. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6413. continue;
  6414. }
  6415. used_devices++;
  6416. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6417. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6418. ggml_cuda_set_device(id);
  6419. const cudaStream_t stream = g_cudaStreams[id][0];
  6420. if (src0_on_device && src0_is_contiguous) {
  6421. src0_dd[id] = (char *) src0_extra->data_device[id];
  6422. } else {
  6423. // const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  6424. src0_dd[id] = (char *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  6425. }
  6426. if (src1_on_device && src1_is_contiguous) {
  6427. src1_ddf[id] = (float *) src1_extra->data_device[id];
  6428. } else {
  6429. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  6430. }
  6431. if (convert_src1_to_q8_1) {
  6432. src1_ddq[id] = (char *) ggml_cuda_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  6433. if (src1_on_device && src1_is_contiguous) {
  6434. quantize_row_q8_1_cuda(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  6435. CUDA_CHECK(cudaGetLastError());
  6436. }
  6437. }
  6438. if (dst_on_device) {
  6439. dst_dd[id] = (float *) dst_extra->data_device[id];
  6440. } else {
  6441. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  6442. dst_dd[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_as[id]);
  6443. }
  6444. }
  6445. // if multiple devices are used they need to wait for the main device
  6446. // here an event is recorded that signals that the main device has finished calculating the input data
  6447. if (split && used_devices > 1) {
  6448. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6449. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  6450. }
  6451. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  6452. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  6453. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  6454. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  6455. for (int64_t id = 0; id < g_device_count; ++id) {
  6456. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6457. continue;
  6458. }
  6459. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6460. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6461. const int64_t row_diff = row_high[id] - row_low[id];
  6462. ggml_cuda_set_device(id);
  6463. const cudaStream_t stream = g_cudaStreams[id][is];
  6464. // wait for main GPU data if necessary
  6465. if (split && (id != g_main_device || is != 0)) {
  6466. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  6467. }
  6468. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  6469. const int64_t i03 = i0 / ne12;
  6470. const int64_t i02 = i0 % ne12;
  6471. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  6472. // for split tensors the data begins at i0 == i0_offset_low
  6473. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  6474. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  6475. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  6476. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  6477. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  6478. // in that case an offset on dst_ddf_i is needed
  6479. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  6480. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  6481. }
  6482. // copy src0, src1 to device if necessary
  6483. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  6484. if (id != g_main_device) {
  6485. if (convert_src1_to_q8_1) {
  6486. char * src1_ddq_i_source = src1_ddq[g_main_device] + src1_ddq_i_offset;
  6487. CUDA_CHECK(cudaMemcpyAsync(src1_ddq_i, src1_ddq_i_source, src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs,
  6488. cudaMemcpyDeviceToDevice, stream));
  6489. } else {
  6490. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  6491. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  6492. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_ncols*ne10*sizeof(float),
  6493. cudaMemcpyDeviceToDevice, stream));
  6494. }
  6495. }
  6496. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  6497. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  6498. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  6499. } else {
  6500. GGML_ASSERT(false);
  6501. }
  6502. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  6503. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  6504. CUDA_CHECK(cudaGetLastError());
  6505. }
  6506. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  6507. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  6508. }
  6509. // do the computation
  6510. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  6511. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  6512. CUDA_CHECK(cudaGetLastError());
  6513. // copy dst to host or other device if necessary
  6514. if (!dst_on_device) {
  6515. void * dst_off_device;
  6516. cudaMemcpyKind kind;
  6517. if (dst->backend == GGML_BACKEND_CPU) {
  6518. dst_off_device = dst->data;
  6519. kind = cudaMemcpyDeviceToHost;
  6520. } else if (dst->backend == GGML_BACKEND_GPU) {
  6521. dst_off_device = dst_extra->data_device[g_main_device];
  6522. kind = cudaMemcpyDeviceToDevice;
  6523. } else {
  6524. GGML_ASSERT(false);
  6525. }
  6526. if (split) {
  6527. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  6528. // dst is NOT transposed.
  6529. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  6530. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  6531. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  6532. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6533. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6534. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  6535. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_dd_i, row_diff*sizeof(float),
  6536. row_diff*sizeof(float), src1_ncols, kind, stream));
  6537. } else {
  6538. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6539. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6540. dhf_dst_i += src1_col_0*ne0;
  6541. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  6542. }
  6543. }
  6544. // add event for the main device to wait on until other device is done
  6545. if (split && (id != g_main_device || is != 0)) {
  6546. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  6547. }
  6548. }
  6549. }
  6550. }
  6551. for (int64_t id = 0; id < g_device_count; ++id) {
  6552. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6553. continue;
  6554. }
  6555. CUDA_CHECK(ggml_cuda_set_device(id));
  6556. // free buffers again when done
  6557. if (src0_as[id] > 0) {
  6558. ggml_cuda_pool_free(src0_dd[id], src0_as[id]);
  6559. }
  6560. if (src1_asf[id] > 0) {
  6561. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  6562. }
  6563. if (src1_asq[id] > 0) {
  6564. ggml_cuda_pool_free(src1_ddq[id], src1_asq[id]);
  6565. }
  6566. if (dst_as[id] > 0) {
  6567. ggml_cuda_pool_free(dst_dd[id], dst_as[id]);
  6568. }
  6569. }
  6570. // main device waits for all other devices to be finished
  6571. if (split && g_device_count > 1) {
  6572. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  6573. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  6574. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6575. for (int64_t id = 0; id < g_device_count; ++id) {
  6576. if (row_low[id] == row_high[id]) {
  6577. continue;
  6578. }
  6579. for (int64_t is = 0; is < is_max; ++is) {
  6580. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  6581. }
  6582. }
  6583. }
  6584. if (dst->backend == GGML_BACKEND_CPU) {
  6585. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6586. CUDA_CHECK(cudaDeviceSynchronize());
  6587. }
  6588. }
  6589. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6590. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  6591. }
  6592. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6593. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  6594. }
  6595. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6596. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  6597. }
  6598. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6599. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  6600. }
  6601. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6602. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  6603. }
  6604. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6605. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  6606. }
  6607. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6608. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  6609. }
  6610. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6611. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  6612. }
  6613. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6614. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  6615. }
  6616. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6617. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  6618. }
  6619. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6620. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  6621. }
  6622. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6623. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  6624. }
  6625. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6626. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  6627. }
  6628. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6629. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  6630. }
  6631. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6632. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  6633. }
  6634. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6635. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  6636. }
  6637. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6638. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  6639. }
  6640. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6641. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  6642. }
  6643. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6644. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  6645. }
  6646. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  6647. if (!g_cublas_loaded) return false;
  6648. const int64_t ne10 = src1->ne[0];
  6649. const int64_t ne0 = dst->ne[0];
  6650. const int64_t ne1 = dst->ne[1];
  6651. // TODO: find the optimal values for these
  6652. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  6653. src1->type == GGML_TYPE_F32 &&
  6654. dst->type == GGML_TYPE_F32 &&
  6655. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  6656. }
  6657. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6658. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  6659. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6660. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  6661. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  6662. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6663. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6664. const int64_t ne00 = src0->ne[0];
  6665. const int64_t ne01 = src0->ne[1];
  6666. const int64_t ne02 = src0->ne[2];
  6667. const int64_t ne12 = src1->ne[2];
  6668. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6669. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6670. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6671. void * src0_ddq = src0_extra->data_device[g_main_device];
  6672. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6673. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6674. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6675. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6676. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  6677. }
  6678. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6679. GGML_ASSERT(!ggml_is_transposed(src0));
  6680. GGML_ASSERT(!ggml_is_transposed(src1));
  6681. GGML_ASSERT(!ggml_is_permuted(src0));
  6682. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6683. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6684. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6685. const int64_t ne00 = src0->ne[0];
  6686. const int64_t ne01 = src0->ne[1];
  6687. const int64_t ne02 = src0->ne[2];
  6688. const int64_t nb01 = src0->nb[1];
  6689. const int64_t nb02 = src0->nb[2];
  6690. const int64_t ne12 = src1->ne[2];
  6691. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6692. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6693. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6694. void * src0_ddq = src0_extra->data_device[g_main_device];
  6695. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6696. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6697. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6698. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6699. const int64_t row_stride_x = nb01 / sizeof(half);
  6700. const int64_t channel_stride_x = nb02 / sizeof(half);
  6701. 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);
  6702. }
  6703. static __global__ void k_compute_batched_ptrs(
  6704. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  6705. const void ** ptrs_src, void ** ptrs_dst,
  6706. int64_t ne12, int64_t ne13,
  6707. int64_t ne23,
  6708. size_t nb02, size_t nb03,
  6709. size_t nb12, size_t nb13,
  6710. size_t nbd2, size_t nbd3,
  6711. int64_t r2, int64_t r3) {
  6712. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6713. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6714. if (i13 >= ne13 || i12 >= ne12) {
  6715. return;
  6716. }
  6717. int64_t i03 = i13 / r3;
  6718. int64_t i02 = i12 / r2;
  6719. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  6720. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  6721. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  6722. }
  6723. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6724. GGML_ASSERT(!ggml_is_transposed(src0));
  6725. GGML_ASSERT(!ggml_is_transposed(src1));
  6726. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6727. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6728. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6729. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  6730. const int64_t ne01 = src0->ne[1];
  6731. const int64_t ne02 = src0->ne[2];
  6732. const int64_t ne03 = src0->ne[3];
  6733. const int64_t nb01 = src0->nb[1];
  6734. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  6735. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  6736. const int64_t ne10 = src1->ne[0];
  6737. const int64_t ne11 = src1->ne[1];
  6738. const int64_t ne12 = src1->ne[2];
  6739. const int64_t ne13 = src1->ne[3];
  6740. const int64_t nb11 = src1->nb[1];
  6741. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6742. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6743. const int64_t ne1 = ggml_nelements(src1);
  6744. const int64_t ne = ggml_nelements(dst);
  6745. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6746. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6747. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6748. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6749. void * src0_ddq = src0_extra->data_device[g_main_device];
  6750. half * src0_as_f16 = (half *) src0_ddq;
  6751. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6752. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6753. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6754. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6755. // convert src1 to fp16
  6756. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6757. GGML_ASSERT(to_fp16_cuda != nullptr);
  6758. size_t src1_as = 0;
  6759. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  6760. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  6761. size_t dst_as = 0;
  6762. half * dst_f16 = nullptr;
  6763. char * dst_t = nullptr;
  6764. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  6765. cudaDataType_t cu_data_type = CUDA_R_16F;
  6766. // dst strides
  6767. size_t nbd2 = dst->nb[2];
  6768. size_t nbd3 = dst->nb[3];
  6769. const half alpha_f16 = 1.0f;
  6770. const half beta_f16 = 0.0f;
  6771. const float alpha_f32 = 1.0f;
  6772. const float beta_f32 = 0.0f;
  6773. const void * alpha = &alpha_f16;
  6774. const void * beta = &beta_f16;
  6775. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6776. dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  6777. dst_t = (char *) dst_f16;
  6778. nbd2 /= sizeof(float) / sizeof(half);
  6779. nbd3 /= sizeof(float) / sizeof(half);
  6780. } else {
  6781. dst_t = (char *) dst_ddf;
  6782. cu_compute_type = CUBLAS_COMPUTE_32F;
  6783. cu_data_type = CUDA_R_32F;
  6784. alpha = &alpha_f32;
  6785. beta = &beta_f32;
  6786. }
  6787. GGML_ASSERT(ne12 % ne02 == 0);
  6788. GGML_ASSERT(ne13 % ne03 == 0);
  6789. // broadcast factors
  6790. const int64_t r2 = ne12/ne02;
  6791. const int64_t r3 = ne13/ne03;
  6792. #if 0
  6793. // use cublasGemmEx
  6794. {
  6795. for (int i13 = 0; i13 < ne13; ++i13) {
  6796. for (int i12 = 0; i12 < ne12; ++i12) {
  6797. int i03 = i13 / r3;
  6798. int i02 = i12 / r2;
  6799. CUBLAS_CHECK(
  6800. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6801. ne01, ne11, ne10,
  6802. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6803. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6804. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  6805. cu_compute_type,
  6806. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6807. }
  6808. }
  6809. }
  6810. #else
  6811. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6812. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6813. // use cublasGemmStridedBatchedEx
  6814. CUBLAS_CHECK(
  6815. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6816. ne01, ne11, ne10,
  6817. alpha, (const char *) src0_as_f16, CUDA_R_16F, nb01/sizeof(half), src0->nb[2]/sizeof(half), // strideA
  6818. (const char *) src1_as_f16, CUDA_R_16F, nb11/sizeof(float), src1->nb[2]/sizeof(float), // strideB
  6819. beta, ( char *) dst_t, cu_data_type, ne01, dst->nb[2]/sizeof(float), // strideC
  6820. ne12*ne13,
  6821. cu_compute_type,
  6822. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6823. } else {
  6824. // use cublasGemmBatchedEx
  6825. const int ne23 = ne12*ne13;
  6826. const void ** ptrs_src = nullptr;
  6827. void ** ptrs_dst = nullptr;
  6828. size_t ptrs_src_s = 0;
  6829. size_t ptrs_dst_s = 0;
  6830. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  6831. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  6832. dim3 block_dims(ne13, ne12);
  6833. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6834. src0_as_f16, src1_as_f16, dst_t,
  6835. ptrs_src, ptrs_dst,
  6836. ne12, ne13,
  6837. ne23,
  6838. nb02, nb03,
  6839. nb12, nb13,
  6840. nbd2, nbd3,
  6841. r2, r3);
  6842. CUDA_CHECK(cudaGetLastError());
  6843. CUBLAS_CHECK(
  6844. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6845. ne01, ne11, ne10,
  6846. alpha, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, nb01/sizeof(half),
  6847. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, nb11/sizeof(float),
  6848. beta, ( void **) (ptrs_dst + 0*ne23), cu_data_type, ne01,
  6849. ne23,
  6850. cu_compute_type,
  6851. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6852. if (ptrs_src_s != 0) {
  6853. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  6854. }
  6855. if (ptrs_dst_s != 0) {
  6856. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  6857. }
  6858. }
  6859. #endif
  6860. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6861. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6862. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  6863. ggml_cuda_pool_free(dst_f16, dst_as);
  6864. }
  6865. ggml_cuda_pool_free(src1_as_f16, src1_as);
  6866. }
  6867. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6868. const bool all_on_device =
  6869. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6870. (src1->backend == GGML_BACKEND_GPU) &&
  6871. ( dst->backend == GGML_BACKEND_GPU);
  6872. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6873. int64_t min_compute_capability = INT_MAX;
  6874. for (int64_t id = 0; id < g_device_count; ++id) {
  6875. if (min_compute_capability > g_compute_capabilities[id] && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6876. min_compute_capability = g_compute_capabilities[id];
  6877. }
  6878. }
  6879. #ifdef CUDA_USE_TENSOR_CORES
  6880. const bool use_tensor_cores = true;
  6881. #else
  6882. const bool use_tensor_cores = false;
  6883. #endif
  6884. // debug helpers
  6885. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  6886. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  6887. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  6888. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  6889. //printf("src0 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src0), ggml_is_transposed(src0), ggml_type_name(src0->type), src0->name);
  6890. //printf("src1 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src1), ggml_is_transposed(src1), ggml_type_name(src1->type), src1->name);
  6891. if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  6892. // KQ single-batch
  6893. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  6894. } else if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  6895. // KQV single-batch
  6896. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  6897. } else if (!split && all_on_device && use_tensor_cores && src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  6898. // KQ + KQV multi-batch
  6899. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  6900. } else if (src0->type == GGML_TYPE_F32) {
  6901. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6902. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  6903. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  6904. #ifdef GGML_CUDA_FORCE_DMMV
  6905. const bool use_mul_mat_vec_q = false;
  6906. #else
  6907. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  6908. #endif // GGML_CUDA_FORCE_DMMV
  6909. if (use_mul_mat_vec_q) {
  6910. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  6911. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  6912. } else {
  6913. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  6914. }
  6915. } else {
  6916. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  6917. // when tensor cores are available, use them for large batch size
  6918. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  6919. if (use_tensor_cores && min_compute_capability >= CC_VOLTA && src1->ne[1] > MMQ_MAX_BATCH_SIZE) {
  6920. use_mul_mat_q = false;
  6921. }
  6922. if (use_mul_mat_q) {
  6923. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  6924. } else {
  6925. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6926. }
  6927. }
  6928. } else {
  6929. GGML_ASSERT(false);
  6930. }
  6931. }
  6932. #if 0
  6933. template<typename ... Srcs>
  6934. static __global__ void k_compute_batched_ptrs_id(
  6935. const void ** ptrs_src, void ** ptrs_dst,
  6936. int ne12, int ne13,
  6937. int ne23,
  6938. int nb02, int nb03,
  6939. int nb12, int nb13,
  6940. int nb2, int nb3,
  6941. int r2, int r3,
  6942. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  6943. const half * src1_f16, half * dst_f16,
  6944. const int32_t * ids, const int id,
  6945. Srcs... src0s) {
  6946. int i = ids[id];
  6947. half * src0_f16;
  6948. const void * srcs_ar[] = { (const half *) src0s... };
  6949. if (src0_type == GGML_TYPE_F16) {
  6950. src0_f16 = (half *) srcs_ar[i];
  6951. } else {
  6952. src0_f16 = src0_as_f16;
  6953. if (threadIdx.x == 0 && threadIdx.y == 0) {
  6954. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  6955. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  6956. }
  6957. }
  6958. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6959. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6960. if (i13 >= ne13 || i12 >= ne12) {
  6961. return;
  6962. }
  6963. int i03 = i13 / r3;
  6964. int i02 = i12 / r2;
  6965. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  6966. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  6967. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  6968. }
  6969. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  6970. const struct ggml_tensor * ids = dst->src[0];
  6971. const struct ggml_tensor * src1 = dst->src[1];
  6972. const struct ggml_tensor * src00 = dst->src[2];
  6973. const int id = dst->op_params[0];
  6974. GGML_ASSERT(!ggml_is_transposed(src00));
  6975. GGML_ASSERT(!ggml_is_transposed(src1));
  6976. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  6977. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6978. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  6979. const int64_t ne01 = src00->ne[1];
  6980. const int64_t ne02 = src00->ne[2];
  6981. const int64_t ne03 = src00->ne[3];
  6982. //const int64_t nb01 = src00->nb[1];
  6983. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  6984. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  6985. const int64_t ne10 = src1->ne[0];
  6986. const int64_t ne11 = src1->ne[1];
  6987. const int64_t ne12 = src1->ne[2];
  6988. const int64_t ne13 = src1->ne[3];
  6989. //const int64_t nb11 = src1->nb[1];
  6990. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6991. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6992. const int64_t ne1 = ggml_nelements(src1);
  6993. const int64_t ne = ggml_nelements(dst);
  6994. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6995. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6996. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6997. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6998. //void * src0_ddq = src0_extra->data_device[g_main_device];
  6999. //half * src0_as_f16 = (half *) src0_ddq;
  7000. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7001. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7002. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7003. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7004. // convert src1 to fp16
  7005. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7006. GGML_ASSERT(to_fp16_cuda != nullptr);
  7007. size_t src1_as = 0;
  7008. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7009. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7010. size_t dst_as = 0;
  7011. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7012. GGML_ASSERT(ne12 % ne02 == 0);
  7013. GGML_ASSERT(ne13 % ne03 == 0);
  7014. // broadcast factors
  7015. const int64_t r2 = ne12/ne02;
  7016. const int64_t r3 = ne13/ne03;
  7017. const half alpha_f16 = 1.0f;
  7018. const half beta_f16 = 0.0f;
  7019. // use cublasGemmBatchedEx
  7020. const int ne23 = ne12*ne13;
  7021. const void ** ptrs_src = nullptr;
  7022. void ** ptrs_dst = nullptr;
  7023. size_t ptrs_src_s = 0;
  7024. size_t ptrs_dst_s = 0;
  7025. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7026. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7027. int64_t src0_ne = ggml_nelements(src00);
  7028. half * src0_as_f16 = nullptr;
  7029. size_t src0_as = 0;
  7030. if (src00->type != GGML_TYPE_F16) {
  7031. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7032. }
  7033. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7034. dim3 block_dims(ne13, ne12);
  7035. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7036. ptrs_src, ptrs_dst,
  7037. ne12, ne13,
  7038. ne23,
  7039. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7040. nb12, nb13,
  7041. dst->nb[2], dst->nb[3],
  7042. r2, r3,
  7043. src00->type, src0_as_f16, src0_ne,
  7044. src1_as_f16, dst_f16,
  7045. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7046. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7047. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7048. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7049. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7050. );
  7051. CUDA_CHECK(cudaGetLastError());
  7052. CUBLAS_CHECK(
  7053. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7054. ne01, ne11, ne10,
  7055. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7056. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7057. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7058. ne23,
  7059. CUBLAS_COMPUTE_16F,
  7060. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7061. if (src0_as != 0) {
  7062. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7063. }
  7064. if (ptrs_src_s != 0) {
  7065. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7066. }
  7067. if (ptrs_dst_s != 0) {
  7068. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7069. }
  7070. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7071. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7072. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7073. ggml_cuda_pool_free(dst_f16, dst_as);
  7074. }
  7075. #endif
  7076. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7077. #if 0
  7078. ggml_cuda_mul_mat_id_cublas(dst);
  7079. // TODO: mmq/mmv support
  7080. #endif
  7081. const int64_t nb11 = src1->nb[1];
  7082. const int64_t nb1 = dst->nb[1];
  7083. const struct ggml_tensor * ids = src0;
  7084. const int32_t id = ((int32_t *) dst->op_params)[0];
  7085. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7086. std::vector<char> ids_host(ggml_nbytes(ids));
  7087. const cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7088. if (ids->backend == GGML_BACKEND_GPU) {
  7089. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7090. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7091. CUDA_CHECK(cudaStreamSynchronize(stream));
  7092. } else {
  7093. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7094. }
  7095. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7096. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7097. ggml_tensor_extra_gpu src1_row_extra;
  7098. ggml_tensor_extra_gpu dst_row_extra;
  7099. ggml_tensor src1_row = *src1;
  7100. ggml_tensor dst_row = *dst;
  7101. src1_row.backend = GGML_BACKEND_GPU;
  7102. dst_row.backend = GGML_BACKEND_GPU;
  7103. src1_row.extra = &src1_row_extra;
  7104. dst_row.extra = &dst_row_extra;
  7105. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7106. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7107. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7108. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7109. if (src1->ne[1] == 1) {
  7110. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7111. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7112. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7113. //int32_t row_id;
  7114. //CUDA_CHECK(cudaMemcpyAsync(&row_id, ids_dev + i01*ids->nb[1] + id*ids->nb[0], sizeof(int32_t), cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  7115. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7116. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7117. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7118. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7119. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7120. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7121. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7122. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7123. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7124. }
  7125. } else {
  7126. size_t as_src1, as_dst;
  7127. char * src1_contiguous = (char *) ggml_cuda_pool_malloc(sizeof(float)*ggml_nelements(src1), &as_src1);
  7128. char * dst_contiguous = (char *) ggml_cuda_pool_malloc(sizeof(float)*ggml_nelements(dst), &as_dst);
  7129. src1_row_extra.data_device[g_main_device] = src1_contiguous;
  7130. dst_row_extra.data_device[g_main_device] = dst_contiguous;
  7131. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7132. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7133. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7134. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7135. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7136. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7137. int64_t num_src1_rows = 0;
  7138. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7139. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7140. if (row_id_i != row_id) {
  7141. continue;
  7142. }
  7143. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7144. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous + num_src1_rows*nb11, src1_original + i01*nb11,
  7145. nb11, src1_kind, stream));
  7146. num_src1_rows++;
  7147. }
  7148. if (num_src1_rows == 0) {
  7149. continue;
  7150. }
  7151. src1_row.ne[1] = num_src1_rows;
  7152. dst_row.ne[1] = num_src1_rows;
  7153. src1_row.nb[1] = nb11;
  7154. src1_row.nb[2] = num_src1_rows*nb11;
  7155. src1_row.nb[3] = num_src1_rows*nb11;
  7156. dst_row.nb[1] = nb1;
  7157. dst_row.nb[2] = num_src1_rows*nb1;
  7158. dst_row.nb[3] = num_src1_rows*nb1;
  7159. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7160. num_src1_rows = 0;
  7161. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7162. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7163. if (row_id_i != row_id) {
  7164. continue;
  7165. }
  7166. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7167. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous + num_src1_rows*nb1,
  7168. nb1, dst_kind, stream));
  7169. num_src1_rows++;
  7170. }
  7171. }
  7172. ggml_cuda_pool_free(src1_contiguous, as_src1);
  7173. ggml_cuda_pool_free(dst_contiguous, as_dst);
  7174. }
  7175. if (dst->backend == GGML_BACKEND_CPU) {
  7176. CUDA_CHECK(cudaStreamSynchronize(stream));
  7177. }
  7178. }
  7179. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7180. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7181. }
  7182. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7183. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7184. }
  7185. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7186. const int64_t ne = ggml_nelements(src0);
  7187. GGML_ASSERT(ne == ggml_nelements(src1));
  7188. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7189. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7190. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7191. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7192. const int64_t ne00 = src0->ne[0];
  7193. const int64_t ne01 = src0->ne[1];
  7194. GGML_ASSERT(src0->ne[3] == 1);
  7195. const int64_t nb00 = src0->nb[0];
  7196. const int64_t nb01 = src0->nb[1];
  7197. const int64_t nb02 = src0->nb[2];
  7198. const int64_t ne10 = src1->ne[0];
  7199. const int64_t ne11 = src1->ne[1];
  7200. GGML_ASSERT(src1->ne[3] == 1);
  7201. const int64_t nb10 = src1->nb[0];
  7202. const int64_t nb11 = src1->nb[1];
  7203. const int64_t nb12 = src1->nb[2];
  7204. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7205. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7206. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7207. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7208. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7209. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7210. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7211. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7212. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7213. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7214. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7215. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7216. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7217. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7218. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7219. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7220. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7221. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7222. } else {
  7223. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7224. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7225. GGML_ASSERT(false);
  7226. }
  7227. (void) dst;
  7228. }
  7229. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7230. // TODO: why do we pass dst as src1 here?
  7231. ggml_cuda_cpy(src0, dst, nullptr);
  7232. (void) src1;
  7233. }
  7234. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7235. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7236. }
  7237. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7238. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7239. }
  7240. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7241. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7242. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7243. }
  7244. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7245. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7246. }
  7247. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7248. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7249. }
  7250. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7251. GGML_ASSERT(ggml_is_contiguous(src0));
  7252. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7253. }
  7254. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7255. GGML_ASSERT(ggml_is_contiguous(src0));
  7256. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7257. }
  7258. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7259. (void) src0;
  7260. (void) src1;
  7261. (void) dst;
  7262. }
  7263. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7264. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7265. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7266. }
  7267. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7268. const int64_t nrows = ggml_nrows(tensor);
  7269. const int64_t ne0 = tensor->ne[0];
  7270. const size_t nb1 = tensor->nb[1];
  7271. ggml_backend_type backend = tensor->backend;
  7272. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7273. memset(extra, 0, sizeof(*extra));
  7274. for (int64_t id = 0; id < g_device_count; ++id) {
  7275. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7276. continue;
  7277. }
  7278. ggml_cuda_set_device(id);
  7279. int64_t row_low, row_high;
  7280. if (backend == GGML_BACKEND_GPU) {
  7281. row_low = 0;
  7282. row_high = nrows;
  7283. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7284. const int64_t rounding = get_row_rounding(tensor->type);
  7285. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7286. row_low -= row_low % rounding;
  7287. if (id == g_device_count - 1) {
  7288. row_high = nrows;
  7289. } else {
  7290. row_high = nrows*g_tensor_split[id + 1];
  7291. row_high -= row_high % rounding;
  7292. }
  7293. } else {
  7294. GGML_ASSERT(false);
  7295. }
  7296. if (row_low == row_high) {
  7297. continue;
  7298. }
  7299. int64_t nrows_split = row_high - row_low;
  7300. const size_t offset_split = row_low*nb1;
  7301. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7302. const size_t original_size = size;
  7303. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7304. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7305. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7306. }
  7307. char * buf;
  7308. CUDA_CHECK(cudaMalloc(&buf, size));
  7309. char * buf_host = (char *)data + offset_split;
  7310. // set padding to 0 to avoid possible NaN values
  7311. if (size > original_size) {
  7312. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7313. }
  7314. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7315. extra->data_device[id] = buf;
  7316. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7317. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7318. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7319. }
  7320. }
  7321. }
  7322. tensor->extra = extra;
  7323. }
  7324. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7325. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7326. return;
  7327. }
  7328. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7329. for (int64_t id = 0; id < g_device_count; ++id) {
  7330. if (extra->data_device[id] != nullptr) {
  7331. CUDA_CHECK(ggml_cuda_set_device(id));
  7332. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7333. }
  7334. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7335. if (extra->events[id][is] != nullptr) {
  7336. CUDA_CHECK(ggml_cuda_set_device(id));
  7337. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7338. }
  7339. }
  7340. }
  7341. delete extra;
  7342. }
  7343. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7344. static size_t g_temp_tensor_extra_index = 0;
  7345. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7346. if (g_temp_tensor_extras == nullptr) {
  7347. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7348. }
  7349. size_t alloc_index = g_temp_tensor_extra_index;
  7350. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7351. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7352. memset(extra, 0, sizeof(*extra));
  7353. return extra;
  7354. }
  7355. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7356. if (scratch && g_scratch_size == 0) {
  7357. return;
  7358. }
  7359. tensor->backend = GGML_BACKEND_GPU;
  7360. // recursively assign CUDA buffers until a compute tensor is found
  7361. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7362. const ggml_op src0_op = tensor->src[0]->op;
  7363. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7364. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7365. }
  7366. }
  7367. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7368. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7369. }
  7370. if (scratch && no_alloc) {
  7371. return;
  7372. }
  7373. ggml_tensor_extra_gpu * extra;
  7374. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7375. tensor->op == GGML_OP_VIEW ||
  7376. force_inplace;
  7377. const size_t size = ggml_nbytes(tensor);
  7378. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7379. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7380. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7381. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7382. size_t offset = 0;
  7383. if (tensor->op == GGML_OP_VIEW) {
  7384. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7385. }
  7386. extra = ggml_cuda_alloc_temp_tensor_extra();
  7387. extra->data_device[g_main_device] = src0_ddc + offset;
  7388. } else if (tensor->op == GGML_OP_CPY) {
  7389. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7390. void * src1_ddv = src1_extra->data_device[g_main_device];
  7391. extra = ggml_cuda_alloc_temp_tensor_extra();
  7392. extra->data_device[g_main_device] = src1_ddv;
  7393. } else if (scratch) {
  7394. GGML_ASSERT(size <= g_scratch_size);
  7395. if (g_scratch_offset + size > g_scratch_size) {
  7396. g_scratch_offset = 0;
  7397. }
  7398. char * data = (char *) g_scratch_buffer;
  7399. if (data == nullptr) {
  7400. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7401. g_scratch_buffer = data;
  7402. }
  7403. extra = ggml_cuda_alloc_temp_tensor_extra();
  7404. extra->data_device[g_main_device] = data + g_scratch_offset;
  7405. g_scratch_offset += size;
  7406. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7407. } else { // allocate new buffers outside of scratch
  7408. void * data;
  7409. CUDA_CHECK(cudaMalloc(&data, size));
  7410. CUDA_CHECK(cudaMemset(data, 0, size));
  7411. extra = new ggml_tensor_extra_gpu;
  7412. memset(extra, 0, sizeof(*extra));
  7413. extra->data_device[g_main_device] = data;
  7414. }
  7415. tensor->extra = extra;
  7416. }
  7417. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7418. if (g_scratch_size == 0) {
  7419. return;
  7420. }
  7421. if (g_scratch_buffer == nullptr) {
  7422. ggml_cuda_set_device(g_main_device);
  7423. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7424. }
  7425. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7426. const bool inplace = tensor->view_src != nullptr;
  7427. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  7428. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  7429. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7430. size_t view_offset = 0;
  7431. if (tensor->op == GGML_OP_VIEW) {
  7432. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7433. }
  7434. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7435. } else {
  7436. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7437. }
  7438. tensor->extra = extra;
  7439. }
  7440. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7441. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7442. GGML_ASSERT(ggml_is_contiguous(tensor));
  7443. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7444. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7445. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7446. }
  7447. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7448. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7449. }
  7450. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7451. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7452. }
  7453. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  7454. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  7455. }
  7456. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  7457. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  7458. }
  7459. void ggml_cuda_set_main_device(const int main_device) {
  7460. if (main_device >= g_device_count) {
  7461. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  7462. main_device, g_device_count, g_main_device);
  7463. return;
  7464. }
  7465. if (g_main_device != main_device && g_device_count > 1) {
  7466. g_main_device = main_device;
  7467. cudaDeviceProp prop;
  7468. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  7469. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  7470. }
  7471. }
  7472. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  7473. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  7474. // it still won't always work as expected, but it's better than nothing
  7475. if (scratch_size > g_scratch_size) {
  7476. ggml_cuda_free_scratch();
  7477. }
  7478. g_scratch_size = std::max(g_scratch_size, scratch_size);
  7479. }
  7480. void ggml_cuda_free_scratch() {
  7481. if (g_scratch_buffer == nullptr) {
  7482. return;
  7483. }
  7484. CUDA_CHECK(cudaFree(g_scratch_buffer));
  7485. g_scratch_buffer = nullptr;
  7486. }
  7487. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  7488. if (!g_cublas_loaded) return false;
  7489. ggml_cuda_func_t func;
  7490. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  7491. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  7492. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  7493. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  7494. return false;
  7495. }
  7496. if (tensor->op == GGML_OP_MUL_MAT) {
  7497. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  7498. #ifndef NDEBUG
  7499. fprintf(stderr, "%s: cannot compute %s: src0->ne[3] = %" PRId64 ", src1->ne[3] = %" PRId64 " - fallback to CPU\n", __func__, tensor->name, tensor->src[0]->ne[3], tensor->src[1]->ne[3]);
  7500. #endif
  7501. return false;
  7502. }
  7503. }
  7504. switch (tensor->op) {
  7505. case GGML_OP_REPEAT:
  7506. func = ggml_cuda_repeat;
  7507. break;
  7508. case GGML_OP_GET_ROWS:
  7509. func = ggml_cuda_get_rows;
  7510. break;
  7511. case GGML_OP_DUP:
  7512. func = ggml_cuda_dup;
  7513. break;
  7514. case GGML_OP_ADD:
  7515. func = ggml_cuda_add;
  7516. break;
  7517. case GGML_OP_ACC:
  7518. func = ggml_cuda_acc;
  7519. break;
  7520. case GGML_OP_MUL:
  7521. func = ggml_cuda_mul;
  7522. break;
  7523. case GGML_OP_DIV:
  7524. func = ggml_cuda_div;
  7525. break;
  7526. case GGML_OP_UNARY:
  7527. switch (ggml_get_unary_op(tensor)) {
  7528. case GGML_UNARY_OP_GELU:
  7529. func = ggml_cuda_gelu;
  7530. break;
  7531. case GGML_UNARY_OP_SILU:
  7532. func = ggml_cuda_silu;
  7533. break;
  7534. case GGML_UNARY_OP_GELU_QUICK:
  7535. func = ggml_cuda_gelu_quick;
  7536. break;
  7537. case GGML_UNARY_OP_TANH:
  7538. func = ggml_cuda_tanh;
  7539. break;
  7540. case GGML_UNARY_OP_RELU:
  7541. func = ggml_cuda_relu;
  7542. break;
  7543. default:
  7544. return false;
  7545. }
  7546. break;
  7547. case GGML_OP_NORM:
  7548. func = ggml_cuda_norm;
  7549. break;
  7550. case GGML_OP_GROUP_NORM:
  7551. func = ggml_cuda_group_norm;
  7552. break;
  7553. case GGML_OP_CONCAT:
  7554. func = ggml_cuda_concat;
  7555. break;
  7556. case GGML_OP_UPSCALE:
  7557. func = ggml_cuda_upscale;
  7558. break;
  7559. case GGML_OP_PAD:
  7560. func = ggml_cuda_pad;
  7561. break;
  7562. case GGML_OP_LEAKY_RELU:
  7563. func = ggml_cuda_leaky_relu;
  7564. break;
  7565. case GGML_OP_RMS_NORM:
  7566. func = ggml_cuda_rms_norm;
  7567. break;
  7568. case GGML_OP_MUL_MAT:
  7569. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  7570. return false;
  7571. }
  7572. func = ggml_cuda_mul_mat;
  7573. break;
  7574. case GGML_OP_MUL_MAT_ID:
  7575. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  7576. return false;
  7577. }
  7578. func = ggml_cuda_mul_mat_id;
  7579. break;
  7580. case GGML_OP_SCALE:
  7581. func = ggml_cuda_scale;
  7582. break;
  7583. case GGML_OP_SQR:
  7584. func = ggml_cuda_sqr;
  7585. break;
  7586. case GGML_OP_CLAMP:
  7587. func = ggml_cuda_clamp;
  7588. break;
  7589. case GGML_OP_CPY:
  7590. func = ggml_cuda_cpy;
  7591. break;
  7592. case GGML_OP_CONT:
  7593. func = ggml_cuda_dup;
  7594. break;
  7595. case GGML_OP_NONE:
  7596. case GGML_OP_RESHAPE:
  7597. case GGML_OP_VIEW:
  7598. case GGML_OP_PERMUTE:
  7599. case GGML_OP_TRANSPOSE:
  7600. func = ggml_cuda_nop;
  7601. break;
  7602. case GGML_OP_DIAG_MASK_INF:
  7603. func = ggml_cuda_diag_mask_inf;
  7604. break;
  7605. case GGML_OP_SOFT_MAX:
  7606. func = ggml_cuda_soft_max;
  7607. break;
  7608. case GGML_OP_ROPE:
  7609. func = ggml_cuda_rope;
  7610. break;
  7611. case GGML_OP_ALIBI:
  7612. func = ggml_cuda_alibi;
  7613. break;
  7614. case GGML_OP_IM2COL:
  7615. func = ggml_cuda_im2col;
  7616. break;
  7617. case GGML_OP_SUM_ROWS:
  7618. func = ggml_cuda_sum_rows;
  7619. break;
  7620. case GGML_OP_ARGSORT:
  7621. func = ggml_cuda_argsort;
  7622. break;
  7623. default:
  7624. return false;
  7625. }
  7626. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  7627. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  7628. }
  7629. if (params->ith != 0) {
  7630. return true;
  7631. }
  7632. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  7633. return true;
  7634. }
  7635. func(tensor->src[0], tensor->src[1], tensor);
  7636. return true;
  7637. }
  7638. int ggml_cuda_get_device_count() {
  7639. int device_count;
  7640. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  7641. return 0;
  7642. }
  7643. return device_count;
  7644. }
  7645. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  7646. cudaDeviceProp prop;
  7647. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  7648. snprintf(description, description_size, "%s", prop.name);
  7649. }
  7650. ////////////////////////////////////////////////////////////////////////////////
  7651. // backend interface
  7652. #define UNUSED GGML_UNUSED
  7653. // cuda buffer
  7654. struct ggml_backend_buffer_context_cuda {
  7655. int device;
  7656. void * dev_ptr = nullptr;
  7657. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  7658. size_t temp_tensor_extra_index = 0;
  7659. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  7660. ~ggml_backend_buffer_context_cuda() {
  7661. delete[] temp_tensor_extras;
  7662. }
  7663. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7664. if (temp_tensor_extras == nullptr) {
  7665. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7666. }
  7667. size_t alloc_index = temp_tensor_extra_index;
  7668. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7669. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  7670. memset(extra, 0, sizeof(*extra));
  7671. return extra;
  7672. }
  7673. };
  7674. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7675. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7676. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  7677. delete ctx;
  7678. }
  7679. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  7680. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7681. return ctx->dev_ptr;
  7682. }
  7683. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7684. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7685. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  7686. assert(tensor->view_src->buffer->buft == buffer->buft);
  7687. tensor->backend = tensor->view_src->backend;
  7688. tensor->extra = tensor->view_src->extra;
  7689. return;
  7690. }
  7691. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  7692. extra->data_device[ctx->device] = tensor->data;
  7693. tensor->backend = GGML_BACKEND_GPU;
  7694. tensor->extra = extra;
  7695. if (ggml_is_quantized(tensor->type)) {
  7696. // initialize padding to 0 to avoid possible NaN values
  7697. int64_t row_low = 0;
  7698. int64_t row_high = ggml_nrows(tensor);
  7699. int64_t nrows_split = row_high - row_low;
  7700. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  7701. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  7702. if (padded_size > original_size && tensor->view_src == nullptr) {
  7703. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  7704. }
  7705. }
  7706. UNUSED(buffer);
  7707. }
  7708. static void ggml_backend_cuda_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7709. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7710. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7711. ggml_cuda_set_device(ctx->device);
  7712. CUDA_CHECK(cudaDeviceSynchronize());
  7713. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  7714. }
  7715. static void ggml_backend_cuda_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7716. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7717. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7718. ggml_cuda_set_device(ctx->device);
  7719. CUDA_CHECK(cudaDeviceSynchronize());
  7720. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  7721. }
  7722. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7723. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7724. ggml_cuda_set_device(ctx->device);
  7725. CUDA_CHECK(cudaDeviceSynchronize());
  7726. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  7727. }
  7728. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  7729. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  7730. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  7731. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  7732. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  7733. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  7734. /* .cpy_tensor_from = */ NULL,
  7735. /* .cpy_tensor_to = */ NULL,
  7736. /* .clear = */ ggml_backend_cuda_buffer_clear,
  7737. };
  7738. // cuda buffer type
  7739. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7740. int device = (int) (intptr_t) buft->context;
  7741. ggml_cuda_set_device(device);
  7742. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  7743. void * dev_ptr;
  7744. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  7745. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  7746. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  7747. }
  7748. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7749. return 128;
  7750. UNUSED(buft);
  7751. }
  7752. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  7753. int64_t row_low = 0;
  7754. int64_t row_high = ggml_nrows(tensor);
  7755. int64_t nrows_split = row_high - row_low;
  7756. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7757. int64_t ne0 = tensor->ne[0];
  7758. if (ggml_is_quantized(tensor->type)) {
  7759. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7760. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7761. }
  7762. }
  7763. return size;
  7764. UNUSED(buft);
  7765. }
  7766. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  7767. return ggml_backend_is_cuda(backend);
  7768. UNUSED(buft);
  7769. }
  7770. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  7771. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  7772. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  7773. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  7774. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  7775. /* .is_host = */ nullptr,
  7776. };
  7777. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  7778. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  7779. static bool ggml_backend_cuda_buffer_type_initialized = false;
  7780. if (!ggml_backend_cuda_buffer_type_initialized) {
  7781. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  7782. ggml_backend_cuda_buffer_types[i] = {
  7783. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  7784. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  7785. };
  7786. }
  7787. ggml_backend_cuda_buffer_type_initialized = true;
  7788. }
  7789. return &ggml_backend_cuda_buffer_types[device];
  7790. }
  7791. // host buffer type
  7792. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7793. CUDA_CHECK(cudaFreeHost(buffer->context));
  7794. }
  7795. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7796. void * ptr;
  7797. CUDA_CHECK(cudaMallocHost(&ptr, size));
  7798. // FIXME: this is a hack to avoid having to implement a new buffer type
  7799. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7800. buffer->buft = buft;
  7801. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  7802. return buffer;
  7803. }
  7804. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  7805. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  7806. /* .iface = */ {
  7807. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  7808. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  7809. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7810. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  7811. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7812. },
  7813. /* .context = */ nullptr,
  7814. };
  7815. return &ggml_backend_cuda_buffer_type_host;
  7816. }
  7817. // backend
  7818. struct ggml_backend_context_cuda {
  7819. int device;
  7820. };
  7821. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  7822. return GGML_CUDA_NAME;
  7823. UNUSED(backend);
  7824. }
  7825. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  7826. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7827. delete cuda_ctx;
  7828. delete backend;
  7829. }
  7830. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  7831. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7832. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  7833. }
  7834. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7835. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7836. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7837. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7838. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  7839. }
  7840. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7841. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7842. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7843. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7844. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  7845. }
  7846. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  7847. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7848. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  7849. UNUSED(backend);
  7850. }
  7851. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7852. GGML_ASSERT(!"not implemented");
  7853. return nullptr;
  7854. UNUSED(backend);
  7855. UNUSED(cgraph);
  7856. }
  7857. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7858. GGML_ASSERT(!"not implemented");
  7859. UNUSED(backend);
  7860. UNUSED(plan);
  7861. }
  7862. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7863. GGML_ASSERT(!"not implemented");
  7864. UNUSED(backend);
  7865. UNUSED(plan);
  7866. }
  7867. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7868. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7869. ggml_cuda_set_main_device(cuda_ctx->device);
  7870. ggml_compute_params params = {};
  7871. params.type = GGML_TASK_COMPUTE;
  7872. params.ith = 0;
  7873. for (int i = 0; i < cgraph->n_nodes; i++) {
  7874. ggml_tensor * node = cgraph->nodes[i];
  7875. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  7876. continue;
  7877. assert(node->backend == GGML_BACKEND_GPU);
  7878. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7879. assert(node->extra != nullptr);
  7880. for (int j = 0; j < GGML_MAX_SRC; j++) {
  7881. if (node->src[j] != nullptr) {
  7882. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  7883. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7884. assert(node->src[j]->extra != nullptr);
  7885. }
  7886. }
  7887. bool ok = ggml_cuda_compute_forward(&params, node);
  7888. if (!ok) {
  7889. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  7890. }
  7891. GGML_ASSERT(ok);
  7892. #if 0
  7893. if (node->type == GGML_TYPE_F32) {
  7894. cudaDeviceSynchronize();
  7895. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  7896. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  7897. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  7898. ggml_type_name(node->src[0]->type),
  7899. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  7900. node->src[0]->name,
  7901. node->src[1] ? node->src[1]->name : "none");
  7902. double sum = 0.0;
  7903. double sq_sum = 0.0;
  7904. for (int i = 0; i < ggml_nelements(node); i++) {
  7905. printf("%f ", tmp[i]);
  7906. sum += tmp[i];
  7907. sq_sum += tmp[i]*tmp[i];
  7908. }
  7909. printf("\n");
  7910. printf("sum: %f, ", sum);
  7911. printf("sq_sum: %f\n", sq_sum);
  7912. }
  7913. #endif
  7914. }
  7915. UNUSED(backend);
  7916. }
  7917. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  7918. switch (op->op) {
  7919. case GGML_OP_UNARY:
  7920. switch (ggml_get_unary_op(op)) {
  7921. case GGML_UNARY_OP_GELU:
  7922. case GGML_UNARY_OP_SILU:
  7923. case GGML_UNARY_OP_RELU:
  7924. case GGML_UNARY_OP_GELU_QUICK:
  7925. case GGML_UNARY_OP_TANH:
  7926. return true;
  7927. default:
  7928. return false;
  7929. }
  7930. break;
  7931. case GGML_OP_MUL_MAT:
  7932. case GGML_OP_MUL_MAT_ID:
  7933. {
  7934. struct ggml_tensor * a;
  7935. struct ggml_tensor * b;
  7936. if (op->op == GGML_OP_MUL_MAT) {
  7937. a = op->src[0];
  7938. b = op->src[1];
  7939. } else {
  7940. a = op->src[2];
  7941. b = op->src[1];
  7942. }
  7943. if (a->ne[3] != b->ne[3]) {
  7944. return false;
  7945. }
  7946. return true;
  7947. } break;
  7948. case GGML_OP_GET_ROWS:
  7949. {
  7950. switch (op->src[0]->type) {
  7951. case GGML_TYPE_F16:
  7952. case GGML_TYPE_F32:
  7953. case GGML_TYPE_Q4_0:
  7954. case GGML_TYPE_Q4_1:
  7955. case GGML_TYPE_Q5_0:
  7956. case GGML_TYPE_Q5_1:
  7957. case GGML_TYPE_Q8_0:
  7958. return true;
  7959. default:
  7960. return false;
  7961. }
  7962. } break;
  7963. case GGML_OP_CPY:
  7964. {
  7965. ggml_type src0_type = op->src[0]->type;
  7966. ggml_type src1_type = op->src[1]->type;
  7967. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  7968. return true;
  7969. }
  7970. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  7971. return true;
  7972. }
  7973. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  7974. return true;
  7975. }
  7976. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  7977. return true;
  7978. }
  7979. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  7980. return true;
  7981. }
  7982. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  7983. return true;
  7984. }
  7985. return false;
  7986. } break;
  7987. case GGML_OP_NONE:
  7988. case GGML_OP_RESHAPE:
  7989. case GGML_OP_VIEW:
  7990. case GGML_OP_PERMUTE:
  7991. case GGML_OP_TRANSPOSE:
  7992. case GGML_OP_NORM:
  7993. case GGML_OP_REPEAT:
  7994. case GGML_OP_DUP:
  7995. case GGML_OP_ADD:
  7996. case GGML_OP_MUL:
  7997. case GGML_OP_DIV:
  7998. case GGML_OP_RMS_NORM:
  7999. case GGML_OP_SCALE:
  8000. case GGML_OP_SQR:
  8001. case GGML_OP_CLAMP:
  8002. case GGML_OP_CONT:
  8003. case GGML_OP_DIAG_MASK_INF:
  8004. case GGML_OP_SOFT_MAX:
  8005. case GGML_OP_ROPE:
  8006. case GGML_OP_ALIBI:
  8007. case GGML_OP_IM2COL:
  8008. case GGML_OP_SUM_ROWS:
  8009. case GGML_OP_ARGSORT:
  8010. case GGML_OP_ACC:
  8011. case GGML_OP_CONCAT:
  8012. case GGML_OP_GROUP_NORM:
  8013. case GGML_OP_UPSCALE:
  8014. case GGML_OP_PAD:
  8015. case GGML_OP_LEAKY_RELU:
  8016. return true;
  8017. default:
  8018. return false;
  8019. }
  8020. UNUSED(backend);
  8021. }
  8022. static ggml_backend_i cuda_backend_i = {
  8023. /* .get_name = */ ggml_backend_cuda_name,
  8024. /* .free = */ ggml_backend_cuda_free,
  8025. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8026. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8027. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8028. /* .cpy_tensor_from_async = */ NULL,
  8029. /* .cpy_tensor_to_async = */ NULL,
  8030. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8031. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8032. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8033. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8034. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8035. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8036. };
  8037. ggml_backend_t ggml_backend_cuda_init(int device) {
  8038. ggml_init_cublas(); // TODO: remove from ggml.c
  8039. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8040. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8041. return nullptr;
  8042. }
  8043. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8044. ggml_cuda_set_main_device(device);
  8045. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8046. /* .device = */ device
  8047. };
  8048. ggml_backend_t cuda_backend = new ggml_backend {
  8049. /* .interface = */ cuda_backend_i,
  8050. /* .context = */ ctx
  8051. };
  8052. return cuda_backend;
  8053. }
  8054. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8055. return backend->iface.get_name == ggml_backend_cuda_name;
  8056. }
  8057. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8058. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8059. return cuda_backend;
  8060. UNUSED(params);
  8061. }
  8062. extern "C" int ggml_backend_cuda_reg_devices();
  8063. int ggml_backend_cuda_reg_devices() {
  8064. int device_count = ggml_cuda_get_device_count();
  8065. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8066. for (int i = 0; i < device_count; i++) {
  8067. char name[128];
  8068. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8069. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8070. }
  8071. return device_count;
  8072. }