ggml-metal-embed.metal 395 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906
  1. /**
  2. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - 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. /**
  30. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - do not edit this file
  31. *
  32. * MIT License
  33. *
  34. * Copyright (c) 2023-2024 The ggml authors
  35. *
  36. * Permission is hereby granted, free of charge, to any person obtaining a copy
  37. * of this software and associated documentation files (the "Software"), to deal
  38. * in the Software without restriction, including without limitation the rights
  39. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  40. * copies of the Software, and to permit persons to whom the Software is
  41. * furnished to do so, subject to the following conditions:
  42. *
  43. * The above copyright notice and this permission notice shall be included in all
  44. * copies or substantial portions of the Software.
  45. *
  46. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  47. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  48. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  49. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  50. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  51. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  52. * SOFTWARE.
  53. */
  54. #ifndef GGML_COMMON_DECL
  55. #if defined(GGML_COMMON_DECL_C)
  56. #include <stdint.h>
  57. typedef uint16_t ggml_half;
  58. typedef uint32_t ggml_half2;
  59. #define GGML_COMMON_AGGR
  60. #define GGML_COMMON_DECL
  61. #elif defined(GGML_COMMON_DECL_METAL)
  62. #include <metal_stdlib>
  63. typedef half ggml_half;
  64. typedef half2 ggml_half2;
  65. #define GGML_COMMON_AGGR
  66. #define GGML_COMMON_DECL
  67. #elif defined(GGML_COMMON_DECL_CUDA)
  68. #if defined(GGML_COMMON_DECL_MUSA)
  69. #include <musa_fp16.h>
  70. #else
  71. #include <cuda_fp16.h>
  72. #endif
  73. #include <cstdint>
  74. typedef half ggml_half;
  75. typedef half2 ggml_half2;
  76. #define GGML_COMMON_AGGR data
  77. #define GGML_COMMON_DECL
  78. #elif defined(GGML_COMMON_DECL_HIP)
  79. #include <hip/hip_fp16.h>
  80. #include <cstdint>
  81. typedef half ggml_half;
  82. typedef half2 ggml_half2;
  83. #define GGML_COMMON_AGGR data
  84. #define GGML_COMMON_DECL
  85. #elif defined(GGML_COMMON_DECL_SYCL)
  86. #include <sycl/half_type.hpp>
  87. #include <cstdint>
  88. typedef sycl::half ggml_half;
  89. typedef sycl::half2 ggml_half2;
  90. #define GGML_COMMON_AGGR data
  91. #define GGML_COMMON_DECL
  92. #endif
  93. #if defined(GGML_COMMON_DECL)
  94. #ifndef __cplusplus
  95. #ifndef static_assert
  96. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
  97. #define static_assert(cond, msg) _Static_assert(cond, msg)
  98. #else
  99. #define static_assert(cond, msg) struct global_scope_noop_trick
  100. #endif
  101. #endif
  102. #endif // __cplusplus
  103. // QK = number of values after dequantization
  104. // QK_K = super-block size
  105. #define QK_K 256
  106. #define K_SCALE_SIZE 12
  107. #if defined(GGML_COMMON_DECL_CUDA) || defined(GGML_COMMON_DECL_HIP) || defined(GGML_COMMON_DECL_SYCL)
  108. // QR = QK / number of values before dequantization
  109. // QI = number of 32 bit integers before dequantization
  110. #define QI4_0 (QK4_0 / (4 * QR4_0))
  111. #define QR4_0 2
  112. #define QI4_1 (QK4_1 / (4 * QR4_1))
  113. #define QR4_1 2
  114. #define QI5_0 (QK5_0 / (4 * QR5_0))
  115. #define QR5_0 2
  116. #define QI5_1 (QK5_1 / (4 * QR5_1))
  117. #define QR5_1 2
  118. #define QI8_0 (QK8_0 / (4 * QR8_0))
  119. #define QR8_0 1
  120. #define QI8_1 (QK8_1 / (4 * QR8_1))
  121. #define QR8_1 1
  122. #define QI2_K (QK_K / (4*QR2_K))
  123. #define QR2_K 4
  124. #define QI3_K (QK_K / (4*QR3_K))
  125. #define QR3_K 4
  126. #define QI4_K (QK_K / (4*QR4_K))
  127. #define QR4_K 2
  128. #define QI5_K (QK_K / (4*QR5_K))
  129. #define QR5_K 2
  130. #define QI6_K (QK_K / (4*QR6_K))
  131. #define QR6_K 2
  132. #define QI2_XXS (QK_K / (4*QR2_XXS))
  133. #define QR2_XXS 4
  134. #define QI2_XS (QK_K / (4*QR2_XS))
  135. #define QR2_XS 4
  136. #define QI2_S (QK_K / (4*QR2_S))
  137. #define QR2_S 4
  138. #define QI3_XXS (QK_K / (4*QR3_XXS))
  139. #define QR3_XXS 4
  140. #define QI3_XS (QK_K / (4*QR3_XS))
  141. #define QR3_XS 4
  142. #define QI1_S (QK_K / (4*QR1_S))
  143. #define QR1_S 8
  144. #define QI1_M (QK_K / (4*QR1_M))
  145. #define QR1_M 8
  146. #define QI4_NL (QK4_NL / (4*QR4_NL))
  147. #define QR4_NL 2
  148. #define QI4_XS (QK_K / (4*QR4_XS))
  149. #define QR4_XS 2
  150. #define QI3_S (QK_K / (4*QR3_S))
  151. #define QR3_S 4
  152. #endif // GGML_COMMON_DECL_CUDA || GGML_COMMON_DECL_HIP
  153. #define QK4_0 32
  154. typedef struct {
  155. ggml_half d; // delta
  156. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  157. } block_q4_0;
  158. static_assert(sizeof(block_q4_0) == sizeof(ggml_half) + QK4_0 / 2, "wrong q4_0 block size/padding");
  159. #define QK4_1 32
  160. typedef struct {
  161. union {
  162. struct {
  163. ggml_half d; // delta
  164. ggml_half m; // min
  165. } GGML_COMMON_AGGR;
  166. ggml_half2 dm;
  167. };
  168. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  169. } block_q4_1;
  170. static_assert(sizeof(block_q4_1) == 2 * sizeof(ggml_half) + QK4_1 / 2, "wrong q4_1 block size/padding");
  171. #define QK5_0 32
  172. typedef struct {
  173. ggml_half d; // delta
  174. uint8_t qh[4]; // 5-th bit of quants
  175. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  176. } block_q5_0;
  177. static_assert(sizeof(block_q5_0) == sizeof(ggml_half) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  178. #define QK5_1 32
  179. typedef struct {
  180. union {
  181. struct {
  182. ggml_half d; // delta
  183. ggml_half m; // min
  184. } GGML_COMMON_AGGR;
  185. ggml_half2 dm;
  186. };
  187. uint8_t qh[4]; // 5-th bit of quants
  188. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  189. } block_q5_1;
  190. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_half) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  191. #define QK8_0 32
  192. typedef struct {
  193. ggml_half d; // delta
  194. int8_t qs[QK8_0]; // quants
  195. } block_q8_0;
  196. static_assert(sizeof(block_q8_0) == sizeof(ggml_half) + QK8_0, "wrong q8_0 block size/padding");
  197. #define QK8_1 32
  198. typedef struct {
  199. union {
  200. struct {
  201. ggml_half d; // delta
  202. ggml_half s; // d * sum(qs[i])
  203. } GGML_COMMON_AGGR;
  204. ggml_half2 ds;
  205. };
  206. int8_t qs[QK8_1]; // quants
  207. } block_q8_1;
  208. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_half) + QK8_1, "wrong q8_1 block size/padding");
  209. typedef struct {
  210. ggml_half d[4]; // deltas for 4 q4_0 blocks
  211. uint8_t qs[QK4_0 * 2]; // nibbles / quants for 4 q4_0 blocks
  212. } block_q4_0x4;
  213. static_assert(sizeof(block_q4_0x4) == 4 * sizeof(ggml_half) + QK4_0 * 2, "wrong q4_0x4 block size/padding");
  214. typedef struct {
  215. ggml_half d[8]; // deltas for 8 q4_0 blocks
  216. uint8_t qs[QK4_0 * 4]; // nibbles / quants for 8 q4_0 blocks
  217. } block_q4_0x8;
  218. static_assert(sizeof(block_q4_0x8) == 8 * sizeof(ggml_half) + QK4_0 * 4, "wrong q4_0x8 block size/padding");
  219. typedef struct {
  220. ggml_half d[4]; // deltas for 4 q8_0 blocks
  221. int8_t qs[QK8_0 * 4]; // quants for 4 q8_0 blocks
  222. } block_q8_0x4;
  223. static_assert(sizeof(block_q8_0x4) == 4 * sizeof(ggml_half) + QK8_0 * 4, "wrong q8_0x4 block size/padding");
  224. typedef struct {
  225. ggml_half d[8]; // deltas for 8 q8_0 blocks
  226. int8_t qs[QK8_0 * 8]; // quants for 8 q8_0 blocks
  227. } block_q8_0x8;
  228. static_assert(sizeof(block_q8_0x8) == 8 * sizeof(ggml_half) + QK8_0 * 8, "wrong q8_0x8 block size/padding");
  229. //
  230. // Ternary quantization
  231. //
  232. // 1.6875 bpw
  233. typedef struct {
  234. uint8_t qs[(QK_K - 4 * QK_K / 64) / 5]; // 5 elements per byte (3^5 = 243 < 256)
  235. uint8_t qh[QK_K/64]; // 4 elements per byte
  236. ggml_half d;
  237. } block_tq1_0;
  238. static_assert(sizeof(block_tq1_0) == sizeof(ggml_half) + QK_K / 64 + (QK_K - 4 * QK_K / 64) / 5, "wrong tq1_0 block size/padding");
  239. // 2.0625 bpw
  240. typedef struct {
  241. uint8_t qs[QK_K/4]; // 2 bits per element
  242. ggml_half d;
  243. } block_tq2_0;
  244. static_assert(sizeof(block_tq2_0) == sizeof(ggml_half) + QK_K / 4, "wrong tq2_0 block size/padding");
  245. //
  246. // Super-block quantization structures
  247. //
  248. // 2-bit quantization
  249. // weight is represented as x = a * q + b
  250. // 16 blocks of 16 elements each
  251. // Effectively 2.625 bits per weight
  252. typedef struct {
  253. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  254. uint8_t qs[QK_K/4]; // quants
  255. union {
  256. struct {
  257. ggml_half d; // super-block scale for quantized scales
  258. ggml_half dmin; // super-block scale for quantized mins
  259. } GGML_COMMON_AGGR;
  260. ggml_half2 dm;
  261. };
  262. } block_q2_K;
  263. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_half) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  264. // 3-bit quantization
  265. // weight is represented as x = a * q
  266. // 16 blocks of 16 elements each
  267. // Effectively 3.4375 bits per weight
  268. typedef struct {
  269. uint8_t hmask[QK_K/8]; // quants - high bit
  270. uint8_t qs[QK_K/4]; // quants - low 2 bits
  271. uint8_t scales[12]; // scales, quantized with 6 bits
  272. ggml_half d; // super-block scale
  273. } block_q3_K;
  274. static_assert(sizeof(block_q3_K) == sizeof(ggml_half) + QK_K / 4 + QK_K / 8 + 12, "wrong q3_K block size/padding");
  275. // 4-bit quantization
  276. // 8 blocks of 32 elements each
  277. // weight is represented as x = a * q + b
  278. // Effectively 4.5 bits per weight
  279. typedef struct {
  280. union {
  281. struct {
  282. ggml_half d; // super-block scale for quantized scales
  283. ggml_half dmin; // super-block scale for quantized mins
  284. } GGML_COMMON_AGGR;
  285. ggml_half2 dm;
  286. };
  287. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  288. uint8_t qs[QK_K/2]; // 4--bit quants
  289. } block_q4_K;
  290. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2, "wrong q4_K block size/padding");
  291. // 5-bit quantization
  292. // 8 blocks of 32 elements each
  293. // weight is represented as x = a * q + b
  294. // Effectively 5.5 bits per weight
  295. typedef struct {
  296. union {
  297. struct {
  298. ggml_half d; // super-block scale for quantized scales
  299. ggml_half dmin; // super-block scale for quantized mins
  300. } GGML_COMMON_AGGR;
  301. ggml_half2 dm;
  302. };
  303. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  304. uint8_t qh[QK_K/8]; // quants, high bit
  305. uint8_t qs[QK_K/2]; // quants, low 4 bits
  306. } block_q5_K;
  307. static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2 + QK_K/8, "wrong q5_K block size/padding");
  308. // 6-bit quantization
  309. // weight is represented as x = a * q
  310. // 16 blocks of 16 elements each
  311. // Effectively 6.5625 bits per weight
  312. typedef struct {
  313. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  314. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  315. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  316. ggml_half d; // super-block scale
  317. } block_q6_K;
  318. static_assert(sizeof(block_q6_K) == sizeof(ggml_half) + QK_K / 16 + 3*QK_K/4, "wrong q6_K block size/padding");
  319. // This is only used for intermediate quantization and dot products
  320. typedef struct {
  321. float d; // delta
  322. int8_t qs[QK_K]; // quants
  323. int16_t bsums[QK_K/16]; // sum of quants in groups of 16
  324. } block_q8_K;
  325. static_assert(sizeof(block_q8_K) == sizeof(float) + QK_K + QK_K/16*sizeof(int16_t), "wrong q8_K block size/padding");
  326. // (Almost) "true" 2-bit quantization.
  327. // Due to the need to use blocks as per ggml design, it ends up using
  328. // 2.0625 bpw because of the 16-bit scale for each block of 256.
  329. typedef struct {
  330. ggml_half d;
  331. uint16_t qs[QK_K/8];
  332. } block_iq2_xxs;
  333. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  334. // 2.3125 bpw quants
  335. typedef struct {
  336. ggml_half d;
  337. uint16_t qs[QK_K/8];
  338. uint8_t scales[QK_K/32];
  339. } block_iq2_xs;
  340. static_assert(sizeof(block_iq2_xs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");
  341. // 2.5625 bpw quants
  342. typedef struct {
  343. ggml_half d;
  344. uint8_t qs[QK_K/4];
  345. uint8_t qh[QK_K/32];
  346. uint8_t scales[QK_K/32];
  347. } block_iq2_s;
  348. static_assert(sizeof(block_iq2_s) == sizeof(ggml_half) + QK_K/4 + QK_K/16, "wrong iq2_s block size/padding");
  349. // (Almost) "true" 3-bit quantization.
  350. // Due to the need to use blocks as per ggml design, it ends up using
  351. // 3.0625 bpw because of the 16-bit scale for each block of 256.
  352. typedef struct {
  353. ggml_half d;
  354. uint8_t qs[3*QK_K/8];
  355. } block_iq3_xxs;
  356. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_half) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  357. // 3.4375 bpw
  358. #define IQ3S_N_SCALE QK_K/64
  359. typedef struct {
  360. ggml_half d;
  361. uint8_t qs[QK_K/4];
  362. uint8_t qh[QK_K/32];
  363. uint8_t signs[QK_K/8];
  364. uint8_t scales[IQ3S_N_SCALE];
  365. } block_iq3_s;
  366. static_assert(sizeof(block_iq3_s) == sizeof(ggml_half) + 13*(QK_K/32) + IQ3S_N_SCALE, "wrong iq3_s block size/padding");
  367. // 1.5625 bpw
  368. typedef struct {
  369. ggml_half d;
  370. uint8_t qs[QK_K/8];
  371. uint16_t qh[QK_K/32];
  372. } block_iq1_s;
  373. static_assert(sizeof(block_iq1_s) == sizeof(ggml_half) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  374. // 1.75 bpw
  375. typedef struct {
  376. uint8_t qs[QK_K/8]; // grid index, low 8 bits
  377. uint8_t qh[QK_K/16]; // grid index, high 3 bits + grid shift bit (for two groups of 8)
  378. uint8_t scales[QK_K/32]; // 3-bit block scales (4-bit if QK_K == 64)
  379. } block_iq1_m;
  380. static_assert(sizeof(block_iq1_m) == QK_K/8 + QK_K/16 + QK_K/32, "wrong iq1_m block size/padding");
  381. // Used by IQ1_M quants
  382. typedef union {
  383. ggml_half f16;
  384. uint16_t u16;
  385. } iq1m_scale_t;
  386. // Non-linear quants
  387. #define QK4_NL 32
  388. typedef struct {
  389. ggml_half d;
  390. uint8_t qs[QK4_NL/2];
  391. } block_iq4_nl;
  392. static_assert(sizeof(block_iq4_nl) == sizeof(ggml_half) + QK4_NL/2, "wrong iq4_nl block size/padding");
  393. typedef struct {
  394. ggml_half d;
  395. uint16_t scales_h;
  396. uint8_t scales_l[QK_K/64];
  397. uint8_t qs[QK_K/2];
  398. } block_iq4_xs;
  399. static_assert(sizeof(block_iq4_xs) == sizeof(ggml_half) + sizeof(uint16_t) + QK_K/64 + QK_K/2, "wrong iq4_xs block size/padding");
  400. typedef struct {
  401. ggml_half d[4]; // deltas for 4 iq4_nl blocks
  402. uint8_t qs[QK4_NL * 2];// nibbles / quants for 4 iq4_nl blocks
  403. } block_iq4_nlx4;
  404. static_assert(sizeof(block_iq4_nlx4) == 4 * sizeof(ggml_half) + QK4_NL * 2, "wrong iq4_nlx4 block size/padding");
  405. #endif // GGML_COMMON_DECL
  406. #endif // GGML_COMMON_DECL
  407. ////////////////////////////////////////////////////////////////////////////////
  408. #ifndef GGML_COMMON_IMPL
  409. #if defined(GGML_COMMON_IMPL_C)
  410. #include <stdint.h>
  411. #define GGML_TABLE_BEGIN(type, name, size) static const type name[size] = {
  412. #define GGML_TABLE_END() };
  413. #define GGML_COMMON_IMPL
  414. #elif defined(GGML_COMMON_IMPL_METAL)
  415. #include <metal_stdlib>
  416. #define GGML_TABLE_BEGIN(type, name, size) static const constant type name[size] = {
  417. #define GGML_TABLE_END() };
  418. #define GGML_COMMON_IMPL
  419. #elif defined(GGML_COMMON_IMPL_CUDA) || defined(GGML_COMMON_IMPL_HIP) || defined(GGML_COMMON_IMPL_MUSA)
  420. #include <cstdint>
  421. #define GGML_TABLE_BEGIN(type, name, size) static const __device__ type name[size] = {
  422. #define GGML_TABLE_END() };
  423. #define GGML_COMMON_IMPL
  424. #elif defined(GGML_COMMON_IMPL_SYCL)
  425. #include <cstdint>
  426. #define GGML_TABLE_BEGIN(type, name, size) static const type name[size] = {
  427. #define GGML_TABLE_END() };
  428. #define GGML_COMMON_IMPL
  429. #endif
  430. #if defined(GGML_COMMON_IMPL)
  431. GGML_TABLE_BEGIN(uint8_t, kmask_iq2xs, 8)
  432. 1, 2, 4, 8, 16, 32, 64, 128
  433. GGML_TABLE_END()
  434. GGML_TABLE_BEGIN(uint8_t, ksigns_iq2xs, 128)
  435. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  436. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  437. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  438. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  439. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  440. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  441. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  442. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  443. GGML_TABLE_END()
  444. //#if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  445. GGML_TABLE_BEGIN(uint64_t, ksigns64, 128)
  446. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  447. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  448. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  449. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  450. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  451. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  452. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  453. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  454. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  455. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  456. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  457. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  458. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  459. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  460. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  461. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  462. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  463. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  464. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  465. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  466. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  467. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  468. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  469. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  470. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  471. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  472. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  473. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  474. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  475. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  476. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  477. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  478. GGML_TABLE_END()
  479. //#endif
  480. GGML_TABLE_BEGIN(uint64_t, iq2xxs_grid, 256)
  481. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  482. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  483. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  484. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  485. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  486. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  487. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  488. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  489. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  490. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  491. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  492. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  493. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  494. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  495. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  496. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  497. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  498. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  499. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  500. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  501. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  502. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  503. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  504. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  505. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  506. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  507. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  508. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  509. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  510. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  511. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  512. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  513. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  514. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  515. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  516. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  517. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  518. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  519. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  520. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  521. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  522. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  523. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  524. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  525. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  526. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  527. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  528. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  529. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  530. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  531. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  532. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  533. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  534. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  535. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  536. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  537. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  538. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  539. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  540. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  541. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  542. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  543. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  544. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  545. GGML_TABLE_END()
  546. GGML_TABLE_BEGIN(uint64_t, iq2xs_grid, 512)
  547. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  548. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  549. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  550. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  551. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  552. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  553. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  554. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  555. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  556. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  557. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  558. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  559. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  560. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  561. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  562. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  563. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  564. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  565. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  566. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  567. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  568. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  569. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  570. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  571. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  572. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  573. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  574. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  575. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  576. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  577. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  578. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  579. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  580. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  581. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  582. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  583. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  584. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  585. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  586. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  587. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  588. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  589. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  590. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  591. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  592. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  593. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  594. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  595. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  596. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  597. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  598. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  599. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  600. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  601. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  602. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  603. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  604. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  605. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  606. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  607. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  608. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  609. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  610. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  611. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  612. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  613. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  614. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  615. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  616. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  617. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  618. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  619. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  620. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  621. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  622. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  623. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  624. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  625. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  626. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  627. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  628. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  629. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  630. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  631. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  632. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  633. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  634. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  635. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  636. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  637. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  638. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  639. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  640. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  641. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  642. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  643. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  644. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  645. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  646. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  647. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  648. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  649. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  650. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  651. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  652. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  653. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  654. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  655. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  656. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  657. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  658. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  659. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  660. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  661. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  662. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  663. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  664. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  665. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  666. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  667. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  668. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  669. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  670. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  671. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  672. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  673. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  674. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  675. GGML_TABLE_END()
  676. GGML_TABLE_BEGIN(uint64_t, iq2s_grid, 1024)
  677. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  678. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  679. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  680. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  681. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  682. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
  683. 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
  684. 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
  685. 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
  686. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
  687. 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
  688. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
  689. 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
  690. 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
  691. 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
  692. 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
  693. 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
  694. 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
  695. 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
  696. 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
  697. 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  698. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
  699. 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
  700. 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
  701. 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
  702. 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
  703. 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
  704. 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
  705. 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
  706. 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
  707. 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
  708. 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
  709. 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
  710. 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
  711. 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
  712. 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
  713. 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
  714. 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
  715. 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
  716. 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
  717. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
  718. 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
  719. 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
  720. 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
  721. 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
  722. 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
  723. 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
  724. 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
  725. 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
  726. 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
  727. 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
  728. 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
  729. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
  730. 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
  731. 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
  732. 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
  733. 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
  734. 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
  735. 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
  736. 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
  737. 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
  738. 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
  739. 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
  740. 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
  741. 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
  742. 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
  743. 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
  744. 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
  745. 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
  746. 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
  747. 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  748. 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
  749. 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
  750. 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
  751. 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
  752. 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
  753. 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
  754. 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
  755. 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
  756. 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
  757. 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
  758. 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
  759. 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
  760. 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
  761. 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
  762. 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
  763. 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
  764. 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
  765. 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
  766. 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
  767. 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
  768. 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
  769. 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
  770. 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
  771. 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
  772. 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
  773. 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
  774. 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
  775. 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
  776. 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
  777. 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
  778. 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
  779. 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
  780. 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
  781. 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
  782. 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
  783. 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
  784. 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
  785. 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
  786. 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
  787. 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
  788. 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
  789. 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
  790. 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
  791. 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
  792. 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
  793. 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
  794. 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  795. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
  796. 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
  797. 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
  798. 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
  799. 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
  800. 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
  801. 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
  802. 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
  803. 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  804. 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
  805. 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
  806. 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
  807. 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
  808. 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
  809. 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
  810. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
  811. 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
  812. 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
  813. 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
  814. 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
  815. 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
  816. 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
  817. 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
  818. 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
  819. 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
  820. 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
  821. 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
  822. 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
  823. 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
  824. 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
  825. 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
  826. 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
  827. 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
  828. 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
  829. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
  830. 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
  831. 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
  832. 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
  833. 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
  834. 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
  835. 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
  836. 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
  837. 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
  838. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
  839. 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
  840. 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
  841. 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
  842. 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
  843. 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
  844. 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
  845. 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
  846. 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
  847. 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
  848. 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
  849. 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
  850. 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
  851. 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
  852. 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
  853. 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
  854. 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
  855. 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
  856. 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
  857. 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
  858. 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
  859. 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
  860. 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
  861. 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
  862. 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
  863. 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
  864. 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
  865. 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
  866. 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
  867. 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
  868. 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
  869. 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
  870. 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
  871. 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
  872. 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
  873. 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
  874. 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
  875. 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
  876. 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
  877. 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
  878. 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
  879. 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
  880. 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
  881. 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
  882. 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  883. 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
  884. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
  885. 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
  886. 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
  887. 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
  888. 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
  889. 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
  890. 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
  891. 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
  892. 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
  893. 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
  894. 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
  895. 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
  896. 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
  897. 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
  898. 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
  899. 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
  900. 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
  901. 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
  902. 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
  903. 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
  904. 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
  905. 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
  906. 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
  907. 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
  908. 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
  909. 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
  910. 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
  911. 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
  912. 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
  913. 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
  914. 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
  915. 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
  916. 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
  917. 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
  918. 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
  919. 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
  920. 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
  921. 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
  922. 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
  923. 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
  924. 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
  925. 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
  926. 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
  927. 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
  928. 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
  929. 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
  930. 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
  931. 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
  932. 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
  933. GGML_TABLE_END()
  934. GGML_TABLE_BEGIN(uint32_t, iq3xxs_grid, 256)
  935. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  936. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  937. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  938. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  939. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  940. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  941. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  942. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  943. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  944. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  945. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  946. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  947. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  948. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  949. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  950. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  951. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  952. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  953. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  954. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  955. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  956. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  957. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  958. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  959. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  960. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  961. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  962. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  963. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  964. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  965. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  966. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  967. GGML_TABLE_END()
  968. GGML_TABLE_BEGIN(uint32_t, iq3s_grid, 512)
  969. 0x01010101, 0x01010103, 0x01010105, 0x0101010b, 0x0101010f, 0x01010301, 0x01010303, 0x01010305,
  970. 0x01010309, 0x0101030d, 0x01010501, 0x01010503, 0x0101050b, 0x01010707, 0x01010901, 0x01010905,
  971. 0x0101090b, 0x0101090f, 0x01010b03, 0x01010b07, 0x01010d01, 0x01010d05, 0x01010f03, 0x01010f09,
  972. 0x01010f0f, 0x01030101, 0x01030103, 0x01030105, 0x01030109, 0x01030301, 0x01030303, 0x0103030b,
  973. 0x01030501, 0x01030507, 0x0103050f, 0x01030703, 0x0103070b, 0x01030909, 0x01030d03, 0x01030d0b,
  974. 0x01030f05, 0x01050101, 0x01050103, 0x0105010b, 0x0105010f, 0x01050301, 0x01050307, 0x0105030d,
  975. 0x01050503, 0x0105050b, 0x01050701, 0x01050709, 0x01050905, 0x0105090b, 0x0105090f, 0x01050b03,
  976. 0x01050b07, 0x01050f01, 0x01050f07, 0x01070107, 0x01070303, 0x0107030b, 0x01070501, 0x01070505,
  977. 0x01070703, 0x01070707, 0x0107070d, 0x01070909, 0x01070b01, 0x01070b05, 0x01070d0f, 0x01070f03,
  978. 0x01070f0b, 0x01090101, 0x01090307, 0x0109030f, 0x01090503, 0x01090509, 0x01090705, 0x01090901,
  979. 0x01090907, 0x01090b03, 0x01090f01, 0x010b0105, 0x010b0109, 0x010b0501, 0x010b0505, 0x010b050d,
  980. 0x010b0707, 0x010b0903, 0x010b090b, 0x010b090f, 0x010b0d0d, 0x010b0f07, 0x010d010d, 0x010d0303,
  981. 0x010d0307, 0x010d0703, 0x010d0b05, 0x010d0f03, 0x010f0101, 0x010f0105, 0x010f0109, 0x010f0501,
  982. 0x010f0505, 0x010f050d, 0x010f0707, 0x010f0b01, 0x010f0b09, 0x03010101, 0x03010103, 0x03010105,
  983. 0x03010109, 0x03010301, 0x03010303, 0x03010307, 0x0301030b, 0x0301030f, 0x03010501, 0x03010505,
  984. 0x03010703, 0x03010709, 0x0301070d, 0x03010b09, 0x03010b0d, 0x03010d03, 0x03010f05, 0x03030101,
  985. 0x03030103, 0x03030107, 0x0303010d, 0x03030301, 0x03030309, 0x03030503, 0x03030701, 0x03030707,
  986. 0x03030903, 0x03030b01, 0x03030b05, 0x03030f01, 0x03030f0d, 0x03050101, 0x03050305, 0x0305030b,
  987. 0x0305030f, 0x03050501, 0x03050509, 0x03050705, 0x03050901, 0x03050907, 0x03050b0b, 0x03050d01,
  988. 0x03050f05, 0x03070103, 0x03070109, 0x0307010f, 0x03070301, 0x03070307, 0x03070503, 0x0307050f,
  989. 0x03070701, 0x03070709, 0x03070903, 0x03070d05, 0x03070f01, 0x03090107, 0x0309010b, 0x03090305,
  990. 0x03090309, 0x03090703, 0x03090707, 0x03090905, 0x0309090d, 0x03090b01, 0x03090b09, 0x030b0103,
  991. 0x030b0301, 0x030b0307, 0x030b0503, 0x030b0701, 0x030b0705, 0x030b0b03, 0x030d0501, 0x030d0509,
  992. 0x030d050f, 0x030d0909, 0x030d090d, 0x030f0103, 0x030f0107, 0x030f0301, 0x030f0305, 0x030f0503,
  993. 0x030f070b, 0x030f0903, 0x030f0d05, 0x030f0f01, 0x05010101, 0x05010103, 0x05010107, 0x0501010b,
  994. 0x0501010f, 0x05010301, 0x05010305, 0x05010309, 0x0501030d, 0x05010503, 0x05010507, 0x0501050f,
  995. 0x05010701, 0x05010705, 0x05010903, 0x05010907, 0x0501090b, 0x05010b01, 0x05010b05, 0x05010d0f,
  996. 0x05010f01, 0x05010f07, 0x05010f0b, 0x05030101, 0x05030105, 0x05030301, 0x05030307, 0x0503030f,
  997. 0x05030505, 0x0503050b, 0x05030703, 0x05030709, 0x05030905, 0x05030b03, 0x05050103, 0x05050109,
  998. 0x0505010f, 0x05050503, 0x05050507, 0x05050701, 0x0505070f, 0x05050903, 0x05050b07, 0x05050b0f,
  999. 0x05050f03, 0x05050f09, 0x05070101, 0x05070105, 0x0507010b, 0x05070303, 0x05070505, 0x05070509,
  1000. 0x05070703, 0x05070707, 0x05070905, 0x05070b01, 0x05070d0d, 0x05090103, 0x0509010f, 0x05090501,
  1001. 0x05090507, 0x05090705, 0x0509070b, 0x05090903, 0x05090f05, 0x05090f0b, 0x050b0109, 0x050b0303,
  1002. 0x050b0505, 0x050b070f, 0x050b0901, 0x050b0b07, 0x050b0f01, 0x050d0101, 0x050d0105, 0x050d010f,
  1003. 0x050d0503, 0x050d0b0b, 0x050d0d03, 0x050f010b, 0x050f0303, 0x050f050d, 0x050f0701, 0x050f0907,
  1004. 0x050f0b01, 0x07010105, 0x07010303, 0x07010307, 0x0701030b, 0x0701030f, 0x07010505, 0x07010703,
  1005. 0x07010707, 0x0701070b, 0x07010905, 0x07010909, 0x0701090f, 0x07010b03, 0x07010d07, 0x07010f03,
  1006. 0x07030103, 0x07030107, 0x0703010b, 0x07030309, 0x07030503, 0x07030507, 0x07030901, 0x07030d01,
  1007. 0x07030f05, 0x07030f0d, 0x07050101, 0x07050305, 0x07050501, 0x07050705, 0x07050709, 0x07050b01,
  1008. 0x07070103, 0x07070301, 0x07070309, 0x07070503, 0x07070507, 0x0707050f, 0x07070701, 0x07070903,
  1009. 0x07070907, 0x0707090f, 0x07070b0b, 0x07070f07, 0x07090107, 0x07090303, 0x0709030d, 0x07090505,
  1010. 0x07090703, 0x07090b05, 0x07090d01, 0x07090d09, 0x070b0103, 0x070b0301, 0x070b0305, 0x070b050b,
  1011. 0x070b0705, 0x070b0909, 0x070b0b0d, 0x070b0f07, 0x070d030d, 0x070d0903, 0x070f0103, 0x070f0107,
  1012. 0x070f0501, 0x070f0505, 0x070f070b, 0x09010101, 0x09010109, 0x09010305, 0x09010501, 0x09010509,
  1013. 0x0901050f, 0x09010705, 0x09010903, 0x09010b01, 0x09010f01, 0x09030105, 0x0903010f, 0x09030303,
  1014. 0x09030307, 0x09030505, 0x09030701, 0x0903070b, 0x09030907, 0x09030b03, 0x09030b0b, 0x09050103,
  1015. 0x09050107, 0x09050301, 0x0905030b, 0x09050503, 0x09050707, 0x09050901, 0x09050b0f, 0x09050d05,
  1016. 0x09050f01, 0x09070109, 0x09070303, 0x09070307, 0x09070501, 0x09070505, 0x09070703, 0x0907070b,
  1017. 0x09090101, 0x09090105, 0x09090509, 0x0909070f, 0x09090901, 0x09090f03, 0x090b010b, 0x090b010f,
  1018. 0x090b0503, 0x090b0d05, 0x090d0307, 0x090d0709, 0x090d0d01, 0x090f0301, 0x090f030b, 0x090f0701,
  1019. 0x090f0907, 0x090f0b03, 0x0b010105, 0x0b010301, 0x0b010309, 0x0b010505, 0x0b010901, 0x0b010909,
  1020. 0x0b01090f, 0x0b010b05, 0x0b010d0d, 0x0b010f09, 0x0b030103, 0x0b030107, 0x0b03010b, 0x0b030305,
  1021. 0x0b030503, 0x0b030705, 0x0b030f05, 0x0b050101, 0x0b050303, 0x0b050507, 0x0b050701, 0x0b05070d,
  1022. 0x0b050b07, 0x0b070105, 0x0b07010f, 0x0b070301, 0x0b07050f, 0x0b070909, 0x0b070b03, 0x0b070d0b,
  1023. 0x0b070f07, 0x0b090103, 0x0b090109, 0x0b090501, 0x0b090705, 0x0b09090d, 0x0b0b0305, 0x0b0b050d,
  1024. 0x0b0b0b03, 0x0b0b0b07, 0x0b0d0905, 0x0b0f0105, 0x0b0f0109, 0x0b0f0505, 0x0d010303, 0x0d010307,
  1025. 0x0d01030b, 0x0d010703, 0x0d010707, 0x0d010d01, 0x0d030101, 0x0d030501, 0x0d03050f, 0x0d030d09,
  1026. 0x0d050305, 0x0d050709, 0x0d050905, 0x0d050b0b, 0x0d050d05, 0x0d050f01, 0x0d070101, 0x0d070309,
  1027. 0x0d070503, 0x0d070901, 0x0d09050b, 0x0d090907, 0x0d090d05, 0x0d0b0101, 0x0d0b0107, 0x0d0b0709,
  1028. 0x0d0b0d01, 0x0d0d010b, 0x0d0d0901, 0x0d0f0303, 0x0d0f0307, 0x0f010101, 0x0f010109, 0x0f01010f,
  1029. 0x0f010501, 0x0f010505, 0x0f01070d, 0x0f010901, 0x0f010b09, 0x0f010d05, 0x0f030105, 0x0f030303,
  1030. 0x0f030509, 0x0f030907, 0x0f03090b, 0x0f050103, 0x0f050109, 0x0f050301, 0x0f05030d, 0x0f050503,
  1031. 0x0f050701, 0x0f050b03, 0x0f070105, 0x0f070705, 0x0f07070b, 0x0f070b07, 0x0f090103, 0x0f09010b,
  1032. 0x0f090307, 0x0f090501, 0x0f090b01, 0x0f0b0505, 0x0f0b0905, 0x0f0d0105, 0x0f0d0703, 0x0f0f0101,
  1033. GGML_TABLE_END()
  1034. #define NGRID_IQ1S 2048
  1035. #define IQ1S_DELTA 0.125f
  1036. #define IQ1M_DELTA 0.125f
  1037. #if defined(GGML_COMMON_IMPL_C)
  1038. GGML_TABLE_BEGIN(uint64_t, iq1s_grid, NGRID_IQ1S)
  1039. 0xffffffffffffffff, 0xffffffffffffff01, 0xffffffffffff0000, 0xffffffffffff01ff,
  1040. 0xffffffffffff0101, 0xffffffffff00ff00, 0xffffffffff000000, 0xffffffffff01ffff,
  1041. 0xffffffffff01ff01, 0xffffffffff0101ff, 0xffffffffff010101, 0xffffffff00ff0000,
  1042. 0xffffffff0000ff00, 0xffffffff000000ff, 0xffffffff00000001, 0xffffffff00010000,
  1043. 0xffffffff01ffffff, 0xffffffff01ffff01, 0xffffffff01ff01ff, 0xffffffff01ff0101,
  1044. 0xffffffff01000000, 0xffffffff0101ffff, 0xffffffff0101ff01, 0xffffffff010101ff,
  1045. 0xffffffff01010101, 0xffffff00ffff00ff, 0xffffff00ffff0000, 0xffffff00ff00ff00,
  1046. 0xffffff00ff0000ff, 0xffffff00ff000001, 0xffffff00ff000100, 0xffffff00ff000101,
  1047. 0xffffff00ff010000, 0xffffff0000ffff00, 0xffffff0000ff0001, 0xffffff0000ff0100,
  1048. 0xffffff000000ff01, 0xffffff0000000000, 0xffffff0000000101, 0xffffff000001ff00,
  1049. 0xffffff00000100ff, 0xffffff0000010001, 0xffffff00000101ff, 0xffffff0001ff0000,
  1050. 0xffffff000100ff00, 0xffffff00010000ff, 0xffffff0001000001, 0xffffff0001010000,
  1051. 0xffffff01ffffffff, 0xffffff01ffffff01, 0xffffff01ffff01ff, 0xffffff01ffff0101,
  1052. 0xffffff01ff000000, 0xffffff01ff01ffff, 0xffffff01ff01ff01, 0xffffff01ff0101ff,
  1053. 0xffffff01ff010101, 0xffffff0100ff0000, 0xffffff010000ff00, 0xffffff0100000100,
  1054. 0xffffff01000100ff, 0xffffff0100010100, 0xffffff0101ffffff, 0xffffff0101ffff01,
  1055. 0xffffff0101ff01ff, 0xffffff0101ff0101, 0xffffff010100ff00, 0xffffff0101000000,
  1056. 0xffffff0101000100, 0xffffff010101ffff, 0xffffff010101ff01, 0xffffff01010101ff,
  1057. 0xffffff0101010101, 0xffff00ffff00ff00, 0xffff00ffff0000ff, 0xffff00ffff000001,
  1058. 0xffff00ffff010000, 0xffff00ff00ffff00, 0xffff00ff00ff0100, 0xffff00ff00000000,
  1059. 0xffff00ff00000101, 0xffff00ff000100ff, 0xffff00ff00010000, 0xffff00ff0100ff00,
  1060. 0xffff00ff01000100, 0xffff00ff01010000, 0xffff0000ffffff00, 0xffff0000ffff00ff,
  1061. 0xffff0000ffff0000, 0xffff0000ffff0001, 0xffff0000ff000000, 0xffff0000ff0001ff,
  1062. 0xffff0000ff000101, 0xffff0000ff010100, 0xffff000000ffffff, 0xffff000000ff0000,
  1063. 0xffff000000ff0101, 0xffff00000000ffff, 0xffff00000000ff00, 0xffff0000000000ff,
  1064. 0xffff000000000000, 0xffff000000000001, 0xffff000000000100, 0xffff00000001ffff,
  1065. 0xffff00000001ff01, 0xffff000000010000, 0xffff0000000101ff, 0xffff000000010101,
  1066. 0xffff000001ffff00, 0xffff00000100ff00, 0xffff000001000000, 0xffff0000010001ff,
  1067. 0xffff000001000101, 0xffff00000101ff00, 0xffff0000010100ff, 0xffff000001010000,
  1068. 0xffff000001010001, 0xffff000001010100, 0xffff0001ff0000ff, 0xffff0001ff000100,
  1069. 0xffff000100ffff00, 0xffff000100ff00ff, 0xffff00010000ffff, 0xffff00010000ff01,
  1070. 0xffff000100000000, 0xffff0001000001ff, 0xffff00010001ffff, 0xffff00010001ff00,
  1071. 0xffff000100010001, 0xffff000100010100, 0xffff000101ff0000, 0xffff00010100ff00,
  1072. 0xffff0001010000ff, 0xffff000101000100, 0xffff01ffffffffff, 0xffff01ffffffff01,
  1073. 0xffff01ffffff01ff, 0xffff01ffffff0101, 0xffff01ffff000000, 0xffff01ffff01ffff,
  1074. 0xffff01ffff01ff01, 0xffff01ffff0101ff, 0xffff01ffff010101, 0xffff01ff00ff0000,
  1075. 0xffff01ff0000ff00, 0xffff01ff00000001, 0xffff01ff00010000, 0xffff01ff01ffffff,
  1076. 0xffff01ff01ffff01, 0xffff01ff01ff01ff, 0xffff01ff01ff0101, 0xffff01ff01000000,
  1077. 0xffff01ff0101ffff, 0xffff01ff0101ff01, 0xffff01ff010101ff, 0xffff01ff01010101,
  1078. 0xffff0100ffff0000, 0xffff0100ff00ff00, 0xffff0100ff0000ff, 0xffff0100ff000100,
  1079. 0xffff0100ff0100ff, 0xffff0100ff010000, 0xffff010000ffff00, 0xffff01000000ffff,
  1080. 0xffff01000000ff00, 0xffff010000000000, 0xffff01000001ff00, 0xffff0100000100ff,
  1081. 0xffff010000010100, 0xffff01000100ff00, 0xffff0100010000ff, 0xffff010001000001,
  1082. 0xffff010001000100, 0xffff010001010000, 0xffff0101ffffffff, 0xffff0101ffffff01,
  1083. 0xffff0101ffff01ff, 0xffff0101ffff0101, 0xffff0101ff000000, 0xffff0101ff01ffff,
  1084. 0xffff0101ff01ff01, 0xffff0101ff0101ff, 0xffff0101ff010101, 0xffff010100ff0000,
  1085. 0xffff01010000ff00, 0xffff010100000100, 0xffff01010001ff00, 0xffff010100010000,
  1086. 0xffff010101ffffff, 0xffff010101ffff01, 0xffff010101ff0000, 0xffff010101ff01ff,
  1087. 0xffff010101ff0101, 0xffff010101000000, 0xffff01010101ffff, 0xffff01010101ff01,
  1088. 0xffff0101010101ff, 0xffff010101010101, 0xff00ffffff00ffff, 0xff00ffffff00ff00,
  1089. 0xff00ffffff0000ff, 0xff00ffffff000100, 0xff00ffffff0100ff, 0xff00ffffff010000,
  1090. 0xff00ffff00ffff00, 0xff00ffff00ff00ff, 0xff00ffff0000ffff, 0xff00ffff00000000,
  1091. 0xff00ffff000001ff, 0xff00ffff0001ff00, 0xff00ffff000100ff, 0xff00ffff00010000,
  1092. 0xff00ffff00010100, 0xff00ffff0100ff00, 0xff00ffff010000ff, 0xff00ffff01000001,
  1093. 0xff00ffff0101ff00, 0xff00ffff01010000, 0xff00ff00ffffff00, 0xff00ff00ffff00ff,
  1094. 0xff00ff00ffff0001, 0xff00ff00ffff0100, 0xff00ff00ff00ffff, 0xff00ff00ff00ff01,
  1095. 0xff00ff00ff000000, 0xff00ff00ff0001ff, 0xff00ff00ff01ff00, 0xff00ff00ff0100ff,
  1096. 0xff00ff00ff010100, 0xff00ff0000ff0000, 0xff00ff0000ff0101, 0xff00ff000000ffff,
  1097. 0xff00ff000000ff00, 0xff00ff000000ff01, 0xff00ff00000000ff, 0xff00ff0000000000,
  1098. 0xff00ff0000000001, 0xff00ff0000000100, 0xff00ff000001ffff, 0xff00ff0000010000,
  1099. 0xff00ff0001ff00ff, 0xff00ff000100ff01, 0xff00ff0001000000, 0xff00ff000101ff00,
  1100. 0xff00ff00010100ff, 0xff00ff01ff00ff00, 0xff00ff01ff0000ff, 0xff00ff01ff000001,
  1101. 0xff00ff01ff010000, 0xff00ff0100ffffff, 0xff00ff0100ff0001, 0xff00ff0100ff0100,
  1102. 0xff00ff010000ff01, 0xff00ff0100000000, 0xff00ff01000001ff, 0xff00ff0100000101,
  1103. 0xff00ff01000100ff, 0xff00ff0100010001, 0xff00ff0101ff0000, 0xff00ff010100ff00,
  1104. 0xff00ff01010000ff, 0xff00ff0101000001, 0xff00ff0101010000, 0xff0000ffffffff00,
  1105. 0xff0000ffffff0001, 0xff0000ffffff0100, 0xff0000ffff0000ff, 0xff0000ffff000000,
  1106. 0xff0000ffff0001ff, 0xff0000ffff000100, 0xff0000ffff01ff00, 0xff0000ffff010001,
  1107. 0xff0000ff00ffff00, 0xff0000ff00ff0000, 0xff0000ff00ff0001, 0xff0000ff00ff01ff,
  1108. 0xff0000ff00ff0101, 0xff0000ff0000ff00, 0xff0000ff000000ff, 0xff0000ff00000000,
  1109. 0xff0000ff00000001, 0xff0000ff00000100, 0xff0000ff0001ff01, 0xff0000ff00010000,
  1110. 0xff0000ff000101ff, 0xff0000ff01ff00ff, 0xff0000ff01ff0100, 0xff0000ff0100ffff,
  1111. 0xff0000ff010000ff, 0xff0000ff01000000, 0xff0000ff010001ff, 0xff0000ff01000100,
  1112. 0xff0000ff01000101, 0xff0000ff0101ff00, 0xff0000ff010100ff, 0xff0000ff01010000,
  1113. 0xff0000ff01010100, 0xff000000ffffff01, 0xff000000ffff0000, 0xff000000ffff0101,
  1114. 0xff000000ff00ff00, 0xff000000ff0000ff, 0xff000000ff000000, 0xff000000ff000001,
  1115. 0xff000000ff000100, 0xff000000ff01ffff, 0xff000000ff01ff01, 0xff000000ff010000,
  1116. 0xff000000ff0101ff, 0xff000000ff010101, 0xff00000000ffff00, 0xff00000000ff00ff,
  1117. 0xff00000000ff0000, 0xff00000000ff0001, 0xff0000000000ff00, 0xff0000000000ff01,
  1118. 0xff000000000000ff, 0xff00000000000000, 0xff00000000000001, 0xff00000000000100,
  1119. 0xff00000000000101, 0xff0000000001ff00, 0xff000000000100ff, 0xff00000000010000,
  1120. 0xff00000000010001, 0xff00000000010100, 0xff00000001ffffff, 0xff00000001ffff01,
  1121. 0xff00000001ff00ff, 0xff00000001ff0000, 0xff00000001ff01ff, 0xff00000001ff0101,
  1122. 0xff0000000100ffff, 0xff0000000100ff00, 0xff000000010000ff, 0xff00000001000000,
  1123. 0xff00000001000001, 0xff00000001000100, 0xff00000001000101, 0xff0000000101ffff,
  1124. 0xff0000000101ff01, 0xff00000001010000, 0xff000001ffffff00, 0xff000001ffff00ff,
  1125. 0xff000001ffff0000, 0xff000001ffff0001, 0xff000001ff000000, 0xff000001ff000001,
  1126. 0xff000001ff0001ff, 0xff000001ff000101, 0xff000001ff01ff00, 0xff000001ff010001,
  1127. 0xff00000100ffffff, 0xff00000100ffff01, 0xff00000100ff00ff, 0xff00000100ff0000,
  1128. 0xff00000100ff01ff, 0xff00000100ff0101, 0xff0000010000ff00, 0xff00000100000000,
  1129. 0xff00000100000001, 0xff000001000001ff, 0xff00000100000100, 0xff0000010001ff00,
  1130. 0xff000001000100ff, 0xff00000100010000, 0xff000001000101ff, 0xff00000100010100,
  1131. 0xff00000100010101, 0xff00000101ff0001, 0xff00000101ff0101, 0xff0000010100ff01,
  1132. 0xff00000101000000, 0xff000001010100ff, 0xff00000101010100, 0xff0001ffff00ff00,
  1133. 0xff0001ffff000001, 0xff0001ffff010000, 0xff0001ff00ffff00, 0xff0001ff00ff00ff,
  1134. 0xff0001ff00ff0001, 0xff0001ff00ff0100, 0xff0001ff0000ffff, 0xff0001ff00000000,
  1135. 0xff0001ff000001ff, 0xff0001ff00000101, 0xff0001ff0001ffff, 0xff0001ff0001ff00,
  1136. 0xff0001ff000100ff, 0xff0001ff00010001, 0xff0001ff00010100, 0xff0001ff01ff0000,
  1137. 0xff0001ff0100ff00, 0xff0001ff010000ff, 0xff0001ff01010000, 0xff000100ff00ffff,
  1138. 0xff000100ff00ff01, 0xff000100ff000000, 0xff000100ff000101, 0xff000100ff01ff00,
  1139. 0xff000100ff010000, 0xff00010000ffff01, 0xff00010000ff00ff, 0xff00010000ff0000,
  1140. 0xff00010000ff01ff, 0xff0001000000ff00, 0xff000100000000ff, 0xff00010000000000,
  1141. 0xff00010000000001, 0xff00010000000100, 0xff00010000000101, 0xff0001000001ffff,
  1142. 0xff00010000010000, 0xff00010000010101, 0xff00010001ff0100, 0xff0001000100ff00,
  1143. 0xff0001000100ff01, 0xff00010001000000, 0xff000100010001ff, 0xff0001000101ff00,
  1144. 0xff00010001010001, 0xff00010001010100, 0xff000101ffff0100, 0xff000101ff000001,
  1145. 0xff000101ff0100ff, 0xff000101ff010001, 0xff00010100ff00ff, 0xff00010100ff0001,
  1146. 0xff00010100ff0100, 0xff0001010000ffff, 0xff0001010000ff01, 0xff00010100000000,
  1147. 0xff000101000001ff, 0xff0001010001ff00, 0xff00010100010001, 0xff00010100010100,
  1148. 0xff00010101ff0000, 0xff0001010100ff00, 0xff00010101000001, 0xff00010101000101,
  1149. 0xff01ffffffffffff, 0xff01ffffffffff01, 0xff01ffffffff01ff, 0xff01ffffffff0101,
  1150. 0xff01ffffff000000, 0xff01ffffff01ffff, 0xff01ffffff01ff01, 0xff01ffffff010000,
  1151. 0xff01ffffff0101ff, 0xff01ffffff010101, 0xff01ffff00ff0000, 0xff01ffff0000ff00,
  1152. 0xff01ffff00000100, 0xff01ffff0001ff00, 0xff01ffff00010000, 0xff01ffff01ffffff,
  1153. 0xff01ffff01ffff01, 0xff01ffff01ff01ff, 0xff01ffff01ff0101, 0xff01ffff01000000,
  1154. 0xff01ffff0101ffff, 0xff01ffff0101ff01, 0xff01ffff01010000, 0xff01ffff010101ff,
  1155. 0xff01ffff01010101, 0xff01ff00ffff0000, 0xff01ff00ff00ff00, 0xff01ff00ff0000ff,
  1156. 0xff01ff00ff000100, 0xff01ff00ff010000, 0xff01ff0000ffff01, 0xff01ff0000ff00ff,
  1157. 0xff01ff0000ff0100, 0xff01ff0000000000, 0xff01ff00000001ff, 0xff01ff0000000101,
  1158. 0xff01ff000001ff00, 0xff01ff00000100ff, 0xff01ff0000010000, 0xff01ff0000010001,
  1159. 0xff01ff0001ff0000, 0xff01ff000100ffff, 0xff01ff0001000001, 0xff01ff0001000100,
  1160. 0xff01ff0001010000, 0xff01ff01ffffff00, 0xff01ff01ffff01ff, 0xff01ff01ffff0101,
  1161. 0xff01ff01ff00ff00, 0xff01ff01ff000000, 0xff01ff01ff01ffff, 0xff01ff01ff01ff01,
  1162. 0xff01ff01ff0101ff, 0xff01ff01ff010101, 0xff01ff0100ff0000, 0xff01ff010000ff00,
  1163. 0xff01ff0100000001, 0xff01ff0100000100, 0xff01ff0100010000, 0xff01ff0101ffff00,
  1164. 0xff01ff0101ff01ff, 0xff01ff0101ff0101, 0xff01ff010100ff00, 0xff01ff0101000000,
  1165. 0xff01ff010101ffff, 0xff01ff010101ff01, 0xff01ff01010101ff, 0xff01ff0101010101,
  1166. 0xff0100ffffff0000, 0xff0100ffff0000ff, 0xff0100ffff000001, 0xff0100ffff000100,
  1167. 0xff0100ffff010000, 0xff0100ff00ff00ff, 0xff0100ff00ff0000, 0xff0100ff00ff0001,
  1168. 0xff0100ff00ff0100, 0xff0100ff0000ff01, 0xff0100ff00000000, 0xff0100ff000001ff,
  1169. 0xff0100ff00000101, 0xff0100ff00010001, 0xff0100ff01ff0000, 0xff0100ff0100ff00,
  1170. 0xff0100ff010000ff, 0xff0100ff01000100, 0xff0100ff0101ff00, 0xff0100ff01010000,
  1171. 0xff010000ffff0100, 0xff010000ff000000, 0xff010000ff01ff00, 0xff010000ff010100,
  1172. 0xff01000000ffffff, 0xff01000000ff0000, 0xff01000000ff01ff, 0xff0100000000ff00,
  1173. 0xff010000000000ff, 0xff01000000000000, 0xff01000000000100, 0xff0100000001ff01,
  1174. 0xff01000000010000, 0xff010000000101ff, 0xff01000001ff0100, 0xff0100000100ffff,
  1175. 0xff010000010000ff, 0xff01000001000000, 0xff010000010001ff, 0xff01000001000101,
  1176. 0xff0100000101ff00, 0xff010000010100ff, 0xff01000001010001, 0xff01000001010100,
  1177. 0xff010001ffff0000, 0xff010001ff00ffff, 0xff010001ff00ff01, 0xff010001ff000100,
  1178. 0xff010001ff010000, 0xff01000100ffff00, 0xff01000100ff0100, 0xff01000100000000,
  1179. 0xff0100010001ffff, 0xff0100010001ff00, 0xff01000100010100, 0xff01000101ff00ff,
  1180. 0xff01000101ff0001, 0xff0100010100ffff, 0xff01000101000101, 0xff0101ffffffffff,
  1181. 0xff0101ffffffff01, 0xff0101ffffff01ff, 0xff0101ffffff0101, 0xff0101ffff000000,
  1182. 0xff0101ffff01ffff, 0xff0101ffff01ff01, 0xff0101ffff0101ff, 0xff0101ffff010101,
  1183. 0xff0101ff00ff0000, 0xff0101ff0000ff00, 0xff0101ff000000ff, 0xff0101ff00010000,
  1184. 0xff0101ff01ffffff, 0xff0101ff01ffff01, 0xff0101ff01ff01ff, 0xff0101ff01ff0101,
  1185. 0xff0101ff0101ffff, 0xff0101ff0101ff01, 0xff0101ff010101ff, 0xff0101ff01010101,
  1186. 0xff010100ffff0100, 0xff010100ff00ff00, 0xff010100ff0000ff, 0xff010100ff000100,
  1187. 0xff010100ff010000, 0xff01010000ff0001, 0xff01010000ff0100, 0xff0101000000ff01,
  1188. 0xff01010000000000, 0xff0101000001ff00, 0xff010100000100ff, 0xff01010000010001,
  1189. 0xff01010000010100, 0xff01010001ff0000, 0xff0101000100ffff, 0xff01010001000001,
  1190. 0xff01010001000100, 0xff010100010100ff, 0xff01010001010000, 0xff010101ffffffff,
  1191. 0xff010101ffffff01, 0xff010101ffff01ff, 0xff010101ffff0101, 0xff010101ff01ffff,
  1192. 0xff010101ff01ff01, 0xff010101ff0101ff, 0xff010101ff010101, 0xff01010100ff0000,
  1193. 0xff0101010000ff00, 0xff01010100000001, 0xff01010100000100, 0xff01010100010000,
  1194. 0xff01010101ffffff, 0xff01010101ffff01, 0xff01010101ff01ff, 0xff01010101ff0101,
  1195. 0xff01010101000000, 0xff0101010101ffff, 0xff0101010101ff01, 0xff010101010101ff,
  1196. 0xff01010101010101, 0x00ffffffffff0000, 0x00ffffffff00ff00, 0x00ffffffff000001,
  1197. 0x00ffffffff010000, 0x00ffffff00ff0100, 0x00ffffff0000ff01, 0x00ffffff00000000,
  1198. 0x00ffffff000001ff, 0x00ffffff00000101, 0x00ffffff0001ff00, 0x00ffffff000100ff,
  1199. 0x00ffffff00010001, 0x00ffffff010000ff, 0x00ffffff01000100, 0x00ffffff0101ff00,
  1200. 0x00ffffff01010001, 0x00ffff00ffffffff, 0x00ffff00ffffff00, 0x00ffff00ffff00ff,
  1201. 0x00ffff00ffff0001, 0x00ffff00ffff0100, 0x00ffff00ff00ff01, 0x00ffff00ff000000,
  1202. 0x00ffff00ff000001, 0x00ffff00ff0001ff, 0x00ffff00ff000101, 0x00ffff00ff01ff00,
  1203. 0x00ffff00ff010001, 0x00ffff00ff010100, 0x00ffff0000ff0000, 0x00ffff0000ff01ff,
  1204. 0x00ffff0000ff0101, 0x00ffff000000ff00, 0x00ffff00000000ff, 0x00ffff0000000000,
  1205. 0x00ffff0000000001, 0x00ffff0000000100, 0x00ffff0000000101, 0x00ffff0000010000,
  1206. 0x00ffff00000101ff, 0x00ffff0000010101, 0x00ffff0001ffff00, 0x00ffff0001ff00ff,
  1207. 0x00ffff0001ff0001, 0x00ffff000100ffff, 0x00ffff000100ff01, 0x00ffff0001000000,
  1208. 0x00ffff000101ffff, 0x00ffff000101ff00, 0x00ffff000101ff01, 0x00ffff01ffff0000,
  1209. 0x00ffff01ff00ff00, 0x00ffff01ff0000ff, 0x00ffff01ff000001, 0x00ffff01ff010000,
  1210. 0x00ffff0100ffff00, 0x00ffff010000ff01, 0x00ffff0100000000, 0x00ffff0100000101,
  1211. 0x00ffff01000100ff, 0x00ffff0100010100, 0x00ffff0101ff0100, 0x00ffff01010000ff,
  1212. 0x00ffff0101010000, 0x00ff00ffffffff00, 0x00ff00ffff000000, 0x00ff00ffff000100,
  1213. 0x00ff00ffff010100, 0x00ff00ff00ff0000, 0x00ff00ff00ff01ff, 0x00ff00ff00ff0101,
  1214. 0x00ff00ff0000ff00, 0x00ff00ff000000ff, 0x00ff00ff00000000, 0x00ff00ff00000001,
  1215. 0x00ff00ff0001ff00, 0x00ff00ff0001ff01, 0x00ff00ff00010000, 0x00ff00ff000101ff,
  1216. 0x00ff00ff00010101, 0x00ff00ff01ffff00, 0x00ff00ff01ff0001, 0x00ff00ff01ff0100,
  1217. 0x00ff00ff0100ffff, 0x00ff00ff0100ff01, 0x00ff00ff01000000, 0x00ff00ff0101ffff,
  1218. 0x00ff00ff0101ff00, 0x00ff00ff01010100, 0x00ff0000ffffff00, 0x00ff0000ffffff01,
  1219. 0x00ff0000ffff0000, 0x00ff0000ffff0101, 0x00ff0000ff00ff00, 0x00ff0000ff0000ff,
  1220. 0x00ff0000ff000000, 0x00ff0000ff000001, 0x00ff0000ff000100, 0x00ff0000ff01ffff,
  1221. 0x00ff0000ff010000, 0x00ff0000ff010101, 0x00ff000000ffff00, 0x00ff000000ff00ff,
  1222. 0x00ff000000ff0000, 0x00ff000000ff0001, 0x00ff000000ff0100, 0x00ff00000000ffff,
  1223. 0x00ff00000000ff00, 0x00ff0000000000ff, 0x00ff000000000000, 0x00ff000000000001,
  1224. 0x00ff0000000001ff, 0x00ff000000000100, 0x00ff00000001ff00, 0x00ff0000000100ff,
  1225. 0x00ff000000010000, 0x00ff000000010001, 0x00ff000000010100, 0x00ff000001ffff01,
  1226. 0x00ff000001ff00ff, 0x00ff000001ff0000, 0x00ff000001ff01ff, 0x00ff00000100ff00,
  1227. 0x00ff0000010000ff, 0x00ff000001000000, 0x00ff000001000001, 0x00ff000001000100,
  1228. 0x00ff000001000101, 0x00ff000001010000, 0x00ff0000010101ff, 0x00ff000001010101,
  1229. 0x00ff0001ffffff00, 0x00ff0001ffff0000, 0x00ff0001ffff0100, 0x00ff0001ff0000ff,
  1230. 0x00ff0001ff000000, 0x00ff0001ff0001ff, 0x00ff0001ff000101, 0x00ff0001ff01ff00,
  1231. 0x00ff0001ff0100ff, 0x00ff0001ff010100, 0x00ff000100ffffff, 0x00ff000100ffff01,
  1232. 0x00ff000100ff0000, 0x00ff000100ff01ff, 0x00ff00010000ffff, 0x00ff00010000ff00,
  1233. 0x00ff00010000ff01, 0x00ff000100000000, 0x00ff000100000001, 0x00ff000100000100,
  1234. 0x00ff00010001ff01, 0x00ff000100010000, 0x00ff0001000101ff, 0x00ff000101ffff00,
  1235. 0x00ff000101ff0000, 0x00ff000101ff0101, 0x00ff0001010000ff, 0x00ff000101000000,
  1236. 0x00ff00010101ff00, 0x00ff0001010100ff, 0x00ff000101010001, 0x00ff01ffffff0000,
  1237. 0x00ff01ffff00ff00, 0x00ff01ffff000000, 0x00ff01ffff000101, 0x00ff01ffff010000,
  1238. 0x00ff01ff00ffff01, 0x00ff01ff00ff0100, 0x00ff01ff0000ffff, 0x00ff01ff00000000,
  1239. 0x00ff01ff000001ff, 0x00ff01ff0001ff00, 0x00ff01ff000100ff, 0x00ff01ff00010001,
  1240. 0x00ff01ff00010100, 0x00ff01ff01ff0000, 0x00ff01ff0100ff00, 0x00ff01ff010000ff,
  1241. 0x00ff01ff01000001, 0x00ff01ff01000100, 0x00ff01ff01010000, 0x00ff0100ffffff00,
  1242. 0x00ff0100ffff0000, 0x00ff0100ffff0001, 0x00ff0100ffff0101, 0x00ff0100ff00ffff,
  1243. 0x00ff0100ff0000ff, 0x00ff0100ff000000, 0x00ff0100ff0001ff, 0x00ff0100ff01ff00,
  1244. 0x00ff0100ff0100ff, 0x00ff0100ff010001, 0x00ff010000ffffff, 0x00ff010000ff0000,
  1245. 0x00ff010000ff0101, 0x00ff01000000ff00, 0x00ff01000000ff01, 0x00ff0100000000ff,
  1246. 0x00ff010000000000, 0x00ff010000000001, 0x00ff010000000100, 0x00ff01000001ffff,
  1247. 0x00ff01000001ff01, 0x00ff010000010000, 0x00ff010000010001, 0x00ff010000010101,
  1248. 0x00ff010001ff0001, 0x00ff010001ff0100, 0x00ff01000100ff01, 0x00ff010001000000,
  1249. 0x00ff010001000001, 0x00ff0100010001ff, 0x00ff01000101ff00, 0x00ff0100010100ff,
  1250. 0x00ff010001010001, 0x00ff010001010100, 0x00ff0101ff000001, 0x00ff010100ff00ff,
  1251. 0x00ff010100ff0001, 0x00ff010100ff0100, 0x00ff010100000000, 0x00ff0101000001ff,
  1252. 0x00ff010100000101, 0x00ff0101000100ff, 0x00ff010100010100, 0x00ff0101010000ff,
  1253. 0x00ff010101010000, 0x0000ffffffffff00, 0x0000ffffffff00ff, 0x0000ffffffff0000,
  1254. 0x0000ffffffff0001, 0x0000ffffffff0100, 0x0000ffffff00ff01, 0x0000ffffff000000,
  1255. 0x0000ffffff000101, 0x0000ffffff01ff00, 0x0000ffffff0100ff, 0x0000ffffff010100,
  1256. 0x0000ffff00ffffff, 0x0000ffff00ff0000, 0x0000ffff00ff01ff, 0x0000ffff0000ff00,
  1257. 0x0000ffff000000ff, 0x0000ffff00000000, 0x0000ffff00000001, 0x0000ffff00000100,
  1258. 0x0000ffff00010000, 0x0000ffff000101ff, 0x0000ffff01ff0001, 0x0000ffff01ff0100,
  1259. 0x0000ffff01000000, 0x0000ffff010001ff, 0x0000ffff0101ffff, 0x0000ffff0101ff00,
  1260. 0x0000ffff01010001, 0x0000ffff01010100, 0x0000ff00ffff0000, 0x0000ff00ffff01ff,
  1261. 0x0000ff00ffff0100, 0x0000ff00ffff0101, 0x0000ff00ff00ff00, 0x0000ff00ff0000ff,
  1262. 0x0000ff00ff000000, 0x0000ff00ff000001, 0x0000ff00ff0001ff, 0x0000ff00ff000100,
  1263. 0x0000ff00ff01ffff, 0x0000ff00ff010000, 0x0000ff00ff010001, 0x0000ff00ff0101ff,
  1264. 0x0000ff00ff010101, 0x0000ff0000ffff00, 0x0000ff0000ff00ff, 0x0000ff0000ff0000,
  1265. 0x0000ff0000ff0001, 0x0000ff0000ff0100, 0x0000ff000000ffff, 0x0000ff000000ff00,
  1266. 0x0000ff000000ff01, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1267. 0x0000ff00000001ff, 0x0000ff0000000100, 0x0000ff0000000101, 0x0000ff000001ff00,
  1268. 0x0000ff00000100ff, 0x0000ff0000010000, 0x0000ff0000010001, 0x0000ff0000010100,
  1269. 0x0000ff0001ffff01, 0x0000ff0001ff0000, 0x0000ff000100ff00, 0x0000ff00010000ff,
  1270. 0x0000ff0001000000, 0x0000ff0001000001, 0x0000ff0001000100, 0x0000ff000101ffff,
  1271. 0x0000ff0001010000, 0x0000ff0001010101, 0x0000ff01ffffff00, 0x0000ff01ffff0001,
  1272. 0x0000ff01ff00ff01, 0x0000ff01ff000000, 0x0000ff01ff000101, 0x0000ff01ff01ff00,
  1273. 0x0000ff01ff0100ff, 0x0000ff0100ffff01, 0x0000ff0100ff0000, 0x0000ff0100ff0101,
  1274. 0x0000ff010000ff00, 0x0000ff01000000ff, 0x0000ff0100000000, 0x0000ff0100000001,
  1275. 0x0000ff0100000100, 0x0000ff010001ff01, 0x0000ff0100010000, 0x0000ff0101ff0000,
  1276. 0x0000ff010100ffff, 0x0000ff010100ff01, 0x0000ff0101000000, 0x0000ff0101000100,
  1277. 0x0000ff0101000101, 0x0000ff01010100ff, 0x000000ffffff00ff, 0x000000ffffff0000,
  1278. 0x000000ffff00ff00, 0x000000ffff0000ff, 0x000000ffff000000, 0x000000ffff000001,
  1279. 0x000000ffff0001ff, 0x000000ffff000100, 0x000000ffff01ff00, 0x000000ffff010000,
  1280. 0x000000ffff0101ff, 0x000000ffff010101, 0x000000ff00ffff00, 0x000000ff00ff00ff,
  1281. 0x000000ff00ff0000, 0x000000ff00ff0001, 0x000000ff00ff0100, 0x000000ff00ff0101,
  1282. 0x000000ff0000ffff, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1283. 0x000000ff00000001, 0x000000ff000001ff, 0x000000ff00000100, 0x000000ff00000101,
  1284. 0x000000ff0001ff00, 0x000000ff0001ff01, 0x000000ff000100ff, 0x000000ff00010000,
  1285. 0x000000ff00010001, 0x000000ff00010100, 0x000000ff01ffffff, 0x000000ff01ff01ff,
  1286. 0x000000ff01ff0101, 0x000000ff0100ff00, 0x000000ff010000ff, 0x000000ff01000000,
  1287. 0x000000ff01000001, 0x000000ff01000100, 0x000000ff0101ff00, 0x000000ff010100ff,
  1288. 0x000000ff01010000, 0x000000ff01010101, 0x00000000ffffff00, 0x00000000ffffff01,
  1289. 0x00000000ffff00ff, 0x00000000ffff0000, 0x00000000ffff0001, 0x00000000ffff0100,
  1290. 0x00000000ff00ffff, 0x00000000ff00ff00, 0x00000000ff00ff01, 0x00000000ff0000ff,
  1291. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff000101,
  1292. 0x00000000ff01ff00, 0x00000000ff0100ff, 0x00000000ff010000, 0x00000000ff010001,
  1293. 0x00000000ff010100, 0x0000000000ffffff, 0x0000000000ffff00, 0x0000000000ffff01,
  1294. 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001, 0x0000000000ff01ff,
  1295. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1296. 0x00000000000000ff, 0x0000000000000000, 0x0000000000000001, 0x00000000000001ff,
  1297. 0x0000000000000100, 0x0000000000000101, 0x000000000001ffff, 0x000000000001ff00,
  1298. 0x00000000000100ff, 0x0000000000010000, 0x0000000000010001, 0x00000000000101ff,
  1299. 0x0000000000010100, 0x0000000000010101, 0x0000000001ffff00, 0x0000000001ff00ff,
  1300. 0x0000000001ff0000, 0x0000000001ff0100, 0x0000000001ff0101, 0x000000000100ffff,
  1301. 0x000000000100ff00, 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001,
  1302. 0x00000000010001ff, 0x0000000001000100, 0x000000000101ff00, 0x00000000010100ff,
  1303. 0x0000000001010000, 0x0000000001010001, 0x0000000001010100, 0x00000001ffffffff,
  1304. 0x00000001ffffff00, 0x00000001ffffff01, 0x00000001ffff00ff, 0x00000001ffff0001,
  1305. 0x00000001ffff01ff, 0x00000001ffff0100, 0x00000001ff00ff00, 0x00000001ff0000ff,
  1306. 0x00000001ff000000, 0x00000001ff0001ff, 0x00000001ff000100, 0x00000001ff01ffff,
  1307. 0x00000001ff01ff00, 0x00000001ff01ff01, 0x00000001ff0100ff, 0x00000001ff010000,
  1308. 0x00000001ff010001, 0x00000001ff0101ff, 0x00000001ff010100, 0x0000000100ffff00,
  1309. 0x0000000100ff0000, 0x0000000100ff0001, 0x0000000100ff01ff, 0x0000000100ff0100,
  1310. 0x0000000100ff0101, 0x000000010000ffff, 0x000000010000ff00, 0x000000010000ff01,
  1311. 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001, 0x00000001000001ff,
  1312. 0x0000000100000100, 0x0000000100000101, 0x000000010001ff00, 0x00000001000100ff,
  1313. 0x0000000100010000, 0x0000000100010100, 0x0000000101ffff01, 0x0000000101ff0000,
  1314. 0x0000000101ff0001, 0x0000000101ff01ff, 0x0000000101ff0100, 0x0000000101ff0101,
  1315. 0x000000010100ff00, 0x0000000101000000, 0x0000000101000101, 0x000000010101ff01,
  1316. 0x0000000101010000, 0x0000000101010001, 0x00000001010101ff, 0x0000000101010100,
  1317. 0x000001ffffff00ff, 0x000001ffffff0000, 0x000001ffffff0001, 0x000001ffffff0100,
  1318. 0x000001ffff00ffff, 0x000001ffff000000, 0x000001ffff0001ff, 0x000001ffff01ff00,
  1319. 0x000001ffff010101, 0x000001ff00ff0000, 0x000001ff00ff01ff, 0x000001ff00ff0101,
  1320. 0x000001ff0000ff00, 0x000001ff000000ff, 0x000001ff00000000, 0x000001ff00000001,
  1321. 0x000001ff000001ff, 0x000001ff00000100, 0x000001ff0001ffff, 0x000001ff0001ff01,
  1322. 0x000001ff000100ff, 0x000001ff00010000, 0x000001ff01ffff01, 0x000001ff01ff0100,
  1323. 0x000001ff0100ffff, 0x000001ff0100ff01, 0x000001ff01000000, 0x000001ff010001ff,
  1324. 0x000001ff0101ff00, 0x000001ff01010100, 0x00000100ffffff00, 0x00000100ffffff01,
  1325. 0x00000100ffff0000, 0x00000100ffff0101, 0x00000100ff00ff00, 0x00000100ff0000ff,
  1326. 0x00000100ff000000, 0x00000100ff000001, 0x00000100ff000100, 0x00000100ff010000,
  1327. 0x0000010000ffff00, 0x0000010000ff00ff, 0x0000010000ff0000, 0x0000010000ff0001,
  1328. 0x0000010000ff0100, 0x000001000000ffff, 0x000001000000ff00, 0x000001000000ff01,
  1329. 0x00000100000000ff, 0x0000010000000000, 0x0000010000000001, 0x00000100000001ff,
  1330. 0x0000010000000100, 0x0000010000000101, 0x000001000001ff00, 0x00000100000100ff,
  1331. 0x0000010000010000, 0x0000010000010001, 0x0000010000010100, 0x0000010001ffff00,
  1332. 0x0000010001ff0000, 0x0000010001ff0100, 0x000001000100ff00, 0x00000100010000ff,
  1333. 0x0000010001000000, 0x0000010001000001, 0x00000100010001ff, 0x0000010001000100,
  1334. 0x0000010001010000, 0x00000101ffff00ff, 0x00000101ffff01ff, 0x00000101ff000000,
  1335. 0x00000101ff000101, 0x00000101ff01ffff, 0x00000101ff010000, 0x00000101ff010001,
  1336. 0x00000101ff010100, 0x0000010100ff0000, 0x0000010100ff01ff, 0x0000010100ff0100,
  1337. 0x000001010000ff00, 0x0000010100000000, 0x0000010100000001, 0x00000101000001ff,
  1338. 0x0000010100000100, 0x000001010001ff01, 0x0000010100010000, 0x00000101000101ff,
  1339. 0x0000010100010101, 0x0000010101ffff00, 0x0000010101ff0101, 0x000001010100ff01,
  1340. 0x0000010101000000, 0x0000010101000001, 0x00000101010001ff, 0x0000010101000101,
  1341. 0x000001010101ff00, 0x0001ffffffff0000, 0x0001ffffff0000ff, 0x0001ffffff000001,
  1342. 0x0001ffffff000100, 0x0001ffffff010000, 0x0001ffff00ff00ff, 0x0001ffff0000ffff,
  1343. 0x0001ffff00000000, 0x0001ffff00000001, 0x0001ffff000001ff, 0x0001ffff00000101,
  1344. 0x0001ffff0001ff00, 0x0001ffff000100ff, 0x0001ffff00010001, 0x0001ffff00010100,
  1345. 0x0001ffff01ffff00, 0x0001ffff01000001, 0x0001ffff01010000, 0x0001ff00ffffff00,
  1346. 0x0001ff00ffff00ff, 0x0001ff00ffff0001, 0x0001ff00ffff0100, 0x0001ff00ff00ff01,
  1347. 0x0001ff00ff000000, 0x0001ff00ff01ff00, 0x0001ff00ff01ff01, 0x0001ff00ff010001,
  1348. 0x0001ff00ff010100, 0x0001ff0000ff0000, 0x0001ff0000ff0100, 0x0001ff000000ff00,
  1349. 0x0001ff0000000000, 0x0001ff0000000001, 0x0001ff0000000100, 0x0001ff0000010000,
  1350. 0x0001ff0000010001, 0x0001ff0000010101, 0x0001ff0001ff00ff, 0x0001ff0001ff0101,
  1351. 0x0001ff000100ff01, 0x0001ff0001000000, 0x0001ff000101ff00, 0x0001ff0001010001,
  1352. 0x0001ff0001010100, 0x0001ff01ff00ff00, 0x0001ff01ff000001, 0x0001ff01ff000100,
  1353. 0x0001ff0100ffffff, 0x0001ff0100ffff00, 0x0001ff0100ff0001, 0x0001ff0100000000,
  1354. 0x0001ff0100000001, 0x0001ff01000001ff, 0x0001ff010001ffff, 0x0001ff0101ff0000,
  1355. 0x0001ff010100ff00, 0x0001ff0101000001, 0x0001ff0101010000, 0x000100ffff00ff00,
  1356. 0x000100ffff00ff01, 0x000100ffff000000, 0x000100ffff000001, 0x000100ffff000101,
  1357. 0x000100ffff01ff00, 0x000100ffff010001, 0x000100ffff010100, 0x000100ff00ffffff,
  1358. 0x000100ff00ffff01, 0x000100ff00ff0000, 0x000100ff00ff01ff, 0x000100ff00ff0101,
  1359. 0x000100ff0000ff00, 0x000100ff000000ff, 0x000100ff00000000, 0x000100ff00000001,
  1360. 0x000100ff00000100, 0x000100ff00000101, 0x000100ff0001ffff, 0x000100ff0001ff01,
  1361. 0x000100ff00010000, 0x000100ff01ff00ff, 0x000100ff01ff0000, 0x000100ff01ff0100,
  1362. 0x000100ff0100ffff, 0x000100ff0100ff01, 0x000100ff010000ff, 0x000100ff01000000,
  1363. 0x000100ff01000001, 0x000100ff010001ff, 0x000100ff01000101, 0x000100ff0101ff00,
  1364. 0x000100ff010100ff, 0x000100ff01010100, 0x00010000ffff0000, 0x00010000ffff01ff,
  1365. 0x00010000ffff0101, 0x00010000ff00ff00, 0x00010000ff000000, 0x00010000ff000001,
  1366. 0x00010000ff000100, 0x0001000000ff00ff, 0x0001000000ff0000, 0x0001000000ff0001,
  1367. 0x0001000000ff0100, 0x000100000000ffff, 0x000100000000ff00, 0x00010000000000ff,
  1368. 0x0001000000000000, 0x0001000000000001, 0x0001000000000100, 0x000100000001ff00,
  1369. 0x00010000000100ff, 0x0001000000010000, 0x0001000000010001, 0x0001000000010100,
  1370. 0x0001000001ff0001, 0x0001000001ff0100, 0x0001000001ff0101, 0x000100000100ff00,
  1371. 0x0001000001000000, 0x0001000001000001, 0x0001000001000100, 0x0001000001000101,
  1372. 0x000100000101ff01, 0x0001000001010000, 0x0001000001010001, 0x00010000010101ff,
  1373. 0x00010001ffffff01, 0x00010001ffff0100, 0x00010001ff000000, 0x00010001ff01ffff,
  1374. 0x00010001ff010001, 0x00010001ff0101ff, 0x00010001ff010100, 0x0001000100ffffff,
  1375. 0x0001000100ff0000, 0x0001000100ff01ff, 0x0001000100ff0101, 0x000100010000ff00,
  1376. 0x00010001000000ff, 0x0001000100000000, 0x0001000100000001, 0x00010001000001ff,
  1377. 0x0001000100000101, 0x000100010001ffff, 0x0001000100010000, 0x00010001000101ff,
  1378. 0x0001000101ffffff, 0x0001000101ffff01, 0x0001000101ff0000, 0x0001000101ff0101,
  1379. 0x00010001010000ff, 0x0001000101000001, 0x00010001010001ff, 0x0001000101000100,
  1380. 0x000100010101ffff, 0x00010001010100ff, 0x0001000101010001, 0x0001000101010101,
  1381. 0x000101ffff000001, 0x000101ffff000100, 0x000101ffff010000, 0x000101ff00ffff00,
  1382. 0x000101ff0000ff01, 0x000101ff00000000, 0x000101ff00000101, 0x000101ff0001ff00,
  1383. 0x000101ff00010100, 0x000101ff01ff0000, 0x000101ff0100ff00, 0x000101ff010001ff,
  1384. 0x000101ff01010001, 0x00010100ffffff00, 0x00010100ffff00ff, 0x00010100ff00ffff,
  1385. 0x00010100ff000000, 0x00010100ff01ff00, 0x00010100ff0100ff, 0x00010100ff010001,
  1386. 0x00010100ff010100, 0x0001010000ffffff, 0x0001010000ffff00, 0x0001010000ff0000,
  1387. 0x0001010000ff0001, 0x0001010000ff01ff, 0x000101000000ff00, 0x00010100000000ff,
  1388. 0x0001010000000000, 0x0001010000000001, 0x0001010000000100, 0x000101000001ffff,
  1389. 0x0001010000010000, 0x0001010000010101, 0x0001010001ffff01, 0x0001010001ff00ff,
  1390. 0x0001010001ff0101, 0x0001010001000000, 0x000101000101ff00, 0x00010100010100ff,
  1391. 0x0001010001010000, 0x0001010001010100, 0x00010101ff00ff00, 0x00010101ff000001,
  1392. 0x00010101ff0001ff, 0x0001010100ffff00, 0x0001010100ff00ff, 0x0001010100ff0100,
  1393. 0x000101010000ffff, 0x0001010100000000, 0x00010101000001ff, 0x0001010100000101,
  1394. 0x00010101000100ff, 0x0001010100010000, 0x0001010100010100, 0x0001010101ff0001,
  1395. 0x00010101010000ff, 0x00010101010001ff, 0x0001010101000101, 0x0001010101010001,
  1396. 0x01ffffffffffffff, 0x01ffffffffffff01, 0x01ffffffffff01ff, 0x01ffffffffff0101,
  1397. 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff, 0x01ffffffff010101,
  1398. 0x01ffffff00ff0000, 0x01ffffff0000ffff, 0x01ffffff0000ff00, 0x01ffffff000000ff,
  1399. 0x01ffffff00000001, 0x01ffffff00000100, 0x01ffffff00010000, 0x01ffffff01ffffff,
  1400. 0x01ffffff01ffff01, 0x01ffffff01ff01ff, 0x01ffffff01ff0101, 0x01ffffff01000000,
  1401. 0x01ffffff0101ffff, 0x01ffffff0101ff01, 0x01ffffff010101ff, 0x01ffffff01010101,
  1402. 0x01ffff00ffff0000, 0x01ffff00ff00ff00, 0x01ffff00ff0000ff, 0x01ffff00ff000001,
  1403. 0x01ffff00ff000100, 0x01ffff00ff010000, 0x01ffff0000ffff00, 0x01ffff0000ff00ff,
  1404. 0x01ffff0000ff0100, 0x01ffff000000ffff, 0x01ffff000000ff01, 0x01ffff0000000000,
  1405. 0x01ffff0000000001, 0x01ffff00000001ff, 0x01ffff0000000100, 0x01ffff00000100ff,
  1406. 0x01ffff0000010001, 0x01ffff0000010100, 0x01ffff0001ff0000, 0x01ffff0001ff0100,
  1407. 0x01ffff00010000ff, 0x01ffff0001000001, 0x01ffff0001000100, 0x01ffff0001010000,
  1408. 0x01ffff01ffffffff, 0x01ffff01ffffff01, 0x01ffff01ffff01ff, 0x01ffff01ffff0101,
  1409. 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff01ff01, 0x01ffff01ff0101ff,
  1410. 0x01ffff01ff010101, 0x01ffff010000ff00, 0x01ffff01000000ff, 0x01ffff0100000100,
  1411. 0x01ffff0100010000, 0x01ffff0101ffffff, 0x01ffff0101ffff01, 0x01ffff0101ff01ff,
  1412. 0x01ffff0101ff0101, 0x01ffff0101000000, 0x01ffff010101ffff, 0x01ffff010101ff01,
  1413. 0x01ffff01010101ff, 0x01ffff0101010101, 0x01ff00ffff0000ff, 0x01ff00ffff000100,
  1414. 0x01ff00ff00ffff00, 0x01ff00ff00ff00ff, 0x01ff00ff0000ff00, 0x01ff00ff00000000,
  1415. 0x01ff00ff00000101, 0x01ff00ff0001ff00, 0x01ff00ff000100ff, 0x01ff00ff00010100,
  1416. 0x01ff00ff010000ff, 0x01ff00ff01000100, 0x01ff0000ffffff00, 0x01ff0000ffff0100,
  1417. 0x01ff0000ff00ff01, 0x01ff0000ff000000, 0x01ff0000ff000101, 0x01ff0000ff010001,
  1418. 0x01ff0000ff010100, 0x01ff000000ffffff, 0x01ff000000ffff00, 0x01ff000000ff0000,
  1419. 0x01ff000000ff01ff, 0x01ff00000000ff00, 0x01ff0000000000ff, 0x01ff000000000000,
  1420. 0x01ff000000000001, 0x01ff000000000100, 0x01ff000000000101, 0x01ff000000010000,
  1421. 0x01ff000000010001, 0x01ff0000000101ff, 0x01ff000000010101, 0x01ff000001ffff00,
  1422. 0x01ff000001ff00ff, 0x01ff000001ff0001, 0x01ff000001ff0100, 0x01ff00000100ffff,
  1423. 0x01ff00000100ff01, 0x01ff000001000000, 0x01ff0000010001ff, 0x01ff000001010001,
  1424. 0x01ff0001ff00ff00, 0x01ff0001ff000001, 0x01ff0001ff000100, 0x01ff0001ff010000,
  1425. 0x01ff000100ffff00, 0x01ff000100ff00ff, 0x01ff000100ff0100, 0x01ff000100ff0101,
  1426. 0x01ff00010000ffff, 0x01ff000100000000, 0x01ff000100000100, 0x01ff000100000101,
  1427. 0x01ff00010001ff00, 0x01ff000100010001, 0x01ff000100010101, 0x01ff000101ff0000,
  1428. 0x01ff00010100ff00, 0x01ff000101000101, 0x01ff0001010100ff, 0x01ff01ffffffffff,
  1429. 0x01ff01ffffffff01, 0x01ff01ffffff01ff, 0x01ff01ffffff0101, 0x01ff01ffff000000,
  1430. 0x01ff01ffff01ffff, 0x01ff01ffff01ff01, 0x01ff01ffff0101ff, 0x01ff01ffff010101,
  1431. 0x01ff01ff00ffff00, 0x01ff01ff00ff0000, 0x01ff01ff0000ff00, 0x01ff01ff000000ff,
  1432. 0x01ff01ff00000100, 0x01ff01ff00010000, 0x01ff01ff00010100, 0x01ff01ff01ffffff,
  1433. 0x01ff01ff01ffff01, 0x01ff01ff01ff01ff, 0x01ff01ff01ff0101, 0x01ff01ff01000000,
  1434. 0x01ff01ff0101ffff, 0x01ff01ff0101ff01, 0x01ff01ff010101ff, 0x01ff01ff01010101,
  1435. 0x01ff0100ffff0000, 0x01ff0100ffff0001, 0x01ff0100ff00ff00, 0x01ff0100ff0000ff,
  1436. 0x01ff0100ff000001, 0x01ff0100ff010000, 0x01ff010000ffff00, 0x01ff010000ff00ff,
  1437. 0x01ff010000ff0001, 0x01ff010000ff0100, 0x01ff01000000ffff, 0x01ff01000000ff01,
  1438. 0x01ff010000000000, 0x01ff010000000101, 0x01ff01000001ff00, 0x01ff0100000100ff,
  1439. 0x01ff010001ff0000, 0x01ff010001000001, 0x01ff010001000100, 0x01ff010001010000,
  1440. 0x01ff0101ffffffff, 0x01ff0101ffffff01, 0x01ff0101ffff01ff, 0x01ff0101ffff0101,
  1441. 0x01ff0101ff000000, 0x01ff0101ff01ffff, 0x01ff0101ff01ff01, 0x01ff0101ff0101ff,
  1442. 0x01ff0101ff010101, 0x01ff010100ff0000, 0x01ff01010000ff00, 0x01ff0101000000ff,
  1443. 0x01ff010100000001, 0x01ff010101ffffff, 0x01ff010101ffff01, 0x01ff010101ff01ff,
  1444. 0x01ff010101ff0101, 0x01ff010101000000, 0x01ff01010101ffff, 0x01ff01010101ff01,
  1445. 0x01ff0101010101ff, 0x01ff010101010101, 0x0100ffffffff0000, 0x0100ffffff00ff00,
  1446. 0x0100ffffff000001, 0x0100ffffff0001ff, 0x0100ffffff000100, 0x0100ffffff010000,
  1447. 0x0100ffff00ffff00, 0x0100ffff00ff0001, 0x0100ffff00ff0100, 0x0100ffff00000000,
  1448. 0x0100ffff000001ff, 0x0100ffff00000101, 0x0100ffff00010100, 0x0100ffff00010101,
  1449. 0x0100ffff01ff0000, 0x0100ffff0100ff00, 0x0100ffff010000ff, 0x0100ffff01000001,
  1450. 0x0100ffff01000100, 0x0100ffff01010000, 0x0100ff00ffffff00, 0x0100ff00ffff00ff,
  1451. 0x0100ff00ffff0001, 0x0100ff00ffff0100, 0x0100ff00ff00ffff, 0x0100ff00ff000000,
  1452. 0x0100ff00ff0001ff, 0x0100ff00ff000101, 0x0100ff00ff01ff00, 0x0100ff00ff0100ff,
  1453. 0x0100ff00ff010001, 0x0100ff00ff010100, 0x0100ff0000ffffff, 0x0100ff0000ff0000,
  1454. 0x0100ff000000ffff, 0x0100ff000000ff00, 0x0100ff00000000ff, 0x0100ff0000000000,
  1455. 0x0100ff0000000001, 0x0100ff0000000100, 0x0100ff000001ff01, 0x0100ff0000010000,
  1456. 0x0100ff0001ff00ff, 0x0100ff0001ff0001, 0x0100ff000100ff01, 0x0100ff0001000000,
  1457. 0x0100ff00010001ff, 0x0100ff000101ff00, 0x0100ff00010100ff, 0x0100ff0001010001,
  1458. 0x0100ff0001010100, 0x0100ff01ffff0000, 0x0100ff01ff00ff00, 0x0100ff01ff0000ff,
  1459. 0x0100ff01ff000100, 0x0100ff01ff010000, 0x0100ff0100ff00ff, 0x0100ff0100ff0001,
  1460. 0x0100ff0100ff0100, 0x0100ff010000ffff, 0x0100ff010000ff01, 0x0100ff0100000000,
  1461. 0x0100ff01000001ff, 0x0100ff0100010001, 0x0100ff0100010100, 0x0100ff0101ff0000,
  1462. 0x0100ff01010000ff, 0x0100ff0101000001, 0x0100ff0101010100, 0x010000ffffffff00,
  1463. 0x010000ffffff00ff, 0x010000ffffff0001, 0x010000ffff00ffff, 0x010000ffff000000,
  1464. 0x010000ffff0001ff, 0x010000ffff010001, 0x010000ff00ffffff, 0x010000ff00ff0101,
  1465. 0x010000ff0000ff00, 0x010000ff000000ff, 0x010000ff00000000, 0x010000ff00000001,
  1466. 0x010000ff000001ff, 0x010000ff00000100, 0x010000ff0001ffff, 0x010000ff0001ff00,
  1467. 0x010000ff0001ff01, 0x010000ff00010000, 0x010000ff01ff00ff, 0x010000ff01ff0001,
  1468. 0x010000ff0100ff01, 0x010000ff010000ff, 0x010000ff01000000, 0x010000ff010001ff,
  1469. 0x010000ff0101ff00, 0x010000ff01010100, 0x01000000ffffffff, 0x01000000ffff0000,
  1470. 0x01000000ffff01ff, 0x01000000ffff0101, 0x01000000ff00ffff, 0x01000000ff00ff00,
  1471. 0x01000000ff0000ff, 0x01000000ff000000, 0x01000000ff000001, 0x01000000ff000100,
  1472. 0x01000000ff01ff00, 0x01000000ff010000, 0x01000000ff010100, 0x01000000ff010101,
  1473. 0x0100000000ffff00, 0x0100000000ff00ff, 0x0100000000ff0000, 0x0100000000ff0001,
  1474. 0x0100000000ff0100, 0x010000000000ffff, 0x010000000000ff00, 0x010000000000ff01,
  1475. 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001, 0x01000000000001ff,
  1476. 0x0100000000000100, 0x0100000000000101, 0x010000000001ff00, 0x01000000000100ff,
  1477. 0x0100000000010000, 0x0100000000010001, 0x0100000000010100, 0x0100000001ffff00,
  1478. 0x0100000001ff0000, 0x0100000001ff01ff, 0x010000000100ff00, 0x010000000100ff01,
  1479. 0x01000000010000ff, 0x0100000001000000, 0x0100000001000001, 0x0100000001000100,
  1480. 0x0100000001000101, 0x010000000101ffff, 0x010000000101ff01, 0x0100000001010000,
  1481. 0x01000000010101ff, 0x0100000001010101, 0x01000001ffffff00, 0x01000001ffff00ff,
  1482. 0x01000001ff00ffff, 0x01000001ff000000, 0x01000001ff000100, 0x01000001ff01ffff,
  1483. 0x01000001ff010001, 0x01000001ff010100, 0x0100000100ff0000, 0x0100000100ff01ff,
  1484. 0x0100000100ff0100, 0x010000010000ff00, 0x010000010000ff01, 0x0100000100000000,
  1485. 0x0100000100000001, 0x0100000100000100, 0x0100000100010000, 0x01000001000101ff,
  1486. 0x0100000101ffff01, 0x0100000101ff00ff, 0x0100000101ff0100, 0x0100000101ff0101,
  1487. 0x010000010100ff01, 0x01000001010000ff, 0x0100000101000000, 0x01000001010100ff,
  1488. 0x0100000101010001, 0x0100000101010100, 0x010001ffffff0000, 0x010001ffff000001,
  1489. 0x010001ffff000100, 0x010001ffff010000, 0x010001ff00ffff00, 0x010001ff00ff0001,
  1490. 0x010001ff0000ffff, 0x010001ff0000ff01, 0x010001ff00000000, 0x010001ff00000001,
  1491. 0x010001ff00000101, 0x010001ff000100ff, 0x010001ff00010000, 0x010001ff01ff0000,
  1492. 0x010001ff0100ff00, 0x010001ff01000001, 0x010001ff01000100, 0x010001ff01010000,
  1493. 0x01000100ffff00ff, 0x01000100ffff0001, 0x01000100ffff0100, 0x01000100ff00ffff,
  1494. 0x01000100ff00ff01, 0x01000100ff000000, 0x01000100ff0001ff, 0x01000100ff000101,
  1495. 0x01000100ff01ffff, 0x01000100ff01ff00, 0x01000100ff0100ff, 0x01000100ff010001,
  1496. 0x0100010000ffffff, 0x0100010000ffff01, 0x0100010000ff0000, 0x0100010000ff01ff,
  1497. 0x0100010000ff0101, 0x010001000000ff00, 0x01000100000000ff, 0x0100010000000000,
  1498. 0x0100010000000001, 0x0100010000000100, 0x010001000001ff01, 0x0100010000010000,
  1499. 0x0100010000010001, 0x0100010000010101, 0x0100010001ffff00, 0x0100010001ff00ff,
  1500. 0x010001000100ffff, 0x010001000100ff01, 0x0100010001000000, 0x0100010001000101,
  1501. 0x010001000101ff00, 0x0100010001010001, 0x01000101ffff0000, 0x01000101ff000000,
  1502. 0x01000101ff010000, 0x0100010100ff00ff, 0x0100010100ff0001, 0x0100010100ff0100,
  1503. 0x010001010000ffff, 0x0100010100000000, 0x01000101000001ff, 0x010001010001ff00,
  1504. 0x0100010101ff0000, 0x010001010100ff00, 0x01000101010000ff, 0x0100010101000000,
  1505. 0x0100010101000001, 0x0101ffffffffffff, 0x0101ffffffffff01, 0x0101ffffffff01ff,
  1506. 0x0101ffffffff0101, 0x0101ffffff000000, 0x0101ffffff01ffff, 0x0101ffffff01ff01,
  1507. 0x0101ffffff0101ff, 0x0101ffffff010101, 0x0101ffff00ff0000, 0x0101ffff0000ff00,
  1508. 0x0101ffff000000ff, 0x0101ffff00000001, 0x0101ffff00000100, 0x0101ffff01ffffff,
  1509. 0x0101ffff01ffff01, 0x0101ffff01ff01ff, 0x0101ffff01ff0101, 0x0101ffff01000000,
  1510. 0x0101ffff0101ffff, 0x0101ffff0101ff01, 0x0101ffff010101ff, 0x0101ffff01010101,
  1511. 0x0101ff00ffff0000, 0x0101ff00ffff0100, 0x0101ff00ff00ff00, 0x0101ff00ff0000ff,
  1512. 0x0101ff00ff000001, 0x0101ff00ff000100, 0x0101ff00ff000101, 0x0101ff0000ff0001,
  1513. 0x0101ff0000ff0100, 0x0101ff000000ff00, 0x0101ff0000000000, 0x0101ff00000001ff,
  1514. 0x0101ff0000000101, 0x0101ff000001ff00, 0x0101ff00000100ff, 0x0101ff0001ff0000,
  1515. 0x0101ff000100ffff, 0x0101ff000100ff01, 0x0101ff0001000001, 0x0101ff0001000100,
  1516. 0x0101ff01ffffff01, 0x0101ff01ffff01ff, 0x0101ff01ffff0101, 0x0101ff01ff00ffff,
  1517. 0x0101ff01ff000100, 0x0101ff01ff01ff01, 0x0101ff01ff0101ff, 0x0101ff01ff010101,
  1518. 0x0101ff0100ff0000, 0x0101ff010000ff00, 0x0101ff0100000001, 0x0101ff0100000100,
  1519. 0x0101ff0100010000, 0x0101ff0101ffffff, 0x0101ff0101ffff01, 0x0101ff0101ff01ff,
  1520. 0x0101ff0101ff0101, 0x0101ff0101000000, 0x0101ff010101ffff, 0x0101ff010101ff01,
  1521. 0x0101ff01010101ff, 0x0101ff0101010101, 0x010100ffff000100, 0x010100ffff010000,
  1522. 0x010100ff00ffff00, 0x010100ff00ff00ff, 0x010100ff0000ffff, 0x010100ff000000ff,
  1523. 0x010100ff00000000, 0x010100ff000001ff, 0x010100ff00000101, 0x010100ff0001ff00,
  1524. 0x010100ff00010000, 0x010100ff00010001, 0x010100ff000101ff, 0x010100ff00010100,
  1525. 0x010100ff01ff0000, 0x01010000ffff0001, 0x01010000ffff0100, 0x01010000ff00ffff,
  1526. 0x01010000ff00ff01, 0x01010000ff000000, 0x01010000ff0001ff, 0x01010000ff010001,
  1527. 0x01010000ff010100, 0x0101000000ffff01, 0x0101000000ff0000, 0x010100000000ff00,
  1528. 0x01010000000000ff, 0x0101000000000000, 0x0101000000000001, 0x0101000000000100,
  1529. 0x0101000000010000, 0x0101000000010101, 0x0101000001ffff00, 0x0101000001ff00ff,
  1530. 0x0101000001ff0000, 0x0101000001ff0001, 0x0101000001ff0100, 0x010100000100ff01,
  1531. 0x0101000001000000, 0x01010000010001ff, 0x01010001ffff0000, 0x01010001ff00ff00,
  1532. 0x01010001ff000001, 0x01010001ff000101, 0x01010001ff01ff00, 0x01010001ff010000,
  1533. 0x0101000100ff00ff, 0x0101000100ff0001, 0x0101000100ff0101, 0x010100010000ff01,
  1534. 0x0101000100000000, 0x0101000100000001, 0x01010001000001ff, 0x010100010001ffff,
  1535. 0x010100010001ff01, 0x0101000101ff0001, 0x010100010100ffff, 0x0101000101000000,
  1536. 0x0101000101000001, 0x0101000101000100, 0x010100010101ff00, 0x01010001010100ff,
  1537. 0x0101000101010001, 0x010101ffffffffff, 0x010101ffffffff01, 0x010101ffffff01ff,
  1538. 0x010101ffffff0101, 0x010101ffff01ffff, 0x010101ffff01ff01, 0x010101ffff0101ff,
  1539. 0x010101ffff010101, 0x010101ff0000ff00, 0x010101ff000000ff, 0x010101ff00000001,
  1540. 0x010101ff00000100, 0x010101ff01ffffff, 0x010101ff01ffff01, 0x010101ff01ff01ff,
  1541. 0x010101ff01ff0101, 0x010101ff01000000, 0x010101ff0101ffff, 0x010101ff0101ff01,
  1542. 0x010101ff010101ff, 0x010101ff01010101, 0x01010100ffff0000, 0x01010100ff0000ff,
  1543. 0x01010100ff000100, 0x01010100ff01ff00, 0x01010100ff010000, 0x0101010000ffff00,
  1544. 0x010101000000ffff, 0x0101010000000000, 0x0101010000000101, 0x010101000001ff00,
  1545. 0x0101010000010001, 0x0101010000010100, 0x010101000100ffff, 0x0101010001000001,
  1546. 0x01010101ffffffff, 0x01010101ffffff01, 0x01010101ffff01ff, 0x01010101ffff0101,
  1547. 0x01010101ff01ffff, 0x01010101ff01ff01, 0x01010101ff0101ff, 0x01010101ff010101,
  1548. 0x010101010000ff00, 0x01010101000000ff, 0x0101010100000001, 0x0101010101ffffff,
  1549. 0x0101010101ffff01, 0x0101010101ff01ff, 0x0101010101ff0101, 0x0101010101000000,
  1550. 0x010101010101ffff, 0x010101010101ff01, 0x01010101010101ff, 0x0101010101010101,
  1551. GGML_TABLE_END()
  1552. #else
  1553. GGML_TABLE_BEGIN(uint32_t, iq1s_grid_gpu, NGRID_IQ1S)
  1554. 0x00000000, 0x00000002, 0x00000101, 0x00000200, 0x00000202, 0x00010001, 0x00010101, 0x00020000,
  1555. 0x00020002, 0x00020200, 0x00020202, 0x01000101, 0x01010001, 0x01010100, 0x01010102, 0x01020101,
  1556. 0x02000000, 0x02000002, 0x02000200, 0x02000202, 0x02010101, 0x02020000, 0x02020002, 0x02020200,
  1557. 0x02020202, 0x00000110, 0x00000111, 0x00010011, 0x00010110, 0x00010112, 0x00010211, 0x00010212,
  1558. 0x00020111, 0x01000011, 0x01000112, 0x01000211, 0x01010012, 0x01010111, 0x01010212, 0x01020011,
  1559. 0x01020110, 0x01020112, 0x01020210, 0x02000111, 0x02010011, 0x02010110, 0x02010112, 0x02020111,
  1560. 0x00000020, 0x00000022, 0x00000220, 0x00000222, 0x00010121, 0x00020020, 0x00020022, 0x00020220,
  1561. 0x00020222, 0x01000121, 0x01010021, 0x01010221, 0x01020120, 0x01020221, 0x02000020, 0x02000022,
  1562. 0x02000220, 0x02000222, 0x02010021, 0x02010121, 0x02010221, 0x02020020, 0x02020022, 0x02020220,
  1563. 0x02020222, 0x00011001, 0x00011100, 0x00011102, 0x00021101, 0x01001001, 0x01001201, 0x01011101,
  1564. 0x01011202, 0x01021100, 0x01021101, 0x02011001, 0x02011201, 0x02021101, 0x00001011, 0x00001110,
  1565. 0x00001111, 0x00001112, 0x00011111, 0x00011210, 0x00011212, 0x00021211, 0x01001010, 0x01001111,
  1566. 0x01001212, 0x01011010, 0x01011011, 0x01011110, 0x01011111, 0x01011112, 0x01011211, 0x01021010,
  1567. 0x01021012, 0x01021111, 0x01021210, 0x01021212, 0x02001011, 0x02011011, 0x02011111, 0x02011210,
  1568. 0x02011212, 0x02021011, 0x02021110, 0x02021111, 0x02021112, 0x02021211, 0x00011120, 0x00011221,
  1569. 0x01001021, 0x01001120, 0x01011020, 0x01011022, 0x01011121, 0x01011220, 0x01021020, 0x01021021,
  1570. 0x01021122, 0x01021221, 0x02001121, 0x02011021, 0x02011120, 0x02011221, 0x00002000, 0x00002002,
  1571. 0x00002200, 0x00002202, 0x00012101, 0x00022000, 0x00022002, 0x00022200, 0x00022202, 0x01002101,
  1572. 0x01012001, 0x01012102, 0x01022101, 0x02002000, 0x02002002, 0x02002200, 0x02002202, 0x02012101,
  1573. 0x02022000, 0x02022002, 0x02022200, 0x02022202, 0x00002111, 0x00012011, 0x00012110, 0x00012211,
  1574. 0x00022110, 0x00022111, 0x01002011, 0x01012010, 0x01012011, 0x01012111, 0x01022011, 0x01022110,
  1575. 0x01022211, 0x02012011, 0x02012110, 0x02012112, 0x02012211, 0x02022111, 0x00002020, 0x00002022,
  1576. 0x00002220, 0x00002222, 0x00012121, 0x00022020, 0x00022022, 0x00022220, 0x00022222, 0x01002121,
  1577. 0x01012021, 0x01012221, 0x01022021, 0x01022121, 0x02002020, 0x02002022, 0x02002121, 0x02002220,
  1578. 0x02002222, 0x02012121, 0x02022020, 0x02022022, 0x02022220, 0x02022222, 0x00110000, 0x00110001,
  1579. 0x00110100, 0x00110201, 0x00120100, 0x00120101, 0x01100001, 0x01100100, 0x01110000, 0x01110101,
  1580. 0x01110200, 0x01120001, 0x01120100, 0x01120101, 0x01120201, 0x02110001, 0x02110100, 0x02110102,
  1581. 0x02120001, 0x02120101, 0x00100011, 0x00100110, 0x00100112, 0x00100211, 0x00110010, 0x00110012,
  1582. 0x00110111, 0x00110210, 0x00120011, 0x00120110, 0x00120211, 0x01100111, 0x01100212, 0x01110010,
  1583. 0x01110011, 0x01110012, 0x01110110, 0x01110111, 0x01110112, 0x01110211, 0x01120010, 0x01120111,
  1584. 0x02100110, 0x02110012, 0x02110111, 0x02120011, 0x02120110, 0x00110021, 0x00110120, 0x00110122,
  1585. 0x00120121, 0x01100020, 0x01100122, 0x01100221, 0x01110022, 0x01110121, 0x01110220, 0x01110222,
  1586. 0x01120120, 0x01120122, 0x02100121, 0x02110021, 0x02110120, 0x02110122, 0x02120121, 0x00101001,
  1587. 0x00101102, 0x00101201, 0x00111100, 0x00111101, 0x00111200, 0x00111201, 0x00121001, 0x00121102,
  1588. 0x01101001, 0x01101101, 0x01101102, 0x01101200, 0x01101202, 0x01111001, 0x01111100, 0x01111101,
  1589. 0x01111102, 0x01111201, 0x01121002, 0x01121101, 0x01121200, 0x02101100, 0x02101201, 0x02111000,
  1590. 0x02111100, 0x02111101, 0x02111200, 0x02111201, 0x02111202, 0x02121001, 0x02121100, 0x02121101,
  1591. 0x02121201, 0x00101012, 0x00101111, 0x00101212, 0x00111011, 0x00111110, 0x00111111, 0x00111112,
  1592. 0x00111211, 0x00121010, 0x00121012, 0x00121111, 0x00121210, 0x00121212, 0x01101011, 0x01101110,
  1593. 0x01101111, 0x01101112, 0x01111011, 0x01111012, 0x01111110, 0x01111111, 0x01111112, 0x01111211,
  1594. 0x01111212, 0x01121011, 0x01121110, 0x01121111, 0x01121112, 0x01121211, 0x02101010, 0x02101012,
  1595. 0x02101110, 0x02101111, 0x02101210, 0x02101212, 0x02111010, 0x02111011, 0x02111110, 0x02111111,
  1596. 0x02111112, 0x02111211, 0x02111212, 0x02121010, 0x02121012, 0x02121111, 0x00101021, 0x00101120,
  1597. 0x00101121, 0x00101122, 0x00111121, 0x00111122, 0x00111220, 0x00111222, 0x00121021, 0x00121122,
  1598. 0x01101020, 0x01101022, 0x01101120, 0x01101121, 0x01101220, 0x01101222, 0x01111021, 0x01111121,
  1599. 0x01111122, 0x01111220, 0x01111221, 0x01121021, 0x01121120, 0x01121121, 0x01121220, 0x01121221,
  1600. 0x01121222, 0x02101122, 0x02101222, 0x02111022, 0x02111121, 0x02121120, 0x02121221, 0x00112001,
  1601. 0x00112102, 0x00122101, 0x01102001, 0x01102100, 0x01102102, 0x01102201, 0x01112000, 0x01112101,
  1602. 0x01112200, 0x01112202, 0x01122000, 0x01122001, 0x01122100, 0x01122102, 0x01122201, 0x02102101,
  1603. 0x02112001, 0x02112100, 0x02122101, 0x00112010, 0x00112012, 0x00112111, 0x00112212, 0x00122011,
  1604. 0x00122111, 0x01102012, 0x01102110, 0x01102111, 0x01102210, 0x01112011, 0x01112110, 0x01112111,
  1605. 0x01112112, 0x01112211, 0x01112212, 0x01122010, 0x01122111, 0x01122212, 0x02102211, 0x02112011,
  1606. 0x02112012, 0x02112111, 0x02112210, 0x02122011, 0x02122112, 0x02122211, 0x00102221, 0x00112122,
  1607. 0x00122120, 0x00122122, 0x01102120, 0x01102122, 0x01102221, 0x01112020, 0x01112022, 0x01112121,
  1608. 0x01112220, 0x01122021, 0x01122122, 0x01122221, 0x02102121, 0x02112021, 0x02112122, 0x02112222,
  1609. 0x00200000, 0x00200002, 0x00200200, 0x00200202, 0x00210101, 0x00220000, 0x00220002, 0x00220101,
  1610. 0x00220200, 0x00220202, 0x01200101, 0x01210001, 0x01210201, 0x01220001, 0x01220101, 0x02200000,
  1611. 0x02200002, 0x02200200, 0x02200202, 0x02210101, 0x02220000, 0x02220002, 0x02220101, 0x02220200,
  1612. 0x02220202, 0x00200111, 0x00210011, 0x00210110, 0x00210211, 0x00220111, 0x01200012, 0x01200110,
  1613. 0x01200211, 0x01210111, 0x01210210, 0x01210212, 0x01220011, 0x01220110, 0x01220111, 0x01220112,
  1614. 0x02200111, 0x02210010, 0x02210112, 0x02210211, 0x02220111, 0x00200021, 0x00200220, 0x00200222,
  1615. 0x00210021, 0x00210121, 0x00220020, 0x00220022, 0x00220220, 0x00220222, 0x01200121, 0x01210021,
  1616. 0x01210122, 0x01210221, 0x01220121, 0x02200021, 0x02200220, 0x02200222, 0x02210021, 0x02210121,
  1617. 0x02220020, 0x02220022, 0x02220220, 0x02220222, 0x00201101, 0x00211100, 0x00211102, 0x00211201,
  1618. 0x00221101, 0x01201100, 0x01201101, 0x01201102, 0x01201201, 0x01211002, 0x01211101, 0x01211200,
  1619. 0x01211202, 0x01221102, 0x02201101, 0x02211001, 0x02211100, 0x02211201, 0x02221001, 0x02221101,
  1620. 0x00201211, 0x00211111, 0x00221011, 0x00221211, 0x01201010, 0x01201111, 0x01201210, 0x01211011,
  1621. 0x01211110, 0x01211111, 0x01211211, 0x01221012, 0x01221111, 0x01221210, 0x02201211, 0x02211010,
  1622. 0x02211110, 0x02211111, 0x02211210, 0x02211212, 0x02221011, 0x02221110, 0x02221112, 0x02221211,
  1623. 0x00201121, 0x00211020, 0x00211022, 0x00211221, 0x00221121, 0x01201021, 0x01201221, 0x01211121,
  1624. 0x01221020, 0x01221021, 0x01221221, 0x02201120, 0x02201122, 0x02211020, 0x02211222, 0x00202000,
  1625. 0x00202002, 0x00202200, 0x00202202, 0x00212101, 0x00222000, 0x00222002, 0x00222200, 0x00222202,
  1626. 0x01202101, 0x01212001, 0x01212100, 0x01222101, 0x02202000, 0x02202002, 0x02202200, 0x02202202,
  1627. 0x02222000, 0x02222002, 0x02222200, 0x02222202, 0x00202211, 0x00212011, 0x00212110, 0x00212211,
  1628. 0x00222111, 0x01202112, 0x01202211, 0x01212012, 0x01212111, 0x01222011, 0x01222110, 0x01222112,
  1629. 0x01222211, 0x02202111, 0x02212010, 0x02212112, 0x02212211, 0x02222110, 0x02222111, 0x00202020,
  1630. 0x00202022, 0x00202220, 0x00202222, 0x00222020, 0x00222022, 0x00222220, 0x00222222, 0x01202121,
  1631. 0x01212021, 0x01212122, 0x01212221, 0x01222121, 0x02202020, 0x02202022, 0x02202220, 0x02202222,
  1632. 0x02212121, 0x02222020, 0x02222022, 0x02222220, 0x02222222, 0x10000101, 0x10010001, 0x10010102,
  1633. 0x10020101, 0x11000201, 0x11010002, 0x11010101, 0x11010200, 0x11010202, 0x11020001, 0x11020100,
  1634. 0x11020102, 0x12010100, 0x12010201, 0x12020001, 0x12020102, 0x10000010, 0x10000011, 0x10000110,
  1635. 0x10000112, 0x10000211, 0x10010012, 0x10010111, 0x10010112, 0x10010210, 0x10010212, 0x10020011,
  1636. 0x10020112, 0x10020211, 0x11000111, 0x11000210, 0x11000212, 0x11010011, 0x11010110, 0x11010111,
  1637. 0x11010112, 0x11010211, 0x11010212, 0x11020111, 0x11020210, 0x11020212, 0x12000011, 0x12000110,
  1638. 0x12000112, 0x12010010, 0x12010012, 0x12010111, 0x12020010, 0x12020011, 0x12020012, 0x10000121,
  1639. 0x10010021, 0x10010120, 0x10010122, 0x10020121, 0x11000021, 0x11010022, 0x11010121, 0x11010222,
  1640. 0x11020120, 0x11020221, 0x12000221, 0x12010120, 0x12020121, 0x10001001, 0x10011101, 0x10011201,
  1641. 0x10021201, 0x11001101, 0x11001200, 0x11001202, 0x11011001, 0x11011100, 0x11011101, 0x11011102,
  1642. 0x11021001, 0x11021002, 0x11021101, 0x11021200, 0x11021202, 0x12001001, 0x12001102, 0x12001201,
  1643. 0x12011000, 0x12011002, 0x12011101, 0x12021000, 0x12021001, 0x12021201, 0x10001011, 0x10001012,
  1644. 0x10001111, 0x10001212, 0x10011011, 0x10011110, 0x10011111, 0x10011112, 0x10011211, 0x10021010,
  1645. 0x10021111, 0x10021212, 0x11001011, 0x11001110, 0x11001111, 0x11001112, 0x11001211, 0x11011010,
  1646. 0x11011011, 0x11011110, 0x11011111, 0x11011112, 0x11011210, 0x11011211, 0x11021011, 0x11021110,
  1647. 0x11021111, 0x11021112, 0x11021211, 0x12001012, 0x12001110, 0x12001111, 0x12001210, 0x12011011,
  1648. 0x12011110, 0x12011111, 0x12011112, 0x12011211, 0x12011212, 0x12021111, 0x12021210, 0x12021212,
  1649. 0x10001021, 0x10001121, 0x10001221, 0x10011120, 0x10011121, 0x10011220, 0x10011222, 0x10021021,
  1650. 0x10021120, 0x10021221, 0x11001020, 0x11001022, 0x11001121, 0x11001220, 0x11011020, 0x11011021,
  1651. 0x11011022, 0x11011121, 0x11011122, 0x11011221, 0x11021022, 0x11021121, 0x11021220, 0x12001021,
  1652. 0x12001121, 0x12001222, 0x12011120, 0x12011121, 0x12021021, 0x12021120, 0x12021122, 0x10002101,
  1653. 0x10012001, 0x10012101, 0x10012202, 0x10022101, 0x11002002, 0x11002201, 0x11012000, 0x11012101,
  1654. 0x11012200, 0x11022001, 0x11022100, 0x11022102, 0x11022201, 0x12002101, 0x12012001, 0x12012100,
  1655. 0x12012102, 0x12012201, 0x12022101, 0x10002011, 0x10002111, 0x10002112, 0x10002212, 0x10012010,
  1656. 0x10012110, 0x10012111, 0x10012210, 0x10022011, 0x10022110, 0x10022112, 0x11002010, 0x11002111,
  1657. 0x11002212, 0x11012011, 0x11012012, 0x11012110, 0x11012111, 0x11012112, 0x11012211, 0x11022010,
  1658. 0x11022012, 0x11022111, 0x11022112, 0x11022212, 0x12002112, 0x12002211, 0x12012012, 0x12012111,
  1659. 0x12012112, 0x12012210, 0x12022011, 0x12022110, 0x12022112, 0x12022211, 0x10012122, 0x11002120,
  1660. 0x11002122, 0x11002221, 0x11012121, 0x11012220, 0x11012222, 0x11022120, 0x11022221, 0x12012120,
  1661. 0x12022121, 0x10100001, 0x10100100, 0x10100101, 0x10100102, 0x10100201, 0x10110002, 0x10110101,
  1662. 0x10110202, 0x10120001, 0x10120100, 0x10120201, 0x11100000, 0x11100101, 0x11100200, 0x11110001,
  1663. 0x11110100, 0x11110101, 0x11110102, 0x11110201, 0x11120101, 0x11120200, 0x12100102, 0x12100201,
  1664. 0x12110101, 0x12110200, 0x12120000, 0x12120001, 0x12120102, 0x12120201, 0x10100111, 0x10100210,
  1665. 0x10100211, 0x10100212, 0x10110011, 0x10110110, 0x10110111, 0x10110112, 0x10110210, 0x10110211,
  1666. 0x10120010, 0x10120111, 0x10120112, 0x10120210, 0x10120212, 0x11100011, 0x11100110, 0x11100111,
  1667. 0x11100112, 0x11100211, 0x11110010, 0x11110011, 0x11110012, 0x11110110, 0x11110111, 0x11110112,
  1668. 0x11110210, 0x11110211, 0x11110212, 0x11120011, 0x11120110, 0x11120111, 0x11120112, 0x11120211,
  1669. 0x12100012, 0x12100111, 0x12110011, 0x12110110, 0x12110111, 0x12110112, 0x12110211, 0x12120010,
  1670. 0x12120111, 0x12120212, 0x10100021, 0x10100122, 0x10110022, 0x10110121, 0x10110222, 0x10120021,
  1671. 0x10120120, 0x11100022, 0x11100121, 0x11100222, 0x11110021, 0x11110120, 0x11110121, 0x11110122,
  1672. 0x11110221, 0x11120022, 0x11120121, 0x12100121, 0x12110020, 0x12110022, 0x12110121, 0x12110221,
  1673. 0x12110222, 0x12120120, 0x10101100, 0x10101101, 0x10111001, 0x10111100, 0x10111101, 0x10111102,
  1674. 0x10111200, 0x10111201, 0x10121001, 0x10121101, 0x10121200, 0x10121202, 0x11101001, 0x11101100,
  1675. 0x11101101, 0x11101102, 0x11101201, 0x11101202, 0x11111000, 0x11111001, 0x11111100, 0x11111101,
  1676. 0x11111102, 0x11111200, 0x11111201, 0x11111202, 0x11121001, 0x11121002, 0x11121100, 0x11121101,
  1677. 0x11121102, 0x11121201, 0x12101000, 0x12101200, 0x12101202, 0x12111001, 0x12111100, 0x12111101,
  1678. 0x12111102, 0x12111201, 0x12121001, 0x12121100, 0x12121101, 0x12121202, 0x10101011, 0x10101012,
  1679. 0x10101110, 0x10101111, 0x10101112, 0x10101211, 0x10111010, 0x10111011, 0x10111012, 0x10111110,
  1680. 0x10111111, 0x10111112, 0x10111211, 0x10111212, 0x10121011, 0x10121110, 0x10121111, 0x10121112,
  1681. 0x10121211, 0x11101010, 0x11101011, 0x11101012, 0x11101110, 0x11101111, 0x11101112, 0x11101210,
  1682. 0x11101211, 0x11111010, 0x11111011, 0x11111012, 0x11111110, 0x11111111, 0x11111112, 0x11111210,
  1683. 0x11111211, 0x11111212, 0x11121010, 0x11121011, 0x11121110, 0x11121111, 0x11121112, 0x11121210,
  1684. 0x11121211, 0x11121212, 0x12101011, 0x12101110, 0x12101111, 0x12101211, 0x12101212, 0x12111010,
  1685. 0x12111011, 0x12111110, 0x12111111, 0x12111112, 0x12111210, 0x12111211, 0x12121011, 0x12121110,
  1686. 0x12121111, 0x12121112, 0x12121211, 0x10101020, 0x10101021, 0x10101022, 0x10101120, 0x10101122,
  1687. 0x10101220, 0x10101221, 0x10111021, 0x10111120, 0x10111121, 0x10111220, 0x10111221, 0x10121020,
  1688. 0x10121021, 0x10121022, 0x10121120, 0x10121121, 0x10121122, 0x10121220, 0x10121221, 0x11101021,
  1689. 0x11101121, 0x11101122, 0x11101220, 0x11101221, 0x11101222, 0x11111020, 0x11111021, 0x11111022,
  1690. 0x11111120, 0x11111121, 0x11111122, 0x11111220, 0x11111221, 0x11111222, 0x11121021, 0x11121120,
  1691. 0x11121121, 0x11121221, 0x12101022, 0x12101121, 0x12101122, 0x12101220, 0x12101221, 0x12101222,
  1692. 0x12111021, 0x12111121, 0x12111222, 0x12121022, 0x12121121, 0x12121122, 0x12121220, 0x12121221,
  1693. 0x10102100, 0x10102101, 0x10102102, 0x10102201, 0x10112000, 0x10112101, 0x10112200, 0x10122001,
  1694. 0x10122202, 0x11102101, 0x11102200, 0x11102202, 0x11112001, 0x11112100, 0x11112101, 0x11112102,
  1695. 0x11112200, 0x11112201, 0x11122000, 0x11122002, 0x11122100, 0x11122101, 0x12102002, 0x12102201,
  1696. 0x12112000, 0x12112002, 0x12112101, 0x12112200, 0x12122001, 0x12122201, 0x10102011, 0x10102012,
  1697. 0x10102111, 0x10102212, 0x10112011, 0x10112110, 0x10112111, 0x10112112, 0x10112211, 0x10122111,
  1698. 0x11102011, 0x11102110, 0x11102111, 0x11102112, 0x11102211, 0x11112010, 0x11112011, 0x11112012,
  1699. 0x11112110, 0x11112111, 0x11112112, 0x11112210, 0x11112211, 0x11112212, 0x11122011, 0x11122110,
  1700. 0x11122111, 0x11122112, 0x11122211, 0x12102011, 0x12102111, 0x12102211, 0x12112011, 0x12112110,
  1701. 0x12112111, 0x12112112, 0x12112210, 0x12112211, 0x12122111, 0x10102120, 0x10102220, 0x10112121,
  1702. 0x10112222, 0x10122020, 0x10122121, 0x10122122, 0x10122221, 0x11102121, 0x11102220, 0x11102221,
  1703. 0x11112021, 0x11112121, 0x11112122, 0x11112220, 0x11112221, 0x11122022, 0x11122121, 0x11122220,
  1704. 0x11122222, 0x12102021, 0x12102222, 0x12112022, 0x12112121, 0x12112122, 0x12112220, 0x12112222,
  1705. 0x12122021, 0x10200101, 0x10210100, 0x10210102, 0x10210201, 0x10220101, 0x11200100, 0x11210000,
  1706. 0x11210101, 0x11210102, 0x11210200, 0x11210202, 0x11220001, 0x11220100, 0x11220102, 0x11220201,
  1707. 0x12200001, 0x12210102, 0x12220101, 0x10200011, 0x10200110, 0x10200112, 0x10200211, 0x10210012,
  1708. 0x10210111, 0x10220011, 0x10220012, 0x10220112, 0x10220211, 0x11200111, 0x11200211, 0x11210011,
  1709. 0x11210111, 0x11210112, 0x11210211, 0x11220111, 0x11220112, 0x11220212, 0x12200110, 0x12200212,
  1710. 0x12210012, 0x12210111, 0x12220011, 0x12220112, 0x12220211, 0x10210021, 0x10210122, 0x10210221,
  1711. 0x11200020, 0x11200021, 0x11200122, 0x11210121, 0x11210122, 0x11210220, 0x11220020, 0x12200121,
  1712. 0x12210021, 0x12210122, 0x12220121, 0x10211001, 0x10211002, 0x10211101, 0x10211102, 0x10211202,
  1713. 0x10221001, 0x10221102, 0x10221201, 0x11201000, 0x11201002, 0x11201101, 0x11201200, 0x11201202,
  1714. 0x11211001, 0x11211100, 0x11211101, 0x11211102, 0x11211201, 0x11211202, 0x11221000, 0x11221002,
  1715. 0x11221101, 0x12201100, 0x12201101, 0x12201201, 0x12211000, 0x12211002, 0x12211100, 0x12211101,
  1716. 0x12211102, 0x12211200, 0x12211202, 0x12221001, 0x12221100, 0x12221201, 0x10201111, 0x10201210,
  1717. 0x10201212, 0x10211011, 0x10211111, 0x10211112, 0x10211211, 0x11201110, 0x11201111, 0x11201112,
  1718. 0x11201211, 0x11211010, 0x11211011, 0x11211110, 0x11211111, 0x11211112, 0x11211211, 0x11221011,
  1719. 0x11221110, 0x11221111, 0x11221112, 0x11221211, 0x12201112, 0x12201211, 0x12201212, 0x12211011,
  1720. 0x12211111, 0x12211112, 0x12211211, 0x12211212, 0x12221012, 0x12221111, 0x12221112, 0x12221210,
  1721. 0x10201022, 0x10201221, 0x10211121, 0x10221020, 0x10221122, 0x10221220, 0x10221221, 0x11201020,
  1722. 0x11201121, 0x11201220, 0x11201222, 0x11211021, 0x11211120, 0x11211121, 0x11211122, 0x11211220,
  1723. 0x11211222, 0x11221020, 0x11221121, 0x11221220, 0x12201020, 0x12201022, 0x12201121, 0x12201222,
  1724. 0x12211120, 0x12211122, 0x12211220, 0x12211221, 0x12221020, 0x12221120, 0x12221122, 0x12221222,
  1725. 0x10212102, 0x10212201, 0x10222101, 0x11202001, 0x11212002, 0x11212101, 0x11212202, 0x11222001,
  1726. 0x11222201, 0x12202101, 0x12212001, 0x12212200, 0x12222102, 0x10202011, 0x10202110, 0x10212010,
  1727. 0x10212111, 0x10222011, 0x10222110, 0x10222112, 0x10222211, 0x11202010, 0x11202011, 0x11202111,
  1728. 0x11202112, 0x11202210, 0x11212011, 0x11212110, 0x11212111, 0x11212112, 0x11212211, 0x11222010,
  1729. 0x11222111, 0x11222212, 0x12202012, 0x12202110, 0x12202212, 0x12212111, 0x12222011, 0x12222110,
  1730. 0x12222111, 0x12222211, 0x10212021, 0x10212122, 0x10212220, 0x11202021, 0x11202120, 0x11202221,
  1731. 0x11212020, 0x11212121, 0x11212220, 0x11212222, 0x11222120, 0x11222121, 0x11222221, 0x12202122,
  1732. 0x12212120, 0x12212220, 0x12212222, 0x12222122, 0x20000000, 0x20000002, 0x20000200, 0x20000202,
  1733. 0x20020000, 0x20020002, 0x20020200, 0x20020202, 0x21000101, 0x21010000, 0x21010001, 0x21010100,
  1734. 0x21010102, 0x21010201, 0x21020101, 0x22000000, 0x22000002, 0x22000200, 0x22000202, 0x22010101,
  1735. 0x22020000, 0x22020002, 0x22020200, 0x22020202, 0x20000111, 0x20010011, 0x20010110, 0x20010112,
  1736. 0x20010211, 0x20020111, 0x21000011, 0x21000110, 0x21000211, 0x21010010, 0x21010012, 0x21010111,
  1737. 0x21010112, 0x21010210, 0x21010211, 0x21020110, 0x21020112, 0x21020211, 0x22000111, 0x22000211,
  1738. 0x22010110, 0x22010112, 0x22010211, 0x22020111, 0x20000020, 0x20000022, 0x20000220, 0x20000222,
  1739. 0x20010121, 0x20020020, 0x20020022, 0x20020220, 0x20020222, 0x21010021, 0x21010120, 0x21010221,
  1740. 0x21020121, 0x22000020, 0x22000022, 0x22000220, 0x22000222, 0x22010121, 0x22020020, 0x22020022,
  1741. 0x22020220, 0x22020222, 0x20011100, 0x20011201, 0x21001001, 0x21001100, 0x21011001, 0x21011101,
  1742. 0x21011202, 0x21021001, 0x21021100, 0x21021201, 0x22011100, 0x22011201, 0x20001011, 0x20001211,
  1743. 0x20011012, 0x20011111, 0x20011212, 0x20021112, 0x20021211, 0x21001010, 0x21001011, 0x21001111,
  1744. 0x21001210, 0x21011011, 0x21011110, 0x21011111, 0x21011112, 0x21011211, 0x21011212, 0x21021111,
  1745. 0x21021112, 0x21021210, 0x21021212, 0x22001011, 0x22001110, 0x22001112, 0x22001211, 0x22011010,
  1746. 0x22011012, 0x22011111, 0x22011210, 0x22021112, 0x20011021, 0x20011122, 0x20011221, 0x20021121,
  1747. 0x21001021, 0x21001120, 0x21001221, 0x21001222, 0x21011020, 0x21011121, 0x21011221, 0x21011222,
  1748. 0x21021021, 0x21021122, 0x21021222, 0x22001121, 0x22011021, 0x22011222, 0x22021120, 0x20002000,
  1749. 0x20002002, 0x20002200, 0x20002202, 0x20012101, 0x20022000, 0x20022002, 0x20022200, 0x20022202,
  1750. 0x21002001, 0x21002101, 0x21012001, 0x21012100, 0x21012201, 0x21022101, 0x21022201, 0x22002000,
  1751. 0x22002002, 0x22002200, 0x22002202, 0x22012101, 0x22022000, 0x22022002, 0x22022200, 0x22022202,
  1752. 0x20002111, 0x20002112, 0x20012011, 0x20012110, 0x20012112, 0x20022111, 0x21002011, 0x21002110,
  1753. 0x21002112, 0x21002211, 0x21012010, 0x21012012, 0x21012111, 0x21012212, 0x21022011, 0x21022110,
  1754. 0x22002111, 0x22012112, 0x22012211, 0x22022111, 0x20002020, 0x20002022, 0x20002220, 0x20002222,
  1755. 0x20012121, 0x20022020, 0x20022022, 0x20022220, 0x20022222, 0x21002121, 0x21012021, 0x21012120,
  1756. 0x21012122, 0x22002020, 0x22002022, 0x22002220, 0x22002222, 0x22012121, 0x22022020, 0x22022022,
  1757. 0x22022220, 0x22022222, 0x20100101, 0x20110001, 0x20110102, 0x20110200, 0x20110201, 0x20120101,
  1758. 0x21100001, 0x21100102, 0x21100201, 0x21110101, 0x21110200, 0x21110202, 0x21120201, 0x21120202,
  1759. 0x22100101, 0x22110001, 0x22110100, 0x22110102, 0x22110201, 0x22120101, 0x20100011, 0x20100110,
  1760. 0x20100112, 0x20100211, 0x20110010, 0x20110111, 0x20110210, 0x20110212, 0x20120011, 0x20120110,
  1761. 0x20120112, 0x20120211, 0x21100010, 0x21100111, 0x21110010, 0x21110011, 0x21110110, 0x21110111,
  1762. 0x21110112, 0x21110211, 0x21120012, 0x21120111, 0x22100110, 0x22100112, 0x22110012, 0x22110111,
  1763. 0x22110210, 0x22120011, 0x22120110, 0x22120112, 0x22120211, 0x20100121, 0x20110021, 0x20110120,
  1764. 0x20110221, 0x20120121, 0x21100120, 0x21100122, 0x21100221, 0x21110020, 0x21110022, 0x21110121,
  1765. 0x21110220, 0x21120122, 0x21120221, 0x22100121, 0x22110120, 0x22110122, 0x22120221, 0x20101001,
  1766. 0x20101100, 0x20101102, 0x20111000, 0x20111101, 0x20111200, 0x20121102, 0x21101000, 0x21101202,
  1767. 0x21111001, 0x21111100, 0x21111101, 0x21111102, 0x21111200, 0x21111201, 0x21121000, 0x21121001,
  1768. 0x21121002, 0x21121101, 0x22101100, 0x22101102, 0x22111002, 0x22111100, 0x22111101, 0x22111200,
  1769. 0x22121001, 0x22121201, 0x20101010, 0x20101111, 0x20101210, 0x20101212, 0x20111010, 0x20111011,
  1770. 0x20111110, 0x20111111, 0x20111112, 0x20111211, 0x20121011, 0x20121111, 0x20121211, 0x20121212,
  1771. 0x21101011, 0x21101110, 0x21101111, 0x21101112, 0x21101211, 0x21111010, 0x21111011, 0x21111012,
  1772. 0x21111110, 0x21111111, 0x21111112, 0x21111210, 0x21111211, 0x21111212, 0x21121011, 0x21121110,
  1773. 0x21121111, 0x21121112, 0x21121211, 0x22101011, 0x22101111, 0x22101210, 0x22111011, 0x22111012,
  1774. 0x22111110, 0x22111111, 0x22111112, 0x22111211, 0x22111212, 0x22121010, 0x22121012, 0x22121111,
  1775. 0x22121210, 0x22121212, 0x20101021, 0x20101120, 0x20111020, 0x20111121, 0x20111221, 0x20121020,
  1776. 0x20121122, 0x20121221, 0x21101121, 0x21101220, 0x21101221, 0x21111021, 0x21111022, 0x21111121,
  1777. 0x21111122, 0x21111221, 0x21121121, 0x21121220, 0x22101022, 0x22101120, 0x22101221, 0x22101222,
  1778. 0x22111022, 0x22111120, 0x22111121, 0x22121120, 0x22121122, 0x22121221, 0x20102101, 0x20112102,
  1779. 0x20112201, 0x20122101, 0x21102001, 0x21102102, 0x21112000, 0x21112002, 0x21112101, 0x21112102,
  1780. 0x21112202, 0x21122100, 0x21122101, 0x22102101, 0x22112001, 0x22112102, 0x22112201, 0x22122101,
  1781. 0x20102110, 0x20102112, 0x20102211, 0x20112010, 0x20112012, 0x20112111, 0x20112210, 0x20112212,
  1782. 0x20122010, 0x20122011, 0x20122110, 0x20122112, 0x21102010, 0x21102012, 0x21102111, 0x21102210,
  1783. 0x21102212, 0x21112011, 0x21112110, 0x21112111, 0x21112112, 0x21112211, 0x21122012, 0x21122111,
  1784. 0x21122112, 0x21122212, 0x22102011, 0x22102110, 0x22112010, 0x22112012, 0x22112111, 0x22112212,
  1785. 0x22122011, 0x22122112, 0x20102121, 0x20112121, 0x20122121, 0x21102120, 0x21102122, 0x21102221,
  1786. 0x21112020, 0x21112121, 0x21112220, 0x21122021, 0x22102121, 0x22112021, 0x22112120, 0x22112121,
  1787. 0x22112122, 0x20200000, 0x20200002, 0x20200200, 0x20200202, 0x20210101, 0x20220000, 0x20220002,
  1788. 0x20220200, 0x20220202, 0x21200101, 0x21210001, 0x21210100, 0x21210102, 0x21210201, 0x22200000,
  1789. 0x22200002, 0x22200200, 0x22200202, 0x22210101, 0x22220000, 0x22220002, 0x22220200, 0x22220202,
  1790. 0x20200111, 0x20200211, 0x20210011, 0x20210110, 0x20210112, 0x20210211, 0x20210212, 0x21200112,
  1791. 0x21200211, 0x21210011, 0x21210111, 0x21210210, 0x21210212, 0x21220011, 0x21220110, 0x22200111,
  1792. 0x22210010, 0x22210012, 0x22210112, 0x22210211, 0x20200022, 0x20200220, 0x20200222, 0x20210020,
  1793. 0x20210221, 0x20220022, 0x20220220, 0x20220222, 0x21200121, 0x21210021, 0x21210122, 0x21210221,
  1794. 0x21220121, 0x22200020, 0x22200022, 0x22200220, 0x22200222, 0x22210121, 0x22220020, 0x22220022,
  1795. 0x22220220, 0x22220222, 0x20211201, 0x20221101, 0x21201001, 0x21201100, 0x21211000, 0x21211100,
  1796. 0x21211101, 0x21211200, 0x21211202, 0x21221001, 0x21221101, 0x21221102, 0x21221200, 0x21221201,
  1797. 0x22201101, 0x20201112, 0x20201211, 0x20211010, 0x20211012, 0x20211111, 0x20211210, 0x20221112,
  1798. 0x20221211, 0x21201012, 0x21201111, 0x21211011, 0x21211110, 0x21211111, 0x21211112, 0x21211211,
  1799. 0x21221111, 0x21221212, 0x22201011, 0x22201110, 0x22201111, 0x22201112, 0x22201211, 0x22211012,
  1800. 0x22211111, 0x22211210, 0x20201121, 0x20211021, 0x20211122, 0x20211222, 0x20221021, 0x20221121,
  1801. 0x21201120, 0x21201122, 0x21201222, 0x21211022, 0x21211121, 0x21211122, 0x21211220, 0x21221020,
  1802. 0x21221022, 0x22201122, 0x22211020, 0x22211121, 0x22211122, 0x22211221, 0x22221021, 0x22221120,
  1803. 0x22221122, 0x20202000, 0x20202002, 0x20202200, 0x20202202, 0x20222000, 0x20222002, 0x20222200,
  1804. 0x20222202, 0x21212001, 0x21212100, 0x21212102, 0x21212201, 0x22202000, 0x22202002, 0x22202200,
  1805. 0x22202202, 0x22212101, 0x22222000, 0x22222002, 0x22222200, 0x22222202, 0x20202111, 0x20212110,
  1806. 0x20212211, 0x20222011, 0x20222111, 0x21202011, 0x21212010, 0x21212111, 0x21212212, 0x21222011,
  1807. 0x21222112, 0x21222211, 0x22212010, 0x22212112, 0x20202020, 0x20202022, 0x20202220, 0x20202222,
  1808. 0x20222020, 0x20222022, 0x20222220, 0x20222222, 0x21212021, 0x21212120, 0x21212122, 0x22202020,
  1809. 0x22202022, 0x22202220, 0x22202222, 0x22212121, 0x22222020, 0x22222022, 0x22222220, 0x22222222,
  1810. GGML_TABLE_END()
  1811. #endif
  1812. #endif // GGML_COMMON_IMPL
  1813. #endif // GGML_COMMON_IMPL
  1814. #else
  1815. // TODO: this should not be a relative path, but can't figure out how to set Metal include paths in Package.swift
  1816. #include "../ggml-common.h"
  1817. #endif
  1818. /**
  1819. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - do not edit this file
  1820. *
  1821. * MIT License
  1822. *
  1823. * Copyright (c) 2023-2024 The ggml authors
  1824. *
  1825. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1826. * of this software and associated documentation files (the "Software"), to deal
  1827. * in the Software without restriction, including without limitation the rights
  1828. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1829. * copies of the Software, and to permit persons to whom the Software is
  1830. * furnished to do so, subject to the following conditions:
  1831. *
  1832. * The above copyright notice and this permission notice shall be included in all
  1833. * copies or substantial portions of the Software.
  1834. *
  1835. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1836. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1837. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1838. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1839. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1840. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1841. * SOFTWARE.
  1842. */
  1843. #ifndef GGML_METAL_IMPL
  1844. #define GGML_METAL_IMPL
  1845. // kernel argument structs
  1846. //
  1847. // - element counters (e.g. ne00) typically use int32_t to reduce register usage
  1848. // however, be careful from int overflows when using those in the kernel implementation
  1849. //
  1850. // - strides (e.g. nb00) use uint64_t
  1851. typedef struct {
  1852. int32_t ne00;
  1853. int32_t ne01;
  1854. int32_t ne02;
  1855. int32_t ne03;
  1856. uint64_t nb00;
  1857. uint64_t nb01;
  1858. uint64_t nb02;
  1859. uint64_t nb03;
  1860. int32_t ne10;
  1861. int32_t ne11;
  1862. int32_t ne12;
  1863. int32_t ne13;
  1864. uint64_t nb10;
  1865. uint64_t nb11;
  1866. uint64_t nb12;
  1867. uint64_t nb13;
  1868. int32_t ne0;
  1869. int32_t ne1;
  1870. int32_t ne2;
  1871. int32_t ne3;
  1872. uint64_t nb0;
  1873. uint64_t nb1;
  1874. uint64_t nb2;
  1875. uint64_t nb3;
  1876. int32_t dim;
  1877. } ggml_metal_kargs_concat;
  1878. typedef struct {
  1879. int32_t ne00;
  1880. int32_t ne01;
  1881. int32_t ne02;
  1882. int32_t ne03;
  1883. uint64_t nb00;
  1884. uint64_t nb01;
  1885. uint64_t nb02;
  1886. uint64_t nb03;
  1887. int32_t ne10;
  1888. int32_t ne11;
  1889. int32_t ne12;
  1890. int32_t ne13;
  1891. uint64_t nb10;
  1892. uint64_t nb11;
  1893. uint64_t nb12;
  1894. uint64_t nb13;
  1895. int32_t ne0;
  1896. int32_t ne1;
  1897. int32_t ne2;
  1898. int32_t ne3;
  1899. uint64_t nb0;
  1900. uint64_t nb1;
  1901. uint64_t nb2;
  1902. uint64_t nb3;
  1903. uint64_t offs;
  1904. } ggml_metal_kargs_bin;
  1905. typedef struct {
  1906. int32_t ne00;
  1907. int32_t ne01;
  1908. int32_t ne02;
  1909. int32_t ne03;
  1910. uint64_t nb00;
  1911. uint64_t nb01;
  1912. uint64_t nb02;
  1913. uint64_t nb03;
  1914. int32_t ne0;
  1915. int32_t ne1;
  1916. int32_t ne2;
  1917. int32_t ne3;
  1918. uint64_t nb0;
  1919. uint64_t nb1;
  1920. uint64_t nb2;
  1921. uint64_t nb3;
  1922. } ggml_metal_kargs_repeat;
  1923. typedef struct {
  1924. int64_t ne00;
  1925. int64_t ne01;
  1926. int64_t ne02;
  1927. int64_t ne03;
  1928. uint64_t nb00;
  1929. uint64_t nb01;
  1930. uint64_t nb02;
  1931. uint64_t nb03;
  1932. int64_t ne0;
  1933. int64_t ne1;
  1934. int64_t ne2;
  1935. int64_t ne3;
  1936. uint64_t nb0;
  1937. uint64_t nb1;
  1938. uint64_t nb2;
  1939. uint64_t nb3;
  1940. } ggml_metal_kargs_cpy;
  1941. typedef struct {
  1942. int32_t ne00;
  1943. int32_t ne01;
  1944. int32_t ne02;
  1945. int32_t ne03;
  1946. uint64_t nb00;
  1947. uint64_t nb01;
  1948. uint64_t nb02;
  1949. uint64_t nb03;
  1950. int32_t ne0;
  1951. int32_t ne1;
  1952. int32_t ne2;
  1953. int32_t ne3;
  1954. uint64_t nb0;
  1955. uint64_t nb1;
  1956. uint64_t nb2;
  1957. uint64_t nb3;
  1958. int32_t n_past;
  1959. int32_t n_dims;
  1960. int32_t n_ctx_orig;
  1961. float freq_base;
  1962. float freq_scale;
  1963. float ext_factor;
  1964. float attn_factor;
  1965. float beta_fast;
  1966. float beta_slow;
  1967. } ggml_metal_kargs_rope;
  1968. typedef struct {
  1969. int32_t ne01;
  1970. int32_t ne02;
  1971. int32_t ne03;
  1972. uint64_t nb01;
  1973. uint64_t nb02;
  1974. uint64_t nb03;
  1975. int32_t ne11;
  1976. int32_t ne_12_2; // assume K and V are same shape
  1977. int32_t ne_12_3;
  1978. uint64_t nb_12_1;
  1979. uint64_t nb_12_2;
  1980. uint64_t nb_12_3;
  1981. uint64_t nb31;
  1982. int32_t ne1;
  1983. int32_t ne2;
  1984. float scale;
  1985. float max_bias;
  1986. float m0;
  1987. float m1;
  1988. uint16_t n_head_log2;
  1989. float logit_softcap;
  1990. } ggml_metal_kargs_flash_attn_ext;
  1991. typedef struct {
  1992. int32_t ne00;
  1993. int32_t ne02;
  1994. uint64_t nb01;
  1995. uint64_t nb02;
  1996. uint64_t nb03;
  1997. int32_t ne12;
  1998. uint64_t nb10;
  1999. uint64_t nb11;
  2000. uint64_t nb12;
  2001. uint64_t nb13;
  2002. int32_t ne0;
  2003. int32_t ne1;
  2004. int16_t r2;
  2005. int16_t r3;
  2006. } ggml_metal_kargs_mul_mm;
  2007. typedef struct {
  2008. int32_t ne00;
  2009. int32_t ne01;
  2010. int32_t ne02;
  2011. uint64_t nb00;
  2012. uint64_t nb01;
  2013. uint64_t nb02;
  2014. uint64_t nb03;
  2015. int32_t ne10;
  2016. int32_t ne11;
  2017. int32_t ne12;
  2018. uint64_t nb10;
  2019. uint64_t nb11;
  2020. uint64_t nb12;
  2021. uint64_t nb13;
  2022. int32_t ne0;
  2023. int32_t ne1;
  2024. int16_t r2;
  2025. int16_t r3;
  2026. } ggml_metal_kargs_mul_mv;
  2027. typedef struct {
  2028. int32_t ne00;
  2029. int32_t ne01;
  2030. int32_t ne02;
  2031. uint64_t nb00;
  2032. uint64_t nb01;
  2033. uint64_t nb02;
  2034. uint64_t nb03;
  2035. int32_t ne10;
  2036. int32_t ne11;
  2037. int32_t ne12;
  2038. uint64_t nb10;
  2039. uint64_t nb11;
  2040. uint64_t nb12;
  2041. uint64_t nb13;
  2042. int32_t ne0;
  2043. int32_t ne1;
  2044. int16_t r2;
  2045. int16_t r3;
  2046. int16_t nsg;
  2047. int16_t nxpsg;
  2048. int16_t r1ptg;
  2049. } ggml_metal_kargs_mul_mv_ext;
  2050. typedef struct {
  2051. int32_t nei0;
  2052. int32_t nei1;
  2053. uint64_t nbi1;
  2054. int32_t ne00;
  2055. int32_t ne02;
  2056. uint64_t nb01;
  2057. uint64_t nb02;
  2058. int32_t ne11;
  2059. int32_t ne12;
  2060. int32_t ne13;
  2061. uint64_t nb10;
  2062. uint64_t nb11;
  2063. uint64_t nb12;
  2064. int32_t ne0;
  2065. int32_t ne1;
  2066. } ggml_metal_kargs_mul_mm_id;
  2067. typedef struct {
  2068. int32_t nei0;
  2069. int32_t nei1;
  2070. uint64_t nbi1;
  2071. int32_t ne00;
  2072. int32_t ne01;
  2073. int32_t ne02;
  2074. uint64_t nb00;
  2075. uint64_t nb01;
  2076. uint64_t nb02;
  2077. int32_t ne10;
  2078. int32_t ne11;
  2079. int32_t ne12;
  2080. int32_t ne13;
  2081. uint64_t nb10;
  2082. uint64_t nb11;
  2083. uint64_t nb12;
  2084. int32_t ne0;
  2085. int32_t ne1;
  2086. uint64_t nb1;
  2087. } ggml_metal_kargs_mul_mv_id;
  2088. typedef struct {
  2089. int32_t ne00;
  2090. int32_t ne00_4;
  2091. uint64_t nb01;
  2092. float eps;
  2093. } ggml_metal_kargs_norm;
  2094. typedef struct {
  2095. int32_t ne00;
  2096. int32_t ne00_4;
  2097. uint64_t nb01;
  2098. float eps;
  2099. } ggml_metal_kargs_rms_norm;
  2100. #endif // GGML_METAL_IMPL
  2101. #include <metal_stdlib>
  2102. using namespace metal;
  2103. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  2104. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  2105. #define SWAP(x, y) { auto tmp = (x); (x) = (y); (y) = tmp; }
  2106. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  2107. // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
  2108. //
  2109. // cmd:
  2110. // .../usr/bin/metal -dM -E -c ggml/src/ggml-metal/ggml-metal.metal
  2111. // .../usr/bin/metal -dM -E -c -target air64-apple-ios14.0 ggml/src/ggml-metal/ggml-metal.metal
  2112. //
  2113. #if __METAL_VERSION__ < 310 && defined(GGML_METAL_USE_BF16)
  2114. #undef GGML_METAL_USE_BF16
  2115. #endif
  2116. #if defined(GGML_METAL_USE_BF16)
  2117. typedef matrix<bfloat, 4, 4> bfloat4x4;
  2118. #endif
  2119. constexpr constant static float kvalues_iq4nl_f[16] = {
  2120. -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
  2121. };
  2122. // NOTE: this is not dequantizing - we are simply fitting the template
  2123. template <typename type4x4>
  2124. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  2125. reg = (type4x4)(*src);
  2126. }
  2127. template <typename type4x4>
  2128. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  2129. reg = (type4x4)(*src);
  2130. }
  2131. template <typename type4>
  2132. void dequantize_f16_t4(device const half4 * src, short il, thread type4 & reg) {
  2133. reg = (type4)(*(src + il));
  2134. }
  2135. #if defined(GGML_METAL_USE_BF16)
  2136. template <typename type4x4>
  2137. void dequantize_bf16(device const bfloat4x4 * src, short il, thread type4x4 & reg) {
  2138. reg = (type4x4)(*src);
  2139. }
  2140. #endif
  2141. template <typename type4x4>
  2142. void dequantize_q4_0(device const block_q4_0 * xb, short il, thread type4x4 & reg) {
  2143. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  2144. const float d1 = il ? (xb->d / 16.h) : xb->d;
  2145. const float d2 = d1 / 256.f;
  2146. const float md = -8.h * xb->d;
  2147. const ushort mask0 = il ? 0x00F0 : 0x000F;
  2148. const ushort mask1 = mask0 << 8;
  2149. float4x4 reg_f;
  2150. for (int i = 0; i < 8; i++) {
  2151. reg_f[i/2][2*(i%2) + 0] = d1 * (qs[i] & mask0) + md;
  2152. reg_f[i/2][2*(i%2) + 1] = d2 * (qs[i] & mask1) + md;
  2153. }
  2154. reg = (type4x4) reg_f;
  2155. }
  2156. template <typename type4>
  2157. void dequantize_q4_0_t4(device const block_q4_0 * xb, short il, thread type4 & reg) {
  2158. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  2159. const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
  2160. const float d2 = d1 / 256.f;
  2161. const float md = -8.h * xb->d;
  2162. const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
  2163. const ushort mask1 = mask0 << 8;
  2164. for (int i = 0; i < 2; i++) {
  2165. reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + md;
  2166. reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + md;
  2167. }
  2168. }
  2169. template <typename type4x4>
  2170. void dequantize_q4_1(device const block_q4_1 * xb, short il, thread type4x4 & reg) {
  2171. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  2172. const float d1 = il ? (xb->d / 16.h) : xb->d;
  2173. const float d2 = d1 / 256.f;
  2174. const float m = xb->m;
  2175. const ushort mask0 = il ? 0x00F0 : 0x000F;
  2176. const ushort mask1 = mask0 << 8;
  2177. float4x4 reg_f;
  2178. for (int i = 0; i < 8; i++) {
  2179. reg_f[i/2][2*(i%2) + 0] = ((qs[i] & mask0) * d1) + m;
  2180. reg_f[i/2][2*(i%2) + 1] = ((qs[i] & mask1) * d2) + m;
  2181. }
  2182. reg = (type4x4) reg_f;
  2183. }
  2184. template <typename type4>
  2185. void dequantize_q4_1_t4(device const block_q4_1 * xb, short il, thread type4 & reg) {
  2186. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  2187. const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
  2188. const float d2 = d1 / 256.f;
  2189. const float m = xb->m;
  2190. const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
  2191. const ushort mask1 = mask0 << 8;
  2192. for (int i = 0; i < 2; i++) {
  2193. reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + m;
  2194. reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + m;
  2195. }
  2196. }
  2197. template <typename type4x4>
  2198. void dequantize_q5_0(device const block_q5_0 * xb, short il, thread type4x4 & reg) {
  2199. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  2200. const float d = xb->d;
  2201. const float md = -16.h * xb->d;
  2202. const ushort mask = il ? 0x00F0 : 0x000F;
  2203. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2204. const int x_mv = il ? 4 : 0;
  2205. const int gh_mv = il ? 12 : 0;
  2206. const int gh_bk = il ? 0 : 4;
  2207. float4x4 reg_f;
  2208. for (int i = 0; i < 8; i++) {
  2209. // extract the 5-th bits for x0 and x1
  2210. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2211. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2212. // combine the 4-bits from qs with the 5th bit
  2213. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2214. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2215. reg_f[i/2][2*(i%2) + 0] = d * x0 + md;
  2216. reg_f[i/2][2*(i%2) + 1] = d * x1 + md;
  2217. }
  2218. reg = (type4x4) reg_f;
  2219. }
  2220. template <typename type4>
  2221. void dequantize_q5_0_t4(device const block_q5_0 * xb, short il, thread type4 & reg) {
  2222. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  2223. const float d = xb->d;
  2224. const float md = -16.h * xb->d;
  2225. const ushort mask = (il/4) ? 0x00F0 : 0x000F;
  2226. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2227. const int x_mv = (il/4) ? 4 : 0;
  2228. const int gh_mv = (il/4) ? 12 : 0;
  2229. const int gh_bk = (il/4) ? 0 : 4;
  2230. for (int ii = 0; ii < 2; ii++) {
  2231. int i = 2*(il%4) + ii;
  2232. // extract the 5-th bits for x0 and x1
  2233. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2234. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2235. // combine the 4-bits from qs with the 5th bit
  2236. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2237. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2238. reg[2*ii + 0] = d * x0 + md;
  2239. reg[2*ii + 1] = d * x1 + md;
  2240. }
  2241. }
  2242. template <typename type4x4>
  2243. void dequantize_q5_1(device const block_q5_1 * xb, short il, thread type4x4 & reg) {
  2244. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  2245. const float d = xb->d;
  2246. const float m = xb->m;
  2247. const ushort mask = il ? 0x00F0 : 0x000F;
  2248. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2249. const int x_mv = il ? 4 : 0;
  2250. const int gh_mv = il ? 12 : 0;
  2251. const int gh_bk = il ? 0 : 4;
  2252. float4x4 reg_f;
  2253. for (int i = 0; i < 8; i++) {
  2254. // extract the 5-th bits for x0 and x1
  2255. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2256. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2257. // combine the 4-bits from qs with the 5th bit
  2258. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2259. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2260. reg_f[i/2][2*(i%2) + 0] = d * x0 + m;
  2261. reg_f[i/2][2*(i%2) + 1] = d * x1 + m;
  2262. }
  2263. reg = (type4x4) reg_f;
  2264. }
  2265. template <typename type4>
  2266. void dequantize_q5_1_t4(device const block_q5_1 * xb, short il, thread type4 & reg) {
  2267. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  2268. const float d = xb->d;
  2269. const float m = xb->m;
  2270. const ushort mask = (il/4) ? 0x00F0 : 0x000F;
  2271. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2272. const int x_mv = (il/4) ? 4 : 0;
  2273. const int gh_mv = (il/4) ? 12 : 0;
  2274. const int gh_bk = (il/4) ? 0 : 4;
  2275. for (int ii = 0; ii < 2; ii++) {
  2276. int i = 2*(il%4) + ii;
  2277. // extract the 5-th bits for x0 and x1
  2278. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2279. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2280. // combine the 4-bits from qs with the 5th bit
  2281. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2282. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2283. reg[2*ii + 0] = d * x0 + m;
  2284. reg[2*ii + 1] = d * x1 + m;
  2285. }
  2286. }
  2287. template <typename type4x4>
  2288. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  2289. device const int8_t * qs = ((device const int8_t *)xb->qs);
  2290. const float d = xb->d;
  2291. float4x4 reg_f;
  2292. for (int i = 0; i < 16; i++) {
  2293. reg_f[i/4][i%4] = (qs[i + 16*il] * d);
  2294. }
  2295. reg = (type4x4) reg_f;
  2296. }
  2297. template <typename type4>
  2298. void dequantize_q8_0_t4(device const block_q8_0 *xb, short il, thread type4 & reg) {
  2299. device const int8_t * qs = ((device const int8_t *)xb->qs);
  2300. const float d = xb->d;
  2301. for (int i = 0; i < 4; i++) {
  2302. reg[i] = (qs[4*(il%4) + i + 16*(il/4)] * d);
  2303. }
  2304. }
  2305. template <typename type4x4>
  2306. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  2307. const float d = xb->d;
  2308. const float min = xb->dmin;
  2309. device const uint8_t * q = (device const uint8_t *)xb->qs;
  2310. float dl, ml;
  2311. uint8_t sc = xb->scales[il];
  2312. q = q + 32*(il/8) + 16*(il&1);
  2313. il = (il/2)%4;
  2314. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2315. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2316. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  2317. for (int i = 0; i < 16; ++i) {
  2318. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  2319. }
  2320. }
  2321. template <typename type4x4>
  2322. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  2323. const half d_all = xb->d;
  2324. device const uint8_t * q = (device const uint8_t *)xb->qs;
  2325. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  2326. device const int8_t * scales = (device const int8_t *)xb->scales;
  2327. q = q + 32 * (il/8) + 16 * (il&1);
  2328. h = h + 16 * (il&1);
  2329. uint8_t m = 1 << (il/2);
  2330. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  2331. ((il/4)>0 ? 12 : 3);
  2332. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  2333. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  2334. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  2335. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  2336. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  2337. const float ml = 4.f * dl;
  2338. il = (il/2) & 3;
  2339. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2340. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2341. dl *= coef;
  2342. for (int i = 0; i < 16; ++i) {
  2343. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  2344. }
  2345. }
  2346. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  2347. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  2348. : 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))};
  2349. }
  2350. template <typename type4x4>
  2351. void dequantize_q4_K(device const block_q4_K * xb, short il, thread type4x4 & reg) {
  2352. device const uchar * q = xb->qs;
  2353. short is = (il/4) * 2;
  2354. q = q + (il/4) * 32 + 16 * (il&1);
  2355. il = il & 3;
  2356. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2357. const float d = il < 2 ? xb->d : xb->d / 16.h;
  2358. const float min = xb->dmin;
  2359. const float dl = d * sc[0];
  2360. const float ml = min * sc[1];
  2361. const ushort mask = il < 2 ? 0x0F : 0xF0;
  2362. for (int i = 0; i < 16; ++i) {
  2363. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  2364. }
  2365. }
  2366. template <typename type4x4>
  2367. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  2368. device const uint8_t * q = xb->qs;
  2369. device const uint8_t * qh = xb->qh;
  2370. short is = (il/4) * 2;
  2371. q = q + 32 * (il/4) + 16 * (il&1);
  2372. qh = qh + 16 * (il&1);
  2373. uint8_t ul = 1 << (il/2);
  2374. il = il & 3;
  2375. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2376. const float d = il < 2 ? xb->d : xb->d / 16.f;
  2377. const float min = xb->dmin;
  2378. const float dl = d * sc[0];
  2379. const float ml = min * sc[1];
  2380. const ushort mask = il<2 ? 0x0F : 0xF0;
  2381. const float qh_val = il<2 ? 16.f : 256.f;
  2382. for (int i = 0; i < 16; ++i) {
  2383. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  2384. }
  2385. }
  2386. template <typename type4x4>
  2387. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  2388. const half d_all = xb->d;
  2389. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  2390. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  2391. device const int8_t * scales = (device const int8_t *)xb->scales;
  2392. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  2393. qh = qh + 32*(il/8) + 16*(il&1);
  2394. float sc = scales[(il%2) + 2 * ((il/2))];
  2395. il = (il/2) & 3;
  2396. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2397. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  2398. const float coef = il>1 ? 1.f/16.f : 1.f;
  2399. const float ml = d_all * sc * 32.f;
  2400. const float dl = d_all * sc * coef;
  2401. for (int i = 0; i < 16; ++i) {
  2402. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  2403. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  2404. reg[i/4][i%4] = dl * q - ml;
  2405. }
  2406. }
  2407. template <typename type4x4>
  2408. void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
  2409. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2410. const float d = xb->d;
  2411. const int ib32 = il/2;
  2412. il = il%2;
  2413. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2414. // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
  2415. device const uint16_t * q2 = xb->qs + 4*ib32;
  2416. const uint32_t aux32_g = q2[0] | (q2[1] << 16);
  2417. const uint32_t aux32_s = q2[2] | (q2[3] << 16);
  2418. thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
  2419. const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
  2420. constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  2421. uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
  2422. for (int i = 0; i < 8; ++i) {
  2423. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2424. }
  2425. grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  2426. signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
  2427. for (int i = 0; i < 8; ++i) {
  2428. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2429. }
  2430. }
  2431. template <typename type4x4>
  2432. void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
  2433. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2434. const float d = xb->d;
  2435. const int ib32 = il/2;
  2436. il = il%2;
  2437. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2438. device const uint16_t * q2 = xb->qs + 4*ib32;
  2439. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  2440. constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
  2441. uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
  2442. for (int i = 0; i < 8; ++i) {
  2443. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2444. }
  2445. grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
  2446. signs = ksigns_iq2xs[q2[2*il+1] >> 9];
  2447. for (int i = 0; i < 8; ++i) {
  2448. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2449. }
  2450. }
  2451. template <typename type4x4>
  2452. void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
  2453. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2454. const float d = xb->d;
  2455. const int ib32 = il/2;
  2456. il = il%2;
  2457. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2458. device const uint8_t * q3 = xb->qs + 8*ib32;
  2459. device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
  2460. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  2461. const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2462. constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
  2463. constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
  2464. uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  2465. for (int i = 0; i < 4; ++i) {
  2466. reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  2467. reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  2468. }
  2469. grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
  2470. grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
  2471. signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
  2472. for (int i = 0; i < 4; ++i) {
  2473. reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  2474. reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  2475. }
  2476. }
  2477. template <typename type4x4>
  2478. void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
  2479. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2480. const float d = xb->d;
  2481. const int ib32 = il/2;
  2482. il = il%2;
  2483. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2484. device const uint8_t * qs = xb->qs + 8*ib32;
  2485. device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
  2486. const uint8_t qh = xb->qh[ib32] >> 4*il;
  2487. const float dl = d * (1 + 2*((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf));
  2488. constant uint8_t * grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+0] | ((qh << 8) & 256)));
  2489. constant uint8_t * grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+1] | ((qh << 7) & 256)));
  2490. for (int i = 0; i < 4; ++i) {
  2491. reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
  2492. reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
  2493. }
  2494. grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+2] | ((qh << 6) & 256)));
  2495. grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+3] | ((qh << 5) & 256)));
  2496. for (int i = 0; i < 4; ++i) {
  2497. reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
  2498. reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
  2499. }
  2500. }
  2501. template <typename type4x4>
  2502. void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
  2503. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2504. const float d = xb->d;
  2505. const int ib32 = il/2;
  2506. il = il%2;
  2507. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2508. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  2509. device const uint8_t * signs = qs + QK_K/8;
  2510. const uint8_t qh = xb->qh[ib32] >> 4*il;
  2511. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  2512. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
  2513. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
  2514. for (int i = 0; i < 8; ++i) {
  2515. reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
  2516. reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
  2517. }
  2518. }
  2519. template <typename type4x4>
  2520. void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
  2521. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2522. const int ib32 = il/2;
  2523. il = il%2;
  2524. const float d = xb->d;
  2525. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  2526. device const uint16_t * qh = xb->qh;
  2527. const float dl = d * (2*((qh[ib32] >> 12) & 7) + 1);
  2528. const float ml = dl * (qh[ib32] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA);
  2529. const uint16_t h = qh[ib32] >> 6*il;
  2530. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((h << 8) & 0x700)));
  2531. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((h << 5) & 0x700)));
  2532. for (int i = 0; i < 4; ++i) {
  2533. reg[0][i] = dl * (grid1[i] & 0xf) + ml;
  2534. reg[1][i] = dl * (grid1[i] >> 4) + ml;
  2535. reg[2][i] = dl * (grid2[i] & 0xf) + ml;
  2536. reg[3][i] = dl * (grid2[i] >> 4) + ml;
  2537. }
  2538. }
  2539. template <typename type4x4>
  2540. void dequantize_iq1_m(device const block_iq1_m * xb, short il, thread type4x4 & reg) {
  2541. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2542. const int ib32 = il/2;
  2543. il = il%2;
  2544. device const uint16_t * sc = (device const uint16_t *)xb->scales;
  2545. iq1m_scale_t scale;
  2546. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2547. const float d = scale.f16;
  2548. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  2549. device const uint8_t * qh = xb->qh + 2*ib32 + il;
  2550. const float dl = d * (2*((sc[ib32/2] >> (6*(ib32%2)+3*il)) & 7) + 1);
  2551. const float ml1 = dl * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  2552. const float ml2 = dl * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  2553. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  2554. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  2555. for (int i = 0; i < 4; ++i) {
  2556. reg[0][i] = dl * (grid1[i] & 0xf) + ml1;
  2557. reg[1][i] = dl * (grid1[i] >> 4) + ml1;
  2558. reg[2][i] = dl * (grid2[i] & 0xf) + ml2;
  2559. reg[3][i] = dl * (grid2[i] >> 4) + ml2;
  2560. }
  2561. }
  2562. template <typename type4x4>
  2563. void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
  2564. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  2565. const float d = xb->d;
  2566. uint32_t aux32;
  2567. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  2568. for (int i = 0; i < 4; ++i) {
  2569. aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
  2570. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  2571. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  2572. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  2573. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  2574. }
  2575. }
  2576. template <typename type4>
  2577. void dequantize_iq4_nl_t4(device const block_iq4_nl * xb, short il, thread type4 & reg) {
  2578. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  2579. const float d = xb->d;
  2580. uint32_t aux32;
  2581. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  2582. aux32 = ((q4[2*(il%4)] | (q4[2*(il%4)+1] << 16)) >> 4*(il/4)) & 0x0f0f0f0f;
  2583. reg[0] = d * kvalues_iq4nl_f[q8[0]];
  2584. reg[1] = d * kvalues_iq4nl_f[q8[1]];
  2585. reg[2] = d * kvalues_iq4nl_f[q8[2]];
  2586. reg[3] = d * kvalues_iq4nl_f[q8[3]];
  2587. }
  2588. template <typename type4x4>
  2589. void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) {
  2590. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2591. const int ib32 = il/2;
  2592. il = il%2;
  2593. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2594. device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32;
  2595. const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4);
  2596. const float d = (float)xb->d * (ls - 32);
  2597. uint32_t aux32;
  2598. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  2599. for (int i = 0; i < 4; ++i) {
  2600. aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f;
  2601. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  2602. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  2603. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  2604. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  2605. }
  2606. }
  2607. enum ggml_sort_order {
  2608. GGML_SORT_ORDER_ASC,
  2609. GGML_SORT_ORDER_DESC,
  2610. };
  2611. // general-purpose kernel for addition, subtraction, multiplication and division of two tensors
  2612. // pros: works for non-contiguous tensors, supports broadcast across all dims
  2613. // cons: not very efficient
  2614. kernel void kernel_add(
  2615. constant ggml_metal_kargs_bin & args,
  2616. device const char * src0,
  2617. device const char * src1,
  2618. device char * dst,
  2619. uint3 tgpig[[threadgroup_position_in_grid]],
  2620. ushort3 tpitg[[thread_position_in_threadgroup]],
  2621. ushort3 ntg[[threads_per_threadgroup]]) {
  2622. const int i03 = tgpig.z;
  2623. const int i02 = tgpig.y;
  2624. const int i01 = tgpig.x;
  2625. const int i13 = i03%args.ne13;
  2626. const int i12 = i02%args.ne12;
  2627. const int i11 = i01%args.ne11;
  2628. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs;
  2629. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2630. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs;
  2631. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2632. const int i10 = i0%args.ne10;
  2633. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) + *((device float *)(src1_ptr + i10*args.nb10));
  2634. }
  2635. }
  2636. kernel void kernel_sub(
  2637. constant ggml_metal_kargs_bin & args,
  2638. device const char * src0,
  2639. device const char * src1,
  2640. device char * dst,
  2641. uint3 tgpig[[threadgroup_position_in_grid]],
  2642. ushort3 tpitg[[thread_position_in_threadgroup]],
  2643. ushort3 ntg[[threads_per_threadgroup]]) {
  2644. const int i03 = tgpig.z;
  2645. const int i02 = tgpig.y;
  2646. const int i01 = tgpig.x;
  2647. const int i13 = i03%args.ne13;
  2648. const int i12 = i02%args.ne12;
  2649. const int i11 = i01%args.ne11;
  2650. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs;
  2651. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2652. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs;
  2653. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2654. const int i10 = i0%args.ne10;
  2655. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) - *((device float *)(src1_ptr + i10*args.nb10));
  2656. }
  2657. }
  2658. kernel void kernel_mul(
  2659. constant ggml_metal_kargs_bin & args,
  2660. device const char * src0,
  2661. device const char * src1,
  2662. device char * dst,
  2663. uint3 tgpig[[threadgroup_position_in_grid]],
  2664. ushort3 tpitg[[thread_position_in_threadgroup]],
  2665. ushort3 ntg[[threads_per_threadgroup]]) {
  2666. const int i03 = tgpig.z;
  2667. const int i02 = tgpig.y;
  2668. const int i01 = tgpig.x;
  2669. const int i13 = i03%args.ne13;
  2670. const int i12 = i02%args.ne12;
  2671. const int i11 = i01%args.ne11;
  2672. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  2673. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2674. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1;
  2675. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2676. const int i10 = i0%args.ne10;
  2677. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) * *((device float *)(src1_ptr + i10*args.nb10));
  2678. }
  2679. }
  2680. kernel void kernel_div(
  2681. constant ggml_metal_kargs_bin & args,
  2682. device const char * src0,
  2683. device const char * src1,
  2684. device char * dst,
  2685. uint3 tgpig[[threadgroup_position_in_grid]],
  2686. ushort3 tpitg[[thread_position_in_threadgroup]],
  2687. ushort3 ntg[[threads_per_threadgroup]]) {
  2688. const int i03 = tgpig.z;
  2689. const int i02 = tgpig.y;
  2690. const int i01 = tgpig.x;
  2691. const int i13 = i03%args.ne13;
  2692. const int i12 = i02%args.ne12;
  2693. const int i11 = i01%args.ne11;
  2694. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  2695. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2696. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1;
  2697. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2698. const int i10 = i0%args.ne10;
  2699. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) / *((device float *)(src1_ptr + i10*args.nb10));
  2700. }
  2701. }
  2702. template<typename T>
  2703. kernel void kernel_repeat(
  2704. constant ggml_metal_kargs_repeat & args,
  2705. device const char * src0,
  2706. device char * dst,
  2707. uint3 tgpig[[threadgroup_position_in_grid]],
  2708. ushort3 tpitg[[thread_position_in_threadgroup]],
  2709. ushort3 ntg[[threads_per_threadgroup]]) {
  2710. const int i3 = tgpig.z;
  2711. const int i2 = tgpig.y;
  2712. const int i1 = tgpig.x;
  2713. const int i03 = i3%args.ne03;
  2714. const int i02 = i2%args.ne02;
  2715. const int i01 = i1%args.ne01;
  2716. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  2717. device char * dst_ptr = dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1;
  2718. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2719. const int i00 = i0%args.ne00;
  2720. *((device T *)(dst_ptr + i0*args.nb0)) = *((device T *)(src0_ptr + i00*args.nb00));
  2721. }
  2722. }
  2723. typedef decltype(kernel_repeat<float>) kernel_repeat_t;
  2724. template [[host_name("kernel_repeat_f32")]] kernel kernel_repeat_t kernel_repeat<float>;
  2725. template [[host_name("kernel_repeat_f16")]] kernel kernel_repeat_t kernel_repeat<half>;
  2726. template [[host_name("kernel_repeat_i32")]] kernel kernel_repeat_t kernel_repeat<int>;
  2727. template [[host_name("kernel_repeat_i16")]] kernel kernel_repeat_t kernel_repeat<short>;
  2728. // assumption: src1 is a row
  2729. // broadcast src1 into src0
  2730. kernel void kernel_add_row(
  2731. constant ggml_metal_kargs_bin & args,
  2732. device const float4 * src0,
  2733. device const float4 * src1,
  2734. device float4 * dst,
  2735. uint tpig[[thread_position_in_grid]]) {
  2736. const uint nb = args.ne00/4;
  2737. dst[tpig] = src0[tpig] + src1[tpig % nb];
  2738. }
  2739. kernel void kernel_sub_row(
  2740. constant ggml_metal_kargs_bin & args,
  2741. device const float4 * src0,
  2742. device const float4 * src1,
  2743. device float4 * dst,
  2744. uint tpig[[thread_position_in_grid]]) {
  2745. const uint nb = args.ne00/4;
  2746. dst[tpig] = src0[tpig] - src1[tpig % nb];
  2747. }
  2748. kernel void kernel_mul_row(
  2749. constant ggml_metal_kargs_bin & args,
  2750. device const float4 * src0,
  2751. device const float4 * src1,
  2752. device float4 * dst,
  2753. uint tpig[[thread_position_in_grid]]) {
  2754. const uint nb = args.ne00/4;
  2755. dst[tpig] = src0[tpig] * src1[tpig % nb];
  2756. }
  2757. kernel void kernel_div_row(
  2758. constant ggml_metal_kargs_bin & args,
  2759. device const float4 * src0,
  2760. device const float4 * src1,
  2761. device float4 * dst,
  2762. uint tpig[[thread_position_in_grid]]) {
  2763. const uint nb = args.ne00/4;
  2764. dst[tpig] = src0[tpig] / src1[tpig % nb];
  2765. }
  2766. kernel void kernel_scale(
  2767. device const float * src0,
  2768. device float * dst,
  2769. constant float & scale,
  2770. uint tpig[[thread_position_in_grid]]) {
  2771. dst[tpig] = src0[tpig] * scale;
  2772. }
  2773. kernel void kernel_scale_4(
  2774. device const float4 * src0,
  2775. device float4 * dst,
  2776. constant float & scale,
  2777. uint tpig[[thread_position_in_grid]]) {
  2778. dst[tpig] = src0[tpig] * scale;
  2779. }
  2780. kernel void kernel_clamp(
  2781. device const float * src0,
  2782. device float * dst,
  2783. constant float & min,
  2784. constant float & max,
  2785. uint tpig[[thread_position_in_grid]]) {
  2786. dst[tpig] = src0[tpig] < min ? min : (src0[tpig] > max ? max : src0[tpig]);
  2787. }
  2788. kernel void kernel_relu(
  2789. device const float * src0,
  2790. device float * dst,
  2791. uint tpig[[thread_position_in_grid]]) {
  2792. dst[tpig] = max(0.0f, src0[tpig]);
  2793. }
  2794. kernel void kernel_sigmoid(
  2795. device const float * src0,
  2796. device float * dst,
  2797. uint tpig[[thread_position_in_grid]]) {
  2798. dst[tpig] = 1.0f / (1.0f + exp(-src0[tpig]));
  2799. }
  2800. kernel void kernel_tanh(
  2801. device const float * src0,
  2802. device float * dst,
  2803. uint tpig[[thread_position_in_grid]]) {
  2804. device const float & x = src0[tpig];
  2805. dst[tpig] = precise::tanh(x);
  2806. }
  2807. constant float GELU_COEF_A = 0.044715f;
  2808. constant float GELU_QUICK_COEF = -1.702f;
  2809. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  2810. kernel void kernel_gelu(
  2811. device const float * src0,
  2812. device float * dst,
  2813. uint tpig[[thread_position_in_grid]]) {
  2814. device const float & x = src0[tpig];
  2815. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  2816. }
  2817. kernel void kernel_gelu_4(
  2818. device const float4 * src0,
  2819. device float4 * dst,
  2820. uint tpig[[thread_position_in_grid]]) {
  2821. device const float4 & x = src0[tpig];
  2822. // BEWARE !!!
  2823. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  2824. // This was observed with Falcon 7B and 40B models
  2825. //
  2826. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  2827. }
  2828. kernel void kernel_gelu_quick(
  2829. device const float * src0,
  2830. device float * dst,
  2831. uint tpig[[thread_position_in_grid]]) {
  2832. device const float & x = src0[tpig];
  2833. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  2834. }
  2835. kernel void kernel_gelu_quick_4(
  2836. device const float4 * src0,
  2837. device float4 * dst,
  2838. uint tpig[[thread_position_in_grid]]) {
  2839. device const float4 & x = src0[tpig];
  2840. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  2841. }
  2842. kernel void kernel_silu(
  2843. device const float * src0,
  2844. device float * dst,
  2845. uint tpig[[thread_position_in_grid]]) {
  2846. device const float & x = src0[tpig];
  2847. dst[tpig] = x / (1.0f + exp(-x));
  2848. }
  2849. kernel void kernel_silu_4(
  2850. device const float4 * src0,
  2851. device float4 * dst,
  2852. uint tpig[[thread_position_in_grid]]) {
  2853. device const float4 & x = src0[tpig];
  2854. dst[tpig] = x / (1.0f + exp(-x));
  2855. }
  2856. kernel void kernel_elu(
  2857. device const float * src0,
  2858. device float * dst,
  2859. uint tpig[[thread_position_in_grid]]) {
  2860. device const float & x = src0[tpig];
  2861. dst[tpig] = (x > 0.0f) ? x : (exp(x) - 1.0f);
  2862. }
  2863. kernel void kernel_sqr(
  2864. device const float * src0,
  2865. device float * dst,
  2866. uint tpig[[thread_position_in_grid]]) {
  2867. dst[tpig] = src0[tpig] * src0[tpig];
  2868. }
  2869. kernel void kernel_sqrt(
  2870. device const float * src0,
  2871. device float * dst,
  2872. uint tpig[[thread_position_in_grid]]) {
  2873. dst[tpig] = sqrt(src0[tpig]);
  2874. }
  2875. kernel void kernel_sin(
  2876. device const float * src0,
  2877. device float * dst,
  2878. uint tpig[[thread_position_in_grid]]) {
  2879. dst[tpig] = sin(src0[tpig]);
  2880. }
  2881. kernel void kernel_cos(
  2882. device const float * src0,
  2883. device float * dst,
  2884. uint tpig[[thread_position_in_grid]]) {
  2885. dst[tpig] = cos(src0[tpig]);
  2886. }
  2887. kernel void kernel_sum_rows(
  2888. device const float * src0,
  2889. device float * dst,
  2890. constant int64_t & ne00,
  2891. constant int64_t & ne01,
  2892. constant int64_t & ne02,
  2893. constant int64_t & ne03,
  2894. constant uint64_t & nb00,
  2895. constant uint64_t & nb01,
  2896. constant uint64_t & nb02,
  2897. constant uint64_t & nb03,
  2898. constant int64_t & ne10,
  2899. constant int64_t & ne11,
  2900. constant int64_t & ne12,
  2901. constant int64_t & ne13,
  2902. constant uint64_t & nb10,
  2903. constant uint64_t & nb11,
  2904. constant uint64_t & nb12,
  2905. constant uint64_t & nb13,
  2906. constant int64_t & ne0,
  2907. constant int64_t & ne1,
  2908. constant int64_t & ne2,
  2909. constant int64_t & ne3,
  2910. constant uint64_t & nb0,
  2911. constant uint64_t & nb1,
  2912. constant uint64_t & nb2,
  2913. constant uint64_t & nb3,
  2914. uint3 tpig[[thread_position_in_grid]]) {
  2915. int64_t i3 = tpig.z;
  2916. int64_t i2 = tpig.y;
  2917. int64_t i1 = tpig.x;
  2918. if (i3 >= ne03 || i2 >= ne02 || i1 >= ne01) {
  2919. return;
  2920. }
  2921. device const float * src_row = (device const float *) ((device const char *) src0 + i1*nb01 + i2*nb02 + i3*nb03);
  2922. device float * dst_row = (device float *) ((device char *) dst + i1*nb1 + i2*nb2 + i3*nb3);
  2923. float row_sum = 0;
  2924. for (int64_t i0 = 0; i0 < ne00; i0++) {
  2925. row_sum += src_row[i0];
  2926. }
  2927. dst_row[0] = row_sum;
  2928. }
  2929. template<typename T>
  2930. kernel void kernel_soft_max(
  2931. device const char * src0,
  2932. device const char * src1,
  2933. device char * dst,
  2934. constant int64_t & ne00,
  2935. constant int64_t & ne01,
  2936. constant int64_t & ne02,
  2937. constant float & scale,
  2938. constant float & max_bias,
  2939. constant float & m0,
  2940. constant float & m1,
  2941. constant uint32_t & n_head_log2,
  2942. threadgroup float * buf [[threadgroup(0)]],
  2943. uint tgpig[[threadgroup_position_in_grid]],
  2944. uint tpitg[[thread_position_in_threadgroup]],
  2945. uint sgitg[[simdgroup_index_in_threadgroup]],
  2946. uint tiisg[[thread_index_in_simdgroup]],
  2947. uint ntg[[threads_per_threadgroup]]) {
  2948. const int64_t i03 = (tgpig) / (ne02*ne01);
  2949. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  2950. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  2951. device const float * psrc0 = (device const float *) src0 + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  2952. device const T * pmask = src1 != src0 ? (device const T *) src1 + i01*ne00 : nullptr;
  2953. device float * pdst = (device float *) dst + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  2954. float slope = 1.0f;
  2955. // ALiBi
  2956. if (max_bias > 0.0f) {
  2957. const int64_t h = i02;
  2958. const float base = h < n_head_log2 ? m0 : m1;
  2959. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  2960. slope = pow(base, exp);
  2961. }
  2962. // parallel max
  2963. float lmax = -INFINITY;
  2964. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  2965. lmax = MAX(lmax, psrc0[i00]*scale + (pmask ? slope*pmask[i00] : 0.0f));
  2966. }
  2967. // find the max value in the block
  2968. float max_val = simd_max(lmax);
  2969. if (ntg > N_SIMDWIDTH) {
  2970. if (sgitg == 0) {
  2971. buf[tiisg] = -INFINITY;
  2972. }
  2973. threadgroup_barrier(mem_flags::mem_threadgroup);
  2974. if (tiisg == 0) {
  2975. buf[sgitg] = max_val;
  2976. }
  2977. threadgroup_barrier(mem_flags::mem_threadgroup);
  2978. max_val = buf[tiisg];
  2979. max_val = simd_max(max_val);
  2980. }
  2981. // parallel sum
  2982. float lsum = 0.0f;
  2983. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  2984. const float exp_psrc0 = exp((psrc0[i00]*scale + (pmask ? slope*pmask[i00] : 0.0f)) - max_val);
  2985. lsum += exp_psrc0;
  2986. pdst[i00] = exp_psrc0;
  2987. }
  2988. // This barrier fixes a failing test
  2989. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  2990. threadgroup_barrier(mem_flags::mem_none);
  2991. float sum = simd_sum(lsum);
  2992. if (ntg > N_SIMDWIDTH) {
  2993. if (sgitg == 0) {
  2994. buf[tiisg] = 0.0f;
  2995. }
  2996. threadgroup_barrier(mem_flags::mem_threadgroup);
  2997. if (tiisg == 0) {
  2998. buf[sgitg] = sum;
  2999. }
  3000. threadgroup_barrier(mem_flags::mem_threadgroup);
  3001. sum = buf[tiisg];
  3002. sum = simd_sum(sum);
  3003. }
  3004. const float inv_sum = 1.0f/sum;
  3005. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  3006. pdst[i00] *= inv_sum;
  3007. }
  3008. }
  3009. template<typename T>
  3010. kernel void kernel_soft_max_4(
  3011. device const char * src0,
  3012. device const char * src1,
  3013. device char * dst,
  3014. constant int64_t & ne00,
  3015. constant int64_t & ne01,
  3016. constant int64_t & ne02,
  3017. constant float & scale,
  3018. constant float & max_bias,
  3019. constant float & m0,
  3020. constant float & m1,
  3021. constant uint32_t & n_head_log2,
  3022. threadgroup float * buf [[threadgroup(0)]],
  3023. uint tgpig[[threadgroup_position_in_grid]],
  3024. uint tpitg[[thread_position_in_threadgroup]],
  3025. uint sgitg[[simdgroup_index_in_threadgroup]],
  3026. uint tiisg[[thread_index_in_simdgroup]],
  3027. uint ntg[[threads_per_threadgroup]]) {
  3028. const int64_t i03 = (tgpig) / (ne02*ne01);
  3029. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  3030. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  3031. device const float4 * psrc4 = (device const float4 *) src0 + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00)/4;
  3032. device const T * pmask = src1 != src0 ? (device const T *) src1 + i01*ne00/4 : nullptr;
  3033. device float4 * pdst4 = (device float4 *) dst + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00)/4;
  3034. float slope = 1.0f;
  3035. if (max_bias > 0.0f) {
  3036. const int64_t h = i02;
  3037. const float base = h < n_head_log2 ? m0 : m1;
  3038. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  3039. slope = pow(base, exp);
  3040. }
  3041. // parallel max
  3042. float4 lmax4 = -INFINITY;
  3043. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  3044. lmax4 = fmax(lmax4, psrc4[i00]*scale + (float4)((pmask ? slope*pmask[i00] : 0.0f)));
  3045. }
  3046. const float lmax = MAX(MAX(lmax4[0], lmax4[1]), MAX(lmax4[2], lmax4[3]));
  3047. float max_val = simd_max(lmax);
  3048. if (ntg > N_SIMDWIDTH) {
  3049. if (sgitg == 0) {
  3050. buf[tiisg] = -INFINITY;
  3051. }
  3052. threadgroup_barrier(mem_flags::mem_threadgroup);
  3053. if (tiisg == 0) {
  3054. buf[sgitg] = max_val;
  3055. }
  3056. threadgroup_barrier(mem_flags::mem_threadgroup);
  3057. max_val = buf[tiisg];
  3058. max_val = simd_max(max_val);
  3059. }
  3060. // parallel sum
  3061. float4 lsum4 = 0.0f;
  3062. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  3063. const float4 exp_psrc4 = exp((psrc4[i00]*scale + (float4)((pmask ? slope*pmask[i00] : 0.0f))) - max_val);
  3064. lsum4 += exp_psrc4;
  3065. pdst4[i00] = exp_psrc4;
  3066. }
  3067. const float lsum = lsum4[0] + lsum4[1] + lsum4[2] + lsum4[3];
  3068. // This barrier fixes a failing test
  3069. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  3070. threadgroup_barrier(mem_flags::mem_none);
  3071. float sum = simd_sum(lsum);
  3072. if (ntg > N_SIMDWIDTH) {
  3073. if (sgitg == 0) {
  3074. buf[tiisg] = 0.0f;
  3075. }
  3076. threadgroup_barrier(mem_flags::mem_threadgroup);
  3077. if (tiisg == 0) {
  3078. buf[sgitg] = sum;
  3079. }
  3080. threadgroup_barrier(mem_flags::mem_threadgroup);
  3081. sum = buf[tiisg];
  3082. sum = simd_sum(sum);
  3083. }
  3084. const float inv_sum = 1.0f/sum;
  3085. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  3086. pdst4[i00] *= inv_sum;
  3087. }
  3088. }
  3089. typedef decltype(kernel_soft_max<float>) kernel_soft_max_t;
  3090. typedef decltype(kernel_soft_max_4<float4>) kernel_soft_max_4_t;
  3091. template [[host_name("kernel_soft_max_f16")]] kernel kernel_soft_max_t kernel_soft_max<half>;
  3092. template [[host_name("kernel_soft_max_f32")]] kernel kernel_soft_max_t kernel_soft_max<float>;
  3093. template [[host_name("kernel_soft_max_f16_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<half4>;
  3094. template [[host_name("kernel_soft_max_f32_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<float4>;
  3095. kernel void kernel_diag_mask_inf(
  3096. device const float * src0,
  3097. device float * dst,
  3098. constant int64_t & ne00,
  3099. constant int64_t & ne01,
  3100. constant int & n_past,
  3101. uint3 tpig[[thread_position_in_grid]]) {
  3102. const int64_t i02 = tpig[2];
  3103. const int64_t i01 = tpig[1];
  3104. const int64_t i00 = tpig[0];
  3105. if (i00 > n_past + i01) {
  3106. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  3107. } else {
  3108. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  3109. }
  3110. }
  3111. kernel void kernel_diag_mask_inf_8(
  3112. device const float4 * src0,
  3113. device float4 * dst,
  3114. constant int64_t & ne00,
  3115. constant int64_t & ne01,
  3116. constant int & n_past,
  3117. uint3 tpig[[thread_position_in_grid]]) {
  3118. const int64_t i = 2*tpig[0];
  3119. dst[i+0] = src0[i+0];
  3120. dst[i+1] = src0[i+1];
  3121. int64_t i4 = 4*i;
  3122. const int64_t i02 = i4/(ne00*ne01); i4 -= i02*ne00*ne01;
  3123. const int64_t i01 = i4/(ne00); i4 -= i01*ne00;
  3124. const int64_t i00 = i4;
  3125. for (int k = 3; k >= 0; --k) {
  3126. if (i00 + 4 + k <= n_past + i01) {
  3127. break;
  3128. }
  3129. dst[i+1][k] = -INFINITY;
  3130. if (i00 + k > n_past + i01) {
  3131. dst[i][k] = -INFINITY;
  3132. }
  3133. }
  3134. }
  3135. // ref: ggml.c:ggml_compute_forward_ssm_conv_f32
  3136. // TODO: optimize
  3137. kernel void kernel_ssm_conv_f32(
  3138. device const void * src0,
  3139. device const void * src1,
  3140. device float * dst,
  3141. constant int64_t & ne00,
  3142. constant int64_t & ne01,
  3143. constant int64_t & ne02,
  3144. constant uint64_t & nb00,
  3145. constant uint64_t & nb01,
  3146. constant uint64_t & nb02,
  3147. constant int64_t & ne10,
  3148. constant int64_t & ne11,
  3149. constant uint64_t & nb10,
  3150. constant uint64_t & nb11,
  3151. constant int64_t & ne0,
  3152. constant int64_t & ne1,
  3153. constant int64_t & ne2,
  3154. constant uint64_t & nb0,
  3155. constant uint64_t & nb1,
  3156. constant uint64_t & nb2,
  3157. uint3 tgpig[[threadgroup_position_in_grid]],
  3158. uint3 tpitg[[thread_position_in_threadgroup]],
  3159. uint3 ntg[[threads_per_threadgroup]]) {
  3160. const int64_t ir = tgpig.x;
  3161. const int64_t i2 = tgpig.y;
  3162. const int64_t i3 = tgpig.z;
  3163. const int64_t nc = ne10;
  3164. //const int64_t ncs = ne00;
  3165. //const int64_t nr = ne01;
  3166. //const int64_t n_t = ne1;
  3167. //const int64_t n_s = ne2;
  3168. device const float * s = (device const float *) ((device const char *) src0 + ir*nb01 + i2*nb00 + i3*nb02);
  3169. device const float * c = (device const float *) ((device const char *) src1 + ir*nb11);
  3170. device float * x = (device float *) ((device char *) dst + ir*nb0 + i2*nb1 + i3*nb2);
  3171. float sumf = 0.0f;
  3172. for (int64_t i0 = 0; i0 < nc; ++i0) {
  3173. sumf += s[i0] * c[i0];
  3174. }
  3175. x[0] = sumf;
  3176. }
  3177. // ref: ggml.c:ggml_compute_forward_ssm_scan_f32
  3178. // TODO: optimize
  3179. kernel void kernel_ssm_scan_f32(
  3180. device const void * src0,
  3181. device const void * src1,
  3182. device const void * src2,
  3183. device const void * src3,
  3184. device const void * src4,
  3185. device const void * src5,
  3186. device float * dst,
  3187. constant int64_t & d_state,
  3188. constant int64_t & d_inner,
  3189. constant int64_t & n_seq_tokens,
  3190. constant int64_t & n_seqs,
  3191. constant uint64_t & nb00,
  3192. constant uint64_t & nb01,
  3193. constant uint64_t & nb02,
  3194. constant uint64_t & nb10,
  3195. constant uint64_t & nb11,
  3196. constant uint64_t & nb12,
  3197. constant uint64_t & nb13,
  3198. constant uint64_t & nb20,
  3199. constant uint64_t & nb21,
  3200. constant uint64_t & nb22,
  3201. constant uint64_t & nb30,
  3202. constant uint64_t & nb31,
  3203. constant uint64_t & nb40,
  3204. constant uint64_t & nb41,
  3205. constant uint64_t & nb42,
  3206. constant uint64_t & nb50,
  3207. constant uint64_t & nb51,
  3208. constant uint64_t & nb52,
  3209. uint3 tgpig[[threadgroup_position_in_grid]],
  3210. uint3 tpitg[[thread_position_in_threadgroup]],
  3211. uint3 ntg[[threads_per_threadgroup]]) {
  3212. const int64_t ir = tgpig.x;
  3213. const int64_t i3 = tgpig.y;
  3214. const int64_t nc = d_state;
  3215. //const int64_t nr = d_inner;
  3216. const int64_t n_t = n_seq_tokens;
  3217. //const int64_t n_s = n_seqs;
  3218. for (int64_t i2 = 0; i2 < n_t; ++i2) {
  3219. device const float * s0 = (device const float *) ((device const char *) src0 + ir*nb01 + i3*nb02);
  3220. device const float * x = (device const float *) ((device const char *) src1 + ir*nb10 + i2*nb11 + i3*nb12);
  3221. device const float * dt = (device const float *) ((device const char *) src2 + ir*nb20 + i2*nb21 + i3*nb22);
  3222. device const float * A = (device const float *) ((device const char *) src3 + ir*nb31);
  3223. device const float * B = (device const float *) ((device const char *) src4 + i2*nb41 + i3*nb42);
  3224. device const float * C = (device const float *) ((device const char *) src5 + i2*nb51 + i3*nb52);
  3225. device float * y = (device float *) ((device char *) dst + ir*nb10 + i2*nb11 + i3*nb12); // TODO: do not use src1 strides
  3226. device float * s = (device float *) ((device char *) dst + ir*nb01 + i3*nb02 + nb13);
  3227. if (i2 > 0) {
  3228. s0 = s;
  3229. }
  3230. // i1 == 0
  3231. float dt_soft_plus = dt[0] <= 20.0f ? log(1.0f + exp(dt[0])) : dt[0];
  3232. float x_dt = x[0] * dt_soft_plus;
  3233. float sumf = 0.0f;
  3234. for (int64_t i0 = 0; i0 < nc; ++i0) {
  3235. int64_t i = i0;
  3236. float state = (s0[i] * exp(dt_soft_plus * A[i])) + (B[i0] * x_dt);
  3237. sumf += state * C[i0];
  3238. s[i] = state;
  3239. }
  3240. y[0] = sumf;
  3241. }
  3242. }
  3243. kernel void kernel_argmax(
  3244. device const void * x,
  3245. device int32_t * dst,
  3246. constant int64_t & ncols,
  3247. constant uint64_t & nb01,
  3248. threadgroup float * shared_maxval [[threadgroup(0)]],
  3249. threadgroup int32_t * shared_argmax [[threadgroup(1)]],
  3250. uint tgpig[[threadgroup_position_in_grid]],
  3251. uint tpitg[[thread_position_in_threadgroup]],
  3252. uint sgitg[[simdgroup_index_in_threadgroup]],
  3253. uint tiisg[[thread_index_in_simdgroup]],
  3254. uint ntg[[threads_per_threadgroup]]) {
  3255. device const float * x_row = (device const float *) ((device const char *) x + tgpig * nb01);
  3256. float lmax = -INFINITY;
  3257. int32_t larg = -1;
  3258. for (int i00 = tpitg; i00 < ncols; i00 += ntg) {
  3259. if (x_row[i00] > lmax) {
  3260. lmax = x_row[i00];
  3261. larg = i00;
  3262. }
  3263. }
  3264. // find the argmax value in the block
  3265. float max_val = simd_max(lmax);
  3266. int32_t arg_val = simd_max(select(-1, larg, lmax == max_val));
  3267. if (ntg > N_SIMDWIDTH) {
  3268. if (sgitg == 0) {
  3269. shared_maxval[tiisg] = -INFINITY;
  3270. shared_argmax[tiisg] = -1;
  3271. }
  3272. threadgroup_barrier(mem_flags::mem_threadgroup);
  3273. if (tiisg == 0) {
  3274. shared_maxval[sgitg] = max_val;
  3275. shared_argmax[sgitg] = arg_val;
  3276. }
  3277. threadgroup_barrier(mem_flags::mem_threadgroup);
  3278. max_val = shared_maxval[tiisg];
  3279. arg_val = shared_argmax[tiisg];
  3280. float max_val_reduced = simd_max(max_val);
  3281. int32_t arg_val_reduced = simd_max(select(-1, arg_val, max_val == max_val_reduced));
  3282. dst[tgpig] = arg_val_reduced;
  3283. return;
  3284. }
  3285. dst[tgpig] = arg_val;
  3286. }
  3287. kernel void kernel_norm(
  3288. constant ggml_metal_kargs_norm & args,
  3289. device const char * src0,
  3290. device char * dst,
  3291. threadgroup float * shmem_f32 [[threadgroup(0)]],
  3292. uint tgpig[[threadgroup_position_in_grid]],
  3293. ushort tpitg[[thread_position_in_threadgroup]],
  3294. ushort sgitg[[simdgroup_index_in_threadgroup]],
  3295. ushort tiisg[[thread_index_in_simdgroup]],
  3296. ushort ntg[[threads_per_threadgroup]]) {
  3297. if (sgitg == 0) {
  3298. shmem_f32[tiisg] = 0.0f;
  3299. }
  3300. device const float4 * x = (device const float4 *) (src0 + tgpig*args.nb01);
  3301. float4 sumf4(0.0f);
  3302. float sumf = 0.0f;
  3303. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3304. sumf4 += x[i00];
  3305. }
  3306. sumf = sumf4[0] + sumf4[1] + sumf4[2] + sumf4[3];
  3307. sumf = simd_sum(sumf);
  3308. threadgroup_barrier(mem_flags::mem_threadgroup);
  3309. if (tiisg == 0) {
  3310. shmem_f32[sgitg] = sumf;
  3311. }
  3312. threadgroup_barrier(mem_flags::mem_threadgroup);
  3313. sumf = shmem_f32[tiisg];
  3314. sumf = simd_sum(sumf);
  3315. const float mean = sumf/args.ne00;
  3316. device float4 * y = (device float4 *) dst + tgpig*args.ne00_4;
  3317. sumf = 0.0f;
  3318. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3319. y[i00] = x[i00] - mean;
  3320. sumf += dot(y[i00], y[i00]);
  3321. }
  3322. sumf = simd_sum(sumf);
  3323. threadgroup_barrier(mem_flags::mem_threadgroup);
  3324. if (tiisg == 0) {
  3325. shmem_f32[sgitg] = sumf;
  3326. }
  3327. threadgroup_barrier(mem_flags::mem_threadgroup);
  3328. sumf = shmem_f32[tiisg];
  3329. sumf = simd_sum(sumf);
  3330. const float variance = sumf/args.ne00;
  3331. const float scale = 1.0f/sqrt(variance + args.eps);
  3332. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3333. y[i00] = y[i00] * scale;
  3334. }
  3335. }
  3336. kernel void kernel_rms_norm(
  3337. constant ggml_metal_kargs_rms_norm & args,
  3338. device const char * src0,
  3339. device char * dst,
  3340. threadgroup float * shmem_f32 [[threadgroup(0)]],
  3341. uint tgpig[[threadgroup_position_in_grid]],
  3342. ushort tpitg[[thread_position_in_threadgroup]],
  3343. ushort sgitg[[simdgroup_index_in_threadgroup]],
  3344. ushort tiisg[[thread_index_in_simdgroup]],
  3345. ushort ntg[[threads_per_threadgroup]]) {
  3346. if (sgitg == 0) {
  3347. shmem_f32[tiisg] = 0.0f;
  3348. }
  3349. device const float4 * x = (device const float4 *) (src0 + tgpig*args.nb01);
  3350. float sumf = 0.0f;
  3351. // parallel sum
  3352. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3353. sumf += dot(x[i00], x[i00]);
  3354. }
  3355. sumf = simd_sum(sumf);
  3356. threadgroup_barrier(mem_flags::mem_threadgroup);
  3357. if (tiisg == 0) {
  3358. shmem_f32[sgitg] = sumf;
  3359. }
  3360. threadgroup_barrier(mem_flags::mem_threadgroup);
  3361. sumf = shmem_f32[tiisg];
  3362. sumf = simd_sum(sumf);
  3363. const float mean = sumf/args.ne00;
  3364. const float scale = 1.0f/sqrt(mean + args.eps);
  3365. device float4 * y = (device float4 *) dst + tgpig*args.ne00_4;
  3366. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3367. y[i00] = x[i00] * scale;
  3368. }
  3369. }
  3370. kernel void kernel_group_norm(
  3371. device const float * src0,
  3372. device float * dst,
  3373. constant int64_t & ne00,
  3374. constant int64_t & ne01,
  3375. constant int64_t & ne02,
  3376. constant uint64_t & nb00,
  3377. constant uint64_t & nb01,
  3378. constant uint64_t & nb02,
  3379. constant int32_t & n_groups,
  3380. constant float & eps,
  3381. threadgroup float * buf [[threadgroup(0)]],
  3382. uint tgpig[[threadgroup_position_in_grid]],
  3383. uint tpitg[[thread_position_in_threadgroup]],
  3384. uint sgitg[[simdgroup_index_in_threadgroup]],
  3385. uint tiisg[[thread_index_in_simdgroup]],
  3386. uint ntg[[threads_per_threadgroup]]) {
  3387. const int64_t ne = ne00*ne01*ne02;
  3388. const int64_t gs = ne00*ne01*((ne02 + n_groups - 1) / n_groups);
  3389. int start = tgpig * gs;
  3390. int end = start + gs;
  3391. start += tpitg;
  3392. if (end >= ne) {
  3393. end = ne;
  3394. }
  3395. float tmp = 0.0f; // partial sum for thread in warp
  3396. for (int j = start; j < end; j += ntg) {
  3397. tmp += src0[j];
  3398. }
  3399. threadgroup_barrier(mem_flags::mem_threadgroup);
  3400. tmp = simd_sum(tmp);
  3401. if (ntg > N_SIMDWIDTH) {
  3402. if (sgitg == 0) {
  3403. buf[tiisg] = 0.0f;
  3404. }
  3405. threadgroup_barrier(mem_flags::mem_threadgroup);
  3406. if (tiisg == 0) {
  3407. buf[sgitg] = tmp;
  3408. }
  3409. threadgroup_barrier(mem_flags::mem_threadgroup);
  3410. tmp = buf[tiisg];
  3411. tmp = simd_sum(tmp);
  3412. }
  3413. const float mean = tmp / gs;
  3414. tmp = 0.0f;
  3415. for (int j = start; j < end; j += ntg) {
  3416. float xi = src0[j] - mean;
  3417. dst[j] = xi;
  3418. tmp += xi * xi;
  3419. }
  3420. tmp = simd_sum(tmp);
  3421. if (ntg > N_SIMDWIDTH) {
  3422. if (sgitg == 0) {
  3423. buf[tiisg] = 0.0f;
  3424. }
  3425. threadgroup_barrier(mem_flags::mem_threadgroup);
  3426. if (tiisg == 0) {
  3427. buf[sgitg] = tmp;
  3428. }
  3429. threadgroup_barrier(mem_flags::mem_threadgroup);
  3430. tmp = buf[tiisg];
  3431. tmp = simd_sum(tmp);
  3432. }
  3433. const float variance = tmp / gs;
  3434. const float scale = 1.0f/sqrt(variance + eps);
  3435. for (int j = start; j < end; j += ntg) {
  3436. dst[j] *= scale;
  3437. }
  3438. }
  3439. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  3440. // il indicates where the q4 quants begin (0 or QK4_0/4)
  3441. // we assume that the yl's have been multiplied with the appropriate scale factor
  3442. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3443. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  3444. float d = qb_curr->d;
  3445. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3446. device const uint16_t * qs = ((device const uint16_t *) qb_curr + 1 + il/2);
  3447. for (int i = 0; i < 8; i += 2) {
  3448. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F);
  3449. acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00);
  3450. acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0);
  3451. acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000);
  3452. }
  3453. return d * (sumy * -8.f + acc[0] + acc[1] + acc[2] + acc[3]);
  3454. }
  3455. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  3456. // il indicates where the q4 quants begin (0 or QK4_0/4)
  3457. // we assume that the yl's have been multiplied with the appropriate scale factor
  3458. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3459. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  3460. float d = qb_curr->d;
  3461. float m = qb_curr->m;
  3462. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3463. device const uint16_t * qs = ((device const uint16_t *) qb_curr + 2 + il/2);
  3464. for (int i = 0; i < 8; i+=2) {
  3465. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F);
  3466. acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00);
  3467. acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0);
  3468. acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000);
  3469. }
  3470. return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m;
  3471. }
  3472. // function for calculate inner product between half a q5_0 block and 16 floats (yl), sumy is SUM(yl[i])
  3473. // il indicates where the q5 quants begin (0 or QK5_0/4)
  3474. // we assume that the yl's have been multiplied with the appropriate scale factor
  3475. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3476. inline float block_q_n_dot_y(device const block_q5_0 * qb_curr, float sumy, thread float * yl, int il) {
  3477. float d = qb_curr->d;
  3478. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3479. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 3 + il/2);
  3480. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  3481. for (int i = 0; i < 8; i+=2) {
  3482. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010));
  3483. acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  3484. acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100));
  3485. acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  3486. }
  3487. return d * (sumy * -16.f + acc[0] + acc[1] + acc[2] + acc[3]);
  3488. }
  3489. // function for calculate inner product between half a q5_1 block and 16 floats (yl), sumy is SUM(yl[i])
  3490. // il indicates where the q5 quants begin (0 or QK5_1/4)
  3491. // we assume that the yl's have been multiplied with the appropriate scale factor
  3492. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3493. inline float block_q_n_dot_y(device const block_q5_1 * qb_curr, float sumy, thread float * yl, int il) {
  3494. float d = qb_curr->d;
  3495. float m = qb_curr->m;
  3496. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3497. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 4 + il/2);
  3498. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  3499. for (int i = 0; i < 8; i+=2) {
  3500. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010));
  3501. acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  3502. acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100));
  3503. acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  3504. }
  3505. return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m;
  3506. }
  3507. // putting them in the kernel cause a significant performance penalty
  3508. #define N_DST 4 // each SIMD group works on 4 rows
  3509. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  3510. //Note: This is a template, but strictly speaking it only applies to
  3511. // quantizations where the block size is 32. It also does not
  3512. // guard against the number of rows not being divisible by
  3513. // N_DST, so this is another explicit assumption of the implementation.
  3514. template<typename block_q_type, int nr, int nsg, int nw, typename args_t>
  3515. void mul_vec_q_n_f32_impl(
  3516. args_t args,
  3517. device const char * src0,
  3518. device const char * src1,
  3519. device char * dst,
  3520. threadgroup char * shmem,
  3521. uint3 tgpig,
  3522. ushort tiisg,
  3523. ushort sgitg) {
  3524. const int nb = args.ne00/QK4_0;
  3525. const int r0 = tgpig.x;
  3526. const int r1 = tgpig.y;
  3527. const int im = tgpig.z;
  3528. const int first_row = (r0 * nsg + sgitg) * nr;
  3529. const uint i12 = im%args.ne12;
  3530. const uint i13 = im/args.ne12;
  3531. //const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3532. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3533. //device const block_q_type * x = (device const block_q_type *) (src0 + offset0);
  3534. device const float * y = (device const float *) (src1 + offset1);
  3535. // pointers to src0 rows
  3536. device const block_q_type * ax[nr];
  3537. for (int row = 0; row < nr; ++row) {
  3538. const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3539. ax[row] = (device const block_q_type *) ((device char *) src0 + offset0);
  3540. }
  3541. float yl[16]; // src1 vector cache
  3542. float sumf[nr] = {0.f};
  3543. const short ix = (tiisg/2);
  3544. const short il = (tiisg%2)*8;
  3545. device const float * yb = y + ix*QK4_0 + il;
  3546. // each thread in a SIMD group deals with half a block.
  3547. for (int ib = ix; ib < nb; ib += nw/2) {
  3548. float sumy[2] = { 0.f, 0.f };
  3549. #pragma unroll
  3550. for (int i = 0; i < 8; i += 2) {
  3551. sumy[0] += yb[i + 0] + yb[i + 1];
  3552. yl[i + 0] = yb[i + 0];
  3553. yl[i + 1] = yb[i + 1]/256.f;
  3554. sumy[1] += yb[i + 16] + yb[i + 17];
  3555. yl[i + 8] = yb[i + 16]/16.f;
  3556. yl[i + 9] = yb[i + 17]/4096.f;
  3557. }
  3558. #pragma unroll
  3559. for (int row = 0; row < nr; row++) {
  3560. sumf[row] += block_q_n_dot_y(ax[row] + ib, sumy[0] + sumy[1], yl, il);
  3561. }
  3562. yb += QK4_0 * 16;
  3563. }
  3564. device float * dst_f32 = (device float *) dst + im*args.ne0*args.ne1 + r1*args.ne0;
  3565. for (int row = 0; row < nr; ++row) {
  3566. const float tot = simd_sum(sumf[row]);
  3567. if (tiisg == 0 && first_row + row < args.ne01) {
  3568. dst_f32[first_row + row] = tot;
  3569. }
  3570. }
  3571. }
  3572. kernel void kernel_mul_mv_q4_0_f32(
  3573. constant ggml_metal_kargs_mul_mv & args,
  3574. device const char * src0,
  3575. device const char * src1,
  3576. device char * dst,
  3577. uint3 tgpig[[threadgroup_position_in_grid]],
  3578. ushort tiisg[[thread_index_in_simdgroup]],
  3579. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3580. 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);
  3581. }
  3582. kernel void kernel_mul_mv_q4_1_f32(
  3583. constant ggml_metal_kargs_mul_mv & args,
  3584. device const char * src0,
  3585. device const char * src1,
  3586. device char * dst,
  3587. uint3 tgpig[[threadgroup_position_in_grid]],
  3588. ushort tiisg[[thread_index_in_simdgroup]],
  3589. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3590. 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);
  3591. }
  3592. kernel void kernel_mul_mv_q5_0_f32(
  3593. constant ggml_metal_kargs_mul_mv & args,
  3594. device const char * src0,
  3595. device const char * src1,
  3596. device char * dst,
  3597. uint3 tgpig[[threadgroup_position_in_grid]],
  3598. ushort tiisg[[thread_index_in_simdgroup]],
  3599. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3600. 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);
  3601. }
  3602. kernel void kernel_mul_mv_q5_1_f32(
  3603. constant ggml_metal_kargs_mul_mv & args,
  3604. device const char * src0,
  3605. device const char * src1,
  3606. device char * dst,
  3607. uint3 tgpig[[threadgroup_position_in_grid]],
  3608. ushort tiisg[[thread_index_in_simdgroup]],
  3609. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3610. 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);
  3611. }
  3612. #define NB_Q8_0 8
  3613. template<typename args_t>
  3614. void kernel_mul_mv_q8_0_f32_impl(
  3615. args_t args,
  3616. device const char * src0,
  3617. device const char * src1,
  3618. device char * dst,
  3619. threadgroup char * shmem,
  3620. uint3 tgpig,
  3621. ushort tiisg,
  3622. ushort sgitg) {
  3623. const int nr = N_DST;
  3624. const int nsg = N_SIMDGROUP;
  3625. const int nw = N_SIMDWIDTH;
  3626. const int nb = args.ne00/QK8_0;
  3627. const int r0 = tgpig.x;
  3628. const int r1 = tgpig.y;
  3629. const int im = tgpig.z;
  3630. const int first_row = (r0*nsg + sgitg)*nr;
  3631. const uint i12 = im%args.ne12;
  3632. const uint i13 = im/args.ne12;
  3633. //const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3634. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3635. //device const block_q8_0 * x = (device const block_q8_0 *) (src0 + offset0);
  3636. device const float * y = (device const float *) (src1 + offset1);
  3637. // pointers to src0 rows
  3638. device const block_q8_0 * ax[nr];
  3639. for (int row = 0; row < nr; ++row) {
  3640. const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3641. ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0);
  3642. }
  3643. float yl[NB_Q8_0];
  3644. float sumf[nr] = { 0.f };
  3645. const short ix = tiisg/4;
  3646. const short il = tiisg%4;
  3647. device const float * yb = y + ix*QK8_0 + il*NB_Q8_0;
  3648. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  3649. for (int ib = ix; ib < nb; ib += nw/4) {
  3650. for (short i = 0; i < NB_Q8_0; ++i) {
  3651. yl[i] = yb[i];
  3652. }
  3653. for (int row = 0; row < nr; row++) {
  3654. device const int8_t * qs = ax[row][ib].qs + il*NB_Q8_0;
  3655. float sumq = 0.f;
  3656. for (short iq = 0; iq < NB_Q8_0; ++iq) {
  3657. sumq += qs[iq] * yl[iq];
  3658. }
  3659. sumf[row] += sumq*ax[row][ib].d;
  3660. }
  3661. yb += nw*NB_Q8_0;
  3662. }
  3663. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  3664. for (int row = 0; row < nr; ++row) {
  3665. const float tot = simd_sum(sumf[row]);
  3666. if (tiisg == 0 && first_row + row < args.ne01) {
  3667. dst_f32[first_row + row] = tot;
  3668. }
  3669. }
  3670. }
  3671. [[host_name("kernel_mul_mv_q8_0_f32")]]
  3672. kernel void kernel_mul_mv_q8_0_f32(
  3673. constant ggml_metal_kargs_mul_mv & args,
  3674. device const char * src0,
  3675. device const char * src1,
  3676. device char * dst,
  3677. uint3 tgpig[[threadgroup_position_in_grid]],
  3678. ushort tiisg[[thread_index_in_simdgroup]],
  3679. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3680. kernel_mul_mv_q8_0_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  3681. }
  3682. // mat-vec kernel processing in chunks of float4
  3683. // chpb - chunks per quantization block
  3684. template<short nxpsg, short r1ptg, typename q_t, short chpb, void (*deq_t4)(device const q_t *, short, thread float4 &) >
  3685. void kernel_mul_mv_ext_q4_f32_impl(
  3686. constant ggml_metal_kargs_mul_mv_ext & args,
  3687. device const char * src0,
  3688. device const char * src1,
  3689. device char * dst,
  3690. uint3 tgpig[[threadgroup_position_in_grid]],
  3691. ushort tiisg[[thread_index_in_simdgroup]],
  3692. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3693. const short chpt = 4; // chunks per thread
  3694. //const short nxpsg = (32);
  3695. const short nypsg = (32/nxpsg);
  3696. const short tx = tiisg%nxpsg;
  3697. const short ty = tiisg/nxpsg;
  3698. const int i01 = tgpig.x*(nypsg*args.nsg) + nypsg*sgitg + ty;
  3699. const int i11 = tgpig.y*r1ptg;
  3700. const int i1m = tgpig.z;
  3701. const int i12 = i1m%args.ne12;
  3702. const int i13 = i1m/args.ne12;
  3703. const uint64_t offset0 = i01*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3704. const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3705. device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0;
  3706. device const float4 * y4[r1ptg];
  3707. for (int ir1 = 0; ir1 < r1ptg; ++ir1) {
  3708. y4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4 *) src1;
  3709. }
  3710. float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f };
  3711. short cch = tx%chpb; // current chunk index
  3712. for (int ich = tx; 4*ich < args.ne00; ich += chpt*nxpsg) {
  3713. float4 lx[chpt];
  3714. #pragma unroll(chpt)
  3715. for (short ch = 0; ch < chpt; ++ch) {
  3716. deq_t4(xq, cch, lx[ch]);
  3717. cch += nxpsg;
  3718. if (cch >= chpb) {
  3719. xq += cch/chpb;
  3720. cch %= chpb;
  3721. }
  3722. }
  3723. #pragma unroll(chpt)
  3724. for (short ch = 0; ch < chpt; ++ch) {
  3725. #pragma unroll(r1ptg)
  3726. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3727. sumf[ir1] += dot(lx[ch], y4[ir1][ch*nxpsg]);
  3728. }
  3729. }
  3730. #pragma unroll(r1ptg)
  3731. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3732. y4[ir1] += chpt*nxpsg;
  3733. }
  3734. }
  3735. // reduce only the threads in each row
  3736. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3737. if (nxpsg >= 32) {
  3738. sumf[ir1] += simd_shuffle_down(sumf[ir1], 16);
  3739. }
  3740. if (nxpsg >= 16) {
  3741. sumf[ir1] += simd_shuffle_down(sumf[ir1], 8);
  3742. }
  3743. if (nxpsg >= 8) {
  3744. sumf[ir1] += simd_shuffle_down(sumf[ir1], 4);
  3745. }
  3746. if (nxpsg >= 4) {
  3747. sumf[ir1] += simd_shuffle_down(sumf[ir1], 2);
  3748. }
  3749. if (nxpsg >= 2) {
  3750. sumf[ir1] += simd_shuffle_down(sumf[ir1], 1);
  3751. }
  3752. //sumf[ir1] = simd_sum(sumf[ir1]);
  3753. }
  3754. if (tx == 0) {
  3755. for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) {
  3756. device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0;
  3757. if (i01 < args.ne01) {
  3758. dst_f32[i01] = sumf[ir1];
  3759. }
  3760. }
  3761. }
  3762. }
  3763. // mat-vec kernel processing in chunks of float4x4
  3764. template<short nxpsg, short r1ptg, typename q_t, short chpb, void (*deq_t4x4)(device const q_t *, short, thread float4x4 &) >
  3765. void kernel_mul_mv_ext_q4x4_f32_impl(
  3766. constant ggml_metal_kargs_mul_mv_ext & args,
  3767. device const char * src0,
  3768. device const char * src1,
  3769. device char * dst,
  3770. uint3 tgpig[[threadgroup_position_in_grid]],
  3771. ushort tiisg[[thread_index_in_simdgroup]],
  3772. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3773. const short chpt = 1;
  3774. //const short nxpsg = (32);
  3775. const short nypsg = (32/nxpsg);
  3776. const short tx = tiisg%nxpsg;
  3777. const short ty = tiisg/nxpsg;
  3778. const int i01 = tgpig.x*(nypsg*args.nsg) + nypsg*sgitg + ty;
  3779. const int i11 = tgpig.y*r1ptg;
  3780. const int i1m = tgpig.z;
  3781. const int i12 = i1m%args.ne12;
  3782. const int i13 = i1m/args.ne12;
  3783. const uint64_t offset0 = i01*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3784. const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3785. device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0;
  3786. device const float4x4 * y4x4[r1ptg];
  3787. for (int ir1 = 0; ir1 < r1ptg; ++ir1) {
  3788. y4x4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4x4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4x4 *) src1;
  3789. }
  3790. float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f };
  3791. short cch = tx%chpb;
  3792. for (int ich = tx; 16*ich < args.ne00; ich += chpt*nxpsg) {
  3793. float4x4 lx[chpt];
  3794. #pragma unroll(chpt)
  3795. for (short ch = 0; ch < chpt; ++ch) {
  3796. deq_t4x4(xq, cch, lx[ch]);
  3797. cch += nxpsg;
  3798. if (cch >= chpb) {
  3799. xq += cch/chpb;
  3800. cch %= chpb;
  3801. }
  3802. }
  3803. #pragma unroll(chpt)
  3804. for (short ch = 0; ch < chpt; ++ch) {
  3805. #pragma unroll(r1ptg)
  3806. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3807. sumf[ir1] +=
  3808. dot(lx[ch][0], y4x4[ir1][ch*nxpsg][0]) +
  3809. dot(lx[ch][1], y4x4[ir1][ch*nxpsg][1]) +
  3810. dot(lx[ch][2], y4x4[ir1][ch*nxpsg][2]) +
  3811. dot(lx[ch][3], y4x4[ir1][ch*nxpsg][3]);
  3812. }
  3813. }
  3814. #pragma unroll(r1ptg)
  3815. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3816. y4x4[ir1] += chpt*nxpsg;
  3817. }
  3818. }
  3819. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3820. if (nxpsg >= 32) {
  3821. sumf[ir1] += simd_shuffle_down(sumf[ir1], 16);
  3822. }
  3823. if (nxpsg >= 16) {
  3824. sumf[ir1] += simd_shuffle_down(sumf[ir1], 8);
  3825. }
  3826. if (nxpsg >= 8) {
  3827. sumf[ir1] += simd_shuffle_down(sumf[ir1], 4);
  3828. }
  3829. if (nxpsg >= 4) {
  3830. sumf[ir1] += simd_shuffle_down(sumf[ir1], 2);
  3831. }
  3832. if (nxpsg >= 2) {
  3833. sumf[ir1] += simd_shuffle_down(sumf[ir1], 1);
  3834. }
  3835. //sumf[ir1] = simd_sum(sumf[ir1]);
  3836. }
  3837. if (tx == 0) {
  3838. for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) {
  3839. device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0;
  3840. if (i01 < args.ne01) {
  3841. dst_f32[i01] = sumf[ir1];
  3842. }
  3843. }
  3844. }
  3845. }
  3846. // dispatchers needed for compile-time nxpsg
  3847. // epb - elements per quantization block
  3848. template<short r1ptg, typename q_t, short epb, void (*deq_t4)(device const q_t *, short, thread float4 &)>
  3849. kernel void kernel_mul_mv_ext_q4_f32_disp(
  3850. constant ggml_metal_kargs_mul_mv_ext & args,
  3851. device const char * src0,
  3852. device const char * src1,
  3853. device char * dst,
  3854. uint3 tgpig[[threadgroup_position_in_grid]],
  3855. ushort tiisg[[thread_index_in_simdgroup]],
  3856. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3857. switch (args.nxpsg) {
  3858. 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;
  3859. 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;
  3860. 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;
  3861. 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;
  3862. }
  3863. }
  3864. template<short r1ptg, typename q_t, short epb, void (*deq_t4x4)(device const q_t *, short, thread float4x4 &)>
  3865. kernel void kernel_mul_mv_ext_q4x4_f32_disp(
  3866. constant ggml_metal_kargs_mul_mv_ext & args,
  3867. device const char * src0,
  3868. device const char * src1,
  3869. device char * dst,
  3870. uint3 tgpig[[threadgroup_position_in_grid]],
  3871. ushort tiisg[[thread_index_in_simdgroup]],
  3872. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3873. switch (args.nxpsg) {
  3874. 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;
  3875. 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;
  3876. 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;
  3877. 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;
  3878. }
  3879. }
  3880. typedef decltype(kernel_mul_mv_ext_q4_f32_disp <2, block_q8_0, 32, dequantize_q8_0_t4>) mul_mv_ext_q4_f32_t;
  3881. typedef decltype(kernel_mul_mv_ext_q4x4_f32_disp<2, block_q4_K, 256, dequantize_q4_K>) mul_mv_ext_q4x4_f32_t;
  3882. 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>;
  3883. 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>;
  3884. 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>;
  3885. 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>;
  3886. 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>;
  3887. 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>;
  3888. 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>;
  3889. 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>;
  3890. 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>;
  3891. 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>;
  3892. 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>;
  3893. 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>;
  3894. 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>;
  3895. 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>;
  3896. 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>;
  3897. 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>;
  3898. 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>;
  3899. 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>;
  3900. 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>;
  3901. 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>;
  3902. 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>;
  3903. 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>;
  3904. 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>;
  3905. 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>;
  3906. 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>;
  3907. 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>;
  3908. 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>;
  3909. 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>;
  3910. 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>;
  3911. 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>;
  3912. 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>;
  3913. 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>;
  3914. 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>;
  3915. 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>;
  3916. 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>;
  3917. 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>;
  3918. 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>;
  3919. 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>;
  3920. 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>;
  3921. 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>;
  3922. #define N_MV_T_T 4
  3923. template<typename T0, typename T04, typename T1, typename T14, typename args_t>
  3924. void kernel_mul_mv_impl(
  3925. args_t args,
  3926. device const char * src0,
  3927. device const char * src1,
  3928. device char * dst,
  3929. uint3 tgpig,
  3930. ushort tiisg) {
  3931. const int r0 = tgpig.x;
  3932. const int rb = tgpig.y*N_MV_T_T;
  3933. const int im = tgpig.z;
  3934. const uint i12 = im%args.ne12;
  3935. const uint i13 = im/args.ne12;
  3936. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3937. device const T0 * x = (device const T0 *) (src0 + offset0);
  3938. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1;
  3939. if (args.ne00 < 128) {
  3940. for (int row = 0; row < N_MV_T_T; ++row) {
  3941. int r1 = rb + row;
  3942. if (r1 >= args.ne11) {
  3943. break;
  3944. }
  3945. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3946. device const T1 * y = (device const T1 *) (src1 + offset1);
  3947. float sumf = 0;
  3948. for (int i = tiisg; i < args.ne00; i += 32) {
  3949. sumf += (T0) x[i] * (T1) y[i];
  3950. }
  3951. float all_sum = simd_sum(sumf);
  3952. if (tiisg == 0) {
  3953. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  3954. }
  3955. }
  3956. } else {
  3957. device const T04 * x4 = (device const T04 *) x;
  3958. for (int row = 0; row < N_MV_T_T; ++row) {
  3959. int r1 = rb + row;
  3960. if (r1 >= args.ne11) {
  3961. break;
  3962. }
  3963. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3964. device const T1 * y = (device const T1 *) (src1 + offset1);
  3965. device const T14 * y4 = (device const T14 *) y;
  3966. float sumf = 0;
  3967. for (int i = tiisg; i < args.ne00/4; i += 32) {
  3968. sumf += dot((float4) x4[i], (float4) y4[i]);
  3969. }
  3970. float all_sum = simd_sum(sumf);
  3971. if (tiisg == 0) {
  3972. for (int i = 4*(args.ne00/4); i < args.ne00; ++i) all_sum += (float) (x[i] * y[i]);
  3973. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  3974. }
  3975. }
  3976. }
  3977. }
  3978. template<typename T0, typename T04, typename T1, typename T14>
  3979. kernel void kernel_mul_mv(
  3980. constant ggml_metal_kargs_mul_mv & args,
  3981. device const char * src0,
  3982. device const char * src1,
  3983. device char * dst,
  3984. uint3 tgpig[[threadgroup_position_in_grid]],
  3985. ushort tiisg[[thread_index_in_simdgroup]]) {
  3986. kernel_mul_mv_impl<T0, T04, T1, T14, constant ggml_metal_kargs_mul_mv &>(
  3987. args,
  3988. src0,
  3989. src1,
  3990. dst,
  3991. tgpig,
  3992. tiisg);
  3993. }
  3994. typedef decltype(kernel_mul_mv<half, half4, half, half4>) mul_mv_t;
  3995. template [[host_name("kernel_mul_mv_f32_f32")]] kernel mul_mv_t kernel_mul_mv<float, float4, float, float4>;
  3996. template [[host_name("kernel_mul_mv_f16_f32")]] kernel mul_mv_t kernel_mul_mv<half, half4, float, float4>;
  3997. template [[host_name("kernel_mul_mv_f16_f16")]] kernel mul_mv_t kernel_mul_mv<half, half4, half, half4>;
  3998. #if defined(GGML_METAL_USE_BF16)
  3999. template [[host_name("kernel_mul_mv_bf16_f32")]] kernel mul_mv_t kernel_mul_mv<bfloat, bfloat4, float, float4>;
  4000. template [[host_name("kernel_mul_mv_bf16_bf16")]] kernel mul_mv_t kernel_mul_mv<bfloat, bfloat4, bfloat, bfloat4>;
  4001. #endif
  4002. template<typename T, typename T4>
  4003. kernel void kernel_mul_mv_1row(
  4004. constant ggml_metal_kargs_mul_mv & args,
  4005. device const char * src0,
  4006. device const char * src1,
  4007. device char * dst,
  4008. uint3 tgpig[[threadgroup_position_in_grid]],
  4009. ushort tiisg[[thread_index_in_simdgroup]]) {
  4010. const int r0 = tgpig.x;
  4011. const int r1 = tgpig.y;
  4012. const int im = tgpig.z;
  4013. const uint i12 = im%args.ne12;
  4014. const uint i13 = im/args.ne12;
  4015. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4016. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4017. device const T * x = (device const T *) (src0 + offset0);
  4018. device const float * y = (device const float *) (src1 + offset1);
  4019. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4020. float sumf = 0;
  4021. if (args.ne00 < 128) {
  4022. for (int i = tiisg; i < args.ne00; i += 32) {
  4023. sumf += (float) x[i] * (float) y[i];
  4024. }
  4025. float all_sum = simd_sum(sumf);
  4026. if (tiisg == 0) {
  4027. dst_f32[r0] = all_sum;
  4028. }
  4029. } else {
  4030. device const T4 * x4 = (device const T4 *) x;
  4031. device const float4 * y4 = (device const float4 *) y;
  4032. for (int i = tiisg; i < args.ne00/4; i += 32) {
  4033. sumf += dot((float4) x4[i], y4[i]);
  4034. }
  4035. float all_sum = simd_sum(sumf);
  4036. if (tiisg == 0) {
  4037. for (int i = 4*(args.ne00/4); i < args.ne00; ++i) all_sum += (float) (x[i] * y[i]);
  4038. dst_f32[r0] = all_sum;
  4039. }
  4040. }
  4041. }
  4042. typedef decltype(kernel_mul_mv_1row<half, half4>) mul_mv_1row_t;
  4043. template [[host_name("kernel_mul_mv_f16_f32_1row")]] kernel mul_mv_1row_t kernel_mul_mv_1row<half, half4>;
  4044. #if defined(GGML_METAL_USE_BF16)
  4045. template [[host_name("kernel_mul_mv_bf16_f32_1row")]] kernel mul_mv_1row_t kernel_mul_mv_1row<bfloat, bfloat4>;
  4046. #endif
  4047. // Assumes row size (ne00) is a multiple of 4
  4048. template<typename T, typename T4>
  4049. kernel void kernel_mul_mv_l4(
  4050. constant ggml_metal_kargs_mul_mv & args,
  4051. device const char * src0,
  4052. device const char * src1,
  4053. device char * dst,
  4054. uint3 tgpig[[threadgroup_position_in_grid]],
  4055. ushort tiisg[[thread_index_in_simdgroup]]) {
  4056. const int nrows = args.ne11;
  4057. const int r0 = tgpig.x;
  4058. const int im = tgpig.z;
  4059. const uint i12 = im%args.ne12;
  4060. const uint i13 = im/args.ne12;
  4061. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4062. device const T4 * x4 = (device const T4 *) (src0 + offset0);
  4063. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1;
  4064. for (int r1 = 0; r1 < nrows; ++r1) {
  4065. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4066. device const float4 * y4 = (device const float4 *) (src1 + offset1);
  4067. float sumf = 0;
  4068. for (int i = tiisg; i < args.ne00/4; i += 32) {
  4069. sumf += dot((float4) x4[i], y4[i]);
  4070. }
  4071. float all_sum = simd_sum(sumf);
  4072. if (tiisg == 0) {
  4073. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  4074. }
  4075. }
  4076. }
  4077. typedef decltype(kernel_mul_mv_l4<half, half4>) mul_mv_l4_t;
  4078. template [[host_name("kernel_mul_mv_f16_f32_l4")]] kernel mul_mv_l4_t kernel_mul_mv_l4<half, half4>;
  4079. #if defined(GGML_METAL_USE_BF16)
  4080. template [[host_name("kernel_mul_mv_bf16_f32_l4")]] kernel mul_mv_l4_t kernel_mul_mv_l4<bfloat, bfloat4>;
  4081. #endif
  4082. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  4083. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4084. return 1.0f - min(1.0f, max(0.0f, y));
  4085. }
  4086. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4087. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4088. static void rope_yarn(
  4089. float theta_extrap, float freq_scale, float corr_dims[2], int i0, float ext_factor, float mscale,
  4090. thread float * cos_theta, thread float * sin_theta) {
  4091. // Get n-d rotational scaling corrected for extrapolation
  4092. float theta_interp = freq_scale * theta_extrap;
  4093. float theta = theta_interp;
  4094. if (ext_factor != 0.0f) {
  4095. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  4096. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4097. // Get n-d magnitude scaling corrected for interpolation
  4098. mscale *= 1.0f + 0.1f * log(1.0f / freq_scale);
  4099. }
  4100. *cos_theta = cos(theta) * mscale;
  4101. *sin_theta = sin(theta) * mscale;
  4102. }
  4103. // Apparently solving `n_rot = 2pi * x * base^((2 * max_pos_emb) / n_dims)` for x, we get
  4104. // `corr_fac(n_rot) = n_dims * log(max_pos_emb / (n_rot * 2pi)) / (2 * log(base))`
  4105. static float rope_yarn_corr_factor(int n_dims, int n_ctx_orig, float n_rot, float base) {
  4106. return n_dims * log(n_ctx_orig / (n_rot * 2 * M_PI_F)) / (2 * log(base));
  4107. }
  4108. static void rope_yarn_corr_dims(
  4109. int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]
  4110. ) {
  4111. // start and end correction dims
  4112. dims[0] = max(0.0f, floor(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_fast, freq_base)));
  4113. dims[1] = min(n_dims - 1.0f, ceil(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_slow, freq_base)));
  4114. }
  4115. template<typename T>
  4116. kernel void kernel_rope_norm(
  4117. constant ggml_metal_kargs_rope & args,
  4118. device const char * src0,
  4119. device const char * src1,
  4120. device const char * src2,
  4121. device char * dst,
  4122. ushort tiitg[[thread_index_in_threadgroup]],
  4123. ushort3 tptg [[threads_per_threadgroup]],
  4124. uint3 tgpig[[threadgroup_position_in_grid]]) {
  4125. const int i3 = tgpig[2];
  4126. const int i2 = tgpig[1];
  4127. const int i1 = tgpig[0];
  4128. float corr_dims[2];
  4129. rope_yarn_corr_dims(args.n_dims, args.n_ctx_orig, args.freq_base, args.beta_fast, args.beta_slow, corr_dims);
  4130. device const int32_t * pos = (device const int32_t *) src1;
  4131. const float theta_base = (float) pos[i2];
  4132. const float inv_ndims = -1.f/args.n_dims;
  4133. float cos_theta;
  4134. float sin_theta;
  4135. for (int i0 = 2*tiitg; i0 < args.ne0; i0 += 2*tptg.x) {
  4136. if (i0 < args.n_dims) {
  4137. const int ic = i0/2;
  4138. const float theta = theta_base * pow(args.freq_base, inv_ndims*i0);
  4139. const float freq_factor = src2 != src0 ? ((device const float *) src2)[ic] : 1.0f;
  4140. rope_yarn(theta/freq_factor, args.freq_scale, corr_dims, i0, args.ext_factor, args.attn_factor, &cos_theta, &sin_theta);
  4141. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  4142. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  4143. const float x0 = src[0];
  4144. const float x1 = src[1];
  4145. dst_data[0] = x0*cos_theta - x1*sin_theta;
  4146. dst_data[1] = x0*sin_theta + x1*cos_theta;
  4147. } else {
  4148. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  4149. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  4150. dst_data[0] = src[0];
  4151. dst_data[1] = src[1];
  4152. }
  4153. }
  4154. }
  4155. template<typename T>
  4156. kernel void kernel_rope_neox(
  4157. constant ggml_metal_kargs_rope & args,
  4158. device const char * src0,
  4159. device const char * src1,
  4160. device const char * src2,
  4161. device char * dst,
  4162. ushort tiitg[[thread_index_in_threadgroup]],
  4163. ushort3 tptg [[threads_per_threadgroup]],
  4164. uint3 tgpig[[threadgroup_position_in_grid]]) {
  4165. const int i3 = tgpig[2];
  4166. const int i2 = tgpig[1];
  4167. const int i1 = tgpig[0];
  4168. float corr_dims[2];
  4169. rope_yarn_corr_dims(args.n_dims, args.n_ctx_orig, args.freq_base, args.beta_fast, args.beta_slow, corr_dims);
  4170. device const int32_t * pos = (device const int32_t *) src1;
  4171. const float theta_base = (float) pos[i2];
  4172. const float inv_ndims = -1.f/args.n_dims;
  4173. float cos_theta;
  4174. float sin_theta;
  4175. for (int i0 = 2*tiitg; i0 < args.ne0; i0 += 2*tptg.x) {
  4176. if (i0 < args.n_dims) {
  4177. const int ic = i0/2;
  4178. const float theta = theta_base * pow(args.freq_base, inv_ndims*i0);
  4179. const float freq_factor = src2 != src0 ? ((device const float *) src2)[ic] : 1.0f;
  4180. rope_yarn(theta/freq_factor, args.freq_scale, corr_dims, i0, args.ext_factor, args.attn_factor, &cos_theta, &sin_theta);
  4181. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + ic*args.nb00);
  4182. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + ic*args.nb0);
  4183. const float x0 = src[0];
  4184. const float x1 = src[args.n_dims/2];
  4185. dst_data[0] = x0*cos_theta - x1*sin_theta;
  4186. dst_data[args.n_dims/2] = x0*sin_theta + x1*cos_theta;
  4187. } else {
  4188. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  4189. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  4190. dst_data[0] = src[0];
  4191. dst_data[1] = src[1];
  4192. }
  4193. }
  4194. }
  4195. typedef decltype(kernel_rope_norm<float>) kernel_rope_norm_t;
  4196. typedef decltype(kernel_rope_neox<float>) kernel_rope_neox_t;
  4197. template [[host_name("kernel_rope_norm_f32")]] kernel kernel_rope_norm_t kernel_rope_norm<float>;
  4198. template [[host_name("kernel_rope_norm_f16")]] kernel kernel_rope_norm_t kernel_rope_norm<half>;
  4199. template [[host_name("kernel_rope_neox_f32")]] kernel kernel_rope_neox_t kernel_rope_neox<float>;
  4200. template [[host_name("kernel_rope_neox_f16")]] kernel kernel_rope_neox_t kernel_rope_neox<half>;
  4201. typedef void (im2col_t)(
  4202. device const float * x,
  4203. device char * dst,
  4204. constant int32_t & ofs0,
  4205. constant int32_t & ofs1,
  4206. constant int32_t & IW,
  4207. constant int32_t & IH,
  4208. constant int32_t & CHW,
  4209. constant int32_t & s0,
  4210. constant int32_t & s1,
  4211. constant int32_t & p0,
  4212. constant int32_t & p1,
  4213. constant int32_t & d0,
  4214. constant int32_t & d1,
  4215. uint3 tgpig[[threadgroup_position_in_grid]],
  4216. uint3 tgpg[[threadgroups_per_grid]],
  4217. uint3 tpitg[[thread_position_in_threadgroup]],
  4218. uint3 ntg[[threads_per_threadgroup]]);
  4219. template <typename T>
  4220. kernel void kernel_im2col(
  4221. device const float * x,
  4222. device char * dst,
  4223. constant int32_t & ofs0,
  4224. constant int32_t & ofs1,
  4225. constant int32_t & IW,
  4226. constant int32_t & IH,
  4227. constant int32_t & CHW,
  4228. constant int32_t & s0,
  4229. constant int32_t & s1,
  4230. constant int32_t & p0,
  4231. constant int32_t & p1,
  4232. constant int32_t & d0,
  4233. constant int32_t & d1,
  4234. uint3 tgpig[[threadgroup_position_in_grid]],
  4235. uint3 tgpg[[threadgroups_per_grid]],
  4236. uint3 tpitg[[thread_position_in_threadgroup]],
  4237. uint3 ntg[[threads_per_threadgroup]]) {
  4238. // const int64_t IC = tgpg[0];
  4239. const int64_t OH = tgpg[1];
  4240. const int64_t OW = tgpg[2];
  4241. // const int64_t N = ntg[0];
  4242. const int64_t KH = ntg[1];
  4243. const int64_t KW = ntg[2];
  4244. const int64_t in = tpitg[0];
  4245. const int64_t ikh = tpitg[1];
  4246. const int64_t ikw = tpitg[2];
  4247. const int64_t iic = tgpig[0];
  4248. const int64_t ioh = tgpig[1];
  4249. const int64_t iow = tgpig[2];
  4250. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  4251. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  4252. const int64_t offset_dst = (in*OH*OW + ioh*OW + iow)*CHW + (iic*(KH*KW) + ikh*KW + ikw);
  4253. device T * pdst = (device T *) (dst);
  4254. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4255. pdst[offset_dst] = 0.0f;
  4256. } else {
  4257. const int64_t offset_src = in*ofs0 + iic*ofs1 + iih*IW + iiw;
  4258. pdst[offset_dst] = x[offset_src];
  4259. }
  4260. }
  4261. template [[host_name("kernel_im2col_f32")]] kernel im2col_t kernel_im2col<float>;
  4262. template [[host_name("kernel_im2col_f16")]] kernel im2col_t kernel_im2col<half>;
  4263. typedef void (im2col_ext_t)(
  4264. device const float * x,
  4265. device char * dst,
  4266. constant int32_t & ofs0,
  4267. constant int32_t & ofs1,
  4268. constant int32_t & IW,
  4269. constant int32_t & IH,
  4270. constant int32_t & CHW,
  4271. constant int32_t & s0,
  4272. constant int32_t & s1,
  4273. constant int32_t & p0,
  4274. constant int32_t & p1,
  4275. constant int32_t & d0,
  4276. constant int32_t & d1,
  4277. constant int32_t & N,
  4278. constant int32_t & KH,
  4279. constant int32_t & KW,
  4280. uint3 tgpig[[threadgroup_position_in_grid]],
  4281. uint3 tgpg[[threadgroups_per_grid]],
  4282. uint3 tpitg[[thread_position_in_threadgroup]],
  4283. uint3 ntg[[threads_per_threadgroup]]);
  4284. template <typename T>
  4285. kernel void kernel_im2col_ext(
  4286. device const float * x,
  4287. device char * dst,
  4288. constant int32_t & ofs0,
  4289. constant int32_t & ofs1,
  4290. constant int32_t & IW,
  4291. constant int32_t & IH,
  4292. constant int32_t & CHW,
  4293. constant int32_t & s0,
  4294. constant int32_t & s1,
  4295. constant int32_t & p0,
  4296. constant int32_t & p1,
  4297. constant int32_t & d0,
  4298. constant int32_t & d1,
  4299. constant int32_t & N,
  4300. constant int32_t & KH,
  4301. constant int32_t & KW,
  4302. uint3 tgpig[[threadgroup_position_in_grid]],
  4303. uint3 tgpg[[threadgroups_per_grid]], // tgpg[0] = D x IC x KH x KW, CHW = IC x KH x KW
  4304. uint3 tpitg[[thread_position_in_threadgroup]],
  4305. uint3 ntg[[threads_per_threadgroup]]) { // [M, 1, 1]
  4306. const int64_t KHW = KH * KW; // KHW == ntg[1] * ntg[2], KW == ntg[2]
  4307. const int64_t d = tgpig[0] / CHW;
  4308. const int64_t chw = tgpig[0] % CHW;
  4309. const int64_t tgpig_0 = chw / KHW; // 0 ~ (IC - 1)
  4310. const int64_t HW = tgpig[0] % KHW;
  4311. const int64_t tpitg_0 = (d * ntg[0]) + tpitg[0];
  4312. if (tpitg_0 >= N) {
  4313. return;
  4314. }
  4315. const int64_t tpitg_1 = HW / KW;
  4316. const int64_t tpitg_2 = HW % KW;
  4317. const int64_t iiw = tgpig[2] * s0 + tpitg_2 * d0 - p0;
  4318. const int64_t iih = tgpig[1] * s1 + tpitg_1 * d1 - p1;
  4319. const int64_t offset_dst =
  4320. (tpitg_0 * tgpg[1] * tgpg[2] + tgpig[1] * tgpg[2] + tgpig[2]) * CHW +
  4321. (tgpig_0 * KHW + tpitg_1 * KW + tpitg_2);
  4322. device T * pdst = (device T *) (dst);
  4323. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4324. pdst[offset_dst] = 0.0f;
  4325. } else {
  4326. const int64_t offset_src = tpitg_0 * ofs0 + tgpig_0 * ofs1;
  4327. pdst[offset_dst] = x[offset_src + iih * IW + iiw];
  4328. }
  4329. }
  4330. template [[host_name("kernel_im2col_ext_f32")]] kernel im2col_ext_t kernel_im2col_ext<float>;
  4331. template [[host_name("kernel_im2col_ext_f16")]] kernel im2col_ext_t kernel_im2col_ext<half>;
  4332. typedef void (conv_transpose_1d_t)(
  4333. device const float * src0,
  4334. device const float * src1,
  4335. device char * dst,
  4336. constant int32_t & IC,
  4337. constant int32_t & IL,
  4338. constant int32_t & K,
  4339. constant int32_t & s0,
  4340. constant uint64_t & nb0,
  4341. constant uint64_t & nb1,
  4342. uint3 tgpig[[threadgroup_position_in_grid]],
  4343. uint3 tgpg[[threadgroups_per_grid]]);
  4344. template <typename T>
  4345. kernel void kernel_conv_transpose_1d(
  4346. device const T * src0,
  4347. device const float * src1,
  4348. device char * dst,
  4349. constant int32_t & IC,
  4350. constant int32_t & IL,
  4351. constant int32_t & K,
  4352. constant int32_t & s0,
  4353. constant uint64_t & nb0,
  4354. constant uint64_t & nb1,
  4355. uint3 tgpig[[threadgroup_position_in_grid]],
  4356. uint3 tgpg[[threadgroups_per_grid]]) {
  4357. float v = 0.0f;
  4358. for (int64_t c = 0; c < IC; c++) {
  4359. const int32_t kernel_offset = c * tgpg[1] * K + K * tgpig[1];
  4360. const int32_t input_offset = c * IL;
  4361. for (int64_t i = 0; i < IL; i++) {
  4362. if (tgpig[0] >= i * s0 && tgpig[0] < i * s0 + K) {
  4363. v += src0[kernel_offset + tgpig[0] - i * s0] * src1[input_offset + i];
  4364. }
  4365. }
  4366. }
  4367. device float * dst_ptr = (device float *) (dst + tgpig[0] * nb0 + tgpig[1] * nb1);
  4368. dst_ptr[0] = v;
  4369. }
  4370. template [[host_name("kernel_conv_transpose_1d_f32_f32")]]
  4371. kernel void kernel_conv_transpose_1d<float>(
  4372. device const float * src0,
  4373. device const float * src1,
  4374. device char * dst,
  4375. constant int32_t & IC,
  4376. constant int32_t & IL,
  4377. constant int32_t & K,
  4378. constant int32_t & s0,
  4379. constant uint64_t & nb0,
  4380. constant uint64_t & nb1,
  4381. uint3 tgpig[[threadgroup_position_in_grid]],
  4382. uint3 tgpg[[threadgroups_per_grid]]);
  4383. template [[host_name("kernel_conv_transpose_1d_f16_f32")]]
  4384. kernel void kernel_conv_transpose_1d<half>(
  4385. device const half * src0,
  4386. device const float * src1,
  4387. device char * dst,
  4388. constant int32_t & IC,
  4389. constant int32_t & IL,
  4390. constant int32_t & K,
  4391. constant int32_t & s0,
  4392. constant uint64_t & nb0,
  4393. constant uint64_t & nb1,
  4394. uint3 tgpig[[threadgroup_position_in_grid]],
  4395. uint3 tgpg[[threadgroups_per_grid]]);
  4396. kernel void kernel_upscale_f32(
  4397. device const char * src0,
  4398. device char * dst,
  4399. constant int64_t & ne00,
  4400. constant int64_t & ne01,
  4401. constant int64_t & ne02,
  4402. constant int64_t & ne03,
  4403. constant uint64_t & nb00,
  4404. constant uint64_t & nb01,
  4405. constant uint64_t & nb02,
  4406. constant uint64_t & nb03,
  4407. constant int64_t & ne0,
  4408. constant int64_t & ne1,
  4409. constant int64_t & ne2,
  4410. constant int64_t & ne3,
  4411. constant uint64_t & nb0,
  4412. constant uint64_t & nb1,
  4413. constant uint64_t & nb2,
  4414. constant uint64_t & nb3,
  4415. constant float & sf0,
  4416. constant float & sf1,
  4417. constant float & sf2,
  4418. constant float & sf3,
  4419. uint3 tgpig[[threadgroup_position_in_grid]],
  4420. uint3 tpitg[[thread_position_in_threadgroup]],
  4421. uint3 ntg[[threads_per_threadgroup]]) {
  4422. const int64_t i3 = tgpig.z;
  4423. const int64_t i2 = tgpig.y;
  4424. const int64_t i1 = tgpig.x;
  4425. const int64_t i03 = i3/sf3;
  4426. const int64_t i02 = i2/sf2;
  4427. const int64_t i01 = i1/sf1;
  4428. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4429. const int64_t i00 = i0/sf0;
  4430. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  4431. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  4432. dst_ptr[0] = src0_ptr[0];
  4433. }
  4434. }
  4435. kernel void kernel_pad_f32(
  4436. device const char * src0,
  4437. device char * dst,
  4438. constant int64_t & ne00,
  4439. constant int64_t & ne01,
  4440. constant int64_t & ne02,
  4441. constant int64_t & ne03,
  4442. constant uint64_t & nb00,
  4443. constant uint64_t & nb01,
  4444. constant uint64_t & nb02,
  4445. constant uint64_t & nb03,
  4446. constant int64_t & ne0,
  4447. constant int64_t & ne1,
  4448. constant int64_t & ne2,
  4449. constant int64_t & ne3,
  4450. constant uint64_t & nb0,
  4451. constant uint64_t & nb1,
  4452. constant uint64_t & nb2,
  4453. constant uint64_t & nb3,
  4454. uint3 tgpig[[threadgroup_position_in_grid]],
  4455. uint3 tpitg[[thread_position_in_threadgroup]],
  4456. uint3 ntg[[threads_per_threadgroup]]) {
  4457. const int64_t i3 = tgpig.z;
  4458. const int64_t i2 = tgpig.y;
  4459. const int64_t i1 = tgpig.x;
  4460. const int64_t i03 = i3;
  4461. const int64_t i02 = i2;
  4462. const int64_t i01 = i1;
  4463. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  4464. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  4465. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  4466. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4467. if (i0 < ne00) {
  4468. dst_ptr[i0] = src0_ptr[i0];
  4469. } else {
  4470. dst_ptr[i0] = 0.0f;
  4471. }
  4472. }
  4473. return;
  4474. }
  4475. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4476. dst_ptr[i0] = 0.0f;
  4477. }
  4478. }
  4479. kernel void kernel_unpad_f32(
  4480. device const char * src0,
  4481. device char * dst,
  4482. constant int64_t & ne00,
  4483. constant int64_t & ne01,
  4484. constant int64_t & ne02,
  4485. constant int64_t & ne03,
  4486. constant uint64_t & nb00,
  4487. constant uint64_t & nb01,
  4488. constant uint64_t & nb02,
  4489. constant uint64_t & nb03,
  4490. constant int64_t & ne0,
  4491. constant int64_t & ne1,
  4492. constant int64_t & ne2,
  4493. constant int64_t & ne3,
  4494. constant uint64_t & nb0,
  4495. constant uint64_t & nb1,
  4496. constant uint64_t & nb2,
  4497. constant uint64_t & nb3,
  4498. uint3 tgpig[[threadgroup_position_in_grid]],
  4499. uint3 tpitg[[thread_position_in_threadgroup]],
  4500. uint3 ntg[[threads_per_threadgroup]]) {
  4501. const int64_t i3 = tgpig.z;
  4502. const int64_t i2 = tgpig.y;
  4503. const int64_t i1 = tgpig.x;
  4504. const int64_t i03 = i3;
  4505. const int64_t i02 = i2;
  4506. const int64_t i01 = i1;
  4507. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  4508. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  4509. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  4510. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4511. if (i0 < ne00) {
  4512. dst_ptr[i0] = src0_ptr[i0];
  4513. }
  4514. }
  4515. return;
  4516. }
  4517. }
  4518. kernel void kernel_arange_f32(
  4519. device char * dst,
  4520. constant int64_t & ne0,
  4521. constant float & start,
  4522. constant float & step,
  4523. uint3 tgpig[[threadgroup_position_in_grid]],
  4524. uint3 tpitg[[thread_position_in_threadgroup]],
  4525. uint3 ntg[[threads_per_threadgroup]]) {
  4526. device float * dst_ptr = (device float *) dst;
  4527. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4528. dst_ptr[i0] = start + step * i0;
  4529. }
  4530. }
  4531. kernel void kernel_timestep_embedding_f32(
  4532. device const char * src0,
  4533. device char * dst,
  4534. constant uint64_t & nb1,
  4535. constant int & dim,
  4536. constant int & max_period,
  4537. uint3 tgpig[[threadgroup_position_in_grid]],
  4538. uint3 tpitg[[thread_position_in_threadgroup]],
  4539. uint3 ntg[[threads_per_threadgroup]]) {
  4540. int i = tgpig.x;
  4541. device float * embed_data = (device float *)(dst + i*nb1);
  4542. int half_ = dim / 2;
  4543. for (int j = tpitg.x; j < half_; j += ntg.x) {
  4544. float timestep = ((device float *)src0)[i];
  4545. float freq = (float)exp(-log((float)max_period) * j / half_);
  4546. float arg = timestep * freq;
  4547. embed_data[j ] = cos(arg);
  4548. embed_data[j + half_] = sin(arg);
  4549. }
  4550. if (dim % 2 != 0 && tpitg.x == 0) {
  4551. embed_data[dim] = 0.f;
  4552. }
  4553. }
  4554. // bitonic sort implementation following the CUDA kernels as reference
  4555. typedef void (argsort_t)(
  4556. device const float * x,
  4557. device int32_t * dst,
  4558. constant int64_t & ncols,
  4559. constant int64_t & ncols_pad,
  4560. threadgroup int32_t * shared_values [[threadgroup(0)]],
  4561. uint3 tgpig[[threadgroup_position_in_grid]],
  4562. uint3 tpitg[[thread_position_in_threadgroup]]);
  4563. template<ggml_sort_order order>
  4564. kernel void kernel_argsort_f32_i32(
  4565. device const float * x,
  4566. device int32_t * dst,
  4567. constant int64_t & ncols,
  4568. constant int64_t & ncols_pad,
  4569. threadgroup int32_t * shared_values [[threadgroup(0)]],
  4570. uint3 tgpig[[threadgroup_position_in_grid]],
  4571. uint3 tpitg[[thread_position_in_threadgroup]]) {
  4572. // bitonic sort
  4573. int col = tpitg[0];
  4574. int row = tgpig[1];
  4575. if (col >= ncols_pad) return;
  4576. device const float * x_row = x + row * ncols;
  4577. threadgroup int32_t * dst_row = shared_values;
  4578. // initialize indices
  4579. dst_row[col] = col;
  4580. threadgroup_barrier(mem_flags::mem_threadgroup);
  4581. for (int k = 2; k <= ncols_pad; k *= 2) {
  4582. for (int j = k / 2; j > 0; j /= 2) {
  4583. int ixj = col ^ j;
  4584. if (ixj > col) {
  4585. if ((col & k) == 0) {
  4586. if (dst_row[col] >= ncols ||
  4587. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  4588. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  4589. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  4590. ) {
  4591. SWAP(dst_row[col], dst_row[ixj]);
  4592. }
  4593. } else {
  4594. if (dst_row[ixj] >= ncols ||
  4595. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  4596. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  4597. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  4598. ) {
  4599. SWAP(dst_row[col], dst_row[ixj]);
  4600. }
  4601. }
  4602. }
  4603. threadgroup_barrier(mem_flags::mem_threadgroup);
  4604. }
  4605. }
  4606. // copy the result to dst without the padding
  4607. if (col < ncols) {
  4608. dst[row * ncols + col] = dst_row[col];
  4609. }
  4610. }
  4611. template [[host_name("kernel_argsort_f32_i32_asc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_ASC>;
  4612. template [[host_name("kernel_argsort_f32_i32_desc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_DESC>;
  4613. kernel void kernel_leaky_relu_f32(
  4614. device const float * src0,
  4615. device float * dst,
  4616. constant float & slope,
  4617. uint tpig[[thread_position_in_grid]]) {
  4618. dst[tpig] = src0[tpig] > 0.0f ? src0[tpig] : src0[tpig] * slope;
  4619. }
  4620. // ref: https://arxiv.org/pdf/2307.08691.pdf
  4621. template<
  4622. typename q_t, // query types in shared memory
  4623. typename q4_t,
  4624. typename q8x8_t,
  4625. typename k_t, // key types in shared memory
  4626. typename k4x4_t,
  4627. typename k8x8_t,
  4628. typename v_t, // value types in shared memory
  4629. typename v4x4_t,
  4630. typename v8x8_t,
  4631. typename qk_t, // Q*K types
  4632. typename qk8x8_t,
  4633. typename s_t, // soft-max types
  4634. typename s8x8_t,
  4635. typename o_t, // attention accumulation types
  4636. typename o4_t,
  4637. typename o8x8_t,
  4638. typename kd4x4_t, // key type in device memory
  4639. short nl_k,
  4640. void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &),
  4641. typename vd4x4_t, // key type in device memory
  4642. short nl_v,
  4643. void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &),
  4644. short D, // head size
  4645. short Q = 8, // queries per threadgroup
  4646. short KV = 8, // key/value processed per each simdgroup
  4647. short C = 32> // cache items per threadgroup
  4648. kernel void kernel_flash_attn_ext(
  4649. constant ggml_metal_kargs_flash_attn_ext & args,
  4650. device const char * q,
  4651. device const char * k,
  4652. device const char * v,
  4653. device const char * mask,
  4654. device char * dst,
  4655. threadgroup half * shmem_f16 [[threadgroup(0)]],
  4656. uint3 tgpig[[threadgroup_position_in_grid]],
  4657. ushort3 ntg[[threads_per_threadgroup]],
  4658. ushort tiisg[[thread_index_in_simdgroup]],
  4659. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4660. const short nsg = ntg.y; // number of simdgroups
  4661. const int iq3 = tgpig[2];
  4662. const int iq2 = tgpig[1];
  4663. const int iq1 = tgpig[0]*Q;
  4664. const short D4 = D/4;
  4665. const short D8 = D/8;
  4666. const short D16 = D/16;
  4667. const short NW = N_SIMDWIDTH;
  4668. const short SH = (2*C + Q); // shared memory per simdgroup (s_t == float)
  4669. const short TS = nsg*SH; // shared memory size per query in (s_t == float)
  4670. const short T = D + 2*TS; // shared memory size per query in (half)
  4671. threadgroup q_t * sq = (threadgroup q_t *) (shmem_f16 + 0*D); // holds the query data
  4672. threadgroup q4_t * sq4 = (threadgroup q4_t *) (shmem_f16 + 0*D); // same as above but in q4_t
  4673. threadgroup o_t * so = (threadgroup o_t *) (shmem_f16 + 0*D); // reuse query data for accumulation
  4674. threadgroup o4_t * so4 = (threadgroup o4_t *) (shmem_f16 + 0*D); // same as above but in o4_t
  4675. threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + 2*sgitg*SH + Q*D); // scratch buffer for attention, mask and diagonal matrix
  4676. threadgroup k_t * sk = (threadgroup k_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // scratch buffer to load K in shared memory
  4677. threadgroup k4x4_t * sk4x4 = (threadgroup k4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // same as above but in k4x4_t
  4678. threadgroup v_t * sv = (threadgroup v_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // scratch buffer to load V in shared memory
  4679. threadgroup v4x4_t * sv4x4 = (threadgroup v4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // same as above but in v4x4_t
  4680. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  4681. o8x8_t lo[D8];
  4682. // load heads from Q to shared memory
  4683. for (short j = sgitg; j < Q; j += nsg) {
  4684. device const float4 * q4 = (device const float4 *) ((device const char *) q + ((iq1 + j)*args.nb01 + iq2*args.nb02 + iq3*args.nb03));
  4685. for (short i = tiisg; i < D4; i += NW) {
  4686. if (iq1 + j < args.ne01) {
  4687. sq4[j*D4 + i] = (q4_t) q4[i];
  4688. } else {
  4689. sq4[j*D4 + i] = (q4_t) 0.0f;
  4690. }
  4691. }
  4692. }
  4693. // zero out lo
  4694. for (short i = 0; i < D8; ++i) {
  4695. lo[i] = make_filled_simdgroup_matrix<o_t, 8>((o_t) 0.0f);
  4696. }
  4697. // zero out shared memory SH
  4698. for (short j = 0; j < Q; ++j) {
  4699. for (short i = tiisg; i < SH; i += NW) {
  4700. ss[j*TS + i] = 0.0f;
  4701. }
  4702. }
  4703. threadgroup_barrier(mem_flags::mem_threadgroup);
  4704. {
  4705. half S[Q] = { [0 ... Q-1] = 0.0f };
  4706. half M[Q] = { [0 ... Q-1] = -__FLT16_MAX__/2 };
  4707. // thread indices inside the simdgroup
  4708. // TODO: see if we can utilize quad-group functions for better performance
  4709. // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (6.9.3)
  4710. const short tx = tiisg%4;
  4711. const short ty = tiisg/4;
  4712. // broadcast kv
  4713. //const short rk2 = args.ne02/args.ne12;
  4714. //const short rk3 = args.ne03/args.ne13;
  4715. const short ikv2 = iq2/(args.ne02/args.ne_12_2);
  4716. const short ikv3 = iq3/(args.ne03/args.ne_12_3);
  4717. // load the queries from shared memory into local memory
  4718. q8x8_t mq[D8];
  4719. for (short i = 0; i < D8; ++i) {
  4720. simdgroup_load(mq[i], sq + i*8, D);
  4721. }
  4722. const bool has_mask = mask != q;
  4723. half slope = 1.0f;
  4724. // ALiBi
  4725. if (args.max_bias > 0.0f) {
  4726. const short h = iq2;
  4727. const half base = h < args.n_head_log2 ? args.m0 : args.m1;
  4728. const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1;
  4729. slope = pow(base, exph);
  4730. }
  4731. // loop over the KV cache
  4732. // each simdgroup handles blocks of Q rows and C columns
  4733. for (int ic0 = 0; ic0 < args.ne11; ic0 += C*nsg) {
  4734. const int ic = ic0 + C*sgitg;
  4735. if (ic >= args.ne11) {
  4736. break;
  4737. }
  4738. if (has_mask) {
  4739. // used to detect blocks full of -INF
  4740. half smax = -INFINITY;
  4741. // load the mask in shared memory
  4742. #pragma unroll(Q)
  4743. for (short j = 0; j < Q; ++j) {
  4744. device const half * pm = (device const half *) ((device const char *) mask + (iq1 + j)*args.nb31);
  4745. const half m = pm[ic + tiisg];
  4746. ss[j*TS + C + tiisg] = m;
  4747. smax = max(smax, m);
  4748. }
  4749. smax = simd_max(smax);
  4750. if (smax == -INFINITY) {
  4751. continue;
  4752. }
  4753. }
  4754. // Q*K^T
  4755. {
  4756. for (short cc = 0; cc < C/8; ++cc) {
  4757. qk8x8_t mqk = make_filled_simdgroup_matrix<qk_t, 8>((qk_t) 0.0f);
  4758. // this is compile-time check, so it does not have runtime overhead
  4759. if (is_same<kd4x4_t, k4x4_t>::value) {
  4760. // we can read directly from global memory
  4761. 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));
  4762. #pragma unroll(D8)
  4763. for (short i = 0; i < D8; ++i) {
  4764. k8x8_t mk;
  4765. simdgroup_load(mk, pk + i*8, args.nb_12_1/sizeof(k_t), 0, true); // transpose // TODO: use ne10
  4766. simdgroup_multiply_accumulate(mqk, mq[i], mk, mqk);
  4767. }
  4768. } else {
  4769. for (short ii = 0; ii < D16; ii += 4) {
  4770. 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));
  4771. if (D16%4 == 0) {
  4772. // the head is evenly divisible by 4*16 = 64, so no need for bound checks
  4773. {
  4774. k4x4_t tmp;
  4775. deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp);
  4776. sk4x4[4*ty + tx] = tmp;
  4777. }
  4778. simdgroup_barrier(mem_flags::mem_threadgroup);
  4779. #pragma unroll(4)
  4780. for (short k = 0; k < 4; ++k) {
  4781. k8x8_t mk;
  4782. simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose
  4783. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 0], mk, mqk);
  4784. simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose
  4785. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 1], mk, mqk);
  4786. }
  4787. } else {
  4788. if (ii + tx < D16) {
  4789. k4x4_t tmp;
  4790. deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp);
  4791. sk4x4[4*ty + tx] = tmp;
  4792. }
  4793. simdgroup_barrier(mem_flags::mem_threadgroup);
  4794. for (short k = 0; k < 4 && ii + k < D16; ++k) {
  4795. k8x8_t mk;
  4796. simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose
  4797. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 0], mk, mqk);
  4798. simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose
  4799. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 1], mk, mqk);
  4800. }
  4801. }
  4802. }
  4803. }
  4804. // cast qk_t -> s_t
  4805. //s8x8_t mqks(1.0f);
  4806. //simdgroup_multiply(mqks, mqk, mqks);
  4807. //simdgroup_store(mqks, ss + 8*cc, TS, 0, false);
  4808. simdgroup_store(mqk, ss + 8*cc, TS, 0, false);
  4809. }
  4810. }
  4811. // online softmax
  4812. {
  4813. for (ushort j = 0; j < Q; ++j) {
  4814. const half m = M[j];
  4815. // scale and apply the logitcap / mask
  4816. half s = ss[j*TS + tiisg]*args.scale;
  4817. if (args.logit_softcap != 0.0f) {
  4818. s = args.logit_softcap*precise::tanh(s);
  4819. }
  4820. // mqk = mqk + mask*slope
  4821. s += slope*ss[j*TS + C + tiisg];
  4822. M[j] = simd_max(max(M[j], s));
  4823. const half ms = exp(m - M[j]);
  4824. const half vs = exp(s - M[j]);
  4825. S[j] = S[j]*ms + simd_sum(vs);
  4826. // the P matrix from the paper (Q rows, C columns)
  4827. ss[j*TS + tiisg] = vs;
  4828. // create a QxQ diagonal matrix for rescaling the output
  4829. if (tiisg == j) {
  4830. ss[j*TS + 2*C + j] = ms;
  4831. }
  4832. }
  4833. }
  4834. // O = diag(ms)*O
  4835. {
  4836. s8x8_t mm;
  4837. simdgroup_load(mm, ss + 2*C, TS, 0, false);
  4838. #pragma unroll(D8)
  4839. for (short i = 0; i < D8; ++i) {
  4840. simdgroup_multiply(lo[i], mm, lo[i]);
  4841. }
  4842. }
  4843. // O = O + (Q*K^T)*V
  4844. {
  4845. for (short cc = 0; cc < C/8; ++cc) {
  4846. s8x8_t ms;
  4847. simdgroup_load(ms, ss + 8*cc, TS, 0, false);
  4848. if (is_same<vd4x4_t, v4x4_t>::value) {
  4849. // we can read directly from global memory
  4850. 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));
  4851. #pragma unroll(D8)
  4852. for (short i = 0; i < D8; ++i) {
  4853. v8x8_t mv;
  4854. simdgroup_load(mv, pv + i*8, args.nb_12_1/sizeof(v_t), 0, false); // TODO: use ne20
  4855. simdgroup_multiply_accumulate(lo[i], ms, mv, lo[i]);
  4856. }
  4857. } else {
  4858. for (short ii = 0; ii < D16; ii += 4) {
  4859. 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));
  4860. if (D16%4 == 0) {
  4861. // no need for bound checks
  4862. {
  4863. v4x4_t tmp;
  4864. deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp);
  4865. sv4x4[4*ty + tx] = tmp;
  4866. }
  4867. simdgroup_barrier(mem_flags::mem_threadgroup);
  4868. #pragma unroll(4)
  4869. for (short k = 0; k < 4; ++k) {
  4870. v8x8_t mv;
  4871. simdgroup_load(mv, sv + 16*k + 0*8, 4*16, 0, false);
  4872. simdgroup_multiply_accumulate(lo[2*(ii + k) + 0], ms, mv, lo[2*(ii + k) + 0]);
  4873. simdgroup_load(mv, sv + 16*k + 1*8, 4*16, 0, false);
  4874. simdgroup_multiply_accumulate(lo[2*(ii + k) + 1], ms, mv, lo[2*(ii + k) + 1]);
  4875. }
  4876. } else {
  4877. if (ii + tx < D16) {
  4878. v4x4_t tmp;
  4879. deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp);
  4880. sv4x4[4*ty + tx] = tmp;
  4881. }
  4882. simdgroup_barrier(mem_flags::mem_threadgroup);
  4883. for (short k = 0; k < 4 && ii + k < D16; ++k) {
  4884. v8x8_t mv;
  4885. simdgroup_load(mv, sv + 16*k + 0*8, 4*16, 0, false);
  4886. simdgroup_multiply_accumulate(lo[2*(ii + k) + 0], ms, mv, lo[2*(ii + k) + 0]);
  4887. simdgroup_load(mv, sv + 16*k + 1*8, 4*16, 0, false);
  4888. simdgroup_multiply_accumulate(lo[2*(ii + k) + 1], ms, mv, lo[2*(ii + k) + 1]);
  4889. }
  4890. }
  4891. }
  4892. }
  4893. }
  4894. }
  4895. }
  4896. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  4897. for (short j = 0; j < Q; ++j) {
  4898. if (tiisg == 0) {
  4899. ss[j*TS + 0] = S[j];
  4900. ss[j*TS + 1] = M[j];
  4901. }
  4902. }
  4903. }
  4904. // reduce the warps sequentially
  4905. for (ushort sg = 1; sg < nsg; ++sg) {
  4906. half S = { 0.0f };
  4907. half M = { -__FLT16_MAX__/2 };
  4908. threadgroup_barrier(mem_flags::mem_threadgroup);
  4909. // each simdgroup stores its output to shared memory, reusing sq
  4910. if (sgitg == sg) {
  4911. for (short i = 0; i < D8; ++i) {
  4912. simdgroup_store(lo[i], so + i*8, D, 0, false);
  4913. }
  4914. }
  4915. threadgroup_barrier(mem_flags::mem_threadgroup);
  4916. // the first simdgroup accumulates the results from the other simdgroups
  4917. if (sgitg == 0) {
  4918. for (short j = 0; j < Q; ++j) {
  4919. const half S0 = ss[j*TS + 0];
  4920. const half S1 = ss[j*TS + sg*SH + 0];
  4921. const half M0 = ss[j*TS + 1];
  4922. const half M1 = ss[j*TS + sg*SH + 1];
  4923. M = max(M0, M1);
  4924. const half ms0 = exp(M0 - M);
  4925. const half ms1 = exp(M1 - M);
  4926. S = S0*ms0 + S1*ms1;
  4927. if (tiisg == 0) {
  4928. ss[j*TS + 0] = S;
  4929. ss[j*TS + 1] = M;
  4930. ss[j*TS + 2*C + j ] = ms0;
  4931. ss[j*TS + 2*C + j + sg*SH] = ms1;
  4932. }
  4933. }
  4934. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  4935. {
  4936. s8x8_t ms0;
  4937. s8x8_t ms1;
  4938. simdgroup_load(ms0, ss + 2*C, TS, 0, false);
  4939. simdgroup_load(ms1, ss + 2*C + sg*SH, TS, 0, false);
  4940. #pragma unroll(D8)
  4941. for (short i = 0; i < D8; ++i) {
  4942. o8x8_t t;
  4943. simdgroup_load (t, so + i*8, D, 0, false);
  4944. simdgroup_multiply(t, ms1, t);
  4945. simdgroup_multiply_accumulate(lo[i], ms0, lo[i], t);
  4946. }
  4947. }
  4948. }
  4949. }
  4950. // store result to shared memory (reuse sq)
  4951. if (sgitg == 0) {
  4952. for (short i = 0; i < D8; ++i) {
  4953. simdgroup_store(lo[i], so + i*8, D, 0, false);
  4954. }
  4955. }
  4956. device float4 * dst4 = (device float4 *) dst;
  4957. // final rescale with 1/S and store to global memory
  4958. if (sgitg == 0) {
  4959. for (short j = 0; j < Q && iq1 + j < args.ne01; ++j) {
  4960. const float S = ss[j*TS + 0];
  4961. for (short i = tiisg; i < D4; i += NW) {
  4962. dst4[((uint64_t)iq3*args.ne2*args.ne1 + iq2 + (uint64_t)(iq1 + j)*args.ne1)*D4 + i] = (float4) so4[j*D4 + i]/S;
  4963. }
  4964. }
  4965. }
  4966. }
  4967. // TODO: this is quite ugly. in the future these types will be hardcoded in the kernel, but for now keep them as
  4968. // template to be able to explore different combinations
  4969. //
  4970. #define FA_TYPES \
  4971. half, half4, simdgroup_half8x8, \
  4972. half, half4x4, simdgroup_half8x8, \
  4973. half, half4x4, simdgroup_half8x8, \
  4974. float, simdgroup_float8x8, \
  4975. float, simdgroup_float8x8, \
  4976. half, half4, simdgroup_half8x8
  4977. typedef decltype(kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 64>) flash_attn_ext_t;
  4978. 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>;
  4979. 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>;
  4980. 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>;
  4981. 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>;
  4982. 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>;
  4983. 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>;
  4984. #if defined(GGML_METAL_USE_BF16)
  4985. 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>;
  4986. 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>;
  4987. 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>;
  4988. 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>;
  4989. 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>;
  4990. 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>;
  4991. #endif
  4992. 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>;
  4993. 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>;
  4994. 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>;
  4995. 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>;
  4996. 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>;
  4997. 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>;
  4998. 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>;
  4999. 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>;
  5000. 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>;
  5001. 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>;
  5002. 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>;
  5003. 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>;
  5004. 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>;
  5005. 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>;
  5006. 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>;
  5007. 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>;
  5008. 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>;
  5009. 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>;
  5010. 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>;
  5011. 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>;
  5012. 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>;
  5013. 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>;
  5014. 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>;
  5015. 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>;
  5016. 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>;
  5017. 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>;
  5018. 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>;
  5019. 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>;
  5020. 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>;
  5021. 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>;
  5022. #undef FA_TYPES
  5023. template<
  5024. typename q4_t, // query types in shared memory
  5025. typename q4x4_t,
  5026. typename k4x4_t, // key types in shared memory
  5027. typename v4x4_t, // value types in shared memory
  5028. typename qk_t, // Q*K types
  5029. typename s_t, // soft-max types
  5030. typename s4_t,
  5031. typename s4x4_t,
  5032. typename o4x4_t, // attention accumulation types
  5033. typename kd4x4_t, // key type in device memory
  5034. short nl_k,
  5035. void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &),
  5036. typename vd4x4_t, // key type in device memory
  5037. short nl_v,
  5038. void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &),
  5039. short D, // head size
  5040. short Q = 1, // queries per threadgroup
  5041. short C = 32> // cache items per threadgroup
  5042. kernel void kernel_flash_attn_ext_vec(
  5043. constant ggml_metal_kargs_flash_attn_ext & args,
  5044. device const char * q,
  5045. device const char * k,
  5046. device const char * v,
  5047. device const char * mask,
  5048. device char * dst,
  5049. threadgroup half * shmem_f16 [[threadgroup(0)]],
  5050. uint3 tgpig[[threadgroup_position_in_grid]],
  5051. ushort3 ntg[[threads_per_threadgroup]],
  5052. ushort tiisg[[thread_index_in_simdgroup]],
  5053. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5054. const short nsg = ntg.y; // number of simdgroups
  5055. const int iq3 = tgpig[2];
  5056. const int iq2 = tgpig[1];
  5057. const int iq1 = tgpig[0];
  5058. const short D4 = D/4;
  5059. const short D16 = D/16;
  5060. const short NW = N_SIMDWIDTH;
  5061. const short NL = NW/4; // note: this can be adjusted to support D%64 == 0 and D%32 == 0
  5062. const short SH = 2*C; // shared memory per simdgroup
  5063. const short T = D + nsg*SH; // shared memory size per query in (half)
  5064. //threadgroup q_t * sq = (threadgroup q_t *) (shmem_f16 + 0*D); // holds the query data
  5065. threadgroup q4_t * sq4 = (threadgroup q4_t *) (shmem_f16 + 0*D); // same as above but in q4_t
  5066. threadgroup q4x4_t * sq4x4 = (threadgroup q4x4_t *) (shmem_f16 + 0*D); // same as above but in q4x4_t
  5067. threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + sgitg*SH + Q*D); // scratch buffer for attention
  5068. threadgroup s4_t * ss4 = (threadgroup s4_t *) (shmem_f16 + sgitg*SH + Q*D); // same as above but in s4_t
  5069. threadgroup half * sm = (threadgroup half *) (shmem_f16 + sgitg*SH + C + Q*D); // scratch buffer for mask
  5070. threadgroup o4x4_t * sr4x4 = (threadgroup o4x4_t *) (shmem_f16 + sgitg*D + Q*T); // scratch buffer for the results
  5071. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  5072. o4x4_t lo[D16/NL];
  5073. // load heads from Q to shared memory
  5074. device const float4 * q4 = (device const float4 *) ((device const char *) q + (iq1*args.nb01 + iq2*args.nb02 + iq3*args.nb03));
  5075. for (short i = tiisg; i < D4; i += NW) {
  5076. if (iq1 < args.ne01) {
  5077. sq4[i] = (q4_t) q4[i];
  5078. } else {
  5079. sq4[i] = (q4_t) 0.0f;
  5080. }
  5081. }
  5082. // zero out lo
  5083. for (short i = 0; i < D16/NL; ++i) {
  5084. lo[i] = (o4x4_t) 0.0f;
  5085. }
  5086. // zero out shared memory SH
  5087. for (short i = tiisg; i < SH/4; i += NW) {
  5088. ss4[i] = (s4_t) 0.0f;
  5089. }
  5090. threadgroup_barrier(mem_flags::mem_threadgroup);
  5091. {
  5092. half S = 0.0f;
  5093. half M = -__FLT16_MAX__/2;
  5094. // thread indices inside the simdgroup
  5095. const short tx = tiisg%NL;
  5096. const short ty = tiisg/NL;
  5097. // broadcast kv
  5098. //const short rk2 = args.ne02/args.ne12;
  5099. //const short rk3 = args.ne03/args.ne13;
  5100. const short ikv2 = iq2/(args.ne02/args.ne_12_2);
  5101. const short ikv3 = iq3/(args.ne03/args.ne_12_3);
  5102. // load the queries from shared memory into local memory
  5103. q4x4_t mq[D16/NL];
  5104. #pragma unroll(D16/NL)
  5105. for (short ii = 0; ii < D16; ii += NL) {
  5106. mq[ii/NL] = sq4x4[ii + tx];
  5107. }
  5108. const bool has_mask = mask != q;
  5109. // pointer to the mask
  5110. device const half * pm = (device const half *) (mask + iq1*args.nb31);
  5111. half slope = 1.0f;
  5112. // ALiBi
  5113. if (args.max_bias > 0.0f) {
  5114. const short h = iq2;
  5115. const half base = h < args.n_head_log2 ? args.m0 : args.m1;
  5116. const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1;
  5117. slope = pow(base, exph);
  5118. }
  5119. // loop over the KV cache
  5120. // each simdgroup handles blocks of Q rows and C columns
  5121. for (int ic0 = 0; ic0 < args.ne11; ic0 += C*nsg) {
  5122. const int ic = ic0 + C*sgitg;
  5123. if (ic >= args.ne11) {
  5124. break;
  5125. }
  5126. if (has_mask) {
  5127. sm[tiisg] = pm[ic + tiisg];
  5128. }
  5129. // Q*K^T
  5130. {
  5131. // each simdgroup processes 1 query and 4 (NW/NL) keys
  5132. for (short cc = 0; cc < C/4; ++cc) {
  5133. qk_t mqka[4] = { 0.0, 0.0, 0.0, 0.0 };
  5134. 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));
  5135. #pragma unroll(D16/NL)
  5136. for (short ii = 0; ii < D16; ii += NL) {
  5137. const short i = ii + tx;
  5138. k4x4_t mk;
  5139. deq_k(pk + i/nl_k, i%nl_k, mk);
  5140. // note: this is less precise than the version below
  5141. //mqka[0] += dot(mq[ii/NL][0], mk[0]);
  5142. //mqka[1] += dot(mq[ii/NL][1], mk[1]);
  5143. //mqka[2] += dot(mq[ii/NL][2], mk[2]);
  5144. //mqka[3] += dot(mq[ii/NL][3], mk[3]);
  5145. mqka[0] += dot((float4) mq[ii/NL][0], (float4) mk[0]);
  5146. mqka[1] += dot((float4) mq[ii/NL][1], (float4) mk[1]);
  5147. mqka[2] += dot((float4) mq[ii/NL][2], (float4) mk[2]);
  5148. mqka[3] += dot((float4) mq[ii/NL][3], (float4) mk[3]);
  5149. }
  5150. qk_t mqk = mqka[0] + mqka[1] + mqka[2] + mqka[3];
  5151. // simdgroup reduce
  5152. // [ 0 .. 7] -> [ 0]
  5153. // [ 8 .. 15] -> [ 8]
  5154. // [16 .. 23] -> [16]
  5155. // [24 .. 31] -> [24]
  5156. //mqk += simd_shuffle_down(mqk, 16);
  5157. //mqk += simd_shuffle_down(mqk, 8);
  5158. mqk += simd_shuffle_down(mqk, 4);
  5159. mqk += simd_shuffle_down(mqk, 2);
  5160. mqk += simd_shuffle_down(mqk, 1);
  5161. // mqk = mqk*scale + mask*slope
  5162. if (tx == 0) {
  5163. mqk *= args.scale;
  5164. if (args.logit_softcap != 0.0f) {
  5165. mqk = args.logit_softcap*precise::tanh(mqk);
  5166. }
  5167. mqk += sm[4*cc + ty]*slope;
  5168. ss[4*cc + ty] = mqk;
  5169. }
  5170. }
  5171. }
  5172. simdgroup_barrier(mem_flags::mem_threadgroup);
  5173. // online softmax
  5174. {
  5175. const half m = M;
  5176. const half s = ss[tiisg];
  5177. M = simd_max(max(M, s));
  5178. const half ms = exp(m - M);
  5179. const half vs = exp(s - M);
  5180. S = S*ms + simd_sum(vs);
  5181. // the P matrix from the paper (Q rows, C columns)
  5182. ss[tiisg] = vs;
  5183. // O = diag(ms)*O
  5184. #pragma unroll(D16/NL)
  5185. for (short ii = 0; ii < D16; ii += NL) {
  5186. lo[ii/NL] *= ms;
  5187. }
  5188. }
  5189. simdgroup_barrier(mem_flags::mem_threadgroup);
  5190. // O = O + (Q*K^T)*V
  5191. {
  5192. for (short cc = 0; cc < C/4; ++cc) {
  5193. 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));
  5194. const s4x4_t ms(ss[4*cc + ty]);
  5195. #pragma unroll(D16/NL)
  5196. for (short ii = 0; ii < D16; ii += NL) {
  5197. const short i = ii + tx;
  5198. v4x4_t mv;
  5199. deq_v(pv4 + i/nl_v, i%nl_v, mv);
  5200. lo[ii/NL] += mv*ms;
  5201. }
  5202. }
  5203. }
  5204. }
  5205. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  5206. if (tiisg == 0) {
  5207. ss[0] = (s_t) S;
  5208. ss[1] = (s_t) M;
  5209. }
  5210. }
  5211. // simdgroup reduce
  5212. // [ 0, 8, 16, 24] -> [ 0]
  5213. // [ 1, 9, 17, 25] -> [ 1]
  5214. // [ 2, 10, 18, 26] -> [ 2]
  5215. // [ 3, 11, 19, 27] -> [ 3]
  5216. // [ 4, 12, 20, 28] -> [ 4]
  5217. // [ 5, 13, 21, 29] -> [ 5]
  5218. // [ 6, 14, 22, 30] -> [ 6]
  5219. // [ 7, 15, 23, 31] -> [ 7]
  5220. for (short ii = 0; ii < D16; ii += NL) {
  5221. lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 16);
  5222. lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 8);
  5223. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 4);
  5224. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 2);
  5225. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 1);
  5226. lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 16);
  5227. lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 8);
  5228. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 4);
  5229. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 2);
  5230. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 1);
  5231. lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 16);
  5232. lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 8);
  5233. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 4);
  5234. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 2);
  5235. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 1);
  5236. lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 16);
  5237. lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 8);
  5238. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 4);
  5239. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 2);
  5240. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 1);
  5241. }
  5242. threadgroup_barrier(mem_flags::mem_threadgroup);
  5243. // store results to shared memory
  5244. for (short i = tiisg; i < D16; i += NL) {
  5245. sr4x4[i] = lo[i/NL];
  5246. }
  5247. threadgroup_barrier(mem_flags::mem_threadgroup);
  5248. // parallel reduce
  5249. for (short r = nsg/2; r > 0; r >>= 1) {
  5250. if (sgitg < r) {
  5251. const half S0 = ss[ 0];
  5252. const half S1 = ss[r*SH + 0];
  5253. const half M0 = ss[ 1];
  5254. const half M1 = ss[r*SH + 1];
  5255. const half M = max(M0, M1);
  5256. const half ms0 = exp(M0 - M);
  5257. const half ms1 = exp(M1 - M);
  5258. const half S = S0*ms0 + S1*ms1;
  5259. if (tiisg == 0) {
  5260. ss[0] = S;
  5261. ss[1] = M;
  5262. }
  5263. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  5264. for (short i = tiisg; i < D16; i += NW) {
  5265. sr4x4[i] = sr4x4[i]*ms0 + sr4x4[i + r*D16]*ms1;
  5266. }
  5267. }
  5268. threadgroup_barrier(mem_flags::mem_threadgroup);
  5269. }
  5270. device float4x4 * dst44 = (device float4x4 *) dst;
  5271. // final rescale with 1/S and store to global memory
  5272. if (sgitg == 0) {
  5273. const float S = ss[0];
  5274. for (short i = tiisg; i < D16; i += NW) {
  5275. dst44[((uint64_t)iq3*args.ne2*args.ne1 + iq2 + (uint64_t)iq1*args.ne1)*D16 + i] = (float4x4) sr4x4[i]/S;
  5276. }
  5277. }
  5278. }
  5279. // note: I think the s_t can be half instead of float, because the Q*K scaling is done before storing to shared mem
  5280. // in the other (non-vec) kernel, we need s_t to also be float because we scale during the soft_max
  5281. //
  5282. #define FA_TYPES \
  5283. half4, half4x4, \
  5284. half4x4, \
  5285. half4x4, \
  5286. float, \
  5287. half, half4, half4x4, \
  5288. half4x4
  5289. typedef decltype(kernel_flash_attn_ext_vec<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 128>) flash_attn_ext_vec_t;
  5290. 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>;
  5291. #if defined(GGML_METAL_USE_BF16)
  5292. 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>;
  5293. #endif
  5294. 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>;
  5295. 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>;
  5296. 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>;
  5297. 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>;
  5298. 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>;
  5299. 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>;
  5300. #if defined(GGML_METAL_USE_BF16)
  5301. 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>;
  5302. #endif
  5303. 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>;
  5304. 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>;
  5305. 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>;
  5306. 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>;
  5307. 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>;
  5308. #undef FA_TYPES
  5309. template<typename T0, typename T1>
  5310. kernel void kernel_cpy(
  5311. constant ggml_metal_kargs_cpy & args,
  5312. device const char * src0,
  5313. device char * dst,
  5314. uint3 tgpig[[threadgroup_position_in_grid]],
  5315. ushort3 tpitg[[thread_position_in_threadgroup]],
  5316. ushort3 ntg[[threads_per_threadgroup]]) {
  5317. const int i03 = tgpig[2];
  5318. const int i02 = tgpig[1];
  5319. const int i01 = tgpig[0];
  5320. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5321. const int64_t i3 = n/(args.ne2*args.ne1*args.ne0);
  5322. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0)/(args.ne1*args.ne0);
  5323. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0)/args.ne0;
  5324. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0);
  5325. device T1 * dst_data = (device T1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5326. for (int64_t i00 = tpitg.x; i00 < args.ne00; i00 += ntg.x) {
  5327. device const T0 * src = (device T0 *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5328. dst_data[i00] = (T1) src[0];
  5329. }
  5330. }
  5331. typedef decltype(kernel_cpy<float, float>) kernel_cpy_t;
  5332. template [[host_name("kernel_cpy_f32_f32")]] kernel kernel_cpy_t kernel_cpy<float, float>;
  5333. template [[host_name("kernel_cpy_f32_f16")]] kernel kernel_cpy_t kernel_cpy<float, half>;
  5334. #if defined(GGML_METAL_USE_BF16)
  5335. template [[host_name("kernel_cpy_f32_bf16")]] kernel kernel_cpy_t kernel_cpy<float, bfloat>;
  5336. #endif
  5337. template [[host_name("kernel_cpy_f16_f32")]] kernel kernel_cpy_t kernel_cpy<half, float>;
  5338. template [[host_name("kernel_cpy_f16_f16")]] kernel kernel_cpy_t kernel_cpy<half, half>;
  5339. #if defined(GGML_METAL_USE_BF16)
  5340. template [[host_name("kernel_cpy_bf16_f32")]] kernel kernel_cpy_t kernel_cpy<bfloat, float>;
  5341. template [[host_name("kernel_cpy_bf16_bf16")]] kernel kernel_cpy_t kernel_cpy<bfloat, bfloat>;
  5342. #endif
  5343. kernel void kernel_cpy_f32_q8_0(
  5344. constant ggml_metal_kargs_cpy & args,
  5345. device const char * src0,
  5346. device char * dst,
  5347. uint3 tgpig[[threadgroup_position_in_grid]],
  5348. ushort3 tpitg[[thread_position_in_threadgroup]],
  5349. ushort3 ntg[[threads_per_threadgroup]]) {
  5350. const int i03 = tgpig[2];
  5351. const int i02 = tgpig[1];
  5352. const int i01 = tgpig[0];
  5353. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5354. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5355. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5356. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5357. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK8_0;
  5358. device block_q8_0 * dst_data = (device block_q8_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5359. for (int64_t i00 = tpitg.x*QK8_0; i00 < args.ne00; i00 += ntg.x*QK8_0) {
  5360. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5361. float amax = 0.0f; // absolute max
  5362. for (int j = 0; j < QK8_0; j++) {
  5363. const float v = src[j];
  5364. amax = MAX(amax, fabs(v));
  5365. }
  5366. const float d = amax / ((1 << 7) - 1);
  5367. const float id = d ? 1.0f/d : 0.0f;
  5368. dst_data[i00/QK8_0].d = d;
  5369. for (int j = 0; j < QK8_0; ++j) {
  5370. const float x0 = src[j]*id;
  5371. dst_data[i00/QK8_0].qs[j] = round(x0);
  5372. }
  5373. }
  5374. }
  5375. kernel void kernel_cpy_f32_q4_0(
  5376. constant ggml_metal_kargs_cpy & args,
  5377. device const char * src0,
  5378. device char * dst,
  5379. uint3 tgpig[[threadgroup_position_in_grid]],
  5380. ushort3 tpitg[[thread_position_in_threadgroup]],
  5381. ushort3 ntg[[threads_per_threadgroup]]) {
  5382. const int i03 = tgpig[2];
  5383. const int i02 = tgpig[1];
  5384. const int i01 = tgpig[0];
  5385. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5386. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5387. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5388. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5389. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_0;
  5390. device block_q4_0 * dst_data = (device block_q4_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5391. for (int64_t i00 = tpitg.x*QK4_0; i00 < args.ne00; i00 += ntg.x*QK4_0) {
  5392. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5393. float amax = 0.0f; // absolute max
  5394. float max = 0.0f;
  5395. for (int j = 0; j < QK4_0; j++) {
  5396. const float v = src[j];
  5397. if (amax < fabs(v)) {
  5398. amax = fabs(v);
  5399. max = v;
  5400. }
  5401. }
  5402. const float d = max / -8;
  5403. const float id = d ? 1.0f/d : 0.0f;
  5404. dst_data[i00/QK4_0].d = d;
  5405. for (int j = 0; j < QK4_0/2; ++j) {
  5406. const float x0 = src[0 + j]*id;
  5407. const float x1 = src[QK4_0/2 + j]*id;
  5408. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  5409. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  5410. dst_data[i00/QK4_0].qs[j] = xi0;
  5411. dst_data[i00/QK4_0].qs[j] |= xi1 << 4;
  5412. }
  5413. }
  5414. }
  5415. kernel void kernel_cpy_f32_q4_1(
  5416. constant ggml_metal_kargs_cpy & args,
  5417. device const char * src0,
  5418. device char * dst,
  5419. uint3 tgpig[[threadgroup_position_in_grid]],
  5420. ushort3 tpitg[[thread_position_in_threadgroup]],
  5421. ushort3 ntg[[threads_per_threadgroup]]) {
  5422. const int i03 = tgpig[2];
  5423. const int i02 = tgpig[1];
  5424. const int i01 = tgpig[0];
  5425. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5426. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5427. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5428. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5429. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_1;
  5430. device block_q4_1 * dst_data = (device block_q4_1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5431. for (int64_t i00 = tpitg.x*QK4_1; i00 < args.ne00; i00 += ntg.x*QK4_1) {
  5432. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5433. float min = FLT_MAX;
  5434. float max = -FLT_MAX;
  5435. for (int j = 0; j < QK4_1; j++) {
  5436. const float v = src[j];
  5437. if (min > v) min = v;
  5438. if (max < v) max = v;
  5439. }
  5440. const float d = (max - min) / ((1 << 4) - 1);
  5441. const float id = d ? 1.0f/d : 0.0f;
  5442. dst_data[i00/QK4_1].d = d;
  5443. dst_data[i00/QK4_1].m = min;
  5444. for (int j = 0; j < QK4_1/2; ++j) {
  5445. const float x0 = (src[0 + j] - min)*id;
  5446. const float x1 = (src[QK4_1/2 + j] - min)*id;
  5447. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  5448. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  5449. dst_data[i00/QK4_1].qs[j] = xi0;
  5450. dst_data[i00/QK4_1].qs[j] |= xi1 << 4;
  5451. }
  5452. }
  5453. }
  5454. kernel void kernel_cpy_f32_q5_0(
  5455. constant ggml_metal_kargs_cpy & args,
  5456. device const char * src0,
  5457. device char * dst,
  5458. uint3 tgpig[[threadgroup_position_in_grid]],
  5459. ushort3 tpitg[[thread_position_in_threadgroup]],
  5460. ushort3 ntg[[threads_per_threadgroup]]) {
  5461. const int i03 = tgpig[2];
  5462. const int i02 = tgpig[1];
  5463. const int i01 = tgpig[0];
  5464. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5465. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5466. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5467. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5468. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK5_0;
  5469. device block_q5_0 * dst_data = (device block_q5_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5470. for (int64_t i00 = tpitg.x*QK5_0; i00 < args.ne00; i00 += ntg.x*QK5_0) {
  5471. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5472. float amax = 0.0f; // absolute max
  5473. float max = 0.0f;
  5474. for (int j = 0; j < QK5_0; j++) {
  5475. const float v = src[j];
  5476. if (amax < fabs(v)) {
  5477. amax = fabs(v);
  5478. max = v;
  5479. }
  5480. }
  5481. const float d = max / -16;
  5482. const float id = d ? 1.0f/d : 0.0f;
  5483. dst_data[i00/QK5_0].d = d;
  5484. uint32_t qh = 0;
  5485. for (int j = 0; j < QK5_0/2; ++j) {
  5486. const float x0 = src[0 + j]*id;
  5487. const float x1 = src[QK5_0/2 + j]*id;
  5488. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  5489. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  5490. dst_data[i00/QK5_0].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  5491. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  5492. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  5493. }
  5494. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  5495. for (int j = 0; j < 4; ++j) {
  5496. dst_data[i00/QK5_0].qh[j] = qh8[j];
  5497. }
  5498. }
  5499. }
  5500. kernel void kernel_cpy_f32_q5_1(
  5501. constant ggml_metal_kargs_cpy & args,
  5502. device const char * src0,
  5503. device char * dst,
  5504. uint3 tgpig[[threadgroup_position_in_grid]],
  5505. ushort3 tpitg[[thread_position_in_threadgroup]],
  5506. ushort3 ntg[[threads_per_threadgroup]]) {
  5507. const int i03 = tgpig[2];
  5508. const int i02 = tgpig[1];
  5509. const int i01 = tgpig[0];
  5510. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5511. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5512. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5513. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5514. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK5_1;
  5515. device block_q5_1 * dst_data = (device block_q5_1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5516. for (int64_t i00 = tpitg.x*QK5_1; i00 < args.ne00; i00 += ntg.x*QK5_1) {
  5517. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5518. float max = src[0];
  5519. float min = src[0];
  5520. for (int j = 1; j < QK5_1; j++) {
  5521. const float v = src[j];
  5522. min = v < min ? v : min;
  5523. max = v > max ? v : max;
  5524. }
  5525. const float d = (max - min) / 31;
  5526. const float id = d ? 1.0f/d : 0.0f;
  5527. dst_data[i00/QK5_1].d = d;
  5528. dst_data[i00/QK5_1].m = min;
  5529. uint32_t qh = 0;
  5530. for (int j = 0; j < QK5_1/2; ++j) {
  5531. const float x0 = (src[0 + j] - min)*id;
  5532. const float x1 = (src[QK5_1/2 + j] - min)*id;
  5533. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  5534. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  5535. dst_data[i00/QK5_1].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  5536. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  5537. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2);
  5538. }
  5539. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  5540. for (int j = 0; j < 4; ++j) {
  5541. dst_data[i00/QK5_1].qh[j] = qh8[j];
  5542. }
  5543. }
  5544. }
  5545. static inline int best_index_int8(int n, constant float * val, float x) {
  5546. if (x <= val[0]) return 0;
  5547. if (x >= val[n-1]) return n-1;
  5548. int ml = 0, mu = n-1;
  5549. while (mu-ml > 1) {
  5550. int mav = (ml+mu)/2;
  5551. if (x < val[mav]) mu = mav; else ml = mav;
  5552. }
  5553. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  5554. }
  5555. kernel void kernel_cpy_f32_iq4_nl(
  5556. constant ggml_metal_kargs_cpy & args,
  5557. device const char * src0,
  5558. device char * dst,
  5559. uint3 tgpig[[threadgroup_position_in_grid]],
  5560. ushort3 tpitg[[thread_position_in_threadgroup]],
  5561. ushort3 ntg[[threads_per_threadgroup]]) {
  5562. const int i03 = tgpig[2];
  5563. const int i02 = tgpig[1];
  5564. const int i01 = tgpig[0];
  5565. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5566. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5567. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5568. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5569. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_NL;
  5570. device block_iq4_nl * dst_data = (device block_iq4_nl *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5571. for (int64_t i00 = tpitg.x*QK4_NL; i00 < args.ne00; i00 += ntg.x*QK4_NL) {
  5572. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5573. float amax = 0.0f; // absolute max
  5574. float max = 0.0f;
  5575. for (int j = 0; j < QK4_0; j++) {
  5576. const float v = src[j];
  5577. if (amax < fabs(v)) {
  5578. amax = fabs(v);
  5579. max = v;
  5580. }
  5581. }
  5582. const float d = max / kvalues_iq4nl_f[0];
  5583. const float id = d ? 1.0f/d : 0.0f;
  5584. float sumqx = 0, sumq2 = 0;
  5585. for (int j = 0; j < QK4_NL/2; ++j) {
  5586. const float x0 = src[0 + j]*id;
  5587. const float x1 = src[QK4_NL/2 + j]*id;
  5588. const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl_f, x0);
  5589. const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl_f, x1);
  5590. dst_data[i00/QK4_NL].qs[j] = xi0 | (xi1 << 4);
  5591. const float v0 = kvalues_iq4nl_f[xi0];
  5592. const float v1 = kvalues_iq4nl_f[xi1];
  5593. const float w0 = src[0 + j]*src[0 + j];
  5594. const float w1 = src[QK4_NL/2 + j]*src[QK4_NL/2 + j];
  5595. sumqx += w0*v0*src[j] + w1*v1*src[QK4_NL/2 + j];
  5596. sumq2 += w0*v0*v0 + w1*v1*v1;
  5597. }
  5598. dst_data[i00/QK4_NL].d = sumq2 > 0 ? sumqx/sumq2 : d;
  5599. }
  5600. }
  5601. kernel void kernel_concat(
  5602. constant ggml_metal_kargs_concat & args,
  5603. device const char * src0,
  5604. device const char * src1,
  5605. device char * dst,
  5606. uint3 tgpig[[threadgroup_position_in_grid]],
  5607. ushort3 tpitg[[thread_position_in_threadgroup]],
  5608. ushort3 ntg[[threads_per_threadgroup]]) {
  5609. const int i3 = tgpig.z;
  5610. const int i2 = tgpig.y;
  5611. const int i1 = tgpig.x;
  5612. int o[4] = {0, 0, 0, 0};
  5613. o[args.dim] = args.dim == 0 ? args.ne00 : (args.dim == 1 ? args.ne01 : (args.dim == 2 ? args.ne02 : args.ne03));
  5614. device const float * x;
  5615. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  5616. if (i0 < args.ne00 && i1 < args.ne01 && i2 < args.ne02 && i3 < args.ne03) {
  5617. x = (device const float *)(src0 + (i3 )*args.nb03 + (i2 )*args.nb02 + (i1 )*args.nb01 + (i0 )*args.nb00);
  5618. } else {
  5619. 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);
  5620. }
  5621. device float * y = (device float *)(dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5622. *y = *x;
  5623. }
  5624. }
  5625. template<typename args_t>
  5626. void kernel_mul_mv_q2_K_f32_impl(
  5627. args_t args,
  5628. device const char * src0,
  5629. device const char * src1,
  5630. device char * dst,
  5631. threadgroup char * shmem,
  5632. uint3 tgpig,
  5633. ushort tiisg,
  5634. ushort sgitg) {
  5635. const int nb = args.ne00/QK_K;
  5636. const int r0 = tgpig.x;
  5637. const int r1 = tgpig.y;
  5638. const int im = tgpig.z;
  5639. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  5640. const uint i12 = im%args.ne12;
  5641. const uint i13 = im/args.ne12;
  5642. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  5643. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  5644. device const block_q2_K * x = (device const block_q2_K *) (src0 + offset0);
  5645. device const float * y = (device const float *) (src1 + offset1);
  5646. float yl[32];
  5647. float sumf[N_DST]={0.f}, all_sum;
  5648. const int ix = tiisg/8; // 0...3
  5649. const int it = tiisg%8; // 0...7
  5650. const int iq = it/4; // 0 or 1
  5651. const int ir = it%4; // 0...3
  5652. const int is = (8*ir)/16;// 0 or 1
  5653. device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir;
  5654. for (int ib = ix; ib < nb; ib += 4) {
  5655. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  5656. for (int i = 0; i < 8; ++i) {
  5657. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  5658. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  5659. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  5660. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  5661. }
  5662. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is;
  5663. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  5664. device const half * dh = &x[ib].d;
  5665. for (int row = 0; row < N_DST; row++) {
  5666. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  5667. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  5668. for (int i = 0; i < 8; i += 2) {
  5669. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  5670. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  5671. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  5672. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  5673. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  5674. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  5675. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  5676. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  5677. }
  5678. float dall = dh[0];
  5679. float dmin = dh[1] * 1.f/16.f;
  5680. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  5681. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  5682. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  5683. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  5684. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  5685. qs += args.nb01/2;
  5686. sc += args.nb01;
  5687. dh += args.nb01/2;
  5688. }
  5689. y4 += 4 * QK_K;
  5690. }
  5691. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  5692. for (int row = 0; row < N_DST; ++row) {
  5693. all_sum = simd_sum(sumf[row]);
  5694. if (tiisg == 0) {
  5695. dst_f32[first_row + row] = all_sum;
  5696. }
  5697. }
  5698. }
  5699. [[host_name("kernel_mul_mv_q2_K_f32")]]
  5700. kernel void kernel_mul_mv_q2_K_f32(
  5701. constant ggml_metal_kargs_mul_mv & args,
  5702. device const char * src0,
  5703. device const char * src1,
  5704. device char * dst,
  5705. uint3 tgpig[[threadgroup_position_in_grid]],
  5706. ushort tiisg[[thread_index_in_simdgroup]],
  5707. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5708. kernel_mul_mv_q2_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  5709. }
  5710. template<typename args_t>
  5711. void kernel_mul_mv_q3_K_f32_impl(
  5712. args_t args,
  5713. device const char * src0,
  5714. device const char * src1,
  5715. device char * dst,
  5716. threadgroup char * shmem,
  5717. uint3 tgpig,
  5718. ushort tiisg,
  5719. ushort sgitg) {
  5720. const int nb = args.ne00/QK_K;
  5721. const int r0 = tgpig.x;
  5722. const int r1 = tgpig.y;
  5723. const int im = tgpig.z;
  5724. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  5725. const uint i12 = im%args.ne12;
  5726. const uint i13 = im/args.ne12;
  5727. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  5728. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  5729. device const block_q3_K * x = (device const block_q3_K *) (src0 + offset0);
  5730. device const float * yy = (device const float *) (src1 + offset1);
  5731. float yl[32];
  5732. //const uint16_t kmask1 = 0x3030;
  5733. //const uint16_t kmask2 = 0x0f0f;
  5734. const int tid = tiisg/4;
  5735. const int ix = tiisg%4;
  5736. const int ip = tid/4; // 0 or 1
  5737. const int il = 2*((tid%4)/2); // 0 or 2
  5738. const int ir = tid%2;
  5739. const int n = 8;
  5740. const int l0 = n*ir;
  5741. // One would think that the Metal compiler would figure out that ip and il can only have
  5742. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  5743. // with these two tales.
  5744. //
  5745. // Possible masks for the high bit
  5746. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  5747. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  5748. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  5749. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  5750. // Possible masks for the low 2 bits
  5751. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  5752. const ushort4 hm = mm[2*ip + il/2];
  5753. const short shift = 2*il;
  5754. const float v1 = il == 0 ? 4.f : 64.f;
  5755. const float v2 = 4.f * v1;
  5756. const uint16_t s_shift1 = 4*ip;
  5757. const uint16_t s_shift2 = s_shift1 + il;
  5758. const int q_offset = 32*ip + l0;
  5759. const int y_offset = 128*ip + 32*il + l0;
  5760. device const float * y1 = yy + ix*QK_K + y_offset;
  5761. uint32_t scales32, aux32;
  5762. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  5763. thread const int8_t * scales = (thread const int8_t *)&scales32;
  5764. float sumf1[2] = {0.f};
  5765. float sumf2[2] = {0.f};
  5766. for (int i = ix; i < nb; i += 4) {
  5767. for (int l = 0; l < 8; ++l) {
  5768. yl[l+ 0] = y1[l+ 0];
  5769. yl[l+ 8] = y1[l+16];
  5770. yl[l+16] = y1[l+32];
  5771. yl[l+24] = y1[l+48];
  5772. }
  5773. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  5774. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  5775. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  5776. device const half * dh = &x[i].d;
  5777. for (int row = 0; row < 2; ++row) {
  5778. const float d_all = (float)dh[0];
  5779. scales16[0] = a[4];
  5780. scales16[1] = a[5];
  5781. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  5782. scales16[0] = a[il+0];
  5783. scales16[1] = a[il+1];
  5784. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  5785. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  5786. for (int l = 0; l < n; l += 2) {
  5787. const int32_t qs = q[l/2];
  5788. s1 += yl[l+0] * (qs & qm[il/2][0]);
  5789. s2 += yl[l+1] * (qs & qm[il/2][1]);
  5790. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  5791. s4 += yl[l+16] * (qs & qm[il/2][2]);
  5792. s5 += yl[l+17] * (qs & qm[il/2][3]);
  5793. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  5794. }
  5795. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  5796. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  5797. sumf1[row] += d1 * (scales[0] - 32);
  5798. sumf2[row] += d2 * (scales[2] - 32);
  5799. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  5800. for (int l = 0; l < n; l += 2) {
  5801. const int32_t qs = q[l/2+8];
  5802. s1 += yl[l+8] * (qs & qm[il/2][0]);
  5803. s2 += yl[l+9] * (qs & qm[il/2][1]);
  5804. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  5805. s4 += yl[l+24] * (qs & qm[il/2][2]);
  5806. s5 += yl[l+25] * (qs & qm[il/2][3]);
  5807. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  5808. }
  5809. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  5810. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  5811. sumf1[row] += d1 * (scales[1] - 32);
  5812. sumf2[row] += d2 * (scales[3] - 32);
  5813. q += args.nb01/2;
  5814. h += args.nb01/2;
  5815. a += args.nb01/2;
  5816. dh += args.nb01/2;
  5817. }
  5818. y1 += 4 * QK_K;
  5819. }
  5820. for (int row = 0; row < 2; ++row) {
  5821. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  5822. sumf1[row] = simd_sum(sumf);
  5823. }
  5824. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  5825. if (tiisg == 0) {
  5826. for (int row = 0; row < 2; ++row) {
  5827. dst_f32[first_row + row] = sumf1[row];
  5828. }
  5829. }
  5830. }
  5831. [[host_name("kernel_mul_mv_q3_K_f32")]]
  5832. kernel void kernel_mul_mv_q3_K_f32(
  5833. constant ggml_metal_kargs_mul_mv & args,
  5834. device const char * src0,
  5835. device const char * src1,
  5836. device char * dst,
  5837. uint3 tgpig[[threadgroup_position_in_grid]],
  5838. ushort tiisg[[thread_index_in_simdgroup]],
  5839. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5840. kernel_mul_mv_q3_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  5841. }
  5842. template<typename args_t>
  5843. void kernel_mul_mv_q4_K_f32_impl(
  5844. args_t args,
  5845. device const char * src0,
  5846. device const char * src1,
  5847. device char * dst,
  5848. threadgroup char * shmem,
  5849. uint3 tgpig,
  5850. ushort tiisg,
  5851. ushort sgitg) {
  5852. const uint16_t kmask1 = 0x3f3f;
  5853. const uint16_t kmask2 = 0x0f0f;
  5854. const uint16_t kmask3 = 0xc0c0;
  5855. const int ix = tiisg/8; // 0...3
  5856. const int it = tiisg%8; // 0...7
  5857. const int iq = it/4; // 0 or 1
  5858. const int ir = it%4; // 0...3
  5859. const int nb = args.ne00/QK_K;
  5860. const int r0 = tgpig.x;
  5861. const int r1 = tgpig.y;
  5862. const int im = tgpig.z;
  5863. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  5864. const int first_row = r0 * N_DST;
  5865. const uint i12 = im%args.ne12;
  5866. const uint i13 = im/args.ne12;
  5867. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  5868. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  5869. device const block_q4_K * x = (device const block_q4_K *) (src0 + offset0);
  5870. device const float * y = (device const float *) (src1 + offset1);
  5871. float yl[16];
  5872. float yh[16];
  5873. float sumf[N_DST]={0.f}, all_sum;
  5874. device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
  5875. uint16_t sc16[4];
  5876. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  5877. for (int ib = ix; ib < nb; ib += 4) {
  5878. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  5879. for (int i = 0; i < 8; ++i) {
  5880. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  5881. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  5882. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  5883. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  5884. }
  5885. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq;
  5886. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  5887. device const half * dh = &x[ib].d;
  5888. for (int row = 0; row < N_DST; row++) {
  5889. sc16[0] = sc[0] & kmask1;
  5890. sc16[1] = sc[2] & kmask1;
  5891. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  5892. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  5893. device const uint16_t * q2 = q1 + 32;
  5894. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  5895. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  5896. for (int i = 0; i < 8; i += 2) {
  5897. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  5898. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  5899. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  5900. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  5901. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  5902. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  5903. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  5904. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  5905. }
  5906. float dall = dh[0];
  5907. float dmin = dh[1];
  5908. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  5909. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  5910. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  5911. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  5912. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  5913. q1 += args.nb01/2;
  5914. sc += args.nb01/2;
  5915. dh += args.nb01/2;
  5916. }
  5917. y4 += 4 * QK_K;
  5918. }
  5919. device float * dst_f32 = (device float *) dst + (int64_t)im*args.ne0*args.ne1 + (int64_t)r1*args.ne0;
  5920. for (int row = 0; row < N_DST; ++row) {
  5921. all_sum = simd_sum(sumf[row]);
  5922. if (tiisg == 0) {
  5923. dst_f32[first_row + row] = all_sum;
  5924. }
  5925. }
  5926. }
  5927. [[host_name("kernel_mul_mv_q4_K_f32")]]
  5928. kernel void kernel_mul_mv_q4_K_f32(
  5929. constant ggml_metal_kargs_mul_mv & args,
  5930. device const char * src0,
  5931. device const char * src1,
  5932. device char * dst,
  5933. uint3 tgpig[[threadgroup_position_in_grid]],
  5934. ushort tiisg[[thread_index_in_simdgroup]],
  5935. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5936. kernel_mul_mv_q4_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  5937. }
  5938. template<typename args_t>
  5939. void kernel_mul_mv_q5_K_f32_impl(
  5940. args_t args,
  5941. device const char * src0,
  5942. device const char * src1,
  5943. device char * dst,
  5944. threadgroup char * shmem,
  5945. uint3 tgpig,
  5946. ushort tiisg,
  5947. ushort sgitg) {
  5948. const int nb = args.ne00/QK_K;
  5949. const int r0 = tgpig.x;
  5950. const int r1 = tgpig.y;
  5951. const int im = tgpig.z;
  5952. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  5953. const uint i12 = im%args.ne12;
  5954. const uint i13 = im/args.ne12;
  5955. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  5956. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  5957. device const block_q5_K * x = (device const block_q5_K *) (src0 + offset0);
  5958. device const float * yy = (device const float *) (src1 + offset1);
  5959. float sumf[2]={0.f};
  5960. float yl[16], yh[16];
  5961. const uint16_t kmask1 = 0x3f3f;
  5962. const uint16_t kmask2 = 0x0f0f;
  5963. const uint16_t kmask3 = 0xc0c0;
  5964. const int tid = tiisg/4;
  5965. const int ix = tiisg%4;
  5966. const int iq = tid/4;
  5967. const int ir = tid%4;
  5968. const int n = 8;
  5969. const int l0 = n*ir;
  5970. const int q_offset = 32*iq + l0;
  5971. const int y_offset = 64*iq + l0;
  5972. const uint8_t hm1 = 1u << (2*iq);
  5973. const uint8_t hm2 = hm1 << 1;
  5974. const uint8_t hm3 = hm1 << 4;
  5975. const uint8_t hm4 = hm2 << 4;
  5976. uint16_t sc16[4];
  5977. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  5978. device const float * y1 = yy + ix*QK_K + y_offset;
  5979. for (int i = ix; i < nb; i += 4) {
  5980. device const uint8_t * q1 = x[i].qs + q_offset;
  5981. device const uint8_t * qh = x[i].qh + l0;
  5982. device const half * dh = &x[i].d;
  5983. device const uint16_t * a = (device const uint16_t *)x[i].scales + iq;
  5984. device const float * y2 = y1 + 128;
  5985. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  5986. for (int l = 0; l < 8; ++l) {
  5987. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  5988. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  5989. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  5990. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  5991. }
  5992. for (int row = 0; row < 2; ++row) {
  5993. device const uint8_t * q2 = q1 + 64;
  5994. sc16[0] = a[0] & kmask1;
  5995. sc16[1] = a[2] & kmask1;
  5996. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  5997. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  5998. float4 acc1 = {0.f};
  5999. float4 acc2 = {0.f};
  6000. for (int l = 0; l < n; ++l) {
  6001. uint8_t h = qh[l];
  6002. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  6003. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  6004. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  6005. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  6006. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  6007. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  6008. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  6009. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  6010. }
  6011. const float dall = dh[0];
  6012. const float dmin = dh[1];
  6013. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  6014. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  6015. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  6016. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  6017. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  6018. q1 += args.nb01;
  6019. qh += args.nb01;
  6020. dh += args.nb01/2;
  6021. a += args.nb01/2;
  6022. }
  6023. y1 += 4 * QK_K;
  6024. }
  6025. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6026. for (int row = 0; row < 2; ++row) {
  6027. const float tot = simd_sum(sumf[row]);
  6028. if (tiisg == 0) {
  6029. dst_f32[first_row + row] = tot;
  6030. }
  6031. }
  6032. }
  6033. [[host_name("kernel_mul_mv_q5_K_f32")]]
  6034. kernel void kernel_mul_mv_q5_K_f32(
  6035. constant ggml_metal_kargs_mul_mv & args,
  6036. device const char * src0,
  6037. device const char * src1,
  6038. device char * dst,
  6039. uint3 tgpig[[threadgroup_position_in_grid]],
  6040. ushort tiisg[[thread_index_in_simdgroup]],
  6041. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6042. kernel_mul_mv_q5_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6043. }
  6044. template <typename args_t>
  6045. void kernel_mul_mv_q6_K_f32_impl(
  6046. args_t args,
  6047. device const char * src0,
  6048. device const char * src1,
  6049. device char * dst,
  6050. threadgroup char * shmem,
  6051. uint3 tgpig,
  6052. ushort tiisg,
  6053. ushort sgitg) {
  6054. const uint8_t kmask1 = 0x03;
  6055. const uint8_t kmask2 = 0x0C;
  6056. const uint8_t kmask3 = 0x30;
  6057. const uint8_t kmask4 = 0xC0;
  6058. const int nb = args.ne00/QK_K;
  6059. const int r0 = tgpig.x;
  6060. const int r1 = tgpig.y;
  6061. const int im = tgpig.z;
  6062. const int row = 2*r0 + sgitg;
  6063. const uint i12 = im%args.ne12;
  6064. const uint i13 = im/args.ne12;
  6065. const uint64_t offset0 = row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6066. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6067. device const block_q6_K * x = (device const block_q6_K *) (src0 + offset0);
  6068. device const float * yy = (device const float *) (src1 + offset1);
  6069. float sumf = 0;
  6070. const int tid = tiisg/2;
  6071. const int ix = tiisg%2;
  6072. const int ip = tid/8; // 0 or 1
  6073. const int il = tid%8;
  6074. const int n = 4;
  6075. const int l0 = n*il;
  6076. const int is = 8*ip + l0/16;
  6077. const int y_offset = 128*ip + l0;
  6078. const int q_offset_l = 64*ip + l0;
  6079. const int q_offset_h = 32*ip + l0;
  6080. for (int i = ix; i < nb; i += 2) {
  6081. device const uint8_t * q1 = x[i].ql + q_offset_l;
  6082. device const uint8_t * q2 = q1 + 32;
  6083. device const uint8_t * qh = x[i].qh + q_offset_h;
  6084. device const int8_t * sc = x[i].scales + is;
  6085. device const float * y = yy + i * QK_K + y_offset;
  6086. const float dall = x[i].d;
  6087. float4 sums = {0.f, 0.f, 0.f, 0.f};
  6088. for (int l = 0; l < n; ++l) {
  6089. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  6090. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  6091. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  6092. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  6093. }
  6094. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  6095. }
  6096. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6097. const float tot = simd_sum(sumf);
  6098. if (tiisg == 0) {
  6099. dst_f32[row] = tot;
  6100. }
  6101. }
  6102. [[host_name("kernel_mul_mv_q6_K_f32")]]
  6103. kernel void kernel_mul_mv_q6_K_f32(
  6104. constant ggml_metal_kargs_mul_mv & args,
  6105. device const char * src0,
  6106. device const char * src1,
  6107. device char * dst,
  6108. uint3 tgpig[[threadgroup_position_in_grid]],
  6109. ushort tiisg[[thread_index_in_simdgroup]],
  6110. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6111. kernel_mul_mv_q6_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6112. }
  6113. // ======================= "True" 2-bit
  6114. template<typename args_t>
  6115. void kernel_mul_mv_iq2_xxs_f32_impl(
  6116. args_t args,
  6117. device const char * src0,
  6118. device const char * src1,
  6119. device char * dst,
  6120. threadgroup char * shmem,
  6121. uint3 tgpig,
  6122. ushort tiisg,
  6123. ushort sgitg) {
  6124. const int nb = args.ne00/QK_K;
  6125. const int r0 = tgpig.x;
  6126. const int r1 = tgpig.y;
  6127. const int im = tgpig.z;
  6128. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6129. const uint i12 = im%args.ne12;
  6130. const uint i13 = im/args.ne12;
  6131. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6132. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6133. device const block_iq2_xxs * x = (device const block_iq2_xxs *) (src0 + offset0);
  6134. device const float * y = (device const float *) (src1 + offset1);
  6135. float yl[32];
  6136. float sumf[N_DST]={0.f}, all_sum;
  6137. const int nb32 = nb * (QK_K / 32);
  6138. threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem);
  6139. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256);
  6140. {
  6141. int nval = 4;
  6142. int pos = (32*sgitg + tiisg)*nval;
  6143. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xxs_grid[pos + i];
  6144. nval = 2;
  6145. pos = (32*sgitg + tiisg)*nval;
  6146. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  6147. threadgroup_barrier(mem_flags::mem_threadgroup);
  6148. }
  6149. const int ix = tiisg;
  6150. device const float * y4 = y + 32 * ix;
  6151. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6152. for (int i = 0; i < 32; ++i) {
  6153. yl[i] = y4[i];
  6154. }
  6155. const int ibl = ib32 / (QK_K / 32);
  6156. const int ib = ib32 % (QK_K / 32);
  6157. device const block_iq2_xxs * xr = x + ibl;
  6158. device const uint16_t * q2 = xr->qs + 4 * ib;
  6159. device const half * dh = &xr->d;
  6160. for (int row = 0; row < N_DST; row++) {
  6161. const float db = dh[0];
  6162. device const uint8_t * aux8 = (device const uint8_t *)q2;
  6163. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  6164. const float d = db * (0.5f + (aux32 >> 28));
  6165. float sum = 0;
  6166. for (int l = 0; l < 4; ++l) {
  6167. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + aux8[l]);
  6168. const uint8_t signs = ssigns[(aux32 >> 7*l) & 127];
  6169. for (int j = 0; j < 8; ++j) {
  6170. sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  6171. }
  6172. }
  6173. sumf[row] += d * sum;
  6174. dh += args.nb01/2;
  6175. q2 += args.nb01/2;
  6176. }
  6177. y4 += 32 * 32;
  6178. }
  6179. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6180. for (int row = 0; row < N_DST; ++row) {
  6181. all_sum = simd_sum(sumf[row]);
  6182. if (tiisg == 0) {
  6183. dst_f32[first_row + row] = all_sum * 0.25f;
  6184. }
  6185. }
  6186. }
  6187. [[host_name("kernel_mul_mv_iq2_xxs_f32")]]
  6188. kernel void kernel_mul_mv_iq2_xxs_f32(
  6189. constant ggml_metal_kargs_mul_mv & args,
  6190. device const char * src0,
  6191. device const char * src1,
  6192. device char * dst,
  6193. threadgroup char * shmem [[threadgroup(0)]],
  6194. uint3 tgpig[[threadgroup_position_in_grid]],
  6195. ushort tiisg[[thread_index_in_simdgroup]],
  6196. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6197. kernel_mul_mv_iq2_xxs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6198. }
  6199. template<typename args_t>
  6200. void kernel_mul_mv_iq2_xs_f32_impl(
  6201. args_t args,
  6202. device const char * src0,
  6203. device const char * src1,
  6204. device char * dst,
  6205. threadgroup char * shmem,
  6206. uint3 tgpig,
  6207. ushort tiisg,
  6208. ushort sgitg) {
  6209. const int nb = args.ne00/QK_K;
  6210. const int r0 = tgpig.x;
  6211. const int r1 = tgpig.y;
  6212. const int im = tgpig.z;
  6213. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6214. const uint i12 = im%args.ne12;
  6215. const uint i13 = im/args.ne12;
  6216. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6217. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6218. device const block_iq2_xs * x = (device const block_iq2_xs *) (src0 + offset0);
  6219. device const float * y = (device const float *) (src1 + offset1);
  6220. float yl[32];
  6221. float sumf[N_DST]={0.f}, all_sum;
  6222. const int nb32 = nb * (QK_K / 32);
  6223. threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem);
  6224. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 512);
  6225. {
  6226. int nval = 8;
  6227. int pos = (32*sgitg + tiisg)*nval;
  6228. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xs_grid[pos + i];
  6229. nval = 2;
  6230. pos = (32*sgitg + tiisg)*nval;
  6231. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  6232. threadgroup_barrier(mem_flags::mem_threadgroup);
  6233. }
  6234. const int ix = tiisg;
  6235. device const float * y4 = y + 32 * ix;
  6236. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6237. for (int i = 0; i < 32; ++i) {
  6238. yl[i] = y4[i];
  6239. }
  6240. const int ibl = ib32 / (QK_K / 32);
  6241. const int ib = ib32 % (QK_K / 32);
  6242. device const block_iq2_xs * xr = x + ibl;
  6243. device const uint16_t * q2 = xr->qs + 4 * ib;
  6244. device const uint8_t * sc = xr->scales + ib;
  6245. device const half * dh = &xr->d;
  6246. for (int row = 0; row < N_DST; row++) {
  6247. const float db = dh[0];
  6248. const uint8_t ls1 = sc[0] & 0xf;
  6249. const uint8_t ls2 = sc[0] >> 4;
  6250. const float d1 = db * (0.5f + ls1);
  6251. const float d2 = db * (0.5f + ls2);
  6252. float sum1 = 0, sum2 = 0;
  6253. for (int l = 0; l < 2; ++l) {
  6254. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511));
  6255. const uint8_t signs = ssigns[(q2[l] >> 9)];
  6256. for (int j = 0; j < 8; ++j) {
  6257. sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  6258. }
  6259. }
  6260. for (int l = 2; l < 4; ++l) {
  6261. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511));
  6262. const uint8_t signs = ssigns[(q2[l] >> 9)];
  6263. for (int j = 0; j < 8; ++j) {
  6264. sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  6265. }
  6266. }
  6267. sumf[row] += d1 * sum1 + d2 * sum2;
  6268. dh += args.nb01/2;
  6269. q2 += args.nb01/2;
  6270. sc += args.nb01;
  6271. }
  6272. y4 += 32 * 32;
  6273. }
  6274. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6275. for (int row = 0; row < N_DST; ++row) {
  6276. all_sum = simd_sum(sumf[row]);
  6277. if (tiisg == 0) {
  6278. dst_f32[first_row + row] = all_sum * 0.25f;
  6279. }
  6280. }
  6281. }
  6282. [[host_name("kernel_mul_mv_iq2_xs_f32")]]
  6283. kernel void kernel_mul_mv_iq2_xs_f32(
  6284. constant ggml_metal_kargs_mul_mv & args,
  6285. device const char * src0,
  6286. device const char * src1,
  6287. device char * dst,
  6288. threadgroup char * shmem [[threadgroup(0)]],
  6289. uint3 tgpig[[threadgroup_position_in_grid]],
  6290. ushort tiisg[[thread_index_in_simdgroup]],
  6291. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6292. kernel_mul_mv_iq2_xs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6293. }
  6294. template <typename args_t>
  6295. void kernel_mul_mv_iq3_xxs_f32_impl(
  6296. args_t args,
  6297. device const char * src0,
  6298. device const char * src1,
  6299. device char * dst,
  6300. threadgroup char * shmem,
  6301. uint3 tgpig,
  6302. ushort tiisg,
  6303. ushort sgitg) {
  6304. const int nb = args.ne00/QK_K;
  6305. const int r0 = tgpig.x;
  6306. const int r1 = tgpig.y;
  6307. const int im = tgpig.z;
  6308. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6309. const uint i12 = im%args.ne12;
  6310. const uint i13 = im/args.ne12;
  6311. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6312. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6313. device const block_iq3_xxs * x = (device const block_iq3_xxs *) (src0 + offset0);
  6314. device const float * y = (device const float *) (src1 + offset1);
  6315. float yl[32];
  6316. float sumf[N_DST]={0.f}, all_sum;
  6317. const int nb32 = nb * (QK_K / 32);
  6318. threadgroup uint32_t * svalues = (threadgroup uint32_t *)(shmem);
  6319. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256);
  6320. {
  6321. int nval = 4;
  6322. int pos = (32*sgitg + tiisg)*nval;
  6323. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3xxs_grid[pos + i];
  6324. nval = 2;
  6325. pos = (32*sgitg + tiisg)*nval;
  6326. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  6327. threadgroup_barrier(mem_flags::mem_threadgroup);
  6328. }
  6329. const int ix = tiisg;
  6330. device const float * y4 = y + 32 * ix;
  6331. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6332. for (int i = 0; i < 32; ++i) {
  6333. yl[i] = y4[i];
  6334. }
  6335. const int ibl = ib32 / (QK_K / 32);
  6336. const int ib = ib32 % (QK_K / 32);
  6337. device const block_iq3_xxs * xr = x + ibl;
  6338. device const uint8_t * q3 = xr->qs + 8 * ib;
  6339. device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib;
  6340. device const half * dh = &xr->d;
  6341. for (int row = 0; row < N_DST; row++) {
  6342. const float db = dh[0];
  6343. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  6344. const float d = db * (0.5f + (aux32 >> 28));
  6345. float2 sum = {0};
  6346. for (int l = 0; l < 4; ++l) {
  6347. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + q3[2*l+0]);
  6348. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + q3[2*l+1]);
  6349. const uint8_t signs = ssigns[(aux32 >> 7*l) & 127];
  6350. for (int j = 0; j < 4; ++j) {
  6351. sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  6352. sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  6353. }
  6354. }
  6355. sumf[row] += d * (sum[0] + sum[1]);
  6356. dh += args.nb01/2;
  6357. q3 += args.nb01;
  6358. gas += args.nb01/2;
  6359. }
  6360. y4 += 32 * 32;
  6361. }
  6362. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6363. for (int row = 0; row < N_DST; ++row) {
  6364. all_sum = simd_sum(sumf[row]);
  6365. if (tiisg == 0) {
  6366. dst_f32[first_row + row] = all_sum * 0.5f;
  6367. }
  6368. }
  6369. }
  6370. [[host_name("kernel_mul_mv_iq3_xxs_f32")]]
  6371. kernel void kernel_mul_mv_iq3_xxs_f32(
  6372. constant ggml_metal_kargs_mul_mv & args,
  6373. device const char * src0,
  6374. device const char * src1,
  6375. device char * dst,
  6376. threadgroup char * shmem [[threadgroup(0)]],
  6377. uint3 tgpig[[threadgroup_position_in_grid]],
  6378. ushort tiisg[[thread_index_in_simdgroup]],
  6379. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6380. kernel_mul_mv_iq3_xxs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6381. }
  6382. template<typename args_t>
  6383. void kernel_mul_mv_iq3_s_f32_impl(
  6384. args_t args,
  6385. device const char * src0,
  6386. device const char * src1,
  6387. device char * dst,
  6388. threadgroup char * shmem,
  6389. uint3 tgpig,
  6390. ushort tiisg,
  6391. ushort sgitg) {
  6392. const int nb = args.ne00/QK_K;
  6393. const int r0 = tgpig.x;
  6394. const int r1 = tgpig.y;
  6395. const int im = tgpig.z;
  6396. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6397. const uint i12 = im%args.ne12;
  6398. const uint i13 = im/args.ne12;
  6399. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6400. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6401. device const block_iq3_s * x = (device const block_iq3_s *) (src0 + offset0);
  6402. device const float * y = (device const float *) (src1 + offset1);
  6403. float yl[32];
  6404. float sumf[N_DST]={0.f}, all_sum;
  6405. const int nb32 = nb * (QK_K / 32);
  6406. threadgroup uint32_t * svalues = (threadgroup uint32_t *) shmem;
  6407. {
  6408. int nval = 8;
  6409. int pos = (32*sgitg + tiisg)*nval;
  6410. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3s_grid[pos + i];
  6411. threadgroup_barrier(mem_flags::mem_threadgroup);
  6412. }
  6413. const int ix = tiisg;
  6414. device const float * y4 = y + 32 * ix;
  6415. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6416. for (int i = 0; i < 32; ++i) {
  6417. yl[i] = y4[i];
  6418. }
  6419. const int ibl = ib32 / (QK_K / 32);
  6420. const int ib = ib32 % (QK_K / 32);
  6421. device const block_iq3_s * xr = x + ibl;
  6422. device const uint8_t * qs = xr->qs + 8 * ib;
  6423. device const uint8_t * qh = xr->qh + ib;
  6424. device const uint8_t * sc = xr->scales + (ib/2);
  6425. device const uint8_t * signs = xr->signs + 4 * ib;
  6426. device const half * dh = &xr->d;
  6427. for (int row = 0; row < N_DST; row++) {
  6428. const float db = dh[0];
  6429. const float d = db * (1 + 2*((sc[0] >> 4*(ib%2)) & 0xf));
  6430. float2 sum = {0};
  6431. for (int l = 0; l < 4; ++l) {
  6432. const threadgroup uint32_t * table1 = qh[0] & kmask_iq2xs[2*l+0] ? svalues + 256 : svalues;
  6433. const threadgroup uint32_t * table2 = qh[0] & kmask_iq2xs[2*l+1] ? svalues + 256 : svalues;
  6434. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(table1 + qs[2*l+0]);
  6435. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(table2 + qs[2*l+1]);
  6436. for (int j = 0; j < 4; ++j) {
  6437. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]);
  6438. sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]);
  6439. }
  6440. }
  6441. sumf[row] += d * (sum[0] + sum[1]);
  6442. dh += args.nb01/2;
  6443. qs += args.nb01;
  6444. qh += args.nb01;
  6445. sc += args.nb01;
  6446. signs += args.nb01;
  6447. }
  6448. y4 += 32 * 32;
  6449. }
  6450. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6451. for (int row = 0; row < N_DST; ++row) {
  6452. all_sum = simd_sum(sumf[row]);
  6453. if (tiisg == 0) {
  6454. dst_f32[first_row + row] = all_sum;
  6455. }
  6456. }
  6457. }
  6458. [[host_name("kernel_mul_mv_iq3_s_f32")]]
  6459. kernel void kernel_mul_mv_iq3_s_f32(
  6460. constant ggml_metal_kargs_mul_mv & args,
  6461. device const char * src0,
  6462. device const char * src1,
  6463. device char * dst,
  6464. threadgroup char * shmem [[threadgroup(0)]],
  6465. uint3 tgpig[[threadgroup_position_in_grid]],
  6466. ushort tiisg[[thread_index_in_simdgroup]],
  6467. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6468. kernel_mul_mv_iq3_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6469. }
  6470. template <typename args_t>
  6471. void kernel_mul_mv_iq2_s_f32_impl(
  6472. args_t args,
  6473. device const char * src0,
  6474. device const char * src1,
  6475. device char * dst,
  6476. threadgroup char * shmem,
  6477. uint3 tgpig,
  6478. ushort tiisg,
  6479. ushort sgitg) {
  6480. const int nb = args.ne00/QK_K;
  6481. const int r0 = tgpig.x;
  6482. const int r1 = tgpig.y;
  6483. const int im = tgpig.z;
  6484. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6485. const uint i12 = im%args.ne12;
  6486. const uint i13 = im/args.ne12;
  6487. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6488. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6489. device const block_iq2_s * x = (device const block_iq2_s *) (src0 + offset0);
  6490. device const float * y = (device const float *) (src1 + offset1);
  6491. float yl[32];
  6492. float sumf[N_DST]={0.f}, all_sum;
  6493. const int nb32 = nb * (QK_K / 32);
  6494. //threadgroup uint64_t * svalues = (threadgroup uint64_t *) shmem;
  6495. //{
  6496. // int nval = 32;
  6497. // int pos = (32*sgitg + tiisg)*nval;
  6498. // for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2s_grid[pos + i];
  6499. // threadgroup_barrier(mem_flags::mem_threadgroup);
  6500. //}
  6501. const int ix = tiisg;
  6502. device const float * y4 = y + 32 * ix;
  6503. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6504. for (int i = 0; i < 32; ++i) {
  6505. yl[i] = y4[i];
  6506. }
  6507. const int ibl = ib32 / (QK_K / 32);
  6508. const int ib = ib32 % (QK_K / 32);
  6509. device const block_iq2_s * xr = x + ibl;
  6510. device const uint8_t * qs = xr->qs + 4 * ib;
  6511. device const uint8_t * qh = xr->qh + ib;
  6512. device const uint8_t * sc = xr->scales + ib;
  6513. device const uint8_t * signs = qs + QK_K/8;
  6514. device const half * dh = &xr->d;
  6515. for (int row = 0; row < N_DST; row++) {
  6516. const float db = dh[0];
  6517. const float d1 = db * (0.5f + (sc[0] & 0xf));
  6518. const float d2 = db * (0.5f + (sc[0] >> 4));
  6519. float2 sum = {0};
  6520. for (int l = 0; l < 2; ++l) {
  6521. //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  6522. //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  6523. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  6524. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  6525. for (int j = 0; j < 8; ++j) {
  6526. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]);
  6527. sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]);
  6528. }
  6529. }
  6530. sumf[row] += d1 * sum[0] + d2 * sum[1];
  6531. dh += args.nb01/2;
  6532. qs += args.nb01;
  6533. qh += args.nb01;
  6534. sc += args.nb01;
  6535. signs += args.nb01;
  6536. }
  6537. y4 += 32 * 32;
  6538. }
  6539. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6540. for (int row = 0; row < N_DST; ++row) {
  6541. all_sum = simd_sum(sumf[row]);
  6542. if (tiisg == 0) {
  6543. dst_f32[first_row + row] = all_sum * 0.25f;
  6544. }
  6545. }
  6546. }
  6547. [[host_name("kernel_mul_mv_iq2_s_f32")]]
  6548. kernel void kernel_mul_mv_iq2_s_f32(
  6549. constant ggml_metal_kargs_mul_mv & args,
  6550. device const char * src0,
  6551. device const char * src1,
  6552. device char * dst,
  6553. threadgroup char * shmem [[threadgroup(0)]],
  6554. uint3 tgpig[[threadgroup_position_in_grid]],
  6555. ushort tiisg[[thread_index_in_simdgroup]],
  6556. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6557. kernel_mul_mv_iq2_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6558. }
  6559. template<typename args_t>
  6560. void kernel_mul_mv_iq1_s_f32_impl(
  6561. args_t args,
  6562. device const char * src0,
  6563. device const char * src1,
  6564. device char * dst,
  6565. threadgroup char * shmem,
  6566. uint3 tgpig,
  6567. ushort tiisg,
  6568. ushort sgitg) {
  6569. const int nb = args.ne00/QK_K;
  6570. const int r0 = tgpig.x;
  6571. const int r1 = tgpig.y;
  6572. const int im = tgpig.z;
  6573. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6574. const uint i12 = im%args.ne12;
  6575. const uint i13 = im/args.ne12;
  6576. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6577. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6578. device const block_iq1_s * x = (device const block_iq1_s *) (src0 + offset0);
  6579. device const float * y = (device const float *) (src1 + offset1);
  6580. float yl[32];
  6581. float sumf[N_DST]={0.f}, all_sum;
  6582. const int nb32 = nb * (QK_K / 32);
  6583. const int ix = tiisg;
  6584. device const float * y4 = y + 32 * ix;
  6585. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6586. float sumy = 0;
  6587. for (int i = 0; i < 32; ++i) {
  6588. yl[i] = y4[i];
  6589. sumy += yl[i];
  6590. }
  6591. const int ibl = ib32 / (QK_K / 32);
  6592. const int ib = ib32 % (QK_K / 32);
  6593. device const block_iq1_s * xr = x + ibl;
  6594. device const uint8_t * qs = xr->qs + 4 * ib;
  6595. device const uint16_t * qh = xr->qh + ib;
  6596. device const half * dh = &xr->d;
  6597. for (int row = 0; row < N_DST; row++) {
  6598. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  6599. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 5) & 0x700)));
  6600. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[0] << 2) & 0x700)));
  6601. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[0] >> 1) & 0x700)));
  6602. float sum = 0;
  6603. for (int j = 0; j < 4; ++j) {
  6604. sum += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  6605. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4)
  6606. + yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  6607. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  6608. }
  6609. sumf[row] += (float)dh[0] * (sum + sumy * (qh[0] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA)) * (2*((qh[0] >> 12) & 7) + 1);
  6610. dh += args.nb01/2;
  6611. qs += args.nb01;
  6612. qh += args.nb01/2;
  6613. }
  6614. y4 += 32 * 32;
  6615. }
  6616. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6617. for (int row = 0; row < N_DST; ++row) {
  6618. all_sum = simd_sum(sumf[row]);
  6619. if (tiisg == 0) {
  6620. dst_f32[first_row + row] = all_sum;
  6621. }
  6622. }
  6623. }
  6624. template <typename args_t>
  6625. void kernel_mul_mv_iq1_m_f32_impl(
  6626. args_t args,
  6627. device const char * src0,
  6628. device const char * src1,
  6629. device char * dst,
  6630. threadgroup char * shmem,
  6631. uint3 tgpig,
  6632. ushort tiisg,
  6633. ushort sgitg) {
  6634. const int nb = args.ne00/QK_K;
  6635. const int r0 = tgpig.x;
  6636. const int r1 = tgpig.y;
  6637. const int im = tgpig.z;
  6638. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6639. const uint i12 = im%args.ne12;
  6640. const uint i13 = im/args.ne12;
  6641. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6642. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6643. device const block_iq1_m * x = (device const block_iq1_m *) (src0 + offset0);
  6644. device const float * y = (device const float *) (src1 + offset1);
  6645. float yl[32];
  6646. float sumf[N_DST]={0.f}, all_sum;
  6647. const int nb32 = nb * (QK_K / 32);
  6648. const int ix = tiisg;
  6649. device const float * y4 = y + 32 * ix;
  6650. iq1m_scale_t scale;
  6651. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6652. float4 sumy = {0.f};
  6653. for (int i = 0; i < 8; ++i) {
  6654. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  6655. yl[i+ 8] = y4[i+ 8]; sumy[1] += yl[i+ 8];
  6656. yl[i+16] = y4[i+16]; sumy[2] += yl[i+16];
  6657. yl[i+24] = y4[i+24]; sumy[3] += yl[i+24];
  6658. }
  6659. const int ibl = ib32 / (QK_K / 32);
  6660. const int ib = ib32 % (QK_K / 32);
  6661. device const block_iq1_m * xr = x + ibl;
  6662. device const uint8_t * qs = xr->qs + 4 * ib;
  6663. device const uint8_t * qh = xr->qh + 2 * ib;
  6664. device const uint16_t * sc = (device const uint16_t *)xr->scales;
  6665. for (int row = 0; row < N_DST; row++) {
  6666. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  6667. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  6668. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  6669. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[1] << 8) & 0x700)));
  6670. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[1] << 4) & 0x700)));
  6671. float2 sum = {0.f};
  6672. for (int j = 0; j < 4; ++j) {
  6673. sum[0] += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  6674. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4);
  6675. sum[1] += yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  6676. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  6677. }
  6678. 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);
  6679. 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);
  6680. sumf[row] += (float)scale.f16 * ((sum[0] + delta1) * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 7) + 1) +
  6681. (sum[1] + delta2) * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 7) + 1));
  6682. sc += args.nb01/2;
  6683. qs += args.nb01;
  6684. qh += args.nb01;
  6685. }
  6686. y4 += 32 * 32;
  6687. }
  6688. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6689. for (int row = 0; row < N_DST; ++row) {
  6690. all_sum = simd_sum(sumf[row]);
  6691. if (tiisg == 0) {
  6692. dst_f32[first_row + row] = all_sum;
  6693. }
  6694. }
  6695. }
  6696. template<typename args_t>
  6697. void kernel_mul_mv_iq4_nl_f32_impl(
  6698. args_t args,
  6699. device const char * src0,
  6700. device const char * src1,
  6701. device char * dst,
  6702. threadgroup char * shmem,
  6703. uint3 tgpig,
  6704. ushort tiisg,
  6705. ushort sgitg) {
  6706. threadgroup float * shmem_f32 = (threadgroup float *) shmem;
  6707. const int nb = args.ne00/QK4_NL;
  6708. const int r0 = tgpig.x;
  6709. const int r1 = tgpig.y;
  6710. const int im = tgpig.z;
  6711. const int first_row = (r0 * 2 + sgitg) * 2;
  6712. const uint i12 = im%args.ne12;
  6713. const uint i13 = im/args.ne12;
  6714. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6715. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6716. device const block_iq4_nl * x = (device const block_iq4_nl *) (src0 + offset0);
  6717. device const float * y = (device const float *) (src1 + offset1);
  6718. const int ix = tiisg/2; // 0...15
  6719. const int it = tiisg%2; // 0 or 1
  6720. shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16];
  6721. threadgroup_barrier(mem_flags::mem_threadgroup);
  6722. float4 yl[4];
  6723. float sumf[2]={0.f}, all_sum;
  6724. device const float * yb = y + ix * QK4_NL + it * 8;
  6725. uint32_t aux32[2];
  6726. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  6727. float4 qf1, qf2;
  6728. for (int ib = ix; ib < nb; ib += 16) {
  6729. device const float4 * y4 = (device const float4 *)yb;
  6730. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  6731. for (int row = 0; row < 2 && first_row + row < args.ne01; ++row) {
  6732. device const block_iq4_nl & xb = x[row*nb + ib];
  6733. device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it);
  6734. float4 acc1 = {0.f}, acc2 = {0.f};
  6735. aux32[0] = q4[0] | (q4[1] << 16);
  6736. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  6737. aux32[0] &= 0x0f0f0f0f;
  6738. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6739. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6740. acc1 += yl[0] * qf1;
  6741. acc2 += yl[1] * qf2;
  6742. aux32[0] = q4[2] | (q4[3] << 16);
  6743. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  6744. aux32[0] &= 0x0f0f0f0f;
  6745. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6746. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6747. acc1 += yl[2] * qf1;
  6748. acc2 += yl[3] * qf2;
  6749. acc1 += acc2;
  6750. sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  6751. }
  6752. yb += 16 * QK4_NL;
  6753. }
  6754. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6755. for (int row = 0; row < 2 && first_row + row < args.ne01; ++row) {
  6756. all_sum = simd_sum(sumf[row]);
  6757. if (tiisg == 0) {
  6758. dst_f32[first_row + row] = all_sum;
  6759. }
  6760. }
  6761. }
  6762. template<typename args_t>
  6763. void kernel_mul_mv_iq4_xs_f32_impl(
  6764. args_t args,
  6765. device const char * src0,
  6766. device const char * src1,
  6767. device char * dst,
  6768. threadgroup char * shmem,
  6769. uint3 tgpig,
  6770. ushort tiisg,
  6771. ushort sgitg) {
  6772. threadgroup float * shmem_f32 = (threadgroup float *) shmem;
  6773. const int nb = args.ne00/QK_K;
  6774. const int r0 = tgpig.x;
  6775. const int r1 = tgpig.y;
  6776. const int im = tgpig.z;
  6777. const int first_row = (r0 * 2 + sgitg) * 2;
  6778. const uint i12 = im%args.ne12;
  6779. const uint i13 = im/args.ne12;
  6780. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6781. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6782. device const block_iq4_xs * x = (device const block_iq4_xs *) (src0 + offset0);
  6783. device const float * y = (device const float *) (src1 + offset1);
  6784. const int ix = tiisg/16; // 0 or 1
  6785. const int it = tiisg%16; // 0...15
  6786. const int ib = it/2;
  6787. const int il = it%2;
  6788. shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16];
  6789. threadgroup_barrier(mem_flags::mem_threadgroup);
  6790. float4 yl[4];
  6791. float sumf[2]={0.f}, all_sum;
  6792. device const float * yb = y + ix * QK_K + ib * 32 + il * 8;
  6793. uint32_t aux32[2];
  6794. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  6795. float4 qf1, qf2;
  6796. for (int ibl = ix; ibl < nb; ibl += 2) {
  6797. device const float4 * y4 = (device const float4 *)yb;
  6798. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  6799. for (int row = 0; row < 2; ++row) {
  6800. device const block_iq4_xs & xb = x[row*nb + ibl];
  6801. device const uint32_t * q4 = (device const uint32_t *)(xb.qs + 16*ib + 8*il);
  6802. float4 acc1 = {0.f}, acc2 = {0.f};
  6803. aux32[0] = (q4[0] ) & 0x0f0f0f0f;
  6804. aux32[1] = (q4[0] >> 4) & 0x0f0f0f0f;
  6805. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6806. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6807. acc1 += yl[0] * qf1;
  6808. acc2 += yl[1] * qf2;
  6809. aux32[0] = (q4[1] ) & 0x0f0f0f0f;
  6810. aux32[1] = (q4[1] >> 4) & 0x0f0f0f0f;
  6811. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6812. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6813. acc1 += yl[2] * qf1;
  6814. acc2 += yl[3] * qf2;
  6815. acc1 += acc2;
  6816. const int ls = (((xb.scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((xb.scales_h >> 2*ib) & 3) << 4)) - 32;
  6817. sumf[row] += (float)xb.d * ls * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  6818. }
  6819. yb += 2 * QK_K;
  6820. }
  6821. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6822. for (int row = 0; row < 2; ++row) {
  6823. all_sum = simd_sum(sumf[row]);
  6824. if (tiisg == 0) {
  6825. dst_f32[first_row + row] = all_sum;
  6826. }
  6827. }
  6828. }
  6829. [[host_name("kernel_mul_mv_iq1_s_f32")]]
  6830. kernel void kernel_mul_mv_iq1_s_f32(
  6831. constant ggml_metal_kargs_mul_mv & args,
  6832. device const char * src0,
  6833. device const char * src1,
  6834. device char * dst,
  6835. uint3 tgpig[[threadgroup_position_in_grid]],
  6836. ushort tiisg[[thread_index_in_simdgroup]],
  6837. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6838. kernel_mul_mv_iq1_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6839. }
  6840. [[host_name("kernel_mul_mv_iq1_m_f32")]]
  6841. kernel void kernel_mul_mv_iq1_m_f32(
  6842. constant ggml_metal_kargs_mul_mv & args,
  6843. device const char * src0,
  6844. device const char * src1,
  6845. device char * dst,
  6846. uint3 tgpig[[threadgroup_position_in_grid]],
  6847. ushort tiisg[[thread_index_in_simdgroup]],
  6848. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6849. kernel_mul_mv_iq1_m_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6850. }
  6851. [[host_name("kernel_mul_mv_iq4_nl_f32")]]
  6852. kernel void kernel_mul_mv_iq4_nl_f32(
  6853. constant ggml_metal_kargs_mul_mv & args,
  6854. device const char * src0,
  6855. device const char * src1,
  6856. device char * dst,
  6857. threadgroup char * shmem [[threadgroup(0)]],
  6858. uint3 tgpig[[threadgroup_position_in_grid]],
  6859. ushort tiisg[[thread_index_in_simdgroup]],
  6860. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6861. kernel_mul_mv_iq4_nl_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6862. }
  6863. [[host_name("kernel_mul_mv_iq4_xs_f32")]]
  6864. kernel void kernel_mul_mv_iq4_xs_f32(
  6865. constant ggml_metal_kargs_mul_mv & args,
  6866. device const char * src0,
  6867. device const char * src1,
  6868. device char * dst,
  6869. threadgroup char * shmem [[threadgroup(0)]],
  6870. uint3 tgpig[[threadgroup_position_in_grid]],
  6871. ushort tiisg[[thread_index_in_simdgroup]],
  6872. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6873. kernel_mul_mv_iq4_xs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6874. }
  6875. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  6876. kernel void kernel_get_rows_q(
  6877. device const void * src0,
  6878. device const void * src1,
  6879. device float * dst,
  6880. constant int64_t & ne00,
  6881. constant uint64_t & nb01,
  6882. constant uint64_t & nb02,
  6883. constant int64_t & ne10,
  6884. constant uint64_t & nb10,
  6885. constant uint64_t & nb11,
  6886. constant uint64_t & nb1,
  6887. constant uint64_t & nb2,
  6888. uint3 tgpig[[threadgroup_position_in_grid]],
  6889. uint tiitg[[thread_index_in_threadgroup]],
  6890. uint3 tptg [[threads_per_threadgroup]]) {
  6891. const int64_t i10 = tgpig.x;
  6892. const int64_t i11 = tgpig.y;
  6893. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  6894. const int64_t i02 = i11;
  6895. for (int64_t ind = tiitg; ind < ne00/16; ind += tptg.x) {
  6896. float4x4 temp;
  6897. dequantize_func(((device const block_q *) ((const device char *) src0 + r*nb01 + i02*nb02)) + ind/nl, ind%nl, temp);
  6898. *(((device float4x4 *) ((device char *) dst + i11*nb2 + i10*nb1)) + ind) = temp;
  6899. }
  6900. }
  6901. template<typename T>
  6902. kernel void kernel_get_rows_f(
  6903. device const void * src0,
  6904. device const void * src1,
  6905. device float * dst,
  6906. constant int64_t & ne00,
  6907. constant uint64_t & nb01,
  6908. constant uint64_t & nb02,
  6909. constant int64_t & ne10,
  6910. constant uint64_t & nb10,
  6911. constant uint64_t & nb11,
  6912. constant uint64_t & nb1,
  6913. constant uint64_t & nb2,
  6914. uint3 tgpig[[threadgroup_position_in_grid]],
  6915. uint tiitg[[thread_index_in_threadgroup]],
  6916. uint3 tptg [[threads_per_threadgroup]]) {
  6917. const int64_t i10 = tgpig.x;
  6918. const int64_t i11 = tgpig.y;
  6919. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  6920. const int64_t i02 = i11;
  6921. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  6922. (( device float *) (( device char *) dst + i11*nb2 + i10*nb1))[ind] =
  6923. ((const device T *) ((const device char *) src0 + i02*nb02 + r*nb01))[ind];
  6924. }
  6925. }
  6926. kernel void kernel_get_rows_i32(
  6927. device const void * src0,
  6928. device const void * src1,
  6929. device int32_t * dst,
  6930. constant int64_t & ne00,
  6931. constant uint64_t & nb01,
  6932. constant uint64_t & nb02,
  6933. constant int64_t & ne10,
  6934. constant uint64_t & nb10,
  6935. constant uint64_t & nb11,
  6936. constant uint64_t & nb1,
  6937. constant uint64_t & nb2,
  6938. uint3 tgpig[[threadgroup_position_in_grid]],
  6939. uint tiitg[[thread_index_in_threadgroup]],
  6940. uint3 tptg [[threads_per_threadgroup]]) {
  6941. const int64_t i10 = tgpig.x;
  6942. const int64_t i11 = tgpig.y;
  6943. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  6944. const int64_t i02 = i11;
  6945. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  6946. (( device int32_t *) (( device char *) dst + i11*nb2 + i10*nb1))[ind] =
  6947. ((const device int32_t *) ((const device char *) src0 + i02*nb02 + r*nb01))[ind];
  6948. }
  6949. }
  6950. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  6951. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  6952. #define BLOCK_SIZE_K 32
  6953. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  6954. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  6955. #define THREAD_PER_BLOCK 128
  6956. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  6957. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  6958. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  6959. #define SG_MAT_ROW 8
  6960. // each block_q contains 16*nl weights
  6961. template<typename T, typename T4x4, typename simdgroup_T8x8, typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread T4x4 &)>
  6962. kernel void kernel_mul_mm(
  6963. constant ggml_metal_kargs_mul_mm & args,
  6964. device const char * src0,
  6965. device const char * src1,
  6966. device char * dst,
  6967. threadgroup char * shmem [[threadgroup(0)]],
  6968. uint3 tgpig[[threadgroup_position_in_grid]],
  6969. ushort tiitg[[thread_index_in_threadgroup]],
  6970. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6971. threadgroup T * sa = (threadgroup T *)(shmem);
  6972. threadgroup float * sb = (threadgroup float *)(shmem + 4096);
  6973. const int r0 = tgpig.y;
  6974. const int r1 = tgpig.x;
  6975. const int im = tgpig.z;
  6976. // if this block is of 64x32 shape or smaller
  6977. const short n_rows = (args.ne0 - r0*BLOCK_SIZE_M < BLOCK_SIZE_M) ? (args.ne0 - r0*BLOCK_SIZE_M) : BLOCK_SIZE_M;
  6978. const short n_cols = (args.ne1 - r1*BLOCK_SIZE_N < BLOCK_SIZE_N) ? (args.ne1 - r1*BLOCK_SIZE_N) : BLOCK_SIZE_N;
  6979. // a thread shouldn't load data outside of the matrix
  6980. const short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  6981. const short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  6982. simdgroup_T8x8 ma[4];
  6983. simdgroup_float8x8 mb[2];
  6984. simdgroup_float8x8 mc[8];
  6985. for (short i = 0; i < 8; i++){
  6986. mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  6987. }
  6988. short il = (tiitg % THREAD_PER_ROW);
  6989. const int i12 = im%args.ne12;
  6990. const int i13 = im/args.ne12;
  6991. const uint64_t offset0 = (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6992. const short offset1 = il/nl;
  6993. device const block_q * x = (device const block_q *)(src0
  6994. + args.nb01*(r0*BLOCK_SIZE_M + thread_row) + offset0) + offset1;
  6995. device const float * y = (device const float *)(src1
  6996. + args.nb13*i13
  6997. + args.nb12*i12
  6998. + args.nb11*(r1*BLOCK_SIZE_N + thread_col)
  6999. + args.nb10*(BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  7000. for (int loop_k = 0; loop_k < args.ne00; loop_k += BLOCK_SIZE_K) {
  7001. // load data and store to threadgroup memory
  7002. T4x4 temp_a;
  7003. dequantize_func(x, il, temp_a);
  7004. threadgroup_barrier(mem_flags::mem_threadgroup);
  7005. #pragma unroll(16)
  7006. for (short i = 0; i < 16; i++) {
  7007. *(sa + SG_MAT_SIZE * ((tiitg/THREAD_PER_ROW/8) \
  7008. + (tiitg%THREAD_PER_ROW)*16 + (i/8)*8) \
  7009. + (tiitg/THREAD_PER_ROW)%8 + (i&7)*8) = temp_a[i/4][i%4];
  7010. }
  7011. *(threadgroup float2x4 *)(sb + 32*8*(tiitg%THREAD_PER_COL) + 8*(tiitg/THREAD_PER_COL)) = *((device float2x4 *) y);
  7012. il = (il + 2 < nl) ? il + 2 : il % 2;
  7013. x = (il < 2) ? x + (2 + nl - 1)/nl : x;
  7014. y += BLOCK_SIZE_K;
  7015. threadgroup_barrier(mem_flags::mem_threadgroup);
  7016. // load matrices from threadgroup memory and conduct outer products
  7017. threadgroup const T * lsma = (sa + THREAD_MAT_M*SG_MAT_SIZE*(sgitg%2));
  7018. threadgroup const float * lsmb = (sb + THREAD_MAT_N*SG_MAT_SIZE*(sgitg/2));
  7019. #pragma unroll(4)
  7020. for (short ik = 0; ik < BLOCK_SIZE_K/8; ik++) {
  7021. #pragma unroll(4)
  7022. for (short i = 0; i < 4; i++) {
  7023. simdgroup_load(ma[i], lsma + SG_MAT_SIZE * i);
  7024. }
  7025. simdgroup_barrier(mem_flags::mem_none);
  7026. #pragma unroll(2)
  7027. for (short i = 0; i < 2; i++) {
  7028. simdgroup_load(mb[i], lsmb + SG_MAT_SIZE * i);
  7029. }
  7030. #pragma unroll(8)
  7031. for (short i = 0; i < 8; i++){
  7032. simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
  7033. }
  7034. lsma += (BLOCK_SIZE_M/SG_MAT_ROW)*SG_MAT_SIZE;
  7035. lsmb += (BLOCK_SIZE_N/SG_MAT_ROW)*SG_MAT_SIZE;
  7036. }
  7037. }
  7038. if ((r0 + 1) * BLOCK_SIZE_M <= args.ne0 && (r1 + 1) * BLOCK_SIZE_N <= args.ne1) {
  7039. device float * C = (device float *) dst +
  7040. (BLOCK_SIZE_M * r0 + 32*(sgitg & 1)) + \
  7041. (BLOCK_SIZE_N * r1 + 16*(sgitg >> 1)) * args.ne0 + im*args.ne1*args.ne0;
  7042. for (short i = 0; i < 8; i++) {
  7043. simdgroup_store(mc[i], C + 8 * (i%4) + 8 * args.ne0 * (i/4), args.ne0);
  7044. }
  7045. } else {
  7046. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  7047. threadgroup_barrier(mem_flags::mem_threadgroup);
  7048. threadgroup float * temp_str = ((threadgroup float *) shmem) \
  7049. + 32*(sgitg&1) + (16*(sgitg >> 1))*BLOCK_SIZE_M;
  7050. for (short i = 0; i < 8; i++) {
  7051. simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*BLOCK_SIZE_M*(i/4), BLOCK_SIZE_M);
  7052. }
  7053. threadgroup_barrier(mem_flags::mem_threadgroup);
  7054. if (sgitg == 0) {
  7055. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  7056. device float * D = (device float *) dst + (r0*BLOCK_SIZE_M) + (r1*BLOCK_SIZE_N + j)*args.ne0 + im*args.ne1*args.ne0;
  7057. device float4 * D4 = (device float4 *) D;
  7058. threadgroup float * C = temp_str + (j*BLOCK_SIZE_M);
  7059. threadgroup float4 * C4 = (threadgroup float4 *) C;
  7060. int i = 0;
  7061. for (; i < n_rows/4; i++) {
  7062. *(D4 + i) = *(C4 + i);
  7063. }
  7064. i *= 4;
  7065. for (; i < n_rows; i++) {
  7066. *(D + i) = *(C + i);
  7067. }
  7068. }
  7069. }
  7070. }
  7071. }
  7072. // same as kernel_mul_mm_impl, but src1 and dst are accessed via indices stored in rowids
  7073. // TODO: this kernel needs to be reimplemented from scratch for better performance
  7074. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  7075. void kernel_mul_mm_id_impl(
  7076. int32_t ne00,
  7077. int32_t ne02,
  7078. uint64_t nb01,
  7079. uint64_t nb02,
  7080. int32_t ne11,
  7081. int32_t ne12,
  7082. uint64_t nb10,
  7083. uint64_t nb11,
  7084. uint64_t nb12,
  7085. int32_t ne0,
  7086. int32_t ne1,
  7087. int64_t ne0ne1,
  7088. device const char * src0,
  7089. device const char * src1,
  7090. threadgroup ushort2 * rowids,
  7091. device char * dst,
  7092. threadgroup char * shmem,
  7093. uint3 tgpig[[threadgroup_position_in_grid]],
  7094. ushort tiitg[[thread_index_in_threadgroup]],
  7095. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  7096. threadgroup half * sa = (threadgroup half *)(shmem);
  7097. threadgroup float * sb = (threadgroup float *)(shmem + 4096);
  7098. const int r0 = tgpig.y;
  7099. const int r1 = tgpig.x;
  7100. if (r1*BLOCK_SIZE_N >= ne1) return;
  7101. // if this block is of 64x32 shape or smaller
  7102. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  7103. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  7104. // a thread shouldn't load data outside of the matrix
  7105. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  7106. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  7107. simdgroup_half8x8 ma[4];
  7108. simdgroup_float8x8 mb[2];
  7109. simdgroup_float8x8 mc[8];
  7110. for (int i = 0; i < 8; i++){
  7111. mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  7112. }
  7113. short il = (tiitg % THREAD_PER_ROW);
  7114. ushort offset1 = il/nl;
  7115. threadgroup const auto & id = rowids[r1 * BLOCK_SIZE_N + thread_col];
  7116. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01) + offset1;
  7117. device const float * y = (device const float *)(src1
  7118. + nb12 * id[1]
  7119. + nb11 * (id[0] % ne11)
  7120. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  7121. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  7122. // load data and store to threadgroup memory
  7123. half4x4 temp_a;
  7124. dequantize_func(x, il, temp_a);
  7125. threadgroup_barrier(mem_flags::mem_threadgroup);
  7126. for (int i = 0; i < 16; i++) {
  7127. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  7128. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  7129. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  7130. }
  7131. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  7132. il = (il + 2 < nl) ? il + 2 : il % 2;
  7133. x = (il < 2) ? x + (2+nl-1)/nl : x;
  7134. y += BLOCK_SIZE_K;
  7135. threadgroup_barrier(mem_flags::mem_threadgroup);
  7136. // load matrices from threadgroup memory and conduct outer products
  7137. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  7138. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  7139. #pragma unroll(BLOCK_SIZE_K/8)
  7140. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  7141. #pragma unroll(4)
  7142. for (int i = 0; i < 4; i++) {
  7143. simdgroup_load(ma[i], lsma + SG_MAT_SIZE * i);
  7144. }
  7145. simdgroup_barrier(mem_flags::mem_none);
  7146. #pragma unroll(2)
  7147. for (int i = 0; i < 2; i++) {
  7148. simdgroup_load(mb[i], lsmb + SG_MAT_SIZE * i);
  7149. }
  7150. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  7151. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  7152. #pragma unroll(8)
  7153. for (int i = 0; i < 8; i++){
  7154. simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
  7155. }
  7156. }
  7157. }
  7158. {
  7159. threadgroup_barrier(mem_flags::mem_threadgroup);
  7160. threadgroup float * temp_str = ((threadgroup float *) shmem) \
  7161. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  7162. for (int i = 0; i < 8; i++) {
  7163. simdgroup_store(mc[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  7164. }
  7165. threadgroup_barrier(mem_flags::mem_threadgroup);
  7166. if (sgitg == 0) {
  7167. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  7168. threadgroup const auto & jid = rowids[r1 * BLOCK_SIZE_N + j];
  7169. int64_t joff = jid[0]*ne0 + jid[1]*ne0ne1;
  7170. device float * D = (device float *) dst + (r0*BLOCK_SIZE_M) + joff;
  7171. device float4 * D4 = (device float4 *) D;
  7172. threadgroup float * C = temp_str + (j*BLOCK_SIZE_M);
  7173. threadgroup float4 * C4 = (threadgroup float4 *) C;
  7174. int i = 0;
  7175. for (; i < n_rows/4; i++) {
  7176. *(D4 + i) = *(C4 + i);
  7177. }
  7178. i *= 4;
  7179. for (; i < n_rows; i++) {
  7180. *(D + i) = *(C + i);
  7181. }
  7182. }
  7183. }
  7184. }
  7185. }
  7186. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  7187. kernel void kernel_mul_mm_id(
  7188. constant ggml_metal_kargs_mul_mm_id & args,
  7189. device const char * src0s,
  7190. device const char * src1,
  7191. device char * dst,
  7192. device const char * ids,
  7193. threadgroup char * shmem [[threadgroup(0)]],
  7194. uint3 tgpig[[threadgroup_position_in_grid]],
  7195. ushort tiitg[[thread_index_in_threadgroup]],
  7196. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  7197. const int32_t i02 = tgpig.z;
  7198. tgpig.z = 0;
  7199. device const char * src0 = src0s + i02*args.nb02;
  7200. // row indices
  7201. threadgroup ushort2 * rowids = (threadgroup ushort2 *)(shmem + 8192);
  7202. // TODO: parallelize this loop
  7203. int32_t _ne1 = 0;
  7204. for (ushort ii1 = 0; ii1 < args.nei1; ii1++) {
  7205. for (ushort ii0 = 0; ii0 < args.nei0; ii0++) {
  7206. int32_t id = ((device int32_t *) (ids + ii1*args.nbi1))[ii0];
  7207. if (id == i02) {
  7208. if (tiitg == 0) {
  7209. rowids[_ne1] = ushort2(ii0, ii1);
  7210. }
  7211. _ne1++;
  7212. }
  7213. }
  7214. }
  7215. threadgroup_barrier(mem_flags::mem_threadgroup);
  7216. kernel_mul_mm_id_impl<block_q, nl, dequantize_func>(
  7217. args.ne00,
  7218. args.ne02,
  7219. args.nb01,
  7220. args.nb02,
  7221. args.ne11,
  7222. args.ne12,
  7223. args.nb10,
  7224. args.nb11,
  7225. args.nb12,
  7226. args.ne0,
  7227. _ne1,
  7228. (int64_t)args.ne0*args.ne1,
  7229. src0,
  7230. src1,
  7231. rowids,
  7232. dst,
  7233. shmem,
  7234. tgpig,
  7235. tiitg,
  7236. sgitg);
  7237. }
  7238. #define QK_NL 16
  7239. //
  7240. // get rows
  7241. //
  7242. typedef decltype(kernel_get_rows_f<float>) get_rows_f_t;
  7243. template [[host_name("kernel_get_rows_f32")]] kernel get_rows_f_t kernel_get_rows_f<float>;
  7244. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_f_t kernel_get_rows_f<half>;
  7245. #if defined(GGML_METAL_USE_BF16)
  7246. template [[host_name("kernel_get_rows_bf16")]] kernel get_rows_f_t kernel_get_rows_f<bfloat>;
  7247. #endif
  7248. typedef decltype(kernel_get_rows_q<block_q4_0, 2, dequantize_q4_0>) get_rows_q_t;
  7249. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q4_0, 2, dequantize_q4_0>;
  7250. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_q_t kernel_get_rows_q<block_q4_1, 2, dequantize_q4_1>;
  7251. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q5_0, 2, dequantize_q5_0>;
  7252. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_q_t kernel_get_rows_q<block_q5_1, 2, dequantize_q5_1>;
  7253. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q8_0, 2, dequantize_q8_0>;
  7254. 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>;
  7255. 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>;
  7256. 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>;
  7257. 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>;
  7258. 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>;
  7259. 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>;
  7260. 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>;
  7261. 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>;
  7262. 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>;
  7263. 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>;
  7264. 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>;
  7265. 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>;
  7266. template [[host_name("kernel_get_rows_iq4_nl")]] kernel get_rows_q_t kernel_get_rows_q<block_iq4_nl, 2, dequantize_iq4_nl>;
  7267. 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>;
  7268. //
  7269. // matrix-matrix multiplication
  7270. //
  7271. typedef decltype(kernel_mul_mm<half, half4x4, simdgroup_half8x8, float4x4, 1, dequantize_f32>) mat_mm_t;
  7272. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, float4x4, 1, dequantize_f32>;
  7273. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half4x4, 1, dequantize_f16>;
  7274. #if defined(GGML_METAL_USE_BF16)
  7275. template [[host_name("kernel_mul_mm_bf16_f32")]] kernel mat_mm_t kernel_mul_mm<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat4x4, 1, dequantize_bf16>;
  7276. #endif
  7277. 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>;
  7278. 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>;
  7279. 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>;
  7280. 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>;
  7281. 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>;
  7282. 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>;
  7283. 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>;
  7284. 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>;
  7285. 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>;
  7286. 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>;
  7287. 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>;
  7288. 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>;
  7289. 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>;
  7290. 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>;
  7291. 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>;
  7292. 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>;
  7293. 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>;
  7294. 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>;
  7295. 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>;
  7296. //
  7297. // indirect matrix-matrix multiplication
  7298. //
  7299. typedef decltype(kernel_mul_mm_id<float4x4, 1, dequantize_f32>) mat_mm_id_t;
  7300. template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<float4x4, 1, dequantize_f32>;
  7301. template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<half4x4, 1, dequantize_f16>;
  7302. #if defined(GGML_METAL_USE_BF16)
  7303. template [[host_name("kernel_mul_mm_id_bf16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<bfloat4x4, 1, dequantize_bf16>;
  7304. #endif
  7305. 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>;
  7306. 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>;
  7307. 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>;
  7308. 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>;
  7309. 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>;
  7310. 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>;
  7311. 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>;
  7312. 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>;
  7313. 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>;
  7314. 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>;
  7315. 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>;
  7316. 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>;
  7317. 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>;
  7318. 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>;
  7319. 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>;
  7320. 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>;
  7321. 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>;
  7322. 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>;
  7323. 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>;
  7324. //
  7325. // matrix-vector multiplication
  7326. //
  7327. typedef void (kernel_mul_mv_impl_t)(
  7328. ggml_metal_kargs_mul_mv args,
  7329. device const char * src0,
  7330. device const char * src1,
  7331. device char * dst,
  7332. uint3 tgpig,
  7333. ushort tiisg);
  7334. typedef void (kernel_mul_mv2_impl_t)(
  7335. ggml_metal_kargs_mul_mv args,
  7336. device const char * src0,
  7337. device const char * src1,
  7338. device char * dst,
  7339. threadgroup char * shmem,
  7340. uint3 tgpig,
  7341. ushort tiisg,
  7342. ushort sgitg);
  7343. template<kernel_mul_mv_impl_t impl_fn>
  7344. void mmv_fn(
  7345. ggml_metal_kargs_mul_mv args,
  7346. device const char * src0,
  7347. device const char * src1,
  7348. device char * dst,
  7349. threadgroup char * shmem,
  7350. uint3 tgpig,
  7351. ushort tiitg,
  7352. ushort tiisg,
  7353. ushort sgitg) {
  7354. impl_fn(args, src0, src1, dst, tgpig, tiisg);
  7355. }
  7356. template<kernel_mul_mv2_impl_t impl_fn>
  7357. void mmv_fn(
  7358. ggml_metal_kargs_mul_mv args,
  7359. device const char * src0,
  7360. device const char * src1,
  7361. device char * dst,
  7362. threadgroup char * shmem,
  7363. uint3 tgpig,
  7364. ushort tiitg,
  7365. ushort tiisg,
  7366. ushort sgitg) {
  7367. impl_fn(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  7368. }
  7369. typedef decltype(mmv_fn<kernel_mul_mv_impl<half, half4, half, half4, ggml_metal_kargs_mul_mv>>) mul_mv_impl_fn_t;
  7370. template<mul_mv_impl_fn_t impl_fn>
  7371. kernel void kernel_mul_mv_id(
  7372. constant ggml_metal_kargs_mul_mv_id & args,
  7373. device const char * src0s,
  7374. device const char * src1,
  7375. device char * dst,
  7376. device const char * ids,
  7377. threadgroup char * shmem [[threadgroup(0)]],
  7378. uint3 tgpig[[threadgroup_position_in_grid]],
  7379. ushort tiitg[[thread_index_in_threadgroup]],
  7380. ushort tiisg[[thread_index_in_simdgroup]],
  7381. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  7382. const int iid1 = tgpig.z/args.nei0;
  7383. const int idx = tgpig.z%args.nei0;
  7384. tgpig.z = 0;
  7385. const int32_t i02 = ((device const int32_t *) (ids + iid1*args.nbi1))[idx];
  7386. const int64_t i11 = idx % args.ne11;
  7387. const int64_t i12 = iid1;
  7388. const int64_t i1 = idx;
  7389. const int64_t i2 = i12;
  7390. device const char * src0_cur = src0s + i02*args.nb02;
  7391. device const char * src1_cur = src1 + i11*args.nb11 + i12*args.nb12;
  7392. device char * dst_cur = dst + (i1*args.ne0 + i2*args.ne1*args.ne0)*sizeof(float);
  7393. ggml_metal_kargs_mul_mv args0 = {
  7394. /*.ne00 =*/ args.ne00,
  7395. /*.ne01 =*/ args.ne01,
  7396. /*.ne02 =*/ 1, // args.ne02,
  7397. /*.nb00 =*/ args.nb00,
  7398. /*.nb01 =*/ args.nb01,
  7399. /*.nb02 =*/ args.nb02,
  7400. /*.nb03 =*/ args.nb02, // args.ne02 == 1
  7401. /*.ne10 =*/ args.ne10,
  7402. /*.ne11 =*/ 1, // args.ne11,
  7403. /*.ne12 =*/ 1, // args.ne12,
  7404. /*.nb10 =*/ args.nb10,
  7405. /*.nb11 =*/ args.nb11,
  7406. /*.nb12 =*/ args.nb12,
  7407. /*.nb13 =*/ args.nb12, // ne12 == 1
  7408. /*.ne0 =*/ args.ne0,
  7409. /*.ne1 =*/ 1, // args.ne1,
  7410. /*.r2 =*/ 1,
  7411. /*.r3 =*/ 1,
  7412. };
  7413. impl_fn(
  7414. args0,
  7415. /* src0 */ src0_cur,
  7416. /* src1 */ src1_cur,
  7417. /* dst */ dst_cur,
  7418. shmem,
  7419. tgpig,
  7420. tiitg,
  7421. tiisg,
  7422. sgitg);
  7423. }
  7424. typedef decltype(kernel_mul_mv_id<mmv_fn<kernel_mul_mv_impl<float, float4, float, float4>>>) kernel_mul_mv_id_t;
  7425. 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>>>;
  7426. 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>>>;
  7427. #if defined(GGML_METAL_USE_BF16)
  7428. 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>>>;
  7429. #endif
  7430. 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>>;
  7431. 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>>>;
  7432. 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>>>;
  7433. 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>>>;
  7434. 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>>>;
  7435. 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>>;
  7436. 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>>;
  7437. 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>>;
  7438. 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>>;
  7439. 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>>;
  7440. 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>>;
  7441. 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>>;
  7442. 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>>;
  7443. 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>>;
  7444. 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>>;
  7445. 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>>;
  7446. 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>>;
  7447. 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>>;
  7448. 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>>;
  7449. kernel void kernel_pool_2d_max_f32(
  7450. device const float * src0,
  7451. device float * dst,
  7452. constant int32_t & k0,
  7453. constant int32_t & k1,
  7454. constant int32_t & s0,
  7455. constant int32_t & s1,
  7456. constant int32_t & p0,
  7457. constant int32_t & p1,
  7458. constant int64_t & IH,
  7459. constant int64_t & IW,
  7460. constant int64_t & OH,
  7461. constant int64_t & OW,
  7462. constant int64_t & parallel_elements,
  7463. uint gid[[thread_position_in_grid]]) {
  7464. if (gid >= parallel_elements) {
  7465. return;
  7466. }
  7467. const int idx = gid;
  7468. const int I_HW = IH * IW;
  7469. const int O_HW = OH * OW;
  7470. const int nc = idx / O_HW;
  7471. const int cur_oh = idx % O_HW / OW;
  7472. const int cur_ow = idx % O_HW % OW;
  7473. device const float * i_ptr = src0 + nc * I_HW;
  7474. device float * o_ptr = dst + nc * O_HW;
  7475. const int start_h = cur_oh * s1 - p1;
  7476. const int bh = MAX(0, start_h);
  7477. const int eh = MIN(IH, start_h + k1);
  7478. const int start_w = cur_ow * s0 - p0;
  7479. const int bw = MAX(0, start_w);
  7480. const int ew = MIN(IW, start_w + k0);
  7481. float res = -INFINITY;
  7482. for (int i = bh; i < eh; i += 1) {
  7483. for (int j = bw; j < ew; j += 1) {
  7484. res = MAX(res, i_ptr[i * IW + j]);
  7485. }
  7486. }
  7487. o_ptr[cur_oh * OW + cur_ow] = res;
  7488. }
  7489. kernel void kernel_pool_2d_avg_f32(
  7490. device const float * src0,
  7491. device float * dst,
  7492. constant int32_t & k0,
  7493. constant int32_t & k1,
  7494. constant int32_t & s0,
  7495. constant int32_t & s1,
  7496. constant int32_t & p0,
  7497. constant int32_t & p1,
  7498. constant int64_t & IH,
  7499. constant int64_t & IW,
  7500. constant int64_t & OH,
  7501. constant int64_t & OW,
  7502. constant int64_t & parallel_elements,
  7503. uint gid[[thread_position_in_grid]]) {
  7504. if (gid >= parallel_elements) {
  7505. return;
  7506. }
  7507. const int idx = gid;
  7508. const int I_HW = IH * IW;
  7509. const int O_HW = OH * OW;
  7510. const int nc = idx / O_HW;
  7511. const int cur_oh = idx % O_HW / OW;
  7512. const int cur_ow = idx % O_HW % OW;
  7513. device const float * i_ptr = src0 + nc * I_HW;
  7514. device float * o_ptr = dst + nc * O_HW;
  7515. const int start_h = cur_oh * s1 - p1;
  7516. const int bh = MAX(0, start_h);
  7517. const int eh = MIN(IH, start_h + k1);
  7518. const int start_w = cur_ow * s0 - p0;
  7519. const int bw = MAX(0, start_w);
  7520. const int ew = MIN(IW, start_w + k0);
  7521. // const float scale = 1. / ((eh - bh) * (ew - bw));
  7522. const float scale = 1. / (k0 * k1);
  7523. float res = 0;
  7524. for (int i = bh; i < eh; i += 1) {
  7525. for (int j = bw; j < ew; j += 1) {
  7526. float cur = i_ptr[i * IW + j];
  7527. res += cur * scale;
  7528. }
  7529. }
  7530. o_ptr[cur_oh * OW + cur_ow] = res;
  7531. }