ggml-cuda.cu 311 KB

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