ggml-cuda.cu 272 KB

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