ggml-cuda.cu 369 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135
  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. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  88. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  89. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  90. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  91. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  92. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  93. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  94. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  95. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  96. #else
  97. #include <cuda_runtime.h>
  98. #include <cuda.h>
  99. #include <cublas_v2.h>
  100. #include <cuda_fp16.h>
  101. #if CUDART_VERSION < 11020
  102. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  103. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  104. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  105. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  106. #define cublasComputeType_t cudaDataType_t
  107. #endif // CUDART_VERSION < 11020
  108. #endif // defined(GGML_USE_HIPBLAS)
  109. #include "ggml-cuda.h"
  110. #include "ggml.h"
  111. #include "ggml-backend-impl.h"
  112. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  113. #define CC_VOLTA 700
  114. #define CC_OFFSET_AMD 1000000
  115. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  116. #define GGML_CUDA_MAX_NODES 8192
  117. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  118. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  119. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  120. // - 7B quantum model: +100-200 MB
  121. // - 13B quantum model: +200-400 MB
  122. //
  123. //#define GGML_CUDA_FORCE_MMQ
  124. // TODO: improve this to be correct for more hardware
  125. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  126. // probably other such cases, and not sure what happens on AMD hardware
  127. #if !defined(GGML_CUDA_FORCE_MMQ)
  128. #define CUDA_USE_TENSOR_CORES
  129. #endif
  130. // max batch size to use MMQ kernels when tensor cores are available
  131. #define MMQ_MAX_BATCH_SIZE 32
  132. #if defined(GGML_USE_HIPBLAS)
  133. #define __CUDA_ARCH__ 1300
  134. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  135. defined(__gfx1150__) || defined(__gfx1151__)
  136. #define RDNA3
  137. #endif
  138. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  139. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  140. #define RDNA2
  141. #endif
  142. #ifndef __has_builtin
  143. #define __has_builtin(x) 0
  144. #endif
  145. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  146. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  147. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  148. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  149. #if __has_builtin(__builtin_elementwise_sub_sat)
  150. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  151. return reinterpret_cast<const int&>(c);
  152. #else
  153. int8x4_t c;
  154. int16_t tmp;
  155. #pragma unroll
  156. for (int i = 0; i < 4; i++) {
  157. tmp = va[i] - vb[i];
  158. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  159. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  160. c[i] = tmp;
  161. }
  162. return reinterpret_cast<int&>(c);
  163. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  164. }
  165. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  166. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  167. c = __builtin_amdgcn_sdot4(a, b, c, false);
  168. #elif defined(__gfx1100__)
  169. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  170. #elif defined(__gfx1010__) || defined(__gfx900__)
  171. int tmp1;
  172. int tmp2;
  173. asm("\n \
  174. 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 \
  175. 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 \
  176. v_add3_u32 %0, %1, %2, %0 \n \
  177. 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 \
  178. 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 \
  179. v_add3_u32 %0, %1, %2, %0 \n \
  180. "
  181. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  182. : "v"(a), "v"(b)
  183. );
  184. #else
  185. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  186. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  187. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  188. #endif
  189. return c;
  190. }
  191. #endif // defined(GGML_USE_HIPBLAS)
  192. #if defined(_MSC_VER)
  193. #pragma warning(disable: 4244 4267) // possible loss of data
  194. #endif
  195. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  196. #if CUDART_VERSION >= 12000
  197. static const char * cublas_get_error_str(const cublasStatus_t err) {
  198. return cublasGetStatusString(err);
  199. }
  200. #else
  201. static const char * cublas_get_error_str(const cublasStatus_t err) {
  202. switch (err) {
  203. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  204. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  205. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  206. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  207. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  208. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  209. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  210. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  211. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  212. default: return "unknown error";
  213. }
  214. }
  215. #endif // CUDART_VERSION >= 12000
  216. [[noreturn]]
  217. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  218. fprintf(stderr, "CUDA error: %s: %s\n", stmt, msg);
  219. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  220. GGML_ASSERT(!"CUDA error");
  221. }
  222. #define CUDA_CHECK(err) do { auto err_ = (err); if (err_ != cudaSuccess) ggml_cuda_error(#err, __func__, __FILE__, __LINE__, cudaGetErrorString(err_)); } while (0)
  223. #define CUBLAS_CHECK(err) do { auto err_ = (err); if (err_ != CUBLAS_STATUS_SUCCESS) ggml_cuda_error(#err, __func__, __FILE__, __LINE__, cublas_get_error_str(err_)); } while (0)
  224. #if !defined(GGML_USE_HIPBLAS)
  225. static const char * cu_get_error_str(CUresult err) {
  226. const char * err_str;
  227. cuGetErrorString(err, &err_str);
  228. return err_str;
  229. }
  230. #define CU_CHECK(err) do { auto err_ = (err); if (err_ != CUDA_SUCCESS) ggml_cuda_error(#err, __func__, __FILE__, __LINE__, cu_get_error_str(err_)); } while (0)
  231. #endif
  232. #if CUDART_VERSION >= 11100
  233. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  234. #else
  235. #define GGML_CUDA_ASSUME(x)
  236. #endif // CUDART_VERSION >= 11100
  237. #ifdef GGML_CUDA_F16
  238. typedef half dfloat; // dequantize float
  239. typedef half2 dfloat2;
  240. #else
  241. typedef float dfloat; // dequantize float
  242. typedef float2 dfloat2;
  243. #endif //GGML_CUDA_F16
  244. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  245. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  246. int x32 = 0;
  247. x32 |= x16[0] << 0;
  248. x32 |= x16[1] << 16;
  249. return x32;
  250. }
  251. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  252. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  253. int x32 = 0;
  254. x32 |= x16[0] << 0;
  255. x32 |= x16[1] << 16;
  256. return x32;
  257. }
  258. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  259. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  260. }
  261. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  262. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  263. }
  264. template<typename T>
  265. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  266. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  267. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  268. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  269. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  270. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  271. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  272. typedef void (*ggml_cuda_op_mul_mat_t)(
  273. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  274. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  275. const int64_t src1_padded_row_size, const cudaStream_t & stream);
  276. typedef void (*ggml_cuda_op_flatten_t)(
  277. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  278. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream);
  279. // QK = number of values after dequantization
  280. // QR = QK / number of values before dequantization
  281. // QI = number of 32 bit integers before dequantization
  282. #define QK4_0 32
  283. #define QR4_0 2
  284. #define QI4_0 (QK4_0 / (4 * QR4_0))
  285. typedef struct {
  286. half d; // delta
  287. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  288. } block_q4_0;
  289. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  290. #define QK4_1 32
  291. #define QR4_1 2
  292. #define QI4_1 (QK4_1 / (4 * QR4_1))
  293. typedef struct {
  294. half2 dm; // dm.x = delta, dm.y = min
  295. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  296. } block_q4_1;
  297. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  298. #define QK5_0 32
  299. #define QR5_0 2
  300. #define QI5_0 (QK5_0 / (4 * QR5_0))
  301. typedef struct {
  302. half d; // delta
  303. uint8_t qh[4]; // 5-th bit of quants
  304. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  305. } block_q5_0;
  306. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  307. #define QK5_1 32
  308. #define QR5_1 2
  309. #define QI5_1 (QK5_1 / (4 * QR5_1))
  310. typedef struct {
  311. half2 dm; // dm.x = delta, dm.y = min
  312. uint8_t qh[4]; // 5-th bit of quants
  313. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  314. } block_q5_1;
  315. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  316. #define QK8_0 32
  317. #define QR8_0 1
  318. #define QI8_0 (QK8_0 / (4 * QR8_0))
  319. typedef struct {
  320. half d; // delta
  321. int8_t qs[QK8_0]; // quants
  322. } block_q8_0;
  323. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  324. #define QK8_1 32
  325. #define QR8_1 1
  326. #define QI8_1 (QK8_1 / (4 * QR8_1))
  327. typedef struct {
  328. half2 ds; // ds.x = delta, ds.y = sum
  329. int8_t qs[QK8_0]; // quants
  330. } block_q8_1;
  331. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  332. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  333. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  334. typedef void (*load_tiles_cuda_t)(
  335. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  336. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  337. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  338. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  339. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  340. //================================= k-quants
  341. #ifdef GGML_QKK_64
  342. #define QK_K 64
  343. #define K_SCALE_SIZE 4
  344. #else
  345. #define QK_K 256
  346. #define K_SCALE_SIZE 12
  347. #endif
  348. #define QR2_K 4
  349. #define QI2_K (QK_K / (4*QR2_K))
  350. typedef struct {
  351. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  352. uint8_t qs[QK_K/4]; // quants
  353. half2 dm; // super-block scale for quantized scales/mins
  354. } block_q2_K;
  355. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  356. #define QR3_K 4
  357. #define QI3_K (QK_K / (4*QR3_K))
  358. typedef struct {
  359. uint8_t hmask[QK_K/8]; // quants - high bit
  360. uint8_t qs[QK_K/4]; // quants - low 2 bits
  361. #ifdef GGML_QKK_64
  362. uint8_t scales[2]; // scales, quantized with 8 bits
  363. #else
  364. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  365. #endif
  366. half d; // super-block scale
  367. } block_q3_K;
  368. //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");
  369. #define QR4_K 2
  370. #define QI4_K (QK_K / (4*QR4_K))
  371. #ifdef GGML_QKK_64
  372. typedef struct {
  373. half dm[2]; // super-block scales/mins
  374. uint8_t scales[2]; // 4-bit block scales/mins
  375. uint8_t qs[QK_K/2]; // 4--bit quants
  376. } block_q4_K;
  377. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  378. #else
  379. typedef struct {
  380. half2 dm; // super-block scale for quantized scales/mins
  381. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  382. uint8_t qs[QK_K/2]; // 4--bit quants
  383. } block_q4_K;
  384. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  385. #endif
  386. #define QR5_K 2
  387. #define QI5_K (QK_K / (4*QR5_K))
  388. #ifdef GGML_QKK_64
  389. typedef struct {
  390. half d; // super-block scale
  391. int8_t scales[QK_K/16]; // block scales
  392. uint8_t qh[QK_K/8]; // quants, high bit
  393. uint8_t qs[QK_K/2]; // quants, low 4 bits
  394. } block_q5_K;
  395. 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");
  396. #else
  397. typedef struct {
  398. half2 dm; // super-block scale for quantized scales/mins
  399. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  400. uint8_t qh[QK_K/8]; // quants, high bit
  401. uint8_t qs[QK_K/2]; // quants, low 4 bits
  402. } block_q5_K;
  403. 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");
  404. #endif
  405. #define QR6_K 2
  406. #define QI6_K (QK_K / (4*QR6_K))
  407. typedef struct {
  408. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  409. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  410. int8_t scales[QK_K/16]; // scales
  411. half d; // delta
  412. } block_q6_K;
  413. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  414. #define WARP_SIZE 32
  415. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  416. #define CUDA_GELU_BLOCK_SIZE 256
  417. #define CUDA_SILU_BLOCK_SIZE 256
  418. #define CUDA_TANH_BLOCK_SIZE 256
  419. #define CUDA_RELU_BLOCK_SIZE 256
  420. #define CUDA_SQR_BLOCK_SIZE 256
  421. #define CUDA_CPY_BLOCK_SIZE 32
  422. #define CUDA_SCALE_BLOCK_SIZE 256
  423. #define CUDA_CLAMP_BLOCK_SIZE 256
  424. #define CUDA_ROPE_BLOCK_SIZE 256
  425. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  426. #define CUDA_ALIBI_BLOCK_SIZE 32
  427. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  428. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  429. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  430. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  431. #define CUDA_UPSCALE_BLOCK_SIZE 256
  432. #define CUDA_CONCAT_BLOCK_SIZE 256
  433. #define CUDA_PAD_BLOCK_SIZE 256
  434. #define CUDA_ACC_BLOCK_SIZE 256
  435. #define CUDA_IM2COL_BLOCK_SIZE 256
  436. // dmmv = dequantize_mul_mat_vec
  437. #ifndef GGML_CUDA_DMMV_X
  438. #define GGML_CUDA_DMMV_X 32
  439. #endif
  440. #ifndef GGML_CUDA_MMV_Y
  441. #define GGML_CUDA_MMV_Y 1
  442. #endif
  443. #ifndef K_QUANTS_PER_ITERATION
  444. #define K_QUANTS_PER_ITERATION 2
  445. #else
  446. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  447. #endif
  448. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  449. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  450. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  451. #define MUL_MAT_SRC1_COL_STRIDE 128
  452. #define MAX_STREAMS 8
  453. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  454. struct ggml_tensor_extra_gpu {
  455. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  456. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  457. };
  458. // this is faster on Windows
  459. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  460. inline cudaError_t ggml_cuda_set_device(const int device) {
  461. int current_device;
  462. CUDA_CHECK(cudaGetDevice(&current_device));
  463. if (device == current_device) {
  464. return cudaSuccess;
  465. }
  466. return cudaSetDevice(device);
  467. }
  468. static int g_device_count = -1;
  469. static int g_main_device = 0;
  470. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  471. struct cuda_device_capabilities {
  472. int cc; // compute capability
  473. bool vmm; // virtual memory support
  474. size_t vmm_granularity; // granularity of virtual memory
  475. };
  476. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, false, 0} };
  477. static void * g_scratch_buffer = nullptr;
  478. static size_t g_scratch_size = 0; // disabled by default
  479. static size_t g_scratch_offset = 0;
  480. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  481. [[noreturn]]
  482. static __device__ void bad_arch() {
  483. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  484. __trap();
  485. (void) bad_arch; // suppress unused function warning
  486. }
  487. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  488. #pragma unroll
  489. for (int mask = 16; mask > 0; mask >>= 1) {
  490. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  491. }
  492. return x;
  493. }
  494. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  495. #pragma unroll
  496. for (int mask = 16; mask > 0; mask >>= 1) {
  497. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  498. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  499. }
  500. return a;
  501. }
  502. static __device__ __forceinline__ float warp_reduce_max(float x) {
  503. #pragma unroll
  504. for (int mask = 16; mask > 0; mask >>= 1) {
  505. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  506. }
  507. return x;
  508. }
  509. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  510. return b;
  511. }
  512. static __device__ __forceinline__ float op_add(const float a, const float b) {
  513. return a + b;
  514. }
  515. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  516. return a * b;
  517. }
  518. static __device__ __forceinline__ float op_div(const float a, const float b) {
  519. return a / b;
  520. }
  521. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  522. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  523. int ne0, int ne1, int ne2, int ne3,
  524. int ne10, int ne11, int ne12, int ne13,
  525. /*int s0, */ int s1, int s2, int s3,
  526. /*int s10,*/ int s11, int s12, int s13) {
  527. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  528. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  529. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  530. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  531. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  532. return;
  533. }
  534. const int i11 = i1 % ne11;
  535. const int i12 = i2 % ne12;
  536. const int i13 = i3 % ne13;
  537. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  538. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  539. const size_t i_dst = i_src0;
  540. const src0_t * src0_row = src0 + i_src0;
  541. const src1_t * src1_row = src1 + i_src1;
  542. dst_t * dst_row = dst + i_dst;
  543. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  544. const int i10 = i0 % ne10;
  545. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  546. }
  547. }
  548. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  549. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  550. int ne0, int ne1, int ne2, int ne3,
  551. int ne10, int ne11, int ne12, int ne13,
  552. /*int s0, */ int s1, int s2, int s3,
  553. /*int s10,*/ int s11, int s12, int s13) {
  554. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  555. const int i3 = i/(ne2*ne1*ne0);
  556. const int i2 = (i/(ne1*ne0)) % ne2;
  557. const int i1 = (i/ne0) % ne1;
  558. const int i0 = i % ne0;
  559. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  560. return;
  561. }
  562. const int i11 = i1 % ne11;
  563. const int i12 = i2 % ne12;
  564. const int i13 = i3 % ne13;
  565. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  566. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  567. const size_t i_dst = i_src0;
  568. const src0_t * src0_row = src0 + i_src0;
  569. const src1_t * src1_row = src1 + i_src1;
  570. dst_t * dst_row = dst + i_dst;
  571. const int i10 = i0 % ne10;
  572. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  573. }
  574. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  575. const int ne10, const int ne11, const int ne12,
  576. const int nb1, const int nb2, int offset) {
  577. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  578. if (i >= ne) {
  579. return;
  580. }
  581. int src1_idx = i - offset;
  582. int oz = src1_idx / nb2;
  583. int oy = (src1_idx - (oz * nb2)) / nb1;
  584. int ox = src1_idx % nb1;
  585. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  586. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  587. } else {
  588. dst[i] = x[i];
  589. }
  590. }
  591. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  592. const float GELU_COEF_A = 0.044715f;
  593. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  594. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  595. if (i >= k) {
  596. return;
  597. }
  598. float xi = x[i];
  599. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  600. }
  601. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  602. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  603. if (i >= k) {
  604. return;
  605. }
  606. dst[i] = x[i] / (1.0f + expf(-x[i]));
  607. }
  608. static __global__ void gelu_quick_f32(const float *x, float *dst, int k) {
  609. const float GELU_QUICK_COEF = -1.702f;
  610. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  611. if (i >= k) {
  612. return;
  613. }
  614. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  615. }
  616. static __global__ void tanh_f32(const float *x, float *dst, int k) {
  617. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  618. if (i >= k) {
  619. return;
  620. }
  621. dst[i] = tanhf(x[i]);
  622. }
  623. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  624. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  625. if (i >= k) {
  626. return;
  627. }
  628. dst[i] = fmaxf(x[i], 0);
  629. }
  630. static __global__ void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope) {
  631. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  632. if (i >= k) {
  633. return;
  634. }
  635. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  636. }
  637. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  638. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  639. if (i >= k) {
  640. return;
  641. }
  642. dst[i] = x[i] * x[i];
  643. }
  644. template <int block_size>
  645. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  646. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  647. const int tid = threadIdx.x;
  648. float2 mean_var = make_float2(0.f, 0.f);
  649. for (int col = tid; col < ncols; col += block_size) {
  650. const float xi = x[row*ncols + col];
  651. mean_var.x += xi;
  652. mean_var.y += xi * xi;
  653. }
  654. // sum up partial sums
  655. mean_var = warp_reduce_sum(mean_var);
  656. if (block_size > WARP_SIZE) {
  657. __shared__ float2 s_sum[32];
  658. int warp_id = threadIdx.x / WARP_SIZE;
  659. int lane_id = threadIdx.x % WARP_SIZE;
  660. if (lane_id == 0) {
  661. s_sum[warp_id] = mean_var;
  662. }
  663. __syncthreads();
  664. mean_var = s_sum[lane_id];
  665. mean_var = warp_reduce_sum(mean_var);
  666. }
  667. const float mean = mean_var.x / ncols;
  668. const float var = mean_var.y / ncols - mean * mean;
  669. const float inv_std = rsqrtf(var + eps);
  670. for (int col = tid; col < ncols; col += block_size) {
  671. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  672. }
  673. }
  674. static __global__ void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02) {
  675. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  676. if (nidx >= ne0) {
  677. return;
  678. }
  679. // operation
  680. int offset_dst =
  681. nidx +
  682. blockIdx.y * ne0 +
  683. blockIdx.z * ne0 * gridDim.y;
  684. if (blockIdx.z < ne02) { // src0
  685. int offset_src =
  686. nidx +
  687. blockIdx.y * ne0 +
  688. blockIdx.z * ne0 * gridDim.y;
  689. dst[offset_dst] = x[offset_src];
  690. } else {
  691. int offset_src =
  692. nidx +
  693. blockIdx.y * ne0 +
  694. (blockIdx.z - ne02) * ne0 * gridDim.y;
  695. dst[offset_dst] = y[offset_src];
  696. }
  697. }
  698. static __global__ void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor) {
  699. int ne0 = ne00 * scale_factor;
  700. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  701. if (nidx >= ne0) {
  702. return;
  703. }
  704. // operation
  705. int i00 = nidx / scale_factor;
  706. int i01 = blockIdx.y / scale_factor;
  707. int offset_src =
  708. i00 +
  709. i01 * ne00 +
  710. blockIdx.z * nb02;
  711. int offset_dst =
  712. nidx +
  713. blockIdx.y * ne0 +
  714. blockIdx.z * ne0 * gridDim.y;
  715. dst[offset_dst] = x[offset_src];
  716. }
  717. static __global__ void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  718. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  719. if (nidx >= ne0) {
  720. return;
  721. }
  722. // operation
  723. int offset_dst =
  724. nidx +
  725. blockIdx.y * ne0 +
  726. blockIdx.z * ne0 * gridDim.y;
  727. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  728. int offset_src =
  729. nidx +
  730. blockIdx.y * ne00 +
  731. blockIdx.z * ne00 * ne01;
  732. dst[offset_dst] = x[offset_src];
  733. } else {
  734. dst[offset_dst] = 0.0f;
  735. }
  736. }
  737. template <int block_size>
  738. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  739. int start = blockIdx.x * group_size;
  740. int end = start + group_size;
  741. start += threadIdx.x;
  742. if (end >= ne_elements) {
  743. end = ne_elements;
  744. }
  745. float tmp = 0.0f; // partial sum for thread in warp
  746. for (int j = start; j < end; j += block_size) {
  747. tmp += x[j];
  748. }
  749. tmp = warp_reduce_sum(tmp);
  750. if (block_size > WARP_SIZE) {
  751. __shared__ float s_sum[32];
  752. int warp_id = threadIdx.x / WARP_SIZE;
  753. int lane_id = threadIdx.x % WARP_SIZE;
  754. if (lane_id == 0) {
  755. s_sum[warp_id] = tmp;
  756. }
  757. __syncthreads();
  758. tmp = s_sum[lane_id];
  759. tmp = warp_reduce_sum(tmp);
  760. }
  761. float mean = tmp / group_size;
  762. tmp = 0.0f;
  763. for (int j = start; j < end; j += block_size) {
  764. float xi = x[j] - mean;
  765. dst[j] = xi;
  766. tmp += xi * xi;
  767. }
  768. tmp = warp_reduce_sum(tmp);
  769. if (block_size > WARP_SIZE) {
  770. __shared__ float s_sum[32];
  771. int warp_id = threadIdx.x / WARP_SIZE;
  772. int lane_id = threadIdx.x % WARP_SIZE;
  773. if (lane_id == 0) {
  774. s_sum[warp_id] = tmp;
  775. }
  776. __syncthreads();
  777. tmp = s_sum[lane_id];
  778. tmp = warp_reduce_sum(tmp);
  779. }
  780. float variance = tmp / group_size;
  781. float scale = rsqrtf(variance + eps);
  782. for (int j = start; j < end; j += block_size) {
  783. dst[j] *= scale;
  784. }
  785. }
  786. template <int block_size>
  787. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  788. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  789. const int tid = threadIdx.x;
  790. float tmp = 0.0f; // partial sum for thread in warp
  791. for (int col = tid; col < ncols; col += block_size) {
  792. const float xi = x[row*ncols + col];
  793. tmp += xi * xi;
  794. }
  795. // sum up partial sums
  796. tmp = warp_reduce_sum(tmp);
  797. if (block_size > WARP_SIZE) {
  798. __shared__ float s_sum[32];
  799. int warp_id = threadIdx.x / WARP_SIZE;
  800. int lane_id = threadIdx.x % WARP_SIZE;
  801. if (lane_id == 0) {
  802. s_sum[warp_id] = tmp;
  803. }
  804. __syncthreads();
  805. tmp = s_sum[lane_id];
  806. tmp = warp_reduce_sum(tmp);
  807. }
  808. const float mean = tmp / ncols;
  809. const float scale = rsqrtf(mean + eps);
  810. for (int col = tid; col < ncols; col += block_size) {
  811. dst[row*ncols + col] = scale * x[row*ncols + col];
  812. }
  813. }
  814. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  815. const block_q4_0 * x = (const block_q4_0 *) vx;
  816. const dfloat d = x[ib].d;
  817. const int vui = x[ib].qs[iqs];
  818. v.x = vui & 0xF;
  819. v.y = vui >> 4;
  820. #ifdef GGML_CUDA_F16
  821. v = __hsub2(v, {8.0f, 8.0f});
  822. v = __hmul2(v, {d, d});
  823. #else
  824. v.x = (v.x - 8.0f) * d;
  825. v.y = (v.y - 8.0f) * d;
  826. #endif // GGML_CUDA_F16
  827. }
  828. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  829. const block_q4_1 * x = (const block_q4_1 *) vx;
  830. const dfloat d = __low2half(x[ib].dm);
  831. const dfloat m = __high2half(x[ib].dm);
  832. const int vui = x[ib].qs[iqs];
  833. v.x = vui & 0xF;
  834. v.y = vui >> 4;
  835. #ifdef GGML_CUDA_F16
  836. v = __hmul2(v, {d, d});
  837. v = __hadd2(v, {m, m});
  838. #else
  839. v.x = (v.x * d) + m;
  840. v.y = (v.y * d) + m;
  841. #endif // GGML_CUDA_F16
  842. }
  843. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  844. const block_q5_0 * x = (const block_q5_0 *) vx;
  845. const dfloat d = x[ib].d;
  846. uint32_t qh;
  847. memcpy(&qh, x[ib].qh, sizeof(qh));
  848. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  849. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  850. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  851. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  852. #ifdef GGML_CUDA_F16
  853. v = __hsub2(v, {16.0f, 16.0f});
  854. v = __hmul2(v, {d, d});
  855. #else
  856. v.x = (v.x - 16.0f) * d;
  857. v.y = (v.y - 16.0f) * d;
  858. #endif // GGML_CUDA_F16
  859. }
  860. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  861. const block_q5_1 * x = (const block_q5_1 *) vx;
  862. const dfloat d = __low2half(x[ib].dm);
  863. const dfloat m = __high2half(x[ib].dm);
  864. uint32_t qh;
  865. memcpy(&qh, x[ib].qh, sizeof(qh));
  866. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  867. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  868. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  869. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  870. #ifdef GGML_CUDA_F16
  871. v = __hmul2(v, {d, d});
  872. v = __hadd2(v, {m, m});
  873. #else
  874. v.x = (v.x * d) + m;
  875. v.y = (v.y * d) + m;
  876. #endif // GGML_CUDA_F16
  877. }
  878. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  879. const block_q8_0 * x = (const block_q8_0 *) vx;
  880. const dfloat d = x[ib].d;
  881. v.x = x[ib].qs[iqs + 0];
  882. v.y = x[ib].qs[iqs + 1];
  883. #ifdef GGML_CUDA_F16
  884. v = __hmul2(v, {d, d});
  885. #else
  886. v.x *= d;
  887. v.y *= d;
  888. #endif // GGML_CUDA_F16
  889. }
  890. //================================== k-quants
  891. template<typename dst_t>
  892. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  893. const int i = blockIdx.x;
  894. const block_q2_K * x = (const block_q2_K *) vx;
  895. const int tid = threadIdx.x;
  896. #if QK_K == 256
  897. const int n = tid/32;
  898. const int l = tid - 32*n;
  899. const int is = 8*n + l/16;
  900. const uint8_t q = x[i].qs[32*n + l];
  901. dst_t * y = yy + i*QK_K + 128*n;
  902. float dall = __low2half(x[i].dm);
  903. float dmin = __high2half(x[i].dm);
  904. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  905. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  906. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  907. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  908. #else
  909. const int is = tid/16; // 0 or 1
  910. const int il = tid%16; // 0...15
  911. const uint8_t q = x[i].qs[il] >> (2*is);
  912. dst_t * y = yy + i*QK_K + 16*is + il;
  913. float dall = __low2half(x[i].dm);
  914. float dmin = __high2half(x[i].dm);
  915. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  916. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  917. #endif
  918. }
  919. template<typename dst_t>
  920. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  921. const int i = blockIdx.x;
  922. const block_q3_K * x = (const block_q3_K *) vx;
  923. #if QK_K == 256
  924. const int r = threadIdx.x/4;
  925. const int tid = r/2;
  926. const int is0 = r%2;
  927. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  928. const int n = tid / 4;
  929. const int j = tid - 4*n;
  930. uint8_t m = 1 << (4*n + j);
  931. int is = 8*n + 2*j + is0;
  932. int shift = 2*j;
  933. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  934. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  935. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  936. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  937. float d_all = x[i].d;
  938. float dl = d_all * (us - 32);
  939. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  940. const uint8_t * q = x[i].qs + 32*n;
  941. const uint8_t * hm = x[i].hmask;
  942. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  943. #else
  944. const int tid = threadIdx.x;
  945. const int is = tid/16; // 0 or 1
  946. const int il = tid%16; // 0...15
  947. const int im = il/8; // 0...1
  948. const int in = il%8; // 0...7
  949. dst_t * y = yy + i*QK_K + 16*is + il;
  950. const uint8_t q = x[i].qs[il] >> (2*is);
  951. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  952. const float d = (float)x[i].d;
  953. if (is == 0) {
  954. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  955. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  956. } else {
  957. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  958. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  959. }
  960. #endif
  961. }
  962. #if QK_K == 256
  963. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  964. if (j < 4) {
  965. d = q[j] & 63; m = q[j + 4] & 63;
  966. } else {
  967. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  968. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  969. }
  970. }
  971. #endif
  972. template<typename dst_t>
  973. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  974. const block_q4_K * x = (const block_q4_K *) vx;
  975. const int i = blockIdx.x;
  976. #if QK_K == 256
  977. // assume 32 threads
  978. const int tid = threadIdx.x;
  979. const int il = tid/8;
  980. const int ir = tid%8;
  981. const int is = 2*il;
  982. const int n = 4;
  983. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  984. const float dall = __low2half(x[i].dm);
  985. const float dmin = __high2half(x[i].dm);
  986. const uint8_t * q = x[i].qs + 32*il + n*ir;
  987. uint8_t sc, m;
  988. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  989. const float d1 = dall * sc; const float m1 = dmin * m;
  990. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  991. const float d2 = dall * sc; const float m2 = dmin * m;
  992. for (int l = 0; l < n; ++l) {
  993. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  994. y[l +32] = d2 * (q[l] >> 4) - m2;
  995. }
  996. #else
  997. const int tid = threadIdx.x;
  998. const uint8_t * q = x[i].qs;
  999. dst_t * y = yy + i*QK_K;
  1000. const float d = (float)x[i].dm[0];
  1001. const float m = (float)x[i].dm[1];
  1002. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1003. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1004. #endif
  1005. }
  1006. template<typename dst_t>
  1007. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1008. const block_q5_K * x = (const block_q5_K *) vx;
  1009. const int i = blockIdx.x;
  1010. #if QK_K == 256
  1011. // assume 64 threads - this is very slightly better than the one below
  1012. const int tid = threadIdx.x;
  1013. const int il = tid/16; // il is in 0...3
  1014. const int ir = tid%16; // ir is in 0...15
  1015. const int is = 2*il; // is is in 0...6
  1016. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1017. const float dall = __low2half(x[i].dm);
  1018. const float dmin = __high2half(x[i].dm);
  1019. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1020. const uint8_t * qh = x[i].qh + 2*ir;
  1021. uint8_t sc, m;
  1022. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1023. const float d1 = dall * sc; const float m1 = dmin * m;
  1024. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1025. const float d2 = dall * sc; const float m2 = dmin * m;
  1026. uint8_t hm = 1 << (2*il);
  1027. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1028. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1029. hm <<= 1;
  1030. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1031. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1032. #else
  1033. const int tid = threadIdx.x;
  1034. const uint8_t q = x[i].qs[tid];
  1035. const int im = tid/8; // 0...3
  1036. const int in = tid%8; // 0...7
  1037. const int is = tid/16; // 0 or 1
  1038. const uint8_t h = x[i].qh[in] >> im;
  1039. const float d = x[i].d;
  1040. dst_t * y = yy + i*QK_K + tid;
  1041. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1042. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1043. #endif
  1044. }
  1045. template<typename dst_t>
  1046. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1047. const block_q6_K * x = (const block_q6_K *) vx;
  1048. const int i = blockIdx.x;
  1049. #if QK_K == 256
  1050. // assume 64 threads - this is very slightly better than the one below
  1051. const int tid = threadIdx.x;
  1052. const int ip = tid/32; // ip is 0 or 1
  1053. const int il = tid - 32*ip; // 0...32
  1054. const int is = 8*ip + il/16;
  1055. dst_t * y = yy + i*QK_K + 128*ip + il;
  1056. const float d = x[i].d;
  1057. const uint8_t * ql = x[i].ql + 64*ip + il;
  1058. const uint8_t qh = x[i].qh[32*ip + il];
  1059. const int8_t * sc = x[i].scales + is;
  1060. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1061. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1062. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1063. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1064. #else
  1065. // assume 32 threads
  1066. const int tid = threadIdx.x;
  1067. const int ip = tid/16; // 0 or 1
  1068. const int il = tid - 16*ip; // 0...15
  1069. dst_t * y = yy + i*QK_K + 16*ip + il;
  1070. const float d = x[i].d;
  1071. const uint8_t ql = x[i].ql[16*ip + il];
  1072. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1073. const int8_t * sc = x[i].scales;
  1074. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1075. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1076. #endif
  1077. }
  1078. 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) {
  1079. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1080. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1081. if (row > nrows) return;
  1082. const int num_blocks_per_row = ncols / QK_K;
  1083. const int ib0 = row*num_blocks_per_row;
  1084. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1085. float tmp = 0; // partial sum for thread in warp
  1086. #if QK_K == 256
  1087. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1088. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1089. const int step = 16/K_QUANTS_PER_ITERATION;
  1090. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1091. const int in = tid - step*im; // 0...15 or 0...7
  1092. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1093. const int q_offset = 32*im + l0;
  1094. const int s_offset = 8*im;
  1095. const int y_offset = 128*im + l0;
  1096. uint32_t aux[4];
  1097. const uint8_t * d = (const uint8_t *)aux;
  1098. const uint8_t * m = (const uint8_t *)(aux + 2);
  1099. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1100. const float * y = yy + i * QK_K + y_offset;
  1101. const uint8_t * q = x[i].qs + q_offset;
  1102. const float dall = __low2half(x[i].dm);
  1103. const float dmin = __high2half(x[i].dm);
  1104. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1105. aux[0] = a[0] & 0x0f0f0f0f;
  1106. aux[1] = a[1] & 0x0f0f0f0f;
  1107. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1108. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1109. float sum1 = 0, sum2 = 0;
  1110. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1111. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1112. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1113. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1114. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1115. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1116. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1117. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1118. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1119. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1120. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1121. }
  1122. tmp += dall * sum1 - dmin * sum2;
  1123. }
  1124. #else
  1125. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1126. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1127. const int offset = tid * K_QUANTS_PER_ITERATION;
  1128. uint32_t uaux[2];
  1129. const uint8_t * d = (const uint8_t *)uaux;
  1130. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1131. const float * y = yy + i * QK_K + offset;
  1132. const uint8_t * q = x[i].qs + offset;
  1133. const uint32_t * s = (const uint32_t *)x[i].scales;
  1134. uaux[0] = s[0] & 0x0f0f0f0f;
  1135. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1136. const float2 dall = __half22float2(x[i].dm);
  1137. float sum1 = 0, sum2 = 0;
  1138. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1139. const uint8_t ql = q[l];
  1140. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1141. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1142. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1143. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1144. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1145. }
  1146. tmp += dall.x * sum1 - dall.y * sum2;
  1147. }
  1148. #endif
  1149. // sum up partial sums and write back result
  1150. #pragma unroll
  1151. for (int mask = 16; mask > 0; mask >>= 1) {
  1152. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1153. }
  1154. if (threadIdx.x == 0) {
  1155. dst[row] = tmp;
  1156. }
  1157. }
  1158. 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) {
  1159. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1160. if (row > nrows) return;
  1161. const int num_blocks_per_row = ncols / QK_K;
  1162. const int ib0 = row*num_blocks_per_row;
  1163. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1164. float tmp = 0; // partial sum for thread in warp
  1165. #if QK_K == 256
  1166. const uint16_t kmask1 = 0x0303;
  1167. const uint16_t kmask2 = 0x0f0f;
  1168. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1169. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1170. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1171. const int step = 16/K_QUANTS_PER_ITERATION;
  1172. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1173. const int in = tid - step*im; // 0....15 or 0...7
  1174. const uint8_t m = 1 << (4*im);
  1175. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1176. const int q_offset = 32*im + l0;
  1177. const int y_offset = 128*im + l0;
  1178. uint16_t utmp[4];
  1179. const int8_t * s = (const int8_t *)utmp;
  1180. const uint16_t s_shift = 4*im;
  1181. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1182. const float * y = yy + i * QK_K + y_offset;
  1183. const uint8_t * q = x[i].qs + q_offset;
  1184. const uint8_t * h = x[i].hmask + l0;
  1185. const uint16_t * a = (const uint16_t *)x[i].scales;
  1186. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1187. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1188. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1189. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1190. const float d = x[i].d;
  1191. float sum = 0;
  1192. for (int l = 0; l < n; ++l) {
  1193. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1194. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1195. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1196. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1197. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1198. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1199. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1200. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1201. }
  1202. tmp += d * sum;
  1203. }
  1204. #else
  1205. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1206. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1207. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1208. const int in = offset/8; // 0 or 1
  1209. const int im = offset%8; // 0...7
  1210. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1211. const float * y = yy + i * QK_K + offset;
  1212. const uint8_t * q = x[i].qs + offset;
  1213. const uint8_t * s = x[i].scales;
  1214. const float dall = (float)x[i].d;
  1215. float sum = 0;
  1216. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1217. const uint8_t hl = x[i].hmask[im+l] >> in;
  1218. const uint8_t ql = q[l];
  1219. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1220. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1221. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1222. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1223. }
  1224. tmp += sum;
  1225. }
  1226. #endif
  1227. // sum up partial sums and write back result
  1228. #pragma unroll
  1229. for (int mask = 16; mask > 0; mask >>= 1) {
  1230. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1231. }
  1232. if (threadIdx.x == 0) {
  1233. dst[row] = tmp;
  1234. }
  1235. }
  1236. 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) {
  1237. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1238. if (row > nrows) return;
  1239. const int num_blocks_per_row = ncols / QK_K;
  1240. const int ib0 = row*num_blocks_per_row;
  1241. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1242. #if QK_K == 256
  1243. const uint16_t kmask1 = 0x3f3f;
  1244. const uint16_t kmask2 = 0x0f0f;
  1245. const uint16_t kmask3 = 0xc0c0;
  1246. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1247. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1248. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1249. const int il = tid/step; // 0...3
  1250. const int ir = tid - step*il; // 0...7 or 0...3
  1251. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1252. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1253. const int in = il%2;
  1254. const int l0 = n*(2*ir + in);
  1255. const int q_offset = 32*im + l0;
  1256. const int y_offset = 64*im + l0;
  1257. uint16_t aux[4];
  1258. const uint8_t * sc = (const uint8_t *)aux;
  1259. #if K_QUANTS_PER_ITERATION == 2
  1260. uint32_t q32[4];
  1261. const uint8_t * q4 = (const uint8_t *)q32;
  1262. #else
  1263. uint16_t q16[4];
  1264. const uint8_t * q4 = (const uint8_t *)q16;
  1265. #endif
  1266. float tmp = 0; // partial sum for thread in warp
  1267. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1268. const float * y1 = yy + i*QK_K + y_offset;
  1269. const float * y2 = y1 + 128;
  1270. const float dall = __low2half(x[i].dm);
  1271. const float dmin = __high2half(x[i].dm);
  1272. const uint16_t * a = (const uint16_t *)x[i].scales;
  1273. aux[0] = a[im+0] & kmask1;
  1274. aux[1] = a[im+2] & kmask1;
  1275. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1276. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1277. #if K_QUANTS_PER_ITERATION == 2
  1278. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1279. const uint32_t * q2 = q1 + 16;
  1280. q32[0] = q1[0] & 0x0f0f0f0f;
  1281. q32[1] = q1[0] & 0xf0f0f0f0;
  1282. q32[2] = q2[0] & 0x0f0f0f0f;
  1283. q32[3] = q2[0] & 0xf0f0f0f0;
  1284. float4 s = {0.f, 0.f, 0.f, 0.f};
  1285. float smin = 0;
  1286. for (int l = 0; l < 4; ++l) {
  1287. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1288. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1289. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1290. }
  1291. 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;
  1292. #else
  1293. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1294. const uint16_t * q2 = q1 + 32;
  1295. q16[0] = q1[0] & 0x0f0f;
  1296. q16[1] = q1[0] & 0xf0f0;
  1297. q16[2] = q2[0] & 0x0f0f;
  1298. q16[3] = q2[0] & 0xf0f0;
  1299. float4 s = {0.f, 0.f, 0.f, 0.f};
  1300. float smin = 0;
  1301. for (int l = 0; l < 2; ++l) {
  1302. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1303. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1304. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1305. }
  1306. 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;
  1307. #endif
  1308. }
  1309. #else
  1310. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1311. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1312. const int step = tid * K_QUANTS_PER_ITERATION;
  1313. uint16_t aux16[2];
  1314. const uint8_t * s = (const uint8_t *)aux16;
  1315. float tmp = 0;
  1316. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1317. const uint8_t * q = x[i].qs + step;
  1318. const float * y = yy + i*QK_K + step;
  1319. const uint16_t * a = (const uint16_t *)x[i].scales;
  1320. aux16[0] = a[0] & 0x0f0f;
  1321. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1322. const float d = (float)x[i].dm[0];
  1323. const float m = (float)x[i].dm[1];
  1324. float sum = 0.f;
  1325. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1326. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1327. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1328. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1329. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1330. }
  1331. tmp += sum;
  1332. }
  1333. #endif
  1334. // sum up partial sums and write back result
  1335. #pragma unroll
  1336. for (int mask = 16; mask > 0; mask >>= 1) {
  1337. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1338. }
  1339. if (tid == 0) {
  1340. dst[row] = tmp;
  1341. }
  1342. }
  1343. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1344. const int row = blockIdx.x;
  1345. const int num_blocks_per_row = ncols / QK_K;
  1346. const int ib0 = row*num_blocks_per_row;
  1347. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1348. float tmp = 0; // partial sum for thread in warp
  1349. #if QK_K == 256
  1350. const uint16_t kmask1 = 0x3f3f;
  1351. const uint16_t kmask2 = 0x0f0f;
  1352. const uint16_t kmask3 = 0xc0c0;
  1353. const int tid = threadIdx.x/2; // 0...15
  1354. const int ix = threadIdx.x%2;
  1355. const int il = tid/4; // 0...3
  1356. const int ir = tid - 4*il;// 0...3
  1357. const int n = 2;
  1358. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1359. const int in = il%2;
  1360. const int l0 = n*(2*ir + in);
  1361. const int q_offset = 32*im + l0;
  1362. const int y_offset = 64*im + l0;
  1363. const uint8_t hm1 = 1 << (2*im);
  1364. const uint8_t hm2 = hm1 << 4;
  1365. uint16_t aux[4];
  1366. const uint8_t * sc = (const uint8_t *)aux;
  1367. uint16_t q16[8];
  1368. const uint8_t * q4 = (const uint8_t *)q16;
  1369. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1370. const uint8_t * ql1 = x[i].qs + q_offset;
  1371. const uint8_t * qh = x[i].qh + l0;
  1372. const float * y1 = yy + i*QK_K + y_offset;
  1373. const float * y2 = y1 + 128;
  1374. const float dall = __low2half(x[i].dm);
  1375. const float dmin = __high2half(x[i].dm);
  1376. const uint16_t * a = (const uint16_t *)x[i].scales;
  1377. aux[0] = a[im+0] & kmask1;
  1378. aux[1] = a[im+2] & kmask1;
  1379. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1380. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1381. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1382. float smin = 0;
  1383. const uint16_t * q1 = (const uint16_t *)ql1;
  1384. const uint16_t * q2 = q1 + 32;
  1385. q16[0] = q1[0] & 0x0f0f;
  1386. q16[1] = q1[8] & 0x0f0f;
  1387. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1388. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1389. q16[4] = q2[0] & 0x0f0f;
  1390. q16[5] = q2[8] & 0x0f0f;
  1391. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1392. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1393. for (int l = 0; l < n; ++l) {
  1394. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1395. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1396. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1397. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1398. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1399. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1400. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1401. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1402. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1403. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1404. }
  1405. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1406. }
  1407. #else
  1408. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1409. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1410. const int step = tid * K_QUANTS_PER_ITERATION;
  1411. const int im = step/8;
  1412. const int in = step%8;
  1413. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1414. const uint8_t * q = x[i].qs + step;
  1415. const int8_t * s = x[i].scales;
  1416. const float * y = yy + i*QK_K + step;
  1417. const float d = x[i].d;
  1418. float sum = 0.f;
  1419. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1420. const uint8_t h = x[i].qh[in+j] >> im;
  1421. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1422. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1423. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1424. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1425. }
  1426. tmp += sum;
  1427. }
  1428. #endif
  1429. // sum up partial sums and write back result
  1430. #pragma unroll
  1431. for (int mask = 16; mask > 0; mask >>= 1) {
  1432. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1433. }
  1434. if (threadIdx.x == 0) {
  1435. dst[row] = tmp;
  1436. }
  1437. }
  1438. 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) {
  1439. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1440. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1441. if (row > nrows) return;
  1442. const int num_blocks_per_row = ncols / QK_K;
  1443. const int ib0 = row*num_blocks_per_row;
  1444. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1445. #if QK_K == 256
  1446. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1447. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1448. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1449. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1450. const int in = tid - step*im; // 0...15 or 0...7
  1451. #if K_QUANTS_PER_ITERATION == 1
  1452. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1453. const int is = 0;
  1454. #else
  1455. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1456. const int is = in / 4;
  1457. #endif
  1458. const int ql_offset = 64*im + l0;
  1459. const int qh_offset = 32*im + l0;
  1460. const int s_offset = 8*im + is;
  1461. const int y_offset = 128*im + l0;
  1462. float tmp = 0; // partial sum for thread in warp
  1463. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1464. const float * y = yy + i * QK_K + y_offset;
  1465. const uint8_t * ql = x[i].ql + ql_offset;
  1466. const uint8_t * qh = x[i].qh + qh_offset;
  1467. const int8_t * s = x[i].scales + s_offset;
  1468. const float d = x[i].d;
  1469. #if K_QUANTS_PER_ITERATION == 1
  1470. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1471. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1472. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1473. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1474. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1475. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1476. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1477. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1478. tmp += sum;
  1479. #else
  1480. float sum = 0;
  1481. for (int l = 0; l < 4; ++l) {
  1482. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1483. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1484. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1485. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1486. }
  1487. tmp += sum;
  1488. #endif
  1489. }
  1490. #else
  1491. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1492. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1493. const int step = tid * K_QUANTS_PER_ITERATION;
  1494. float tmp = 0; // partial sum for thread in warp
  1495. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1496. const float * y = yy + i * QK_K + step;
  1497. const uint8_t * ql = x[i].ql + step;
  1498. const uint8_t * qh = x[i].qh + step;
  1499. const int8_t * s = x[i].scales;
  1500. const float d = x[i+0].d;
  1501. float sum = 0;
  1502. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1503. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1504. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1505. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1506. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1507. }
  1508. tmp += sum;
  1509. }
  1510. #endif
  1511. // sum up partial sums and write back result
  1512. #pragma unroll
  1513. for (int mask = 16; mask > 0; mask >>= 1) {
  1514. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1515. }
  1516. if (tid == 0) {
  1517. dst[row] = tmp;
  1518. }
  1519. }
  1520. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1521. const half * x = (const half *) vx;
  1522. // automatic half -> float type cast if dfloat == float
  1523. v.x = x[ib + iqs + 0];
  1524. v.y = x[ib + iqs + 1];
  1525. }
  1526. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1527. const float * x = (const float *) vx;
  1528. // automatic half -> float type cast if dfloat == float
  1529. v.x = x[ib + iqs + 0];
  1530. v.y = x[ib + iqs + 1];
  1531. }
  1532. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1533. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1534. if (ix >= kx_padded) {
  1535. return;
  1536. }
  1537. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1538. const int i_padded = iy*kx_padded + ix;
  1539. block_q8_1 * y = (block_q8_1 *) vy;
  1540. const int ib = i_padded / QK8_1; // block index
  1541. const int iqs = i_padded % QK8_1; // quant index
  1542. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1543. float amax = fabsf(xi);
  1544. float sum = xi;
  1545. #pragma unroll
  1546. for (int mask = 16; mask > 0; mask >>= 1) {
  1547. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1548. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1549. }
  1550. const float d = amax / 127;
  1551. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1552. y[ib].qs[iqs] = q;
  1553. if (iqs > 0) {
  1554. return;
  1555. }
  1556. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1557. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1558. }
  1559. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1560. static __global__ void k_get_rows(
  1561. const void * src0, const int32_t * src1, dst_t * dst,
  1562. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1563. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1564. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1565. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1566. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1567. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1568. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1569. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1570. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1571. if (i00 >= ne00) {
  1572. return;
  1573. }
  1574. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1575. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1576. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1577. const int ib = i00/qk; // block index
  1578. const int iqs = (i00%qk)/qr; // quant index
  1579. const int iybs = i00 - i00%qk; // dst block start index
  1580. const int y_offset = qr == 1 ? 1 : qk/2;
  1581. // dequantize
  1582. dfloat2 v;
  1583. dequantize_kernel(src0_row, ib, iqs, v);
  1584. dst_row[iybs + iqs + 0] = v.x;
  1585. dst_row[iybs + iqs + y_offset] = v.y;
  1586. }
  1587. template<typename src0_t, typename dst_t>
  1588. static __global__ void k_get_rows_float(
  1589. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1590. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1591. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1592. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1593. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1594. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1595. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1596. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1597. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1598. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1599. if (i00 >= ne00) {
  1600. return;
  1601. }
  1602. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1603. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1604. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1605. dst_row[i00] = src0_row[i00];
  1606. }
  1607. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1608. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1609. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1610. if (i >= k) {
  1611. return;
  1612. }
  1613. const int ib = i/qk; // block index
  1614. const int iqs = (i%qk)/qr; // quant index
  1615. const int iybs = i - i%qk; // y block start index
  1616. const int y_offset = qr == 1 ? 1 : qk/2;
  1617. // dequantize
  1618. dfloat2 v;
  1619. dequantize_kernel(vx, ib, iqs, v);
  1620. y[iybs + iqs + 0] = v.x;
  1621. y[iybs + iqs + y_offset] = v.y;
  1622. }
  1623. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1624. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1625. #define VDR_Q4_0_Q8_1_MMVQ 2
  1626. #define VDR_Q4_0_Q8_1_MMQ 4
  1627. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1628. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1629. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1630. int sumi = 0;
  1631. #pragma unroll
  1632. for (int i = 0; i < vdr; ++i) {
  1633. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1634. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1635. // SIMD dot product of quantized values
  1636. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1637. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1638. }
  1639. const float2 ds8f = __half22float2(ds8);
  1640. // second part effectively subtracts 8 from each quant value
  1641. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1642. #else
  1643. bad_arch();
  1644. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1645. }
  1646. #define VDR_Q4_1_Q8_1_MMVQ 2
  1647. #define VDR_Q4_1_Q8_1_MMQ 4
  1648. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1649. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1650. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1651. int sumi = 0;
  1652. #pragma unroll
  1653. for (int i = 0; i < vdr; ++i) {
  1654. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1655. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1656. // SIMD dot product of quantized values
  1657. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1658. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1659. }
  1660. #ifdef GGML_CUDA_F16
  1661. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1662. const float d4d8 = tmp.x;
  1663. const float m4s8 = tmp.y;
  1664. #else
  1665. const float2 dm4f = __half22float2(dm4);
  1666. const float2 ds8f = __half22float2(ds8);
  1667. const float d4d8 = dm4f.x * ds8f.x;
  1668. const float m4s8 = dm4f.y * ds8f.y;
  1669. #endif // GGML_CUDA_F16
  1670. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1671. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1672. #else
  1673. bad_arch();
  1674. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1675. }
  1676. #define VDR_Q5_0_Q8_1_MMVQ 2
  1677. #define VDR_Q5_0_Q8_1_MMQ 4
  1678. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1679. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1680. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1681. int sumi = 0;
  1682. #pragma unroll
  1683. for (int i = 0; i < vdr; ++i) {
  1684. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1685. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1686. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1687. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1688. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1689. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1690. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1691. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1692. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1693. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1694. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1695. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1696. }
  1697. const float2 ds8f = __half22float2(ds8);
  1698. // second part effectively subtracts 16 from each quant value
  1699. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1700. #else
  1701. bad_arch();
  1702. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1703. }
  1704. #define VDR_Q5_1_Q8_1_MMVQ 2
  1705. #define VDR_Q5_1_Q8_1_MMQ 4
  1706. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1707. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1708. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1709. int sumi = 0;
  1710. #pragma unroll
  1711. for (int i = 0; i < vdr; ++i) {
  1712. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1713. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1714. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1715. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1716. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1717. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1718. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1719. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1720. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1721. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1722. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1723. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1724. }
  1725. #ifdef GGML_CUDA_F16
  1726. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1727. const float d5d8 = tmp.x;
  1728. const float m5s8 = tmp.y;
  1729. #else
  1730. const float2 dm5f = __half22float2(dm5);
  1731. const float2 ds8f = __half22float2(ds8);
  1732. const float d5d8 = dm5f.x * ds8f.x;
  1733. const float m5s8 = dm5f.y * ds8f.y;
  1734. #endif // GGML_CUDA_F16
  1735. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1736. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1737. #else
  1738. bad_arch();
  1739. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1740. }
  1741. #define VDR_Q8_0_Q8_1_MMVQ 2
  1742. #define VDR_Q8_0_Q8_1_MMQ 8
  1743. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1744. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1745. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1746. int sumi = 0;
  1747. #pragma unroll
  1748. for (int i = 0; i < vdr; ++i) {
  1749. // SIMD dot product of quantized values
  1750. sumi = __dp4a(v[i], u[i], sumi);
  1751. }
  1752. return d8_0*d8_1 * sumi;
  1753. #else
  1754. bad_arch();
  1755. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1756. }
  1757. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1758. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1759. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1760. int sumi = 0;
  1761. #pragma unroll
  1762. for (int i = 0; i < vdr; ++i) {
  1763. // SIMD dot product of quantized values
  1764. sumi = __dp4a(v[i], u[i], sumi);
  1765. }
  1766. #ifdef GGML_CUDA_F16
  1767. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1768. const float d8d8 = tmp.x;
  1769. const float m8s8 = tmp.y;
  1770. #else
  1771. const float2 dm8f = __half22float2(dm8);
  1772. const float2 ds8f = __half22float2(ds8);
  1773. const float d8d8 = dm8f.x * ds8f.x;
  1774. const float m8s8 = dm8f.y * ds8f.y;
  1775. #endif // GGML_CUDA_F16
  1776. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1777. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1778. #else
  1779. bad_arch();
  1780. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1781. }
  1782. #define VDR_Q2_K_Q8_1_MMVQ 1
  1783. #define VDR_Q2_K_Q8_1_MMQ 2
  1784. // contiguous v/x values
  1785. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1786. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1787. const half2 & dm2, const float * __restrict__ d8) {
  1788. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1789. float sumf_d = 0.0f;
  1790. float sumf_m = 0.0f;
  1791. #pragma unroll
  1792. for (int i = 0; i < QR2_K; ++i) {
  1793. const int sc = scales[2*i];
  1794. const int vi = (v >> (2*i)) & 0x03030303;
  1795. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1796. // fill int with 4x m
  1797. int m = sc >> 4;
  1798. m |= m << 8;
  1799. m |= m << 16;
  1800. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1801. }
  1802. const float2 dm2f = __half22float2(dm2);
  1803. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1804. #else
  1805. bad_arch();
  1806. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1807. }
  1808. // contiguous u/y values
  1809. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1810. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1811. const half2 & dm2, const float & d8) {
  1812. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1813. int sumi_d = 0;
  1814. int sumi_m = 0;
  1815. #pragma unroll
  1816. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1817. int sumi_d_sc = 0;
  1818. const int sc = scales[i0 / (QI8_1/2)];
  1819. // fill int with 4x m
  1820. int m = sc >> 4;
  1821. m |= m << 8;
  1822. m |= m << 16;
  1823. #pragma unroll
  1824. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1825. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1826. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1827. }
  1828. sumi_d += sumi_d_sc * (sc & 0xF);
  1829. }
  1830. const float2 dm2f = __half22float2(dm2);
  1831. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1832. #else
  1833. bad_arch();
  1834. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1835. }
  1836. #define VDR_Q3_K_Q8_1_MMVQ 1
  1837. #define VDR_Q3_K_Q8_1_MMQ 2
  1838. // contiguous v/x values
  1839. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1840. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1841. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1842. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1843. float sumf = 0.0f;
  1844. #pragma unroll
  1845. for (int i = 0; i < QR3_K; ++i) {
  1846. const int isc = scale_offset + 2*i;
  1847. const int isc_low = isc % (QK_K/32);
  1848. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1849. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1850. const int isc_high = isc % (QK_K/64);
  1851. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1852. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1853. const int sc = (sc_low | sc_high) - 32;
  1854. const int vil = (vl >> (2*i)) & 0x03030303;
  1855. const int vih = ((vh >> i) << 2) & 0x04040404;
  1856. const int vi = __vsubss4(vil, vih);
  1857. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1858. }
  1859. return d3 * sumf;
  1860. #else
  1861. bad_arch();
  1862. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1863. }
  1864. // contiguous u/y values
  1865. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1866. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1867. const float & d3, const float & d8) {
  1868. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1869. int sumi = 0;
  1870. #pragma unroll
  1871. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1872. int sumi_sc = 0;
  1873. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1874. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1875. }
  1876. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1877. }
  1878. return d3*d8 * sumi;
  1879. #else
  1880. bad_arch();
  1881. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1882. }
  1883. #define VDR_Q4_K_Q8_1_MMVQ 2
  1884. #define VDR_Q4_K_Q8_1_MMQ 8
  1885. // contiguous v/x values
  1886. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1887. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1888. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1889. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1890. float sumf_d = 0.0f;
  1891. float sumf_m = 0.0f;
  1892. #pragma unroll
  1893. for (int i = 0; i < QR4_K; ++i) {
  1894. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1895. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1896. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1897. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1898. sumf_d += d8[i] * (dot1 * sc[i]);
  1899. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1900. }
  1901. const float2 dm4f = __half22float2(dm4);
  1902. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1903. #else
  1904. bad_arch();
  1905. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1906. }
  1907. // contiguous u/y values
  1908. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1909. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1910. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1911. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1912. float sumf_d = 0.0f;
  1913. float sumf_m = 0.0f;
  1914. #pragma unroll
  1915. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1916. int sumi_d = 0;
  1917. #pragma unroll
  1918. for (int j = 0; j < QI8_1; ++j) {
  1919. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1920. }
  1921. const float2 ds8f = __half22float2(ds8[i]);
  1922. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1923. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1924. }
  1925. const float2 dm4f = __half22float2(dm4);
  1926. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1927. #else
  1928. bad_arch();
  1929. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1930. }
  1931. #define VDR_Q5_K_Q8_1_MMVQ 2
  1932. #define VDR_Q5_K_Q8_1_MMQ 8
  1933. // contiguous v/x values
  1934. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1935. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1936. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1937. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1938. float sumf_d = 0.0f;
  1939. float sumf_m = 0.0f;
  1940. #pragma unroll
  1941. for (int i = 0; i < QR5_K; ++i) {
  1942. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1943. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1944. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1945. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1946. const int v0i = vl0i | vh0i;
  1947. const int v1i = vl1i | vh1i;
  1948. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1949. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1950. sumf_d += d8[i] * (dot1 * sc[i]);
  1951. sumf_m += d8[i] * (dot2 * m[i]);
  1952. }
  1953. const float2 dm5f = __half22float2(dm5);
  1954. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1955. #else
  1956. bad_arch();
  1957. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1958. }
  1959. // contiguous u/y values
  1960. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1961. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1962. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1963. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1964. float sumf_d = 0.0f;
  1965. float sumf_m = 0.0f;
  1966. #pragma unroll
  1967. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1968. int sumi_d = 0;
  1969. #pragma unroll
  1970. for (int j = 0; j < QI8_1; ++j) {
  1971. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1972. }
  1973. const float2 ds8f = __half22float2(ds8[i]);
  1974. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1975. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1976. }
  1977. const float2 dm4f = __half22float2(dm4);
  1978. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1979. #else
  1980. bad_arch();
  1981. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1982. }
  1983. #define VDR_Q6_K_Q8_1_MMVQ 1
  1984. #define VDR_Q6_K_Q8_1_MMQ 8
  1985. // contiguous v/x values
  1986. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1987. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1988. const float & d, const float * __restrict__ d8) {
  1989. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1990. float sumf = 0.0f;
  1991. #pragma unroll
  1992. for (int i = 0; i < QR6_K; ++i) {
  1993. const int sc = scales[4*i];
  1994. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1995. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1996. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1997. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1998. }
  1999. return d*sumf;
  2000. #else
  2001. bad_arch();
  2002. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2003. }
  2004. // contiguous u/y values
  2005. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2006. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2007. const float & d6, const float * __restrict__ d8) {
  2008. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2009. float sumf_d = 0.0f;
  2010. #pragma unroll
  2011. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2012. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2013. #pragma unroll
  2014. for (int i = i0; i < i0 + 2; ++i) {
  2015. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2016. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2017. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2018. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2019. }
  2020. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2021. }
  2022. return d6 * sumf_d;
  2023. #else
  2024. bad_arch();
  2025. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2026. }
  2027. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2028. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2029. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2030. int v[VDR_Q4_0_Q8_1_MMVQ];
  2031. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2032. #pragma unroll
  2033. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2034. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2035. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2036. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2037. }
  2038. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2039. }
  2040. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2041. (void)x_qh; (void)x_sc;
  2042. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2043. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2044. *x_ql = tile_x_qs;
  2045. *x_dm = (half2 *) tile_x_d;
  2046. }
  2047. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2048. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2049. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2050. (void)x_qh; (void)x_sc;
  2051. GGML_CUDA_ASSUME(i_offset >= 0);
  2052. GGML_CUDA_ASSUME(i_offset < nwarps);
  2053. GGML_CUDA_ASSUME(k >= 0);
  2054. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2055. const int kbx = k / QI4_0;
  2056. const int kqsx = k % QI4_0;
  2057. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2058. float * x_dmf = (float *) x_dm;
  2059. #pragma unroll
  2060. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2061. int i = i0 + i_offset;
  2062. if (need_check) {
  2063. i = min(i, i_max);
  2064. }
  2065. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2066. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2067. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2068. }
  2069. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2070. const int kbxd = k % blocks_per_tile_x_row;
  2071. #pragma unroll
  2072. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2073. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2074. if (need_check) {
  2075. i = min(i, i_max);
  2076. }
  2077. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2078. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2079. }
  2080. }
  2081. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2082. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2083. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2084. (void)x_qh; (void)x_sc;
  2085. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2086. const float * x_dmf = (const float *) x_dm;
  2087. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2088. #pragma unroll
  2089. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2090. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2091. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2092. }
  2093. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2094. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2095. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2096. }
  2097. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2098. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2099. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2100. int v[VDR_Q4_1_Q8_1_MMVQ];
  2101. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2102. #pragma unroll
  2103. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2104. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2105. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2106. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2107. }
  2108. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2109. }
  2110. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2111. (void)x_qh; (void)x_sc;
  2112. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2113. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2114. *x_ql = tile_x_qs;
  2115. *x_dm = tile_x_dm;
  2116. }
  2117. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2118. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2119. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2120. (void)x_qh; (void)x_sc;
  2121. GGML_CUDA_ASSUME(i_offset >= 0);
  2122. GGML_CUDA_ASSUME(i_offset < nwarps);
  2123. GGML_CUDA_ASSUME(k >= 0);
  2124. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2125. const int kbx = k / QI4_1;
  2126. const int kqsx = k % QI4_1;
  2127. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2128. #pragma unroll
  2129. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2130. int i = i0 + i_offset;
  2131. if (need_check) {
  2132. i = min(i, i_max);
  2133. }
  2134. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2135. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2136. }
  2137. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2138. const int kbxd = k % blocks_per_tile_x_row;
  2139. #pragma unroll
  2140. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2141. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2142. if (need_check) {
  2143. i = min(i, i_max);
  2144. }
  2145. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2146. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2147. }
  2148. }
  2149. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2150. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2151. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2152. (void)x_qh; (void)x_sc;
  2153. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2154. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2155. #pragma unroll
  2156. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2157. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2158. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2159. }
  2160. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2161. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2162. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2163. }
  2164. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2165. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2166. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2167. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2168. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2169. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2170. #pragma unroll
  2171. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2172. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2173. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2174. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2175. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2176. }
  2177. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2178. }
  2179. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2180. (void)x_qh; (void)x_sc;
  2181. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2182. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2183. *x_ql = tile_x_ql;
  2184. *x_dm = (half2 *) tile_x_d;
  2185. }
  2186. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2187. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2188. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2189. (void)x_qh; (void)x_sc;
  2190. GGML_CUDA_ASSUME(i_offset >= 0);
  2191. GGML_CUDA_ASSUME(i_offset < nwarps);
  2192. GGML_CUDA_ASSUME(k >= 0);
  2193. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2194. const int kbx = k / QI5_0;
  2195. const int kqsx = k % QI5_0;
  2196. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2197. #pragma unroll
  2198. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2199. int i = i0 + i_offset;
  2200. if (need_check) {
  2201. i = min(i, i_max);
  2202. }
  2203. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2204. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2205. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2206. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2207. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2208. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2209. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2210. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2211. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2212. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2213. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2214. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2215. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2216. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2217. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2218. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2219. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2220. }
  2221. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2222. const int kbxd = k % blocks_per_tile_x_row;
  2223. float * x_dmf = (float *) x_dm;
  2224. #pragma unroll
  2225. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2226. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2227. if (need_check) {
  2228. i = min(i, i_max);
  2229. }
  2230. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2231. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2232. }
  2233. }
  2234. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2235. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2236. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2237. (void)x_qh; (void)x_sc;
  2238. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2239. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2240. const float * x_dmf = (const float *) x_dm;
  2241. const float * y_df = (const float *) y_ds;
  2242. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2243. #pragma unroll
  2244. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2245. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2246. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2247. }
  2248. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2249. (&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)]);
  2250. }
  2251. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2252. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2253. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2254. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2255. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2256. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2257. #pragma unroll
  2258. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2259. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2260. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2261. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2262. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2263. }
  2264. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2265. }
  2266. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2267. (void)x_qh; (void)x_sc;
  2268. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2269. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2270. *x_ql = tile_x_ql;
  2271. *x_dm = tile_x_dm;
  2272. }
  2273. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2274. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2275. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2276. (void)x_qh; (void)x_sc;
  2277. GGML_CUDA_ASSUME(i_offset >= 0);
  2278. GGML_CUDA_ASSUME(i_offset < nwarps);
  2279. GGML_CUDA_ASSUME(k >= 0);
  2280. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2281. const int kbx = k / QI5_1;
  2282. const int kqsx = k % QI5_1;
  2283. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2284. #pragma unroll
  2285. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2286. int i = i0 + i_offset;
  2287. if (need_check) {
  2288. i = min(i, i_max);
  2289. }
  2290. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2291. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2292. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2293. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2294. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2295. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2296. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2297. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2298. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2299. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2300. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2301. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2302. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2303. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2304. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2305. }
  2306. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2307. const int kbxd = k % blocks_per_tile_x_row;
  2308. #pragma unroll
  2309. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2310. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2311. if (need_check) {
  2312. i = min(i, i_max);
  2313. }
  2314. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2315. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2316. }
  2317. }
  2318. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2319. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2320. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2321. (void)x_qh; (void)x_sc;
  2322. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2323. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2324. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2325. #pragma unroll
  2326. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2327. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2328. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2329. }
  2330. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2331. (&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)]);
  2332. }
  2333. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2334. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2335. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2336. int v[VDR_Q8_0_Q8_1_MMVQ];
  2337. int u[VDR_Q8_0_Q8_1_MMVQ];
  2338. #pragma unroll
  2339. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2340. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2341. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2342. }
  2343. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2344. }
  2345. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2346. (void)x_qh; (void)x_sc;
  2347. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2348. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2349. *x_ql = tile_x_qs;
  2350. *x_dm = (half2 *) tile_x_d;
  2351. }
  2352. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2353. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2354. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2355. (void)x_qh; (void)x_sc;
  2356. GGML_CUDA_ASSUME(i_offset >= 0);
  2357. GGML_CUDA_ASSUME(i_offset < nwarps);
  2358. GGML_CUDA_ASSUME(k >= 0);
  2359. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2360. const int kbx = k / QI8_0;
  2361. const int kqsx = k % QI8_0;
  2362. float * x_dmf = (float *) x_dm;
  2363. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2364. #pragma unroll
  2365. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2366. int i = i0 + i_offset;
  2367. if (need_check) {
  2368. i = min(i, i_max);
  2369. }
  2370. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2371. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2372. }
  2373. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2374. const int kbxd = k % blocks_per_tile_x_row;
  2375. #pragma unroll
  2376. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2377. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2378. if (need_check) {
  2379. i = min(i, i_max);
  2380. }
  2381. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2382. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2383. }
  2384. }
  2385. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2386. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2387. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2388. (void)x_qh; (void)x_sc;
  2389. const float * x_dmf = (const float *) x_dm;
  2390. const float * y_df = (const float *) y_ds;
  2391. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2392. (&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],
  2393. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2394. }
  2395. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2396. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2397. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2398. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2399. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2400. const uint8_t * scales = bq2_K->scales + scale_offset;
  2401. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2402. int u[QR2_K];
  2403. float d8[QR2_K];
  2404. #pragma unroll
  2405. for (int i = 0; i < QR2_K; ++ i) {
  2406. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2407. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2408. }
  2409. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2410. }
  2411. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2412. (void)x_qh;
  2413. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2414. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2415. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2416. *x_ql = tile_x_ql;
  2417. *x_dm = tile_x_dm;
  2418. *x_sc = tile_x_sc;
  2419. }
  2420. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2421. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2422. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2423. (void)x_qh;
  2424. GGML_CUDA_ASSUME(i_offset >= 0);
  2425. GGML_CUDA_ASSUME(i_offset < nwarps);
  2426. GGML_CUDA_ASSUME(k >= 0);
  2427. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2428. const int kbx = k / QI2_K;
  2429. const int kqsx = k % QI2_K;
  2430. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2431. #pragma unroll
  2432. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2433. int i = i0 + i_offset;
  2434. if (need_check) {
  2435. i = min(i, i_max);
  2436. }
  2437. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2438. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2439. }
  2440. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2441. const int kbxd = k % blocks_per_tile_x_row;
  2442. #pragma unroll
  2443. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2444. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2445. if (need_check) {
  2446. i = min(i, i_max);
  2447. }
  2448. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2449. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2450. }
  2451. #pragma unroll
  2452. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2453. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2454. if (need_check) {
  2455. i = min(i, i_max);
  2456. }
  2457. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2458. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2459. }
  2460. }
  2461. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2462. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2463. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2464. (void)x_qh;
  2465. const int kbx = k / QI2_K;
  2466. const int ky = (k % QI2_K) * QR2_K;
  2467. const float * y_df = (const float *) y_ds;
  2468. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2469. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2470. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2471. #pragma unroll
  2472. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2473. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2474. }
  2475. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2476. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2477. 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]);
  2478. }
  2479. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2480. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2481. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2482. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2483. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2484. const float d = bq3_K->d;
  2485. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2486. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2487. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2488. int u[QR3_K];
  2489. float d8[QR3_K];
  2490. #pragma unroll
  2491. for (int i = 0; i < QR3_K; ++i) {
  2492. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2493. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2494. }
  2495. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2496. }
  2497. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2498. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2499. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2500. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2501. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2502. *x_ql = tile_x_ql;
  2503. *x_dm = tile_x_dm;
  2504. *x_qh = tile_x_qh;
  2505. *x_sc = tile_x_sc;
  2506. }
  2507. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2508. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2509. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2510. GGML_CUDA_ASSUME(i_offset >= 0);
  2511. GGML_CUDA_ASSUME(i_offset < nwarps);
  2512. GGML_CUDA_ASSUME(k >= 0);
  2513. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2514. const int kbx = k / QI3_K;
  2515. const int kqsx = k % QI3_K;
  2516. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2517. #pragma unroll
  2518. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2519. int i = i0 + i_offset;
  2520. if (need_check) {
  2521. i = min(i, i_max);
  2522. }
  2523. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2524. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2525. }
  2526. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2527. const int kbxd = k % blocks_per_tile_x_row;
  2528. float * x_dmf = (float *) x_dm;
  2529. #pragma unroll
  2530. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2531. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2532. if (need_check) {
  2533. i = min(i, i_max);
  2534. }
  2535. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2536. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2537. }
  2538. #pragma unroll
  2539. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2540. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2541. if (need_check) {
  2542. i = min(i, i_max);
  2543. }
  2544. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2545. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2546. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2547. }
  2548. #pragma unroll
  2549. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2550. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2551. if (need_check) {
  2552. i = min(i, i_max);
  2553. }
  2554. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2555. const int ksc = k % (QI3_K/4);
  2556. const int ksc_low = ksc % (QI3_K/8);
  2557. const int shift_low = 4 * (ksc / (QI3_K/8));
  2558. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2559. const int ksc_high = QI3_K/8;
  2560. const int shift_high = 2 * ksc;
  2561. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2562. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2563. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2564. }
  2565. }
  2566. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2567. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2568. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2569. const int kbx = k / QI3_K;
  2570. const int ky = (k % QI3_K) * QR3_K;
  2571. const float * x_dmf = (const float *) x_dm;
  2572. const float * y_df = (const float *) y_ds;
  2573. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2574. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2575. #pragma unroll
  2576. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2577. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2578. const int shift = 2 * ((ky % 32) / 8);
  2579. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2580. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2581. const int vlh = (vh << 2) & 0x04040404;
  2582. v[l] = __vsubss4(vll, vlh);
  2583. }
  2584. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2585. 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]);
  2586. }
  2587. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2588. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2589. #ifndef GGML_QKK_64
  2590. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2591. int v[2];
  2592. int u[2*QR4_K];
  2593. float d8[QR4_K];
  2594. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2595. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2596. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2597. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2598. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2599. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2600. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2601. v[0] = q4[0];
  2602. v[1] = q4[4];
  2603. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2604. uint16_t aux[2];
  2605. const int j = bq8_offset/2;
  2606. if (j < 2) {
  2607. aux[0] = scales[j+0] & 0x3f3f;
  2608. aux[1] = scales[j+2] & 0x3f3f;
  2609. } else {
  2610. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2611. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2612. }
  2613. const uint8_t * sc = (const uint8_t *)aux;
  2614. const uint8_t * m = sc + 2;
  2615. for (int i = 0; i < QR4_K; ++i) {
  2616. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2617. d8[i] = __low2half(bq8i->ds);
  2618. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2619. u[2*i+0] = q8[0];
  2620. u[2*i+1] = q8[4];
  2621. }
  2622. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2623. #else
  2624. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2625. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2626. float sumf_d = 0.0f;
  2627. float sumf_m = 0.0f;
  2628. uint16_t aux16[2];
  2629. const uint8_t * s = (const uint8_t *)aux16;
  2630. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2631. aux16[0] = a[0] & 0x0f0f;
  2632. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2633. const float dall = bq4_K->dm[0];
  2634. const float dmin = bq4_K->dm[1];
  2635. const float d8_1 = __low2float(bq8_1[0].ds);
  2636. const float d8_2 = __low2float(bq8_1[1].ds);
  2637. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2638. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2639. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2640. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2641. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2642. const int v1 = q4[0];
  2643. const int v2 = q4[4];
  2644. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2645. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2646. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2647. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2648. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2649. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2650. return dall * sumf_d - dmin * sumf_m;
  2651. #else
  2652. bad_arch();
  2653. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2654. #endif
  2655. }
  2656. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2657. (void)x_qh;
  2658. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2659. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2660. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2661. *x_ql = tile_x_ql;
  2662. *x_dm = tile_x_dm;
  2663. *x_sc = tile_x_sc;
  2664. }
  2665. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2666. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2667. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2668. (void)x_qh;
  2669. GGML_CUDA_ASSUME(i_offset >= 0);
  2670. GGML_CUDA_ASSUME(i_offset < nwarps);
  2671. GGML_CUDA_ASSUME(k >= 0);
  2672. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2673. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2674. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2675. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2676. #pragma unroll
  2677. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2678. int i = i0 + i_offset;
  2679. if (need_check) {
  2680. i = min(i, i_max);
  2681. }
  2682. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2683. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2684. }
  2685. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2686. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2687. #pragma unroll
  2688. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2689. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2690. if (need_check) {
  2691. i = min(i, i_max);
  2692. }
  2693. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2694. #if QK_K == 256
  2695. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2696. #else
  2697. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2698. #endif
  2699. }
  2700. #pragma unroll
  2701. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2702. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2703. if (need_check) {
  2704. i = min(i, i_max);
  2705. }
  2706. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2707. const int * scales = (const int *) bxi->scales;
  2708. const int ksc = k % (WARP_SIZE/8);
  2709. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2710. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2711. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2712. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2713. }
  2714. }
  2715. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2716. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2717. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2718. (void)x_qh;
  2719. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2720. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2721. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2722. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2723. }
  2724. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2725. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2726. #ifndef GGML_QKK_64
  2727. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2728. int vl[2];
  2729. int vh[2];
  2730. int u[2*QR5_K];
  2731. float d8[QR5_K];
  2732. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2733. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2734. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2735. vl[0] = ql[0];
  2736. vl[1] = ql[4];
  2737. vh[0] = qh[0] >> bq8_offset;
  2738. vh[1] = qh[4] >> bq8_offset;
  2739. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2740. uint16_t aux[2];
  2741. const int j = bq8_offset/2;
  2742. if (j < 2) {
  2743. aux[0] = scales[j+0] & 0x3f3f;
  2744. aux[1] = scales[j+2] & 0x3f3f;
  2745. } else {
  2746. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2747. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2748. }
  2749. const uint8_t * sc = (const uint8_t *)aux;
  2750. const uint8_t * m = sc + 2;
  2751. #pragma unroll
  2752. for (int i = 0; i < QR5_K; ++i) {
  2753. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2754. d8[i] = __low2float(bq8i->ds);
  2755. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2756. u[2*i+0] = q8[0];
  2757. u[2*i+1] = q8[4];
  2758. }
  2759. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2760. #else
  2761. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2762. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2763. const int8_t * s = bq5_K->scales;
  2764. const float d = bq5_K->d;
  2765. const float d8_1 = __low2half(bq8_1[0].ds);
  2766. const float d8_2 = __low2half(bq8_1[1].ds);
  2767. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2768. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2769. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2770. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2771. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2772. const int vl1 = ql[0];
  2773. const int vl2 = ql[4];
  2774. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2775. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2776. const int in = step%8; // 0, 4, 0, 4
  2777. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2778. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2779. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2780. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2781. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2782. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2783. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2784. return d * sumf_d;
  2785. #else
  2786. bad_arch();
  2787. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2788. #endif
  2789. }
  2790. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2791. (void)x_qh;
  2792. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2793. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2794. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2795. *x_ql = tile_x_ql;
  2796. *x_dm = tile_x_dm;
  2797. *x_sc = tile_x_sc;
  2798. }
  2799. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2800. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2801. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2802. (void)x_qh;
  2803. GGML_CUDA_ASSUME(i_offset >= 0);
  2804. GGML_CUDA_ASSUME(i_offset < nwarps);
  2805. GGML_CUDA_ASSUME(k >= 0);
  2806. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2807. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2808. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2809. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2810. #pragma unroll
  2811. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2812. int i = i0 + i_offset;
  2813. if (need_check) {
  2814. i = min(i, i_max);
  2815. }
  2816. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2817. const int ky = QR5_K*kqsx;
  2818. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2819. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2820. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2821. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2822. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2823. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2824. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2825. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2826. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2827. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2828. }
  2829. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2830. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2831. #pragma unroll
  2832. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2833. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2834. if (need_check) {
  2835. i = min(i, i_max);
  2836. }
  2837. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2838. #if QK_K == 256
  2839. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2840. #endif
  2841. }
  2842. #pragma unroll
  2843. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2844. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2845. if (need_check) {
  2846. i = min(i, i_max);
  2847. }
  2848. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2849. const int * scales = (const int *) bxi->scales;
  2850. const int ksc = k % (WARP_SIZE/8);
  2851. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2852. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2853. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2854. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2855. }
  2856. }
  2857. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2858. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2859. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2860. (void)x_qh;
  2861. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2862. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2863. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2864. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2865. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2866. }
  2867. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2868. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2869. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2870. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2871. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2872. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2873. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2874. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2875. const int8_t * scales = bq6_K->scales + scale_offset;
  2876. int u[QR6_K];
  2877. float d8[QR6_K];
  2878. #pragma unroll
  2879. for (int i = 0; i < QR6_K; ++i) {
  2880. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2881. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2882. }
  2883. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2884. }
  2885. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2886. (void)x_qh;
  2887. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2888. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2889. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2890. *x_ql = tile_x_ql;
  2891. *x_dm = tile_x_dm;
  2892. *x_sc = tile_x_sc;
  2893. }
  2894. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2895. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2896. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2897. (void)x_qh;
  2898. GGML_CUDA_ASSUME(i_offset >= 0);
  2899. GGML_CUDA_ASSUME(i_offset < nwarps);
  2900. GGML_CUDA_ASSUME(k >= 0);
  2901. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2902. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2903. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2904. const block_q6_K * bx0 = (const block_q6_K *) vx;
  2905. #pragma unroll
  2906. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2907. int i = i0 + i_offset;
  2908. if (need_check) {
  2909. i = min(i, i_max);
  2910. }
  2911. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2912. const int ky = QR6_K*kqsx;
  2913. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2914. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2915. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2916. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2917. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2918. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2919. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2920. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2921. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2922. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2923. }
  2924. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2925. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2926. float * x_dmf = (float *) x_dm;
  2927. #pragma unroll
  2928. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2929. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2930. if (need_check) {
  2931. i = min(i, i_max);
  2932. }
  2933. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2934. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2935. }
  2936. #pragma unroll
  2937. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2938. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2939. if (need_check) {
  2940. i = min(i, i_max);
  2941. }
  2942. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2943. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2944. }
  2945. }
  2946. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2947. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2948. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2949. (void)x_qh;
  2950. const float * x_dmf = (const float *) x_dm;
  2951. const float * y_df = (const float *) y_ds;
  2952. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2953. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2954. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2955. 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]);
  2956. }
  2957. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2958. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2959. static __device__ __forceinline__ void mul_mat_q(
  2960. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2961. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2962. const block_q_t * x = (const block_q_t *) vx;
  2963. const block_q8_1 * y = (const block_q8_1 *) vy;
  2964. const int blocks_per_row_x = ncols_x / qk;
  2965. const int blocks_per_col_y = nrows_y / QK8_1;
  2966. const int blocks_per_warp = WARP_SIZE / qi;
  2967. const int & ncols_dst = ncols_y;
  2968. const int row_dst_0 = blockIdx.x*mmq_y;
  2969. const int & row_x_0 = row_dst_0;
  2970. const int col_dst_0 = blockIdx.y*mmq_x;
  2971. const int & col_y_0 = col_dst_0;
  2972. int * tile_x_ql = nullptr;
  2973. half2 * tile_x_dm = nullptr;
  2974. int * tile_x_qh = nullptr;
  2975. int * tile_x_sc = nullptr;
  2976. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2977. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2978. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2979. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  2980. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2981. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2982. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2983. #pragma unroll
  2984. for (int ir = 0; ir < qr; ++ir) {
  2985. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2986. const int kbxd = kqs / QI8_1;
  2987. #pragma unroll
  2988. for (int i = 0; i < mmq_x; i += nwarps) {
  2989. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2990. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2991. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2992. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2993. }
  2994. #pragma unroll
  2995. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2996. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2997. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2998. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2999. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3000. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3001. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3002. if (need_sum) {
  3003. *dsi_dst = *dsi_src;
  3004. } else {
  3005. float * dfi_dst = (float *) dsi_dst;
  3006. *dfi_dst = __low2half(*dsi_src);
  3007. }
  3008. }
  3009. __syncthreads();
  3010. // #pragma unroll // unrolling this loop causes too much register pressure
  3011. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3012. #pragma unroll
  3013. for (int j = 0; j < mmq_x; j += nwarps) {
  3014. #pragma unroll
  3015. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3016. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3017. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3018. threadIdx.x + i, threadIdx.y + j, k);
  3019. }
  3020. }
  3021. }
  3022. __syncthreads();
  3023. }
  3024. }
  3025. #pragma unroll
  3026. for (int j = 0; j < mmq_x; j += nwarps) {
  3027. const int col_dst = col_dst_0 + j + threadIdx.y;
  3028. if (col_dst >= ncols_dst) {
  3029. return;
  3030. }
  3031. #pragma unroll
  3032. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3033. const int row_dst = row_dst_0 + threadIdx.x + i;
  3034. if (row_dst >= nrows_dst) {
  3035. continue;
  3036. }
  3037. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3038. }
  3039. }
  3040. }
  3041. #define MMQ_X_Q4_0_RDNA2 64
  3042. #define MMQ_Y_Q4_0_RDNA2 128
  3043. #define NWARPS_Q4_0_RDNA2 8
  3044. #define MMQ_X_Q4_0_RDNA1 64
  3045. #define MMQ_Y_Q4_0_RDNA1 64
  3046. #define NWARPS_Q4_0_RDNA1 8
  3047. #if defined(CUDA_USE_TENSOR_CORES)
  3048. #define MMQ_X_Q4_0_AMPERE 4
  3049. #define MMQ_Y_Q4_0_AMPERE 32
  3050. #define NWARPS_Q4_0_AMPERE 4
  3051. #else
  3052. #define MMQ_X_Q4_0_AMPERE 64
  3053. #define MMQ_Y_Q4_0_AMPERE 128
  3054. #define NWARPS_Q4_0_AMPERE 4
  3055. #endif
  3056. #define MMQ_X_Q4_0_PASCAL 64
  3057. #define MMQ_Y_Q4_0_PASCAL 64
  3058. #define NWARPS_Q4_0_PASCAL 8
  3059. template <bool need_check> static __global__ void
  3060. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3061. #if defined(RDNA3) || defined(RDNA2)
  3062. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3063. #endif // defined(RDNA3) || defined(RDNA2)
  3064. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3065. mul_mat_q4_0(
  3066. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3067. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3068. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3069. #if defined(RDNA3) || defined(RDNA2)
  3070. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3071. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3072. const int nwarps = NWARPS_Q4_0_RDNA2;
  3073. #else
  3074. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3075. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3076. const int nwarps = NWARPS_Q4_0_RDNA1;
  3077. #endif // defined(RDNA3) || defined(RDNA2)
  3078. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3079. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3080. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3081. #elif __CUDA_ARCH__ >= CC_VOLTA
  3082. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3083. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3084. const int nwarps = NWARPS_Q4_0_AMPERE;
  3085. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3086. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3087. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3088. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3089. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3090. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3091. const int nwarps = NWARPS_Q4_0_PASCAL;
  3092. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3093. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3094. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3095. #else
  3096. (void) vec_dot_q4_0_q8_1_mul_mat;
  3097. bad_arch();
  3098. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3099. }
  3100. #define MMQ_X_Q4_1_RDNA2 64
  3101. #define MMQ_Y_Q4_1_RDNA2 128
  3102. #define NWARPS_Q4_1_RDNA2 8
  3103. #define MMQ_X_Q4_1_RDNA1 64
  3104. #define MMQ_Y_Q4_1_RDNA1 64
  3105. #define NWARPS_Q4_1_RDNA1 8
  3106. #if defined(CUDA_USE_TENSOR_CORES)
  3107. #define MMQ_X_Q4_1_AMPERE 4
  3108. #define MMQ_Y_Q4_1_AMPERE 32
  3109. #define NWARPS_Q4_1_AMPERE 4
  3110. #else
  3111. #define MMQ_X_Q4_1_AMPERE 64
  3112. #define MMQ_Y_Q4_1_AMPERE 128
  3113. #define NWARPS_Q4_1_AMPERE 4
  3114. #endif
  3115. #define MMQ_X_Q4_1_PASCAL 64
  3116. #define MMQ_Y_Q4_1_PASCAL 64
  3117. #define NWARPS_Q4_1_PASCAL 8
  3118. template <bool need_check> static __global__ void
  3119. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3120. #if defined(RDNA3) || defined(RDNA2)
  3121. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3122. #endif // defined(RDNA3) || defined(RDNA2)
  3123. #elif __CUDA_ARCH__ < CC_VOLTA
  3124. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3125. #endif // __CUDA_ARCH__ < CC_VOLTA
  3126. mul_mat_q4_1(
  3127. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3128. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3129. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3130. #if defined(RDNA3) || defined(RDNA2)
  3131. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3132. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3133. const int nwarps = NWARPS_Q4_1_RDNA2;
  3134. #else
  3135. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3136. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3137. const int nwarps = NWARPS_Q4_1_RDNA1;
  3138. #endif // defined(RDNA3) || defined(RDNA2)
  3139. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3140. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3141. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3142. #elif __CUDA_ARCH__ >= CC_VOLTA
  3143. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3144. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3145. const int nwarps = NWARPS_Q4_1_AMPERE;
  3146. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3147. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3148. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3149. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3150. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3151. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3152. const int nwarps = NWARPS_Q4_1_PASCAL;
  3153. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3154. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3155. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3156. #else
  3157. (void) vec_dot_q4_1_q8_1_mul_mat;
  3158. bad_arch();
  3159. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3160. }
  3161. #define MMQ_X_Q5_0_RDNA2 64
  3162. #define MMQ_Y_Q5_0_RDNA2 128
  3163. #define NWARPS_Q5_0_RDNA2 8
  3164. #define MMQ_X_Q5_0_RDNA1 64
  3165. #define MMQ_Y_Q5_0_RDNA1 64
  3166. #define NWARPS_Q5_0_RDNA1 8
  3167. #if defined(CUDA_USE_TENSOR_CORES)
  3168. #define MMQ_X_Q5_0_AMPERE 4
  3169. #define MMQ_Y_Q5_0_AMPERE 32
  3170. #define NWARPS_Q5_0_AMPERE 4
  3171. #else
  3172. #define MMQ_X_Q5_0_AMPERE 128
  3173. #define MMQ_Y_Q5_0_AMPERE 64
  3174. #define NWARPS_Q5_0_AMPERE 4
  3175. #endif
  3176. #define MMQ_X_Q5_0_PASCAL 64
  3177. #define MMQ_Y_Q5_0_PASCAL 64
  3178. #define NWARPS_Q5_0_PASCAL 8
  3179. template <bool need_check> static __global__ void
  3180. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3181. #if defined(RDNA3) || defined(RDNA2)
  3182. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3183. #endif // defined(RDNA3) || defined(RDNA2)
  3184. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3185. mul_mat_q5_0(
  3186. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3187. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3188. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3189. #if defined(RDNA3) || defined(RDNA2)
  3190. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3191. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3192. const int nwarps = NWARPS_Q5_0_RDNA2;
  3193. #else
  3194. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3195. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3196. const int nwarps = NWARPS_Q5_0_RDNA1;
  3197. #endif // defined(RDNA3) || defined(RDNA2)
  3198. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3199. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3200. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3201. #elif __CUDA_ARCH__ >= CC_VOLTA
  3202. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3203. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3204. const int nwarps = NWARPS_Q5_0_AMPERE;
  3205. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3206. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3207. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3208. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3209. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3210. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3211. const int nwarps = NWARPS_Q5_0_PASCAL;
  3212. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3213. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3214. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3215. #else
  3216. (void) vec_dot_q5_0_q8_1_mul_mat;
  3217. bad_arch();
  3218. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3219. }
  3220. #define MMQ_X_Q5_1_RDNA2 64
  3221. #define MMQ_Y_Q5_1_RDNA2 128
  3222. #define NWARPS_Q5_1_RDNA2 8
  3223. #define MMQ_X_Q5_1_RDNA1 64
  3224. #define MMQ_Y_Q5_1_RDNA1 64
  3225. #define NWARPS_Q5_1_RDNA1 8
  3226. #if defined(CUDA_USE_TENSOR_CORES)
  3227. #define MMQ_X_Q5_1_AMPERE 4
  3228. #define MMQ_Y_Q5_1_AMPERE 32
  3229. #define NWARPS_Q5_1_AMPERE 4
  3230. #else
  3231. #define MMQ_X_Q5_1_AMPERE 128
  3232. #define MMQ_Y_Q5_1_AMPERE 64
  3233. #define NWARPS_Q5_1_AMPERE 4
  3234. #endif
  3235. #define MMQ_X_Q5_1_PASCAL 64
  3236. #define MMQ_Y_Q5_1_PASCAL 64
  3237. #define NWARPS_Q5_1_PASCAL 8
  3238. template <bool need_check> static __global__ void
  3239. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3240. #if defined(RDNA3) || defined(RDNA2)
  3241. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3242. #endif // defined(RDNA3) || defined(RDNA2)
  3243. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3244. mul_mat_q5_1(
  3245. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3246. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3247. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3248. #if defined(RDNA3) || defined(RDNA2)
  3249. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3250. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3251. const int nwarps = NWARPS_Q5_1_RDNA2;
  3252. #else
  3253. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3254. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3255. const int nwarps = NWARPS_Q5_1_RDNA1;
  3256. #endif // defined(RDNA3) || defined(RDNA2)
  3257. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3258. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3259. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3260. #elif __CUDA_ARCH__ >= CC_VOLTA
  3261. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3262. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3263. const int nwarps = NWARPS_Q5_1_AMPERE;
  3264. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3265. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3266. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3267. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3268. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3269. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3270. const int nwarps = NWARPS_Q5_1_PASCAL;
  3271. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3272. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3273. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3274. #else
  3275. (void) vec_dot_q5_1_q8_1_mul_mat;
  3276. bad_arch();
  3277. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3278. }
  3279. #define MMQ_X_Q8_0_RDNA2 64
  3280. #define MMQ_Y_Q8_0_RDNA2 128
  3281. #define NWARPS_Q8_0_RDNA2 8
  3282. #define MMQ_X_Q8_0_RDNA1 64
  3283. #define MMQ_Y_Q8_0_RDNA1 64
  3284. #define NWARPS_Q8_0_RDNA1 8
  3285. #if defined(CUDA_USE_TENSOR_CORES)
  3286. #define MMQ_X_Q8_0_AMPERE 4
  3287. #define MMQ_Y_Q8_0_AMPERE 32
  3288. #define NWARPS_Q8_0_AMPERE 4
  3289. #else
  3290. #define MMQ_X_Q8_0_AMPERE 128
  3291. #define MMQ_Y_Q8_0_AMPERE 64
  3292. #define NWARPS_Q8_0_AMPERE 4
  3293. #endif
  3294. #define MMQ_X_Q8_0_PASCAL 64
  3295. #define MMQ_Y_Q8_0_PASCAL 64
  3296. #define NWARPS_Q8_0_PASCAL 8
  3297. template <bool need_check> static __global__ void
  3298. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3299. #if defined(RDNA3) || defined(RDNA2)
  3300. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3301. #endif // defined(RDNA3) || defined(RDNA2)
  3302. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3303. mul_mat_q8_0(
  3304. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3305. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3306. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3307. #if defined(RDNA3) || defined(RDNA2)
  3308. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3309. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3310. const int nwarps = NWARPS_Q8_0_RDNA2;
  3311. #else
  3312. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3313. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3314. const int nwarps = NWARPS_Q8_0_RDNA1;
  3315. #endif // defined(RDNA3) || defined(RDNA2)
  3316. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3317. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3318. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3319. #elif __CUDA_ARCH__ >= CC_VOLTA
  3320. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3321. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3322. const int nwarps = NWARPS_Q8_0_AMPERE;
  3323. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3324. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3325. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3326. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3327. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3328. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3329. const int nwarps = NWARPS_Q8_0_PASCAL;
  3330. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3331. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3332. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3333. #else
  3334. (void) vec_dot_q8_0_q8_1_mul_mat;
  3335. bad_arch();
  3336. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3337. }
  3338. #define MMQ_X_Q2_K_RDNA2 64
  3339. #define MMQ_Y_Q2_K_RDNA2 128
  3340. #define NWARPS_Q2_K_RDNA2 8
  3341. #define MMQ_X_Q2_K_RDNA1 128
  3342. #define MMQ_Y_Q2_K_RDNA1 32
  3343. #define NWARPS_Q2_K_RDNA1 8
  3344. #if defined(CUDA_USE_TENSOR_CORES)
  3345. #define MMQ_X_Q2_K_AMPERE 4
  3346. #define MMQ_Y_Q2_K_AMPERE 32
  3347. #define NWARPS_Q2_K_AMPERE 4
  3348. #else
  3349. #define MMQ_X_Q2_K_AMPERE 64
  3350. #define MMQ_Y_Q2_K_AMPERE 128
  3351. #define NWARPS_Q2_K_AMPERE 4
  3352. #endif
  3353. #define MMQ_X_Q2_K_PASCAL 64
  3354. #define MMQ_Y_Q2_K_PASCAL 64
  3355. #define NWARPS_Q2_K_PASCAL 8
  3356. template <bool need_check> static __global__ void
  3357. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3358. #if defined(RDNA3) || defined(RDNA2)
  3359. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3360. #endif // defined(RDNA3) || defined(RDNA2)
  3361. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3362. mul_mat_q2_K(
  3363. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3364. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3365. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3366. #if defined(RDNA3) || defined(RDNA2)
  3367. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3368. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3369. const int nwarps = NWARPS_Q2_K_RDNA2;
  3370. #else
  3371. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3372. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3373. const int nwarps = NWARPS_Q2_K_RDNA1;
  3374. #endif // defined(RDNA3) || defined(RDNA2)
  3375. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3376. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3377. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3378. #elif __CUDA_ARCH__ >= CC_VOLTA
  3379. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3380. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3381. const int nwarps = NWARPS_Q2_K_AMPERE;
  3382. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3383. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3384. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3385. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3386. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3387. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3388. const int nwarps = NWARPS_Q2_K_PASCAL;
  3389. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3390. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3391. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3392. #else
  3393. (void) vec_dot_q2_K_q8_1_mul_mat;
  3394. bad_arch();
  3395. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3396. }
  3397. #define MMQ_X_Q3_K_RDNA2 128
  3398. #define MMQ_Y_Q3_K_RDNA2 64
  3399. #define NWARPS_Q3_K_RDNA2 8
  3400. #define MMQ_X_Q3_K_RDNA1 32
  3401. #define MMQ_Y_Q3_K_RDNA1 128
  3402. #define NWARPS_Q3_K_RDNA1 8
  3403. #if defined(CUDA_USE_TENSOR_CORES)
  3404. #define MMQ_X_Q3_K_AMPERE 4
  3405. #define MMQ_Y_Q3_K_AMPERE 32
  3406. #define NWARPS_Q3_K_AMPERE 4
  3407. #else
  3408. #define MMQ_X_Q3_K_AMPERE 128
  3409. #define MMQ_Y_Q3_K_AMPERE 128
  3410. #define NWARPS_Q3_K_AMPERE 4
  3411. #endif
  3412. #define MMQ_X_Q3_K_PASCAL 64
  3413. #define MMQ_Y_Q3_K_PASCAL 64
  3414. #define NWARPS_Q3_K_PASCAL 8
  3415. template <bool need_check> static __global__ void
  3416. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3417. #if defined(RDNA3) || defined(RDNA2)
  3418. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3419. #endif // defined(RDNA3) || defined(RDNA2)
  3420. #elif __CUDA_ARCH__ < CC_VOLTA
  3421. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3422. #endif // __CUDA_ARCH__ < CC_VOLTA
  3423. mul_mat_q3_K(
  3424. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3425. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3426. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3427. #if defined(RDNA3) || defined(RDNA2)
  3428. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3429. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3430. const int nwarps = NWARPS_Q3_K_RDNA2;
  3431. #else
  3432. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3433. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3434. const int nwarps = NWARPS_Q3_K_RDNA1;
  3435. #endif // defined(RDNA3) || defined(RDNA2)
  3436. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3437. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3438. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3439. #elif __CUDA_ARCH__ >= CC_VOLTA
  3440. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3441. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3442. const int nwarps = NWARPS_Q3_K_AMPERE;
  3443. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3444. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3445. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3446. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3447. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3448. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3449. const int nwarps = NWARPS_Q3_K_PASCAL;
  3450. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3451. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3452. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3453. #else
  3454. (void) vec_dot_q3_K_q8_1_mul_mat;
  3455. bad_arch();
  3456. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3457. }
  3458. #define MMQ_X_Q4_K_RDNA2 64
  3459. #define MMQ_Y_Q4_K_RDNA2 128
  3460. #define NWARPS_Q4_K_RDNA2 8
  3461. #define MMQ_X_Q4_K_RDNA1 32
  3462. #define MMQ_Y_Q4_K_RDNA1 64
  3463. #define NWARPS_Q4_K_RDNA1 8
  3464. #if defined(CUDA_USE_TENSOR_CORES)
  3465. #define MMQ_X_Q4_K_AMPERE 4
  3466. #define MMQ_Y_Q4_K_AMPERE 32
  3467. #define NWARPS_Q4_K_AMPERE 4
  3468. #else
  3469. #define MMQ_X_Q4_K_AMPERE 64
  3470. #define MMQ_Y_Q4_K_AMPERE 128
  3471. #define NWARPS_Q4_K_AMPERE 4
  3472. #endif
  3473. #define MMQ_X_Q4_K_PASCAL 64
  3474. #define MMQ_Y_Q4_K_PASCAL 64
  3475. #define NWARPS_Q4_K_PASCAL 8
  3476. template <bool need_check> static __global__ void
  3477. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3478. #if defined(RDNA3) || defined(RDNA2)
  3479. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3480. #endif // defined(RDNA3) || defined(RDNA2)
  3481. #elif __CUDA_ARCH__ < CC_VOLTA
  3482. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3483. #endif // __CUDA_ARCH__ < CC_VOLTA
  3484. mul_mat_q4_K(
  3485. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3486. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3487. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3488. #if defined(RDNA3) || defined(RDNA2)
  3489. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3490. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3491. const int nwarps = NWARPS_Q4_K_RDNA2;
  3492. #else
  3493. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3494. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3495. const int nwarps = NWARPS_Q4_K_RDNA1;
  3496. #endif // defined(RDNA3) || defined(RDNA2)
  3497. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3498. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3499. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3500. #elif __CUDA_ARCH__ >= CC_VOLTA
  3501. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3502. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3503. const int nwarps = NWARPS_Q4_K_AMPERE;
  3504. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3505. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3506. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3507. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3508. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3509. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3510. const int nwarps = NWARPS_Q4_K_PASCAL;
  3511. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3512. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3513. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3514. #else
  3515. (void) vec_dot_q4_K_q8_1_mul_mat;
  3516. bad_arch();
  3517. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3518. }
  3519. #define MMQ_X_Q5_K_RDNA2 64
  3520. #define MMQ_Y_Q5_K_RDNA2 128
  3521. #define NWARPS_Q5_K_RDNA2 8
  3522. #define MMQ_X_Q5_K_RDNA1 32
  3523. #define MMQ_Y_Q5_K_RDNA1 64
  3524. #define NWARPS_Q5_K_RDNA1 8
  3525. #if defined(CUDA_USE_TENSOR_CORES)
  3526. #define MMQ_X_Q5_K_AMPERE 4
  3527. #define MMQ_Y_Q5_K_AMPERE 32
  3528. #define NWARPS_Q5_K_AMPERE 4
  3529. #else
  3530. #define MMQ_X_Q5_K_AMPERE 64
  3531. #define MMQ_Y_Q5_K_AMPERE 128
  3532. #define NWARPS_Q5_K_AMPERE 4
  3533. #endif
  3534. #define MMQ_X_Q5_K_PASCAL 64
  3535. #define MMQ_Y_Q5_K_PASCAL 64
  3536. #define NWARPS_Q5_K_PASCAL 8
  3537. template <bool need_check> static __global__ void
  3538. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3539. #if defined(RDNA3) || defined(RDNA2)
  3540. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3541. #endif // defined(RDNA3) || defined(RDNA2)
  3542. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3543. mul_mat_q5_K(
  3544. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3545. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3546. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3547. #if defined(RDNA3) || defined(RDNA2)
  3548. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3549. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3550. const int nwarps = NWARPS_Q5_K_RDNA2;
  3551. #else
  3552. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3553. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3554. const int nwarps = NWARPS_Q5_K_RDNA1;
  3555. #endif // defined(RDNA3) || defined(RDNA2)
  3556. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3557. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3558. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3559. #elif __CUDA_ARCH__ >= CC_VOLTA
  3560. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3561. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3562. const int nwarps = NWARPS_Q5_K_AMPERE;
  3563. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3564. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3565. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3566. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3567. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3568. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3569. const int nwarps = NWARPS_Q5_K_PASCAL;
  3570. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3571. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3572. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3573. #else
  3574. (void) vec_dot_q5_K_q8_1_mul_mat;
  3575. bad_arch();
  3576. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3577. }
  3578. #define MMQ_X_Q6_K_RDNA2 64
  3579. #define MMQ_Y_Q6_K_RDNA2 128
  3580. #define NWARPS_Q6_K_RDNA2 8
  3581. #define MMQ_X_Q6_K_RDNA1 32
  3582. #define MMQ_Y_Q6_K_RDNA1 64
  3583. #define NWARPS_Q6_K_RDNA1 8
  3584. #if defined(CUDA_USE_TENSOR_CORES)
  3585. #define MMQ_X_Q6_K_AMPERE 4
  3586. #define MMQ_Y_Q6_K_AMPERE 32
  3587. #define NWARPS_Q6_K_AMPERE 4
  3588. #else
  3589. #define MMQ_X_Q6_K_AMPERE 64
  3590. #define MMQ_Y_Q6_K_AMPERE 64
  3591. #define NWARPS_Q6_K_AMPERE 4
  3592. #endif
  3593. #define MMQ_X_Q6_K_PASCAL 64
  3594. #define MMQ_Y_Q6_K_PASCAL 64
  3595. #define NWARPS_Q6_K_PASCAL 8
  3596. template <bool need_check> static __global__ void
  3597. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3598. #if defined(RDNA3) || defined(RDNA2)
  3599. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3600. #endif // defined(RDNA3) || defined(RDNA2)
  3601. #elif __CUDA_ARCH__ < CC_VOLTA
  3602. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3603. #endif // __CUDA_ARCH__ < CC_VOLTA
  3604. mul_mat_q6_K(
  3605. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3606. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3607. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3608. #if defined(RDNA3) || defined(RDNA2)
  3609. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3610. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3611. const int nwarps = NWARPS_Q6_K_RDNA2;
  3612. #else
  3613. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3614. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3615. const int nwarps = NWARPS_Q6_K_RDNA1;
  3616. #endif // defined(RDNA3) || defined(RDNA2)
  3617. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3618. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3619. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3620. #elif __CUDA_ARCH__ >= CC_VOLTA
  3621. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3622. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3623. const int nwarps = NWARPS_Q6_K_AMPERE;
  3624. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3625. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3626. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3627. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3628. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3629. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3630. const int nwarps = NWARPS_Q6_K_PASCAL;
  3631. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3632. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3633. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3634. #else
  3635. (void) vec_dot_q6_K_q8_1_mul_mat;
  3636. bad_arch();
  3637. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3638. }
  3639. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3640. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3641. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3642. if (row >= nrows) {
  3643. return;
  3644. }
  3645. const int blocks_per_row = ncols / qk;
  3646. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3647. // partial sum for each thread
  3648. float tmp = 0.0f;
  3649. const block_q_t * x = (const block_q_t *) vx;
  3650. const block_q8_1 * y = (const block_q8_1 *) vy;
  3651. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3652. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3653. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3654. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3655. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3656. }
  3657. // sum up partial sums and write back result
  3658. #pragma unroll
  3659. for (int mask = 16; mask > 0; mask >>= 1) {
  3660. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3661. }
  3662. if (threadIdx.x == 0) {
  3663. dst[row] = tmp;
  3664. }
  3665. }
  3666. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3667. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3668. // qk = quantized weights per x block
  3669. // qr = number of quantized weights per data value in x block
  3670. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3671. if (row >= nrows) {
  3672. return;
  3673. }
  3674. const int tid = threadIdx.x;
  3675. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3676. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3677. const int y_offset = qr == 1 ? 1 : qk/2;
  3678. // partial sum for each thread
  3679. #ifdef GGML_CUDA_F16
  3680. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3681. #else
  3682. float tmp = 0.0f;
  3683. #endif // GGML_CUDA_F16
  3684. for (int i = 0; i < ncols; i += iter_stride) {
  3685. const int col = i + vals_per_iter*tid;
  3686. const int ib = (row*ncols + col)/qk; // x block index
  3687. const int iqs = (col%qk)/qr; // x quant index
  3688. const int iybs = col - col%qk; // y block start index
  3689. // processing >2 values per i iter is faster for fast GPUs
  3690. #pragma unroll
  3691. for (int j = 0; j < vals_per_iter; j += 2) {
  3692. // process 2 vals per j iter
  3693. // dequantize
  3694. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3695. dfloat2 v;
  3696. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3697. // matrix multiplication
  3698. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3699. #ifdef GGML_CUDA_F16
  3700. tmp += __hmul2(v, {
  3701. y[iybs + iqs + j/qr + 0],
  3702. y[iybs + iqs + j/qr + y_offset]
  3703. });
  3704. #else
  3705. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3706. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3707. #endif // GGML_CUDA_F16
  3708. }
  3709. }
  3710. // sum up partial sums and write back result
  3711. #pragma unroll
  3712. for (int mask = 16; mask > 0; mask >>= 1) {
  3713. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3714. }
  3715. if (tid == 0) {
  3716. #ifdef GGML_CUDA_F16
  3717. dst[row] = tmp.x + tmp.y;
  3718. #else
  3719. dst[row] = tmp;
  3720. #endif // GGML_CUDA_F16
  3721. }
  3722. }
  3723. static __global__ void mul_mat_p021_f16_f32(
  3724. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3725. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3726. const half * x = (const half *) vx;
  3727. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3728. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3729. const int channel_x = channel / (nchannels_y / nchannels_x);
  3730. const int nrows_y = ncols_x;
  3731. const int nrows_dst = nrows_x;
  3732. const int row_dst = row_x;
  3733. float tmp = 0.0f;
  3734. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3735. const int col_x = col_x0 + threadIdx.x;
  3736. if (col_x >= ncols_x) {
  3737. break;
  3738. }
  3739. // x is transposed and permuted
  3740. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3741. const float xi = __half2float(x[ix]);
  3742. const int row_y = col_x;
  3743. // y is not transposed but permuted
  3744. const int iy = channel*nrows_y + row_y;
  3745. tmp += xi * y[iy];
  3746. }
  3747. // dst is not transposed and not permuted
  3748. const int idst = channel*nrows_dst + row_dst;
  3749. // sum up partial sums and write back result
  3750. #pragma unroll
  3751. for (int mask = 16; mask > 0; mask >>= 1) {
  3752. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3753. }
  3754. if (threadIdx.x == 0) {
  3755. dst[idst] = tmp;
  3756. }
  3757. }
  3758. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3759. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3760. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3761. const half * x = (const half *) vx;
  3762. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3763. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3764. const int channel_x = channel / channel_x_divisor;
  3765. const int nrows_y = ncols_x;
  3766. const int nrows_dst = nrows_x;
  3767. const int row_dst = row_x;
  3768. const int idst = channel*nrows_dst + row_dst;
  3769. float tmp = 0.0f;
  3770. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3771. const int col_x = col_x0 + threadIdx.x;
  3772. if (col_x >= ncols_x) {
  3773. break;
  3774. }
  3775. const int row_y = col_x;
  3776. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3777. const int iy = channel*nrows_y + row_y;
  3778. const float xi = __half2float(x[ix]);
  3779. tmp += xi * y[iy];
  3780. }
  3781. // sum up partial sums and write back result
  3782. #pragma unroll
  3783. for (int mask = 16; mask > 0; mask >>= 1) {
  3784. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3785. }
  3786. if (threadIdx.x == 0) {
  3787. dst[idst] = tmp;
  3788. }
  3789. }
  3790. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3791. const float * xi = (const float *) cxi;
  3792. float * dsti = (float *) cdsti;
  3793. *dsti = *xi;
  3794. }
  3795. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3796. const float * xi = (const float *) cxi;
  3797. half * dsti = (half *) cdsti;
  3798. *dsti = __float2half(*xi);
  3799. }
  3800. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3801. const half * xi = (const half *) cxi;
  3802. half * dsti = (half *) cdsti;
  3803. *dsti = *xi;
  3804. }
  3805. template <cpy_kernel_t cpy_1>
  3806. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3807. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3808. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3809. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3810. if (i >= ne) {
  3811. return;
  3812. }
  3813. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3814. // then combine those indices with the corresponding byte offsets to get the total offsets
  3815. const int i02 = i / (ne00*ne01);
  3816. const int i01 = (i - i02*ne01*ne00) / ne00;
  3817. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3818. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3819. const int i12 = i / (ne10*ne11);
  3820. const int i11 = (i - i12*ne10*ne11) / ne10;
  3821. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3822. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3823. cpy_1(cx + x_offset, cdst + dst_offset);
  3824. }
  3825. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  3826. const float * xi = (const float *) cxi;
  3827. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  3828. float amax = 0.0f; // absolute max
  3829. for (int j = 0; j < QK8_0; j++) {
  3830. const float v = xi[j];
  3831. amax = fmaxf(amax, fabsf(v));
  3832. }
  3833. const float d = amax / ((1 << 7) - 1);
  3834. const float id = d ? 1.0f/d : 0.0f;
  3835. dsti->d = d;
  3836. for (int j = 0; j < QK8_0; ++j) {
  3837. const float x0 = xi[j]*id;
  3838. dsti->qs[j] = roundf(x0);
  3839. }
  3840. }
  3841. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  3842. const float * xi = (const float *) cxi;
  3843. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  3844. float amax = 0.0f;
  3845. float vmax = 0.0f;
  3846. for (int j = 0; j < QK4_0; ++j) {
  3847. const float v = xi[j];
  3848. if (amax < fabsf(v)) {
  3849. amax = fabsf(v);
  3850. vmax = v;
  3851. }
  3852. }
  3853. const float d = vmax / -8;
  3854. const float id = d ? 1.0f/d : 0.0f;
  3855. dsti->d = d;
  3856. for (int j = 0; j < QK4_0/2; ++j) {
  3857. const float x0 = xi[0 + j]*id;
  3858. const float x1 = xi[QK4_0/2 + j]*id;
  3859. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  3860. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  3861. dsti->qs[j] = xi0;
  3862. dsti->qs[j] |= xi1 << 4;
  3863. }
  3864. }
  3865. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  3866. const float * xi = (const float *) cxi;
  3867. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  3868. float vmin = FLT_MAX;
  3869. float vmax = -FLT_MAX;
  3870. for (int j = 0; j < QK4_1; ++j) {
  3871. const float v = xi[j];
  3872. if (v < vmin) vmin = v;
  3873. if (v > vmax) vmax = v;
  3874. }
  3875. const float d = (vmax - vmin) / ((1 << 4) - 1);
  3876. const float id = d ? 1.0f/d : 0.0f;
  3877. dsti->dm.x = d;
  3878. dsti->dm.y = vmin;
  3879. for (int j = 0; j < QK4_1/2; ++j) {
  3880. const float x0 = (xi[0 + j] - vmin)*id;
  3881. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  3882. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  3883. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  3884. dsti->qs[j] = xi0;
  3885. dsti->qs[j] |= xi1 << 4;
  3886. }
  3887. }
  3888. template <cpy_kernel_t cpy_blck, int qk>
  3889. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  3890. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3891. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3892. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  3893. if (i >= ne) {
  3894. return;
  3895. }
  3896. const int i02 = i / (ne00*ne01);
  3897. const int i01 = (i - i02*ne01*ne00) / ne00;
  3898. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  3899. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3900. const int i12 = i / (ne10*ne11);
  3901. const int i11 = (i - i12*ne10*ne11) / ne10;
  3902. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  3903. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3904. cpy_blck(cx + x_offset, cdst + dst_offset);
  3905. }
  3906. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3907. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3908. return 1.0f - min(1.0f, max(0.0f, y));
  3909. }
  3910. struct rope_corr_dims {
  3911. float v[4];
  3912. };
  3913. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3914. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3915. static __device__ void rope_yarn(
  3916. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3917. float * cos_theta, float * sin_theta
  3918. ) {
  3919. // Get n-d rotational scaling corrected for extrapolation
  3920. float theta_interp = freq_scale * theta_extrap;
  3921. float theta = theta_interp;
  3922. if (ext_factor != 0.0f) {
  3923. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3924. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3925. // Get n-d magnitude scaling corrected for interpolation
  3926. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3927. }
  3928. *cos_theta = cosf(theta) * mscale;
  3929. *sin_theta = sinf(theta) * mscale;
  3930. }
  3931. // rope == RoPE == rotary positional embedding
  3932. template<typename T, bool has_pos>
  3933. static __global__ void rope(
  3934. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3935. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3936. ) {
  3937. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3938. if (col >= ncols) {
  3939. return;
  3940. }
  3941. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3942. const int i = row*ncols + col;
  3943. const int i2 = row/p_delta_rows;
  3944. const int p = has_pos ? pos[i2] : 0;
  3945. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3946. float cos_theta, sin_theta;
  3947. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3948. const float x0 = x[i + 0];
  3949. const float x1 = x[i + 1];
  3950. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3951. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3952. }
  3953. template<typename T, bool has_pos>
  3954. static __global__ void rope_neox(
  3955. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  3956. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  3957. ) {
  3958. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3959. if (col >= ncols) {
  3960. return;
  3961. }
  3962. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3963. const int ib = col / n_dims;
  3964. const int ic = col % n_dims;
  3965. if (ib > 0) {
  3966. const int i = row*ncols + ib*n_dims + ic;
  3967. dst[i + 0] = x[i + 0];
  3968. dst[i + 1] = x[i + 1];
  3969. return;
  3970. }
  3971. const int i = row*ncols + ib*n_dims + ic/2;
  3972. const int i2 = row/p_delta_rows;
  3973. float cur_rot = inv_ndims * ic - ib;
  3974. const int p = has_pos ? pos[i2] : 0;
  3975. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  3976. float cos_theta, sin_theta;
  3977. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3978. const float x0 = x[i + 0];
  3979. const float x1 = x[i + n_dims/2];
  3980. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3981. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  3982. }
  3983. static __global__ void rope_glm_f32(
  3984. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3985. int n_ctx
  3986. ) {
  3987. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3988. const int half_n_dims = ncols/4;
  3989. if (col >= half_n_dims) {
  3990. return;
  3991. }
  3992. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3993. const int i = row*ncols + col;
  3994. const int i2 = row/p_delta_rows;
  3995. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  3996. // FIXME: this is likely wrong
  3997. const int p = pos != nullptr ? pos[i2] : 0;
  3998. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  3999. const float sin_theta = sinf(theta);
  4000. const float cos_theta = cosf(theta);
  4001. const float x0 = x[i + 0];
  4002. const float x1 = x[i + half_n_dims];
  4003. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4004. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4005. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4006. const float sin_block_theta = sinf(block_theta);
  4007. const float cos_block_theta = cosf(block_theta);
  4008. const float x2 = x[i + half_n_dims * 2];
  4009. const float x3 = x[i + half_n_dims * 3];
  4010. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4011. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4012. }
  4013. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4014. const int n_heads_log2_floor, const float m0, const float m1) {
  4015. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4016. if (col >= ncols) {
  4017. return;
  4018. }
  4019. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4020. const int i = row*ncols + col;
  4021. const int k = row/k_rows;
  4022. float m_k;
  4023. if (k < n_heads_log2_floor) {
  4024. m_k = powf(m0, k + 1);
  4025. } else {
  4026. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4027. }
  4028. dst[i] = col * m_k + x[i];
  4029. }
  4030. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4031. const int row = blockIdx.y;
  4032. const int col = threadIdx.x;
  4033. float sum = 0.0f;
  4034. for (int i = col; i < ncols; i += blockDim.x) {
  4035. sum += x[row * ncols + i];
  4036. }
  4037. sum = warp_reduce_sum(sum);
  4038. if (col == 0) {
  4039. dst[row] = sum;
  4040. }
  4041. }
  4042. template<typename T>
  4043. static inline __device__ void swap(T & a, T & b) {
  4044. T tmp = a;
  4045. a = b;
  4046. b = tmp;
  4047. }
  4048. template<ggml_sort_order order>
  4049. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4050. // bitonic sort
  4051. int col = threadIdx.x;
  4052. int row = blockIdx.y;
  4053. if (col >= ncols) return;
  4054. const float * x_row = x + row * ncols;
  4055. int * dst_row = dst + row * ncols;
  4056. // initialize indices
  4057. if (col < ncols) {
  4058. dst_row[col] = col;
  4059. }
  4060. __syncthreads();
  4061. for (int k = 2; k <= ncols; k *= 2) {
  4062. for (int j = k / 2; j > 0; j /= 2) {
  4063. int ixj = col ^ j;
  4064. if (ixj > col) {
  4065. if ((col & k) == 0) {
  4066. 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]]) {
  4067. swap(dst_row[col], dst_row[ixj]);
  4068. }
  4069. } else {
  4070. 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]]) {
  4071. swap(dst_row[col], dst_row[ixj]);
  4072. }
  4073. }
  4074. }
  4075. __syncthreads();
  4076. }
  4077. }
  4078. }
  4079. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4080. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4081. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4082. if (col >= ncols) {
  4083. return;
  4084. }
  4085. const int i = row*ncols + col;
  4086. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4087. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4088. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4089. }
  4090. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {
  4091. const int tid = threadIdx.x;
  4092. const int rowx = blockIdx.x;
  4093. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4094. const int block_size = blockDim.x;
  4095. const int warp_id = threadIdx.x / WARP_SIZE;
  4096. const int lane_id = threadIdx.x % WARP_SIZE;
  4097. __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];
  4098. float max_val = -INFINITY;
  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. max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));
  4103. }
  4104. // find the max value in the block
  4105. max_val = warp_reduce_max(max_val);
  4106. if (block_size > WARP_SIZE) {
  4107. if (warp_id == 0) {
  4108. buf[lane_id] = -INFINITY;
  4109. }
  4110. __syncthreads();
  4111. if (lane_id == 0) {
  4112. buf[warp_id] = max_val;
  4113. }
  4114. __syncthreads();
  4115. max_val = buf[lane_id];
  4116. max_val = warp_reduce_max(max_val);
  4117. }
  4118. float tmp = 0.f;
  4119. for (int col = tid; col < ncols; col += block_size) {
  4120. const int ix = rowx*ncols + col;
  4121. const int iy = rowy*ncols + col;
  4122. const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);
  4123. tmp += val;
  4124. dst[ix] = val;
  4125. }
  4126. // find the sum of exps in the block
  4127. tmp = warp_reduce_sum(tmp);
  4128. if (block_size > WARP_SIZE) {
  4129. if (warp_id == 0) {
  4130. buf[lane_id] = 0.f;
  4131. }
  4132. __syncthreads();
  4133. if (lane_id == 0) {
  4134. buf[warp_id] = tmp;
  4135. }
  4136. __syncthreads();
  4137. tmp = buf[lane_id];
  4138. tmp = warp_reduce_sum(tmp);
  4139. }
  4140. const float inv_tmp = 1.f / tmp;
  4141. for (int col = tid; col < ncols; col += block_size) {
  4142. const int i = rowx*ncols + col;
  4143. dst[i] *= inv_tmp;
  4144. }
  4145. }
  4146. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4147. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4148. if (i >= k) {
  4149. return;
  4150. }
  4151. dst[i] = scale * x[i];
  4152. }
  4153. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4154. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4155. if (i >= k) {
  4156. return;
  4157. }
  4158. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4159. }
  4160. static __global__ void im2col_f32_f16(
  4161. const float * x, half * dst,
  4162. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4163. int s0, int s1, int p0, int p1, int d0, int d1) {
  4164. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4165. if (i >= pelements) {
  4166. return;
  4167. }
  4168. const int ksize = OW * (KH > 1 ? KW : 1);
  4169. const int kx = i / ksize;
  4170. const int kd = kx * ksize;
  4171. const int ky = (i - kd) / OW;
  4172. const int ix = i % OW;
  4173. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4174. const int64_t iih = blockIdx.y * s1 + ky * d1 - p1;
  4175. const int64_t offset_dst =
  4176. (blockIdx.y * OW + ix) * CHW +
  4177. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4178. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4179. dst[offset_dst] = __float2half(0.0f);
  4180. } else {
  4181. const int64_t offset_src = blockIdx.z * offset_delta;
  4182. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4183. }
  4184. }
  4185. template<int qk, int qr, dequantize_kernel_t dq>
  4186. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4187. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4188. GGML_TENSOR_BINARY_OP_LOCALS
  4189. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4190. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4191. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4192. // strides in elements
  4193. //const size_t s0 = nb0 / ggml_element_size(dst);
  4194. const size_t s1 = nb1 / ggml_element_size(dst);
  4195. const size_t s2 = nb2 / ggml_element_size(dst);
  4196. const size_t s3 = nb3 / ggml_element_size(dst);
  4197. const size_t s10 = nb10 / ggml_element_size(src1);
  4198. const size_t s11 = nb11 / ggml_element_size(src1);
  4199. const size_t s12 = nb12 / ggml_element_size(src1);
  4200. //const size_t s13 = nb13 / ggml_element_size(src1);
  4201. GGML_ASSERT(ne00 % 2 == 0);
  4202. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4203. src0_dd, src1_dd, dst_dd,
  4204. ne00, /*ne01, ne02, ne03,*/
  4205. /*ne10, ne11,*/ ne12, /*ne13,*/
  4206. /* s0,*/ s1, s2, s3,
  4207. /* nb00,*/ nb01, nb02, nb03,
  4208. s10, s11, s12/*, s13*/);
  4209. (void) dst;
  4210. }
  4211. template<typename src0_t>
  4212. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4213. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4214. GGML_TENSOR_BINARY_OP_LOCALS
  4215. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4216. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4217. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4218. // strides in elements
  4219. //const size_t s0 = nb0 / ggml_element_size(dst);
  4220. const size_t s1 = nb1 / ggml_element_size(dst);
  4221. const size_t s2 = nb2 / ggml_element_size(dst);
  4222. const size_t s3 = nb3 / ggml_element_size(dst);
  4223. const size_t s10 = nb10 / ggml_element_size(src1);
  4224. const size_t s11 = nb11 / ggml_element_size(src1);
  4225. const size_t s12 = nb12 / ggml_element_size(src1);
  4226. //const size_t s13 = nb13 / ggml_element_size(src1);
  4227. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4228. src0_dd, src1_dd, dst_dd,
  4229. ne00, /*ne01, ne02, ne03,*/
  4230. /*ne10, ne11,*/ ne12, /*ne13,*/
  4231. /* s0,*/ s1, s2, s3,
  4232. /* nb00,*/ nb01, nb02, nb03,
  4233. s10, s11, s12/*, s13*/);
  4234. (void) dst;
  4235. }
  4236. template<float (*bin_op)(const float, const float)>
  4237. struct bin_bcast_cuda {
  4238. template<typename src0_t, typename src1_t, typename dst_t>
  4239. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4240. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4241. cudaStream_t stream) {
  4242. GGML_TENSOR_BINARY_OP_LOCALS
  4243. int nr0 = ne10/ne0;
  4244. int nr1 = ne11/ne1;
  4245. int nr2 = ne12/ne2;
  4246. int nr3 = ne13/ne3;
  4247. int nr[4] = { nr0, nr1, nr2, nr3 };
  4248. // collapse dimensions until first broadcast dimension
  4249. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4250. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4251. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4252. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4253. auto collapse = [](int64_t cne[]) {
  4254. cne[0] *= cne[1];
  4255. cne[1] = cne[2];
  4256. cne[2] = cne[3];
  4257. cne[3] = 1;
  4258. };
  4259. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  4260. cnb[1] *= cne[1];
  4261. cnb[2] *= cne[2];
  4262. cnb[3] *= cne[3];
  4263. };
  4264. for (int i = 0; i < 4; i++) {
  4265. if (nr[i] != 1) {
  4266. break;
  4267. }
  4268. if (i > 0) {
  4269. collapse_nb(cnb0, cne0);
  4270. collapse_nb(cnb1, cne1);
  4271. collapse(cne0);
  4272. collapse(cne1);
  4273. }
  4274. }
  4275. {
  4276. int64_t ne0 = cne0[0];
  4277. int64_t ne1 = cne0[1];
  4278. int64_t ne2 = cne0[2];
  4279. int64_t ne3 = cne0[3];
  4280. int64_t ne10 = cne1[0];
  4281. int64_t ne11 = cne1[1];
  4282. int64_t ne12 = cne1[2];
  4283. int64_t ne13 = cne1[3];
  4284. size_t nb0 = cnb0[0];
  4285. size_t nb1 = cnb0[1];
  4286. size_t nb2 = cnb0[2];
  4287. size_t nb3 = cnb0[3];
  4288. size_t nb10 = cnb1[0];
  4289. size_t nb11 = cnb1[1];
  4290. size_t nb12 = cnb1[2];
  4291. size_t nb13 = cnb1[3];
  4292. size_t s0 = nb0 / sizeof(dst_t);
  4293. size_t s1 = nb1 / sizeof(dst_t);
  4294. size_t s2 = nb2 / sizeof(dst_t);
  4295. size_t s3 = nb3 / sizeof(dst_t);
  4296. size_t s10 = nb10 / sizeof(src1_t);
  4297. size_t s11 = nb11 / sizeof(src1_t);
  4298. size_t s12 = nb12 / sizeof(src1_t);
  4299. size_t s13 = nb13 / sizeof(src1_t);
  4300. GGML_ASSERT(s0 == 1);
  4301. GGML_ASSERT(s10 == 1);
  4302. const int block_size = 128;
  4303. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4304. dim3 block_dims;
  4305. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4306. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4307. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4308. dim3 block_nums(
  4309. (hne0 + block_dims.x - 1) / block_dims.x,
  4310. (ne1 + block_dims.y - 1) / block_dims.y,
  4311. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4312. );
  4313. if (block_nums.z > 65535) {
  4314. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4315. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4316. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4317. src0_dd, src1_dd, dst_dd,
  4318. ne0, ne1, ne2, ne3,
  4319. ne10, ne11, ne12, ne13,
  4320. /* s0, */ s1, s2, s3,
  4321. /* s10, */ s11, s12, s13);
  4322. } else {
  4323. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4324. src0_dd, src1_dd, dst_dd,
  4325. ne0, ne1, ne2, ne3,
  4326. ne10, ne11, ne12, ne13,
  4327. /* s0, */ s1, s2, s3,
  4328. /* s10, */ s11, s12, s13);
  4329. }
  4330. }
  4331. }
  4332. };
  4333. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4334. const int ne10, const int ne11, const int ne12,
  4335. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4336. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4337. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4338. }
  4339. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4340. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4341. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4342. }
  4343. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4344. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4345. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4346. }
  4347. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4348. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4349. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4350. }
  4351. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4352. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4353. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4354. }
  4355. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4356. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4357. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4358. }
  4359. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4360. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4361. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4362. }
  4363. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4364. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4365. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4366. }
  4367. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4368. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4369. if (ncols < 1024) {
  4370. const dim3 block_dims(WARP_SIZE, 1, 1);
  4371. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4372. } else {
  4373. const dim3 block_dims(1024, 1, 1);
  4374. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4375. }
  4376. }
  4377. 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) {
  4378. static const float eps = 1e-6f;
  4379. if (group_size < 1024) {
  4380. const dim3 block_dims(WARP_SIZE, 1, 1);
  4381. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4382. } else {
  4383. const dim3 block_dims(1024, 1, 1);
  4384. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4385. }
  4386. }
  4387. 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) {
  4388. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4389. dim3 gridDim(num_blocks, ne1, ne2);
  4390. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4391. }
  4392. 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) {
  4393. int ne0 = (ne00 * scale_factor);
  4394. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4395. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4396. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4397. }
  4398. static void pad_f32_cuda(const float * x, float * dst,
  4399. const int ne00, const int ne01, const int ne02,
  4400. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4401. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4402. dim3 gridDim(num_blocks, ne1, ne2);
  4403. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4404. }
  4405. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4406. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4407. if (ncols < 1024) {
  4408. const dim3 block_dims(WARP_SIZE, 1, 1);
  4409. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4410. } else {
  4411. const dim3 block_dims(1024, 1, 1);
  4412. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4413. }
  4414. }
  4415. 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) {
  4416. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4417. const dim3 num_blocks(block_num_x, ky, 1);
  4418. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4419. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4420. }
  4421. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4422. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4423. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4424. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4425. }
  4426. template<typename dst_t>
  4427. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4428. const int nb = k / QK_K;
  4429. #if QK_K == 256
  4430. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4431. #else
  4432. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4433. #endif
  4434. }
  4435. template<typename dst_t>
  4436. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4437. const int nb = k / QK_K;
  4438. #if QK_K == 256
  4439. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4440. #else
  4441. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4442. #endif
  4443. }
  4444. template<typename dst_t>
  4445. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4446. const int nb = k / QK_K;
  4447. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4448. }
  4449. template<typename dst_t>
  4450. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4451. const int nb = k / QK_K;
  4452. #if QK_K == 256
  4453. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4454. #else
  4455. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4456. #endif
  4457. }
  4458. template<typename dst_t>
  4459. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4460. const int nb = k / QK_K;
  4461. #if QK_K == 256
  4462. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4463. #else
  4464. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4465. #endif
  4466. }
  4467. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4468. switch (type) {
  4469. case GGML_TYPE_Q4_0:
  4470. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4471. case GGML_TYPE_Q4_1:
  4472. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4473. case GGML_TYPE_Q5_0:
  4474. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4475. case GGML_TYPE_Q5_1:
  4476. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4477. case GGML_TYPE_Q8_0:
  4478. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4479. case GGML_TYPE_Q2_K:
  4480. return dequantize_row_q2_K_cuda;
  4481. case GGML_TYPE_Q3_K:
  4482. return dequantize_row_q3_K_cuda;
  4483. case GGML_TYPE_Q4_K:
  4484. return dequantize_row_q4_K_cuda;
  4485. case GGML_TYPE_Q5_K:
  4486. return dequantize_row_q5_K_cuda;
  4487. case GGML_TYPE_Q6_K:
  4488. return dequantize_row_q6_K_cuda;
  4489. case GGML_TYPE_F32:
  4490. return dequantize_block_cuda<1, 1, convert_f32>;
  4491. default:
  4492. return nullptr;
  4493. }
  4494. }
  4495. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4496. switch (type) {
  4497. case GGML_TYPE_Q4_0:
  4498. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4499. case GGML_TYPE_Q4_1:
  4500. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4501. case GGML_TYPE_Q5_0:
  4502. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4503. case GGML_TYPE_Q5_1:
  4504. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4505. case GGML_TYPE_Q8_0:
  4506. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4507. case GGML_TYPE_Q2_K:
  4508. return dequantize_row_q2_K_cuda;
  4509. case GGML_TYPE_Q3_K:
  4510. return dequantize_row_q3_K_cuda;
  4511. case GGML_TYPE_Q4_K:
  4512. return dequantize_row_q4_K_cuda;
  4513. case GGML_TYPE_Q5_K:
  4514. return dequantize_row_q5_K_cuda;
  4515. case GGML_TYPE_Q6_K:
  4516. return dequantize_row_q6_K_cuda;
  4517. case GGML_TYPE_F16:
  4518. return dequantize_block_cuda<1, 1, convert_f16>;
  4519. default:
  4520. return nullptr;
  4521. }
  4522. }
  4523. 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) {
  4524. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4525. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4526. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4527. const dim3 block_nums(block_num_y, 1, 1);
  4528. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4529. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4530. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4531. }
  4532. 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) {
  4533. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4534. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4535. const dim3 block_nums(block_num_y, 1, 1);
  4536. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4537. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4538. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4539. }
  4540. 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) {
  4541. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4542. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4543. const dim3 block_nums(block_num_y, 1, 1);
  4544. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4545. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4546. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4547. }
  4548. 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) {
  4549. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4550. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4551. const dim3 block_nums(block_num_y, 1, 1);
  4552. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4553. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4554. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4555. }
  4556. 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) {
  4557. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4558. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4559. const dim3 block_nums(block_num_y, 1, 1);
  4560. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4561. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4562. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4563. }
  4564. 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) {
  4565. GGML_ASSERT(ncols % QK_K == 0);
  4566. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4567. const int block_num_y = (nrows + ny - 1) / ny;
  4568. const dim3 block_nums(block_num_y, 1, 1);
  4569. const dim3 block_dims(32, ny, 1);
  4570. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4571. }
  4572. 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) {
  4573. GGML_ASSERT(ncols % QK_K == 0);
  4574. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4575. const int block_num_y = (nrows + ny - 1) / ny;
  4576. const dim3 block_nums(block_num_y, 1, 1);
  4577. const dim3 block_dims(32, ny, 1);
  4578. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4579. }
  4580. 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) {
  4581. GGML_ASSERT(ncols % QK_K == 0);
  4582. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4583. const int block_num_y = (nrows + ny - 1) / ny;
  4584. const dim3 block_nums(block_num_y, 1, 1);
  4585. const dim3 block_dims(32, ny, 1);
  4586. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4587. }
  4588. 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) {
  4589. GGML_ASSERT(ncols % QK_K == 0);
  4590. const dim3 block_dims(32, 1, 1);
  4591. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4592. }
  4593. 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) {
  4594. GGML_ASSERT(ncols % QK_K == 0);
  4595. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4596. const int block_num_y = (nrows + ny - 1) / ny;
  4597. const dim3 block_nums(block_num_y, 1, 1);
  4598. const dim3 block_dims(32, ny, 1);
  4599. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4600. }
  4601. 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) {
  4602. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4603. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4604. const dim3 block_nums(block_num_y, 1, 1);
  4605. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4606. dequantize_mul_mat_vec<1, 1, convert_f16>
  4607. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4608. }
  4609. 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) {
  4610. GGML_ASSERT(ncols % QK4_0 == 0);
  4611. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4612. const dim3 block_nums(block_num_y, 1, 1);
  4613. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4614. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4615. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4616. }
  4617. 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) {
  4618. GGML_ASSERT(ncols % QK4_1 == 0);
  4619. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4620. const dim3 block_nums(block_num_y, 1, 1);
  4621. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4622. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4623. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4624. }
  4625. 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) {
  4626. GGML_ASSERT(ncols % QK5_0 == 0);
  4627. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4628. const dim3 block_nums(block_num_y, 1, 1);
  4629. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4630. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4631. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4632. }
  4633. 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) {
  4634. GGML_ASSERT(ncols % QK5_1 == 0);
  4635. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4636. const dim3 block_nums(block_num_y, 1, 1);
  4637. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4638. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4639. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4640. }
  4641. 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) {
  4642. GGML_ASSERT(ncols % QK8_0 == 0);
  4643. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4644. const dim3 block_nums(block_num_y, 1, 1);
  4645. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4646. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4647. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4648. }
  4649. 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) {
  4650. GGML_ASSERT(ncols % QK_K == 0);
  4651. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4652. const dim3 block_nums(block_num_y, 1, 1);
  4653. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4654. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  4655. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4656. }
  4657. 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) {
  4658. GGML_ASSERT(ncols % QK_K == 0);
  4659. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4660. const dim3 block_nums(block_num_y, 1, 1);
  4661. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4662. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4663. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4664. }
  4665. 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) {
  4666. GGML_ASSERT(ncols % QK_K == 0);
  4667. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4668. const dim3 block_nums(block_num_y, 1, 1);
  4669. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4670. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4671. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4672. }
  4673. 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) {
  4674. GGML_ASSERT(ncols % QK_K == 0);
  4675. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4676. const dim3 block_nums(block_num_y, 1, 1);
  4677. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4678. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4679. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4680. }
  4681. 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) {
  4682. GGML_ASSERT(ncols % QK_K == 0);
  4683. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4684. const dim3 block_nums(block_num_y, 1, 1);
  4685. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4686. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4687. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4688. }
  4689. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4690. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4691. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4692. int id;
  4693. CUDA_CHECK(cudaGetDevice(&id));
  4694. const int compute_capability = g_device_caps[id].cc;
  4695. int mmq_x, mmq_y, nwarps;
  4696. if (compute_capability >= CC_RDNA2) {
  4697. mmq_x = MMQ_X_Q4_0_RDNA2;
  4698. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4699. nwarps = NWARPS_Q4_0_RDNA2;
  4700. } else if (compute_capability >= CC_OFFSET_AMD) {
  4701. mmq_x = MMQ_X_Q4_0_RDNA1;
  4702. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4703. nwarps = NWARPS_Q4_0_RDNA1;
  4704. } else if (compute_capability >= CC_VOLTA) {
  4705. mmq_x = MMQ_X_Q4_0_AMPERE;
  4706. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4707. nwarps = NWARPS_Q4_0_AMPERE;
  4708. } else if (compute_capability >= MIN_CC_DP4A) {
  4709. mmq_x = MMQ_X_Q4_0_PASCAL;
  4710. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4711. nwarps = NWARPS_Q4_0_PASCAL;
  4712. } else {
  4713. GGML_ASSERT(false);
  4714. }
  4715. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4716. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4717. const dim3 block_nums(block_num_x, block_num_y, 1);
  4718. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4719. if (nrows_x % mmq_y == 0) {
  4720. const bool need_check = false;
  4721. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4722. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4723. } else {
  4724. const bool need_check = true;
  4725. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4726. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4727. }
  4728. }
  4729. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4730. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4731. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4732. int id;
  4733. CUDA_CHECK(cudaGetDevice(&id));
  4734. const int compute_capability = g_device_caps[id].cc;
  4735. int mmq_x, mmq_y, nwarps;
  4736. if (compute_capability >= CC_RDNA2) {
  4737. mmq_x = MMQ_X_Q4_1_RDNA2;
  4738. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4739. nwarps = NWARPS_Q4_1_RDNA2;
  4740. } else if (compute_capability >= CC_OFFSET_AMD) {
  4741. mmq_x = MMQ_X_Q4_1_RDNA1;
  4742. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4743. nwarps = NWARPS_Q4_1_RDNA1;
  4744. } else if (compute_capability >= CC_VOLTA) {
  4745. mmq_x = MMQ_X_Q4_1_AMPERE;
  4746. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4747. nwarps = NWARPS_Q4_1_AMPERE;
  4748. } else if (compute_capability >= MIN_CC_DP4A) {
  4749. mmq_x = MMQ_X_Q4_1_PASCAL;
  4750. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4751. nwarps = NWARPS_Q4_1_PASCAL;
  4752. } else {
  4753. GGML_ASSERT(false);
  4754. }
  4755. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4756. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4757. const dim3 block_nums(block_num_x, block_num_y, 1);
  4758. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4759. if (nrows_x % mmq_y == 0) {
  4760. const bool need_check = false;
  4761. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4762. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4763. } else {
  4764. const bool need_check = true;
  4765. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4766. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4767. }
  4768. }
  4769. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4770. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4771. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4772. int id;
  4773. CUDA_CHECK(cudaGetDevice(&id));
  4774. const int compute_capability = g_device_caps[id].cc;
  4775. int mmq_x, mmq_y, nwarps;
  4776. if (compute_capability >= CC_RDNA2) {
  4777. mmq_x = MMQ_X_Q5_0_RDNA2;
  4778. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4779. nwarps = NWARPS_Q5_0_RDNA2;
  4780. } else if (compute_capability >= CC_OFFSET_AMD) {
  4781. mmq_x = MMQ_X_Q5_0_RDNA1;
  4782. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4783. nwarps = NWARPS_Q5_0_RDNA1;
  4784. } else if (compute_capability >= CC_VOLTA) {
  4785. mmq_x = MMQ_X_Q5_0_AMPERE;
  4786. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4787. nwarps = NWARPS_Q5_0_AMPERE;
  4788. } else if (compute_capability >= MIN_CC_DP4A) {
  4789. mmq_x = MMQ_X_Q5_0_PASCAL;
  4790. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4791. nwarps = NWARPS_Q5_0_PASCAL;
  4792. } else {
  4793. GGML_ASSERT(false);
  4794. }
  4795. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4796. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4797. const dim3 block_nums(block_num_x, block_num_y, 1);
  4798. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4799. if (nrows_x % mmq_y == 0) {
  4800. const bool need_check = false;
  4801. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4802. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4803. } else {
  4804. const bool need_check = true;
  4805. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4806. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4807. }
  4808. }
  4809. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4810. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4811. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4812. int id;
  4813. CUDA_CHECK(cudaGetDevice(&id));
  4814. const int compute_capability = g_device_caps[id].cc;
  4815. int mmq_x, mmq_y, nwarps;
  4816. if (compute_capability >= CC_RDNA2) {
  4817. mmq_x = MMQ_X_Q5_1_RDNA2;
  4818. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4819. nwarps = NWARPS_Q5_1_RDNA2;
  4820. } else if (compute_capability >= CC_OFFSET_AMD) {
  4821. mmq_x = MMQ_X_Q5_1_RDNA1;
  4822. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4823. nwarps = NWARPS_Q5_1_RDNA1;
  4824. } else if (compute_capability >= CC_VOLTA) {
  4825. mmq_x = MMQ_X_Q5_1_AMPERE;
  4826. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4827. nwarps = NWARPS_Q5_1_AMPERE;
  4828. } else if (compute_capability >= MIN_CC_DP4A) {
  4829. mmq_x = MMQ_X_Q5_1_PASCAL;
  4830. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4831. nwarps = NWARPS_Q5_1_PASCAL;
  4832. } else {
  4833. GGML_ASSERT(false);
  4834. }
  4835. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4836. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4837. const dim3 block_nums(block_num_x, block_num_y, 1);
  4838. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4839. if (nrows_x % mmq_y == 0) {
  4840. const bool need_check = false;
  4841. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4842. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4843. } else {
  4844. const bool need_check = true;
  4845. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4846. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4847. }
  4848. }
  4849. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4850. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4851. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4852. int id;
  4853. CUDA_CHECK(cudaGetDevice(&id));
  4854. const int compute_capability = g_device_caps[id].cc;
  4855. int mmq_x, mmq_y, nwarps;
  4856. if (compute_capability >= CC_RDNA2) {
  4857. mmq_x = MMQ_X_Q8_0_RDNA2;
  4858. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4859. nwarps = NWARPS_Q8_0_RDNA2;
  4860. } else if (compute_capability >= CC_OFFSET_AMD) {
  4861. mmq_x = MMQ_X_Q8_0_RDNA1;
  4862. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4863. nwarps = NWARPS_Q8_0_RDNA1;
  4864. } else if (compute_capability >= CC_VOLTA) {
  4865. mmq_x = MMQ_X_Q8_0_AMPERE;
  4866. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4867. nwarps = NWARPS_Q8_0_AMPERE;
  4868. } else if (compute_capability >= MIN_CC_DP4A) {
  4869. mmq_x = MMQ_X_Q8_0_PASCAL;
  4870. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4871. nwarps = NWARPS_Q8_0_PASCAL;
  4872. } else {
  4873. GGML_ASSERT(false);
  4874. }
  4875. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4876. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4877. const dim3 block_nums(block_num_x, block_num_y, 1);
  4878. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4879. if (nrows_x % mmq_y == 0) {
  4880. const bool need_check = false;
  4881. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4882. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4883. } else {
  4884. const bool need_check = true;
  4885. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4886. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4887. }
  4888. }
  4889. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4890. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4891. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4892. int id;
  4893. CUDA_CHECK(cudaGetDevice(&id));
  4894. const int compute_capability = g_device_caps[id].cc;
  4895. int mmq_x, mmq_y, nwarps;
  4896. if (compute_capability >= CC_RDNA2) {
  4897. mmq_x = MMQ_X_Q2_K_RDNA2;
  4898. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4899. nwarps = NWARPS_Q2_K_RDNA2;
  4900. } else if (compute_capability >= CC_OFFSET_AMD) {
  4901. mmq_x = MMQ_X_Q2_K_RDNA1;
  4902. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4903. nwarps = NWARPS_Q2_K_RDNA1;
  4904. } else if (compute_capability >= CC_VOLTA) {
  4905. mmq_x = MMQ_X_Q2_K_AMPERE;
  4906. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4907. nwarps = NWARPS_Q2_K_AMPERE;
  4908. } else if (compute_capability >= MIN_CC_DP4A) {
  4909. mmq_x = MMQ_X_Q2_K_PASCAL;
  4910. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4911. nwarps = NWARPS_Q2_K_PASCAL;
  4912. } else {
  4913. GGML_ASSERT(false);
  4914. }
  4915. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4916. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4917. const dim3 block_nums(block_num_x, block_num_y, 1);
  4918. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4919. if (nrows_x % mmq_y == 0) {
  4920. const bool need_check = false;
  4921. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4922. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4923. } else {
  4924. const bool need_check = true;
  4925. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4926. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4927. }
  4928. }
  4929. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4930. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4931. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4932. #if QK_K == 256
  4933. int id;
  4934. CUDA_CHECK(cudaGetDevice(&id));
  4935. const int compute_capability = g_device_caps[id].cc;
  4936. int mmq_x, mmq_y, nwarps;
  4937. if (compute_capability >= CC_RDNA2) {
  4938. mmq_x = MMQ_X_Q3_K_RDNA2;
  4939. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4940. nwarps = NWARPS_Q3_K_RDNA2;
  4941. } else if (compute_capability >= CC_OFFSET_AMD) {
  4942. mmq_x = MMQ_X_Q3_K_RDNA1;
  4943. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4944. nwarps = NWARPS_Q3_K_RDNA1;
  4945. } else if (compute_capability >= CC_VOLTA) {
  4946. mmq_x = MMQ_X_Q3_K_AMPERE;
  4947. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4948. nwarps = NWARPS_Q3_K_AMPERE;
  4949. } else if (compute_capability >= MIN_CC_DP4A) {
  4950. mmq_x = MMQ_X_Q3_K_PASCAL;
  4951. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4952. nwarps = NWARPS_Q3_K_PASCAL;
  4953. } else {
  4954. GGML_ASSERT(false);
  4955. }
  4956. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4957. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4958. const dim3 block_nums(block_num_x, block_num_y, 1);
  4959. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4960. if (nrows_x % mmq_y == 0) {
  4961. const bool need_check = false;
  4962. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4963. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4964. } else {
  4965. const bool need_check = true;
  4966. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4967. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4968. }
  4969. #endif
  4970. }
  4971. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4972. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4973. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4974. int id;
  4975. CUDA_CHECK(cudaGetDevice(&id));
  4976. const int compute_capability = g_device_caps[id].cc;
  4977. int mmq_x, mmq_y, nwarps;
  4978. if (compute_capability >= CC_RDNA2) {
  4979. mmq_x = MMQ_X_Q4_K_RDNA2;
  4980. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4981. nwarps = NWARPS_Q4_K_RDNA2;
  4982. } else if (compute_capability >= CC_OFFSET_AMD) {
  4983. mmq_x = MMQ_X_Q4_K_RDNA1;
  4984. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4985. nwarps = NWARPS_Q4_K_RDNA1;
  4986. } else if (compute_capability >= CC_VOLTA) {
  4987. mmq_x = MMQ_X_Q4_K_AMPERE;
  4988. mmq_y = MMQ_Y_Q4_K_AMPERE;
  4989. nwarps = NWARPS_Q4_K_AMPERE;
  4990. } else if (compute_capability >= MIN_CC_DP4A) {
  4991. mmq_x = MMQ_X_Q4_K_PASCAL;
  4992. mmq_y = MMQ_Y_Q4_K_PASCAL;
  4993. nwarps = NWARPS_Q4_K_PASCAL;
  4994. } else {
  4995. GGML_ASSERT(false);
  4996. }
  4997. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4998. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4999. const dim3 block_nums(block_num_x, block_num_y, 1);
  5000. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5001. if (nrows_x % mmq_y == 0) {
  5002. const bool need_check = false;
  5003. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5004. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5005. } else {
  5006. const bool need_check = true;
  5007. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5008. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5009. }
  5010. }
  5011. static void ggml_mul_mat_q5_K_q8_1_cuda(
  5012. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5013. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5014. int id;
  5015. CUDA_CHECK(cudaGetDevice(&id));
  5016. const int compute_capability = g_device_caps[id].cc;
  5017. int mmq_x, mmq_y, nwarps;
  5018. if (compute_capability >= CC_RDNA2) {
  5019. mmq_x = MMQ_X_Q5_K_RDNA2;
  5020. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5021. nwarps = NWARPS_Q5_K_RDNA2;
  5022. } else if (compute_capability >= CC_OFFSET_AMD) {
  5023. mmq_x = MMQ_X_Q5_K_RDNA1;
  5024. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5025. nwarps = NWARPS_Q5_K_RDNA1;
  5026. } else if (compute_capability >= CC_VOLTA) {
  5027. mmq_x = MMQ_X_Q5_K_AMPERE;
  5028. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5029. nwarps = NWARPS_Q5_K_AMPERE;
  5030. } else if (compute_capability >= MIN_CC_DP4A) {
  5031. mmq_x = MMQ_X_Q5_K_PASCAL;
  5032. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5033. nwarps = NWARPS_Q5_K_PASCAL;
  5034. } else {
  5035. GGML_ASSERT(false);
  5036. }
  5037. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5038. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5039. const dim3 block_nums(block_num_x, block_num_y, 1);
  5040. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5041. if (nrows_x % mmq_y == 0) {
  5042. const bool need_check = false;
  5043. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5044. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5045. } else {
  5046. const bool need_check = true;
  5047. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5048. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5049. }
  5050. }
  5051. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5052. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5053. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5054. int id;
  5055. CUDA_CHECK(cudaGetDevice(&id));
  5056. const int compute_capability = g_device_caps[id].cc;
  5057. int mmq_x, mmq_y, nwarps;
  5058. if (compute_capability >= CC_RDNA2) {
  5059. mmq_x = MMQ_X_Q6_K_RDNA2;
  5060. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5061. nwarps = NWARPS_Q6_K_RDNA2;
  5062. } else if (compute_capability >= CC_OFFSET_AMD) {
  5063. mmq_x = MMQ_X_Q6_K_RDNA1;
  5064. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5065. nwarps = NWARPS_Q6_K_RDNA1;
  5066. } else if (compute_capability >= CC_VOLTA) {
  5067. mmq_x = MMQ_X_Q6_K_AMPERE;
  5068. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5069. nwarps = NWARPS_Q6_K_AMPERE;
  5070. } else if (compute_capability >= MIN_CC_DP4A) {
  5071. mmq_x = MMQ_X_Q6_K_PASCAL;
  5072. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5073. nwarps = NWARPS_Q6_K_PASCAL;
  5074. } else {
  5075. GGML_ASSERT(false);
  5076. }
  5077. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5078. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5079. const dim3 block_nums(block_num_x, block_num_y, 1);
  5080. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5081. if (nrows_x % mmq_y == 0) {
  5082. const bool need_check = false;
  5083. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5084. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5085. } else {
  5086. const bool need_check = true;
  5087. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5088. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5089. }
  5090. }
  5091. static void ggml_mul_mat_p021_f16_f32_cuda(
  5092. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5093. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5094. const dim3 block_nums(1, nrows_x, nchannels_y);
  5095. const dim3 block_dims(WARP_SIZE, 1, 1);
  5096. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5097. }
  5098. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5099. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5100. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5101. const dim3 block_nums(1, nrows_x, nchannels_y);
  5102. const dim3 block_dims(WARP_SIZE, 1, 1);
  5103. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5104. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5105. }
  5106. static void ggml_cpy_f32_f32_cuda(
  5107. const char * cx, char * cdst, const int ne,
  5108. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5109. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5110. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5111. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5112. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5113. }
  5114. static void ggml_cpy_f32_f16_cuda(
  5115. const char * cx, char * cdst, const int ne,
  5116. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5117. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5118. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5119. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5120. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5121. }
  5122. static void ggml_cpy_f32_q8_0_cuda(
  5123. const char * cx, char * cdst, const int ne,
  5124. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5125. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5126. GGML_ASSERT(ne % QK8_0 == 0);
  5127. const int num_blocks = ne / QK8_0;
  5128. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5129. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5130. }
  5131. static void ggml_cpy_f32_q4_0_cuda(
  5132. const char * cx, char * cdst, const int ne,
  5133. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5134. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5135. GGML_ASSERT(ne % QK4_0 == 0);
  5136. const int num_blocks = ne / QK4_0;
  5137. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5138. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5139. }
  5140. static void ggml_cpy_f32_q4_1_cuda(
  5141. const char * cx, char * cdst, const int ne,
  5142. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5143. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5144. GGML_ASSERT(ne % QK4_1 == 0);
  5145. const int num_blocks = ne / QK4_1;
  5146. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5147. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5148. }
  5149. static void ggml_cpy_f16_f16_cuda(
  5150. const char * cx, char * cdst, const int ne,
  5151. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5152. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5153. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5154. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5155. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5156. }
  5157. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5158. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5159. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5160. }
  5161. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5162. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5163. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5164. }
  5165. template<typename T>
  5166. static void rope_cuda(
  5167. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5168. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5169. ) {
  5170. GGML_ASSERT(ncols % 2 == 0);
  5171. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5172. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5173. const dim3 block_nums(nrows, num_blocks_x, 1);
  5174. if (pos == nullptr) {
  5175. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5176. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5177. );
  5178. } else {
  5179. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5180. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5181. );
  5182. }
  5183. }
  5184. template<typename T>
  5185. static void rope_neox_cuda(
  5186. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5187. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5188. ) {
  5189. GGML_ASSERT(ncols % 2 == 0);
  5190. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5191. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5192. const dim3 block_nums(nrows, num_blocks_x, 1);
  5193. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5194. const float inv_ndims = -1.0f / n_dims;
  5195. if (pos == nullptr) {
  5196. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5197. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5198. theta_scale, inv_ndims
  5199. );
  5200. } else {
  5201. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5202. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5203. theta_scale, inv_ndims
  5204. );
  5205. }
  5206. }
  5207. static void rope_glm_f32_cuda(
  5208. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5209. float freq_base, int n_ctx, cudaStream_t stream
  5210. ) {
  5211. GGML_ASSERT(ncols % 4 == 0);
  5212. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5213. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5214. const dim3 block_nums(num_blocks_x, nrows, 1);
  5215. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5216. }
  5217. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5218. const int k_rows, const int n_heads_log2_floor, const float m0,
  5219. const float m1, cudaStream_t stream) {
  5220. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5221. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5222. const dim3 block_nums(num_blocks_x, nrows, 1);
  5223. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5224. }
  5225. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5226. const dim3 block_dims(WARP_SIZE, 1, 1);
  5227. const dim3 block_nums(1, nrows, 1);
  5228. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5229. }
  5230. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5231. // bitonic sort requires ncols to be power of 2
  5232. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5233. const dim3 block_dims(ncols, 1, 1);
  5234. const dim3 block_nums(1, nrows, 1);
  5235. if (order == GGML_SORT_ASC) {
  5236. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5237. } else if (order == GGML_SORT_DESC) {
  5238. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5239. } else {
  5240. GGML_ASSERT(false);
  5241. }
  5242. }
  5243. 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) {
  5244. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5245. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5246. const dim3 block_nums(nrows_x, block_num_x, 1);
  5247. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5248. }
  5249. 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) {
  5250. int nth = WARP_SIZE;
  5251. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5252. const dim3 block_dims(nth, 1, 1);
  5253. const dim3 block_nums(nrows_x, 1, 1);
  5254. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5255. }
  5256. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5257. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5258. int offset_delta,
  5259. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5260. const int parallel_elements = OW * KW * KH;
  5261. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5262. dim3 block_nums(num_blocks, OH, IC);
  5263. 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);
  5264. }
  5265. // buffer pool for cuda
  5266. #define MAX_CUDA_BUFFERS 256
  5267. struct scoped_spin_lock {
  5268. std::atomic_flag& lock;
  5269. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5270. while (lock.test_and_set(std::memory_order_acquire)) {
  5271. ; // spin
  5272. }
  5273. }
  5274. ~scoped_spin_lock() {
  5275. lock.clear(std::memory_order_release);
  5276. }
  5277. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5278. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5279. };
  5280. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5281. // #define DEBUG_CUDA_MALLOC
  5282. struct cuda_buffer {
  5283. void * ptr = nullptr;
  5284. size_t size = 0;
  5285. };
  5286. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5287. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  5288. static void * ggml_cuda_pool_malloc_leg(size_t size, size_t * actual_size) {
  5289. scoped_spin_lock lock(g_cuda_pool_lock);
  5290. int id;
  5291. CUDA_CHECK(cudaGetDevice(&id));
  5292. #ifdef DEBUG_CUDA_MALLOC
  5293. int nnz = 0;
  5294. size_t max_size = 0;
  5295. #endif
  5296. size_t best_diff = 1ull << 36;
  5297. int ibest = -1;
  5298. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5299. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5300. if (b.ptr != nullptr) {
  5301. #ifdef DEBUG_CUDA_MALLOC
  5302. ++nnz;
  5303. if (b.size > max_size) max_size = b.size;
  5304. #endif
  5305. if (b.size >= size) {
  5306. size_t diff = b.size - size;
  5307. if (diff < best_diff) {
  5308. best_diff = diff;
  5309. ibest = i;
  5310. if (!best_diff) {
  5311. void * ptr = b.ptr;
  5312. *actual_size = b.size;
  5313. b.ptr = nullptr;
  5314. b.size = 0;
  5315. return ptr;
  5316. }
  5317. }
  5318. }
  5319. }
  5320. }
  5321. if (ibest >= 0) {
  5322. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  5323. void * ptr = b.ptr;
  5324. *actual_size = b.size;
  5325. b.ptr = nullptr;
  5326. b.size = 0;
  5327. return ptr;
  5328. }
  5329. void * ptr;
  5330. size_t look_ahead_size = (size_t) (1.05 * size);
  5331. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5332. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5333. *actual_size = look_ahead_size;
  5334. g_cuda_pool_size[id] += look_ahead_size;
  5335. #ifdef DEBUG_CUDA_MALLOC
  5336. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  5337. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  5338. #endif
  5339. return ptr;
  5340. }
  5341. static void ggml_cuda_pool_free_leg(void * ptr, size_t size) {
  5342. scoped_spin_lock lock(g_cuda_pool_lock);
  5343. int id;
  5344. CUDA_CHECK(cudaGetDevice(&id));
  5345. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5346. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5347. if (b.ptr == nullptr) {
  5348. b.ptr = ptr;
  5349. b.size = size;
  5350. return;
  5351. }
  5352. }
  5353. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5354. CUDA_CHECK(cudaFree(ptr));
  5355. g_cuda_pool_size[id] -= size;
  5356. }
  5357. #if !defined(GGML_USE_HIPBLAS)
  5358. // pool with virtual memory
  5359. static std::vector<CUmemGenericAllocationHandle> g_cuda_pool_handles[GGML_CUDA_MAX_DEVICES];
  5360. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  5361. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  5362. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 36; // 64 GB
  5363. static void * ggml_cuda_pool_malloc_vmm(size_t size, size_t * actual_size) {
  5364. scoped_spin_lock lock(g_cuda_pool_lock);
  5365. int id;
  5366. CUDA_CHECK(cudaGetDevice(&id));
  5367. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  5368. const size_t alignment = 128;
  5369. size = alignment * ((size + alignment - 1) / alignment);
  5370. size_t avail = g_cuda_pool_size[id] - g_cuda_pool_used[id];
  5371. if (size > avail) {
  5372. // round up to the next multiple of the granularity
  5373. size_t reserve_size = size - avail;
  5374. const size_t granularity = g_device_caps[id].vmm_granularity;
  5375. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  5376. GGML_ASSERT(g_cuda_pool_size[id] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  5377. // allocate more physical memory
  5378. CUmemAllocationProp prop = {};
  5379. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5380. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5381. prop.location.id = id;
  5382. CUmemGenericAllocationHandle handle;
  5383. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  5384. // reserve virtual address space (if not already reserved)
  5385. if (g_cuda_pool_addr[id] == 0) {
  5386. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[id], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  5387. }
  5388. // map at the end of the pool
  5389. CU_CHECK(cuMemMap(g_cuda_pool_addr[id] + g_cuda_pool_size[id], reserve_size, 0, handle, 0));
  5390. // set access
  5391. CUmemAccessDesc access = {};
  5392. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5393. access.location.id = id;
  5394. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  5395. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[id] + g_cuda_pool_size[id], reserve_size, &access, 1));
  5396. // add to the pool
  5397. g_cuda_pool_handles[id].push_back(handle);
  5398. g_cuda_pool_size[id] += reserve_size;
  5399. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  5400. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  5401. // (unsigned long long) (reserve_size/1024/1024));
  5402. }
  5403. GGML_ASSERT(g_cuda_pool_addr[id] != 0);
  5404. void * ptr = (void *) (g_cuda_pool_addr[id] + g_cuda_pool_used[id]);
  5405. *actual_size = size;
  5406. g_cuda_pool_used[id] += size;
  5407. #ifdef DEBUG_CUDA_MALLOC
  5408. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  5409. #endif
  5410. return ptr;
  5411. }
  5412. static void ggml_cuda_pool_free_vmm(void * ptr, size_t size) {
  5413. scoped_spin_lock lock(g_cuda_pool_lock);
  5414. int id;
  5415. CUDA_CHECK(cudaGetDevice(&id));
  5416. #ifdef DEBUG_CUDA_MALLOC
  5417. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  5418. #endif
  5419. g_cuda_pool_used[id] -= size;
  5420. // all deallocations must be in reverse order of the allocations
  5421. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[id] + g_cuda_pool_used[id]));
  5422. }
  5423. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  5424. int id;
  5425. CUDA_CHECK(cudaGetDevice(&id));
  5426. if (g_device_caps[id].vmm) {
  5427. return ggml_cuda_pool_malloc_vmm(size, actual_size);
  5428. } else {
  5429. return ggml_cuda_pool_malloc_leg(size, actual_size);
  5430. }
  5431. }
  5432. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  5433. int id;
  5434. CUDA_CHECK(cudaGetDevice(&id));
  5435. if (g_device_caps[id].vmm) {
  5436. ggml_cuda_pool_free_vmm(ptr, size);
  5437. } else {
  5438. ggml_cuda_pool_free_leg(ptr, size);
  5439. }
  5440. }
  5441. #else
  5442. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  5443. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  5444. #endif // !defined(GGML_USE_HIPBLAS)
  5445. template<typename T>
  5446. struct cuda_pool_alloc {
  5447. T * ptr = nullptr;
  5448. size_t actual_size = 0;
  5449. // size is in number of elements
  5450. T * alloc(size_t size) {
  5451. GGML_ASSERT(ptr == nullptr);
  5452. ptr = (T *) ggml_cuda_pool_malloc(size * sizeof(T), &this->actual_size);
  5453. return ptr;
  5454. }
  5455. cuda_pool_alloc(size_t size) {
  5456. alloc(size);
  5457. }
  5458. ~cuda_pool_alloc() {
  5459. if (ptr != nullptr) {
  5460. ggml_cuda_pool_free(ptr, actual_size);
  5461. }
  5462. }
  5463. T * get() {
  5464. return ptr;
  5465. }
  5466. cuda_pool_alloc() = default;
  5467. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  5468. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  5469. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  5470. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  5471. };
  5472. static bool g_cublas_loaded = false;
  5473. bool ggml_cublas_loaded(void) {
  5474. return g_cublas_loaded;
  5475. }
  5476. void ggml_init_cublas() {
  5477. static bool initialized = false;
  5478. if (!initialized) {
  5479. #ifdef __HIP_PLATFORM_AMD__
  5480. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5481. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5482. rocblas_initialize();
  5483. CUDA_CHECK(cudaDeviceSynchronize());
  5484. #endif
  5485. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5486. initialized = true;
  5487. g_cublas_loaded = false;
  5488. return;
  5489. }
  5490. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5491. int64_t total_vram = 0;
  5492. #if defined(GGML_CUDA_FORCE_MMQ)
  5493. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5494. #else
  5495. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5496. #endif
  5497. #if defined(CUDA_USE_TENSOR_CORES)
  5498. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5499. #else
  5500. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5501. #endif
  5502. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5503. for (int id = 0; id < g_device_count; ++id) {
  5504. int device_vmm = 0;
  5505. #if !defined(GGML_USE_HIPBLAS)
  5506. CUdevice device;
  5507. CU_CHECK(cuDeviceGet(&device, id));
  5508. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  5509. if (device_vmm) {
  5510. CUmemAllocationProp alloc_prop = {};
  5511. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5512. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5513. alloc_prop.location.id = id;
  5514. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM));
  5515. }
  5516. #endif // !defined(GGML_USE_HIPBLAS)
  5517. g_device_caps[id].vmm = !!device_vmm;
  5518. cudaDeviceProp prop;
  5519. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5520. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  5521. g_tensor_split[id] = total_vram;
  5522. total_vram += prop.totalGlobalMem;
  5523. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5524. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5525. #else
  5526. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  5527. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5528. }
  5529. for (int id = 0; id < g_device_count; ++id) {
  5530. g_tensor_split[id] /= total_vram;
  5531. }
  5532. for (int id = 0; id < g_device_count; ++id) {
  5533. CUDA_CHECK(ggml_cuda_set_device(id));
  5534. // create cuda streams
  5535. for (int is = 0; is < MAX_STREAMS; ++is) {
  5536. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5537. }
  5538. // create cublas handle
  5539. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5540. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5541. }
  5542. // configure logging to stdout
  5543. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5544. initialized = true;
  5545. g_cublas_loaded = true;
  5546. }
  5547. }
  5548. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5549. if (tensor_split == nullptr) {
  5550. return;
  5551. }
  5552. bool all_zero = true;
  5553. for (int i = 0; i < g_device_count; ++i) {
  5554. if (tensor_split[i] != 0.0f) {
  5555. all_zero = false;
  5556. break;
  5557. }
  5558. }
  5559. if (all_zero) {
  5560. return;
  5561. }
  5562. float split_sum = 0.0f;
  5563. for (int i = 0; i < g_device_count; ++i) {
  5564. g_tensor_split[i] = split_sum;
  5565. split_sum += tensor_split[i];
  5566. }
  5567. for (int i = 0; i < g_device_count; ++i) {
  5568. g_tensor_split[i] /= split_sum;
  5569. }
  5570. }
  5571. void * ggml_cuda_host_malloc(size_t size) {
  5572. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  5573. return nullptr;
  5574. }
  5575. void * ptr = nullptr;
  5576. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  5577. if (err != cudaSuccess) {
  5578. // clear the error
  5579. cudaGetLastError();
  5580. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  5581. size/1024.0/1024.0, cudaGetErrorString(err));
  5582. return nullptr;
  5583. }
  5584. return ptr;
  5585. }
  5586. void ggml_cuda_host_free(void * ptr) {
  5587. CUDA_CHECK(cudaFreeHost(ptr));
  5588. }
  5589. static cudaError_t ggml_cuda_cpy_tensor_2d(
  5590. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  5591. cudaMemcpyKind kind;
  5592. char * src_ptr;
  5593. if (src->backend == GGML_BACKEND_CPU) {
  5594. kind = cudaMemcpyHostToDevice;
  5595. src_ptr = (char *) src->data;
  5596. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  5597. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  5598. kind = cudaMemcpyDeviceToDevice;
  5599. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  5600. int id;
  5601. CUDA_CHECK(cudaGetDevice(&id));
  5602. src_ptr = (char *) extra->data_device[id];
  5603. } else {
  5604. GGML_ASSERT(false);
  5605. }
  5606. char * dst_ptr = (char *) dst;
  5607. const int64_t ne0 = src->ne[0];
  5608. const int64_t nb0 = src->nb[0];
  5609. const int64_t nb1 = src->nb[1];
  5610. const int64_t nb2 = src->nb[2];
  5611. const int64_t nb3 = src->nb[3];
  5612. const enum ggml_type type = src->type;
  5613. const int64_t ts = ggml_type_size(type);
  5614. const int64_t bs = ggml_blck_size(type);
  5615. int64_t i1_diff = i1_high - i1_low;
  5616. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  5617. if (nb0 == ts && nb1 == ts*ne0/bs) {
  5618. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  5619. } else if (nb0 == ts) {
  5620. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  5621. } else {
  5622. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  5623. const void * rx = (const void *) ((const char *) x + i1*nb1);
  5624. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  5625. // pretend the row is a matrix with cols=1
  5626. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  5627. if (r != cudaSuccess) return r;
  5628. }
  5629. return cudaSuccess;
  5630. }
  5631. }
  5632. static void ggml_cuda_op_get_rows(
  5633. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5634. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & stream) {
  5635. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5636. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5637. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  5638. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  5639. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  5640. const int32_t * src1_i32 = (const int32_t *) src1_d;
  5641. switch (src0->type) {
  5642. case GGML_TYPE_F16:
  5643. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  5644. break;
  5645. case GGML_TYPE_F32:
  5646. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5647. break;
  5648. case GGML_TYPE_Q4_0:
  5649. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5650. break;
  5651. case GGML_TYPE_Q4_1:
  5652. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5653. break;
  5654. case GGML_TYPE_Q5_0:
  5655. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5656. break;
  5657. case GGML_TYPE_Q5_1:
  5658. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5659. break;
  5660. case GGML_TYPE_Q8_0:
  5661. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5662. break;
  5663. default:
  5664. // TODO: k-quants
  5665. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  5666. GGML_ASSERT(false);
  5667. break;
  5668. }
  5669. }
  5670. template<class op>
  5671. inline void ggml_cuda_op_bin_bcast(
  5672. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5673. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5674. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5675. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5676. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5677. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5678. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  5679. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5680. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  5681. } else {
  5682. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  5683. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  5684. GGML_ASSERT(false);
  5685. }
  5686. }
  5687. static void ggml_cuda_op_repeat(
  5688. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5689. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & main_stream) {
  5690. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  5691. (void) src1;
  5692. (void) src1_d;
  5693. }
  5694. inline void ggml_cuda_op_add(
  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_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5698. }
  5699. inline void ggml_cuda_op_acc(
  5700. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5701. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5702. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5703. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5704. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5705. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  5706. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5707. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5708. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5709. int offset = dst->op_params[3] / 4; // offset in bytes
  5710. 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);
  5711. (void) dst;
  5712. }
  5713. inline void ggml_cuda_op_mul(
  5714. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5715. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5716. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5717. }
  5718. inline void ggml_cuda_op_div(
  5719. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5720. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5721. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5722. }
  5723. inline void ggml_cuda_op_gelu(
  5724. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5725. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5726. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5727. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5728. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5729. (void) src1;
  5730. (void) dst;
  5731. (void) src1_dd;
  5732. }
  5733. inline void ggml_cuda_op_silu(
  5734. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5735. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5736. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5737. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5738. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5739. (void) src1;
  5740. (void) dst;
  5741. (void) src1_dd;
  5742. }
  5743. inline void ggml_cuda_op_gelu_quick(
  5744. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5745. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5746. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5747. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5748. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5749. (void) src1;
  5750. (void) dst;
  5751. (void) src1_dd;
  5752. }
  5753. inline void ggml_cuda_op_tanh(
  5754. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5755. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5756. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5757. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5758. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5759. (void) src1;
  5760. (void) dst;
  5761. (void) src1_dd;
  5762. }
  5763. inline void ggml_cuda_op_relu(
  5764. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5765. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5766. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5767. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5768. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5769. (void) src1;
  5770. (void) dst;
  5771. (void) src1_dd;
  5772. }
  5773. inline void ggml_cuda_op_leaky_relu(
  5774. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5775. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5776. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5777. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5778. float negative_slope;
  5779. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5780. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  5781. (void) src1;
  5782. (void) dst;
  5783. (void) src1_dd;
  5784. }
  5785. inline void ggml_cuda_op_sqr(
  5786. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5787. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5788. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5789. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5790. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5791. (void) src1;
  5792. (void) dst;
  5793. (void) src1_dd;
  5794. }
  5795. inline void ggml_cuda_op_norm(
  5796. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5797. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5798. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5799. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5800. const int64_t ne00 = src0->ne[0];
  5801. const int64_t nrows = ggml_nrows(src0);
  5802. float eps;
  5803. memcpy(&eps, dst->op_params, sizeof(float));
  5804. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5805. (void) src1;
  5806. (void) dst;
  5807. (void) src1_dd;
  5808. }
  5809. inline void ggml_cuda_op_group_norm(
  5810. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5811. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5812. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5813. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5814. int num_groups = dst->op_params[0];
  5815. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5816. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  5817. (void) src1;
  5818. (void) dst;
  5819. (void) src1_dd;
  5820. }
  5821. inline void ggml_cuda_op_concat(
  5822. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5823. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5824. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5825. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5826. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5827. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  5828. 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);
  5829. }
  5830. (void) src1;
  5831. (void) dst;
  5832. }
  5833. inline void ggml_cuda_op_upscale(
  5834. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5835. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5836. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5837. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5838. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5839. const int scale_factor = dst->op_params[0];
  5840. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  5841. (void) src1;
  5842. (void) dst;
  5843. (void) src1_dd;
  5844. }
  5845. inline void ggml_cuda_op_pad(
  5846. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5847. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5848. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5849. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5850. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5851. pad_f32_cuda(src0_dd, dst_dd,
  5852. src0->ne[0], src0->ne[1], src0->ne[2],
  5853. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  5854. (void) src1;
  5855. (void) dst;
  5856. (void) src1_dd;
  5857. }
  5858. inline void ggml_cuda_op_rms_norm(
  5859. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5860. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5861. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5862. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5863. const int64_t ne00 = src0->ne[0];
  5864. const int64_t nrows = ggml_nrows(src0);
  5865. float eps;
  5866. memcpy(&eps, dst->op_params, sizeof(float));
  5867. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5868. (void) src1;
  5869. (void) dst;
  5870. (void) src1_dd;
  5871. }
  5872. inline void ggml_cuda_op_mul_mat_q(
  5873. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5874. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5875. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5876. const int64_t ne00 = src0->ne[0];
  5877. const int64_t ne10 = src1->ne[0];
  5878. GGML_ASSERT(ne10 % QK8_1 == 0);
  5879. const int64_t ne0 = dst->ne[0];
  5880. const int64_t row_diff = row_high - row_low;
  5881. int id;
  5882. CUDA_CHECK(cudaGetDevice(&id));
  5883. // the main device has a larger memory buffer to hold the results from all GPUs
  5884. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5885. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5886. switch (src0->type) {
  5887. case GGML_TYPE_Q4_0:
  5888. 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);
  5889. break;
  5890. case GGML_TYPE_Q4_1:
  5891. 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);
  5892. break;
  5893. case GGML_TYPE_Q5_0:
  5894. 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);
  5895. break;
  5896. case GGML_TYPE_Q5_1:
  5897. 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);
  5898. break;
  5899. case GGML_TYPE_Q8_0:
  5900. 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);
  5901. break;
  5902. case GGML_TYPE_Q2_K:
  5903. 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);
  5904. break;
  5905. case GGML_TYPE_Q3_K:
  5906. 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);
  5907. break;
  5908. case GGML_TYPE_Q4_K:
  5909. 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);
  5910. break;
  5911. case GGML_TYPE_Q5_K:
  5912. 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);
  5913. break;
  5914. case GGML_TYPE_Q6_K:
  5915. 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);
  5916. break;
  5917. default:
  5918. GGML_ASSERT(false);
  5919. break;
  5920. }
  5921. (void) src1;
  5922. (void) dst;
  5923. (void) src1_ddf_i;
  5924. }
  5925. static int64_t get_row_rounding(ggml_type type) {
  5926. int64_t min_compute_capability = INT_MAX;
  5927. int64_t max_compute_capability = INT_MIN;
  5928. for (int64_t id = 0; id < g_device_count; ++id) {
  5929. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5930. if (min_compute_capability > g_device_caps[id].cc) {
  5931. min_compute_capability = g_device_caps[id].cc;
  5932. }
  5933. if (max_compute_capability < g_device_caps[id].cc) {
  5934. max_compute_capability = g_device_caps[id].cc;
  5935. }
  5936. }
  5937. }
  5938. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5939. switch(type) {
  5940. case GGML_TYPE_Q4_0:
  5941. case GGML_TYPE_Q4_1:
  5942. case GGML_TYPE_Q5_0:
  5943. case GGML_TYPE_Q5_1:
  5944. case GGML_TYPE_Q8_0:
  5945. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5946. case GGML_TYPE_F16:
  5947. case GGML_TYPE_F32:
  5948. return 1;
  5949. case GGML_TYPE_Q2_K:
  5950. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5951. case GGML_TYPE_Q3_K:
  5952. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5953. case GGML_TYPE_Q4_K:
  5954. case GGML_TYPE_Q5_K:
  5955. case GGML_TYPE_Q6_K:
  5956. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5957. default:
  5958. GGML_ASSERT(false);
  5959. }
  5960. #else
  5961. switch(type) {
  5962. case GGML_TYPE_Q4_0:
  5963. case GGML_TYPE_Q4_1:
  5964. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5965. case GGML_TYPE_Q5_0:
  5966. case GGML_TYPE_Q5_1:
  5967. case GGML_TYPE_Q8_0:
  5968. return 64;
  5969. case GGML_TYPE_F16:
  5970. case GGML_TYPE_F32:
  5971. return 1;
  5972. case GGML_TYPE_Q2_K:
  5973. case GGML_TYPE_Q3_K:
  5974. case GGML_TYPE_Q4_K:
  5975. case GGML_TYPE_Q5_K:
  5976. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5977. case GGML_TYPE_Q6_K:
  5978. return 64;
  5979. default:
  5980. GGML_ASSERT(false);
  5981. }
  5982. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5983. }
  5984. inline void ggml_cuda_op_mul_mat_vec_q(
  5985. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5986. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5987. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5988. GGML_ASSERT(ggml_nrows(src1) == 1);
  5989. const int64_t ne00 = src0->ne[0];
  5990. const int64_t row_diff = row_high - row_low;
  5991. switch (src0->type) {
  5992. case GGML_TYPE_Q4_0:
  5993. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5994. break;
  5995. case GGML_TYPE_Q4_1:
  5996. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5997. break;
  5998. case GGML_TYPE_Q5_0:
  5999. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6000. break;
  6001. case GGML_TYPE_Q5_1:
  6002. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6003. break;
  6004. case GGML_TYPE_Q8_0:
  6005. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6006. break;
  6007. case GGML_TYPE_Q2_K:
  6008. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6009. break;
  6010. case GGML_TYPE_Q3_K:
  6011. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6012. break;
  6013. case GGML_TYPE_Q4_K:
  6014. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6015. break;
  6016. case GGML_TYPE_Q5_K:
  6017. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6018. break;
  6019. case GGML_TYPE_Q6_K:
  6020. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6021. break;
  6022. default:
  6023. GGML_ASSERT(false);
  6024. break;
  6025. }
  6026. (void) src1;
  6027. (void) dst;
  6028. (void) src1_ddf_i;
  6029. (void) src1_ncols;
  6030. (void) src1_padded_row_size;
  6031. }
  6032. inline void ggml_cuda_op_dequantize_mul_mat_vec(
  6033. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6034. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6035. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  6036. const int64_t ne00 = src0->ne[0];
  6037. const int64_t row_diff = row_high - row_low;
  6038. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  6039. #ifdef GGML_CUDA_F16
  6040. cuda_pool_alloc<half> src1_dfloat_a;
  6041. half * src1_dfloat = nullptr; // dfloat == half
  6042. bool src1_convert_f16 =
  6043. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  6044. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  6045. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  6046. if (src1_convert_f16) {
  6047. src1_dfloat = src1_dfloat_a.alloc(ne00);
  6048. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  6049. ne00, 1, sizeof(float), 0, 0,
  6050. ne00, 1, sizeof(half), 0, 0, stream);
  6051. }
  6052. #else
  6053. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  6054. #endif // GGML_CUDA_F16
  6055. switch (src0->type) {
  6056. case GGML_TYPE_Q4_0:
  6057. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6058. break;
  6059. case GGML_TYPE_Q4_1:
  6060. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6061. break;
  6062. case GGML_TYPE_Q5_0:
  6063. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6064. break;
  6065. case GGML_TYPE_Q5_1:
  6066. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6067. break;
  6068. case GGML_TYPE_Q8_0:
  6069. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6070. break;
  6071. case GGML_TYPE_Q2_K:
  6072. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6073. break;
  6074. case GGML_TYPE_Q3_K:
  6075. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6076. break;
  6077. case GGML_TYPE_Q4_K:
  6078. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6079. break;
  6080. case GGML_TYPE_Q5_K:
  6081. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6082. break;
  6083. case GGML_TYPE_Q6_K:
  6084. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6085. break;
  6086. case GGML_TYPE_F16:
  6087. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6088. break;
  6089. default:
  6090. GGML_ASSERT(false);
  6091. break;
  6092. }
  6093. (void) src1;
  6094. (void) dst;
  6095. (void) src1_ddq_i;
  6096. (void) src1_ncols;
  6097. (void) src1_padded_row_size;
  6098. }
  6099. inline void ggml_cuda_op_mul_mat_cublas(
  6100. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6101. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6102. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  6103. GGML_ASSERT(src0_dd_i != nullptr);
  6104. GGML_ASSERT(src1_ddf_i != nullptr);
  6105. GGML_ASSERT(dst_dd_i != nullptr);
  6106. const int64_t ne00 = src0->ne[0];
  6107. const int64_t ne10 = src1->ne[0];
  6108. const int64_t ne0 = dst->ne[0];
  6109. const int64_t row_diff = row_high - row_low;
  6110. int id;
  6111. CUDA_CHECK(cudaGetDevice(&id));
  6112. // the main device has a larger memory buffer to hold the results from all GPUs
  6113. // ldc == nrows of the matrix that cuBLAS writes into
  6114. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6115. const int compute_capability = g_device_caps[id].cc;
  6116. 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) {
  6117. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  6118. cuda_pool_alloc<half> src0_as_f16;
  6119. if (src0->type != GGML_TYPE_F16) {
  6120. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  6121. GGML_ASSERT(to_fp16_cuda != nullptr);
  6122. size_t ne = row_diff*ne00;
  6123. src0_as_f16.alloc(ne);
  6124. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  6125. }
  6126. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  6127. cuda_pool_alloc<half> src1_as_f16;
  6128. if (src1->type != GGML_TYPE_F16) {
  6129. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6130. GGML_ASSERT(to_fp16_cuda != nullptr);
  6131. size_t ne = src1_ncols*ne10;
  6132. src1_as_f16.alloc(ne);
  6133. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  6134. }
  6135. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  6136. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  6137. const half alpha_f16 = 1.0f;
  6138. const half beta_f16 = 0.0f;
  6139. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6140. CUBLAS_CHECK(
  6141. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6142. row_diff, src1_ncols, ne10,
  6143. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6144. src1_ptr, CUDA_R_16F, ne10,
  6145. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  6146. CUBLAS_COMPUTE_16F,
  6147. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6148. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6149. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  6150. }
  6151. else {
  6152. cuda_pool_alloc<float> src0_ddq_as_f32;
  6153. if (src0->type != GGML_TYPE_F32) {
  6154. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6155. GGML_ASSERT(to_fp32_cuda != nullptr);
  6156. src0_ddq_as_f32.alloc(row_diff*ne00);
  6157. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  6158. }
  6159. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  6160. const float alpha = 1.0f;
  6161. const float beta = 0.0f;
  6162. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6163. CUBLAS_CHECK(
  6164. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6165. row_diff, src1_ncols, ne10,
  6166. &alpha, src0_ddf_i, ne00,
  6167. src1_ddf_i, ne10,
  6168. &beta, dst_dd_i, ldc));
  6169. }
  6170. (void) dst;
  6171. (void) src1_ddq_i;
  6172. (void) src1_padded_row_size;
  6173. }
  6174. inline void ggml_cuda_op_rope(
  6175. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6176. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6177. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6178. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6179. GGML_ASSERT(src0->type == dst->type);
  6180. const int64_t ne00 = src0->ne[0];
  6181. const int64_t ne01 = src0->ne[1];
  6182. const int64_t ne2 = dst->ne[2];
  6183. const int64_t nrows = ggml_nrows(src0);
  6184. //const int n_past = ((int32_t *) dst->op_params)[0];
  6185. const int n_dims = ((int32_t *) dst->op_params)[1];
  6186. const int mode = ((int32_t *) dst->op_params)[2];
  6187. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6188. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6189. // RoPE alteration for extended context
  6190. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6191. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6192. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6193. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6194. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6195. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6196. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6197. const int32_t * pos = nullptr;
  6198. if ((mode & 1) == 0) {
  6199. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6200. GGML_ASSERT(src1->ne[0] == ne2);
  6201. pos = (const int32_t *) src1_dd;
  6202. }
  6203. const bool is_neox = mode & 2;
  6204. const bool is_glm = mode & 4;
  6205. rope_corr_dims corr_dims;
  6206. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6207. // compute
  6208. if (is_glm) {
  6209. GGML_ASSERT(false);
  6210. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6211. } else if (is_neox) {
  6212. if (src0->type == GGML_TYPE_F32) {
  6213. rope_neox_cuda(
  6214. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6215. attn_factor, corr_dims, main_stream
  6216. );
  6217. } else if (src0->type == GGML_TYPE_F16) {
  6218. rope_neox_cuda(
  6219. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6220. attn_factor, corr_dims, main_stream
  6221. );
  6222. } else {
  6223. GGML_ASSERT(false);
  6224. }
  6225. } else {
  6226. if (src0->type == GGML_TYPE_F32) {
  6227. rope_cuda(
  6228. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6229. attn_factor, corr_dims, main_stream
  6230. );
  6231. } else if (src0->type == GGML_TYPE_F16) {
  6232. rope_cuda(
  6233. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6234. attn_factor, corr_dims, main_stream
  6235. );
  6236. } else {
  6237. GGML_ASSERT(false);
  6238. }
  6239. }
  6240. (void) src1;
  6241. (void) dst;
  6242. (void) src1_dd;
  6243. }
  6244. inline void ggml_cuda_op_alibi(
  6245. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6246. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6247. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6248. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6249. const int64_t ne00 = src0->ne[0];
  6250. const int64_t ne01 = src0->ne[1];
  6251. const int64_t ne02 = src0->ne[2];
  6252. const int64_t nrows = ggml_nrows(src0);
  6253. //const int n_past = ((int32_t *) dst->op_params)[0];
  6254. const int n_head = ((int32_t *) dst->op_params)[1];
  6255. float max_bias;
  6256. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6257. //GGML_ASSERT(ne01 + n_past == ne00);
  6258. GGML_ASSERT(n_head == ne02);
  6259. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6260. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6261. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6262. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6263. (void) src1;
  6264. (void) src1_dd;
  6265. }
  6266. inline void ggml_cuda_op_im2col(
  6267. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6268. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6269. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6270. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6271. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6272. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6273. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6274. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6275. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6276. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6277. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6278. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6279. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6280. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6281. const int64_t IW = src1->ne[0];
  6282. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6283. const int64_t KW = src0->ne[0];
  6284. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6285. const int64_t OW = dst->ne[1];
  6286. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6287. 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);
  6288. (void) src0;
  6289. (void) src0_dd;
  6290. }
  6291. inline void ggml_cuda_op_sum_rows(
  6292. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6293. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6294. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6295. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6296. const int64_t ncols = src0->ne[0];
  6297. const int64_t nrows = ggml_nrows(src0);
  6298. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6299. (void) src1;
  6300. (void) dst;
  6301. (void) src1_dd;
  6302. }
  6303. inline void ggml_cuda_op_argsort(
  6304. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6305. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6306. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6307. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6308. const int64_t ncols = src0->ne[0];
  6309. const int64_t nrows = ggml_nrows(src0);
  6310. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6311. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6312. (void) src1;
  6313. (void) dst;
  6314. (void) src1_dd;
  6315. }
  6316. inline void ggml_cuda_op_diag_mask_inf(
  6317. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6318. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6319. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6320. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6321. const int64_t ne00 = src0->ne[0];
  6322. const int64_t ne01 = src0->ne[1];
  6323. const int nrows0 = ggml_nrows(src0);
  6324. const int n_past = ((int32_t *) dst->op_params)[0];
  6325. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6326. (void) src1;
  6327. (void) dst;
  6328. (void) src1_dd;
  6329. }
  6330. inline void ggml_cuda_op_soft_max(
  6331. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6332. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6333. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6334. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6335. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6336. const int64_t ne00 = src0->ne[0];
  6337. const int64_t nrows_x = ggml_nrows(src0);
  6338. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6339. float scale = 1.0f;
  6340. memcpy(&scale, dst->op_params, sizeof(float));
  6341. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6342. (void) dst;
  6343. }
  6344. inline void ggml_cuda_op_scale(
  6345. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6346. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6347. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6348. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6349. float scale;
  6350. memcpy(&scale, dst->op_params, sizeof(float));
  6351. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6352. CUDA_CHECK(cudaGetLastError());
  6353. (void) src1;
  6354. (void) dst;
  6355. (void) src1_dd;
  6356. }
  6357. inline void ggml_cuda_op_clamp(
  6358. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6359. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6360. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6361. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6362. float min;
  6363. float max;
  6364. memcpy(&min, dst->op_params, sizeof(float));
  6365. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6366. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6367. CUDA_CHECK(cudaGetLastError());
  6368. (void) src1;
  6369. (void) dst;
  6370. (void) src1_dd;
  6371. }
  6372. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6373. const int64_t nrows0 = ggml_nrows(src0);
  6374. const bool use_src1 = src1 != nullptr;
  6375. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6376. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6377. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6378. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6379. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6380. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6381. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6382. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6383. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6384. // dd = data device
  6385. float * src0_ddf = nullptr;
  6386. float * src1_ddf = nullptr;
  6387. float * dst_ddf = nullptr;
  6388. cuda_pool_alloc<float> src0_f;
  6389. cuda_pool_alloc<float> src1_f;
  6390. cuda_pool_alloc<float> dst_f;
  6391. ggml_cuda_set_device(g_main_device);
  6392. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6393. if (src0_on_device) {
  6394. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6395. } else {
  6396. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  6397. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6398. }
  6399. if (use_src1) {
  6400. if (src1_on_device) {
  6401. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6402. } else {
  6403. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  6404. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6405. }
  6406. }
  6407. if (dst_on_device) {
  6408. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6409. } else {
  6410. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  6411. }
  6412. // do the computation
  6413. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6414. CUDA_CHECK(cudaGetLastError());
  6415. // copy dst to host if necessary
  6416. if (!dst_on_device) {
  6417. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6418. }
  6419. if (dst->backend == GGML_BACKEND_CPU) {
  6420. CUDA_CHECK(cudaDeviceSynchronize());
  6421. }
  6422. }
  6423. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6424. static bool peer_access_enabled = false;
  6425. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6426. if (peer_access_enabled == enable_peer_access) {
  6427. return;
  6428. }
  6429. #ifdef NDEBUG
  6430. for (int id = 0; id < g_device_count; ++id) {
  6431. CUDA_CHECK(ggml_cuda_set_device(id));
  6432. CUDA_CHECK(cudaDeviceSynchronize());
  6433. }
  6434. for (int id = 0; id < g_device_count; ++id) {
  6435. CUDA_CHECK(ggml_cuda_set_device(id));
  6436. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6437. if (id == id_other) {
  6438. continue;
  6439. }
  6440. if (id != g_main_device && id_other != g_main_device) {
  6441. continue;
  6442. }
  6443. int can_access_peer;
  6444. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6445. if (can_access_peer) {
  6446. if (enable_peer_access) {
  6447. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6448. } else {
  6449. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6450. }
  6451. }
  6452. }
  6453. }
  6454. #endif // NDEBUG
  6455. peer_access_enabled = enable_peer_access;
  6456. }
  6457. static void ggml_cuda_op_mul_mat(
  6458. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6459. const bool convert_src1_to_q8_1) {
  6460. const int64_t ne00 = src0->ne[0];
  6461. const int64_t ne01 = src0->ne[1];
  6462. const int64_t ne02 = src0->ne[2];
  6463. const int64_t ne03 = src0->ne[3];
  6464. const int64_t nrows0 = ggml_nrows(src0);
  6465. const int64_t ne10 = src1->ne[0];
  6466. const int64_t ne11 = src1->ne[1];
  6467. const int64_t ne12 = src1->ne[2];
  6468. const int64_t ne13 = src1->ne[3];
  6469. const int64_t nrows1 = ggml_nrows(src1);
  6470. GGML_ASSERT(ne03 == ne13);
  6471. const int64_t ne0 = dst->ne[0];
  6472. const int64_t ne1 = dst->ne[1];
  6473. const int nb2 = dst->nb[2];
  6474. const int nb3 = dst->nb[3];
  6475. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6476. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6477. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6478. const int64_t i02_divisor = ne12 / ne02;
  6479. const size_t src0_ts = ggml_type_size(src0->type);
  6480. const size_t src0_bs = ggml_blck_size(src0->type);
  6481. const size_t q8_1_ts = sizeof(block_q8_1);
  6482. const size_t q8_1_bs = QK8_1;
  6483. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6484. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6485. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6486. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6487. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6488. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6489. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6490. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6491. GGML_ASSERT(!(split && ne02 > 1));
  6492. GGML_ASSERT(!(split && ne03 > 1));
  6493. GGML_ASSERT(!(split && ne02 < ne12));
  6494. // dd = data device
  6495. char * src0_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6496. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  6497. char * src1_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // q8_1
  6498. float * dst_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6499. // as = actual size
  6500. size_t src0_as[GGML_CUDA_MAX_DEVICES] = {0};
  6501. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  6502. size_t src1_asq[GGML_CUDA_MAX_DEVICES] = {0};
  6503. size_t dst_as[GGML_CUDA_MAX_DEVICES] = {0};
  6504. int64_t row_low[GGML_CUDA_MAX_DEVICES];
  6505. int64_t row_high[GGML_CUDA_MAX_DEVICES];
  6506. int used_devices = 0;
  6507. for (int64_t id = 0; id < g_device_count; ++id) {
  6508. // by default, use all rows
  6509. row_low[id] = 0;
  6510. row_high[id] = ne01;
  6511. // for multi GPU, get the row boundaries from tensor split
  6512. // and round to mul_mat_q tile sizes
  6513. if (split) {
  6514. const int64_t rounding = get_row_rounding(src0->type);
  6515. if (id != 0) {
  6516. row_low[id] = ne01*g_tensor_split[id];
  6517. if (row_low[id] < ne01) {
  6518. row_low[id] -= row_low[id] % rounding;
  6519. }
  6520. }
  6521. if (id != g_device_count - 1) {
  6522. row_high[id] = ne01*g_tensor_split[id + 1];
  6523. if (row_high[id] < ne01) {
  6524. row_high[id] -= row_high[id] % rounding;
  6525. }
  6526. }
  6527. }
  6528. }
  6529. for (int64_t id = 0; id < g_device_count; ++id) {
  6530. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6531. continue;
  6532. }
  6533. used_devices++;
  6534. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6535. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6536. ggml_cuda_set_device(id);
  6537. const cudaStream_t stream = g_cudaStreams[id][0];
  6538. if (src0_on_device && src0_is_contiguous) {
  6539. src0_dd[id] = (char *) src0_extra->data_device[id];
  6540. } else {
  6541. // const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  6542. src0_dd[id] = (char *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  6543. }
  6544. if (src1_on_device && src1_is_contiguous) {
  6545. src1_ddf[id] = (float *) src1_extra->data_device[id];
  6546. } else {
  6547. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  6548. }
  6549. if (convert_src1_to_q8_1) {
  6550. src1_ddq[id] = (char *) ggml_cuda_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  6551. if (src1_on_device && src1_is_contiguous) {
  6552. quantize_row_q8_1_cuda(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  6553. CUDA_CHECK(cudaGetLastError());
  6554. }
  6555. }
  6556. if (dst_on_device) {
  6557. dst_dd[id] = (float *) dst_extra->data_device[id];
  6558. } else {
  6559. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  6560. dst_dd[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_as[id]);
  6561. }
  6562. }
  6563. // if multiple devices are used they need to wait for the main device
  6564. // here an event is recorded that signals that the main device has finished calculating the input data
  6565. if (split && used_devices > 1) {
  6566. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6567. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  6568. }
  6569. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  6570. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  6571. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  6572. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  6573. for (int64_t id = 0; id < g_device_count; ++id) {
  6574. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6575. continue;
  6576. }
  6577. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6578. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6579. const int64_t row_diff = row_high[id] - row_low[id];
  6580. ggml_cuda_set_device(id);
  6581. const cudaStream_t stream = g_cudaStreams[id][is];
  6582. // wait for main GPU data if necessary
  6583. if (split && (id != g_main_device || is != 0)) {
  6584. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  6585. }
  6586. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  6587. const int64_t i03 = i0 / ne12;
  6588. const int64_t i02 = i0 % ne12;
  6589. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  6590. // for split tensors the data begins at i0 == i0_offset_low
  6591. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  6592. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  6593. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  6594. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  6595. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  6596. // in that case an offset on dst_ddf_i is needed
  6597. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  6598. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  6599. }
  6600. // copy src0, src1 to device if necessary
  6601. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  6602. if (id != g_main_device) {
  6603. if (convert_src1_to_q8_1) {
  6604. char * src1_ddq_i_source = src1_ddq[g_main_device] + src1_ddq_i_offset;
  6605. CUDA_CHECK(cudaMemcpyAsync(src1_ddq_i, src1_ddq_i_source, src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs,
  6606. cudaMemcpyDeviceToDevice, stream));
  6607. } else {
  6608. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  6609. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  6610. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_ncols*ne10*sizeof(float),
  6611. cudaMemcpyDeviceToDevice, stream));
  6612. }
  6613. }
  6614. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  6615. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  6616. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  6617. } else {
  6618. GGML_ASSERT(false);
  6619. }
  6620. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  6621. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  6622. CUDA_CHECK(cudaGetLastError());
  6623. }
  6624. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  6625. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  6626. }
  6627. // do the computation
  6628. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  6629. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  6630. CUDA_CHECK(cudaGetLastError());
  6631. // copy dst to host or other device if necessary
  6632. if (!dst_on_device) {
  6633. void * dst_off_device;
  6634. cudaMemcpyKind kind;
  6635. if (dst->backend == GGML_BACKEND_CPU) {
  6636. dst_off_device = dst->data;
  6637. kind = cudaMemcpyDeviceToHost;
  6638. } else if (dst->backend == GGML_BACKEND_GPU) {
  6639. dst_off_device = dst_extra->data_device[g_main_device];
  6640. kind = cudaMemcpyDeviceToDevice;
  6641. } else {
  6642. GGML_ASSERT(false);
  6643. }
  6644. if (split) {
  6645. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  6646. // dst is NOT transposed.
  6647. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  6648. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  6649. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  6650. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6651. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6652. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  6653. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_dd_i, row_diff*sizeof(float),
  6654. row_diff*sizeof(float), src1_ncols, kind, stream));
  6655. } else {
  6656. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6657. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6658. dhf_dst_i += src1_col_0*ne0;
  6659. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  6660. }
  6661. }
  6662. // add event for the main device to wait on until other device is done
  6663. if (split && (id != g_main_device || is != 0)) {
  6664. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  6665. }
  6666. }
  6667. }
  6668. }
  6669. for (int64_t id = 0; id < g_device_count; ++id) {
  6670. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6671. continue;
  6672. }
  6673. CUDA_CHECK(ggml_cuda_set_device(id));
  6674. // free buffers again when done
  6675. if (dst_as[id] > 0) {
  6676. ggml_cuda_pool_free(dst_dd[id], dst_as[id]);
  6677. }
  6678. if (src1_asq[id] > 0) {
  6679. ggml_cuda_pool_free(src1_ddq[id], src1_asq[id]);
  6680. }
  6681. if (src1_asf[id] > 0) {
  6682. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  6683. }
  6684. if (src0_as[id] > 0) {
  6685. ggml_cuda_pool_free(src0_dd[id], src0_as[id]);
  6686. }
  6687. }
  6688. // main device waits for all other devices to be finished
  6689. if (split && g_device_count > 1) {
  6690. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  6691. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  6692. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6693. for (int64_t id = 0; id < g_device_count; ++id) {
  6694. if (row_low[id] == row_high[id]) {
  6695. continue;
  6696. }
  6697. for (int64_t is = 0; is < is_max; ++is) {
  6698. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  6699. }
  6700. }
  6701. }
  6702. if (dst->backend == GGML_BACKEND_CPU) {
  6703. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6704. CUDA_CHECK(cudaDeviceSynchronize());
  6705. }
  6706. }
  6707. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6708. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  6709. }
  6710. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6711. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  6712. }
  6713. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6714. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  6715. }
  6716. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6717. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  6718. }
  6719. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6720. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  6721. }
  6722. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6723. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  6724. }
  6725. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6726. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  6727. }
  6728. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6729. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  6730. }
  6731. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6732. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  6733. }
  6734. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6735. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  6736. }
  6737. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6738. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  6739. }
  6740. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6741. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  6742. }
  6743. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6744. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  6745. }
  6746. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6747. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  6748. }
  6749. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6750. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  6751. }
  6752. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6753. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  6754. }
  6755. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6756. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  6757. }
  6758. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6759. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  6760. }
  6761. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6762. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  6763. }
  6764. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  6765. if (!g_cublas_loaded) return false;
  6766. const int64_t ne10 = src1->ne[0];
  6767. const int64_t ne0 = dst->ne[0];
  6768. const int64_t ne1 = dst->ne[1];
  6769. // TODO: find the optimal values for these
  6770. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  6771. src1->type == GGML_TYPE_F32 &&
  6772. dst->type == GGML_TYPE_F32 &&
  6773. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  6774. }
  6775. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6776. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  6777. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6778. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  6779. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  6780. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6781. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6782. const int64_t ne00 = src0->ne[0];
  6783. const int64_t ne01 = src0->ne[1];
  6784. const int64_t ne02 = src0->ne[2];
  6785. const int64_t ne12 = src1->ne[2];
  6786. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6787. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6788. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6789. void * src0_ddq = src0_extra->data_device[g_main_device];
  6790. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6791. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6792. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6793. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6794. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  6795. }
  6796. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6797. GGML_ASSERT(!ggml_is_transposed(src0));
  6798. GGML_ASSERT(!ggml_is_transposed(src1));
  6799. GGML_ASSERT(!ggml_is_permuted(src0));
  6800. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6801. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6802. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6803. const int64_t ne00 = src0->ne[0];
  6804. const int64_t ne01 = src0->ne[1];
  6805. const int64_t ne02 = src0->ne[2];
  6806. const int64_t nb01 = src0->nb[1];
  6807. const int64_t nb02 = src0->nb[2];
  6808. const int64_t ne12 = src1->ne[2];
  6809. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6810. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6811. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6812. void * src0_ddq = src0_extra->data_device[g_main_device];
  6813. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6814. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6815. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6816. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6817. const int64_t row_stride_x = nb01 / sizeof(half);
  6818. const int64_t channel_stride_x = nb02 / sizeof(half);
  6819. 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);
  6820. }
  6821. static __global__ void k_compute_batched_ptrs(
  6822. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  6823. const void ** ptrs_src, void ** ptrs_dst,
  6824. int64_t ne12, int64_t ne13,
  6825. int64_t ne23,
  6826. size_t nb02, size_t nb03,
  6827. size_t nb12, size_t nb13,
  6828. size_t nbd2, size_t nbd3,
  6829. int64_t r2, int64_t r3) {
  6830. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6831. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6832. if (i13 >= ne13 || i12 >= ne12) {
  6833. return;
  6834. }
  6835. int64_t i03 = i13 / r3;
  6836. int64_t i02 = i12 / r2;
  6837. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  6838. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  6839. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  6840. }
  6841. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6842. GGML_ASSERT(!ggml_is_transposed(src0));
  6843. GGML_ASSERT(!ggml_is_transposed(src1));
  6844. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6845. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6846. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6847. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  6848. const int64_t ne01 = src0->ne[1];
  6849. const int64_t ne02 = src0->ne[2];
  6850. const int64_t ne03 = src0->ne[3];
  6851. const int64_t nb01 = src0->nb[1];
  6852. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  6853. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  6854. const int64_t ne10 = src1->ne[0];
  6855. const int64_t ne11 = src1->ne[1];
  6856. const int64_t ne12 = src1->ne[2];
  6857. const int64_t ne13 = src1->ne[3];
  6858. const int64_t nb11 = src1->nb[1];
  6859. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6860. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6861. const int64_t ne1 = ggml_nelements(src1);
  6862. const int64_t ne = ggml_nelements(dst);
  6863. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6864. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6865. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6866. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6867. void * src0_ddq = src0_extra->data_device[g_main_device];
  6868. half * src0_as_f16 = (half *) src0_ddq;
  6869. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6870. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6871. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6872. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6873. // convert src1 to fp16
  6874. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6875. GGML_ASSERT(to_fp16_cuda != nullptr);
  6876. cuda_pool_alloc<half> src1_as_f16(ne1);
  6877. to_fp16_cuda(src1_ddf, src1_as_f16.get(), ne1, main_stream);
  6878. cuda_pool_alloc<half> dst_f16;
  6879. char * dst_t;
  6880. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  6881. cudaDataType_t cu_data_type = CUDA_R_16F;
  6882. // dst strides
  6883. size_t nbd2 = dst->nb[2];
  6884. size_t nbd3 = dst->nb[3];
  6885. const half alpha_f16 = 1.0f;
  6886. const half beta_f16 = 0.0f;
  6887. const float alpha_f32 = 1.0f;
  6888. const float beta_f32 = 0.0f;
  6889. const void * alpha = &alpha_f16;
  6890. const void * beta = &beta_f16;
  6891. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6892. dst_t = (char *) dst_f16.alloc(ne);
  6893. nbd2 /= sizeof(float) / sizeof(half);
  6894. nbd3 /= sizeof(float) / sizeof(half);
  6895. } else {
  6896. dst_t = (char *) dst_ddf;
  6897. cu_compute_type = CUBLAS_COMPUTE_32F;
  6898. cu_data_type = CUDA_R_32F;
  6899. alpha = &alpha_f32;
  6900. beta = &beta_f32;
  6901. }
  6902. GGML_ASSERT(ne12 % ne02 == 0);
  6903. GGML_ASSERT(ne13 % ne03 == 0);
  6904. // broadcast factors
  6905. const int64_t r2 = ne12/ne02;
  6906. const int64_t r3 = ne13/ne03;
  6907. #if 0
  6908. // use cublasGemmEx
  6909. {
  6910. for (int i13 = 0; i13 < ne13; ++i13) {
  6911. for (int i12 = 0; i12 < ne12; ++i12) {
  6912. int i03 = i13 / r3;
  6913. int i02 = i12 / r2;
  6914. CUBLAS_CHECK(
  6915. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6916. ne01, ne11, ne10,
  6917. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6918. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6919. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  6920. cu_compute_type,
  6921. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6922. }
  6923. }
  6924. }
  6925. #else
  6926. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6927. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6928. // use cublasGemmStridedBatchedEx
  6929. CUBLAS_CHECK(
  6930. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6931. ne01, ne11, ne10,
  6932. alpha, (const char *) src0_as_f16, CUDA_R_16F, nb01/sizeof(half), src0->nb[2]/sizeof(half), // strideA
  6933. (const char *) src1_as_f16.get(), CUDA_R_16F, nb11/sizeof(float), src1->nb[2]/sizeof(float), // strideB
  6934. beta, ( char *) dst_t, cu_data_type, ne01, dst->nb[2]/sizeof(float), // strideC
  6935. ne12*ne13,
  6936. cu_compute_type,
  6937. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6938. } else {
  6939. // use cublasGemmBatchedEx
  6940. const int ne23 = ne12*ne13;
  6941. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  6942. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  6943. dim3 block_dims(ne13, ne12);
  6944. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6945. src0_as_f16, src1_as_f16.get(), dst_t,
  6946. ptrs_src.get(), ptrs_dst.get(),
  6947. ne12, ne13,
  6948. ne23,
  6949. nb02, nb03,
  6950. nb12, nb13,
  6951. nbd2, nbd3,
  6952. r2, r3);
  6953. CUDA_CHECK(cudaGetLastError());
  6954. CUBLAS_CHECK(
  6955. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6956. ne01, ne11, ne10,
  6957. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/sizeof(half),
  6958. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/sizeof(float),
  6959. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  6960. ne23,
  6961. cu_compute_type,
  6962. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6963. }
  6964. #endif
  6965. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6966. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6967. to_fp32_cuda(dst_f16.get(), dst_ddf, ne, main_stream);
  6968. }
  6969. }
  6970. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6971. const bool all_on_device =
  6972. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6973. (src1->backend == GGML_BACKEND_GPU) &&
  6974. ( dst->backend == GGML_BACKEND_GPU);
  6975. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6976. int64_t min_compute_capability = INT_MAX;
  6977. for (int64_t id = 0; id < g_device_count; ++id) {
  6978. if (min_compute_capability > g_device_caps[id].cc && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6979. min_compute_capability = g_device_caps[id].cc;
  6980. }
  6981. }
  6982. #ifdef CUDA_USE_TENSOR_CORES
  6983. const bool use_tensor_cores = true;
  6984. #else
  6985. const bool use_tensor_cores = false;
  6986. #endif
  6987. // debug helpers
  6988. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  6989. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  6990. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  6991. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  6992. //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);
  6993. //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);
  6994. 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) {
  6995. // KQ single-batch
  6996. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  6997. } 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) {
  6998. // KQV single-batch
  6999. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  7000. } 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)) {
  7001. // KQ + KQV multi-batch
  7002. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  7003. } else if (src0->type == GGML_TYPE_F32) {
  7004. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7005. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  7006. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  7007. #ifdef GGML_CUDA_FORCE_DMMV
  7008. const bool use_mul_mat_vec_q = false;
  7009. #else
  7010. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  7011. #endif // GGML_CUDA_FORCE_DMMV
  7012. if (use_mul_mat_vec_q) {
  7013. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  7014. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  7015. } else {
  7016. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  7017. }
  7018. } else {
  7019. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  7020. // when tensor cores are available, use them for large batch size
  7021. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  7022. if (use_tensor_cores && min_compute_capability >= CC_VOLTA && src1->ne[1] > MMQ_MAX_BATCH_SIZE) {
  7023. use_mul_mat_q = false;
  7024. }
  7025. if (use_mul_mat_q) {
  7026. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  7027. } else {
  7028. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7029. }
  7030. }
  7031. } else {
  7032. GGML_ASSERT(false);
  7033. }
  7034. }
  7035. #if 0
  7036. template<typename ... Srcs>
  7037. static __global__ void k_compute_batched_ptrs_id(
  7038. const void ** ptrs_src, void ** ptrs_dst,
  7039. int ne12, int ne13,
  7040. int ne23,
  7041. int nb02, int nb03,
  7042. int nb12, int nb13,
  7043. int nb2, int nb3,
  7044. int r2, int r3,
  7045. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  7046. const half * src1_f16, half * dst_f16,
  7047. const int32_t * ids, const int id,
  7048. Srcs... src0s) {
  7049. int i = ids[id];
  7050. half * src0_f16;
  7051. const void * srcs_ar[] = { (const half *) src0s... };
  7052. if (src0_type == GGML_TYPE_F16) {
  7053. src0_f16 = (half *) srcs_ar[i];
  7054. } else {
  7055. src0_f16 = src0_as_f16;
  7056. if (threadIdx.x == 0 && threadIdx.y == 0) {
  7057. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  7058. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  7059. }
  7060. }
  7061. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7062. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7063. if (i13 >= ne13 || i12 >= ne12) {
  7064. return;
  7065. }
  7066. int i03 = i13 / r3;
  7067. int i02 = i12 / r2;
  7068. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  7069. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  7070. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  7071. }
  7072. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  7073. const struct ggml_tensor * ids = dst->src[0];
  7074. const struct ggml_tensor * src1 = dst->src[1];
  7075. const struct ggml_tensor * src00 = dst->src[2];
  7076. const int id = dst->op_params[0];
  7077. GGML_ASSERT(!ggml_is_transposed(src00));
  7078. GGML_ASSERT(!ggml_is_transposed(src1));
  7079. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  7080. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7081. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  7082. const int64_t ne01 = src00->ne[1];
  7083. const int64_t ne02 = src00->ne[2];
  7084. const int64_t ne03 = src00->ne[3];
  7085. //const int64_t nb01 = src00->nb[1];
  7086. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  7087. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  7088. const int64_t ne10 = src1->ne[0];
  7089. const int64_t ne11 = src1->ne[1];
  7090. const int64_t ne12 = src1->ne[2];
  7091. const int64_t ne13 = src1->ne[3];
  7092. //const int64_t nb11 = src1->nb[1];
  7093. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  7094. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  7095. const int64_t ne1 = ggml_nelements(src1);
  7096. const int64_t ne = ggml_nelements(dst);
  7097. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7098. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7099. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7100. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7101. //void * src0_ddq = src0_extra->data_device[g_main_device];
  7102. //half * src0_as_f16 = (half *) src0_ddq;
  7103. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7104. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7105. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7106. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7107. // convert src1 to fp16
  7108. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7109. GGML_ASSERT(to_fp16_cuda != nullptr);
  7110. size_t src1_as = 0;
  7111. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7112. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7113. size_t dst_as = 0;
  7114. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7115. GGML_ASSERT(ne12 % ne02 == 0);
  7116. GGML_ASSERT(ne13 % ne03 == 0);
  7117. // broadcast factors
  7118. const int64_t r2 = ne12/ne02;
  7119. const int64_t r3 = ne13/ne03;
  7120. const half alpha_f16 = 1.0f;
  7121. const half beta_f16 = 0.0f;
  7122. // use cublasGemmBatchedEx
  7123. const int ne23 = ne12*ne13;
  7124. const void ** ptrs_src = nullptr;
  7125. void ** ptrs_dst = nullptr;
  7126. size_t ptrs_src_s = 0;
  7127. size_t ptrs_dst_s = 0;
  7128. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7129. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7130. int64_t src0_ne = ggml_nelements(src00);
  7131. half * src0_as_f16 = nullptr;
  7132. size_t src0_as = 0;
  7133. if (src00->type != GGML_TYPE_F16) {
  7134. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7135. }
  7136. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7137. dim3 block_dims(ne13, ne12);
  7138. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7139. ptrs_src, ptrs_dst,
  7140. ne12, ne13,
  7141. ne23,
  7142. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7143. nb12, nb13,
  7144. dst->nb[2], dst->nb[3],
  7145. r2, r3,
  7146. src00->type, src0_as_f16, src0_ne,
  7147. src1_as_f16, dst_f16,
  7148. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7149. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7150. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7151. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7152. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7153. );
  7154. CUDA_CHECK(cudaGetLastError());
  7155. CUBLAS_CHECK(
  7156. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7157. ne01, ne11, ne10,
  7158. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7159. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7160. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7161. ne23,
  7162. CUBLAS_COMPUTE_16F,
  7163. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7164. if (src0_as != 0) {
  7165. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7166. }
  7167. if (ptrs_src_s != 0) {
  7168. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7169. }
  7170. if (ptrs_dst_s != 0) {
  7171. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7172. }
  7173. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7174. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7175. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7176. ggml_cuda_pool_free(dst_f16, dst_as);
  7177. }
  7178. #endif
  7179. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7180. #if 0
  7181. ggml_cuda_mul_mat_id_cublas(dst);
  7182. // TODO: mmq/mmv support
  7183. #endif
  7184. const int64_t nb11 = src1->nb[1];
  7185. const int64_t nb1 = dst->nb[1];
  7186. const struct ggml_tensor * ids = src0;
  7187. const int32_t id = ((int32_t *) dst->op_params)[0];
  7188. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7189. std::vector<char> ids_host(ggml_nbytes(ids));
  7190. const cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7191. if (ids->backend == GGML_BACKEND_GPU) {
  7192. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7193. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7194. CUDA_CHECK(cudaStreamSynchronize(stream));
  7195. } else {
  7196. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7197. }
  7198. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7199. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7200. ggml_tensor_extra_gpu src1_row_extra;
  7201. ggml_tensor_extra_gpu dst_row_extra;
  7202. ggml_tensor src1_row = *src1;
  7203. ggml_tensor dst_row = *dst;
  7204. src1_row.backend = GGML_BACKEND_GPU;
  7205. dst_row.backend = GGML_BACKEND_GPU;
  7206. src1_row.extra = &src1_row_extra;
  7207. dst_row.extra = &dst_row_extra;
  7208. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7209. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7210. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7211. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7212. if (src1->ne[1] == 1) {
  7213. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7214. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7215. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7216. //int32_t row_id;
  7217. //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]));
  7218. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7219. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7220. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7221. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7222. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7223. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7224. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7225. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7226. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7227. }
  7228. } else {
  7229. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  7230. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  7231. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  7232. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  7233. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7234. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7235. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7236. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  7237. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7238. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7239. int64_t num_src1_rows = 0;
  7240. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7241. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7242. if (row_id_i != row_id) {
  7243. continue;
  7244. }
  7245. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7246. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  7247. nb11, src1_kind, stream));
  7248. num_src1_rows++;
  7249. }
  7250. if (num_src1_rows == 0) {
  7251. continue;
  7252. }
  7253. src1_row.ne[1] = num_src1_rows;
  7254. dst_row.ne[1] = num_src1_rows;
  7255. src1_row.nb[1] = nb11;
  7256. src1_row.nb[2] = num_src1_rows*nb11;
  7257. src1_row.nb[3] = num_src1_rows*nb11;
  7258. dst_row.nb[1] = nb1;
  7259. dst_row.nb[2] = num_src1_rows*nb1;
  7260. dst_row.nb[3] = num_src1_rows*nb1;
  7261. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7262. num_src1_rows = 0;
  7263. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7264. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7265. if (row_id_i != row_id) {
  7266. continue;
  7267. }
  7268. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7269. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  7270. nb1, dst_kind, stream));
  7271. num_src1_rows++;
  7272. }
  7273. }
  7274. }
  7275. if (dst->backend == GGML_BACKEND_CPU) {
  7276. CUDA_CHECK(cudaStreamSynchronize(stream));
  7277. }
  7278. }
  7279. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7280. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7281. }
  7282. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7283. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7284. }
  7285. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7286. const int64_t ne = ggml_nelements(src0);
  7287. GGML_ASSERT(ne == ggml_nelements(src1));
  7288. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7289. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7290. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7291. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7292. const int64_t ne00 = src0->ne[0];
  7293. const int64_t ne01 = src0->ne[1];
  7294. GGML_ASSERT(src0->ne[3] == 1);
  7295. const int64_t nb00 = src0->nb[0];
  7296. const int64_t nb01 = src0->nb[1];
  7297. const int64_t nb02 = src0->nb[2];
  7298. const int64_t ne10 = src1->ne[0];
  7299. const int64_t ne11 = src1->ne[1];
  7300. GGML_ASSERT(src1->ne[3] == 1);
  7301. const int64_t nb10 = src1->nb[0];
  7302. const int64_t nb11 = src1->nb[1];
  7303. const int64_t nb12 = src1->nb[2];
  7304. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7305. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7306. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7307. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7308. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7309. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7310. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7311. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7312. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7313. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7314. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7315. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7316. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7317. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7318. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7319. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7320. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7321. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7322. } else {
  7323. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7324. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7325. GGML_ASSERT(false);
  7326. }
  7327. (void) dst;
  7328. }
  7329. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7330. // TODO: why do we pass dst as src1 here?
  7331. ggml_cuda_cpy(src0, dst, nullptr);
  7332. (void) src1;
  7333. }
  7334. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7335. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7336. }
  7337. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7338. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7339. }
  7340. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7341. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7342. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7343. }
  7344. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7345. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7346. }
  7347. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7348. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7349. }
  7350. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7351. GGML_ASSERT(ggml_is_contiguous(src0));
  7352. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7353. }
  7354. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7355. GGML_ASSERT(ggml_is_contiguous(src0));
  7356. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7357. }
  7358. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7359. (void) src0;
  7360. (void) src1;
  7361. (void) dst;
  7362. }
  7363. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7364. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7365. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7366. }
  7367. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7368. const int64_t nrows = ggml_nrows(tensor);
  7369. const int64_t ne0 = tensor->ne[0];
  7370. const size_t nb1 = tensor->nb[1];
  7371. ggml_backend_type backend = tensor->backend;
  7372. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7373. memset(extra, 0, sizeof(*extra));
  7374. for (int64_t id = 0; id < g_device_count; ++id) {
  7375. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7376. continue;
  7377. }
  7378. ggml_cuda_set_device(id);
  7379. int64_t row_low, row_high;
  7380. if (backend == GGML_BACKEND_GPU) {
  7381. row_low = 0;
  7382. row_high = nrows;
  7383. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7384. const int64_t rounding = get_row_rounding(tensor->type);
  7385. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7386. row_low -= row_low % rounding;
  7387. if (id == g_device_count - 1) {
  7388. row_high = nrows;
  7389. } else {
  7390. row_high = nrows*g_tensor_split[id + 1];
  7391. row_high -= row_high % rounding;
  7392. }
  7393. } else {
  7394. GGML_ASSERT(false);
  7395. }
  7396. if (row_low == row_high) {
  7397. continue;
  7398. }
  7399. int64_t nrows_split = row_high - row_low;
  7400. const size_t offset_split = row_low*nb1;
  7401. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7402. const size_t original_size = size;
  7403. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7404. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7405. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7406. }
  7407. char * buf;
  7408. CUDA_CHECK(cudaMalloc(&buf, size));
  7409. char * buf_host = (char *)data + offset_split;
  7410. // set padding to 0 to avoid possible NaN values
  7411. if (size > original_size) {
  7412. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7413. }
  7414. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7415. extra->data_device[id] = buf;
  7416. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7417. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7418. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7419. }
  7420. }
  7421. }
  7422. tensor->extra = extra;
  7423. }
  7424. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7425. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7426. return;
  7427. }
  7428. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7429. for (int64_t id = 0; id < g_device_count; ++id) {
  7430. if (extra->data_device[id] != nullptr) {
  7431. CUDA_CHECK(ggml_cuda_set_device(id));
  7432. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7433. }
  7434. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7435. if (extra->events[id][is] != nullptr) {
  7436. CUDA_CHECK(ggml_cuda_set_device(id));
  7437. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7438. }
  7439. }
  7440. }
  7441. delete extra;
  7442. }
  7443. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7444. static size_t g_temp_tensor_extra_index = 0;
  7445. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7446. if (g_temp_tensor_extras == nullptr) {
  7447. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7448. }
  7449. size_t alloc_index = g_temp_tensor_extra_index;
  7450. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7451. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7452. memset(extra, 0, sizeof(*extra));
  7453. return extra;
  7454. }
  7455. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7456. if (scratch && g_scratch_size == 0) {
  7457. return;
  7458. }
  7459. tensor->backend = GGML_BACKEND_GPU;
  7460. // recursively assign CUDA buffers until a compute tensor is found
  7461. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7462. const ggml_op src0_op = tensor->src[0]->op;
  7463. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7464. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7465. }
  7466. }
  7467. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7468. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7469. }
  7470. if (scratch && no_alloc) {
  7471. return;
  7472. }
  7473. ggml_tensor_extra_gpu * extra;
  7474. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7475. tensor->op == GGML_OP_VIEW ||
  7476. force_inplace;
  7477. const size_t size = ggml_nbytes(tensor);
  7478. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7479. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7480. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7481. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7482. size_t offset = 0;
  7483. if (tensor->op == GGML_OP_VIEW) {
  7484. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7485. }
  7486. extra = ggml_cuda_alloc_temp_tensor_extra();
  7487. extra->data_device[g_main_device] = src0_ddc + offset;
  7488. } else if (tensor->op == GGML_OP_CPY) {
  7489. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7490. void * src1_ddv = src1_extra->data_device[g_main_device];
  7491. extra = ggml_cuda_alloc_temp_tensor_extra();
  7492. extra->data_device[g_main_device] = src1_ddv;
  7493. } else if (scratch) {
  7494. GGML_ASSERT(size <= g_scratch_size);
  7495. if (g_scratch_offset + size > g_scratch_size) {
  7496. g_scratch_offset = 0;
  7497. }
  7498. char * data = (char *) g_scratch_buffer;
  7499. if (data == nullptr) {
  7500. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7501. g_scratch_buffer = data;
  7502. }
  7503. extra = ggml_cuda_alloc_temp_tensor_extra();
  7504. extra->data_device[g_main_device] = data + g_scratch_offset;
  7505. g_scratch_offset += size;
  7506. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7507. } else { // allocate new buffers outside of scratch
  7508. void * data;
  7509. CUDA_CHECK(cudaMalloc(&data, size));
  7510. CUDA_CHECK(cudaMemset(data, 0, size));
  7511. extra = new ggml_tensor_extra_gpu;
  7512. memset(extra, 0, sizeof(*extra));
  7513. extra->data_device[g_main_device] = data;
  7514. }
  7515. tensor->extra = extra;
  7516. }
  7517. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7518. if (g_scratch_size == 0) {
  7519. return;
  7520. }
  7521. if (g_scratch_buffer == nullptr) {
  7522. ggml_cuda_set_device(g_main_device);
  7523. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7524. }
  7525. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7526. const bool inplace = tensor->view_src != nullptr;
  7527. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  7528. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  7529. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7530. size_t view_offset = 0;
  7531. if (tensor->op == GGML_OP_VIEW) {
  7532. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7533. }
  7534. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7535. } else {
  7536. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7537. }
  7538. tensor->extra = extra;
  7539. }
  7540. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7541. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7542. GGML_ASSERT(ggml_is_contiguous(tensor));
  7543. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7544. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7545. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7546. }
  7547. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7548. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7549. }
  7550. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7551. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7552. }
  7553. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  7554. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  7555. }
  7556. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  7557. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  7558. }
  7559. void ggml_cuda_set_main_device(const int main_device) {
  7560. if (main_device >= g_device_count) {
  7561. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  7562. main_device, g_device_count, g_main_device);
  7563. return;
  7564. }
  7565. if (g_main_device != main_device && g_device_count > 1) {
  7566. g_main_device = main_device;
  7567. cudaDeviceProp prop;
  7568. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  7569. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  7570. }
  7571. }
  7572. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  7573. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  7574. // it still won't always work as expected, but it's better than nothing
  7575. if (scratch_size > g_scratch_size) {
  7576. ggml_cuda_free_scratch();
  7577. }
  7578. g_scratch_size = std::max(g_scratch_size, scratch_size);
  7579. }
  7580. void ggml_cuda_free_scratch() {
  7581. if (g_scratch_buffer == nullptr) {
  7582. return;
  7583. }
  7584. CUDA_CHECK(cudaFree(g_scratch_buffer));
  7585. g_scratch_buffer = nullptr;
  7586. }
  7587. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  7588. if (!g_cublas_loaded) return false;
  7589. ggml_cuda_func_t func;
  7590. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  7591. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  7592. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  7593. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  7594. return false;
  7595. }
  7596. if (tensor->op == GGML_OP_MUL_MAT) {
  7597. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  7598. #ifndef NDEBUG
  7599. 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]);
  7600. #endif
  7601. return false;
  7602. }
  7603. }
  7604. switch (tensor->op) {
  7605. case GGML_OP_REPEAT:
  7606. func = ggml_cuda_repeat;
  7607. break;
  7608. case GGML_OP_GET_ROWS:
  7609. func = ggml_cuda_get_rows;
  7610. break;
  7611. case GGML_OP_DUP:
  7612. func = ggml_cuda_dup;
  7613. break;
  7614. case GGML_OP_ADD:
  7615. func = ggml_cuda_add;
  7616. break;
  7617. case GGML_OP_ACC:
  7618. func = ggml_cuda_acc;
  7619. break;
  7620. case GGML_OP_MUL:
  7621. func = ggml_cuda_mul;
  7622. break;
  7623. case GGML_OP_DIV:
  7624. func = ggml_cuda_div;
  7625. break;
  7626. case GGML_OP_UNARY:
  7627. switch (ggml_get_unary_op(tensor)) {
  7628. case GGML_UNARY_OP_GELU:
  7629. func = ggml_cuda_gelu;
  7630. break;
  7631. case GGML_UNARY_OP_SILU:
  7632. func = ggml_cuda_silu;
  7633. break;
  7634. case GGML_UNARY_OP_GELU_QUICK:
  7635. func = ggml_cuda_gelu_quick;
  7636. break;
  7637. case GGML_UNARY_OP_TANH:
  7638. func = ggml_cuda_tanh;
  7639. break;
  7640. case GGML_UNARY_OP_RELU:
  7641. func = ggml_cuda_relu;
  7642. break;
  7643. default:
  7644. return false;
  7645. }
  7646. break;
  7647. case GGML_OP_NORM:
  7648. func = ggml_cuda_norm;
  7649. break;
  7650. case GGML_OP_GROUP_NORM:
  7651. func = ggml_cuda_group_norm;
  7652. break;
  7653. case GGML_OP_CONCAT:
  7654. func = ggml_cuda_concat;
  7655. break;
  7656. case GGML_OP_UPSCALE:
  7657. func = ggml_cuda_upscale;
  7658. break;
  7659. case GGML_OP_PAD:
  7660. func = ggml_cuda_pad;
  7661. break;
  7662. case GGML_OP_LEAKY_RELU:
  7663. func = ggml_cuda_leaky_relu;
  7664. break;
  7665. case GGML_OP_RMS_NORM:
  7666. func = ggml_cuda_rms_norm;
  7667. break;
  7668. case GGML_OP_MUL_MAT:
  7669. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  7670. return false;
  7671. }
  7672. func = ggml_cuda_mul_mat;
  7673. break;
  7674. case GGML_OP_MUL_MAT_ID:
  7675. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  7676. return false;
  7677. }
  7678. func = ggml_cuda_mul_mat_id;
  7679. break;
  7680. case GGML_OP_SCALE:
  7681. func = ggml_cuda_scale;
  7682. break;
  7683. case GGML_OP_SQR:
  7684. func = ggml_cuda_sqr;
  7685. break;
  7686. case GGML_OP_CLAMP:
  7687. func = ggml_cuda_clamp;
  7688. break;
  7689. case GGML_OP_CPY:
  7690. func = ggml_cuda_cpy;
  7691. break;
  7692. case GGML_OP_CONT:
  7693. func = ggml_cuda_dup;
  7694. break;
  7695. case GGML_OP_NONE:
  7696. case GGML_OP_RESHAPE:
  7697. case GGML_OP_VIEW:
  7698. case GGML_OP_PERMUTE:
  7699. case GGML_OP_TRANSPOSE:
  7700. func = ggml_cuda_nop;
  7701. break;
  7702. case GGML_OP_DIAG_MASK_INF:
  7703. func = ggml_cuda_diag_mask_inf;
  7704. break;
  7705. case GGML_OP_SOFT_MAX:
  7706. func = ggml_cuda_soft_max;
  7707. break;
  7708. case GGML_OP_ROPE:
  7709. func = ggml_cuda_rope;
  7710. break;
  7711. case GGML_OP_ALIBI:
  7712. func = ggml_cuda_alibi;
  7713. break;
  7714. case GGML_OP_IM2COL:
  7715. func = ggml_cuda_im2col;
  7716. break;
  7717. case GGML_OP_SUM_ROWS:
  7718. func = ggml_cuda_sum_rows;
  7719. break;
  7720. case GGML_OP_ARGSORT:
  7721. func = ggml_cuda_argsort;
  7722. break;
  7723. default:
  7724. return false;
  7725. }
  7726. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  7727. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  7728. }
  7729. if (params->ith != 0) {
  7730. return true;
  7731. }
  7732. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  7733. return true;
  7734. }
  7735. func(tensor->src[0], tensor->src[1], tensor);
  7736. return true;
  7737. }
  7738. int ggml_cuda_get_device_count() {
  7739. int device_count;
  7740. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  7741. return 0;
  7742. }
  7743. return device_count;
  7744. }
  7745. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  7746. cudaDeviceProp prop;
  7747. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  7748. snprintf(description, description_size, "%s", prop.name);
  7749. }
  7750. ////////////////////////////////////////////////////////////////////////////////
  7751. // backend interface
  7752. #define UNUSED GGML_UNUSED
  7753. // cuda buffer
  7754. struct ggml_backend_buffer_context_cuda {
  7755. int device;
  7756. void * dev_ptr = nullptr;
  7757. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  7758. size_t temp_tensor_extra_index = 0;
  7759. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  7760. ~ggml_backend_buffer_context_cuda() {
  7761. delete[] temp_tensor_extras;
  7762. }
  7763. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7764. if (temp_tensor_extras == nullptr) {
  7765. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7766. }
  7767. size_t alloc_index = temp_tensor_extra_index;
  7768. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7769. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  7770. memset(extra, 0, sizeof(*extra));
  7771. return extra;
  7772. }
  7773. };
  7774. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7775. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7776. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  7777. delete ctx;
  7778. }
  7779. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  7780. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7781. return ctx->dev_ptr;
  7782. }
  7783. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7784. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7785. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  7786. assert(tensor->view_src->buffer->buft == buffer->buft);
  7787. tensor->backend = tensor->view_src->backend;
  7788. tensor->extra = tensor->view_src->extra;
  7789. return;
  7790. }
  7791. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  7792. extra->data_device[ctx->device] = tensor->data;
  7793. tensor->backend = GGML_BACKEND_GPU;
  7794. tensor->extra = extra;
  7795. if (ggml_is_quantized(tensor->type)) {
  7796. // initialize padding to 0 to avoid possible NaN values
  7797. int64_t row_low = 0;
  7798. int64_t row_high = ggml_nrows(tensor);
  7799. int64_t nrows_split = row_high - row_low;
  7800. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  7801. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  7802. if (padded_size > original_size && tensor->view_src == nullptr) {
  7803. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  7804. }
  7805. }
  7806. UNUSED(buffer);
  7807. }
  7808. 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) {
  7809. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7810. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7811. ggml_cuda_set_device(ctx->device);
  7812. CUDA_CHECK(cudaDeviceSynchronize());
  7813. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  7814. }
  7815. 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) {
  7816. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7817. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7818. ggml_cuda_set_device(ctx->device);
  7819. CUDA_CHECK(cudaDeviceSynchronize());
  7820. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  7821. }
  7822. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7823. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7824. ggml_cuda_set_device(ctx->device);
  7825. CUDA_CHECK(cudaDeviceSynchronize());
  7826. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  7827. }
  7828. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  7829. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  7830. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  7831. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  7832. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  7833. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  7834. /* .cpy_tensor_from = */ NULL,
  7835. /* .cpy_tensor_to = */ NULL,
  7836. /* .clear = */ ggml_backend_cuda_buffer_clear,
  7837. };
  7838. // cuda buffer type
  7839. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7840. int device = (int) (intptr_t) buft->context;
  7841. ggml_cuda_set_device(device);
  7842. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  7843. void * dev_ptr;
  7844. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  7845. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  7846. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  7847. }
  7848. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7849. return 128;
  7850. UNUSED(buft);
  7851. }
  7852. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  7853. int64_t row_low = 0;
  7854. int64_t row_high = ggml_nrows(tensor);
  7855. int64_t nrows_split = row_high - row_low;
  7856. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7857. int64_t ne0 = tensor->ne[0];
  7858. if (ggml_is_quantized(tensor->type)) {
  7859. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7860. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7861. }
  7862. }
  7863. return size;
  7864. UNUSED(buft);
  7865. }
  7866. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  7867. return ggml_backend_is_cuda(backend);
  7868. UNUSED(buft);
  7869. }
  7870. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  7871. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  7872. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  7873. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  7874. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  7875. /* .is_host = */ nullptr,
  7876. };
  7877. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  7878. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  7879. static bool ggml_backend_cuda_buffer_type_initialized = false;
  7880. if (!ggml_backend_cuda_buffer_type_initialized) {
  7881. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  7882. ggml_backend_cuda_buffer_types[i] = {
  7883. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  7884. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  7885. };
  7886. }
  7887. ggml_backend_cuda_buffer_type_initialized = true;
  7888. }
  7889. return &ggml_backend_cuda_buffer_types[device];
  7890. }
  7891. // host buffer type
  7892. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7893. ggml_cuda_host_free(buffer->context);
  7894. }
  7895. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7896. void * ptr = ggml_cuda_host_malloc(size);
  7897. if (ptr == nullptr) {
  7898. // fallback to cpu buffer
  7899. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7900. }
  7901. // FIXME: this is a hack to avoid having to implement a new buffer type
  7902. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7903. buffer->buft = buft;
  7904. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  7905. return buffer;
  7906. }
  7907. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  7908. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  7909. /* .iface = */ {
  7910. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  7911. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  7912. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7913. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  7914. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7915. },
  7916. /* .context = */ nullptr,
  7917. };
  7918. return &ggml_backend_cuda_buffer_type_host;
  7919. }
  7920. // backend
  7921. struct ggml_backend_context_cuda {
  7922. int device;
  7923. };
  7924. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  7925. return GGML_CUDA_NAME;
  7926. UNUSED(backend);
  7927. }
  7928. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  7929. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7930. delete cuda_ctx;
  7931. delete backend;
  7932. }
  7933. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  7934. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7935. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  7936. }
  7937. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7938. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7939. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7940. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7941. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  7942. }
  7943. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7944. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7945. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7946. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7947. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  7948. }
  7949. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  7950. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7951. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  7952. UNUSED(backend);
  7953. }
  7954. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7955. GGML_ASSERT(!"not implemented");
  7956. return nullptr;
  7957. UNUSED(backend);
  7958. UNUSED(cgraph);
  7959. }
  7960. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7961. GGML_ASSERT(!"not implemented");
  7962. UNUSED(backend);
  7963. UNUSED(plan);
  7964. }
  7965. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7966. GGML_ASSERT(!"not implemented");
  7967. UNUSED(backend);
  7968. UNUSED(plan);
  7969. }
  7970. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7971. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7972. ggml_cuda_set_main_device(cuda_ctx->device);
  7973. ggml_compute_params params = {};
  7974. params.type = GGML_TASK_COMPUTE;
  7975. params.ith = 0;
  7976. for (int i = 0; i < cgraph->n_nodes; i++) {
  7977. ggml_tensor * node = cgraph->nodes[i];
  7978. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  7979. continue;
  7980. assert(node->backend == GGML_BACKEND_GPU);
  7981. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7982. assert(node->extra != nullptr);
  7983. for (int j = 0; j < GGML_MAX_SRC; j++) {
  7984. if (node->src[j] != nullptr) {
  7985. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  7986. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7987. assert(node->src[j]->extra != nullptr);
  7988. }
  7989. }
  7990. bool ok = ggml_cuda_compute_forward(&params, node);
  7991. if (!ok) {
  7992. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  7993. }
  7994. GGML_ASSERT(ok);
  7995. #if 0
  7996. if (node->type == GGML_TYPE_F32) {
  7997. cudaDeviceSynchronize();
  7998. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  7999. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  8000. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  8001. ggml_type_name(node->src[0]->type),
  8002. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  8003. node->src[0]->name,
  8004. node->src[1] ? node->src[1]->name : "none");
  8005. double sum = 0.0;
  8006. double sq_sum = 0.0;
  8007. for (int i = 0; i < ggml_nelements(node); i++) {
  8008. printf("%f ", tmp[i]);
  8009. sum += tmp[i];
  8010. sq_sum += tmp[i]*tmp[i];
  8011. }
  8012. printf("\n");
  8013. printf("sum: %f, ", sum);
  8014. printf("sq_sum: %f\n", sq_sum);
  8015. }
  8016. #endif
  8017. }
  8018. UNUSED(backend);
  8019. }
  8020. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  8021. switch (op->op) {
  8022. case GGML_OP_UNARY:
  8023. switch (ggml_get_unary_op(op)) {
  8024. case GGML_UNARY_OP_GELU:
  8025. case GGML_UNARY_OP_SILU:
  8026. case GGML_UNARY_OP_RELU:
  8027. case GGML_UNARY_OP_GELU_QUICK:
  8028. case GGML_UNARY_OP_TANH:
  8029. return true;
  8030. default:
  8031. return false;
  8032. }
  8033. break;
  8034. case GGML_OP_MUL_MAT:
  8035. case GGML_OP_MUL_MAT_ID:
  8036. {
  8037. struct ggml_tensor * a;
  8038. struct ggml_tensor * b;
  8039. if (op->op == GGML_OP_MUL_MAT) {
  8040. a = op->src[0];
  8041. b = op->src[1];
  8042. } else {
  8043. a = op->src[2];
  8044. b = op->src[1];
  8045. }
  8046. if (a->ne[3] != b->ne[3]) {
  8047. return false;
  8048. }
  8049. return true;
  8050. } break;
  8051. case GGML_OP_GET_ROWS:
  8052. {
  8053. switch (op->src[0]->type) {
  8054. case GGML_TYPE_F16:
  8055. case GGML_TYPE_F32:
  8056. case GGML_TYPE_Q4_0:
  8057. case GGML_TYPE_Q4_1:
  8058. case GGML_TYPE_Q5_0:
  8059. case GGML_TYPE_Q5_1:
  8060. case GGML_TYPE_Q8_0:
  8061. return true;
  8062. default:
  8063. return false;
  8064. }
  8065. } break;
  8066. case GGML_OP_CPY:
  8067. {
  8068. ggml_type src0_type = op->src[0]->type;
  8069. ggml_type src1_type = op->src[1]->type;
  8070. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  8071. return true;
  8072. }
  8073. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  8074. return true;
  8075. }
  8076. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  8077. return true;
  8078. }
  8079. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  8080. return true;
  8081. }
  8082. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  8083. return true;
  8084. }
  8085. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8086. return true;
  8087. }
  8088. return false;
  8089. } break;
  8090. case GGML_OP_NONE:
  8091. case GGML_OP_RESHAPE:
  8092. case GGML_OP_VIEW:
  8093. case GGML_OP_PERMUTE:
  8094. case GGML_OP_TRANSPOSE:
  8095. case GGML_OP_NORM:
  8096. case GGML_OP_REPEAT:
  8097. case GGML_OP_DUP:
  8098. case GGML_OP_ADD:
  8099. case GGML_OP_MUL:
  8100. case GGML_OP_DIV:
  8101. case GGML_OP_RMS_NORM:
  8102. case GGML_OP_SCALE:
  8103. case GGML_OP_SQR:
  8104. case GGML_OP_CLAMP:
  8105. case GGML_OP_CONT:
  8106. case GGML_OP_DIAG_MASK_INF:
  8107. case GGML_OP_SOFT_MAX:
  8108. case GGML_OP_ROPE:
  8109. case GGML_OP_ALIBI:
  8110. case GGML_OP_IM2COL:
  8111. case GGML_OP_SUM_ROWS:
  8112. case GGML_OP_ARGSORT:
  8113. case GGML_OP_ACC:
  8114. case GGML_OP_CONCAT:
  8115. case GGML_OP_GROUP_NORM:
  8116. case GGML_OP_UPSCALE:
  8117. case GGML_OP_PAD:
  8118. case GGML_OP_LEAKY_RELU:
  8119. return true;
  8120. default:
  8121. return false;
  8122. }
  8123. UNUSED(backend);
  8124. }
  8125. static ggml_backend_i cuda_backend_i = {
  8126. /* .get_name = */ ggml_backend_cuda_name,
  8127. /* .free = */ ggml_backend_cuda_free,
  8128. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8129. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8130. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8131. /* .cpy_tensor_from_async = */ NULL,
  8132. /* .cpy_tensor_to_async = */ NULL,
  8133. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8134. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8135. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8136. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8137. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8138. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8139. };
  8140. ggml_backend_t ggml_backend_cuda_init(int device) {
  8141. ggml_init_cublas(); // TODO: remove from ggml.c
  8142. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8143. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8144. return nullptr;
  8145. }
  8146. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8147. ggml_cuda_set_main_device(device);
  8148. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8149. /* .device = */ device
  8150. };
  8151. ggml_backend_t cuda_backend = new ggml_backend {
  8152. /* .interface = */ cuda_backend_i,
  8153. /* .context = */ ctx
  8154. };
  8155. return cuda_backend;
  8156. }
  8157. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8158. return backend->iface.get_name == ggml_backend_cuda_name;
  8159. }
  8160. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8161. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8162. return cuda_backend;
  8163. UNUSED(params);
  8164. }
  8165. extern "C" int ggml_backend_cuda_reg_devices();
  8166. int ggml_backend_cuda_reg_devices() {
  8167. int device_count = ggml_cuda_get_device_count();
  8168. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8169. for (int i = 0; i < device_count; i++) {
  8170. char name[128];
  8171. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8172. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8173. }
  8174. return device_count;
  8175. }