ggml-metal.metal 259 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803
  1. /**
  2. * llama.cpp - commit ba1cb19cdd0d92e012e0f6e009e0620f854b6afd - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #define GGML_COMMON_DECL_METAL
  27. #define GGML_COMMON_IMPL_METAL
  28. #if defined(GGML_METAL_EMBED_LIBRARY)
  29. __embed_ggml-common.h__
  30. #else
  31. // TODO: this should not be a relative path, but can't figure out how to set Metal include paths in Package.swift
  32. #include "../ggml-common.h"
  33. #endif
  34. #include "ggml-metal-impl.h"
  35. #include <metal_stdlib>
  36. using namespace metal;
  37. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  38. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  39. #define SWAP(x, y) { auto tmp = (x); (x) = (y); (y) = tmp; }
  40. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  41. // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
  42. //
  43. // cmd:
  44. // .../usr/bin/metal -dM -E -c ggml/src/ggml-metal/ggml-metal.metal
  45. // .../usr/bin/metal -dM -E -c -target air64-apple-ios14.0 ggml/src/ggml-metal/ggml-metal.metal
  46. //
  47. #if __METAL_VERSION__ < 310 && defined(GGML_METAL_USE_BF16)
  48. #undef GGML_METAL_USE_BF16
  49. #endif
  50. #if defined(GGML_METAL_USE_BF16)
  51. typedef matrix<bfloat, 4, 4> bfloat4x4;
  52. #endif
  53. constexpr constant static float kvalues_iq4nl_f[16] = {
  54. -127.f, -104.f, -83.f, -65.f, -49.f, -35.f, -22.f, -10.f, 1.f, 13.f, 25.f, 38.f, 53.f, 69.f, 89.f, 113.f
  55. };
  56. // NOTE: this is not dequantizing - we are simply fitting the template
  57. template <typename type4x4>
  58. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  59. reg = (type4x4)(*src);
  60. }
  61. template <typename type4x4>
  62. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  63. reg = (type4x4)(*src);
  64. }
  65. template <typename type4>
  66. void dequantize_f16_t4(device const half4 * src, short il, thread type4 & reg) {
  67. reg = (type4)(*(src + il));
  68. }
  69. #if defined(GGML_METAL_USE_BF16)
  70. template <typename type4x4>
  71. void dequantize_bf16(device const bfloat4x4 * src, short il, thread type4x4 & reg) {
  72. reg = (type4x4)(*src);
  73. }
  74. #endif
  75. template <typename type4x4>
  76. void dequantize_q4_0(device const block_q4_0 * xb, short il, thread type4x4 & reg) {
  77. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  78. const float d1 = il ? (xb->d / 16.h) : xb->d;
  79. const float d2 = d1 / 256.f;
  80. const float md = -8.h * xb->d;
  81. const ushort mask0 = il ? 0x00F0 : 0x000F;
  82. const ushort mask1 = mask0 << 8;
  83. float4x4 reg_f;
  84. for (int i = 0; i < 8; i++) {
  85. reg_f[i/2][2*(i%2) + 0] = d1 * (qs[i] & mask0) + md;
  86. reg_f[i/2][2*(i%2) + 1] = d2 * (qs[i] & mask1) + md;
  87. }
  88. reg = (type4x4) reg_f;
  89. }
  90. template <typename type4>
  91. void dequantize_q4_0_t4(device const block_q4_0 * xb, short il, thread type4 & reg) {
  92. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  93. const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
  94. const float d2 = d1 / 256.f;
  95. const float md = -8.h * xb->d;
  96. const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
  97. const ushort mask1 = mask0 << 8;
  98. for (int i = 0; i < 2; i++) {
  99. reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + md;
  100. reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + md;
  101. }
  102. }
  103. template <typename type4x4>
  104. void dequantize_q4_1(device const block_q4_1 * xb, short il, thread type4x4 & reg) {
  105. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  106. const float d1 = il ? (xb->d / 16.h) : xb->d;
  107. const float d2 = d1 / 256.f;
  108. const float m = xb->m;
  109. const ushort mask0 = il ? 0x00F0 : 0x000F;
  110. const ushort mask1 = mask0 << 8;
  111. float4x4 reg_f;
  112. for (int i = 0; i < 8; i++) {
  113. reg_f[i/2][2*(i%2) + 0] = ((qs[i] & mask0) * d1) + m;
  114. reg_f[i/2][2*(i%2) + 1] = ((qs[i] & mask1) * d2) + m;
  115. }
  116. reg = (type4x4) reg_f;
  117. }
  118. template <typename type4>
  119. void dequantize_q4_1_t4(device const block_q4_1 * xb, short il, thread type4 & reg) {
  120. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  121. const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
  122. const float d2 = d1 / 256.f;
  123. const float m = xb->m;
  124. const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
  125. const ushort mask1 = mask0 << 8;
  126. for (int i = 0; i < 2; i++) {
  127. reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + m;
  128. reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + m;
  129. }
  130. }
  131. template <typename type4x4>
  132. void dequantize_q5_0(device const block_q5_0 * xb, short il, thread type4x4 & reg) {
  133. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  134. const float d = xb->d;
  135. const float md = -16.h * xb->d;
  136. const ushort mask = il ? 0x00F0 : 0x000F;
  137. const uint32_t qh = *((device const uint32_t *)xb->qh);
  138. const int x_mv = il ? 4 : 0;
  139. const int gh_mv = il ? 12 : 0;
  140. const int gh_bk = il ? 0 : 4;
  141. float4x4 reg_f;
  142. for (int i = 0; i < 8; i++) {
  143. // extract the 5-th bits for x0 and x1
  144. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  145. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  146. // combine the 4-bits from qs with the 5th bit
  147. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  148. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  149. reg_f[i/2][2*(i%2) + 0] = d * x0 + md;
  150. reg_f[i/2][2*(i%2) + 1] = d * x1 + md;
  151. }
  152. reg = (type4x4) reg_f;
  153. }
  154. template <typename type4>
  155. void dequantize_q5_0_t4(device const block_q5_0 * xb, short il, thread type4 & reg) {
  156. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  157. const float d = xb->d;
  158. const float md = -16.h * xb->d;
  159. const ushort mask = (il/4) ? 0x00F0 : 0x000F;
  160. const uint32_t qh = *((device const uint32_t *)xb->qh);
  161. const int x_mv = (il/4) ? 4 : 0;
  162. const int gh_mv = (il/4) ? 12 : 0;
  163. const int gh_bk = (il/4) ? 0 : 4;
  164. for (int ii = 0; ii < 2; ii++) {
  165. int i = 2*(il%4) + ii;
  166. // extract the 5-th bits for x0 and x1
  167. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  168. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  169. // combine the 4-bits from qs with the 5th bit
  170. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  171. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  172. reg[2*ii + 0] = d * x0 + md;
  173. reg[2*ii + 1] = d * x1 + md;
  174. }
  175. }
  176. template <typename type4x4>
  177. void dequantize_q5_1(device const block_q5_1 * xb, short il, thread type4x4 & reg) {
  178. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  179. const float d = xb->d;
  180. const float m = xb->m;
  181. const ushort mask = il ? 0x00F0 : 0x000F;
  182. const uint32_t qh = *((device const uint32_t *)xb->qh);
  183. const int x_mv = il ? 4 : 0;
  184. const int gh_mv = il ? 12 : 0;
  185. const int gh_bk = il ? 0 : 4;
  186. float4x4 reg_f;
  187. for (int i = 0; i < 8; i++) {
  188. // extract the 5-th bits for x0 and x1
  189. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  190. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  191. // combine the 4-bits from qs with the 5th bit
  192. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  193. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  194. reg_f[i/2][2*(i%2) + 0] = d * x0 + m;
  195. reg_f[i/2][2*(i%2) + 1] = d * x1 + m;
  196. }
  197. reg = (type4x4) reg_f;
  198. }
  199. template <typename type4>
  200. void dequantize_q5_1_t4(device const block_q5_1 * xb, short il, thread type4 & reg) {
  201. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  202. const float d = xb->d;
  203. const float m = xb->m;
  204. const ushort mask = (il/4) ? 0x00F0 : 0x000F;
  205. const uint32_t qh = *((device const uint32_t *)xb->qh);
  206. const int x_mv = (il/4) ? 4 : 0;
  207. const int gh_mv = (il/4) ? 12 : 0;
  208. const int gh_bk = (il/4) ? 0 : 4;
  209. for (int ii = 0; ii < 2; ii++) {
  210. int i = 2*(il%4) + ii;
  211. // extract the 5-th bits for x0 and x1
  212. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  213. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  214. // combine the 4-bits from qs with the 5th bit
  215. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  216. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  217. reg[2*ii + 0] = d * x0 + m;
  218. reg[2*ii + 1] = d * x1 + m;
  219. }
  220. }
  221. template <typename type4x4>
  222. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  223. device const int8_t * qs = ((device const int8_t *)xb->qs);
  224. const float d = xb->d;
  225. float4x4 reg_f;
  226. for (int i = 0; i < 16; i++) {
  227. reg_f[i/4][i%4] = (qs[i + 16*il] * d);
  228. }
  229. reg = (type4x4) reg_f;
  230. }
  231. template <typename type4>
  232. void dequantize_q8_0_t4(device const block_q8_0 *xb, short il, thread type4 & reg) {
  233. device const int8_t * qs = ((device const int8_t *)xb->qs);
  234. const float d = xb->d;
  235. for (int i = 0; i < 4; i++) {
  236. reg[i] = (qs[4*(il%4) + i + 16*(il/4)] * d);
  237. }
  238. }
  239. template <typename type4x4>
  240. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  241. const float d = xb->d;
  242. const float min = xb->dmin;
  243. device const uint8_t * q = (device const uint8_t *)xb->qs;
  244. float dl, ml;
  245. uint8_t sc = xb->scales[il];
  246. q = q + 32*(il/8) + 16*(il&1);
  247. il = (il/2)%4;
  248. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  249. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  250. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  251. for (int i = 0; i < 16; ++i) {
  252. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  253. }
  254. }
  255. template <typename type4x4>
  256. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  257. const half d_all = xb->d;
  258. device const uint8_t * q = (device const uint8_t *)xb->qs;
  259. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  260. device const int8_t * scales = (device const int8_t *)xb->scales;
  261. q = q + 32 * (il/8) + 16 * (il&1);
  262. h = h + 16 * (il&1);
  263. uint8_t m = 1 << (il/2);
  264. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  265. ((il/4)>0 ? 12 : 3);
  266. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  267. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  268. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  269. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  270. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  271. const float ml = 4.f * dl;
  272. il = (il/2) & 3;
  273. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  274. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  275. dl *= coef;
  276. for (int i = 0; i < 16; ++i) {
  277. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  278. }
  279. }
  280. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  281. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  282. : uchar2{uchar((q[j+4+k] & 0xF) | ((q[j-4+k] & 0xc0) >> 2)), uchar((q[j+4+k] >> 4) | ((q[j-0+k] & 0xc0) >> 2))};
  283. }
  284. template <typename type4x4>
  285. void dequantize_q4_K(device const block_q4_K * xb, short il, thread type4x4 & reg) {
  286. device const uchar * q = xb->qs;
  287. short is = (il/4) * 2;
  288. q = q + (il/4) * 32 + 16 * (il&1);
  289. il = il & 3;
  290. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  291. const float d = il < 2 ? xb->d : xb->d / 16.h;
  292. const float min = xb->dmin;
  293. const float dl = d * sc[0];
  294. const float ml = min * sc[1];
  295. const ushort mask = il < 2 ? 0x0F : 0xF0;
  296. for (int i = 0; i < 16; ++i) {
  297. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  298. }
  299. }
  300. template <typename type4x4>
  301. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  302. device const uint8_t * q = xb->qs;
  303. device const uint8_t * qh = xb->qh;
  304. short is = (il/4) * 2;
  305. q = q + 32 * (il/4) + 16 * (il&1);
  306. qh = qh + 16 * (il&1);
  307. uint8_t ul = 1 << (il/2);
  308. il = il & 3;
  309. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  310. const float d = il < 2 ? xb->d : xb->d / 16.f;
  311. const float min = xb->dmin;
  312. const float dl = d * sc[0];
  313. const float ml = min * sc[1];
  314. const ushort mask = il<2 ? 0x0F : 0xF0;
  315. const float qh_val = il<2 ? 16.f : 256.f;
  316. for (int i = 0; i < 16; ++i) {
  317. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  318. }
  319. }
  320. template <typename type4x4>
  321. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  322. const half d_all = xb->d;
  323. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  324. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  325. device const int8_t * scales = (device const int8_t *)xb->scales;
  326. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  327. qh = qh + 32*(il/8) + 16*(il&1);
  328. float sc = scales[(il%2) + 2 * ((il/2))];
  329. il = (il/2) & 3;
  330. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  331. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  332. const float coef = il>1 ? 1.f/16.f : 1.f;
  333. const float ml = d_all * sc * 32.f;
  334. const float dl = d_all * sc * coef;
  335. for (int i = 0; i < 16; ++i) {
  336. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  337. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  338. reg[i/4][i%4] = dl * q - ml;
  339. }
  340. }
  341. template <typename type4x4>
  342. void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
  343. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  344. const float d = xb->d;
  345. const int ib32 = il/2;
  346. il = il%2;
  347. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  348. // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
  349. device const uint16_t * q2 = xb->qs + 4*ib32;
  350. const uint32_t aux32_g = q2[0] | (q2[1] << 16);
  351. const uint32_t aux32_s = q2[2] | (q2[3] << 16);
  352. thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
  353. const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
  354. constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  355. uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
  356. for (int i = 0; i < 8; ++i) {
  357. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  358. }
  359. grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  360. signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
  361. for (int i = 0; i < 8; ++i) {
  362. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  363. }
  364. }
  365. template <typename type4x4>
  366. void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
  367. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  368. const float d = xb->d;
  369. const int ib32 = il/2;
  370. il = il%2;
  371. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  372. device const uint16_t * q2 = xb->qs + 4*ib32;
  373. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  374. constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
  375. uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
  376. for (int i = 0; i < 8; ++i) {
  377. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  378. }
  379. grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
  380. signs = ksigns_iq2xs[q2[2*il+1] >> 9];
  381. for (int i = 0; i < 8; ++i) {
  382. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  383. }
  384. }
  385. template <typename type4x4>
  386. void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
  387. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  388. const float d = xb->d;
  389. const int ib32 = il/2;
  390. il = il%2;
  391. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  392. device const uint8_t * q3 = xb->qs + 8*ib32;
  393. device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
  394. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  395. const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
  396. constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
  397. constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
  398. uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  399. for (int i = 0; i < 4; ++i) {
  400. reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  401. reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  402. }
  403. grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
  404. grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
  405. signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
  406. for (int i = 0; i < 4; ++i) {
  407. reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  408. reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  409. }
  410. }
  411. template <typename type4x4>
  412. void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
  413. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  414. const float d = xb->d;
  415. const int ib32 = il/2;
  416. il = il%2;
  417. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  418. device const uint8_t * qs = xb->qs + 8*ib32;
  419. device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
  420. const uint8_t qh = xb->qh[ib32] >> 4*il;
  421. const float dl = d * (1 + 2*((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf));
  422. constant uint8_t * grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+0] | ((qh << 8) & 256)));
  423. constant uint8_t * grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+1] | ((qh << 7) & 256)));
  424. for (int i = 0; i < 4; ++i) {
  425. reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
  426. reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
  427. }
  428. grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+2] | ((qh << 6) & 256)));
  429. grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+3] | ((qh << 5) & 256)));
  430. for (int i = 0; i < 4; ++i) {
  431. reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
  432. reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
  433. }
  434. }
  435. template <typename type4x4>
  436. void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
  437. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  438. const float d = xb->d;
  439. const int ib32 = il/2;
  440. il = il%2;
  441. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  442. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  443. device const uint8_t * signs = qs + QK_K/8;
  444. const uint8_t qh = xb->qh[ib32] >> 4*il;
  445. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  446. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
  447. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
  448. for (int i = 0; i < 8; ++i) {
  449. reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
  450. reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
  451. }
  452. }
  453. template <typename type4x4>
  454. void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
  455. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  456. const int ib32 = il/2;
  457. il = il%2;
  458. const float d = xb->d;
  459. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  460. device const uint16_t * qh = xb->qh;
  461. const float dl = d * (2*((qh[ib32] >> 12) & 7) + 1);
  462. const float ml = dl * (qh[ib32] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA);
  463. const uint16_t h = qh[ib32] >> 6*il;
  464. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((h << 8) & 0x700)));
  465. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((h << 5) & 0x700)));
  466. for (int i = 0; i < 4; ++i) {
  467. reg[0][i] = dl * (grid1[i] & 0xf) + ml;
  468. reg[1][i] = dl * (grid1[i] >> 4) + ml;
  469. reg[2][i] = dl * (grid2[i] & 0xf) + ml;
  470. reg[3][i] = dl * (grid2[i] >> 4) + ml;
  471. }
  472. }
  473. template <typename type4x4>
  474. void dequantize_iq1_m(device const block_iq1_m * xb, short il, thread type4x4 & reg) {
  475. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  476. const int ib32 = il/2;
  477. il = il%2;
  478. device const uint16_t * sc = (device const uint16_t *)xb->scales;
  479. iq1m_scale_t scale;
  480. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  481. const float d = scale.f16;
  482. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  483. device const uint8_t * qh = xb->qh + 2*ib32 + il;
  484. const float dl = d * (2*((sc[ib32/2] >> (6*(ib32%2)+3*il)) & 7) + 1);
  485. const float ml1 = dl * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  486. const float ml2 = dl * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  487. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  488. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  489. for (int i = 0; i < 4; ++i) {
  490. reg[0][i] = dl * (grid1[i] & 0xf) + ml1;
  491. reg[1][i] = dl * (grid1[i] >> 4) + ml1;
  492. reg[2][i] = dl * (grid2[i] & 0xf) + ml2;
  493. reg[3][i] = dl * (grid2[i] >> 4) + ml2;
  494. }
  495. }
  496. template <typename type4x4>
  497. void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
  498. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  499. const float d = xb->d;
  500. uint32_t aux32;
  501. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  502. for (int i = 0; i < 4; ++i) {
  503. aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
  504. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  505. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  506. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  507. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  508. }
  509. }
  510. template <typename type4>
  511. void dequantize_iq4_nl_t4(device const block_iq4_nl * xb, short il, thread type4 & reg) {
  512. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  513. const float d = xb->d;
  514. uint32_t aux32;
  515. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  516. aux32 = ((q4[2*(il%4)] | (q4[2*(il%4)+1] << 16)) >> 4*(il/4)) & 0x0f0f0f0f;
  517. reg[0] = d * kvalues_iq4nl_f[q8[0]];
  518. reg[1] = d * kvalues_iq4nl_f[q8[1]];
  519. reg[2] = d * kvalues_iq4nl_f[q8[2]];
  520. reg[3] = d * kvalues_iq4nl_f[q8[3]];
  521. }
  522. template <typename type4x4>
  523. void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) {
  524. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  525. const int ib32 = il/2;
  526. il = il%2;
  527. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  528. device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32;
  529. const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4);
  530. const float d = (float)xb->d * (ls - 32);
  531. uint32_t aux32;
  532. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  533. for (int i = 0; i < 4; ++i) {
  534. aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f;
  535. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  536. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  537. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  538. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  539. }
  540. }
  541. enum ggml_sort_order {
  542. GGML_SORT_ORDER_ASC,
  543. GGML_SORT_ORDER_DESC,
  544. };
  545. // general-purpose kernel for addition, subtraction, multiplication and division of two tensors
  546. // pros: works for non-contiguous tensors, supports broadcast across all dims
  547. // cons: not very efficient
  548. kernel void kernel_add(
  549. constant ggml_metal_kargs_bin & args,
  550. device const char * src0,
  551. device const char * src1,
  552. device char * dst,
  553. uint3 tgpig[[threadgroup_position_in_grid]],
  554. ushort3 tpitg[[thread_position_in_threadgroup]],
  555. ushort3 ntg[[threads_per_threadgroup]]) {
  556. const int i03 = tgpig.z;
  557. const int i02 = tgpig.y;
  558. const int i01 = tgpig.x;
  559. const int i13 = i03%args.ne13;
  560. const int i12 = i02%args.ne12;
  561. const int i11 = i01%args.ne11;
  562. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs;
  563. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  564. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs;
  565. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  566. const int i10 = i0%args.ne10;
  567. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) + *((device float *)(src1_ptr + i10*args.nb10));
  568. }
  569. }
  570. kernel void kernel_sub(
  571. constant ggml_metal_kargs_bin & args,
  572. device const char * src0,
  573. device const char * src1,
  574. device char * dst,
  575. uint3 tgpig[[threadgroup_position_in_grid]],
  576. ushort3 tpitg[[thread_position_in_threadgroup]],
  577. ushort3 ntg[[threads_per_threadgroup]]) {
  578. const int i03 = tgpig.z;
  579. const int i02 = tgpig.y;
  580. const int i01 = tgpig.x;
  581. const int i13 = i03%args.ne13;
  582. const int i12 = i02%args.ne12;
  583. const int i11 = i01%args.ne11;
  584. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs;
  585. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  586. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs;
  587. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  588. const int i10 = i0%args.ne10;
  589. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) - *((device float *)(src1_ptr + i10*args.nb10));
  590. }
  591. }
  592. kernel void kernel_mul(
  593. constant ggml_metal_kargs_bin & args,
  594. device const char * src0,
  595. device const char * src1,
  596. device char * dst,
  597. uint3 tgpig[[threadgroup_position_in_grid]],
  598. ushort3 tpitg[[thread_position_in_threadgroup]],
  599. ushort3 ntg[[threads_per_threadgroup]]) {
  600. const int i03 = tgpig.z;
  601. const int i02 = tgpig.y;
  602. const int i01 = tgpig.x;
  603. const int i13 = i03%args.ne13;
  604. const int i12 = i02%args.ne12;
  605. const int i11 = i01%args.ne11;
  606. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  607. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  608. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1;
  609. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  610. const int i10 = i0%args.ne10;
  611. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) * *((device float *)(src1_ptr + i10*args.nb10));
  612. }
  613. }
  614. kernel void kernel_div(
  615. constant ggml_metal_kargs_bin & args,
  616. device const char * src0,
  617. device const char * src1,
  618. device char * dst,
  619. uint3 tgpig[[threadgroup_position_in_grid]],
  620. ushort3 tpitg[[thread_position_in_threadgroup]],
  621. ushort3 ntg[[threads_per_threadgroup]]) {
  622. const int i03 = tgpig.z;
  623. const int i02 = tgpig.y;
  624. const int i01 = tgpig.x;
  625. const int i13 = i03%args.ne13;
  626. const int i12 = i02%args.ne12;
  627. const int i11 = i01%args.ne11;
  628. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  629. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  630. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1;
  631. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  632. const int i10 = i0%args.ne10;
  633. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) / *((device float *)(src1_ptr + i10*args.nb10));
  634. }
  635. }
  636. template<typename T>
  637. kernel void kernel_repeat(
  638. constant ggml_metal_kargs_repeat & args,
  639. device const char * src0,
  640. device char * dst,
  641. uint3 tgpig[[threadgroup_position_in_grid]],
  642. ushort3 tpitg[[thread_position_in_threadgroup]],
  643. ushort3 ntg[[threads_per_threadgroup]]) {
  644. const int i3 = tgpig.z;
  645. const int i2 = tgpig.y;
  646. const int i1 = tgpig.x;
  647. const int i03 = i3%args.ne03;
  648. const int i02 = i2%args.ne02;
  649. const int i01 = i1%args.ne01;
  650. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  651. device char * dst_ptr = dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1;
  652. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  653. const int i00 = i0%args.ne00;
  654. *((device T *)(dst_ptr + i0*args.nb0)) = *((device T *)(src0_ptr + i00*args.nb00));
  655. }
  656. }
  657. typedef decltype(kernel_repeat<float>) kernel_repeat_t;
  658. template [[host_name("kernel_repeat_f32")]] kernel kernel_repeat_t kernel_repeat<float>;
  659. template [[host_name("kernel_repeat_f16")]] kernel kernel_repeat_t kernel_repeat<half>;
  660. template [[host_name("kernel_repeat_i32")]] kernel kernel_repeat_t kernel_repeat<int>;
  661. template [[host_name("kernel_repeat_i16")]] kernel kernel_repeat_t kernel_repeat<short>;
  662. // assumption: src1 is a row
  663. // broadcast src1 into src0
  664. kernel void kernel_add_row(
  665. constant ggml_metal_kargs_bin & args,
  666. device const float4 * src0,
  667. device const float4 * src1,
  668. device float4 * dst,
  669. uint tpig[[thread_position_in_grid]]) {
  670. const uint nb = args.ne00/4;
  671. dst[tpig] = src0[tpig] + src1[tpig % nb];
  672. }
  673. kernel void kernel_sub_row(
  674. constant ggml_metal_kargs_bin & args,
  675. device const float4 * src0,
  676. device const float4 * src1,
  677. device float4 * dst,
  678. uint tpig[[thread_position_in_grid]]) {
  679. const uint nb = args.ne00/4;
  680. dst[tpig] = src0[tpig] - src1[tpig % nb];
  681. }
  682. kernel void kernel_mul_row(
  683. constant ggml_metal_kargs_bin & args,
  684. device const float4 * src0,
  685. device const float4 * src1,
  686. device float4 * dst,
  687. uint tpig[[thread_position_in_grid]]) {
  688. const uint nb = args.ne00/4;
  689. dst[tpig] = src0[tpig] * src1[tpig % nb];
  690. }
  691. kernel void kernel_div_row(
  692. constant ggml_metal_kargs_bin & args,
  693. device const float4 * src0,
  694. device const float4 * src1,
  695. device float4 * dst,
  696. uint tpig[[thread_position_in_grid]]) {
  697. const uint nb = args.ne00/4;
  698. dst[tpig] = src0[tpig] / src1[tpig % nb];
  699. }
  700. kernel void kernel_scale(
  701. device const float * src0,
  702. device float * dst,
  703. constant float & scale,
  704. uint tpig[[thread_position_in_grid]]) {
  705. dst[tpig] = src0[tpig] * scale;
  706. }
  707. kernel void kernel_scale_4(
  708. device const float4 * src0,
  709. device float4 * dst,
  710. constant float & scale,
  711. uint tpig[[thread_position_in_grid]]) {
  712. dst[tpig] = src0[tpig] * scale;
  713. }
  714. kernel void kernel_clamp(
  715. device const float * src0,
  716. device float * dst,
  717. constant float & min,
  718. constant float & max,
  719. uint tpig[[thread_position_in_grid]]) {
  720. dst[tpig] = src0[tpig] < min ? min : (src0[tpig] > max ? max : src0[tpig]);
  721. }
  722. kernel void kernel_relu(
  723. device const float * src0,
  724. device float * dst,
  725. uint tpig[[thread_position_in_grid]]) {
  726. dst[tpig] = max(0.0f, src0[tpig]);
  727. }
  728. kernel void kernel_sigmoid(
  729. device const float * src0,
  730. device float * dst,
  731. uint tpig[[thread_position_in_grid]]) {
  732. dst[tpig] = 1.0f / (1.0f + exp(-src0[tpig]));
  733. }
  734. kernel void kernel_tanh(
  735. device const float * src0,
  736. device float * dst,
  737. uint tpig[[thread_position_in_grid]]) {
  738. device const float & x = src0[tpig];
  739. dst[tpig] = precise::tanh(x);
  740. }
  741. constant float GELU_COEF_A = 0.044715f;
  742. constant float GELU_QUICK_COEF = -1.702f;
  743. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  744. kernel void kernel_gelu(
  745. device const float * src0,
  746. device float * dst,
  747. uint tpig[[thread_position_in_grid]]) {
  748. device const float & x = src0[tpig];
  749. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  750. }
  751. kernel void kernel_gelu_4(
  752. device const float4 * src0,
  753. device float4 * dst,
  754. uint tpig[[thread_position_in_grid]]) {
  755. device const float4 & x = src0[tpig];
  756. // BEWARE !!!
  757. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  758. // This was observed with Falcon 7B and 40B models
  759. //
  760. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  761. }
  762. kernel void kernel_gelu_quick(
  763. device const float * src0,
  764. device float * dst,
  765. uint tpig[[thread_position_in_grid]]) {
  766. device const float & x = src0[tpig];
  767. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  768. }
  769. kernel void kernel_gelu_quick_4(
  770. device const float4 * src0,
  771. device float4 * dst,
  772. uint tpig[[thread_position_in_grid]]) {
  773. device const float4 & x = src0[tpig];
  774. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  775. }
  776. kernel void kernel_silu(
  777. device const float * src0,
  778. device float * dst,
  779. uint tpig[[thread_position_in_grid]]) {
  780. device const float & x = src0[tpig];
  781. dst[tpig] = x / (1.0f + exp(-x));
  782. }
  783. kernel void kernel_silu_4(
  784. device const float4 * src0,
  785. device float4 * dst,
  786. uint tpig[[thread_position_in_grid]]) {
  787. device const float4 & x = src0[tpig];
  788. dst[tpig] = x / (1.0f + exp(-x));
  789. }
  790. kernel void kernel_elu(
  791. device const float * src0,
  792. device float * dst,
  793. uint tpig[[thread_position_in_grid]]) {
  794. device const float & x = src0[tpig];
  795. dst[tpig] = (x > 0.0f) ? x : (exp(x) - 1.0f);
  796. }
  797. kernel void kernel_sqr(
  798. device const float * src0,
  799. device float * dst,
  800. uint tpig[[thread_position_in_grid]]) {
  801. dst[tpig] = src0[tpig] * src0[tpig];
  802. }
  803. kernel void kernel_sqrt(
  804. device const float * src0,
  805. device float * dst,
  806. uint tpig[[thread_position_in_grid]]) {
  807. dst[tpig] = sqrt(src0[tpig]);
  808. }
  809. kernel void kernel_sin(
  810. device const float * src0,
  811. device float * dst,
  812. uint tpig[[thread_position_in_grid]]) {
  813. dst[tpig] = sin(src0[tpig]);
  814. }
  815. kernel void kernel_cos(
  816. device const float * src0,
  817. device float * dst,
  818. uint tpig[[thread_position_in_grid]]) {
  819. dst[tpig] = cos(src0[tpig]);
  820. }
  821. kernel void kernel_sum_rows(
  822. device const float * src0,
  823. device float * dst,
  824. constant int64_t & ne00,
  825. constant int64_t & ne01,
  826. constant int64_t & ne02,
  827. constant int64_t & ne03,
  828. constant uint64_t & nb00,
  829. constant uint64_t & nb01,
  830. constant uint64_t & nb02,
  831. constant uint64_t & nb03,
  832. constant int64_t & ne10,
  833. constant int64_t & ne11,
  834. constant int64_t & ne12,
  835. constant int64_t & ne13,
  836. constant uint64_t & nb10,
  837. constant uint64_t & nb11,
  838. constant uint64_t & nb12,
  839. constant uint64_t & nb13,
  840. constant int64_t & ne0,
  841. constant int64_t & ne1,
  842. constant int64_t & ne2,
  843. constant int64_t & ne3,
  844. constant uint64_t & nb0,
  845. constant uint64_t & nb1,
  846. constant uint64_t & nb2,
  847. constant uint64_t & nb3,
  848. uint3 tpig[[thread_position_in_grid]]) {
  849. int64_t i3 = tpig.z;
  850. int64_t i2 = tpig.y;
  851. int64_t i1 = tpig.x;
  852. if (i3 >= ne03 || i2 >= ne02 || i1 >= ne01) {
  853. return;
  854. }
  855. device const float * src_row = (device const float *) ((device const char *) src0 + i1*nb01 + i2*nb02 + i3*nb03);
  856. device float * dst_row = (device float *) ((device char *) dst + i1*nb1 + i2*nb2 + i3*nb3);
  857. float row_sum = 0;
  858. for (int64_t i0 = 0; i0 < ne00; i0++) {
  859. row_sum += src_row[i0];
  860. }
  861. dst_row[0] = row_sum;
  862. }
  863. template<typename T>
  864. kernel void kernel_soft_max(
  865. device const char * src0,
  866. device const char * src1,
  867. device char * dst,
  868. constant int64_t & ne00,
  869. constant int64_t & ne01,
  870. constant int64_t & ne02,
  871. constant float & scale,
  872. constant float & max_bias,
  873. constant float & m0,
  874. constant float & m1,
  875. constant uint32_t & n_head_log2,
  876. threadgroup float * buf [[threadgroup(0)]],
  877. uint tgpig[[threadgroup_position_in_grid]],
  878. uint tpitg[[thread_position_in_threadgroup]],
  879. uint sgitg[[simdgroup_index_in_threadgroup]],
  880. uint tiisg[[thread_index_in_simdgroup]],
  881. uint ntg[[threads_per_threadgroup]]) {
  882. const int64_t i03 = (tgpig) / (ne02*ne01);
  883. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  884. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  885. device const float * psrc0 = (device const float *) src0 + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  886. device const T * pmask = src1 != src0 ? (device const T *) src1 + i01*ne00 : nullptr;
  887. device float * pdst = (device float *) dst + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  888. float slope = 1.0f;
  889. // ALiBi
  890. if (max_bias > 0.0f) {
  891. const int64_t h = i02;
  892. const float base = h < n_head_log2 ? m0 : m1;
  893. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  894. slope = pow(base, exp);
  895. }
  896. // parallel max
  897. float lmax = -INFINITY;
  898. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  899. lmax = MAX(lmax, psrc0[i00]*scale + (pmask ? slope*pmask[i00] : 0.0f));
  900. }
  901. // find the max value in the block
  902. float max_val = simd_max(lmax);
  903. if (ntg > N_SIMDWIDTH) {
  904. if (sgitg == 0) {
  905. buf[tiisg] = -INFINITY;
  906. }
  907. threadgroup_barrier(mem_flags::mem_threadgroup);
  908. if (tiisg == 0) {
  909. buf[sgitg] = max_val;
  910. }
  911. threadgroup_barrier(mem_flags::mem_threadgroup);
  912. max_val = buf[tiisg];
  913. max_val = simd_max(max_val);
  914. }
  915. // parallel sum
  916. float lsum = 0.0f;
  917. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  918. const float exp_psrc0 = exp((psrc0[i00]*scale + (pmask ? slope*pmask[i00] : 0.0f)) - max_val);
  919. lsum += exp_psrc0;
  920. pdst[i00] = exp_psrc0;
  921. }
  922. // This barrier fixes a failing test
  923. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  924. threadgroup_barrier(mem_flags::mem_none);
  925. float sum = simd_sum(lsum);
  926. if (ntg > N_SIMDWIDTH) {
  927. if (sgitg == 0) {
  928. buf[tiisg] = 0.0f;
  929. }
  930. threadgroup_barrier(mem_flags::mem_threadgroup);
  931. if (tiisg == 0) {
  932. buf[sgitg] = sum;
  933. }
  934. threadgroup_barrier(mem_flags::mem_threadgroup);
  935. sum = buf[tiisg];
  936. sum = simd_sum(sum);
  937. }
  938. const float inv_sum = 1.0f/sum;
  939. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  940. pdst[i00] *= inv_sum;
  941. }
  942. }
  943. template<typename T>
  944. kernel void kernel_soft_max_4(
  945. device const char * src0,
  946. device const char * src1,
  947. device char * dst,
  948. constant int64_t & ne00,
  949. constant int64_t & ne01,
  950. constant int64_t & ne02,
  951. constant float & scale,
  952. constant float & max_bias,
  953. constant float & m0,
  954. constant float & m1,
  955. constant uint32_t & n_head_log2,
  956. threadgroup float * buf [[threadgroup(0)]],
  957. uint tgpig[[threadgroup_position_in_grid]],
  958. uint tpitg[[thread_position_in_threadgroup]],
  959. uint sgitg[[simdgroup_index_in_threadgroup]],
  960. uint tiisg[[thread_index_in_simdgroup]],
  961. uint ntg[[threads_per_threadgroup]]) {
  962. const int64_t i03 = (tgpig) / (ne02*ne01);
  963. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  964. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  965. device const float4 * psrc4 = (device const float4 *) src0 + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00)/4;
  966. device const T * pmask = src1 != src0 ? (device const T *) src1 + i01*ne00/4 : nullptr;
  967. device float4 * pdst4 = (device float4 *) dst + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00)/4;
  968. float slope = 1.0f;
  969. if (max_bias > 0.0f) {
  970. const int64_t h = i02;
  971. const float base = h < n_head_log2 ? m0 : m1;
  972. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  973. slope = pow(base, exp);
  974. }
  975. // parallel max
  976. float4 lmax4 = -INFINITY;
  977. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  978. lmax4 = fmax(lmax4, psrc4[i00]*scale + (float4)((pmask ? slope*pmask[i00] : 0.0f)));
  979. }
  980. const float lmax = MAX(MAX(lmax4[0], lmax4[1]), MAX(lmax4[2], lmax4[3]));
  981. float max_val = simd_max(lmax);
  982. if (ntg > N_SIMDWIDTH) {
  983. if (sgitg == 0) {
  984. buf[tiisg] = -INFINITY;
  985. }
  986. threadgroup_barrier(mem_flags::mem_threadgroup);
  987. if (tiisg == 0) {
  988. buf[sgitg] = max_val;
  989. }
  990. threadgroup_barrier(mem_flags::mem_threadgroup);
  991. max_val = buf[tiisg];
  992. max_val = simd_max(max_val);
  993. }
  994. // parallel sum
  995. float4 lsum4 = 0.0f;
  996. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  997. const float4 exp_psrc4 = exp((psrc4[i00]*scale + (float4)((pmask ? slope*pmask[i00] : 0.0f))) - max_val);
  998. lsum4 += exp_psrc4;
  999. pdst4[i00] = exp_psrc4;
  1000. }
  1001. const float lsum = lsum4[0] + lsum4[1] + lsum4[2] + lsum4[3];
  1002. // This barrier fixes a failing test
  1003. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  1004. threadgroup_barrier(mem_flags::mem_none);
  1005. float sum = simd_sum(lsum);
  1006. if (ntg > N_SIMDWIDTH) {
  1007. if (sgitg == 0) {
  1008. buf[tiisg] = 0.0f;
  1009. }
  1010. threadgroup_barrier(mem_flags::mem_threadgroup);
  1011. if (tiisg == 0) {
  1012. buf[sgitg] = sum;
  1013. }
  1014. threadgroup_barrier(mem_flags::mem_threadgroup);
  1015. sum = buf[tiisg];
  1016. sum = simd_sum(sum);
  1017. }
  1018. const float inv_sum = 1.0f/sum;
  1019. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  1020. pdst4[i00] *= inv_sum;
  1021. }
  1022. }
  1023. typedef decltype(kernel_soft_max<float>) kernel_soft_max_t;
  1024. typedef decltype(kernel_soft_max_4<float4>) kernel_soft_max_4_t;
  1025. template [[host_name("kernel_soft_max_f16")]] kernel kernel_soft_max_t kernel_soft_max<half>;
  1026. template [[host_name("kernel_soft_max_f32")]] kernel kernel_soft_max_t kernel_soft_max<float>;
  1027. template [[host_name("kernel_soft_max_f16_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<half4>;
  1028. template [[host_name("kernel_soft_max_f32_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<float4>;
  1029. kernel void kernel_diag_mask_inf(
  1030. device const float * src0,
  1031. device float * dst,
  1032. constant int64_t & ne00,
  1033. constant int64_t & ne01,
  1034. constant int & n_past,
  1035. uint3 tpig[[thread_position_in_grid]]) {
  1036. const int64_t i02 = tpig[2];
  1037. const int64_t i01 = tpig[1];
  1038. const int64_t i00 = tpig[0];
  1039. if (i00 > n_past + i01) {
  1040. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  1041. } else {
  1042. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  1043. }
  1044. }
  1045. kernel void kernel_diag_mask_inf_8(
  1046. device const float4 * src0,
  1047. device float4 * dst,
  1048. constant int64_t & ne00,
  1049. constant int64_t & ne01,
  1050. constant int & n_past,
  1051. uint3 tpig[[thread_position_in_grid]]) {
  1052. const int64_t i = 2*tpig[0];
  1053. dst[i+0] = src0[i+0];
  1054. dst[i+1] = src0[i+1];
  1055. int64_t i4 = 4*i;
  1056. const int64_t i02 = i4/(ne00*ne01); i4 -= i02*ne00*ne01;
  1057. const int64_t i01 = i4/(ne00); i4 -= i01*ne00;
  1058. const int64_t i00 = i4;
  1059. for (int k = 3; k >= 0; --k) {
  1060. if (i00 + 4 + k <= n_past + i01) {
  1061. break;
  1062. }
  1063. dst[i+1][k] = -INFINITY;
  1064. if (i00 + k > n_past + i01) {
  1065. dst[i][k] = -INFINITY;
  1066. }
  1067. }
  1068. }
  1069. // ref: ggml.c:ggml_compute_forward_ssm_conv_f32
  1070. // TODO: optimize
  1071. kernel void kernel_ssm_conv_f32(
  1072. device const void * src0,
  1073. device const void * src1,
  1074. device float * dst,
  1075. constant int64_t & ne00,
  1076. constant int64_t & ne01,
  1077. constant int64_t & ne02,
  1078. constant uint64_t & nb00,
  1079. constant uint64_t & nb01,
  1080. constant uint64_t & nb02,
  1081. constant int64_t & ne10,
  1082. constant int64_t & ne11,
  1083. constant uint64_t & nb10,
  1084. constant uint64_t & nb11,
  1085. constant int64_t & ne0,
  1086. constant int64_t & ne1,
  1087. constant int64_t & ne2,
  1088. constant uint64_t & nb0,
  1089. constant uint64_t & nb1,
  1090. constant uint64_t & nb2,
  1091. uint3 tgpig[[threadgroup_position_in_grid]],
  1092. uint3 tpitg[[thread_position_in_threadgroup]],
  1093. uint3 ntg[[threads_per_threadgroup]]) {
  1094. const int64_t ir = tgpig.x;
  1095. const int64_t i2 = tgpig.y;
  1096. const int64_t i3 = tgpig.z;
  1097. const int64_t nc = ne10;
  1098. //const int64_t ncs = ne00;
  1099. //const int64_t nr = ne01;
  1100. //const int64_t n_t = ne1;
  1101. //const int64_t n_s = ne2;
  1102. device const float * s = (device const float *) ((device const char *) src0 + ir*nb01 + i2*nb00 + i3*nb02);
  1103. device const float * c = (device const float *) ((device const char *) src1 + ir*nb11);
  1104. device float * x = (device float *) ((device char *) dst + ir*nb0 + i2*nb1 + i3*nb2);
  1105. float sumf = 0.0f;
  1106. for (int64_t i0 = 0; i0 < nc; ++i0) {
  1107. sumf += s[i0] * c[i0];
  1108. }
  1109. x[0] = sumf;
  1110. }
  1111. // ref: ggml.c:ggml_compute_forward_ssm_scan_f32
  1112. // TODO: optimize
  1113. kernel void kernel_ssm_scan_f32(
  1114. device const void * src0,
  1115. device const void * src1,
  1116. device const void * src2,
  1117. device const void * src3,
  1118. device const void * src4,
  1119. device const void * src5,
  1120. device float * dst,
  1121. constant int64_t & d_state,
  1122. constant int64_t & d_inner,
  1123. constant int64_t & n_seq_tokens,
  1124. constant int64_t & n_seqs,
  1125. constant uint64_t & nb00,
  1126. constant uint64_t & nb01,
  1127. constant uint64_t & nb02,
  1128. constant uint64_t & nb10,
  1129. constant uint64_t & nb11,
  1130. constant uint64_t & nb12,
  1131. constant uint64_t & nb13,
  1132. constant uint64_t & nb20,
  1133. constant uint64_t & nb21,
  1134. constant uint64_t & nb22,
  1135. constant uint64_t & nb30,
  1136. constant uint64_t & nb31,
  1137. constant uint64_t & nb40,
  1138. constant uint64_t & nb41,
  1139. constant uint64_t & nb42,
  1140. constant uint64_t & nb50,
  1141. constant uint64_t & nb51,
  1142. constant uint64_t & nb52,
  1143. uint3 tgpig[[threadgroup_position_in_grid]],
  1144. uint3 tpitg[[thread_position_in_threadgroup]],
  1145. uint3 ntg[[threads_per_threadgroup]]) {
  1146. const int64_t ir = tgpig.x;
  1147. const int64_t i3 = tgpig.y;
  1148. const int64_t nc = d_state;
  1149. //const int64_t nr = d_inner;
  1150. const int64_t n_t = n_seq_tokens;
  1151. //const int64_t n_s = n_seqs;
  1152. for (int64_t i2 = 0; i2 < n_t; ++i2) {
  1153. device const float * s0 = (device const float *) ((device const char *) src0 + ir*nb01 + i3*nb02);
  1154. device const float * x = (device const float *) ((device const char *) src1 + ir*nb10 + i2*nb11 + i3*nb12);
  1155. device const float * dt = (device const float *) ((device const char *) src2 + ir*nb20 + i2*nb21 + i3*nb22);
  1156. device const float * A = (device const float *) ((device const char *) src3 + ir*nb31);
  1157. device const float * B = (device const float *) ((device const char *) src4 + i2*nb41 + i3*nb42);
  1158. device const float * C = (device const float *) ((device const char *) src5 + i2*nb51 + i3*nb52);
  1159. device float * y = (device float *) ((device char *) dst + ir*nb10 + i2*nb11 + i3*nb12); // TODO: do not use src1 strides
  1160. device float * s = (device float *) ((device char *) dst + ir*nb01 + i3*nb02 + nb13);
  1161. if (i2 > 0) {
  1162. s0 = s;
  1163. }
  1164. // i1 == 0
  1165. float dt_soft_plus = dt[0] <= 20.0f ? log(1.0f + exp(dt[0])) : dt[0];
  1166. float x_dt = x[0] * dt_soft_plus;
  1167. float sumf = 0.0f;
  1168. for (int64_t i0 = 0; i0 < nc; ++i0) {
  1169. int64_t i = i0;
  1170. float state = (s0[i] * exp(dt_soft_plus * A[i])) + (B[i0] * x_dt);
  1171. sumf += state * C[i0];
  1172. s[i] = state;
  1173. }
  1174. y[0] = sumf;
  1175. }
  1176. }
  1177. kernel void kernel_argmax(
  1178. device const void * x,
  1179. device int32_t * dst,
  1180. constant int64_t & ncols,
  1181. constant uint64_t & nb01,
  1182. threadgroup float * shared_maxval [[threadgroup(0)]],
  1183. threadgroup int32_t * shared_argmax [[threadgroup(1)]],
  1184. uint tgpig[[threadgroup_position_in_grid]],
  1185. uint tpitg[[thread_position_in_threadgroup]],
  1186. uint sgitg[[simdgroup_index_in_threadgroup]],
  1187. uint tiisg[[thread_index_in_simdgroup]],
  1188. uint ntg[[threads_per_threadgroup]]) {
  1189. device const float * x_row = (device const float *) ((device const char *) x + tgpig * nb01);
  1190. float lmax = -INFINITY;
  1191. int32_t larg = -1;
  1192. for (int i00 = tpitg; i00 < ncols; i00 += ntg) {
  1193. if (x_row[i00] > lmax) {
  1194. lmax = x_row[i00];
  1195. larg = i00;
  1196. }
  1197. }
  1198. // find the argmax value in the block
  1199. float max_val = simd_max(lmax);
  1200. int32_t arg_val = simd_max(select(-1, larg, lmax == max_val));
  1201. if (ntg > N_SIMDWIDTH) {
  1202. if (sgitg == 0) {
  1203. shared_maxval[tiisg] = -INFINITY;
  1204. shared_argmax[tiisg] = -1;
  1205. }
  1206. threadgroup_barrier(mem_flags::mem_threadgroup);
  1207. if (tiisg == 0) {
  1208. shared_maxval[sgitg] = max_val;
  1209. shared_argmax[sgitg] = arg_val;
  1210. }
  1211. threadgroup_barrier(mem_flags::mem_threadgroup);
  1212. max_val = shared_maxval[tiisg];
  1213. arg_val = shared_argmax[tiisg];
  1214. float max_val_reduced = simd_max(max_val);
  1215. int32_t arg_val_reduced = simd_max(select(-1, arg_val, max_val == max_val_reduced));
  1216. dst[tgpig] = arg_val_reduced;
  1217. return;
  1218. }
  1219. dst[tgpig] = arg_val;
  1220. }
  1221. kernel void kernel_norm(
  1222. constant ggml_metal_kargs_norm & args,
  1223. device const char * src0,
  1224. device char * dst,
  1225. threadgroup float * shmem_f32 [[threadgroup(0)]],
  1226. uint tgpig[[threadgroup_position_in_grid]],
  1227. ushort tpitg[[thread_position_in_threadgroup]],
  1228. ushort sgitg[[simdgroup_index_in_threadgroup]],
  1229. ushort tiisg[[thread_index_in_simdgroup]],
  1230. ushort ntg[[threads_per_threadgroup]]) {
  1231. if (sgitg == 0) {
  1232. shmem_f32[tiisg] = 0.0f;
  1233. }
  1234. device const float4 * x = (device const float4 *) (src0 + tgpig*args.nb01);
  1235. float4 sumf4(0.0f);
  1236. float sumf = 0.0f;
  1237. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  1238. sumf4 += x[i00];
  1239. }
  1240. sumf = sumf4[0] + sumf4[1] + sumf4[2] + sumf4[3];
  1241. sumf = simd_sum(sumf);
  1242. threadgroup_barrier(mem_flags::mem_threadgroup);
  1243. if (tiisg == 0) {
  1244. shmem_f32[sgitg] = sumf;
  1245. }
  1246. threadgroup_barrier(mem_flags::mem_threadgroup);
  1247. sumf = shmem_f32[tiisg];
  1248. sumf = simd_sum(sumf);
  1249. const float mean = sumf/args.ne00;
  1250. device float4 * y = (device float4 *) dst + tgpig*args.ne00_4;
  1251. sumf = 0.0f;
  1252. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  1253. y[i00] = x[i00] - mean;
  1254. sumf += dot(y[i00], y[i00]);
  1255. }
  1256. sumf = simd_sum(sumf);
  1257. threadgroup_barrier(mem_flags::mem_threadgroup);
  1258. if (tiisg == 0) {
  1259. shmem_f32[sgitg] = sumf;
  1260. }
  1261. threadgroup_barrier(mem_flags::mem_threadgroup);
  1262. sumf = shmem_f32[tiisg];
  1263. sumf = simd_sum(sumf);
  1264. const float variance = sumf/args.ne00;
  1265. const float scale = 1.0f/sqrt(variance + args.eps);
  1266. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  1267. y[i00] = y[i00] * scale;
  1268. }
  1269. }
  1270. kernel void kernel_rms_norm(
  1271. constant ggml_metal_kargs_rms_norm & args,
  1272. device const char * src0,
  1273. device char * dst,
  1274. threadgroup float * shmem_f32 [[threadgroup(0)]],
  1275. uint tgpig[[threadgroup_position_in_grid]],
  1276. ushort tpitg[[thread_position_in_threadgroup]],
  1277. ushort sgitg[[simdgroup_index_in_threadgroup]],
  1278. ushort tiisg[[thread_index_in_simdgroup]],
  1279. ushort ntg[[threads_per_threadgroup]]) {
  1280. if (sgitg == 0) {
  1281. shmem_f32[tiisg] = 0.0f;
  1282. }
  1283. device const float4 * x = (device const float4 *) (src0 + tgpig*args.nb01);
  1284. float sumf = 0.0f;
  1285. // parallel sum
  1286. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  1287. sumf += dot(x[i00], x[i00]);
  1288. }
  1289. sumf = simd_sum(sumf);
  1290. threadgroup_barrier(mem_flags::mem_threadgroup);
  1291. if (tiisg == 0) {
  1292. shmem_f32[sgitg] = sumf;
  1293. }
  1294. threadgroup_barrier(mem_flags::mem_threadgroup);
  1295. sumf = shmem_f32[tiisg];
  1296. sumf = simd_sum(sumf);
  1297. const float mean = sumf/args.ne00;
  1298. const float scale = 1.0f/sqrt(mean + args.eps);
  1299. device float4 * y = (device float4 *) dst + tgpig*args.ne00_4;
  1300. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  1301. y[i00] = x[i00] * scale;
  1302. }
  1303. }
  1304. kernel void kernel_group_norm(
  1305. device const float * src0,
  1306. device float * dst,
  1307. constant int64_t & ne00,
  1308. constant int64_t & ne01,
  1309. constant int64_t & ne02,
  1310. constant uint64_t & nb00,
  1311. constant uint64_t & nb01,
  1312. constant uint64_t & nb02,
  1313. constant int32_t & n_groups,
  1314. constant float & eps,
  1315. threadgroup float * buf [[threadgroup(0)]],
  1316. uint tgpig[[threadgroup_position_in_grid]],
  1317. uint tpitg[[thread_position_in_threadgroup]],
  1318. uint sgitg[[simdgroup_index_in_threadgroup]],
  1319. uint tiisg[[thread_index_in_simdgroup]],
  1320. uint ntg[[threads_per_threadgroup]]) {
  1321. const int64_t ne = ne00*ne01*ne02;
  1322. const int64_t gs = ne00*ne01*((ne02 + n_groups - 1) / n_groups);
  1323. int start = tgpig * gs;
  1324. int end = start + gs;
  1325. start += tpitg;
  1326. if (end >= ne) {
  1327. end = ne;
  1328. }
  1329. float tmp = 0.0f; // partial sum for thread in warp
  1330. for (int j = start; j < end; j += ntg) {
  1331. tmp += src0[j];
  1332. }
  1333. threadgroup_barrier(mem_flags::mem_threadgroup);
  1334. tmp = simd_sum(tmp);
  1335. if (ntg > N_SIMDWIDTH) {
  1336. if (sgitg == 0) {
  1337. buf[tiisg] = 0.0f;
  1338. }
  1339. threadgroup_barrier(mem_flags::mem_threadgroup);
  1340. if (tiisg == 0) {
  1341. buf[sgitg] = tmp;
  1342. }
  1343. threadgroup_barrier(mem_flags::mem_threadgroup);
  1344. tmp = buf[tiisg];
  1345. tmp = simd_sum(tmp);
  1346. }
  1347. const float mean = tmp / gs;
  1348. tmp = 0.0f;
  1349. for (int j = start; j < end; j += ntg) {
  1350. float xi = src0[j] - mean;
  1351. dst[j] = xi;
  1352. tmp += xi * xi;
  1353. }
  1354. tmp = simd_sum(tmp);
  1355. if (ntg > N_SIMDWIDTH) {
  1356. if (sgitg == 0) {
  1357. buf[tiisg] = 0.0f;
  1358. }
  1359. threadgroup_barrier(mem_flags::mem_threadgroup);
  1360. if (tiisg == 0) {
  1361. buf[sgitg] = tmp;
  1362. }
  1363. threadgroup_barrier(mem_flags::mem_threadgroup);
  1364. tmp = buf[tiisg];
  1365. tmp = simd_sum(tmp);
  1366. }
  1367. const float variance = tmp / gs;
  1368. const float scale = 1.0f/sqrt(variance + eps);
  1369. for (int j = start; j < end; j += ntg) {
  1370. dst[j] *= scale;
  1371. }
  1372. }
  1373. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  1374. // il indicates where the q4 quants begin (0 or QK4_0/4)
  1375. // we assume that the yl's have been multiplied with the appropriate scale factor
  1376. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  1377. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  1378. float d = qb_curr->d;
  1379. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1380. device const uint16_t * qs = ((device const uint16_t *) qb_curr + 1 + il/2);
  1381. for (int i = 0; i < 8; i += 2) {
  1382. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F);
  1383. acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00);
  1384. acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0);
  1385. acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000);
  1386. }
  1387. return d * (sumy * -8.f + acc[0] + acc[1] + acc[2] + acc[3]);
  1388. }
  1389. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  1390. // il indicates where the q4 quants begin (0 or QK4_0/4)
  1391. // we assume that the yl's have been multiplied with the appropriate scale factor
  1392. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  1393. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  1394. float d = qb_curr->d;
  1395. float m = qb_curr->m;
  1396. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1397. device const uint16_t * qs = ((device const uint16_t *) qb_curr + 2 + il/2);
  1398. for (int i = 0; i < 8; i+=2) {
  1399. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F);
  1400. acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00);
  1401. acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0);
  1402. acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000);
  1403. }
  1404. return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m;
  1405. }
  1406. // function for calculate inner product between half a q5_0 block and 16 floats (yl), sumy is SUM(yl[i])
  1407. // il indicates where the q5 quants begin (0 or QK5_0/4)
  1408. // we assume that the yl's have been multiplied with the appropriate scale factor
  1409. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  1410. inline float block_q_n_dot_y(device const block_q5_0 * qb_curr, float sumy, thread float * yl, int il) {
  1411. float d = qb_curr->d;
  1412. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1413. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 3 + il/2);
  1414. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  1415. for (int i = 0; i < 8; i+=2) {
  1416. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010));
  1417. acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  1418. acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100));
  1419. acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  1420. }
  1421. return d * (sumy * -16.f + acc[0] + acc[1] + acc[2] + acc[3]);
  1422. }
  1423. // function for calculate inner product between half a q5_1 block and 16 floats (yl), sumy is SUM(yl[i])
  1424. // il indicates where the q5 quants begin (0 or QK5_1/4)
  1425. // we assume that the yl's have been multiplied with the appropriate scale factor
  1426. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  1427. inline float block_q_n_dot_y(device const block_q5_1 * qb_curr, float sumy, thread float * yl, int il) {
  1428. float d = qb_curr->d;
  1429. float m = qb_curr->m;
  1430. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1431. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 4 + il/2);
  1432. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  1433. for (int i = 0; i < 8; i+=2) {
  1434. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010));
  1435. acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  1436. acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100));
  1437. acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  1438. }
  1439. return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m;
  1440. }
  1441. // putting them in the kernel cause a significant performance penalty
  1442. #define N_DST 4 // each SIMD group works on 4 rows
  1443. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  1444. //Note: This is a template, but strictly speaking it only applies to
  1445. // quantizations where the block size is 32. It also does not
  1446. // guard against the number of rows not being divisible by
  1447. // N_DST, so this is another explicit assumption of the implementation.
  1448. template<typename block_q_type, int nr, int nsg, int nw, typename args_t>
  1449. void mul_vec_q_n_f32_impl(
  1450. args_t args,
  1451. device const char * src0,
  1452. device const char * src1,
  1453. device char * dst,
  1454. threadgroup char * shmem,
  1455. uint3 tgpig,
  1456. ushort tiisg,
  1457. ushort sgitg) {
  1458. const int nb = args.ne00/QK4_0;
  1459. const int r0 = tgpig.x;
  1460. const int r1 = tgpig.y;
  1461. const int im = tgpig.z;
  1462. const int first_row = (r0 * nsg + sgitg) * nr;
  1463. const uint i12 = im%args.ne12;
  1464. const uint i13 = im/args.ne12;
  1465. //const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1466. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  1467. //device const block_q_type * x = (device const block_q_type *) (src0 + offset0);
  1468. device const float * y = (device const float *) (src1 + offset1);
  1469. // pointers to src0 rows
  1470. device const block_q_type * ax[nr];
  1471. for (int row = 0; row < nr; ++row) {
  1472. const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1473. ax[row] = (device const block_q_type *) ((device char *) src0 + offset0);
  1474. }
  1475. float yl[16]; // src1 vector cache
  1476. float sumf[nr] = {0.f};
  1477. const short ix = (tiisg/2);
  1478. const short il = (tiisg%2)*8;
  1479. device const float * yb = y + ix*QK4_0 + il;
  1480. // each thread in a SIMD group deals with half a block.
  1481. for (int ib = ix; ib < nb; ib += nw/2) {
  1482. float sumy[2] = { 0.f, 0.f };
  1483. #pragma unroll
  1484. for (int i = 0; i < 8; i += 2) {
  1485. sumy[0] += yb[i + 0] + yb[i + 1];
  1486. yl[i + 0] = yb[i + 0];
  1487. yl[i + 1] = yb[i + 1]/256.f;
  1488. sumy[1] += yb[i + 16] + yb[i + 17];
  1489. yl[i + 8] = yb[i + 16]/16.f;
  1490. yl[i + 9] = yb[i + 17]/4096.f;
  1491. }
  1492. #pragma unroll
  1493. for (int row = 0; row < nr; row++) {
  1494. sumf[row] += block_q_n_dot_y(ax[row] + ib, sumy[0] + sumy[1], yl, il);
  1495. }
  1496. yb += QK4_0 * 16;
  1497. }
  1498. device float * dst_f32 = (device float *) dst + im*args.ne0*args.ne1 + r1*args.ne0;
  1499. for (int row = 0; row < nr; ++row) {
  1500. const float tot = simd_sum(sumf[row]);
  1501. if (tiisg == 0 && first_row + row < args.ne01) {
  1502. dst_f32[first_row + row] = tot;
  1503. }
  1504. }
  1505. }
  1506. kernel void kernel_mul_mv_q4_0_f32(
  1507. constant ggml_metal_kargs_mul_mv & args,
  1508. device const char * src0,
  1509. device const char * src1,
  1510. device char * dst,
  1511. uint3 tgpig[[threadgroup_position_in_grid]],
  1512. ushort tiisg[[thread_index_in_simdgroup]],
  1513. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1514. mul_vec_q_n_f32_impl<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH, constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  1515. }
  1516. kernel void kernel_mul_mv_q4_1_f32(
  1517. constant ggml_metal_kargs_mul_mv & args,
  1518. device const char * src0,
  1519. device const char * src1,
  1520. device char * dst,
  1521. uint3 tgpig[[threadgroup_position_in_grid]],
  1522. ushort tiisg[[thread_index_in_simdgroup]],
  1523. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1524. mul_vec_q_n_f32_impl<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH, constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  1525. }
  1526. kernel void kernel_mul_mv_q5_0_f32(
  1527. constant ggml_metal_kargs_mul_mv & args,
  1528. device const char * src0,
  1529. device const char * src1,
  1530. device char * dst,
  1531. uint3 tgpig[[threadgroup_position_in_grid]],
  1532. ushort tiisg[[thread_index_in_simdgroup]],
  1533. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1534. mul_vec_q_n_f32_impl<block_q5_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH, constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  1535. }
  1536. kernel void kernel_mul_mv_q5_1_f32(
  1537. constant ggml_metal_kargs_mul_mv & args,
  1538. device const char * src0,
  1539. device const char * src1,
  1540. device char * dst,
  1541. uint3 tgpig[[threadgroup_position_in_grid]],
  1542. ushort tiisg[[thread_index_in_simdgroup]],
  1543. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1544. mul_vec_q_n_f32_impl<block_q5_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH, constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  1545. }
  1546. #define NB_Q8_0 8
  1547. template<typename args_t>
  1548. void kernel_mul_mv_q8_0_f32_impl(
  1549. args_t args,
  1550. device const char * src0,
  1551. device const char * src1,
  1552. device char * dst,
  1553. threadgroup char * shmem,
  1554. uint3 tgpig,
  1555. ushort tiisg,
  1556. ushort sgitg) {
  1557. const int nr = N_DST;
  1558. const int nsg = N_SIMDGROUP;
  1559. const int nw = N_SIMDWIDTH;
  1560. const int nb = args.ne00/QK8_0;
  1561. const int r0 = tgpig.x;
  1562. const int r1 = tgpig.y;
  1563. const int im = tgpig.z;
  1564. const int first_row = (r0*nsg + sgitg)*nr;
  1565. const uint i12 = im%args.ne12;
  1566. const uint i13 = im/args.ne12;
  1567. //const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1568. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  1569. //device const block_q8_0 * x = (device const block_q8_0 *) (src0 + offset0);
  1570. device const float * y = (device const float *) (src1 + offset1);
  1571. // pointers to src0 rows
  1572. device const block_q8_0 * ax[nr];
  1573. for (int row = 0; row < nr; ++row) {
  1574. const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1575. ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0);
  1576. }
  1577. float yl[NB_Q8_0];
  1578. float sumf[nr] = { 0.f };
  1579. const short ix = tiisg/4;
  1580. const short il = tiisg%4;
  1581. device const float * yb = y + ix*QK8_0 + il*NB_Q8_0;
  1582. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  1583. for (int ib = ix; ib < nb; ib += nw/4) {
  1584. for (short i = 0; i < NB_Q8_0; ++i) {
  1585. yl[i] = yb[i];
  1586. }
  1587. for (int row = 0; row < nr; row++) {
  1588. device const int8_t * qs = ax[row][ib].qs + il*NB_Q8_0;
  1589. float sumq = 0.f;
  1590. for (short iq = 0; iq < NB_Q8_0; ++iq) {
  1591. sumq += qs[iq] * yl[iq];
  1592. }
  1593. sumf[row] += sumq*ax[row][ib].d;
  1594. }
  1595. yb += nw*NB_Q8_0;
  1596. }
  1597. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  1598. for (int row = 0; row < nr; ++row) {
  1599. const float tot = simd_sum(sumf[row]);
  1600. if (tiisg == 0 && first_row + row < args.ne01) {
  1601. dst_f32[first_row + row] = tot;
  1602. }
  1603. }
  1604. }
  1605. [[host_name("kernel_mul_mv_q8_0_f32")]]
  1606. kernel void kernel_mul_mv_q8_0_f32(
  1607. constant ggml_metal_kargs_mul_mv & args,
  1608. device const char * src0,
  1609. device const char * src1,
  1610. device char * dst,
  1611. uint3 tgpig[[threadgroup_position_in_grid]],
  1612. ushort tiisg[[thread_index_in_simdgroup]],
  1613. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1614. kernel_mul_mv_q8_0_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  1615. }
  1616. // mat-vec kernel processing in chunks of float4
  1617. // chpb - chunks per quantization block
  1618. template<short nxpsg, short r1ptg, typename q_t, short chpb, void (*deq_t4)(device const q_t *, short, thread float4 &) >
  1619. void kernel_mul_mv_ext_q4_f32_impl(
  1620. constant ggml_metal_kargs_mul_mv_ext & args,
  1621. device const char * src0,
  1622. device const char * src1,
  1623. device char * dst,
  1624. uint3 tgpig[[threadgroup_position_in_grid]],
  1625. ushort tiisg[[thread_index_in_simdgroup]],
  1626. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1627. const short chpt = 4; // chunks per thread
  1628. //const short nxpsg = (32);
  1629. const short nypsg = (32/nxpsg);
  1630. const short tx = tiisg%nxpsg;
  1631. const short ty = tiisg/nxpsg;
  1632. const int i01 = tgpig.x*(nypsg*args.nsg) + nypsg*sgitg + ty;
  1633. const int i11 = tgpig.y*r1ptg;
  1634. const int i1m = tgpig.z;
  1635. const int i12 = i1m%args.ne12;
  1636. const int i13 = i1m/args.ne12;
  1637. const uint64_t offset0 = i01*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1638. const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  1639. device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0;
  1640. device const float4 * y4[r1ptg];
  1641. for (int ir1 = 0; ir1 < r1ptg; ++ir1) {
  1642. y4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4 *) src1;
  1643. }
  1644. float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f };
  1645. short cch = tx%chpb; // current chunk index
  1646. for (int ich = tx; 4*ich < args.ne00; ich += chpt*nxpsg) {
  1647. float4 lx[chpt];
  1648. #pragma unroll(chpt)
  1649. for (short ch = 0; ch < chpt; ++ch) {
  1650. deq_t4(xq, cch, lx[ch]);
  1651. cch += nxpsg;
  1652. if (cch >= chpb) {
  1653. xq += cch/chpb;
  1654. cch %= chpb;
  1655. }
  1656. }
  1657. #pragma unroll(chpt)
  1658. for (short ch = 0; ch < chpt; ++ch) {
  1659. #pragma unroll(r1ptg)
  1660. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  1661. sumf[ir1] += dot(lx[ch], y4[ir1][ch*nxpsg]);
  1662. }
  1663. }
  1664. #pragma unroll(r1ptg)
  1665. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  1666. y4[ir1] += chpt*nxpsg;
  1667. }
  1668. }
  1669. // reduce only the threads in each row
  1670. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  1671. if (nxpsg >= 32) {
  1672. sumf[ir1] += simd_shuffle_down(sumf[ir1], 16);
  1673. }
  1674. if (nxpsg >= 16) {
  1675. sumf[ir1] += simd_shuffle_down(sumf[ir1], 8);
  1676. }
  1677. if (nxpsg >= 8) {
  1678. sumf[ir1] += simd_shuffle_down(sumf[ir1], 4);
  1679. }
  1680. if (nxpsg >= 4) {
  1681. sumf[ir1] += simd_shuffle_down(sumf[ir1], 2);
  1682. }
  1683. if (nxpsg >= 2) {
  1684. sumf[ir1] += simd_shuffle_down(sumf[ir1], 1);
  1685. }
  1686. //sumf[ir1] = simd_sum(sumf[ir1]);
  1687. }
  1688. if (tx == 0) {
  1689. for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) {
  1690. device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0;
  1691. if (i01 < args.ne01) {
  1692. dst_f32[i01] = sumf[ir1];
  1693. }
  1694. }
  1695. }
  1696. }
  1697. // mat-vec kernel processing in chunks of float4x4
  1698. template<short nxpsg, short r1ptg, typename q_t, short chpb, void (*deq_t4x4)(device const q_t *, short, thread float4x4 &) >
  1699. void kernel_mul_mv_ext_q4x4_f32_impl(
  1700. constant ggml_metal_kargs_mul_mv_ext & args,
  1701. device const char * src0,
  1702. device const char * src1,
  1703. device char * dst,
  1704. uint3 tgpig[[threadgroup_position_in_grid]],
  1705. ushort tiisg[[thread_index_in_simdgroup]],
  1706. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1707. const short chpt = 1;
  1708. //const short nxpsg = (32);
  1709. const short nypsg = (32/nxpsg);
  1710. const short tx = tiisg%nxpsg;
  1711. const short ty = tiisg/nxpsg;
  1712. const int i01 = tgpig.x*(nypsg*args.nsg) + nypsg*sgitg + ty;
  1713. const int i11 = tgpig.y*r1ptg;
  1714. const int i1m = tgpig.z;
  1715. const int i12 = i1m%args.ne12;
  1716. const int i13 = i1m/args.ne12;
  1717. const uint64_t offset0 = i01*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1718. const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  1719. device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0;
  1720. device const float4x4 * y4x4[r1ptg];
  1721. for (int ir1 = 0; ir1 < r1ptg; ++ir1) {
  1722. y4x4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4x4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4x4 *) src1;
  1723. }
  1724. float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f };
  1725. short cch = tx%chpb;
  1726. for (int ich = tx; 16*ich < args.ne00; ich += chpt*nxpsg) {
  1727. float4x4 lx[chpt];
  1728. #pragma unroll(chpt)
  1729. for (short ch = 0; ch < chpt; ++ch) {
  1730. deq_t4x4(xq, cch, lx[ch]);
  1731. cch += nxpsg;
  1732. if (cch >= chpb) {
  1733. xq += cch/chpb;
  1734. cch %= chpb;
  1735. }
  1736. }
  1737. #pragma unroll(chpt)
  1738. for (short ch = 0; ch < chpt; ++ch) {
  1739. #pragma unroll(r1ptg)
  1740. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  1741. sumf[ir1] +=
  1742. dot(lx[ch][0], y4x4[ir1][ch*nxpsg][0]) +
  1743. dot(lx[ch][1], y4x4[ir1][ch*nxpsg][1]) +
  1744. dot(lx[ch][2], y4x4[ir1][ch*nxpsg][2]) +
  1745. dot(lx[ch][3], y4x4[ir1][ch*nxpsg][3]);
  1746. }
  1747. }
  1748. #pragma unroll(r1ptg)
  1749. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  1750. y4x4[ir1] += chpt*nxpsg;
  1751. }
  1752. }
  1753. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  1754. if (nxpsg >= 32) {
  1755. sumf[ir1] += simd_shuffle_down(sumf[ir1], 16);
  1756. }
  1757. if (nxpsg >= 16) {
  1758. sumf[ir1] += simd_shuffle_down(sumf[ir1], 8);
  1759. }
  1760. if (nxpsg >= 8) {
  1761. sumf[ir1] += simd_shuffle_down(sumf[ir1], 4);
  1762. }
  1763. if (nxpsg >= 4) {
  1764. sumf[ir1] += simd_shuffle_down(sumf[ir1], 2);
  1765. }
  1766. if (nxpsg >= 2) {
  1767. sumf[ir1] += simd_shuffle_down(sumf[ir1], 1);
  1768. }
  1769. //sumf[ir1] = simd_sum(sumf[ir1]);
  1770. }
  1771. if (tx == 0) {
  1772. for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) {
  1773. device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0;
  1774. if (i01 < args.ne01) {
  1775. dst_f32[i01] = sumf[ir1];
  1776. }
  1777. }
  1778. }
  1779. }
  1780. // dispatchers needed for compile-time nxpsg
  1781. // epb - elements per quantization block
  1782. template<short r1ptg, typename q_t, short epb, void (*deq_t4)(device const q_t *, short, thread float4 &)>
  1783. kernel void kernel_mul_mv_ext_q4_f32_disp(
  1784. constant ggml_metal_kargs_mul_mv_ext & args,
  1785. device const char * src0,
  1786. device const char * src1,
  1787. device char * dst,
  1788. uint3 tgpig[[threadgroup_position_in_grid]],
  1789. ushort tiisg[[thread_index_in_simdgroup]],
  1790. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1791. switch (args.nxpsg) {
  1792. case 4: kernel_mul_mv_ext_q4_f32_impl<4, r1ptg, q_t, epb/4, deq_t4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1793. case 8: kernel_mul_mv_ext_q4_f32_impl<8, r1ptg, q_t, epb/4, deq_t4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1794. case 16: kernel_mul_mv_ext_q4_f32_impl<16, r1ptg, q_t, epb/4, deq_t4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1795. case 32: kernel_mul_mv_ext_q4_f32_impl<32, r1ptg, q_t, epb/4, deq_t4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1796. }
  1797. }
  1798. template<short r1ptg, typename q_t, short epb, void (*deq_t4x4)(device const q_t *, short, thread float4x4 &)>
  1799. kernel void kernel_mul_mv_ext_q4x4_f32_disp(
  1800. constant ggml_metal_kargs_mul_mv_ext & args,
  1801. device const char * src0,
  1802. device const char * src1,
  1803. device char * dst,
  1804. uint3 tgpig[[threadgroup_position_in_grid]],
  1805. ushort tiisg[[thread_index_in_simdgroup]],
  1806. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1807. switch (args.nxpsg) {
  1808. case 4: kernel_mul_mv_ext_q4x4_f32_impl<4, r1ptg, q_t, epb/16, deq_t4x4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1809. case 8: kernel_mul_mv_ext_q4x4_f32_impl<8, r1ptg, q_t, epb/16, deq_t4x4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1810. case 16: kernel_mul_mv_ext_q4x4_f32_impl<16, r1ptg, q_t, epb/16, deq_t4x4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1811. case 32: kernel_mul_mv_ext_q4x4_f32_impl<32, r1ptg, q_t, epb/16, deq_t4x4>(args, src0, src1, dst, tgpig, tiisg, sgitg); break;
  1812. }
  1813. }
  1814. typedef decltype(kernel_mul_mv_ext_q4_f32_disp <2, block_q8_0, 32, dequantize_q8_0_t4>) mul_mv_ext_q4_f32_t;
  1815. typedef decltype(kernel_mul_mv_ext_q4x4_f32_disp<2, block_q4_K, 256, dequantize_q4_K>) mul_mv_ext_q4x4_f32_t;
  1816. template [[host_name("kernel_mul_mv_ext_f16_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, half4, 4, dequantize_f16_t4>;
  1817. template [[host_name("kernel_mul_mv_ext_f16_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, half4, 4, dequantize_f16_t4>;
  1818. template [[host_name("kernel_mul_mv_ext_f16_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, half4, 4, dequantize_f16_t4>;
  1819. template [[host_name("kernel_mul_mv_ext_f16_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, half4, 4, dequantize_f16_t4>;
  1820. template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q4_0, 32, dequantize_q4_0_t4>;
  1821. template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q4_0, 32, dequantize_q4_0_t4>;
  1822. template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q4_0, 32, dequantize_q4_0_t4>;
  1823. template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q4_0, 32, dequantize_q4_0_t4>;
  1824. template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q4_1, 32, dequantize_q4_1_t4>;
  1825. template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q4_1, 32, dequantize_q4_1_t4>;
  1826. template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q4_1, 32, dequantize_q4_1_t4>;
  1827. template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q4_1, 32, dequantize_q4_1_t4>;
  1828. template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q5_0, 32, dequantize_q5_0_t4>;
  1829. template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q5_0, 32, dequantize_q5_0_t4>;
  1830. template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q5_0, 32, dequantize_q5_0_t4>;
  1831. template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q5_0, 32, dequantize_q5_0_t4>;
  1832. template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q5_1, 32, dequantize_q5_1_t4>;
  1833. template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q5_1, 32, dequantize_q5_1_t4>;
  1834. template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q5_1, 32, dequantize_q5_1_t4>;
  1835. template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q5_1, 32, dequantize_q5_1_t4>;
  1836. template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q8_0, 32, dequantize_q8_0_t4>;
  1837. template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q8_0, 32, dequantize_q8_0_t4>;
  1838. template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q8_0, 32, dequantize_q8_0_t4>;
  1839. template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q8_0, 32, dequantize_q8_0_t4>;
  1840. template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_iq4_nl, 32, dequantize_iq4_nl_t4>;
  1841. template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_iq4_nl, 32, dequantize_iq4_nl_t4>;
  1842. template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_iq4_nl, 32, dequantize_iq4_nl_t4>;
  1843. template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_iq4_nl, 32, dequantize_iq4_nl_t4>;
  1844. template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q4_K, 256, dequantize_q4_K>;
  1845. template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q4_K, 256, dequantize_q4_K>;
  1846. template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q4_K, 256, dequantize_q4_K>;
  1847. template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q4_K, 256, dequantize_q4_K>;
  1848. template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q5_K, 256, dequantize_q5_K>;
  1849. template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q5_K, 256, dequantize_q5_K>;
  1850. template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q5_K, 256, dequantize_q5_K>;
  1851. template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q5_K, 256, dequantize_q5_K>;
  1852. template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q6_K, 256, dequantize_q6_K>;
  1853. template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q6_K, 256, dequantize_q6_K>;
  1854. template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q6_K, 256, dequantize_q6_K>;
  1855. template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q6_K, 256, dequantize_q6_K>;
  1856. #define N_MV_T_T 4
  1857. template<typename T0, typename T04, typename T1, typename T14, typename args_t>
  1858. void kernel_mul_mv_impl(
  1859. args_t args,
  1860. device const char * src0,
  1861. device const char * src1,
  1862. device char * dst,
  1863. uint3 tgpig,
  1864. ushort tiisg) {
  1865. const int r0 = tgpig.x;
  1866. const int rb = tgpig.y*N_MV_T_T;
  1867. const int im = tgpig.z;
  1868. const uint i12 = im%args.ne12;
  1869. const uint i13 = im/args.ne12;
  1870. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1871. device const T0 * x = (device const T0 *) (src0 + offset0);
  1872. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1;
  1873. if (args.ne00 < 128) {
  1874. for (int row = 0; row < N_MV_T_T; ++row) {
  1875. int r1 = rb + row;
  1876. if (r1 >= args.ne11) {
  1877. break;
  1878. }
  1879. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  1880. device const T1 * y = (device const T1 *) (src1 + offset1);
  1881. float sumf = 0;
  1882. for (int i = tiisg; i < args.ne00; i += 32) {
  1883. sumf += (T0) x[i] * (T1) y[i];
  1884. }
  1885. float all_sum = simd_sum(sumf);
  1886. if (tiisg == 0) {
  1887. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  1888. }
  1889. }
  1890. } else {
  1891. device const T04 * x4 = (device const T04 *) x;
  1892. for (int row = 0; row < N_MV_T_T; ++row) {
  1893. int r1 = rb + row;
  1894. if (r1 >= args.ne11) {
  1895. break;
  1896. }
  1897. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  1898. device const T1 * y = (device const T1 *) (src1 + offset1);
  1899. device const T14 * y4 = (device const T14 *) y;
  1900. float sumf = 0;
  1901. for (int i = tiisg; i < args.ne00/4; i += 32) {
  1902. sumf += dot((float4) x4[i], (float4) y4[i]);
  1903. }
  1904. float all_sum = simd_sum(sumf);
  1905. if (tiisg == 0) {
  1906. for (int i = 4*(args.ne00/4); i < args.ne00; ++i) all_sum += (float) (x[i] * y[i]);
  1907. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  1908. }
  1909. }
  1910. }
  1911. }
  1912. template<typename T0, typename T04, typename T1, typename T14>
  1913. kernel void kernel_mul_mv(
  1914. constant ggml_metal_kargs_mul_mv & args,
  1915. device const char * src0,
  1916. device const char * src1,
  1917. device char * dst,
  1918. uint3 tgpig[[threadgroup_position_in_grid]],
  1919. ushort tiisg[[thread_index_in_simdgroup]]) {
  1920. kernel_mul_mv_impl<T0, T04, T1, T14, constant ggml_metal_kargs_mul_mv &>(
  1921. args,
  1922. src0,
  1923. src1,
  1924. dst,
  1925. tgpig,
  1926. tiisg);
  1927. }
  1928. typedef decltype(kernel_mul_mv<half, half4, half, half4>) mul_mv_t;
  1929. template [[host_name("kernel_mul_mv_f32_f32")]] kernel mul_mv_t kernel_mul_mv<float, float4, float, float4>;
  1930. template [[host_name("kernel_mul_mv_f16_f32")]] kernel mul_mv_t kernel_mul_mv<half, half4, float, float4>;
  1931. template [[host_name("kernel_mul_mv_f16_f16")]] kernel mul_mv_t kernel_mul_mv<half, half4, half, half4>;
  1932. #if defined(GGML_METAL_USE_BF16)
  1933. template [[host_name("kernel_mul_mv_bf16_f32")]] kernel mul_mv_t kernel_mul_mv<bfloat, bfloat4, float, float4>;
  1934. template [[host_name("kernel_mul_mv_bf16_bf16")]] kernel mul_mv_t kernel_mul_mv<bfloat, bfloat4, bfloat, bfloat4>;
  1935. #endif
  1936. template<typename T, typename T4>
  1937. kernel void kernel_mul_mv_1row(
  1938. constant ggml_metal_kargs_mul_mv & args,
  1939. device const char * src0,
  1940. device const char * src1,
  1941. device char * dst,
  1942. uint3 tgpig[[threadgroup_position_in_grid]],
  1943. ushort tiisg[[thread_index_in_simdgroup]]) {
  1944. const int r0 = tgpig.x;
  1945. const int r1 = tgpig.y;
  1946. const int im = tgpig.z;
  1947. const uint i12 = im%args.ne12;
  1948. const uint i13 = im/args.ne12;
  1949. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1950. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  1951. device const T * x = (device const T *) (src0 + offset0);
  1952. device const float * y = (device const float *) (src1 + offset1);
  1953. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  1954. float sumf = 0;
  1955. if (args.ne00 < 128) {
  1956. for (int i = tiisg; i < args.ne00; i += 32) {
  1957. sumf += (float) x[i] * (float) y[i];
  1958. }
  1959. float all_sum = simd_sum(sumf);
  1960. if (tiisg == 0) {
  1961. dst_f32[r0] = all_sum;
  1962. }
  1963. } else {
  1964. device const T4 * x4 = (device const T4 *) x;
  1965. device const float4 * y4 = (device const float4 *) y;
  1966. for (int i = tiisg; i < args.ne00/4; i += 32) {
  1967. sumf += dot((float4) x4[i], y4[i]);
  1968. }
  1969. float all_sum = simd_sum(sumf);
  1970. if (tiisg == 0) {
  1971. for (int i = 4*(args.ne00/4); i < args.ne00; ++i) all_sum += (float) (x[i] * y[i]);
  1972. dst_f32[r0] = all_sum;
  1973. }
  1974. }
  1975. }
  1976. typedef decltype(kernel_mul_mv_1row<half, half4>) mul_mv_1row_t;
  1977. template [[host_name("kernel_mul_mv_f16_f32_1row")]] kernel mul_mv_1row_t kernel_mul_mv_1row<half, half4>;
  1978. #if defined(GGML_METAL_USE_BF16)
  1979. template [[host_name("kernel_mul_mv_bf16_f32_1row")]] kernel mul_mv_1row_t kernel_mul_mv_1row<bfloat, bfloat4>;
  1980. #endif
  1981. // Assumes row size (ne00) is a multiple of 4
  1982. template<typename T, typename T4>
  1983. kernel void kernel_mul_mv_l4(
  1984. constant ggml_metal_kargs_mul_mv & args,
  1985. device const char * src0,
  1986. device const char * src1,
  1987. device char * dst,
  1988. uint3 tgpig[[threadgroup_position_in_grid]],
  1989. ushort tiisg[[thread_index_in_simdgroup]]) {
  1990. const int nrows = args.ne11;
  1991. const int r0 = tgpig.x;
  1992. const int im = tgpig.z;
  1993. const uint i12 = im%args.ne12;
  1994. const uint i13 = im/args.ne12;
  1995. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  1996. device const T4 * x4 = (device const T4 *) (src0 + offset0);
  1997. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1;
  1998. for (int r1 = 0; r1 < nrows; ++r1) {
  1999. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  2000. device const float4 * y4 = (device const float4 *) (src1 + offset1);
  2001. float sumf = 0;
  2002. for (int i = tiisg; i < args.ne00/4; i += 32) {
  2003. sumf += dot((float4) x4[i], y4[i]);
  2004. }
  2005. float all_sum = simd_sum(sumf);
  2006. if (tiisg == 0) {
  2007. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  2008. }
  2009. }
  2010. }
  2011. typedef decltype(kernel_mul_mv_l4<half, half4>) mul_mv_l4_t;
  2012. template [[host_name("kernel_mul_mv_f16_f32_l4")]] kernel mul_mv_l4_t kernel_mul_mv_l4<half, half4>;
  2013. #if defined(GGML_METAL_USE_BF16)
  2014. template [[host_name("kernel_mul_mv_bf16_f32_l4")]] kernel mul_mv_l4_t kernel_mul_mv_l4<bfloat, bfloat4>;
  2015. #endif
  2016. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  2017. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  2018. return 1.0f - min(1.0f, max(0.0f, y));
  2019. }
  2020. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  2021. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  2022. static void rope_yarn(
  2023. float theta_extrap, float freq_scale, float corr_dims[2], int i0, float ext_factor, float mscale,
  2024. thread float * cos_theta, thread float * sin_theta) {
  2025. // Get n-d rotational scaling corrected for extrapolation
  2026. float theta_interp = freq_scale * theta_extrap;
  2027. float theta = theta_interp;
  2028. if (ext_factor != 0.0f) {
  2029. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  2030. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  2031. // Get n-d magnitude scaling corrected for interpolation
  2032. mscale *= 1.0f + 0.1f * log(1.0f / freq_scale);
  2033. }
  2034. *cos_theta = cos(theta) * mscale;
  2035. *sin_theta = sin(theta) * mscale;
  2036. }
  2037. // Apparently solving `n_rot = 2pi * x * base^((2 * max_pos_emb) / n_dims)` for x, we get
  2038. // `corr_fac(n_rot) = n_dims * log(max_pos_emb / (n_rot * 2pi)) / (2 * log(base))`
  2039. static float rope_yarn_corr_factor(int n_dims, int n_ctx_orig, float n_rot, float base) {
  2040. return n_dims * log(n_ctx_orig / (n_rot * 2 * M_PI_F)) / (2 * log(base));
  2041. }
  2042. static void rope_yarn_corr_dims(
  2043. int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]
  2044. ) {
  2045. // start and end correction dims
  2046. dims[0] = max(0.0f, floor(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_fast, freq_base)));
  2047. dims[1] = min(n_dims - 1.0f, ceil(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_slow, freq_base)));
  2048. }
  2049. template<typename T>
  2050. kernel void kernel_rope_norm(
  2051. constant ggml_metal_kargs_rope & args,
  2052. device const char * src0,
  2053. device const char * src1,
  2054. device const char * src2,
  2055. device char * dst,
  2056. ushort tiitg[[thread_index_in_threadgroup]],
  2057. ushort3 tptg [[threads_per_threadgroup]],
  2058. uint3 tgpig[[threadgroup_position_in_grid]]) {
  2059. const int i3 = tgpig[2];
  2060. const int i2 = tgpig[1];
  2061. const int i1 = tgpig[0];
  2062. float corr_dims[2];
  2063. rope_yarn_corr_dims(args.n_dims, args.n_ctx_orig, args.freq_base, args.beta_fast, args.beta_slow, corr_dims);
  2064. device const int32_t * pos = (device const int32_t *) src1;
  2065. const float theta_base = (float) pos[i2];
  2066. const float inv_ndims = -1.f/args.n_dims;
  2067. float cos_theta;
  2068. float sin_theta;
  2069. for (int i0 = 2*tiitg; i0 < args.ne0; i0 += 2*tptg.x) {
  2070. if (i0 < args.n_dims) {
  2071. const int ic = i0/2;
  2072. const float theta = theta_base * pow(args.freq_base, inv_ndims*i0);
  2073. const float freq_factor = src2 != src0 ? ((device const float *) src2)[ic] : 1.0f;
  2074. rope_yarn(theta/freq_factor, args.freq_scale, corr_dims, i0, args.ext_factor, args.attn_factor, &cos_theta, &sin_theta);
  2075. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  2076. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  2077. const float x0 = src[0];
  2078. const float x1 = src[1];
  2079. dst_data[0] = x0*cos_theta - x1*sin_theta;
  2080. dst_data[1] = x0*sin_theta + x1*cos_theta;
  2081. } else {
  2082. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  2083. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  2084. dst_data[0] = src[0];
  2085. dst_data[1] = src[1];
  2086. }
  2087. }
  2088. }
  2089. template<typename T>
  2090. kernel void kernel_rope_neox(
  2091. constant ggml_metal_kargs_rope & args,
  2092. device const char * src0,
  2093. device const char * src1,
  2094. device const char * src2,
  2095. device char * dst,
  2096. ushort tiitg[[thread_index_in_threadgroup]],
  2097. ushort3 tptg [[threads_per_threadgroup]],
  2098. uint3 tgpig[[threadgroup_position_in_grid]]) {
  2099. const int i3 = tgpig[2];
  2100. const int i2 = tgpig[1];
  2101. const int i1 = tgpig[0];
  2102. float corr_dims[2];
  2103. rope_yarn_corr_dims(args.n_dims, args.n_ctx_orig, args.freq_base, args.beta_fast, args.beta_slow, corr_dims);
  2104. device const int32_t * pos = (device const int32_t *) src1;
  2105. const float theta_base = (float) pos[i2];
  2106. const float inv_ndims = -1.f/args.n_dims;
  2107. float cos_theta;
  2108. float sin_theta;
  2109. for (int i0 = 2*tiitg; i0 < args.ne0; i0 += 2*tptg.x) {
  2110. if (i0 < args.n_dims) {
  2111. const int ic = i0/2;
  2112. const float theta = theta_base * pow(args.freq_base, inv_ndims*i0);
  2113. const float freq_factor = src2 != src0 ? ((device const float *) src2)[ic] : 1.0f;
  2114. rope_yarn(theta/freq_factor, args.freq_scale, corr_dims, i0, args.ext_factor, args.attn_factor, &cos_theta, &sin_theta);
  2115. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + ic*args.nb00);
  2116. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + ic*args.nb0);
  2117. const float x0 = src[0];
  2118. const float x1 = src[args.n_dims/2];
  2119. dst_data[0] = x0*cos_theta - x1*sin_theta;
  2120. dst_data[args.n_dims/2] = x0*sin_theta + x1*cos_theta;
  2121. } else {
  2122. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  2123. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  2124. dst_data[0] = src[0];
  2125. dst_data[1] = src[1];
  2126. }
  2127. }
  2128. }
  2129. typedef decltype(kernel_rope_norm<float>) kernel_rope_norm_t;
  2130. typedef decltype(kernel_rope_neox<float>) kernel_rope_neox_t;
  2131. template [[host_name("kernel_rope_norm_f32")]] kernel kernel_rope_norm_t kernel_rope_norm<float>;
  2132. template [[host_name("kernel_rope_norm_f16")]] kernel kernel_rope_norm_t kernel_rope_norm<half>;
  2133. template [[host_name("kernel_rope_neox_f32")]] kernel kernel_rope_neox_t kernel_rope_neox<float>;
  2134. template [[host_name("kernel_rope_neox_f16")]] kernel kernel_rope_neox_t kernel_rope_neox<half>;
  2135. typedef void (im2col_t)(
  2136. device const float * x,
  2137. device char * dst,
  2138. constant int32_t & ofs0,
  2139. constant int32_t & ofs1,
  2140. constant int32_t & IW,
  2141. constant int32_t & IH,
  2142. constant int32_t & CHW,
  2143. constant int32_t & s0,
  2144. constant int32_t & s1,
  2145. constant int32_t & p0,
  2146. constant int32_t & p1,
  2147. constant int32_t & d0,
  2148. constant int32_t & d1,
  2149. uint3 tgpig[[threadgroup_position_in_grid]],
  2150. uint3 tgpg[[threadgroups_per_grid]],
  2151. uint3 tpitg[[thread_position_in_threadgroup]],
  2152. uint3 ntg[[threads_per_threadgroup]]);
  2153. template <typename T>
  2154. kernel void kernel_im2col(
  2155. device const float * x,
  2156. device char * dst,
  2157. constant int32_t & ofs0,
  2158. constant int32_t & ofs1,
  2159. constant int32_t & IW,
  2160. constant int32_t & IH,
  2161. constant int32_t & CHW,
  2162. constant int32_t & s0,
  2163. constant int32_t & s1,
  2164. constant int32_t & p0,
  2165. constant int32_t & p1,
  2166. constant int32_t & d0,
  2167. constant int32_t & d1,
  2168. uint3 tgpig[[threadgroup_position_in_grid]],
  2169. uint3 tgpg[[threadgroups_per_grid]],
  2170. uint3 tpitg[[thread_position_in_threadgroup]],
  2171. uint3 ntg[[threads_per_threadgroup]]) {
  2172. // const int64_t IC = tgpg[0];
  2173. const int64_t OH = tgpg[1];
  2174. const int64_t OW = tgpg[2];
  2175. // const int64_t N = ntg[0];
  2176. const int64_t KH = ntg[1];
  2177. const int64_t KW = ntg[2];
  2178. const int64_t in = tpitg[0];
  2179. const int64_t ikh = tpitg[1];
  2180. const int64_t ikw = tpitg[2];
  2181. const int64_t iic = tgpig[0];
  2182. const int64_t ioh = tgpig[1];
  2183. const int64_t iow = tgpig[2];
  2184. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  2185. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  2186. const int64_t offset_dst = (in*OH*OW + ioh*OW + iow)*CHW + (iic*(KH*KW) + ikh*KW + ikw);
  2187. device T * pdst = (device T *) (dst);
  2188. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  2189. pdst[offset_dst] = 0.0f;
  2190. } else {
  2191. const int64_t offset_src = in*ofs0 + iic*ofs1 + iih*IW + iiw;
  2192. pdst[offset_dst] = x[offset_src];
  2193. }
  2194. }
  2195. template [[host_name("kernel_im2col_f32")]] kernel im2col_t kernel_im2col<float>;
  2196. template [[host_name("kernel_im2col_f16")]] kernel im2col_t kernel_im2col<half>;
  2197. typedef void (im2col_ext_t)(
  2198. device const float * x,
  2199. device char * dst,
  2200. constant int32_t & ofs0,
  2201. constant int32_t & ofs1,
  2202. constant int32_t & IW,
  2203. constant int32_t & IH,
  2204. constant int32_t & CHW,
  2205. constant int32_t & s0,
  2206. constant int32_t & s1,
  2207. constant int32_t & p0,
  2208. constant int32_t & p1,
  2209. constant int32_t & d0,
  2210. constant int32_t & d1,
  2211. constant int32_t & N,
  2212. constant int32_t & KH,
  2213. constant int32_t & KW,
  2214. uint3 tgpig[[threadgroup_position_in_grid]],
  2215. uint3 tgpg[[threadgroups_per_grid]],
  2216. uint3 tpitg[[thread_position_in_threadgroup]],
  2217. uint3 ntg[[threads_per_threadgroup]]);
  2218. template <typename T>
  2219. kernel void kernel_im2col_ext(
  2220. device const float * x,
  2221. device char * dst,
  2222. constant int32_t & ofs0,
  2223. constant int32_t & ofs1,
  2224. constant int32_t & IW,
  2225. constant int32_t & IH,
  2226. constant int32_t & CHW,
  2227. constant int32_t & s0,
  2228. constant int32_t & s1,
  2229. constant int32_t & p0,
  2230. constant int32_t & p1,
  2231. constant int32_t & d0,
  2232. constant int32_t & d1,
  2233. constant int32_t & N,
  2234. constant int32_t & KH,
  2235. constant int32_t & KW,
  2236. uint3 tgpig[[threadgroup_position_in_grid]],
  2237. uint3 tgpg[[threadgroups_per_grid]], // tgpg[0] = D x IC x KH x KW, CHW = IC x KH x KW
  2238. uint3 tpitg[[thread_position_in_threadgroup]],
  2239. uint3 ntg[[threads_per_threadgroup]]) { // [M, 1, 1]
  2240. const int64_t KHW = KH * KW; // KHW == ntg[1] * ntg[2], KW == ntg[2]
  2241. const int64_t d = tgpig[0] / CHW;
  2242. const int64_t chw = tgpig[0] % CHW;
  2243. const int64_t tgpig_0 = chw / KHW; // 0 ~ (IC - 1)
  2244. const int64_t HW = tgpig[0] % KHW;
  2245. const int64_t tpitg_0 = (d * ntg[0]) + tpitg[0];
  2246. if (tpitg_0 >= N) {
  2247. return;
  2248. }
  2249. const int64_t tpitg_1 = HW / KW;
  2250. const int64_t tpitg_2 = HW % KW;
  2251. const int64_t iiw = tgpig[2] * s0 + tpitg_2 * d0 - p0;
  2252. const int64_t iih = tgpig[1] * s1 + tpitg_1 * d1 - p1;
  2253. const int64_t offset_dst =
  2254. (tpitg_0 * tgpg[1] * tgpg[2] + tgpig[1] * tgpg[2] + tgpig[2]) * CHW +
  2255. (tgpig_0 * KHW + tpitg_1 * KW + tpitg_2);
  2256. device T * pdst = (device T *) (dst);
  2257. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  2258. pdst[offset_dst] = 0.0f;
  2259. } else {
  2260. const int64_t offset_src = tpitg_0 * ofs0 + tgpig_0 * ofs1;
  2261. pdst[offset_dst] = x[offset_src + iih * IW + iiw];
  2262. }
  2263. }
  2264. template [[host_name("kernel_im2col_ext_f32")]] kernel im2col_ext_t kernel_im2col_ext<float>;
  2265. template [[host_name("kernel_im2col_ext_f16")]] kernel im2col_ext_t kernel_im2col_ext<half>;
  2266. typedef void (conv_transpose_1d_t)(
  2267. device const float * src0,
  2268. device const float * src1,
  2269. device char * dst,
  2270. constant int32_t & IC,
  2271. constant int32_t & IL,
  2272. constant int32_t & K,
  2273. constant int32_t & s0,
  2274. constant uint64_t & nb0,
  2275. constant uint64_t & nb1,
  2276. uint3 tgpig[[threadgroup_position_in_grid]],
  2277. uint3 tgpg[[threadgroups_per_grid]]);
  2278. template <typename T>
  2279. kernel void kernel_conv_transpose_1d(
  2280. device const T * src0,
  2281. device const float * src1,
  2282. device char * dst,
  2283. constant int32_t & IC,
  2284. constant int32_t & IL,
  2285. constant int32_t & K,
  2286. constant int32_t & s0,
  2287. constant uint64_t & nb0,
  2288. constant uint64_t & nb1,
  2289. uint3 tgpig[[threadgroup_position_in_grid]],
  2290. uint3 tgpg[[threadgroups_per_grid]]) {
  2291. float v = 0.0f;
  2292. for (int64_t c = 0; c < IC; c++) {
  2293. const int32_t kernel_offset = c * tgpg[1] * K + K * tgpig[1];
  2294. const int32_t input_offset = c * IL;
  2295. for (int64_t i = 0; i < IL; i++) {
  2296. if (tgpig[0] >= i * s0 && tgpig[0] < i * s0 + K) {
  2297. v += src0[kernel_offset + tgpig[0] - i * s0] * src1[input_offset + i];
  2298. }
  2299. }
  2300. }
  2301. device float * dst_ptr = (device float *) (dst + tgpig[0] * nb0 + tgpig[1] * nb1);
  2302. dst_ptr[0] = v;
  2303. }
  2304. template [[host_name("kernel_conv_transpose_1d_f32_f32")]]
  2305. kernel void kernel_conv_transpose_1d<float>(
  2306. device const float * src0,
  2307. device const float * src1,
  2308. device char * dst,
  2309. constant int32_t & IC,
  2310. constant int32_t & IL,
  2311. constant int32_t & K,
  2312. constant int32_t & s0,
  2313. constant uint64_t & nb0,
  2314. constant uint64_t & nb1,
  2315. uint3 tgpig[[threadgroup_position_in_grid]],
  2316. uint3 tgpg[[threadgroups_per_grid]]);
  2317. template [[host_name("kernel_conv_transpose_1d_f16_f32")]]
  2318. kernel void kernel_conv_transpose_1d<half>(
  2319. device const half * src0,
  2320. device const float * src1,
  2321. device char * dst,
  2322. constant int32_t & IC,
  2323. constant int32_t & IL,
  2324. constant int32_t & K,
  2325. constant int32_t & s0,
  2326. constant uint64_t & nb0,
  2327. constant uint64_t & nb1,
  2328. uint3 tgpig[[threadgroup_position_in_grid]],
  2329. uint3 tgpg[[threadgroups_per_grid]]);
  2330. kernel void kernel_upscale_f32(
  2331. device const char * src0,
  2332. device char * dst,
  2333. constant int64_t & ne00,
  2334. constant int64_t & ne01,
  2335. constant int64_t & ne02,
  2336. constant int64_t & ne03,
  2337. constant uint64_t & nb00,
  2338. constant uint64_t & nb01,
  2339. constant uint64_t & nb02,
  2340. constant uint64_t & nb03,
  2341. constant int64_t & ne0,
  2342. constant int64_t & ne1,
  2343. constant int64_t & ne2,
  2344. constant int64_t & ne3,
  2345. constant uint64_t & nb0,
  2346. constant uint64_t & nb1,
  2347. constant uint64_t & nb2,
  2348. constant uint64_t & nb3,
  2349. constant float & sf0,
  2350. constant float & sf1,
  2351. constant float & sf2,
  2352. constant float & sf3,
  2353. uint3 tgpig[[threadgroup_position_in_grid]],
  2354. uint3 tpitg[[thread_position_in_threadgroup]],
  2355. uint3 ntg[[threads_per_threadgroup]]) {
  2356. const int64_t i3 = tgpig.z;
  2357. const int64_t i2 = tgpig.y;
  2358. const int64_t i1 = tgpig.x;
  2359. const int64_t i03 = i3/sf3;
  2360. const int64_t i02 = i2/sf2;
  2361. const int64_t i01 = i1/sf1;
  2362. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2363. const int64_t i00 = i0/sf0;
  2364. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2365. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2366. dst_ptr[0] = src0_ptr[0];
  2367. }
  2368. }
  2369. kernel void kernel_pad_f32(
  2370. device const char * src0,
  2371. device char * dst,
  2372. constant int64_t & ne00,
  2373. constant int64_t & ne01,
  2374. constant int64_t & ne02,
  2375. constant int64_t & ne03,
  2376. constant uint64_t & nb00,
  2377. constant uint64_t & nb01,
  2378. constant uint64_t & nb02,
  2379. constant uint64_t & nb03,
  2380. constant int64_t & ne0,
  2381. constant int64_t & ne1,
  2382. constant int64_t & ne2,
  2383. constant int64_t & ne3,
  2384. constant uint64_t & nb0,
  2385. constant uint64_t & nb1,
  2386. constant uint64_t & nb2,
  2387. constant uint64_t & nb3,
  2388. uint3 tgpig[[threadgroup_position_in_grid]],
  2389. uint3 tpitg[[thread_position_in_threadgroup]],
  2390. uint3 ntg[[threads_per_threadgroup]]) {
  2391. const int64_t i3 = tgpig.z;
  2392. const int64_t i2 = tgpig.y;
  2393. const int64_t i1 = tgpig.x;
  2394. const int64_t i03 = i3;
  2395. const int64_t i02 = i2;
  2396. const int64_t i01 = i1;
  2397. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  2398. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  2399. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  2400. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2401. if (i0 < ne00) {
  2402. dst_ptr[i0] = src0_ptr[i0];
  2403. } else {
  2404. dst_ptr[i0] = 0.0f;
  2405. }
  2406. }
  2407. return;
  2408. }
  2409. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2410. dst_ptr[i0] = 0.0f;
  2411. }
  2412. }
  2413. kernel void kernel_pad_reflect_1d_f32(
  2414. device const char * src0,
  2415. device char * dst,
  2416. constant int64_t & ne00,
  2417. constant int64_t & ne01,
  2418. constant int64_t & ne02,
  2419. constant int64_t & ne03,
  2420. constant int64_t & ne0,
  2421. constant uint64_t & nb00,
  2422. constant uint64_t & nb01,
  2423. constant uint64_t & nb02,
  2424. constant uint64_t & nb03,
  2425. constant uint64_t & nb0,
  2426. constant uint64_t & nb1,
  2427. constant uint64_t & nb2,
  2428. constant uint64_t & nb3,
  2429. constant int32_t & p0,
  2430. constant int32_t & p1,
  2431. uint3 tgpig[[threadgroup_position_in_grid]],
  2432. uint3 tgpg[[threadgroups_per_grid]],
  2433. uint3 tpitg[[thread_position_in_threadgroup]],
  2434. uint3 ntg[[threads_per_threadgroup]]) {
  2435. const int64_t i3 = tgpig.z;
  2436. const int64_t i2 = tgpig.y;
  2437. const int64_t i1 = tgpig.x;
  2438. const int64_t i03 = i3;
  2439. const int64_t i02 = i2;
  2440. const int64_t i01 = i1;
  2441. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  2442. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  2443. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  2444. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2445. if (i0 < p0) {
  2446. dst_ptr[i0] = src0_ptr[p0 - i0];
  2447. } else if (i0 < ne0 - p1) {
  2448. dst_ptr[i0] = src0_ptr[i0 - p0];
  2449. } else {
  2450. dst_ptr[i0] = src0_ptr[(ne0 - p1 - p0) - (p1 + 1 - (ne0 - i0)) - 1];
  2451. }
  2452. }
  2453. }
  2454. }
  2455. kernel void kernel_unpad_f32(
  2456. device const char * src0,
  2457. device char * dst,
  2458. constant int64_t & ne00,
  2459. constant int64_t & ne01,
  2460. constant int64_t & ne02,
  2461. constant int64_t & ne03,
  2462. constant uint64_t & nb00,
  2463. constant uint64_t & nb01,
  2464. constant uint64_t & nb02,
  2465. constant uint64_t & nb03,
  2466. constant int64_t & ne0,
  2467. constant int64_t & ne1,
  2468. constant int64_t & ne2,
  2469. constant int64_t & ne3,
  2470. constant uint64_t & nb0,
  2471. constant uint64_t & nb1,
  2472. constant uint64_t & nb2,
  2473. constant uint64_t & nb3,
  2474. uint3 tgpig[[threadgroup_position_in_grid]],
  2475. uint3 tpitg[[thread_position_in_threadgroup]],
  2476. uint3 ntg[[threads_per_threadgroup]]) {
  2477. const int64_t i3 = tgpig.z;
  2478. const int64_t i2 = tgpig.y;
  2479. const int64_t i1 = tgpig.x;
  2480. const int64_t i03 = i3;
  2481. const int64_t i02 = i2;
  2482. const int64_t i01 = i1;
  2483. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  2484. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  2485. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  2486. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2487. if (i0 < ne00) {
  2488. dst_ptr[i0] = src0_ptr[i0];
  2489. }
  2490. }
  2491. return;
  2492. }
  2493. }
  2494. kernel void kernel_arange_f32(
  2495. device char * dst,
  2496. constant int64_t & ne0,
  2497. constant float & start,
  2498. constant float & step,
  2499. uint3 tgpig[[threadgroup_position_in_grid]],
  2500. uint3 tpitg[[thread_position_in_threadgroup]],
  2501. uint3 ntg[[threads_per_threadgroup]]) {
  2502. device float * dst_ptr = (device float *) dst;
  2503. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2504. dst_ptr[i0] = start + step * i0;
  2505. }
  2506. }
  2507. kernel void kernel_timestep_embedding_f32(
  2508. device const char * src0,
  2509. device char * dst,
  2510. constant uint64_t & nb1,
  2511. constant int & dim,
  2512. constant int & max_period,
  2513. uint3 tgpig[[threadgroup_position_in_grid]],
  2514. uint3 tpitg[[thread_position_in_threadgroup]],
  2515. uint3 ntg[[threads_per_threadgroup]]) {
  2516. int i = tgpig.x;
  2517. device float * embed_data = (device float *)(dst + i*nb1);
  2518. int half_ = dim / 2;
  2519. for (int j = tpitg.x; j < half_; j += ntg.x) {
  2520. float timestep = ((device float *)src0)[i];
  2521. float freq = (float)exp(-log((float)max_period) * j / half_);
  2522. float arg = timestep * freq;
  2523. embed_data[j ] = cos(arg);
  2524. embed_data[j + half_] = sin(arg);
  2525. }
  2526. if (dim % 2 != 0 && tpitg.x == 0) {
  2527. embed_data[dim] = 0.f;
  2528. }
  2529. }
  2530. // bitonic sort implementation following the CUDA kernels as reference
  2531. typedef void (argsort_t)(
  2532. device const float * x,
  2533. device int32_t * dst,
  2534. constant int64_t & ncols,
  2535. constant int64_t & ncols_pad,
  2536. threadgroup int32_t * shared_values [[threadgroup(0)]],
  2537. uint3 tgpig[[threadgroup_position_in_grid]],
  2538. uint3 tpitg[[thread_position_in_threadgroup]]);
  2539. template<ggml_sort_order order>
  2540. kernel void kernel_argsort_f32_i32(
  2541. device const float * x,
  2542. device int32_t * dst,
  2543. constant int64_t & ncols,
  2544. constant int64_t & ncols_pad,
  2545. threadgroup int32_t * shared_values [[threadgroup(0)]],
  2546. uint3 tgpig[[threadgroup_position_in_grid]],
  2547. uint3 tpitg[[thread_position_in_threadgroup]]) {
  2548. // bitonic sort
  2549. int col = tpitg[0];
  2550. int row = tgpig[1];
  2551. if (col >= ncols_pad) return;
  2552. device const float * x_row = x + row * ncols;
  2553. threadgroup int32_t * dst_row = shared_values;
  2554. // initialize indices
  2555. dst_row[col] = col;
  2556. threadgroup_barrier(mem_flags::mem_threadgroup);
  2557. for (int k = 2; k <= ncols_pad; k *= 2) {
  2558. for (int j = k / 2; j > 0; j /= 2) {
  2559. int ixj = col ^ j;
  2560. if (ixj > col) {
  2561. if ((col & k) == 0) {
  2562. if (dst_row[col] >= ncols ||
  2563. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  2564. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  2565. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  2566. ) {
  2567. SWAP(dst_row[col], dst_row[ixj]);
  2568. }
  2569. } else {
  2570. if (dst_row[ixj] >= ncols ||
  2571. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  2572. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  2573. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  2574. ) {
  2575. SWAP(dst_row[col], dst_row[ixj]);
  2576. }
  2577. }
  2578. }
  2579. threadgroup_barrier(mem_flags::mem_threadgroup);
  2580. }
  2581. }
  2582. // copy the result to dst without the padding
  2583. if (col < ncols) {
  2584. dst[row * ncols + col] = dst_row[col];
  2585. }
  2586. }
  2587. template [[host_name("kernel_argsort_f32_i32_asc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_ASC>;
  2588. template [[host_name("kernel_argsort_f32_i32_desc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_DESC>;
  2589. kernel void kernel_leaky_relu_f32(
  2590. device const float * src0,
  2591. device float * dst,
  2592. constant float & slope,
  2593. uint tpig[[thread_position_in_grid]]) {
  2594. dst[tpig] = src0[tpig] > 0.0f ? src0[tpig] : src0[tpig] * slope;
  2595. }
  2596. // ref: https://arxiv.org/pdf/2307.08691.pdf
  2597. template<
  2598. typename q_t, // query types in shared memory
  2599. typename q4_t,
  2600. typename q8x8_t,
  2601. typename k_t, // key types in shared memory
  2602. typename k4x4_t,
  2603. typename k8x8_t,
  2604. typename v_t, // value types in shared memory
  2605. typename v4x4_t,
  2606. typename v8x8_t,
  2607. typename qk_t, // Q*K types
  2608. typename qk8x8_t,
  2609. typename s_t, // soft-max types
  2610. typename s8x8_t,
  2611. typename o_t, // attention accumulation types
  2612. typename o4_t,
  2613. typename o8x8_t,
  2614. typename kd4x4_t, // key type in device memory
  2615. short nl_k,
  2616. void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &),
  2617. typename vd4x4_t, // key type in device memory
  2618. short nl_v,
  2619. void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &),
  2620. short D, // head size
  2621. short Q = 8, // queries per threadgroup
  2622. short KV = 8, // key/value processed per each simdgroup
  2623. short C = 32> // cache items per threadgroup
  2624. kernel void kernel_flash_attn_ext(
  2625. constant ggml_metal_kargs_flash_attn_ext & args,
  2626. device const char * q,
  2627. device const char * k,
  2628. device const char * v,
  2629. device const char * mask,
  2630. device char * dst,
  2631. threadgroup half * shmem_f16 [[threadgroup(0)]],
  2632. uint3 tgpig[[threadgroup_position_in_grid]],
  2633. ushort3 ntg[[threads_per_threadgroup]],
  2634. ushort tiisg[[thread_index_in_simdgroup]],
  2635. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  2636. const short nsg = ntg.y; // number of simdgroups
  2637. const int iq3 = tgpig[2];
  2638. const int iq2 = tgpig[1];
  2639. const int iq1 = tgpig[0]*Q;
  2640. const short D4 = D/4;
  2641. const short D8 = D/8;
  2642. const short D16 = D/16;
  2643. const short NW = N_SIMDWIDTH;
  2644. const short SH = (2*C + Q); // shared memory per simdgroup (s_t == float)
  2645. const short TS = nsg*SH; // shared memory size per query in (s_t == float)
  2646. const short T = D + 2*TS; // shared memory size per query in (half)
  2647. threadgroup q_t * sq = (threadgroup q_t *) (shmem_f16 + 0*D); // holds the query data
  2648. threadgroup q4_t * sq4 = (threadgroup q4_t *) (shmem_f16 + 0*D); // same as above but in q4_t
  2649. threadgroup o_t * so = (threadgroup o_t *) (shmem_f16 + 0*D); // reuse query data for accumulation
  2650. threadgroup o4_t * so4 = (threadgroup o4_t *) (shmem_f16 + 0*D); // same as above but in o4_t
  2651. threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + 2*sgitg*SH + Q*D); // scratch buffer for attention, mask and diagonal matrix
  2652. threadgroup k_t * sk = (threadgroup k_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // scratch buffer to load K in shared memory
  2653. threadgroup k4x4_t * sk4x4 = (threadgroup k4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // same as above but in k4x4_t
  2654. threadgroup v_t * sv = (threadgroup v_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // scratch buffer to load V in shared memory
  2655. threadgroup v4x4_t * sv4x4 = (threadgroup v4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // same as above but in v4x4_t
  2656. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  2657. o8x8_t lo[D8];
  2658. // load heads from Q to shared memory
  2659. for (short j = sgitg; j < Q; j += nsg) {
  2660. device const float4 * q4 = (device const float4 *) ((device const char *) q + ((iq1 + j)*args.nb01 + iq2*args.nb02 + iq3*args.nb03));
  2661. for (short i = tiisg; i < D4; i += NW) {
  2662. if (iq1 + j < args.ne01) {
  2663. sq4[j*D4 + i] = (q4_t) q4[i];
  2664. } else {
  2665. sq4[j*D4 + i] = (q4_t) 0.0f;
  2666. }
  2667. }
  2668. }
  2669. // zero out lo
  2670. for (short i = 0; i < D8; ++i) {
  2671. lo[i] = make_filled_simdgroup_matrix<o_t, 8>((o_t) 0.0f);
  2672. }
  2673. // zero out shared memory SH
  2674. for (short j = 0; j < Q; ++j) {
  2675. for (short i = tiisg; i < SH; i += NW) {
  2676. ss[j*TS + i] = 0.0f;
  2677. }
  2678. }
  2679. threadgroup_barrier(mem_flags::mem_threadgroup);
  2680. {
  2681. half S[Q] = { [0 ... Q-1] = 0.0f };
  2682. half M[Q] = { [0 ... Q-1] = -__FLT16_MAX__/2 };
  2683. // thread indices inside the simdgroup
  2684. // TODO: see if we can utilize quad-group functions for better performance
  2685. // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (6.9.3)
  2686. const short tx = tiisg%4;
  2687. const short ty = tiisg/4;
  2688. // broadcast kv
  2689. //const short rk2 = args.ne02/args.ne12;
  2690. //const short rk3 = args.ne03/args.ne13;
  2691. const short ikv2 = iq2/(args.ne02/args.ne_12_2);
  2692. const short ikv3 = iq3/(args.ne03/args.ne_12_3);
  2693. // load the queries from shared memory into local memory
  2694. q8x8_t mq[D8];
  2695. for (short i = 0; i < D8; ++i) {
  2696. simdgroup_load(mq[i], sq + i*8, D);
  2697. }
  2698. const bool has_mask = mask != q;
  2699. half slope = 1.0f;
  2700. // ALiBi
  2701. if (args.max_bias > 0.0f) {
  2702. const short h = iq2;
  2703. const half base = h < args.n_head_log2 ? args.m0 : args.m1;
  2704. const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1;
  2705. slope = pow(base, exph);
  2706. }
  2707. // loop over the KV cache
  2708. // each simdgroup handles blocks of Q rows and C columns
  2709. for (int ic0 = 0; ic0 < args.ne11; ic0 += C*nsg) {
  2710. const int ic = ic0 + C*sgitg;
  2711. if (ic >= args.ne11) {
  2712. break;
  2713. }
  2714. if (has_mask) {
  2715. // used to detect blocks full of -INF
  2716. half smax = -INFINITY;
  2717. // load the mask in shared memory
  2718. #pragma unroll(Q)
  2719. for (short j = 0; j < Q; ++j) {
  2720. device const half * pm = (device const half *) ((device const char *) mask + (iq1 + j)*args.nb31);
  2721. const half m = pm[ic + tiisg];
  2722. ss[j*TS + C + tiisg] = m;
  2723. smax = max(smax, m);
  2724. }
  2725. smax = simd_max(smax);
  2726. if (smax == -INFINITY) {
  2727. continue;
  2728. }
  2729. }
  2730. // Q*K^T
  2731. {
  2732. for (short cc = 0; cc < C/8; ++cc) {
  2733. qk8x8_t mqk = make_filled_simdgroup_matrix<qk_t, 8>((qk_t) 0.0f);
  2734. // this is compile-time check, so it does not have runtime overhead
  2735. if (is_same<kd4x4_t, k4x4_t>::value) {
  2736. // we can read directly from global memory
  2737. device const k_t * pk = (device const k_t *) ((device const char *) k + ((ic + 8*cc)*args.nb_12_1 + ikv2*args.nb_12_2 + ikv3*args.nb_12_3));
  2738. #pragma unroll(D8)
  2739. for (short i = 0; i < D8; ++i) {
  2740. k8x8_t mk;
  2741. simdgroup_load(mk, pk + i*8, args.nb_12_1/sizeof(k_t), 0, true); // transpose // TODO: use ne10
  2742. simdgroup_multiply_accumulate(mqk, mq[i], mk, mqk);
  2743. }
  2744. } else {
  2745. for (short ii = 0; ii < D16; ii += 4) {
  2746. device const kd4x4_t * pk4x4 = (device const kd4x4_t *) ((device const char *) k + ((ic + 8*cc + ty)*args.nb_12_1 + ikv2*args.nb_12_2 + ikv3*args.nb_12_3));
  2747. if (D16%4 == 0) {
  2748. // the head is evenly divisible by 4*16 = 64, so no need for bound checks
  2749. {
  2750. k4x4_t tmp;
  2751. deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp);
  2752. sk4x4[4*ty + tx] = tmp;
  2753. }
  2754. simdgroup_barrier(mem_flags::mem_threadgroup);
  2755. #pragma unroll(4)
  2756. for (short k = 0; k < 4; ++k) {
  2757. k8x8_t mk;
  2758. simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose
  2759. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 0], mk, mqk);
  2760. simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose
  2761. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 1], mk, mqk);
  2762. }
  2763. } else {
  2764. if (ii + tx < D16) {
  2765. k4x4_t tmp;
  2766. deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp);
  2767. sk4x4[4*ty + tx] = tmp;
  2768. }
  2769. simdgroup_barrier(mem_flags::mem_threadgroup);
  2770. for (short k = 0; k < 4 && ii + k < D16; ++k) {
  2771. k8x8_t mk;
  2772. simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose
  2773. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 0], mk, mqk);
  2774. simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose
  2775. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 1], mk, mqk);
  2776. }
  2777. }
  2778. }
  2779. }
  2780. // cast qk_t -> s_t
  2781. //s8x8_t mqks(1.0f);
  2782. //simdgroup_multiply(mqks, mqk, mqks);
  2783. //simdgroup_store(mqks, ss + 8*cc, TS, 0, false);
  2784. simdgroup_store(mqk, ss + 8*cc, TS, 0, false);
  2785. }
  2786. }
  2787. // online softmax
  2788. {
  2789. for (ushort j = 0; j < Q; ++j) {
  2790. const half m = M[j];
  2791. // scale and apply the logitcap / mask
  2792. half s = ss[j*TS + tiisg]*args.scale;
  2793. if (args.logit_softcap != 0.0f) {
  2794. s = args.logit_softcap*precise::tanh(s);
  2795. }
  2796. // mqk = mqk + mask*slope
  2797. s += slope*ss[j*TS + C + tiisg];
  2798. M[j] = simd_max(max(M[j], s));
  2799. const half ms = exp(m - M[j]);
  2800. const half vs = exp(s - M[j]);
  2801. S[j] = S[j]*ms + simd_sum(vs);
  2802. // the P matrix from the paper (Q rows, C columns)
  2803. ss[j*TS + tiisg] = vs;
  2804. // create a QxQ diagonal matrix for rescaling the output
  2805. if (tiisg == j) {
  2806. ss[j*TS + 2*C + j] = ms;
  2807. }
  2808. }
  2809. }
  2810. // O = diag(ms)*O
  2811. {
  2812. s8x8_t mm;
  2813. simdgroup_load(mm, ss + 2*C, TS, 0, false);
  2814. #pragma unroll(D8)
  2815. for (short i = 0; i < D8; ++i) {
  2816. simdgroup_multiply(lo[i], mm, lo[i]);
  2817. }
  2818. }
  2819. // O = O + (Q*K^T)*V
  2820. {
  2821. for (short cc = 0; cc < C/8; ++cc) {
  2822. s8x8_t ms;
  2823. simdgroup_load(ms, ss + 8*cc, TS, 0, false);
  2824. if (is_same<vd4x4_t, v4x4_t>::value) {
  2825. // we can read directly from global memory
  2826. device const v_t * pv = (device const v_t *) ((device const char *) v + ((ic + 8*cc)*args.nb_12_1 + ikv2*args.nb_12_2 + ikv3*args.nb_12_3));
  2827. #pragma unroll(D8)
  2828. for (short i = 0; i < D8; ++i) {
  2829. v8x8_t mv;
  2830. simdgroup_load(mv, pv + i*8, args.nb_12_1/sizeof(v_t), 0, false); // TODO: use ne20
  2831. simdgroup_multiply_accumulate(lo[i], ms, mv, lo[i]);
  2832. }
  2833. } else {
  2834. for (short ii = 0; ii < D16; ii += 4) {
  2835. device const vd4x4_t * pv4x4 = (device const vd4x4_t *) ((device const char *) v + ((ic + 8*cc + ty)*args.nb_12_1 + ikv2*args.nb_12_2 + ikv3*args.nb_12_3));
  2836. if (D16%4 == 0) {
  2837. // no need for bound checks
  2838. {
  2839. v4x4_t tmp;
  2840. deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp);
  2841. sv4x4[4*ty + tx] = tmp;
  2842. }
  2843. simdgroup_barrier(mem_flags::mem_threadgroup);
  2844. #pragma unroll(4)
  2845. for (short k = 0; k < 4; ++k) {
  2846. v8x8_t mv;
  2847. simdgroup_load(mv, sv + 16*k + 0*8, 4*16, 0, false);
  2848. simdgroup_multiply_accumulate(lo[2*(ii + k) + 0], ms, mv, lo[2*(ii + k) + 0]);
  2849. simdgroup_load(mv, sv + 16*k + 1*8, 4*16, 0, false);
  2850. simdgroup_multiply_accumulate(lo[2*(ii + k) + 1], ms, mv, lo[2*(ii + k) + 1]);
  2851. }
  2852. } else {
  2853. if (ii + tx < D16) {
  2854. v4x4_t tmp;
  2855. deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp);
  2856. sv4x4[4*ty + tx] = tmp;
  2857. }
  2858. simdgroup_barrier(mem_flags::mem_threadgroup);
  2859. for (short k = 0; k < 4 && ii + k < D16; ++k) {
  2860. v8x8_t mv;
  2861. simdgroup_load(mv, sv + 16*k + 0*8, 4*16, 0, false);
  2862. simdgroup_multiply_accumulate(lo[2*(ii + k) + 0], ms, mv, lo[2*(ii + k) + 0]);
  2863. simdgroup_load(mv, sv + 16*k + 1*8, 4*16, 0, false);
  2864. simdgroup_multiply_accumulate(lo[2*(ii + k) + 1], ms, mv, lo[2*(ii + k) + 1]);
  2865. }
  2866. }
  2867. }
  2868. }
  2869. }
  2870. }
  2871. }
  2872. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  2873. for (short j = 0; j < Q; ++j) {
  2874. if (tiisg == 0) {
  2875. ss[j*TS + 0] = S[j];
  2876. ss[j*TS + 1] = M[j];
  2877. }
  2878. }
  2879. }
  2880. // reduce the warps sequentially
  2881. for (ushort sg = 1; sg < nsg; ++sg) {
  2882. half S = { 0.0f };
  2883. half M = { -__FLT16_MAX__/2 };
  2884. threadgroup_barrier(mem_flags::mem_threadgroup);
  2885. // each simdgroup stores its output to shared memory, reusing sq
  2886. if (sgitg == sg) {
  2887. for (short i = 0; i < D8; ++i) {
  2888. simdgroup_store(lo[i], so + i*8, D, 0, false);
  2889. }
  2890. }
  2891. threadgroup_barrier(mem_flags::mem_threadgroup);
  2892. // the first simdgroup accumulates the results from the other simdgroups
  2893. if (sgitg == 0) {
  2894. for (short j = 0; j < Q; ++j) {
  2895. const half S0 = ss[j*TS + 0];
  2896. const half S1 = ss[j*TS + sg*SH + 0];
  2897. const half M0 = ss[j*TS + 1];
  2898. const half M1 = ss[j*TS + sg*SH + 1];
  2899. M = max(M0, M1);
  2900. const half ms0 = exp(M0 - M);
  2901. const half ms1 = exp(M1 - M);
  2902. S = S0*ms0 + S1*ms1;
  2903. if (tiisg == 0) {
  2904. ss[j*TS + 0] = S;
  2905. ss[j*TS + 1] = M;
  2906. ss[j*TS + 2*C + j ] = ms0;
  2907. ss[j*TS + 2*C + j + sg*SH] = ms1;
  2908. }
  2909. }
  2910. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  2911. {
  2912. s8x8_t ms0;
  2913. s8x8_t ms1;
  2914. simdgroup_load(ms0, ss + 2*C, TS, 0, false);
  2915. simdgroup_load(ms1, ss + 2*C + sg*SH, TS, 0, false);
  2916. #pragma unroll(D8)
  2917. for (short i = 0; i < D8; ++i) {
  2918. o8x8_t t;
  2919. simdgroup_load (t, so + i*8, D, 0, false);
  2920. simdgroup_multiply(t, ms1, t);
  2921. simdgroup_multiply_accumulate(lo[i], ms0, lo[i], t);
  2922. }
  2923. }
  2924. }
  2925. }
  2926. // store result to shared memory (reuse sq)
  2927. if (sgitg == 0) {
  2928. for (short i = 0; i < D8; ++i) {
  2929. simdgroup_store(lo[i], so + i*8, D, 0, false);
  2930. }
  2931. }
  2932. device float4 * dst4 = (device float4 *) dst;
  2933. // final rescale with 1/S and store to global memory
  2934. if (sgitg == 0) {
  2935. for (short j = 0; j < Q && iq1 + j < args.ne01; ++j) {
  2936. const float S = ss[j*TS + 0];
  2937. for (short i = tiisg; i < D4; i += NW) {
  2938. dst4[((uint64_t)iq3*args.ne2*args.ne1 + iq2 + (uint64_t)(iq1 + j)*args.ne1)*D4 + i] = (float4) so4[j*D4 + i]/S;
  2939. }
  2940. }
  2941. }
  2942. }
  2943. // TODO: this is quite ugly. in the future these types will be hardcoded in the kernel, but for now keep them as
  2944. // template to be able to explore different combinations
  2945. //
  2946. #define FA_TYPES \
  2947. half, half4, simdgroup_half8x8, \
  2948. half, half4x4, simdgroup_half8x8, \
  2949. half, half4x4, simdgroup_half8x8, \
  2950. float, simdgroup_float8x8, \
  2951. float, simdgroup_float8x8, \
  2952. half, half4, simdgroup_half8x8
  2953. typedef decltype(kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 64>) flash_attn_ext_t;
  2954. template [[host_name("kernel_flash_attn_ext_f16_h64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 64>;
  2955. template [[host_name("kernel_flash_attn_ext_f16_h80" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 80>;
  2956. template [[host_name("kernel_flash_attn_ext_f16_h96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 96>;
  2957. template [[host_name("kernel_flash_attn_ext_f16_h112")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 112>;
  2958. template [[host_name("kernel_flash_attn_ext_f16_h128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 128>;
  2959. template [[host_name("kernel_flash_attn_ext_f16_h256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 256>;
  2960. #if defined(GGML_METAL_USE_BF16)
  2961. template [[host_name("kernel_flash_attn_ext_bf16_h64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 64>;
  2962. template [[host_name("kernel_flash_attn_ext_bf16_h80" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 80>;
  2963. template [[host_name("kernel_flash_attn_ext_bf16_h96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 96>;
  2964. template [[host_name("kernel_flash_attn_ext_bf16_h112")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 112>;
  2965. template [[host_name("kernel_flash_attn_ext_bf16_h128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 128>;
  2966. template [[host_name("kernel_flash_attn_ext_bf16_h256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 256>;
  2967. #endif
  2968. template [[host_name("kernel_flash_attn_ext_q4_0_h64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 64>;
  2969. template [[host_name("kernel_flash_attn_ext_q4_0_h80" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 80>;
  2970. template [[host_name("kernel_flash_attn_ext_q4_0_h96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 96>;
  2971. template [[host_name("kernel_flash_attn_ext_q4_0_h112")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 112>;
  2972. template [[host_name("kernel_flash_attn_ext_q4_0_h128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 128>;
  2973. template [[host_name("kernel_flash_attn_ext_q4_0_h256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 256>;
  2974. template [[host_name("kernel_flash_attn_ext_q4_1_h64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 64>;
  2975. template [[host_name("kernel_flash_attn_ext_q4_1_h80" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 80>;
  2976. template [[host_name("kernel_flash_attn_ext_q4_1_h96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 96>;
  2977. template [[host_name("kernel_flash_attn_ext_q4_1_h112")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 112>;
  2978. template [[host_name("kernel_flash_attn_ext_q4_1_h128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 128>;
  2979. template [[host_name("kernel_flash_attn_ext_q4_1_h256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 256>;
  2980. template [[host_name("kernel_flash_attn_ext_q5_0_h64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 64>;
  2981. template [[host_name("kernel_flash_attn_ext_q5_0_h80" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 80>;
  2982. template [[host_name("kernel_flash_attn_ext_q5_0_h96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 96>;
  2983. template [[host_name("kernel_flash_attn_ext_q5_0_h112")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 112>;
  2984. template [[host_name("kernel_flash_attn_ext_q5_0_h128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 128>;
  2985. template [[host_name("kernel_flash_attn_ext_q5_0_h256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 256>;
  2986. template [[host_name("kernel_flash_attn_ext_q5_1_h64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 64>;
  2987. template [[host_name("kernel_flash_attn_ext_q5_1_h80" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 80>;
  2988. template [[host_name("kernel_flash_attn_ext_q5_1_h96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 96>;
  2989. template [[host_name("kernel_flash_attn_ext_q5_1_h112")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 112>;
  2990. template [[host_name("kernel_flash_attn_ext_q5_1_h128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 128>;
  2991. template [[host_name("kernel_flash_attn_ext_q5_1_h256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 256>;
  2992. template [[host_name("kernel_flash_attn_ext_q8_0_h64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 64>;
  2993. template [[host_name("kernel_flash_attn_ext_q8_0_h80" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 80>;
  2994. template [[host_name("kernel_flash_attn_ext_q8_0_h96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 96>;
  2995. template [[host_name("kernel_flash_attn_ext_q8_0_h112")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 112>;
  2996. template [[host_name("kernel_flash_attn_ext_q8_0_h128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 128>;
  2997. template [[host_name("kernel_flash_attn_ext_q8_0_h256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 256>;
  2998. #undef FA_TYPES
  2999. template<
  3000. typename q4_t, // query types in shared memory
  3001. typename q4x4_t,
  3002. typename k4x4_t, // key types in shared memory
  3003. typename v4x4_t, // value types in shared memory
  3004. typename qk_t, // Q*K types
  3005. typename s_t, // soft-max types
  3006. typename s4_t,
  3007. typename s4x4_t,
  3008. typename o4x4_t, // attention accumulation types
  3009. typename kd4x4_t, // key type in device memory
  3010. short nl_k,
  3011. void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &),
  3012. typename vd4x4_t, // key type in device memory
  3013. short nl_v,
  3014. void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &),
  3015. short D, // head size
  3016. short Q = 1, // queries per threadgroup
  3017. short C = 32> // cache items per threadgroup
  3018. kernel void kernel_flash_attn_ext_vec(
  3019. constant ggml_metal_kargs_flash_attn_ext & args,
  3020. device const char * q,
  3021. device const char * k,
  3022. device const char * v,
  3023. device const char * mask,
  3024. device char * dst,
  3025. threadgroup half * shmem_f16 [[threadgroup(0)]],
  3026. uint3 tgpig[[threadgroup_position_in_grid]],
  3027. ushort3 ntg[[threads_per_threadgroup]],
  3028. ushort tiisg[[thread_index_in_simdgroup]],
  3029. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3030. const short nsg = ntg.y; // number of simdgroups
  3031. const int iq3 = tgpig[2];
  3032. const int iq2 = tgpig[1];
  3033. const int iq1 = tgpig[0];
  3034. const short D4 = D/4;
  3035. const short D16 = D/16;
  3036. const short NW = N_SIMDWIDTH;
  3037. const short NL = NW/4; // note: this can be adjusted to support D%64 == 0 and D%32 == 0
  3038. const short SH = 2*C; // shared memory per simdgroup
  3039. const short T = D + nsg*SH; // shared memory size per query in (half)
  3040. //threadgroup q_t * sq = (threadgroup q_t *) (shmem_f16 + 0*D); // holds the query data
  3041. threadgroup q4_t * sq4 = (threadgroup q4_t *) (shmem_f16 + 0*D); // same as above but in q4_t
  3042. threadgroup q4x4_t * sq4x4 = (threadgroup q4x4_t *) (shmem_f16 + 0*D); // same as above but in q4x4_t
  3043. threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + sgitg*SH + Q*D); // scratch buffer for attention
  3044. threadgroup s4_t * ss4 = (threadgroup s4_t *) (shmem_f16 + sgitg*SH + Q*D); // same as above but in s4_t
  3045. threadgroup half * sm = (threadgroup half *) (shmem_f16 + sgitg*SH + C + Q*D); // scratch buffer for mask
  3046. threadgroup o4x4_t * sr4x4 = (threadgroup o4x4_t *) (shmem_f16 + sgitg*D + Q*T); // scratch buffer for the results
  3047. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  3048. o4x4_t lo[D16/NL];
  3049. // load heads from Q to shared memory
  3050. device const float4 * q4 = (device const float4 *) ((device const char *) q + (iq1*args.nb01 + iq2*args.nb02 + iq3*args.nb03));
  3051. for (short i = tiisg; i < D4; i += NW) {
  3052. if (iq1 < args.ne01) {
  3053. sq4[i] = (q4_t) q4[i];
  3054. } else {
  3055. sq4[i] = (q4_t) 0.0f;
  3056. }
  3057. }
  3058. // zero out lo
  3059. for (short i = 0; i < D16/NL; ++i) {
  3060. lo[i] = (o4x4_t) 0.0f;
  3061. }
  3062. // zero out shared memory SH
  3063. for (short i = tiisg; i < SH/4; i += NW) {
  3064. ss4[i] = (s4_t) 0.0f;
  3065. }
  3066. threadgroup_barrier(mem_flags::mem_threadgroup);
  3067. {
  3068. half S = 0.0f;
  3069. half M = -__FLT16_MAX__/2;
  3070. // thread indices inside the simdgroup
  3071. const short tx = tiisg%NL;
  3072. const short ty = tiisg/NL;
  3073. // broadcast kv
  3074. //const short rk2 = args.ne02/args.ne12;
  3075. //const short rk3 = args.ne03/args.ne13;
  3076. const short ikv2 = iq2/(args.ne02/args.ne_12_2);
  3077. const short ikv3 = iq3/(args.ne03/args.ne_12_3);
  3078. // load the queries from shared memory into local memory
  3079. q4x4_t mq[D16/NL];
  3080. #pragma unroll(D16/NL)
  3081. for (short ii = 0; ii < D16; ii += NL) {
  3082. mq[ii/NL] = sq4x4[ii + tx];
  3083. }
  3084. const bool has_mask = mask != q;
  3085. // pointer to the mask
  3086. device const half * pm = (device const half *) (mask + iq1*args.nb31);
  3087. half slope = 1.0f;
  3088. // ALiBi
  3089. if (args.max_bias > 0.0f) {
  3090. const short h = iq2;
  3091. const half base = h < args.n_head_log2 ? args.m0 : args.m1;
  3092. const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1;
  3093. slope = pow(base, exph);
  3094. }
  3095. // loop over the KV cache
  3096. // each simdgroup handles blocks of Q rows and C columns
  3097. for (int ic0 = 0; ic0 < args.ne11; ic0 += C*nsg) {
  3098. const int ic = ic0 + C*sgitg;
  3099. if (ic >= args.ne11) {
  3100. break;
  3101. }
  3102. if (has_mask) {
  3103. sm[tiisg] = pm[ic + tiisg];
  3104. }
  3105. // Q*K^T
  3106. {
  3107. // each simdgroup processes 1 query and 4 (NW/NL) keys
  3108. for (short cc = 0; cc < C/4; ++cc) {
  3109. qk_t mqka[4] = { 0.0, 0.0, 0.0, 0.0 };
  3110. device const kd4x4_t * pk = (device const kd4x4_t *) ((device const char *) k + ((ic + 4*cc + ty)*args.nb_12_1 + ikv2*args.nb_12_2 + ikv3*args.nb_12_3));
  3111. #pragma unroll(D16/NL)
  3112. for (short ii = 0; ii < D16; ii += NL) {
  3113. const short i = ii + tx;
  3114. k4x4_t mk;
  3115. deq_k(pk + i/nl_k, i%nl_k, mk);
  3116. // note: this is less precise than the version below
  3117. //mqka[0] += dot(mq[ii/NL][0], mk[0]);
  3118. //mqka[1] += dot(mq[ii/NL][1], mk[1]);
  3119. //mqka[2] += dot(mq[ii/NL][2], mk[2]);
  3120. //mqka[3] += dot(mq[ii/NL][3], mk[3]);
  3121. mqka[0] += dot((float4) mq[ii/NL][0], (float4) mk[0]);
  3122. mqka[1] += dot((float4) mq[ii/NL][1], (float4) mk[1]);
  3123. mqka[2] += dot((float4) mq[ii/NL][2], (float4) mk[2]);
  3124. mqka[3] += dot((float4) mq[ii/NL][3], (float4) mk[3]);
  3125. }
  3126. qk_t mqk = mqka[0] + mqka[1] + mqka[2] + mqka[3];
  3127. // simdgroup reduce
  3128. // [ 0 .. 7] -> [ 0]
  3129. // [ 8 .. 15] -> [ 8]
  3130. // [16 .. 23] -> [16]
  3131. // [24 .. 31] -> [24]
  3132. //mqk += simd_shuffle_down(mqk, 16);
  3133. //mqk += simd_shuffle_down(mqk, 8);
  3134. mqk += simd_shuffle_down(mqk, 4);
  3135. mqk += simd_shuffle_down(mqk, 2);
  3136. mqk += simd_shuffle_down(mqk, 1);
  3137. // mqk = mqk*scale + mask*slope
  3138. if (tx == 0) {
  3139. mqk *= args.scale;
  3140. if (args.logit_softcap != 0.0f) {
  3141. mqk = args.logit_softcap*precise::tanh(mqk);
  3142. }
  3143. mqk += sm[4*cc + ty]*slope;
  3144. ss[4*cc + ty] = mqk;
  3145. }
  3146. }
  3147. }
  3148. simdgroup_barrier(mem_flags::mem_threadgroup);
  3149. // online softmax
  3150. {
  3151. const half m = M;
  3152. const half s = ss[tiisg];
  3153. M = simd_max(max(M, s));
  3154. const half ms = exp(m - M);
  3155. const half vs = exp(s - M);
  3156. S = S*ms + simd_sum(vs);
  3157. // the P matrix from the paper (Q rows, C columns)
  3158. ss[tiisg] = vs;
  3159. // O = diag(ms)*O
  3160. #pragma unroll(D16/NL)
  3161. for (short ii = 0; ii < D16; ii += NL) {
  3162. lo[ii/NL] *= ms;
  3163. }
  3164. }
  3165. simdgroup_barrier(mem_flags::mem_threadgroup);
  3166. // O = O + (Q*K^T)*V
  3167. {
  3168. for (short cc = 0; cc < C/4; ++cc) {
  3169. device const vd4x4_t * pv4 = (device const vd4x4_t *) ((device const char *) v + ((ic + 4*cc + ty)*args.nb_12_1 + ikv2*args.nb_12_2 + ikv3*args.nb_12_3));
  3170. const s4x4_t ms(ss[4*cc + ty]);
  3171. #pragma unroll(D16/NL)
  3172. for (short ii = 0; ii < D16; ii += NL) {
  3173. const short i = ii + tx;
  3174. v4x4_t mv;
  3175. deq_v(pv4 + i/nl_v, i%nl_v, mv);
  3176. lo[ii/NL] += mv*ms;
  3177. }
  3178. }
  3179. }
  3180. }
  3181. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  3182. if (tiisg == 0) {
  3183. ss[0] = (s_t) S;
  3184. ss[1] = (s_t) M;
  3185. }
  3186. }
  3187. // simdgroup reduce
  3188. // [ 0, 8, 16, 24] -> [ 0]
  3189. // [ 1, 9, 17, 25] -> [ 1]
  3190. // [ 2, 10, 18, 26] -> [ 2]
  3191. // [ 3, 11, 19, 27] -> [ 3]
  3192. // [ 4, 12, 20, 28] -> [ 4]
  3193. // [ 5, 13, 21, 29] -> [ 5]
  3194. // [ 6, 14, 22, 30] -> [ 6]
  3195. // [ 7, 15, 23, 31] -> [ 7]
  3196. for (short ii = 0; ii < D16; ii += NL) {
  3197. lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 16);
  3198. lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 8);
  3199. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 4);
  3200. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 2);
  3201. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 1);
  3202. lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 16);
  3203. lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 8);
  3204. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 4);
  3205. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 2);
  3206. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 1);
  3207. lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 16);
  3208. lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 8);
  3209. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 4);
  3210. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 2);
  3211. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 1);
  3212. lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 16);
  3213. lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 8);
  3214. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 4);
  3215. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 2);
  3216. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 1);
  3217. }
  3218. threadgroup_barrier(mem_flags::mem_threadgroup);
  3219. // store results to shared memory
  3220. for (short i = tiisg; i < D16; i += NL) {
  3221. sr4x4[i] = lo[i/NL];
  3222. }
  3223. threadgroup_barrier(mem_flags::mem_threadgroup);
  3224. // parallel reduce
  3225. for (short r = nsg/2; r > 0; r >>= 1) {
  3226. if (sgitg < r) {
  3227. const half S0 = ss[ 0];
  3228. const half S1 = ss[r*SH + 0];
  3229. const half M0 = ss[ 1];
  3230. const half M1 = ss[r*SH + 1];
  3231. const half M = max(M0, M1);
  3232. const half ms0 = exp(M0 - M);
  3233. const half ms1 = exp(M1 - M);
  3234. const half S = S0*ms0 + S1*ms1;
  3235. if (tiisg == 0) {
  3236. ss[0] = S;
  3237. ss[1] = M;
  3238. }
  3239. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  3240. for (short i = tiisg; i < D16; i += NW) {
  3241. sr4x4[i] = sr4x4[i]*ms0 + sr4x4[i + r*D16]*ms1;
  3242. }
  3243. }
  3244. threadgroup_barrier(mem_flags::mem_threadgroup);
  3245. }
  3246. device float4x4 * dst44 = (device float4x4 *) dst;
  3247. // final rescale with 1/S and store to global memory
  3248. if (sgitg == 0) {
  3249. const float S = ss[0];
  3250. for (short i = tiisg; i < D16; i += NW) {
  3251. dst44[((uint64_t)iq3*args.ne2*args.ne1 + iq2 + (uint64_t)iq1*args.ne1)*D16 + i] = (float4x4) sr4x4[i]/S;
  3252. }
  3253. }
  3254. }
  3255. // note: I think the s_t can be half instead of float, because the Q*K scaling is done before storing to shared mem
  3256. // in the other (non-vec) kernel, we need s_t to also be float because we scale during the soft_max
  3257. //
  3258. #define FA_TYPES \
  3259. half4, half4x4, \
  3260. half4x4, \
  3261. half4x4, \
  3262. float, \
  3263. half, half4, half4x4, \
  3264. half4x4
  3265. typedef decltype(kernel_flash_attn_ext_vec<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 128>) flash_attn_ext_vec_t;
  3266. template [[host_name("kernel_flash_attn_ext_vec_f16_h128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 128>;
  3267. #if defined(GGML_METAL_USE_BF16)
  3268. template [[host_name("kernel_flash_attn_ext_vec_bf16_h128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 128>;
  3269. #endif
  3270. template [[host_name("kernel_flash_attn_ext_vec_q4_0_h128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 128>;
  3271. template [[host_name("kernel_flash_attn_ext_vec_q4_1_h128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 128>;
  3272. template [[host_name("kernel_flash_attn_ext_vec_q5_0_h128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 128>;
  3273. template [[host_name("kernel_flash_attn_ext_vec_q5_1_h128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 128>;
  3274. template [[host_name("kernel_flash_attn_ext_vec_q8_0_h128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 128>;
  3275. template [[host_name("kernel_flash_attn_ext_vec_f16_h256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 256>;
  3276. #if defined(GGML_METAL_USE_BF16)
  3277. template [[host_name("kernel_flash_attn_ext_vec_bf16_h256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4x4, 1, dequantize_bf16, bfloat4x4, 1, dequantize_bf16, 256>;
  3278. #endif
  3279. template [[host_name("kernel_flash_attn_ext_vec_q4_0_h256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q4_0, 2, dequantize_q4_0, block_q4_0, 2, dequantize_q4_0, 256>;
  3280. template [[host_name("kernel_flash_attn_ext_vec_q4_1_h256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q4_1, 2, dequantize_q4_1, block_q4_1, 2, dequantize_q4_1, 256>;
  3281. template [[host_name("kernel_flash_attn_ext_vec_q5_0_h256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q5_0, 2, dequantize_q5_0, block_q5_0, 2, dequantize_q5_0, 256>;
  3282. template [[host_name("kernel_flash_attn_ext_vec_q5_1_h256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q5_1, 2, dequantize_q5_1, block_q5_1, 2, dequantize_q5_1, 256>;
  3283. template [[host_name("kernel_flash_attn_ext_vec_q8_0_h256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_q8_0, 2, dequantize_q8_0, block_q8_0, 2, dequantize_q8_0, 256>;
  3284. #undef FA_TYPES
  3285. template<typename T>
  3286. kernel void kernel_set(
  3287. constant ggml_metal_kargs_set & args,
  3288. device const char * src0,
  3289. device const char * src1,
  3290. device char * dst,
  3291. uint3 tgpig[[threadgroup_position_in_grid]],
  3292. ushort3 tpitg[[thread_position_in_threadgroup]],
  3293. ushort3 ntg[[threads_per_threadgroup]]) {
  3294. const int i13 = tgpig[2];
  3295. const int i12 = tgpig[1];
  3296. const int i11 = tgpig[0];
  3297. const int64_t n = i13*args.ne12*args.ne11*args.ne10 + i12*args.ne11*args.ne10 + i11*args.ne10;
  3298. const int64_t i3 = n / (args.ne12*args.ne11*args.ne10);
  3299. const int64_t i2 = (n - i3*args.ne12*args.ne11*args.ne10) / (args.ne11*args.ne10);
  3300. const int64_t i1 = (n - i3*args.ne12*args.ne11*args.ne10 - i2*args.ne11*args.ne10) / args.ne10;
  3301. device T * dst_data = (device T *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + args.offs);
  3302. for (int64_t i10 = tpitg.x; i10 < args.ne10; i10 += ntg.x) {
  3303. device const T * src = (device T *) (src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11 + i10*args.nb10);
  3304. dst_data[i10] = (T) src[0];
  3305. }
  3306. }
  3307. typedef decltype(kernel_set<float>) kernel_set_t;
  3308. template [[host_name("kernel_set_f32")]] kernel kernel_set_t kernel_set<float>;
  3309. template [[host_name("kernel_set_i32")]] kernel kernel_set_t kernel_set<int32_t>;
  3310. template<typename T0, typename T1>
  3311. kernel void kernel_cpy(
  3312. constant ggml_metal_kargs_cpy & args,
  3313. device const char * src0,
  3314. device char * dst,
  3315. uint3 tgpig[[threadgroup_position_in_grid]],
  3316. ushort3 tpitg[[thread_position_in_threadgroup]],
  3317. ushort3 ntg[[threads_per_threadgroup]]) {
  3318. const int i03 = tgpig[2];
  3319. const int i02 = tgpig[1];
  3320. const int i01 = tgpig[0];
  3321. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  3322. const int64_t i3 = n/(args.ne2*args.ne1*args.ne0);
  3323. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0)/(args.ne1*args.ne0);
  3324. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0)/args.ne0;
  3325. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0);
  3326. device T1 * dst_data = (device T1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3327. for (int64_t i00 = tpitg.x; i00 < args.ne00; i00 += ntg.x) {
  3328. device const T0 * src = (device T0 *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  3329. dst_data[i00] = (T1) src[0];
  3330. }
  3331. }
  3332. typedef decltype(kernel_cpy<float, float>) kernel_cpy_t;
  3333. template [[host_name("kernel_cpy_f32_f32")]] kernel kernel_cpy_t kernel_cpy<float, float>;
  3334. template [[host_name("kernel_cpy_f32_f16")]] kernel kernel_cpy_t kernel_cpy<float, half>;
  3335. #if defined(GGML_METAL_USE_BF16)
  3336. template [[host_name("kernel_cpy_f32_bf16")]] kernel kernel_cpy_t kernel_cpy<float, bfloat>;
  3337. #endif
  3338. template [[host_name("kernel_cpy_f16_f32")]] kernel kernel_cpy_t kernel_cpy<half, float>;
  3339. template [[host_name("kernel_cpy_f16_f16")]] kernel kernel_cpy_t kernel_cpy<half, half>;
  3340. #if defined(GGML_METAL_USE_BF16)
  3341. template [[host_name("kernel_cpy_bf16_f32")]] kernel kernel_cpy_t kernel_cpy<bfloat, float>;
  3342. template [[host_name("kernel_cpy_bf16_bf16")]] kernel kernel_cpy_t kernel_cpy<bfloat, bfloat>;
  3343. #endif
  3344. kernel void kernel_cpy_f32_q8_0(
  3345. constant ggml_metal_kargs_cpy & args,
  3346. device const char * src0,
  3347. device char * dst,
  3348. uint3 tgpig[[threadgroup_position_in_grid]],
  3349. ushort3 tpitg[[thread_position_in_threadgroup]],
  3350. ushort3 ntg[[threads_per_threadgroup]]) {
  3351. const int i03 = tgpig[2];
  3352. const int i02 = tgpig[1];
  3353. const int i01 = tgpig[0];
  3354. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  3355. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  3356. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  3357. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  3358. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK8_0;
  3359. device block_q8_0 * dst_data = (device block_q8_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3360. for (int64_t i00 = tpitg.x*QK8_0; i00 < args.ne00; i00 += ntg.x*QK8_0) {
  3361. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  3362. float amax = 0.0f; // absolute max
  3363. for (int j = 0; j < QK8_0; j++) {
  3364. const float v = src[j];
  3365. amax = MAX(amax, fabs(v));
  3366. }
  3367. const float d = amax / ((1 << 7) - 1);
  3368. const float id = d ? 1.0f/d : 0.0f;
  3369. dst_data[i00/QK8_0].d = d;
  3370. for (int j = 0; j < QK8_0; ++j) {
  3371. const float x0 = src[j]*id;
  3372. dst_data[i00/QK8_0].qs[j] = round(x0);
  3373. }
  3374. }
  3375. }
  3376. kernel void kernel_cpy_f32_q4_0(
  3377. constant ggml_metal_kargs_cpy & args,
  3378. device const char * src0,
  3379. device char * dst,
  3380. uint3 tgpig[[threadgroup_position_in_grid]],
  3381. ushort3 tpitg[[thread_position_in_threadgroup]],
  3382. ushort3 ntg[[threads_per_threadgroup]]) {
  3383. const int i03 = tgpig[2];
  3384. const int i02 = tgpig[1];
  3385. const int i01 = tgpig[0];
  3386. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  3387. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  3388. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  3389. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  3390. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_0;
  3391. device block_q4_0 * dst_data = (device block_q4_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3392. for (int64_t i00 = tpitg.x*QK4_0; i00 < args.ne00; i00 += ntg.x*QK4_0) {
  3393. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  3394. float amax = 0.0f; // absolute max
  3395. float max = 0.0f;
  3396. for (int j = 0; j < QK4_0; j++) {
  3397. const float v = src[j];
  3398. if (amax < fabs(v)) {
  3399. amax = fabs(v);
  3400. max = v;
  3401. }
  3402. }
  3403. const float d = max / -8;
  3404. const float id = d ? 1.0f/d : 0.0f;
  3405. dst_data[i00/QK4_0].d = d;
  3406. for (int j = 0; j < QK4_0/2; ++j) {
  3407. const float x0 = src[0 + j]*id;
  3408. const float x1 = src[QK4_0/2 + j]*id;
  3409. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  3410. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  3411. dst_data[i00/QK4_0].qs[j] = xi0;
  3412. dst_data[i00/QK4_0].qs[j] |= xi1 << 4;
  3413. }
  3414. }
  3415. }
  3416. kernel void kernel_cpy_f32_q4_1(
  3417. constant ggml_metal_kargs_cpy & args,
  3418. device const char * src0,
  3419. device char * dst,
  3420. uint3 tgpig[[threadgroup_position_in_grid]],
  3421. ushort3 tpitg[[thread_position_in_threadgroup]],
  3422. ushort3 ntg[[threads_per_threadgroup]]) {
  3423. const int i03 = tgpig[2];
  3424. const int i02 = tgpig[1];
  3425. const int i01 = tgpig[0];
  3426. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  3427. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  3428. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  3429. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  3430. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_1;
  3431. device block_q4_1 * dst_data = (device block_q4_1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3432. for (int64_t i00 = tpitg.x*QK4_1; i00 < args.ne00; i00 += ntg.x*QK4_1) {
  3433. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  3434. float min = FLT_MAX;
  3435. float max = -FLT_MAX;
  3436. for (int j = 0; j < QK4_1; j++) {
  3437. const float v = src[j];
  3438. if (min > v) min = v;
  3439. if (max < v) max = v;
  3440. }
  3441. const float d = (max - min) / ((1 << 4) - 1);
  3442. const float id = d ? 1.0f/d : 0.0f;
  3443. dst_data[i00/QK4_1].d = d;
  3444. dst_data[i00/QK4_1].m = min;
  3445. for (int j = 0; j < QK4_1/2; ++j) {
  3446. const float x0 = (src[0 + j] - min)*id;
  3447. const float x1 = (src[QK4_1/2 + j] - min)*id;
  3448. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  3449. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  3450. dst_data[i00/QK4_1].qs[j] = xi0;
  3451. dst_data[i00/QK4_1].qs[j] |= xi1 << 4;
  3452. }
  3453. }
  3454. }
  3455. kernel void kernel_cpy_f32_q5_0(
  3456. constant ggml_metal_kargs_cpy & args,
  3457. device const char * src0,
  3458. device char * dst,
  3459. uint3 tgpig[[threadgroup_position_in_grid]],
  3460. ushort3 tpitg[[thread_position_in_threadgroup]],
  3461. ushort3 ntg[[threads_per_threadgroup]]) {
  3462. const int i03 = tgpig[2];
  3463. const int i02 = tgpig[1];
  3464. const int i01 = tgpig[0];
  3465. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  3466. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  3467. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  3468. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  3469. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK5_0;
  3470. device block_q5_0 * dst_data = (device block_q5_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3471. for (int64_t i00 = tpitg.x*QK5_0; i00 < args.ne00; i00 += ntg.x*QK5_0) {
  3472. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  3473. float amax = 0.0f; // absolute max
  3474. float max = 0.0f;
  3475. for (int j = 0; j < QK5_0; j++) {
  3476. const float v = src[j];
  3477. if (amax < fabs(v)) {
  3478. amax = fabs(v);
  3479. max = v;
  3480. }
  3481. }
  3482. const float d = max / -16;
  3483. const float id = d ? 1.0f/d : 0.0f;
  3484. dst_data[i00/QK5_0].d = d;
  3485. uint32_t qh = 0;
  3486. for (int j = 0; j < QK5_0/2; ++j) {
  3487. const float x0 = src[0 + j]*id;
  3488. const float x1 = src[QK5_0/2 + j]*id;
  3489. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  3490. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  3491. dst_data[i00/QK5_0].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  3492. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  3493. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  3494. }
  3495. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  3496. for (int j = 0; j < 4; ++j) {
  3497. dst_data[i00/QK5_0].qh[j] = qh8[j];
  3498. }
  3499. }
  3500. }
  3501. kernel void kernel_cpy_f32_q5_1(
  3502. constant ggml_metal_kargs_cpy & args,
  3503. device const char * src0,
  3504. device char * dst,
  3505. uint3 tgpig[[threadgroup_position_in_grid]],
  3506. ushort3 tpitg[[thread_position_in_threadgroup]],
  3507. ushort3 ntg[[threads_per_threadgroup]]) {
  3508. const int i03 = tgpig[2];
  3509. const int i02 = tgpig[1];
  3510. const int i01 = tgpig[0];
  3511. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  3512. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  3513. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  3514. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  3515. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK5_1;
  3516. device block_q5_1 * dst_data = (device block_q5_1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3517. for (int64_t i00 = tpitg.x*QK5_1; i00 < args.ne00; i00 += ntg.x*QK5_1) {
  3518. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  3519. float max = src[0];
  3520. float min = src[0];
  3521. for (int j = 1; j < QK5_1; j++) {
  3522. const float v = src[j];
  3523. min = v < min ? v : min;
  3524. max = v > max ? v : max;
  3525. }
  3526. const float d = (max - min) / 31;
  3527. const float id = d ? 1.0f/d : 0.0f;
  3528. dst_data[i00/QK5_1].d = d;
  3529. dst_data[i00/QK5_1].m = min;
  3530. uint32_t qh = 0;
  3531. for (int j = 0; j < QK5_1/2; ++j) {
  3532. const float x0 = (src[0 + j] - min)*id;
  3533. const float x1 = (src[QK5_1/2 + j] - min)*id;
  3534. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  3535. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  3536. dst_data[i00/QK5_1].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  3537. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  3538. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2);
  3539. }
  3540. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  3541. for (int j = 0; j < 4; ++j) {
  3542. dst_data[i00/QK5_1].qh[j] = qh8[j];
  3543. }
  3544. }
  3545. }
  3546. static inline int best_index_int8(int n, constant float * val, float x) {
  3547. if (x <= val[0]) return 0;
  3548. if (x >= val[n-1]) return n-1;
  3549. int ml = 0, mu = n-1;
  3550. while (mu-ml > 1) {
  3551. int mav = (ml+mu)/2;
  3552. if (x < val[mav]) mu = mav; else ml = mav;
  3553. }
  3554. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  3555. }
  3556. kernel void kernel_cpy_f32_iq4_nl(
  3557. constant ggml_metal_kargs_cpy & args,
  3558. device const char * src0,
  3559. device char * dst,
  3560. uint3 tgpig[[threadgroup_position_in_grid]],
  3561. ushort3 tpitg[[thread_position_in_threadgroup]],
  3562. ushort3 ntg[[threads_per_threadgroup]]) {
  3563. const int i03 = tgpig[2];
  3564. const int i02 = tgpig[1];
  3565. const int i01 = tgpig[0];
  3566. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  3567. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  3568. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  3569. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  3570. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_NL;
  3571. device block_iq4_nl * dst_data = (device block_iq4_nl *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3572. for (int64_t i00 = tpitg.x*QK4_NL; i00 < args.ne00; i00 += ntg.x*QK4_NL) {
  3573. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  3574. float amax = 0.0f; // absolute max
  3575. float max = 0.0f;
  3576. for (int j = 0; j < QK4_0; j++) {
  3577. const float v = src[j];
  3578. if (amax < fabs(v)) {
  3579. amax = fabs(v);
  3580. max = v;
  3581. }
  3582. }
  3583. const float d = max / kvalues_iq4nl_f[0];
  3584. const float id = d ? 1.0f/d : 0.0f;
  3585. float sumqx = 0, sumq2 = 0;
  3586. for (int j = 0; j < QK4_NL/2; ++j) {
  3587. const float x0 = src[0 + j]*id;
  3588. const float x1 = src[QK4_NL/2 + j]*id;
  3589. const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl_f, x0);
  3590. const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl_f, x1);
  3591. dst_data[i00/QK4_NL].qs[j] = xi0 | (xi1 << 4);
  3592. const float v0 = kvalues_iq4nl_f[xi0];
  3593. const float v1 = kvalues_iq4nl_f[xi1];
  3594. const float w0 = src[0 + j]*src[0 + j];
  3595. const float w1 = src[QK4_NL/2 + j]*src[QK4_NL/2 + j];
  3596. sumqx += w0*v0*src[j] + w1*v1*src[QK4_NL/2 + j];
  3597. sumq2 += w0*v0*v0 + w1*v1*v1;
  3598. }
  3599. dst_data[i00/QK4_NL].d = sumq2 > 0 ? sumqx/sumq2 : d;
  3600. }
  3601. }
  3602. kernel void kernel_concat(
  3603. constant ggml_metal_kargs_concat & args,
  3604. device const char * src0,
  3605. device const char * src1,
  3606. device char * dst,
  3607. uint3 tgpig[[threadgroup_position_in_grid]],
  3608. ushort3 tpitg[[thread_position_in_threadgroup]],
  3609. ushort3 ntg[[threads_per_threadgroup]]) {
  3610. const int i3 = tgpig.z;
  3611. const int i2 = tgpig.y;
  3612. const int i1 = tgpig.x;
  3613. int o[4] = {0, 0, 0, 0};
  3614. o[args.dim] = args.dim == 0 ? args.ne00 : (args.dim == 1 ? args.ne01 : (args.dim == 2 ? args.ne02 : args.ne03));
  3615. device const float * x;
  3616. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  3617. if (i0 < args.ne00 && i1 < args.ne01 && i2 < args.ne02 && i3 < args.ne03) {
  3618. x = (device const float *)(src0 + (i3 )*args.nb03 + (i2 )*args.nb02 + (i1 )*args.nb01 + (i0 )*args.nb00);
  3619. } else {
  3620. x = (device const float *)(src1 + (i3 - o[3])*args.nb13 + (i2 - o[2])*args.nb12 + (i1 - o[1])*args.nb11 + (i0 - o[0])*args.nb10);
  3621. }
  3622. device float * y = (device float *)(dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  3623. *y = *x;
  3624. }
  3625. }
  3626. template<typename args_t>
  3627. void kernel_mul_mv_q2_K_f32_impl(
  3628. args_t args,
  3629. device const char * src0,
  3630. device const char * src1,
  3631. device char * dst,
  3632. threadgroup char * shmem,
  3633. uint3 tgpig,
  3634. ushort tiisg,
  3635. ushort sgitg) {
  3636. const int nb = args.ne00/QK_K;
  3637. const int r0 = tgpig.x;
  3638. const int r1 = tgpig.y;
  3639. const int im = tgpig.z;
  3640. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3641. const uint i12 = im%args.ne12;
  3642. const uint i13 = im/args.ne12;
  3643. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3644. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3645. device const block_q2_K * x = (device const block_q2_K *) (src0 + offset0);
  3646. device const float * y = (device const float *) (src1 + offset1);
  3647. float yl[32];
  3648. float sumf[N_DST]={0.f}, all_sum;
  3649. const int ix = tiisg/8; // 0...3
  3650. const int it = tiisg%8; // 0...7
  3651. const int iq = it/4; // 0 or 1
  3652. const int ir = it%4; // 0...3
  3653. const int is = (8*ir)/16;// 0 or 1
  3654. device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir;
  3655. for (int ib = ix; ib < nb; ib += 4) {
  3656. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  3657. for (int i = 0; i < 8; ++i) {
  3658. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  3659. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  3660. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  3661. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  3662. }
  3663. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is;
  3664. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  3665. device const half * dh = &x[ib].d;
  3666. for (int row = 0; row < N_DST; row++) {
  3667. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  3668. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  3669. for (int i = 0; i < 8; i += 2) {
  3670. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  3671. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  3672. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  3673. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  3674. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  3675. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  3676. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  3677. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  3678. }
  3679. float dall = dh[0];
  3680. float dmin = dh[1] * 1.f/16.f;
  3681. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  3682. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  3683. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  3684. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  3685. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  3686. qs += args.nb01/2;
  3687. sc += args.nb01;
  3688. dh += args.nb01/2;
  3689. }
  3690. y4 += 4 * QK_K;
  3691. }
  3692. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  3693. for (int row = 0; row < N_DST; ++row) {
  3694. all_sum = simd_sum(sumf[row]);
  3695. if (tiisg == 0) {
  3696. dst_f32[first_row + row] = all_sum;
  3697. }
  3698. }
  3699. }
  3700. [[host_name("kernel_mul_mv_q2_K_f32")]]
  3701. kernel void kernel_mul_mv_q2_K_f32(
  3702. constant ggml_metal_kargs_mul_mv & args,
  3703. device const char * src0,
  3704. device const char * src1,
  3705. device char * dst,
  3706. uint3 tgpig[[threadgroup_position_in_grid]],
  3707. ushort tiisg[[thread_index_in_simdgroup]],
  3708. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3709. kernel_mul_mv_q2_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  3710. }
  3711. template<typename args_t>
  3712. void kernel_mul_mv_q3_K_f32_impl(
  3713. args_t args,
  3714. device const char * src0,
  3715. device const char * src1,
  3716. device char * dst,
  3717. threadgroup char * shmem,
  3718. uint3 tgpig,
  3719. ushort tiisg,
  3720. ushort sgitg) {
  3721. const int nb = args.ne00/QK_K;
  3722. const int r0 = tgpig.x;
  3723. const int r1 = tgpig.y;
  3724. const int im = tgpig.z;
  3725. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  3726. const uint i12 = im%args.ne12;
  3727. const uint i13 = im/args.ne12;
  3728. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3729. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3730. device const block_q3_K * x = (device const block_q3_K *) (src0 + offset0);
  3731. device const float * yy = (device const float *) (src1 + offset1);
  3732. float yl[32];
  3733. //const uint16_t kmask1 = 0x3030;
  3734. //const uint16_t kmask2 = 0x0f0f;
  3735. const int tid = tiisg/4;
  3736. const int ix = tiisg%4;
  3737. const int ip = tid/4; // 0 or 1
  3738. const int il = 2*((tid%4)/2); // 0 or 2
  3739. const int ir = tid%2;
  3740. const int n = 8;
  3741. const int l0 = n*ir;
  3742. // One would think that the Metal compiler would figure out that ip and il can only have
  3743. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  3744. // with these two tales.
  3745. //
  3746. // Possible masks for the high bit
  3747. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  3748. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  3749. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  3750. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  3751. // Possible masks for the low 2 bits
  3752. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  3753. const ushort4 hm = mm[2*ip + il/2];
  3754. const short shift = 2*il;
  3755. const float v1 = il == 0 ? 4.f : 64.f;
  3756. const float v2 = 4.f * v1;
  3757. const uint16_t s_shift1 = 4*ip;
  3758. const uint16_t s_shift2 = s_shift1 + il;
  3759. const int q_offset = 32*ip + l0;
  3760. const int y_offset = 128*ip + 32*il + l0;
  3761. device const float * y1 = yy + ix*QK_K + y_offset;
  3762. uint32_t scales32, aux32;
  3763. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  3764. thread const int8_t * scales = (thread const int8_t *)&scales32;
  3765. float sumf1[2] = {0.f};
  3766. float sumf2[2] = {0.f};
  3767. for (int i = ix; i < nb; i += 4) {
  3768. for (int l = 0; l < 8; ++l) {
  3769. yl[l+ 0] = y1[l+ 0];
  3770. yl[l+ 8] = y1[l+16];
  3771. yl[l+16] = y1[l+32];
  3772. yl[l+24] = y1[l+48];
  3773. }
  3774. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  3775. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  3776. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  3777. device const half * dh = &x[i].d;
  3778. for (int row = 0; row < 2; ++row) {
  3779. const float d_all = (float)dh[0];
  3780. scales16[0] = a[4];
  3781. scales16[1] = a[5];
  3782. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  3783. scales16[0] = a[il+0];
  3784. scales16[1] = a[il+1];
  3785. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  3786. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  3787. for (int l = 0; l < n; l += 2) {
  3788. const int32_t qs = q[l/2];
  3789. s1 += yl[l+0] * (qs & qm[il/2][0]);
  3790. s2 += yl[l+1] * (qs & qm[il/2][1]);
  3791. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  3792. s4 += yl[l+16] * (qs & qm[il/2][2]);
  3793. s5 += yl[l+17] * (qs & qm[il/2][3]);
  3794. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  3795. }
  3796. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  3797. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  3798. sumf1[row] += d1 * (scales[0] - 32);
  3799. sumf2[row] += d2 * (scales[2] - 32);
  3800. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  3801. for (int l = 0; l < n; l += 2) {
  3802. const int32_t qs = q[l/2+8];
  3803. s1 += yl[l+8] * (qs & qm[il/2][0]);
  3804. s2 += yl[l+9] * (qs & qm[il/2][1]);
  3805. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  3806. s4 += yl[l+24] * (qs & qm[il/2][2]);
  3807. s5 += yl[l+25] * (qs & qm[il/2][3]);
  3808. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  3809. }
  3810. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  3811. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  3812. sumf1[row] += d1 * (scales[1] - 32);
  3813. sumf2[row] += d2 * (scales[3] - 32);
  3814. q += args.nb01/2;
  3815. h += args.nb01/2;
  3816. a += args.nb01/2;
  3817. dh += args.nb01/2;
  3818. }
  3819. y1 += 4 * QK_K;
  3820. }
  3821. for (int row = 0; row < 2; ++row) {
  3822. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  3823. sumf1[row] = simd_sum(sumf);
  3824. }
  3825. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  3826. if (tiisg == 0) {
  3827. for (int row = 0; row < 2; ++row) {
  3828. dst_f32[first_row + row] = sumf1[row];
  3829. }
  3830. }
  3831. }
  3832. [[host_name("kernel_mul_mv_q3_K_f32")]]
  3833. kernel void kernel_mul_mv_q3_K_f32(
  3834. constant ggml_metal_kargs_mul_mv & args,
  3835. device const char * src0,
  3836. device const char * src1,
  3837. device char * dst,
  3838. uint3 tgpig[[threadgroup_position_in_grid]],
  3839. ushort tiisg[[thread_index_in_simdgroup]],
  3840. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3841. kernel_mul_mv_q3_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  3842. }
  3843. template<typename args_t>
  3844. void kernel_mul_mv_q4_K_f32_impl(
  3845. args_t args,
  3846. device const char * src0,
  3847. device const char * src1,
  3848. device char * dst,
  3849. threadgroup char * shmem,
  3850. uint3 tgpig,
  3851. ushort tiisg,
  3852. ushort sgitg) {
  3853. const uint16_t kmask1 = 0x3f3f;
  3854. const uint16_t kmask2 = 0x0f0f;
  3855. const uint16_t kmask3 = 0xc0c0;
  3856. const int ix = tiisg/8; // 0...3
  3857. const int it = tiisg%8; // 0...7
  3858. const int iq = it/4; // 0 or 1
  3859. const int ir = it%4; // 0...3
  3860. const int nb = args.ne00/QK_K;
  3861. const int r0 = tgpig.x;
  3862. const int r1 = tgpig.y;
  3863. const int im = tgpig.z;
  3864. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3865. const int first_row = r0 * N_DST;
  3866. const uint i12 = im%args.ne12;
  3867. const uint i13 = im/args.ne12;
  3868. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3869. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3870. device const block_q4_K * x = (device const block_q4_K *) (src0 + offset0);
  3871. device const float * y = (device const float *) (src1 + offset1);
  3872. float yl[16];
  3873. float yh[16];
  3874. float sumf[N_DST]={0.f}, all_sum;
  3875. device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
  3876. uint16_t sc16[4];
  3877. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  3878. for (int ib = ix; ib < nb; ib += 4) {
  3879. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  3880. for (int i = 0; i < 8; ++i) {
  3881. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  3882. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  3883. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  3884. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  3885. }
  3886. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq;
  3887. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  3888. device const half * dh = &x[ib].d;
  3889. for (int row = 0; row < N_DST; row++) {
  3890. sc16[0] = sc[0] & kmask1;
  3891. sc16[1] = sc[2] & kmask1;
  3892. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  3893. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  3894. device const uint16_t * q2 = q1 + 32;
  3895. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  3896. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  3897. for (int i = 0; i < 8; i += 2) {
  3898. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  3899. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  3900. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  3901. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  3902. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  3903. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  3904. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  3905. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  3906. }
  3907. float dall = dh[0];
  3908. float dmin = dh[1];
  3909. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  3910. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  3911. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  3912. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  3913. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  3914. q1 += args.nb01/2;
  3915. sc += args.nb01/2;
  3916. dh += args.nb01/2;
  3917. }
  3918. y4 += 4 * QK_K;
  3919. }
  3920. device float * dst_f32 = (device float *) dst + (int64_t)im*args.ne0*args.ne1 + (int64_t)r1*args.ne0;
  3921. for (int row = 0; row < N_DST; ++row) {
  3922. all_sum = simd_sum(sumf[row]);
  3923. if (tiisg == 0) {
  3924. dst_f32[first_row + row] = all_sum;
  3925. }
  3926. }
  3927. }
  3928. [[host_name("kernel_mul_mv_q4_K_f32")]]
  3929. kernel void kernel_mul_mv_q4_K_f32(
  3930. constant ggml_metal_kargs_mul_mv & args,
  3931. device const char * src0,
  3932. device const char * src1,
  3933. device char * dst,
  3934. uint3 tgpig[[threadgroup_position_in_grid]],
  3935. ushort tiisg[[thread_index_in_simdgroup]],
  3936. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3937. kernel_mul_mv_q4_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  3938. }
  3939. template<typename args_t>
  3940. void kernel_mul_mv_q5_K_f32_impl(
  3941. args_t args,
  3942. device const char * src0,
  3943. device const char * src1,
  3944. device char * dst,
  3945. threadgroup char * shmem,
  3946. uint3 tgpig,
  3947. ushort tiisg,
  3948. ushort sgitg) {
  3949. const int nb = args.ne00/QK_K;
  3950. const int r0 = tgpig.x;
  3951. const int r1 = tgpig.y;
  3952. const int im = tgpig.z;
  3953. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  3954. const uint i12 = im%args.ne12;
  3955. const uint i13 = im/args.ne12;
  3956. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3957. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3958. device const block_q5_K * x = (device const block_q5_K *) (src0 + offset0);
  3959. device const float * yy = (device const float *) (src1 + offset1);
  3960. float sumf[2]={0.f};
  3961. float yl[16], yh[16];
  3962. const uint16_t kmask1 = 0x3f3f;
  3963. const uint16_t kmask2 = 0x0f0f;
  3964. const uint16_t kmask3 = 0xc0c0;
  3965. const int tid = tiisg/4;
  3966. const int ix = tiisg%4;
  3967. const int iq = tid/4;
  3968. const int ir = tid%4;
  3969. const int n = 8;
  3970. const int l0 = n*ir;
  3971. const int q_offset = 32*iq + l0;
  3972. const int y_offset = 64*iq + l0;
  3973. const uint8_t hm1 = 1u << (2*iq);
  3974. const uint8_t hm2 = hm1 << 1;
  3975. const uint8_t hm3 = hm1 << 4;
  3976. const uint8_t hm4 = hm2 << 4;
  3977. uint16_t sc16[4];
  3978. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  3979. device const float * y1 = yy + ix*QK_K + y_offset;
  3980. for (int i = ix; i < nb; i += 4) {
  3981. device const uint8_t * q1 = x[i].qs + q_offset;
  3982. device const uint8_t * qh = x[i].qh + l0;
  3983. device const half * dh = &x[i].d;
  3984. device const uint16_t * a = (device const uint16_t *)x[i].scales + iq;
  3985. device const float * y2 = y1 + 128;
  3986. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  3987. for (int l = 0; l < 8; ++l) {
  3988. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  3989. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  3990. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  3991. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  3992. }
  3993. for (int row = 0; row < 2; ++row) {
  3994. device const uint8_t * q2 = q1 + 64;
  3995. sc16[0] = a[0] & kmask1;
  3996. sc16[1] = a[2] & kmask1;
  3997. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  3998. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  3999. float4 acc1 = {0.f};
  4000. float4 acc2 = {0.f};
  4001. for (int l = 0; l < n; ++l) {
  4002. uint8_t h = qh[l];
  4003. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  4004. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  4005. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  4006. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  4007. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  4008. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  4009. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  4010. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  4011. }
  4012. const float dall = dh[0];
  4013. const float dmin = dh[1];
  4014. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  4015. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  4016. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  4017. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  4018. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  4019. q1 += args.nb01;
  4020. qh += args.nb01;
  4021. dh += args.nb01/2;
  4022. a += args.nb01/2;
  4023. }
  4024. y1 += 4 * QK_K;
  4025. }
  4026. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4027. for (int row = 0; row < 2; ++row) {
  4028. const float tot = simd_sum(sumf[row]);
  4029. if (tiisg == 0) {
  4030. dst_f32[first_row + row] = tot;
  4031. }
  4032. }
  4033. }
  4034. [[host_name("kernel_mul_mv_q5_K_f32")]]
  4035. kernel void kernel_mul_mv_q5_K_f32(
  4036. constant ggml_metal_kargs_mul_mv & args,
  4037. device const char * src0,
  4038. device const char * src1,
  4039. device char * dst,
  4040. uint3 tgpig[[threadgroup_position_in_grid]],
  4041. ushort tiisg[[thread_index_in_simdgroup]],
  4042. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4043. kernel_mul_mv_q5_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  4044. }
  4045. template <typename args_t>
  4046. void kernel_mul_mv_q6_K_f32_impl(
  4047. args_t args,
  4048. device const char * src0,
  4049. device const char * src1,
  4050. device char * dst,
  4051. threadgroup char * shmem,
  4052. uint3 tgpig,
  4053. ushort tiisg,
  4054. ushort sgitg) {
  4055. const uint8_t kmask1 = 0x03;
  4056. const uint8_t kmask2 = 0x0C;
  4057. const uint8_t kmask3 = 0x30;
  4058. const uint8_t kmask4 = 0xC0;
  4059. const int nb = args.ne00/QK_K;
  4060. const int r0 = tgpig.x;
  4061. const int r1 = tgpig.y;
  4062. const int im = tgpig.z;
  4063. const int row = 2*r0 + sgitg;
  4064. const uint i12 = im%args.ne12;
  4065. const uint i13 = im/args.ne12;
  4066. const uint64_t offset0 = row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4067. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4068. device const block_q6_K * x = (device const block_q6_K *) (src0 + offset0);
  4069. device const float * yy = (device const float *) (src1 + offset1);
  4070. float sumf = 0;
  4071. const int tid = tiisg/2;
  4072. const int ix = tiisg%2;
  4073. const int ip = tid/8; // 0 or 1
  4074. const int il = tid%8;
  4075. const int n = 4;
  4076. const int l0 = n*il;
  4077. const int is = 8*ip + l0/16;
  4078. const int y_offset = 128*ip + l0;
  4079. const int q_offset_l = 64*ip + l0;
  4080. const int q_offset_h = 32*ip + l0;
  4081. for (int i = ix; i < nb; i += 2) {
  4082. device const uint8_t * q1 = x[i].ql + q_offset_l;
  4083. device const uint8_t * q2 = q1 + 32;
  4084. device const uint8_t * qh = x[i].qh + q_offset_h;
  4085. device const int8_t * sc = x[i].scales + is;
  4086. device const float * y = yy + i * QK_K + y_offset;
  4087. const float dall = x[i].d;
  4088. float4 sums = {0.f, 0.f, 0.f, 0.f};
  4089. for (int l = 0; l < n; ++l) {
  4090. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  4091. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  4092. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  4093. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  4094. }
  4095. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  4096. }
  4097. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4098. const float tot = simd_sum(sumf);
  4099. if (tiisg == 0) {
  4100. dst_f32[row] = tot;
  4101. }
  4102. }
  4103. [[host_name("kernel_mul_mv_q6_K_f32")]]
  4104. kernel void kernel_mul_mv_q6_K_f32(
  4105. constant ggml_metal_kargs_mul_mv & args,
  4106. device const char * src0,
  4107. device const char * src1,
  4108. device char * dst,
  4109. uint3 tgpig[[threadgroup_position_in_grid]],
  4110. ushort tiisg[[thread_index_in_simdgroup]],
  4111. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4112. kernel_mul_mv_q6_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  4113. }
  4114. // ======================= "True" 2-bit
  4115. template<typename args_t>
  4116. void kernel_mul_mv_iq2_xxs_f32_impl(
  4117. args_t args,
  4118. device const char * src0,
  4119. device const char * src1,
  4120. device char * dst,
  4121. threadgroup char * shmem,
  4122. uint3 tgpig,
  4123. ushort tiisg,
  4124. ushort sgitg) {
  4125. const int nb = args.ne00/QK_K;
  4126. const int r0 = tgpig.x;
  4127. const int r1 = tgpig.y;
  4128. const int im = tgpig.z;
  4129. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4130. const uint i12 = im%args.ne12;
  4131. const uint i13 = im/args.ne12;
  4132. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4133. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4134. device const block_iq2_xxs * x = (device const block_iq2_xxs *) (src0 + offset0);
  4135. device const float * y = (device const float *) (src1 + offset1);
  4136. float yl[32];
  4137. float sumf[N_DST]={0.f}, all_sum;
  4138. const int nb32 = nb * (QK_K / 32);
  4139. threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem);
  4140. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256);
  4141. {
  4142. int nval = 4;
  4143. int pos = (32*sgitg + tiisg)*nval;
  4144. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xxs_grid[pos + i];
  4145. nval = 2;
  4146. pos = (32*sgitg + tiisg)*nval;
  4147. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  4148. threadgroup_barrier(mem_flags::mem_threadgroup);
  4149. }
  4150. const int ix = tiisg;
  4151. device const float * y4 = y + 32 * ix;
  4152. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4153. for (int i = 0; i < 32; ++i) {
  4154. yl[i] = y4[i];
  4155. }
  4156. const int ibl = ib32 / (QK_K / 32);
  4157. const int ib = ib32 % (QK_K / 32);
  4158. device const block_iq2_xxs * xr = x + ibl;
  4159. device const uint16_t * q2 = xr->qs + 4 * ib;
  4160. device const half * dh = &xr->d;
  4161. for (int row = 0; row < N_DST; row++) {
  4162. const float db = dh[0];
  4163. device const uint8_t * aux8 = (device const uint8_t *)q2;
  4164. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  4165. const float d = db * (0.5f + (aux32 >> 28));
  4166. float sum = 0;
  4167. for (int l = 0; l < 4; ++l) {
  4168. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + aux8[l]);
  4169. const uint8_t signs = ssigns[(aux32 >> 7*l) & 127];
  4170. for (int j = 0; j < 8; ++j) {
  4171. sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4172. }
  4173. }
  4174. sumf[row] += d * sum;
  4175. dh += args.nb01/2;
  4176. q2 += args.nb01/2;
  4177. }
  4178. y4 += 32 * 32;
  4179. }
  4180. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4181. for (int row = 0; row < N_DST; ++row) {
  4182. all_sum = simd_sum(sumf[row]);
  4183. if (tiisg == 0) {
  4184. dst_f32[first_row + row] = all_sum * 0.25f;
  4185. }
  4186. }
  4187. }
  4188. [[host_name("kernel_mul_mv_iq2_xxs_f32")]]
  4189. kernel void kernel_mul_mv_iq2_xxs_f32(
  4190. constant ggml_metal_kargs_mul_mv & args,
  4191. device const char * src0,
  4192. device const char * src1,
  4193. device char * dst,
  4194. threadgroup char * shmem [[threadgroup(0)]],
  4195. uint3 tgpig[[threadgroup_position_in_grid]],
  4196. ushort tiisg[[thread_index_in_simdgroup]],
  4197. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4198. kernel_mul_mv_iq2_xxs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  4199. }
  4200. template<typename args_t>
  4201. void kernel_mul_mv_iq2_xs_f32_impl(
  4202. args_t args,
  4203. device const char * src0,
  4204. device const char * src1,
  4205. device char * dst,
  4206. threadgroup char * shmem,
  4207. uint3 tgpig,
  4208. ushort tiisg,
  4209. ushort sgitg) {
  4210. const int nb = args.ne00/QK_K;
  4211. const int r0 = tgpig.x;
  4212. const int r1 = tgpig.y;
  4213. const int im = tgpig.z;
  4214. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4215. const uint i12 = im%args.ne12;
  4216. const uint i13 = im/args.ne12;
  4217. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4218. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4219. device const block_iq2_xs * x = (device const block_iq2_xs *) (src0 + offset0);
  4220. device const float * y = (device const float *) (src1 + offset1);
  4221. float yl[32];
  4222. float sumf[N_DST]={0.f}, all_sum;
  4223. const int nb32 = nb * (QK_K / 32);
  4224. threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem);
  4225. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 512);
  4226. {
  4227. int nval = 8;
  4228. int pos = (32*sgitg + tiisg)*nval;
  4229. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xs_grid[pos + i];
  4230. nval = 2;
  4231. pos = (32*sgitg + tiisg)*nval;
  4232. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  4233. threadgroup_barrier(mem_flags::mem_threadgroup);
  4234. }
  4235. const int ix = tiisg;
  4236. device const float * y4 = y + 32 * ix;
  4237. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4238. for (int i = 0; i < 32; ++i) {
  4239. yl[i] = y4[i];
  4240. }
  4241. const int ibl = ib32 / (QK_K / 32);
  4242. const int ib = ib32 % (QK_K / 32);
  4243. device const block_iq2_xs * xr = x + ibl;
  4244. device const uint16_t * q2 = xr->qs + 4 * ib;
  4245. device const uint8_t * sc = xr->scales + ib;
  4246. device const half * dh = &xr->d;
  4247. for (int row = 0; row < N_DST; row++) {
  4248. const float db = dh[0];
  4249. const uint8_t ls1 = sc[0] & 0xf;
  4250. const uint8_t ls2 = sc[0] >> 4;
  4251. const float d1 = db * (0.5f + ls1);
  4252. const float d2 = db * (0.5f + ls2);
  4253. float sum1 = 0, sum2 = 0;
  4254. for (int l = 0; l < 2; ++l) {
  4255. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511));
  4256. const uint8_t signs = ssigns[(q2[l] >> 9)];
  4257. for (int j = 0; j < 8; ++j) {
  4258. sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4259. }
  4260. }
  4261. for (int l = 2; l < 4; ++l) {
  4262. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511));
  4263. const uint8_t signs = ssigns[(q2[l] >> 9)];
  4264. for (int j = 0; j < 8; ++j) {
  4265. sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4266. }
  4267. }
  4268. sumf[row] += d1 * sum1 + d2 * sum2;
  4269. dh += args.nb01/2;
  4270. q2 += args.nb01/2;
  4271. sc += args.nb01;
  4272. }
  4273. y4 += 32 * 32;
  4274. }
  4275. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4276. for (int row = 0; row < N_DST; ++row) {
  4277. all_sum = simd_sum(sumf[row]);
  4278. if (tiisg == 0) {
  4279. dst_f32[first_row + row] = all_sum * 0.25f;
  4280. }
  4281. }
  4282. }
  4283. [[host_name("kernel_mul_mv_iq2_xs_f32")]]
  4284. kernel void kernel_mul_mv_iq2_xs_f32(
  4285. constant ggml_metal_kargs_mul_mv & args,
  4286. device const char * src0,
  4287. device const char * src1,
  4288. device char * dst,
  4289. threadgroup char * shmem [[threadgroup(0)]],
  4290. uint3 tgpig[[threadgroup_position_in_grid]],
  4291. ushort tiisg[[thread_index_in_simdgroup]],
  4292. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4293. kernel_mul_mv_iq2_xs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  4294. }
  4295. template <typename args_t>
  4296. void kernel_mul_mv_iq3_xxs_f32_impl(
  4297. args_t args,
  4298. device const char * src0,
  4299. device const char * src1,
  4300. device char * dst,
  4301. threadgroup char * shmem,
  4302. uint3 tgpig,
  4303. ushort tiisg,
  4304. ushort sgitg) {
  4305. const int nb = args.ne00/QK_K;
  4306. const int r0 = tgpig.x;
  4307. const int r1 = tgpig.y;
  4308. const int im = tgpig.z;
  4309. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4310. const uint i12 = im%args.ne12;
  4311. const uint i13 = im/args.ne12;
  4312. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4313. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4314. device const block_iq3_xxs * x = (device const block_iq3_xxs *) (src0 + offset0);
  4315. device const float * y = (device const float *) (src1 + offset1);
  4316. float yl[32];
  4317. float sumf[N_DST]={0.f}, all_sum;
  4318. const int nb32 = nb * (QK_K / 32);
  4319. threadgroup uint32_t * svalues = (threadgroup uint32_t *)(shmem);
  4320. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256);
  4321. {
  4322. int nval = 4;
  4323. int pos = (32*sgitg + tiisg)*nval;
  4324. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3xxs_grid[pos + i];
  4325. nval = 2;
  4326. pos = (32*sgitg + tiisg)*nval;
  4327. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  4328. threadgroup_barrier(mem_flags::mem_threadgroup);
  4329. }
  4330. const int ix = tiisg;
  4331. device const float * y4 = y + 32 * ix;
  4332. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4333. for (int i = 0; i < 32; ++i) {
  4334. yl[i] = y4[i];
  4335. }
  4336. const int ibl = ib32 / (QK_K / 32);
  4337. const int ib = ib32 % (QK_K / 32);
  4338. device const block_iq3_xxs * xr = x + ibl;
  4339. device const uint8_t * q3 = xr->qs + 8 * ib;
  4340. device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib;
  4341. device const half * dh = &xr->d;
  4342. for (int row = 0; row < N_DST; row++) {
  4343. const float db = dh[0];
  4344. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4345. const float d = db * (0.5f + (aux32 >> 28));
  4346. float2 sum = {0};
  4347. for (int l = 0; l < 4; ++l) {
  4348. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + q3[2*l+0]);
  4349. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + q3[2*l+1]);
  4350. const uint8_t signs = ssigns[(aux32 >> 7*l) & 127];
  4351. for (int j = 0; j < 4; ++j) {
  4352. sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4353. sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4354. }
  4355. }
  4356. sumf[row] += d * (sum[0] + sum[1]);
  4357. dh += args.nb01/2;
  4358. q3 += args.nb01;
  4359. gas += args.nb01/2;
  4360. }
  4361. y4 += 32 * 32;
  4362. }
  4363. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4364. for (int row = 0; row < N_DST; ++row) {
  4365. all_sum = simd_sum(sumf[row]);
  4366. if (tiisg == 0) {
  4367. dst_f32[first_row + row] = all_sum * 0.5f;
  4368. }
  4369. }
  4370. }
  4371. [[host_name("kernel_mul_mv_iq3_xxs_f32")]]
  4372. kernel void kernel_mul_mv_iq3_xxs_f32(
  4373. constant ggml_metal_kargs_mul_mv & args,
  4374. device const char * src0,
  4375. device const char * src1,
  4376. device char * dst,
  4377. threadgroup char * shmem [[threadgroup(0)]],
  4378. uint3 tgpig[[threadgroup_position_in_grid]],
  4379. ushort tiisg[[thread_index_in_simdgroup]],
  4380. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4381. kernel_mul_mv_iq3_xxs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  4382. }
  4383. template<typename args_t>
  4384. void kernel_mul_mv_iq3_s_f32_impl(
  4385. args_t args,
  4386. device const char * src0,
  4387. device const char * src1,
  4388. device char * dst,
  4389. threadgroup char * shmem,
  4390. uint3 tgpig,
  4391. ushort tiisg,
  4392. ushort sgitg) {
  4393. const int nb = args.ne00/QK_K;
  4394. const int r0 = tgpig.x;
  4395. const int r1 = tgpig.y;
  4396. const int im = tgpig.z;
  4397. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4398. const uint i12 = im%args.ne12;
  4399. const uint i13 = im/args.ne12;
  4400. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4401. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4402. device const block_iq3_s * x = (device const block_iq3_s *) (src0 + offset0);
  4403. device const float * y = (device const float *) (src1 + offset1);
  4404. float yl[32];
  4405. float sumf[N_DST]={0.f}, all_sum;
  4406. const int nb32 = nb * (QK_K / 32);
  4407. threadgroup uint32_t * svalues = (threadgroup uint32_t *) shmem;
  4408. {
  4409. int nval = 8;
  4410. int pos = (32*sgitg + tiisg)*nval;
  4411. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3s_grid[pos + i];
  4412. threadgroup_barrier(mem_flags::mem_threadgroup);
  4413. }
  4414. const int ix = tiisg;
  4415. device const float * y4 = y + 32 * ix;
  4416. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4417. for (int i = 0; i < 32; ++i) {
  4418. yl[i] = y4[i];
  4419. }
  4420. const int ibl = ib32 / (QK_K / 32);
  4421. const int ib = ib32 % (QK_K / 32);
  4422. device const block_iq3_s * xr = x + ibl;
  4423. device const uint8_t * qs = xr->qs + 8 * ib;
  4424. device const uint8_t * qh = xr->qh + ib;
  4425. device const uint8_t * sc = xr->scales + (ib/2);
  4426. device const uint8_t * signs = xr->signs + 4 * ib;
  4427. device const half * dh = &xr->d;
  4428. for (int row = 0; row < N_DST; row++) {
  4429. const float db = dh[0];
  4430. const float d = db * (1 + 2*((sc[0] >> 4*(ib%2)) & 0xf));
  4431. float2 sum = {0};
  4432. for (int l = 0; l < 4; ++l) {
  4433. const threadgroup uint32_t * table1 = qh[0] & kmask_iq2xs[2*l+0] ? svalues + 256 : svalues;
  4434. const threadgroup uint32_t * table2 = qh[0] & kmask_iq2xs[2*l+1] ? svalues + 256 : svalues;
  4435. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(table1 + qs[2*l+0]);
  4436. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(table2 + qs[2*l+1]);
  4437. for (int j = 0; j < 4; ++j) {
  4438. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]);
  4439. sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]);
  4440. }
  4441. }
  4442. sumf[row] += d * (sum[0] + sum[1]);
  4443. dh += args.nb01/2;
  4444. qs += args.nb01;
  4445. qh += args.nb01;
  4446. sc += args.nb01;
  4447. signs += args.nb01;
  4448. }
  4449. y4 += 32 * 32;
  4450. }
  4451. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4452. for (int row = 0; row < N_DST; ++row) {
  4453. all_sum = simd_sum(sumf[row]);
  4454. if (tiisg == 0) {
  4455. dst_f32[first_row + row] = all_sum;
  4456. }
  4457. }
  4458. }
  4459. [[host_name("kernel_mul_mv_iq3_s_f32")]]
  4460. kernel void kernel_mul_mv_iq3_s_f32(
  4461. constant ggml_metal_kargs_mul_mv & args,
  4462. device const char * src0,
  4463. device const char * src1,
  4464. device char * dst,
  4465. threadgroup char * shmem [[threadgroup(0)]],
  4466. uint3 tgpig[[threadgroup_position_in_grid]],
  4467. ushort tiisg[[thread_index_in_simdgroup]],
  4468. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4469. kernel_mul_mv_iq3_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  4470. }
  4471. template <typename args_t>
  4472. void kernel_mul_mv_iq2_s_f32_impl(
  4473. args_t args,
  4474. device const char * src0,
  4475. device const char * src1,
  4476. device char * dst,
  4477. threadgroup char * shmem,
  4478. uint3 tgpig,
  4479. ushort tiisg,
  4480. ushort sgitg) {
  4481. const int nb = args.ne00/QK_K;
  4482. const int r0 = tgpig.x;
  4483. const int r1 = tgpig.y;
  4484. const int im = tgpig.z;
  4485. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4486. const uint i12 = im%args.ne12;
  4487. const uint i13 = im/args.ne12;
  4488. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4489. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4490. device const block_iq2_s * x = (device const block_iq2_s *) (src0 + offset0);
  4491. device const float * y = (device const float *) (src1 + offset1);
  4492. float yl[32];
  4493. float sumf[N_DST]={0.f}, all_sum;
  4494. const int nb32 = nb * (QK_K / 32);
  4495. //threadgroup uint64_t * svalues = (threadgroup uint64_t *) shmem;
  4496. //{
  4497. // int nval = 32;
  4498. // int pos = (32*sgitg + tiisg)*nval;
  4499. // for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2s_grid[pos + i];
  4500. // threadgroup_barrier(mem_flags::mem_threadgroup);
  4501. //}
  4502. const int ix = tiisg;
  4503. device const float * y4 = y + 32 * ix;
  4504. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4505. for (int i = 0; i < 32; ++i) {
  4506. yl[i] = y4[i];
  4507. }
  4508. const int ibl = ib32 / (QK_K / 32);
  4509. const int ib = ib32 % (QK_K / 32);
  4510. device const block_iq2_s * xr = x + ibl;
  4511. device const uint8_t * qs = xr->qs + 4 * ib;
  4512. device const uint8_t * qh = xr->qh + ib;
  4513. device const uint8_t * sc = xr->scales + ib;
  4514. device const uint8_t * signs = qs + QK_K/8;
  4515. device const half * dh = &xr->d;
  4516. for (int row = 0; row < N_DST; row++) {
  4517. const float db = dh[0];
  4518. const float d1 = db * (0.5f + (sc[0] & 0xf));
  4519. const float d2 = db * (0.5f + (sc[0] >> 4));
  4520. float2 sum = {0};
  4521. for (int l = 0; l < 2; ++l) {
  4522. //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4523. //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4524. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4525. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4526. for (int j = 0; j < 8; ++j) {
  4527. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]);
  4528. sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]);
  4529. }
  4530. }
  4531. sumf[row] += d1 * sum[0] + d2 * sum[1];
  4532. dh += args.nb01/2;
  4533. qs += args.nb01;
  4534. qh += args.nb01;
  4535. sc += args.nb01;
  4536. signs += args.nb01;
  4537. }
  4538. y4 += 32 * 32;
  4539. }
  4540. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4541. for (int row = 0; row < N_DST; ++row) {
  4542. all_sum = simd_sum(sumf[row]);
  4543. if (tiisg == 0) {
  4544. dst_f32[first_row + row] = all_sum * 0.25f;
  4545. }
  4546. }
  4547. }
  4548. [[host_name("kernel_mul_mv_iq2_s_f32")]]
  4549. kernel void kernel_mul_mv_iq2_s_f32(
  4550. constant ggml_metal_kargs_mul_mv & args,
  4551. device const char * src0,
  4552. device const char * src1,
  4553. device char * dst,
  4554. threadgroup char * shmem [[threadgroup(0)]],
  4555. uint3 tgpig[[threadgroup_position_in_grid]],
  4556. ushort tiisg[[thread_index_in_simdgroup]],
  4557. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4558. kernel_mul_mv_iq2_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  4559. }
  4560. template<typename args_t>
  4561. void kernel_mul_mv_iq1_s_f32_impl(
  4562. args_t args,
  4563. device const char * src0,
  4564. device const char * src1,
  4565. device char * dst,
  4566. threadgroup char * shmem,
  4567. uint3 tgpig,
  4568. ushort tiisg,
  4569. ushort sgitg) {
  4570. const int nb = args.ne00/QK_K;
  4571. const int r0 = tgpig.x;
  4572. const int r1 = tgpig.y;
  4573. const int im = tgpig.z;
  4574. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4575. const uint i12 = im%args.ne12;
  4576. const uint i13 = im/args.ne12;
  4577. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4578. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4579. device const block_iq1_s * x = (device const block_iq1_s *) (src0 + offset0);
  4580. device const float * y = (device const float *) (src1 + offset1);
  4581. float yl[32];
  4582. float sumf[N_DST]={0.f}, all_sum;
  4583. const int nb32 = nb * (QK_K / 32);
  4584. const int ix = tiisg;
  4585. device const float * y4 = y + 32 * ix;
  4586. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4587. float sumy = 0;
  4588. for (int i = 0; i < 32; ++i) {
  4589. yl[i] = y4[i];
  4590. sumy += yl[i];
  4591. }
  4592. const int ibl = ib32 / (QK_K / 32);
  4593. const int ib = ib32 % (QK_K / 32);
  4594. device const block_iq1_s * xr = x + ibl;
  4595. device const uint8_t * qs = xr->qs + 4 * ib;
  4596. device const uint16_t * qh = xr->qh + ib;
  4597. device const half * dh = &xr->d;
  4598. for (int row = 0; row < N_DST; row++) {
  4599. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  4600. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 5) & 0x700)));
  4601. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[0] << 2) & 0x700)));
  4602. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[0] >> 1) & 0x700)));
  4603. float sum = 0;
  4604. for (int j = 0; j < 4; ++j) {
  4605. sum += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  4606. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4)
  4607. + yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  4608. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  4609. }
  4610. sumf[row] += (float)dh[0] * (sum + sumy * (qh[0] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA)) * (2*((qh[0] >> 12) & 7) + 1);
  4611. dh += args.nb01/2;
  4612. qs += args.nb01;
  4613. qh += args.nb01/2;
  4614. }
  4615. y4 += 32 * 32;
  4616. }
  4617. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4618. for (int row = 0; row < N_DST; ++row) {
  4619. all_sum = simd_sum(sumf[row]);
  4620. if (tiisg == 0) {
  4621. dst_f32[first_row + row] = all_sum;
  4622. }
  4623. }
  4624. }
  4625. template <typename args_t>
  4626. void kernel_mul_mv_iq1_m_f32_impl(
  4627. args_t args,
  4628. device const char * src0,
  4629. device const char * src1,
  4630. device char * dst,
  4631. threadgroup char * shmem,
  4632. uint3 tgpig,
  4633. ushort tiisg,
  4634. ushort sgitg) {
  4635. const int nb = args.ne00/QK_K;
  4636. const int r0 = tgpig.x;
  4637. const int r1 = tgpig.y;
  4638. const int im = tgpig.z;
  4639. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4640. const uint i12 = im%args.ne12;
  4641. const uint i13 = im/args.ne12;
  4642. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4643. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4644. device const block_iq1_m * x = (device const block_iq1_m *) (src0 + offset0);
  4645. device const float * y = (device const float *) (src1 + offset1);
  4646. float yl[32];
  4647. float sumf[N_DST]={0.f}, all_sum;
  4648. const int nb32 = nb * (QK_K / 32);
  4649. const int ix = tiisg;
  4650. device const float * y4 = y + 32 * ix;
  4651. iq1m_scale_t scale;
  4652. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4653. float4 sumy = {0.f};
  4654. for (int i = 0; i < 8; ++i) {
  4655. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  4656. yl[i+ 8] = y4[i+ 8]; sumy[1] += yl[i+ 8];
  4657. yl[i+16] = y4[i+16]; sumy[2] += yl[i+16];
  4658. yl[i+24] = y4[i+24]; sumy[3] += yl[i+24];
  4659. }
  4660. const int ibl = ib32 / (QK_K / 32);
  4661. const int ib = ib32 % (QK_K / 32);
  4662. device const block_iq1_m * xr = x + ibl;
  4663. device const uint8_t * qs = xr->qs + 4 * ib;
  4664. device const uint8_t * qh = xr->qh + 2 * ib;
  4665. device const uint16_t * sc = (device const uint16_t *)xr->scales;
  4666. for (int row = 0; row < N_DST; row++) {
  4667. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4668. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  4669. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  4670. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[1] << 8) & 0x700)));
  4671. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[1] << 4) & 0x700)));
  4672. float2 sum = {0.f};
  4673. for (int j = 0; j < 4; ++j) {
  4674. sum[0] += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  4675. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4);
  4676. sum[1] += yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  4677. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  4678. }
  4679. const float delta1 = sumy[0] * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA) + sumy[1] * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  4680. const float delta2 = sumy[2] * (qh[1] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA) + sumy[3] * (qh[1] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  4681. sumf[row] += (float)scale.f16 * ((sum[0] + delta1) * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 7) + 1) +
  4682. (sum[1] + delta2) * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 7) + 1));
  4683. sc += args.nb01/2;
  4684. qs += args.nb01;
  4685. qh += args.nb01;
  4686. }
  4687. y4 += 32 * 32;
  4688. }
  4689. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4690. for (int row = 0; row < N_DST; ++row) {
  4691. all_sum = simd_sum(sumf[row]);
  4692. if (tiisg == 0) {
  4693. dst_f32[first_row + row] = all_sum;
  4694. }
  4695. }
  4696. }
  4697. template<typename args_t>
  4698. void kernel_mul_mv_iq4_nl_f32_impl(
  4699. args_t args,
  4700. device const char * src0,
  4701. device const char * src1,
  4702. device char * dst,
  4703. threadgroup char * shmem,
  4704. uint3 tgpig,
  4705. ushort tiisg,
  4706. ushort sgitg) {
  4707. threadgroup float * shmem_f32 = (threadgroup float *) shmem;
  4708. const int nb = args.ne00/QK4_NL;
  4709. const int r0 = tgpig.x;
  4710. const int r1 = tgpig.y;
  4711. const int im = tgpig.z;
  4712. const int first_row = (r0 * 2 + sgitg) * 2;
  4713. const uint i12 = im%args.ne12;
  4714. const uint i13 = im/args.ne12;
  4715. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4716. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4717. device const block_iq4_nl * x = (device const block_iq4_nl *) (src0 + offset0);
  4718. device const float * y = (device const float *) (src1 + offset1);
  4719. const int ix = tiisg/2; // 0...15
  4720. const int it = tiisg%2; // 0 or 1
  4721. shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4722. threadgroup_barrier(mem_flags::mem_threadgroup);
  4723. float4 yl[4];
  4724. float sumf[2]={0.f}, all_sum;
  4725. device const float * yb = y + ix * QK4_NL + it * 8;
  4726. uint32_t aux32[2];
  4727. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4728. float4 qf1, qf2;
  4729. for (int ib = ix; ib < nb; ib += 16) {
  4730. device const float4 * y4 = (device const float4 *)yb;
  4731. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4732. for (int row = 0; row < 2 && first_row + row < args.ne01; ++row) {
  4733. device const block_iq4_nl & xb = x[row*nb + ib];
  4734. device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it);
  4735. float4 acc1 = {0.f}, acc2 = {0.f};
  4736. aux32[0] = q4[0] | (q4[1] << 16);
  4737. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4738. aux32[0] &= 0x0f0f0f0f;
  4739. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  4740. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  4741. acc1 += yl[0] * qf1;
  4742. acc2 += yl[1] * qf2;
  4743. aux32[0] = q4[2] | (q4[3] << 16);
  4744. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4745. aux32[0] &= 0x0f0f0f0f;
  4746. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  4747. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  4748. acc1 += yl[2] * qf1;
  4749. acc2 += yl[3] * qf2;
  4750. acc1 += acc2;
  4751. sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4752. }
  4753. yb += 16 * QK4_NL;
  4754. }
  4755. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4756. for (int row = 0; row < 2 && first_row + row < args.ne01; ++row) {
  4757. all_sum = simd_sum(sumf[row]);
  4758. if (tiisg == 0) {
  4759. dst_f32[first_row + row] = all_sum;
  4760. }
  4761. }
  4762. }
  4763. template<typename args_t>
  4764. void kernel_mul_mv_iq4_xs_f32_impl(
  4765. args_t args,
  4766. device const char * src0,
  4767. device const char * src1,
  4768. device char * dst,
  4769. threadgroup char * shmem,
  4770. uint3 tgpig,
  4771. ushort tiisg,
  4772. ushort sgitg) {
  4773. threadgroup float * shmem_f32 = (threadgroup float *) shmem;
  4774. const int nb = args.ne00/QK_K;
  4775. const int r0 = tgpig.x;
  4776. const int r1 = tgpig.y;
  4777. const int im = tgpig.z;
  4778. const int first_row = (r0 * 2 + sgitg) * 2;
  4779. const uint i12 = im%args.ne12;
  4780. const uint i13 = im/args.ne12;
  4781. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4782. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4783. device const block_iq4_xs * x = (device const block_iq4_xs *) (src0 + offset0);
  4784. device const float * y = (device const float *) (src1 + offset1);
  4785. const int ix = tiisg/16; // 0 or 1
  4786. const int it = tiisg%16; // 0...15
  4787. const int ib = it/2;
  4788. const int il = it%2;
  4789. shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4790. threadgroup_barrier(mem_flags::mem_threadgroup);
  4791. float4 yl[4];
  4792. float sumf[2]={0.f}, all_sum;
  4793. device const float * yb = y + ix * QK_K + ib * 32 + il * 8;
  4794. uint32_t aux32[2];
  4795. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4796. float4 qf1, qf2;
  4797. for (int ibl = ix; ibl < nb; ibl += 2) {
  4798. device const float4 * y4 = (device const float4 *)yb;
  4799. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4800. for (int row = 0; row < 2; ++row) {
  4801. device const block_iq4_xs & xb = x[row*nb + ibl];
  4802. device const uint32_t * q4 = (device const uint32_t *)(xb.qs + 16*ib + 8*il);
  4803. float4 acc1 = {0.f}, acc2 = {0.f};
  4804. aux32[0] = (q4[0] ) & 0x0f0f0f0f;
  4805. aux32[1] = (q4[0] >> 4) & 0x0f0f0f0f;
  4806. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  4807. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  4808. acc1 += yl[0] * qf1;
  4809. acc2 += yl[1] * qf2;
  4810. aux32[0] = (q4[1] ) & 0x0f0f0f0f;
  4811. aux32[1] = (q4[1] >> 4) & 0x0f0f0f0f;
  4812. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  4813. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  4814. acc1 += yl[2] * qf1;
  4815. acc2 += yl[3] * qf2;
  4816. acc1 += acc2;
  4817. const int ls = (((xb.scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((xb.scales_h >> 2*ib) & 3) << 4)) - 32;
  4818. sumf[row] += (float)xb.d * ls * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4819. }
  4820. yb += 2 * QK_K;
  4821. }
  4822. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4823. for (int row = 0; row < 2; ++row) {
  4824. all_sum = simd_sum(sumf[row]);
  4825. if (tiisg == 0) {
  4826. dst_f32[first_row + row] = all_sum;
  4827. }
  4828. }
  4829. }
  4830. [[host_name("kernel_mul_mv_iq1_s_f32")]]
  4831. kernel void kernel_mul_mv_iq1_s_f32(
  4832. constant ggml_metal_kargs_mul_mv & args,
  4833. device const char * src0,
  4834. device const char * src1,
  4835. device char * dst,
  4836. uint3 tgpig[[threadgroup_position_in_grid]],
  4837. ushort tiisg[[thread_index_in_simdgroup]],
  4838. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4839. kernel_mul_mv_iq1_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  4840. }
  4841. [[host_name("kernel_mul_mv_iq1_m_f32")]]
  4842. kernel void kernel_mul_mv_iq1_m_f32(
  4843. constant ggml_metal_kargs_mul_mv & args,
  4844. device const char * src0,
  4845. device const char * src1,
  4846. device char * dst,
  4847. uint3 tgpig[[threadgroup_position_in_grid]],
  4848. ushort tiisg[[thread_index_in_simdgroup]],
  4849. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4850. kernel_mul_mv_iq1_m_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  4851. }
  4852. [[host_name("kernel_mul_mv_iq4_nl_f32")]]
  4853. kernel void kernel_mul_mv_iq4_nl_f32(
  4854. constant ggml_metal_kargs_mul_mv & args,
  4855. device const char * src0,
  4856. device const char * src1,
  4857. device char * dst,
  4858. threadgroup char * shmem [[threadgroup(0)]],
  4859. uint3 tgpig[[threadgroup_position_in_grid]],
  4860. ushort tiisg[[thread_index_in_simdgroup]],
  4861. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4862. kernel_mul_mv_iq4_nl_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  4863. }
  4864. [[host_name("kernel_mul_mv_iq4_xs_f32")]]
  4865. kernel void kernel_mul_mv_iq4_xs_f32(
  4866. constant ggml_metal_kargs_mul_mv & args,
  4867. device const char * src0,
  4868. device const char * src1,
  4869. device char * dst,
  4870. threadgroup char * shmem [[threadgroup(0)]],
  4871. uint3 tgpig[[threadgroup_position_in_grid]],
  4872. ushort tiisg[[thread_index_in_simdgroup]],
  4873. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4874. kernel_mul_mv_iq4_xs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  4875. }
  4876. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  4877. kernel void kernel_get_rows_q(
  4878. device const void * src0,
  4879. device const void * src1,
  4880. device float * dst,
  4881. constant int64_t & ne00,
  4882. constant uint64_t & nb01,
  4883. constant uint64_t & nb02,
  4884. constant int64_t & ne10,
  4885. constant uint64_t & nb10,
  4886. constant uint64_t & nb11,
  4887. constant uint64_t & nb1,
  4888. constant uint64_t & nb2,
  4889. uint3 tgpig[[threadgroup_position_in_grid]],
  4890. uint tiitg[[thread_index_in_threadgroup]],
  4891. uint3 tptg [[threads_per_threadgroup]]) {
  4892. const int64_t i10 = tgpig.x;
  4893. const int64_t i11 = tgpig.y;
  4894. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  4895. const int64_t i02 = i11;
  4896. for (int64_t ind = tiitg; ind < ne00/16; ind += tptg.x) {
  4897. float4x4 temp;
  4898. dequantize_func(((device const block_q *) ((const device char *) src0 + r*nb01 + i02*nb02)) + ind/nl, ind%nl, temp);
  4899. *(((device float4x4 *) ((device char *) dst + i11*nb2 + i10*nb1)) + ind) = temp;
  4900. }
  4901. }
  4902. template<typename T>
  4903. kernel void kernel_get_rows_f(
  4904. device const void * src0,
  4905. device const void * src1,
  4906. device float * dst,
  4907. constant int64_t & ne00,
  4908. constant uint64_t & nb01,
  4909. constant uint64_t & nb02,
  4910. constant int64_t & ne10,
  4911. constant uint64_t & nb10,
  4912. constant uint64_t & nb11,
  4913. constant uint64_t & nb1,
  4914. constant uint64_t & nb2,
  4915. uint3 tgpig[[threadgroup_position_in_grid]],
  4916. uint tiitg[[thread_index_in_threadgroup]],
  4917. uint3 tptg [[threads_per_threadgroup]]) {
  4918. const int64_t i10 = tgpig.x;
  4919. const int64_t i11 = tgpig.y;
  4920. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  4921. const int64_t i02 = i11;
  4922. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4923. (( device float *) (( device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4924. ((const device T *) ((const device char *) src0 + i02*nb02 + r*nb01))[ind];
  4925. }
  4926. }
  4927. kernel void kernel_get_rows_i32(
  4928. device const void * src0,
  4929. device const void * src1,
  4930. device int32_t * dst,
  4931. constant int64_t & ne00,
  4932. constant uint64_t & nb01,
  4933. constant uint64_t & nb02,
  4934. constant int64_t & ne10,
  4935. constant uint64_t & nb10,
  4936. constant uint64_t & nb11,
  4937. constant uint64_t & nb1,
  4938. constant uint64_t & nb2,
  4939. uint3 tgpig[[threadgroup_position_in_grid]],
  4940. uint tiitg[[thread_index_in_threadgroup]],
  4941. uint3 tptg [[threads_per_threadgroup]]) {
  4942. const int64_t i10 = tgpig.x;
  4943. const int64_t i11 = tgpig.y;
  4944. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  4945. const int64_t i02 = i11;
  4946. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4947. (( device int32_t *) (( device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4948. ((const device int32_t *) ((const device char *) src0 + i02*nb02 + r*nb01))[ind];
  4949. }
  4950. }
  4951. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  4952. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  4953. #define BLOCK_SIZE_K 32
  4954. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  4955. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  4956. #define THREAD_PER_BLOCK 128
  4957. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  4958. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  4959. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  4960. #define SG_MAT_ROW 8
  4961. // each block_q contains 16*nl weights
  4962. template<typename T, typename T4x4, typename simdgroup_T8x8, typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread T4x4 &)>
  4963. kernel void kernel_mul_mm(
  4964. constant ggml_metal_kargs_mul_mm & args,
  4965. device const char * src0,
  4966. device const char * src1,
  4967. device char * dst,
  4968. threadgroup char * shmem [[threadgroup(0)]],
  4969. uint3 tgpig[[threadgroup_position_in_grid]],
  4970. ushort tiitg[[thread_index_in_threadgroup]],
  4971. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4972. threadgroup T * sa = (threadgroup T *)(shmem);
  4973. threadgroup float * sb = (threadgroup float *)(shmem + 4096);
  4974. const int r0 = tgpig.y;
  4975. const int r1 = tgpig.x;
  4976. const int im = tgpig.z;
  4977. // if this block is of 64x32 shape or smaller
  4978. const short n_rows = (args.ne0 - r0*BLOCK_SIZE_M < BLOCK_SIZE_M) ? (args.ne0 - r0*BLOCK_SIZE_M) : BLOCK_SIZE_M;
  4979. const short n_cols = (args.ne1 - r1*BLOCK_SIZE_N < BLOCK_SIZE_N) ? (args.ne1 - r1*BLOCK_SIZE_N) : BLOCK_SIZE_N;
  4980. // a thread shouldn't load data outside of the matrix
  4981. const short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  4982. const short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  4983. simdgroup_T8x8 ma[4];
  4984. simdgroup_float8x8 mb[2];
  4985. simdgroup_float8x8 mc[8];
  4986. for (short i = 0; i < 8; i++){
  4987. mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  4988. }
  4989. short il = (tiitg % THREAD_PER_ROW);
  4990. const int i12 = im%args.ne12;
  4991. const int i13 = im/args.ne12;
  4992. const uint64_t offset0 = (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4993. const short offset1 = il/nl;
  4994. device const block_q * x = (device const block_q *)(src0
  4995. + args.nb01*(r0*BLOCK_SIZE_M + thread_row) + offset0) + offset1;
  4996. device const float * y = (device const float *)(src1
  4997. + args.nb13*i13
  4998. + args.nb12*i12
  4999. + args.nb11*(r1*BLOCK_SIZE_N + thread_col)
  5000. + args.nb10*(BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5001. for (int loop_k = 0; loop_k < args.ne00; loop_k += BLOCK_SIZE_K) {
  5002. // load data and store to threadgroup memory
  5003. T4x4 temp_a;
  5004. dequantize_func(x, il, temp_a);
  5005. threadgroup_barrier(mem_flags::mem_threadgroup);
  5006. #pragma unroll(16)
  5007. for (short i = 0; i < 16; i++) {
  5008. *(sa + SG_MAT_SIZE * ((tiitg/THREAD_PER_ROW/8) \
  5009. + (tiitg%THREAD_PER_ROW)*16 + (i/8)*8) \
  5010. + (tiitg/THREAD_PER_ROW)%8 + (i&7)*8) = temp_a[i/4][i%4];
  5011. }
  5012. *(threadgroup float2x4 *)(sb + 32*8*(tiitg%THREAD_PER_COL) + 8*(tiitg/THREAD_PER_COL)) = *((device float2x4 *) y);
  5013. il = (il + 2 < nl) ? il + 2 : il % 2;
  5014. x = (il < 2) ? x + (2 + nl - 1)/nl : x;
  5015. y += BLOCK_SIZE_K;
  5016. threadgroup_barrier(mem_flags::mem_threadgroup);
  5017. // load matrices from threadgroup memory and conduct outer products
  5018. threadgroup const T * lsma = (sa + THREAD_MAT_M*SG_MAT_SIZE*(sgitg%2));
  5019. threadgroup const float * lsmb = (sb + THREAD_MAT_N*SG_MAT_SIZE*(sgitg/2));
  5020. #pragma unroll(4)
  5021. for (short ik = 0; ik < BLOCK_SIZE_K/8; ik++) {
  5022. #pragma unroll(4)
  5023. for (short i = 0; i < 4; i++) {
  5024. simdgroup_load(ma[i], lsma + SG_MAT_SIZE * i);
  5025. }
  5026. simdgroup_barrier(mem_flags::mem_none);
  5027. #pragma unroll(2)
  5028. for (short i = 0; i < 2; i++) {
  5029. simdgroup_load(mb[i], lsmb + SG_MAT_SIZE * i);
  5030. }
  5031. #pragma unroll(8)
  5032. for (short i = 0; i < 8; i++){
  5033. simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
  5034. }
  5035. lsma += (BLOCK_SIZE_M/SG_MAT_ROW)*SG_MAT_SIZE;
  5036. lsmb += (BLOCK_SIZE_N/SG_MAT_ROW)*SG_MAT_SIZE;
  5037. }
  5038. }
  5039. if ((r0 + 1) * BLOCK_SIZE_M <= args.ne0 && (r1 + 1) * BLOCK_SIZE_N <= args.ne1) {
  5040. device float * C = (device float *) dst +
  5041. (BLOCK_SIZE_M * r0 + 32*(sgitg & 1)) + \
  5042. (BLOCK_SIZE_N * r1 + 16*(sgitg >> 1)) * args.ne0 + im*args.ne1*args.ne0;
  5043. for (short i = 0; i < 8; i++) {
  5044. simdgroup_store(mc[i], C + 8 * (i%4) + 8 * args.ne0 * (i/4), args.ne0);
  5045. }
  5046. } else {
  5047. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  5048. threadgroup_barrier(mem_flags::mem_threadgroup);
  5049. threadgroup float * temp_str = ((threadgroup float *) shmem) \
  5050. + 32*(sgitg&1) + (16*(sgitg >> 1))*BLOCK_SIZE_M;
  5051. for (short i = 0; i < 8; i++) {
  5052. simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*BLOCK_SIZE_M*(i/4), BLOCK_SIZE_M);
  5053. }
  5054. threadgroup_barrier(mem_flags::mem_threadgroup);
  5055. if (sgitg == 0) {
  5056. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5057. device float * D = (device float *) dst + (r0*BLOCK_SIZE_M) + (r1*BLOCK_SIZE_N + j)*args.ne0 + im*args.ne1*args.ne0;
  5058. device float4 * D4 = (device float4 *) D;
  5059. threadgroup float * C = temp_str + (j*BLOCK_SIZE_M);
  5060. threadgroup float4 * C4 = (threadgroup float4 *) C;
  5061. int i = 0;
  5062. for (; i < n_rows/4; i++) {
  5063. *(D4 + i) = *(C4 + i);
  5064. }
  5065. i *= 4;
  5066. for (; i < n_rows; i++) {
  5067. *(D + i) = *(C + i);
  5068. }
  5069. }
  5070. }
  5071. }
  5072. }
  5073. // same as kernel_mul_mm_impl, but src1 and dst are accessed via indices stored in rowids
  5074. // TODO: this kernel needs to be reimplemented from scratch for better performance
  5075. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5076. void kernel_mul_mm_id_impl(
  5077. int32_t ne00,
  5078. int32_t ne02,
  5079. uint64_t nb01,
  5080. uint64_t nb02,
  5081. int32_t ne11,
  5082. int32_t ne12,
  5083. uint64_t nb10,
  5084. uint64_t nb11,
  5085. uint64_t nb12,
  5086. int32_t ne0,
  5087. int32_t ne1,
  5088. int64_t ne0ne1,
  5089. device const char * src0,
  5090. device const char * src1,
  5091. threadgroup ushort2 * rowids,
  5092. device char * dst,
  5093. threadgroup char * shmem,
  5094. uint3 tgpig[[threadgroup_position_in_grid]],
  5095. ushort tiitg[[thread_index_in_threadgroup]],
  5096. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5097. threadgroup half * sa = (threadgroup half *)(shmem);
  5098. threadgroup float * sb = (threadgroup float *)(shmem + 4096);
  5099. const int r0 = tgpig.y;
  5100. const int r1 = tgpig.x;
  5101. if (r1*BLOCK_SIZE_N >= ne1) return;
  5102. // if this block is of 64x32 shape or smaller
  5103. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5104. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5105. // a thread shouldn't load data outside of the matrix
  5106. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5107. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5108. simdgroup_half8x8 ma[4];
  5109. simdgroup_float8x8 mb[2];
  5110. simdgroup_float8x8 mc[8];
  5111. for (int i = 0; i < 8; i++){
  5112. mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5113. }
  5114. short il = (tiitg % THREAD_PER_ROW);
  5115. ushort offset1 = il/nl;
  5116. threadgroup const auto & id = rowids[r1 * BLOCK_SIZE_N + thread_col];
  5117. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01) + offset1;
  5118. device const float * y = (device const float *)(src1
  5119. + nb12 * id[1]
  5120. + nb11 * (id[0] % ne11)
  5121. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5122. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5123. // load data and store to threadgroup memory
  5124. half4x4 temp_a;
  5125. dequantize_func(x, il, temp_a);
  5126. threadgroup_barrier(mem_flags::mem_threadgroup);
  5127. for (int i = 0; i < 16; i++) {
  5128. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5129. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5130. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5131. }
  5132. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5133. il = (il + 2 < nl) ? il + 2 : il % 2;
  5134. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5135. y += BLOCK_SIZE_K;
  5136. threadgroup_barrier(mem_flags::mem_threadgroup);
  5137. // load matrices from threadgroup memory and conduct outer products
  5138. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5139. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5140. #pragma unroll(BLOCK_SIZE_K/8)
  5141. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5142. #pragma unroll(4)
  5143. for (int i = 0; i < 4; i++) {
  5144. simdgroup_load(ma[i], lsma + SG_MAT_SIZE * i);
  5145. }
  5146. simdgroup_barrier(mem_flags::mem_none);
  5147. #pragma unroll(2)
  5148. for (int i = 0; i < 2; i++) {
  5149. simdgroup_load(mb[i], lsmb + SG_MAT_SIZE * i);
  5150. }
  5151. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5152. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5153. #pragma unroll(8)
  5154. for (int i = 0; i < 8; i++){
  5155. simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
  5156. }
  5157. }
  5158. }
  5159. {
  5160. threadgroup_barrier(mem_flags::mem_threadgroup);
  5161. threadgroup float * temp_str = ((threadgroup float *) shmem) \
  5162. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5163. for (int i = 0; i < 8; i++) {
  5164. simdgroup_store(mc[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5165. }
  5166. threadgroup_barrier(mem_flags::mem_threadgroup);
  5167. if (sgitg == 0) {
  5168. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5169. threadgroup const auto & jid = rowids[r1 * BLOCK_SIZE_N + j];
  5170. int64_t joff = jid[0]*ne0 + jid[1]*ne0ne1;
  5171. device float * D = (device float *) dst + (r0*BLOCK_SIZE_M) + joff;
  5172. device float4 * D4 = (device float4 *) D;
  5173. threadgroup float * C = temp_str + (j*BLOCK_SIZE_M);
  5174. threadgroup float4 * C4 = (threadgroup float4 *) C;
  5175. int i = 0;
  5176. for (; i < n_rows/4; i++) {
  5177. *(D4 + i) = *(C4 + i);
  5178. }
  5179. i *= 4;
  5180. for (; i < n_rows; i++) {
  5181. *(D + i) = *(C + i);
  5182. }
  5183. }
  5184. }
  5185. }
  5186. }
  5187. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5188. kernel void kernel_mul_mm_id(
  5189. constant ggml_metal_kargs_mul_mm_id & args,
  5190. device const char * src0s,
  5191. device const char * src1,
  5192. device char * dst,
  5193. device const char * ids,
  5194. threadgroup char * shmem [[threadgroup(0)]],
  5195. uint3 tgpig[[threadgroup_position_in_grid]],
  5196. ushort tiitg[[thread_index_in_threadgroup]],
  5197. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5198. const int32_t i02 = tgpig.z;
  5199. tgpig.z = 0;
  5200. device const char * src0 = src0s + i02*args.nb02;
  5201. // row indices
  5202. threadgroup ushort2 * rowids = (threadgroup ushort2 *)(shmem + 8192);
  5203. // TODO: parallelize this loop
  5204. int32_t _ne1 = 0;
  5205. for (ushort ii1 = 0; ii1 < args.nei1; ii1++) {
  5206. for (ushort ii0 = 0; ii0 < args.nei0; ii0++) {
  5207. int32_t id = ((device int32_t *) (ids + ii1*args.nbi1))[ii0];
  5208. if (id == i02) {
  5209. if (tiitg == 0) {
  5210. rowids[_ne1] = ushort2(ii0, ii1);
  5211. }
  5212. _ne1++;
  5213. }
  5214. }
  5215. }
  5216. threadgroup_barrier(mem_flags::mem_threadgroup);
  5217. kernel_mul_mm_id_impl<block_q, nl, dequantize_func>(
  5218. args.ne00,
  5219. args.ne02,
  5220. args.nb01,
  5221. args.nb02,
  5222. args.ne11,
  5223. args.ne12,
  5224. args.nb10,
  5225. args.nb11,
  5226. args.nb12,
  5227. args.ne0,
  5228. _ne1,
  5229. (int64_t)args.ne0*args.ne1,
  5230. src0,
  5231. src1,
  5232. rowids,
  5233. dst,
  5234. shmem,
  5235. tgpig,
  5236. tiitg,
  5237. sgitg);
  5238. }
  5239. #define QK_NL 16
  5240. //
  5241. // get rows
  5242. //
  5243. typedef decltype(kernel_get_rows_f<float>) get_rows_f_t;
  5244. template [[host_name("kernel_get_rows_f32")]] kernel get_rows_f_t kernel_get_rows_f<float>;
  5245. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_f_t kernel_get_rows_f<half>;
  5246. #if defined(GGML_METAL_USE_BF16)
  5247. template [[host_name("kernel_get_rows_bf16")]] kernel get_rows_f_t kernel_get_rows_f<bfloat>;
  5248. #endif
  5249. typedef decltype(kernel_get_rows_q<block_q4_0, 2, dequantize_q4_0>) get_rows_q_t;
  5250. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q4_0, 2, dequantize_q4_0>;
  5251. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_q_t kernel_get_rows_q<block_q4_1, 2, dequantize_q4_1>;
  5252. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q5_0, 2, dequantize_q5_0>;
  5253. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_q_t kernel_get_rows_q<block_q5_1, 2, dequantize_q5_1>;
  5254. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q8_0, 2, dequantize_q8_0>;
  5255. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_q_t kernel_get_rows_q<block_q2_K, QK_NL, dequantize_q2_K>;
  5256. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_q_t kernel_get_rows_q<block_q3_K, QK_NL, dequantize_q3_K>;
  5257. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_q_t kernel_get_rows_q<block_q4_K, QK_NL, dequantize_q4_K>;
  5258. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_q_t kernel_get_rows_q<block_q5_K, QK_NL, dequantize_q5_K>;
  5259. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_q_t kernel_get_rows_q<block_q6_K, QK_NL, dequantize_q6_K>;
  5260. template [[host_name("kernel_get_rows_iq2_xxs")]] kernel get_rows_q_t kernel_get_rows_q<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5261. template [[host_name("kernel_get_rows_iq2_xs")]] kernel get_rows_q_t kernel_get_rows_q<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5262. template [[host_name("kernel_get_rows_iq3_xxs")]] kernel get_rows_q_t kernel_get_rows_q<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5263. template [[host_name("kernel_get_rows_iq3_s")]] kernel get_rows_q_t kernel_get_rows_q<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5264. template [[host_name("kernel_get_rows_iq2_s")]] kernel get_rows_q_t kernel_get_rows_q<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5265. template [[host_name("kernel_get_rows_iq1_s")]] kernel get_rows_q_t kernel_get_rows_q<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5266. template [[host_name("kernel_get_rows_iq1_m")]] kernel get_rows_q_t kernel_get_rows_q<block_iq1_m, QK_NL, dequantize_iq1_m>;
  5267. template [[host_name("kernel_get_rows_iq4_nl")]] kernel get_rows_q_t kernel_get_rows_q<block_iq4_nl, 2, dequantize_iq4_nl>;
  5268. template [[host_name("kernel_get_rows_iq4_xs")]] kernel get_rows_q_t kernel_get_rows_q<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5269. //
  5270. // matrix-matrix multiplication
  5271. //
  5272. typedef decltype(kernel_mul_mm<half, half4x4, simdgroup_half8x8, float4x4, 1, dequantize_f32>) mat_mm_t;
  5273. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, float4x4, 1, dequantize_f32>;
  5274. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half4x4, 1, dequantize_f16>;
  5275. #if defined(GGML_METAL_USE_BF16)
  5276. template [[host_name("kernel_mul_mm_bf16_f32")]] kernel mat_mm_t kernel_mul_mm<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat4x4, 1, dequantize_bf16>;
  5277. #endif
  5278. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0>;
  5279. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1>;
  5280. template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0>;
  5281. template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1>;
  5282. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0>;
  5283. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K>;
  5284. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K>;
  5285. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K>;
  5286. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K>;
  5287. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K>;
  5288. template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5289. template [[host_name("kernel_mul_mm_iq2_xs_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5290. template [[host_name("kernel_mul_mm_iq3_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5291. template [[host_name("kernel_mul_mm_iq3_s_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s>;
  5292. template [[host_name("kernel_mul_mm_iq2_s_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s>;
  5293. template [[host_name("kernel_mul_mm_iq1_s_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s>;
  5294. template [[host_name("kernel_mul_mm_iq1_m_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m>;
  5295. template [[host_name("kernel_mul_mm_iq4_nl_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl>;
  5296. template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5297. //
  5298. // indirect matrix-matrix multiplication
  5299. //
  5300. typedef decltype(kernel_mul_mm_id<float4x4, 1, dequantize_f32>) mat_mm_id_t;
  5301. template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<float4x4, 1, dequantize_f32>;
  5302. template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<half4x4, 1, dequantize_f16>;
  5303. #if defined(GGML_METAL_USE_BF16)
  5304. template [[host_name("kernel_mul_mm_id_bf16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<bfloat4x4, 1, dequantize_bf16>;
  5305. #endif
  5306. template [[host_name("kernel_mul_mm_id_q4_0_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q4_0, 2, dequantize_q4_0>;
  5307. template [[host_name("kernel_mul_mm_id_q4_1_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q4_1, 2, dequantize_q4_1>;
  5308. template [[host_name("kernel_mul_mm_id_q5_0_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q5_0, 2, dequantize_q5_0>;
  5309. template [[host_name("kernel_mul_mm_id_q5_1_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q5_1, 2, dequantize_q5_1>;
  5310. template [[host_name("kernel_mul_mm_id_q8_0_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q8_0, 2, dequantize_q8_0>;
  5311. template [[host_name("kernel_mul_mm_id_q2_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q2_K, QK_NL, dequantize_q2_K>;
  5312. template [[host_name("kernel_mul_mm_id_q3_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q3_K, QK_NL, dequantize_q3_K>;
  5313. template [[host_name("kernel_mul_mm_id_q4_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q4_K, QK_NL, dequantize_q4_K>;
  5314. template [[host_name("kernel_mul_mm_id_q5_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q5_K, QK_NL, dequantize_q5_K>;
  5315. template [[host_name("kernel_mul_mm_id_q6_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q6_K, QK_NL, dequantize_q6_K>;
  5316. template [[host_name("kernel_mul_mm_id_iq2_xxs_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5317. template [[host_name("kernel_mul_mm_id_iq2_xs_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5318. template [[host_name("kernel_mul_mm_id_iq3_xxs_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5319. template [[host_name("kernel_mul_mm_id_iq3_s_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5320. template [[host_name("kernel_mul_mm_id_iq2_s_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5321. template [[host_name("kernel_mul_mm_id_iq1_s_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5322. template [[host_name("kernel_mul_mm_id_iq1_m_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq1_m, QK_NL, dequantize_iq1_m>;
  5323. template [[host_name("kernel_mul_mm_id_iq4_nl_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq4_nl, 2, dequantize_iq4_nl>;
  5324. template [[host_name("kernel_mul_mm_id_iq4_xs_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5325. //
  5326. // matrix-vector multiplication
  5327. //
  5328. typedef void (kernel_mul_mv_impl_t)(
  5329. ggml_metal_kargs_mul_mv args,
  5330. device const char * src0,
  5331. device const char * src1,
  5332. device char * dst,
  5333. uint3 tgpig,
  5334. ushort tiisg);
  5335. typedef void (kernel_mul_mv2_impl_t)(
  5336. ggml_metal_kargs_mul_mv args,
  5337. device const char * src0,
  5338. device const char * src1,
  5339. device char * dst,
  5340. threadgroup char * shmem,
  5341. uint3 tgpig,
  5342. ushort tiisg,
  5343. ushort sgitg);
  5344. template<kernel_mul_mv_impl_t impl_fn>
  5345. void mmv_fn(
  5346. ggml_metal_kargs_mul_mv args,
  5347. device const char * src0,
  5348. device const char * src1,
  5349. device char * dst,
  5350. threadgroup char * shmem,
  5351. uint3 tgpig,
  5352. ushort tiitg,
  5353. ushort tiisg,
  5354. ushort sgitg) {
  5355. impl_fn(args, src0, src1, dst, tgpig, tiisg);
  5356. }
  5357. template<kernel_mul_mv2_impl_t impl_fn>
  5358. void mmv_fn(
  5359. ggml_metal_kargs_mul_mv args,
  5360. device const char * src0,
  5361. device const char * src1,
  5362. device char * dst,
  5363. threadgroup char * shmem,
  5364. uint3 tgpig,
  5365. ushort tiitg,
  5366. ushort tiisg,
  5367. ushort sgitg) {
  5368. impl_fn(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  5369. }
  5370. typedef decltype(mmv_fn<kernel_mul_mv_impl<half, half4, half, half4, ggml_metal_kargs_mul_mv>>) mul_mv_impl_fn_t;
  5371. template<mul_mv_impl_fn_t impl_fn>
  5372. kernel void kernel_mul_mv_id(
  5373. constant ggml_metal_kargs_mul_mv_id & args,
  5374. device const char * src0s,
  5375. device const char * src1,
  5376. device char * dst,
  5377. device const char * ids,
  5378. threadgroup char * shmem [[threadgroup(0)]],
  5379. uint3 tgpig[[threadgroup_position_in_grid]],
  5380. ushort tiitg[[thread_index_in_threadgroup]],
  5381. ushort tiisg[[thread_index_in_simdgroup]],
  5382. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5383. const int iid1 = tgpig.z/args.nei0;
  5384. const int idx = tgpig.z%args.nei0;
  5385. tgpig.z = 0;
  5386. const int32_t i02 = ((device const int32_t *) (ids + iid1*args.nbi1))[idx];
  5387. const int64_t i11 = idx % args.ne11;
  5388. const int64_t i12 = iid1;
  5389. const int64_t i1 = idx;
  5390. const int64_t i2 = i12;
  5391. device const char * src0_cur = src0s + i02*args.nb02;
  5392. device const char * src1_cur = src1 + i11*args.nb11 + i12*args.nb12;
  5393. device char * dst_cur = dst + (i1*args.ne0 + i2*args.ne1*args.ne0)*sizeof(float);
  5394. ggml_metal_kargs_mul_mv args0 = {
  5395. /*.ne00 =*/ args.ne00,
  5396. /*.ne01 =*/ args.ne01,
  5397. /*.ne02 =*/ 1, // args.ne02,
  5398. /*.nb00 =*/ args.nb00,
  5399. /*.nb01 =*/ args.nb01,
  5400. /*.nb02 =*/ args.nb02,
  5401. /*.nb03 =*/ args.nb02, // args.ne02 == 1
  5402. /*.ne10 =*/ args.ne10,
  5403. /*.ne11 =*/ 1, // args.ne11,
  5404. /*.ne12 =*/ 1, // args.ne12,
  5405. /*.nb10 =*/ args.nb10,
  5406. /*.nb11 =*/ args.nb11,
  5407. /*.nb12 =*/ args.nb12,
  5408. /*.nb13 =*/ args.nb12, // ne12 == 1
  5409. /*.ne0 =*/ args.ne0,
  5410. /*.ne1 =*/ 1, // args.ne1,
  5411. /*.r2 =*/ 1,
  5412. /*.r3 =*/ 1,
  5413. };
  5414. impl_fn(
  5415. args0,
  5416. /* src0 */ src0_cur,
  5417. /* src1 */ src1_cur,
  5418. /* dst */ dst_cur,
  5419. shmem,
  5420. tgpig,
  5421. tiitg,
  5422. tiisg,
  5423. sgitg);
  5424. }
  5425. typedef decltype(kernel_mul_mv_id<mmv_fn<kernel_mul_mv_impl<float, float4, float, float4>>>) kernel_mul_mv_id_t;
  5426. template [[host_name("kernel_mul_mv_id_f32_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_impl<float, float4, float, float4>>>;
  5427. template [[host_name("kernel_mul_mv_id_f16_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_impl<half, half4, float, float4>>>;
  5428. #if defined(GGML_METAL_USE_BF16)
  5429. template [[host_name("kernel_mul_mv_id_bf16_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_impl<bfloat, bfloat4, float, float4>>>;
  5430. #endif
  5431. template [[host_name("kernel_mul_mv_id_q8_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q8_0_f32_impl>>;
  5432. template [[host_name("kernel_mul_mv_id_q4_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<mul_vec_q_n_f32_impl<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>>>;
  5433. template [[host_name("kernel_mul_mv_id_q4_1_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<mul_vec_q_n_f32_impl<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>>>;
  5434. template [[host_name("kernel_mul_mv_id_q5_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<mul_vec_q_n_f32_impl<block_q5_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>>>;
  5435. template [[host_name("kernel_mul_mv_id_q5_1_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<mul_vec_q_n_f32_impl<block_q5_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>>>;
  5436. template [[host_name("kernel_mul_mv_id_q2_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q2_K_f32_impl>>;
  5437. template [[host_name("kernel_mul_mv_id_q3_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q3_K_f32_impl>>;
  5438. template [[host_name("kernel_mul_mv_id_q4_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q4_K_f32_impl>>;
  5439. template [[host_name("kernel_mul_mv_id_q5_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q5_K_f32_impl>>;
  5440. template [[host_name("kernel_mul_mv_id_q6_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q6_K_f32_impl>>;
  5441. template [[host_name("kernel_mul_mv_id_iq1_s_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq1_s_f32_impl>>;
  5442. template [[host_name("kernel_mul_mv_id_iq1_m_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq1_m_f32_impl>>;
  5443. template [[host_name("kernel_mul_mv_id_iq2_xxs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq2_xxs_f32_impl>>;
  5444. template [[host_name("kernel_mul_mv_id_iq2_xs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq2_xs_f32_impl>>;
  5445. template [[host_name("kernel_mul_mv_id_iq3_xxs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq3_xxs_f32_impl>>;
  5446. template [[host_name("kernel_mul_mv_id_iq3_s_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq3_s_f32_impl>>;
  5447. template [[host_name("kernel_mul_mv_id_iq2_s_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq2_s_f32_impl>>;
  5448. template [[host_name("kernel_mul_mv_id_iq4_nl_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq4_nl_f32_impl>>;
  5449. template [[host_name("kernel_mul_mv_id_iq4_xs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_iq4_xs_f32_impl>>;
  5450. kernel void kernel_pool_2d_max_f32(
  5451. device const float * src0,
  5452. device float * dst,
  5453. constant int32_t & k0,
  5454. constant int32_t & k1,
  5455. constant int32_t & s0,
  5456. constant int32_t & s1,
  5457. constant int32_t & p0,
  5458. constant int32_t & p1,
  5459. constant int64_t & IH,
  5460. constant int64_t & IW,
  5461. constant int64_t & OH,
  5462. constant int64_t & OW,
  5463. constant int64_t & parallel_elements,
  5464. uint gid[[thread_position_in_grid]]) {
  5465. if (gid >= parallel_elements) {
  5466. return;
  5467. }
  5468. const int idx = gid;
  5469. const int I_HW = IH * IW;
  5470. const int O_HW = OH * OW;
  5471. const int nc = idx / O_HW;
  5472. const int cur_oh = idx % O_HW / OW;
  5473. const int cur_ow = idx % O_HW % OW;
  5474. device const float * i_ptr = src0 + nc * I_HW;
  5475. device float * o_ptr = dst + nc * O_HW;
  5476. const int start_h = cur_oh * s1 - p1;
  5477. const int bh = MAX(0, start_h);
  5478. const int eh = MIN(IH, start_h + k1);
  5479. const int start_w = cur_ow * s0 - p0;
  5480. const int bw = MAX(0, start_w);
  5481. const int ew = MIN(IW, start_w + k0);
  5482. float res = -INFINITY;
  5483. for (int i = bh; i < eh; i += 1) {
  5484. for (int j = bw; j < ew; j += 1) {
  5485. res = MAX(res, i_ptr[i * IW + j]);
  5486. }
  5487. }
  5488. o_ptr[cur_oh * OW + cur_ow] = res;
  5489. }
  5490. kernel void kernel_pool_2d_avg_f32(
  5491. device const float * src0,
  5492. device float * dst,
  5493. constant int32_t & k0,
  5494. constant int32_t & k1,
  5495. constant int32_t & s0,
  5496. constant int32_t & s1,
  5497. constant int32_t & p0,
  5498. constant int32_t & p1,
  5499. constant int64_t & IH,
  5500. constant int64_t & IW,
  5501. constant int64_t & OH,
  5502. constant int64_t & OW,
  5503. constant int64_t & parallel_elements,
  5504. uint gid[[thread_position_in_grid]]) {
  5505. if (gid >= parallel_elements) {
  5506. return;
  5507. }
  5508. const int idx = gid;
  5509. const int I_HW = IH * IW;
  5510. const int O_HW = OH * OW;
  5511. const int nc = idx / O_HW;
  5512. const int cur_oh = idx % O_HW / OW;
  5513. const int cur_ow = idx % O_HW % OW;
  5514. device const float * i_ptr = src0 + nc * I_HW;
  5515. device float * o_ptr = dst + nc * O_HW;
  5516. const int start_h = cur_oh * s1 - p1;
  5517. const int bh = MAX(0, start_h);
  5518. const int eh = MIN(IH, start_h + k1);
  5519. const int start_w = cur_ow * s0 - p0;
  5520. const int bw = MAX(0, start_w);
  5521. const int ew = MIN(IW, start_w + k0);
  5522. // const float scale = 1. / ((eh - bh) * (ew - bw));
  5523. const float scale = 1. / (k0 * k1);
  5524. float res = 0;
  5525. for (int i = bh; i < eh; i += 1) {
  5526. for (int j = bw; j < ew; j += 1) {
  5527. float cur = i_ptr[i * IW + j];
  5528. res += cur * scale;
  5529. }
  5530. }
  5531. o_ptr[cur_oh * OW + cur_ow] = res;
  5532. }